001/*
002 *  Copyright 2022 Anyware Services
003 *
004 *  Licensed under the Apache License, Version 2.0 (the "License");
005 *  you may not use this file except in compliance with the License.
006 *  You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 *  Unless required by applicable law or agreed to in writing, software
011 *  distributed under the License is distributed on an "AS IS" BASIS,
012 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *  See the License for the specific language governing permissions and
014 *  limitations under the License.
015 */
016package org.ametys.plugins.forms.workflow;
017
018import java.io.IOException;
019
020import org.apache.avalon.framework.service.ServiceException;
021import org.apache.avalon.framework.service.ServiceManager;
022import org.apache.cocoon.ProcessingException;
023import org.apache.cocoon.generation.ServiceableGenerator;
024import org.apache.cocoon.xml.AttributesImpl;
025import org.apache.cocoon.xml.XMLUtils;
026import org.xml.sax.SAXException;
027
028import org.ametys.plugins.workflow.support.WorkflowHelper;
029
030/**
031 * This class generates the ids of the extensions used for each workflow tab
032 */
033public abstract class AbstractFormEntriesWorkflowsGenerator extends ServiceableGenerator
034{
035    /** The beginning of the id every workflow tab extension */
036    private static final String __FORM_WORKFLOW_TAB_EXTENSION_ID = "form-workflow-tab";
037    
038    /** The Workflow component for form entries */
039    protected WorkflowHelper _workflowHelper;
040    
041    @Override
042    public void service(ServiceManager serviceManager) throws ServiceException
043    {
044        _workflowHelper = (WorkflowHelper) serviceManager.lookup(WorkflowHelper.ROLE);
045    }
046    
047    @Override
048    public void generate() throws IOException, SAXException, ProcessingException
049    {
050        String[] formWorkflowNames = _workflowHelper.getWorkflowNames();
051        
052        contentHandler.startDocument();
053        
054        AttributesImpl rootAttrs = new AttributesImpl();
055        rootAttrs.addCDATAAttribute("tabLabelKey", _getRibbonTabKeyToOverride());
056        XMLUtils.startElement(contentHandler, "root", rootAttrs);
057        
058        AttributesImpl workflowTabAtts = new AttributesImpl();
059        workflowTabAtts.addCDATAAttribute("tabId", __FORM_WORKFLOW_TAB_EXTENSION_ID);
060
061        AttributesImpl groupAtts = new AttributesImpl();
062        groupAtts.addCDATAAttribute("label", "plugin.forms:RIBBON_TABS_TAB_FORM_ENTRY_WORKFLOW_LABEL");
063        groupAtts.addCDATAAttribute("order", "2");
064        XMLUtils.startElement(contentHandler, "workflow-tab", workflowTabAtts);
065        XMLUtils.startElement(contentHandler, "group", groupAtts);
066        for (String workflowName : formWorkflowNames)
067        {
068            if (workflowName.startsWith(_getPrefixWorkflowName()))
069            {
070                AttributesImpl attrs = new AttributesImpl();
071                attrs.addCDATAAttribute("id", _getPrefixWorkflowButton() + workflowName);
072                XMLUtils.createElement(contentHandler, "control", attrs);
073            }
074        }
075        XMLUtils.endElement(contentHandler, "group");
076        XMLUtils.endElement(contentHandler, "workflow-tab");
077        XMLUtils.endElement(contentHandler, "root");
078        
079        contentHandler.endDocument();
080    }
081    
082    /**
083     * Get prefix of workflows
084     * @return the prefix of workflows
085     */
086    protected abstract String _getPrefixWorkflowName();
087    
088    /**
089     * Get prefix of workflow buttons
090     * @return the prefix of workflow buttons
091     */
092    protected abstract String _getPrefixWorkflowButton();
093    
094    /**
095     * Get label key of ribbon tab to override
096     * @return the label key of ribbon tab to override
097     */
098    protected abstract String _getRibbonTabKeyToOverride();
099}