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;
019import java.util.Map;
020
021import org.apache.avalon.framework.service.ServiceException;
022import org.apache.avalon.framework.service.ServiceManager;
023import org.apache.cocoon.ProcessingException;
024import org.apache.cocoon.environment.ObjectModelHelper;
025import org.apache.cocoon.environment.Request;
026import org.apache.cocoon.xml.AttributesImpl;
027import org.apache.cocoon.xml.XMLUtils;
028import org.xml.sax.SAXException;
029
030import org.ametys.core.user.CurrentUserProvider;
031import org.ametys.core.user.UserIdentity;
032import org.ametys.plugins.forms.Form;
033import org.ametys.plugins.forms.FormsException;
034import org.ametys.plugins.forms.data.FieldValue;
035import org.ametys.plugins.forms.data.UserEntry;
036import org.ametys.plugins.forms.processing.AbstractFormFieldGenerator;
037import org.ametys.plugins.forms.table.FormTableManager;
038import org.ametys.runtime.authentication.AccessDeniedException;
039
040/**
041 * Generate the entry of a form
042 */
043public class FormEntryInformationGenerator extends AbstractFormFieldGenerator
044{
045    /** The form data manager. */
046    protected FormTableManager _formTableManager;
047    
048    /** The current user provider */
049    protected CurrentUserProvider _currentUserProvider;
050    
051    @Override
052    public void service(ServiceManager smanager) throws ServiceException
053    {
054        super.service(smanager);
055        _formTableManager = (FormTableManager) smanager.lookup(FormTableManager.ROLE);
056        _currentUserProvider = (CurrentUserProvider) smanager.lookup(CurrentUserProvider.ROLE);
057    }
058    
059    public void generate() throws IOException, SAXException, ProcessingException
060    {
061        Request request = ObjectModelHelper.getRequest(objectModel);
062
063        String siteName = (String) request.get("siteName");
064        String formId = (String) request.get("formId");
065        String entryId = (String) request.get("entryId");
066        
067        contentHandler.startDocument();
068        
069        try
070        {
071            Form form = _formPropManager.getForm(siteName, formId);
072            if (form == null)
073            {
074                throw new ProcessingException("The form of ID '" + formId + " can't be found for site '" + siteName + "'.");
075            }
076            
077            Map<String, FieldValue> columns = _formTableManager.getColumns(form);
078            UserEntry entry = _formTableManager.getSubmission(form, columns, Integer.parseInt(entryId));
079            
080            UserIdentity currentUser = _currentUserProvider.getUser();
081            if (!entry.getUserIdentity().equals(currentUser))
082            {
083                throw new AccessDeniedException("User '" + currentUser + "' is not allowed to access to user entry data.");
084            }
085            
086            AttributesImpl atts = new AttributesImpl();
087            atts.addCDATAAttribute("id", entryId);
088            atts.addCDATAAttribute("formId", formId);
089            atts.addCDATAAttribute("siteName", siteName);
090            
091            XMLUtils.startElement(contentHandler, "entry", atts);
092            for (FieldValue fdValue : entry.getValues())
093            {
094                saxFieldValue(fdValue);
095            }
096            XMLUtils.endElement(contentHandler, "entry");
097        }
098        catch (FormsException | SAXException e) 
099        {
100            getLogger().error("An error occurred saxing entry information for form '"  + formId + "' and entry '" + entryId + "'", e);
101        }
102        
103        contentHandler.endDocument();
104    }
105
106}