001/*
002 *  Copyright 2021 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.ArrayList;
019import java.util.HashMap;
020import java.util.List;
021import java.util.Map;
022
023import org.apache.avalon.framework.parameters.Parameters;
024import org.apache.avalon.framework.service.ServiceException;
025import org.apache.avalon.framework.service.ServiceManager;
026import org.apache.cocoon.acting.ServiceableAction;
027import org.apache.cocoon.environment.ObjectModelHelper;
028import org.apache.cocoon.environment.Redirector;
029import org.apache.cocoon.environment.Request;
030import org.apache.cocoon.environment.SourceResolver;
031
032import org.ametys.core.cocoon.JSonReader;
033import org.ametys.plugins.forms.dao.FormPageDAO;
034import org.ametys.plugins.forms.dao.FormQuestionDAO;
035import org.ametys.plugins.forms.repository.Form;
036import org.ametys.plugins.forms.repository.FormPage;
037import org.ametys.plugins.forms.repository.FormQuestion;
038import org.ametys.plugins.repository.AmetysObject;
039import org.ametys.plugins.repository.AmetysObjectResolver;
040
041/**
042 * Get the form structure to built tree
043 */
044public class GetFormStructureAction extends ServiceableAction
045{
046    /** The Ametys object resolver */
047    protected AmetysObjectResolver _resolver;
048    
049    /** DAO for manipulating form pages */
050    protected FormPageDAO _formPageDAO;
051    
052    /** DAO for manipulating form pages */
053    protected FormQuestionDAO _formQuestionDAO;
054    
055    @Override
056    public void service(ServiceManager serviceManager) throws ServiceException
057    {
058        super.service(serviceManager);
059        _resolver = (AmetysObjectResolver) serviceManager.lookup(AmetysObjectResolver.ROLE);
060        _formPageDAO = (FormPageDAO) serviceManager.lookup(FormPageDAO.ROLE);
061        _formQuestionDAO = (FormQuestionDAO) serviceManager.lookup(FormQuestionDAO.ROLE);
062    }
063    
064    public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception
065    {
066        @SuppressWarnings("unchecked")
067        Map<String, Object> jsParameters = (Map<String, Object>) objectModel.get(ObjectModelHelper.PARENT_CONTEXT);
068        
069        AmetysObject currentNode = _resolver.resolveById((String) jsParameters.get("node"));
070        
071        List<Map<String, Object>> nodes = new ArrayList<>();
072        if (currentNode instanceof Form)
073        {
074            for (FormPage page : ((Form) currentNode).getPages())
075            {
076                nodes.add(_formPageDAO.getPageProperties(page));
077            }
078        }
079        else if (currentNode instanceof FormPage) 
080        {
081            for (FormQuestion question : ((FormPage) currentNode).getQuestions())
082            {
083                nodes.add(_formQuestionDAO.getQuestionProperties(question));
084            }
085        }
086        
087        Map<String, Object> result = new HashMap<>();
088        result.put("pages", nodes);
089        
090        Request request = ObjectModelHelper.getRequest(objectModel);
091        request.setAttribute(JSonReader.OBJECT_TO_READ, result);
092        
093        return EMPTY_MAP;
094    }
095}