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.actions;
017
018import java.util.HashMap;
019import java.util.Map;
020
021import org.apache.avalon.framework.parameters.Parameters;
022import org.apache.avalon.framework.service.ServiceException;
023import org.apache.avalon.framework.service.ServiceManager;
024import org.apache.cocoon.environment.ObjectModelHelper;
025import org.apache.cocoon.environment.Redirector;
026import org.apache.cocoon.environment.Request;
027import org.apache.cocoon.environment.SourceResolver;
028
029import org.ametys.core.cocoon.JSonReader;
030import org.ametys.plugins.forms.helper.FormStatisticsHelper;
031import org.ametys.plugins.repository.RepositoryConstants;
032import org.ametys.plugins.repository.provider.RequestAttributeWorkspaceSelector;
033
034/**
035 * Submit a mini-survey
036 */
037public class ProcessMiniSurveyAction extends ProcessFormAction
038{
039    /** The form stats helper */
040    protected FormStatisticsHelper _formStatsHelper;
041
042    @Override
043    public void service(ServiceManager serviceManager) throws ServiceException
044    {
045        super.service(serviceManager);
046        _formStatsHelper = (FormStatisticsHelper) serviceManager.lookup(FormStatisticsHelper.ROLE);
047    }
048    
049    @Override
050    public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception
051    {
052        Map<String, Object> result = new HashMap<>();
053        Request request = ObjectModelHelper.getRequest(objectModel);
054        
055        Map<String, String> formResults = _processForm(request);
056        if (formResults == null)
057        {
058            result.put("success", false);
059        }
060        else
061        {
062            // Entries are created on default workspace then copy on live. But the copy is async and on live getting results immediatly after creating the entry could return wrong results.
063            String currentWsp = RequestAttributeWorkspaceSelector.getForcedWorkspace(request);
064            try
065            {
066                // Force the workspace.
067                RequestAttributeWorkspaceSelector.setForcedWorkspace(request, RepositoryConstants.DEFAULT_WORKSPACE);
068                
069                String formId = request.getParameter("formId");
070                
071                result.put("success", true);
072                result.put("results", _formStatsHelper.getMiniSurveyStatistics(formId));
073            }
074            finally 
075            {
076                // Restore context
077                RequestAttributeWorkspaceSelector.setForcedWorkspace(request, currentWsp);
078            }
079        }
080        
081        request.setAttribute(JSonReader.OBJECT_TO_READ, result);
082        return EMPTY_MAP;
083    }
084    
085}