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.generators;
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.environment.ObjectModelHelper;
024import org.apache.cocoon.environment.Request;
025import org.apache.cocoon.xml.AttributesImpl;
026import org.apache.cocoon.xml.XMLUtils;
027import org.xml.sax.SAXException;
028
029import org.ametys.core.right.RightManager;
030import org.ametys.core.right.RightManager.RightResult;
031import org.ametys.core.util.I18nUtils;
032import org.ametys.plugins.forms.dao.FormEntryDAO;
033import org.ametys.plugins.forms.repository.Form;
034import org.ametys.plugins.forms.repository.FormEntry;
035import org.ametys.plugins.workflow.support.WorkflowProvider;
036import org.ametys.plugins.workflow.support.WorkflowProvider.AmetysObjectWorkflow;
037import org.ametys.runtime.authentication.AccessDeniedException;
038import org.ametys.runtime.i18n.I18nizableText;
039
040import com.opensymphony.workflow.loader.StepDescriptor;
041import com.opensymphony.workflow.loader.WorkflowDescriptor;
042import com.opensymphony.workflow.spi.Step;
043
044/**
045 * Generate the entry of a form for xls export
046 */
047public class FormEntriesGenerator extends FormEntryInformationGenerator
048{
049    /** The workflow provider */
050    protected WorkflowProvider _workflowProvider;
051    
052    /** I18n Utils */
053    protected I18nUtils _i18nUtils;
054    
055    /** The right manager */
056    protected RightManager _rightManager;
057    
058    @Override
059    public void service(ServiceManager smanager) throws ServiceException
060    {
061        super.service(smanager);
062        _workflowProvider = (WorkflowProvider) smanager.lookup(WorkflowProvider.ROLE);
063        _i18nUtils = (I18nUtils) smanager.lookup(I18nUtils.ROLE);
064        _rightManager = (RightManager) smanager.lookup(RightManager.ROLE);
065    }
066    
067    @Override
068    public void generate() throws IOException, SAXException, ProcessingException
069    {
070        Request request = ObjectModelHelper.getRequest(objectModel);
071        String formId = (String) request.get("id");
072        
073        contentHandler.startDocument();
074        
075        Form form = _resolver.resolveById(formId);
076        if (_rightManager.currentUserHasRight(FormEntryDAO.HANDLE_FORMS_ENTRIES_RIGHT_ID, form) != RightResult.RIGHT_ALLOW)
077        {
078            throw new AccessDeniedException("User '" + _currentUserProvider.getUser() + "' is not allowed to access to form data.");
079        }
080        
081        XMLUtils.startElement(contentHandler, "form-entries");
082        
083        for (FormEntry entry : form.getEntries())
084        {
085            _saxEntry(entry);
086        }
087        XMLUtils.endElement(contentHandler, "form-entries");
088        
089        contentHandler.endDocument();
090    }
091    
092    @Override
093    protected void _checkRights(FormEntry entry)
094    {
095        // Do nothing ... rights is checked only one time before
096    }
097    
098    @Override
099    protected void _addAdditionalEntryAttributes(FormEntry entry, AttributesImpl attrs)
100    {
101        super._addAdditionalEntryAttributes(entry, attrs);
102        
103        Form form = entry.getForm();
104        if (form.hasWorkflow())
105        {
106            AmetysObjectWorkflow workflow = _workflowProvider.getAmetysObjectWorkflow(entry);
107            WorkflowDescriptor workflowDescriptor = workflow.getWorkflowDescriptor(form.getWorkflowName());
108            Step currentStep = (Step) workflow.getCurrentSteps(entry.getWorkflowId()).iterator().next();
109            
110            StepDescriptor stepDescriptor = workflowDescriptor.getStep(currentStep.getStepId());
111            I18nizableText workflowStepName = new I18nizableText("application", stepDescriptor.getName());
112            attrs.addCDATAAttribute("status", _i18nUtils.translate(workflowStepName));
113        }
114    }
115}