001/*
002 *  Copyright 2018 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.minisurvey;
017
018import java.util.HashMap;
019import java.util.List;
020import java.util.Map;
021
022import org.apache.avalon.framework.parameters.Parameters;
023import org.apache.avalon.framework.service.ServiceException;
024import org.apache.avalon.framework.service.ServiceManager;
025import org.apache.cocoon.environment.ObjectModelHelper;
026import org.apache.cocoon.environment.Redirector;
027import org.apache.cocoon.environment.Request;
028import org.apache.cocoon.environment.Response;
029import org.apache.cocoon.environment.SourceResolver;
030import org.apache.commons.lang.StringUtils;
031
032import org.ametys.core.cocoon.JSonReader;
033import org.ametys.plugins.survey.answer.ProcessInputAction;
034import org.ametys.plugins.survey.answer.SurveyErrors;
035import org.ametys.plugins.survey.dao.SurveyDAO;
036import org.ametys.plugins.survey.repository.Survey;
037import org.ametys.runtime.i18n.I18nizableText;
038
039/**
040 * Submit a mini-survey
041 *
042 */
043public class ProcessMiniSurveyAction extends ProcessInputAction
044{
045    private SurveyDAO _surveyDAO;
046
047    @Override
048    public void service(ServiceManager serviceManager) throws ServiceException
049    {
050        super.service(serviceManager);
051        _surveyDAO = (SurveyDAO) serviceManager.lookup(SurveyDAO.ROLE);
052    }
053    
054    @Override
055    public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception
056    {
057        Map<String, Object> result = new HashMap<>();
058        
059        Request request = ObjectModelHelper.getRequest(objectModel);
060        Response response = ObjectModelHelper.getResponse(objectModel);
061        
062        _pluginName = getPluginName(request);
063        
064        String surveyId = request.getParameter("surveyId");
065        if (StringUtils.isNotEmpty(surveyId))
066        {
067            // Get the survey object.
068            Survey survey = _resolver.resolveById(surveyId);
069            
070            SurveyErrors errors = new SurveyErrors();
071            
072            if (!checkAccess(survey, request, errors))
073            {
074                result.put("success", false);
075                result.put("access-error", true);
076                List<I18nizableText> errorMsg = errors.getErrors().get("survey-access");
077                if (errorMsg != null && !errorMsg.isEmpty())
078                {
079                    result.put("error-msg", errorMsg.get(0));
080                }
081            }
082            else
083            {
084                // Get the user input.
085                SurveyInput surveyInput = getInput(survey, request);
086                
087                // Validate the user input.
088                validateInput(survey, surveyInput, errors, request);
089                
090                // If there were errors in the input, store it as a request attribute and stop.
091                if (errors.hasErrors())
092                {
093                    result.put("success", false);
094                    result.put("input-error", true);
095                }
096                else
097                {
098                    // Add the user session into the database.
099                    _answerDao.addSession(surveyInput);
100                    
101                    setCookie(request, response, surveyId);
102                    
103                    result.put("success", true);
104                    result.put("results", _surveyDAO.getStatistics(surveyId));
105                }
106            }
107        }
108        
109        request.setAttribute(JSonReader.OBJECT_TO_READ, result);
110        return EMPTY_MAP;
111    }
112    
113}