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