001/*
002 *  Copyright 2024 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.odf.data;
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.odf.ODFHelper;
034
035/**
036 * Get the educational path information
037 *
038 */
039public class GetEducationalPathAction extends ServiceableAction
040{
041    private ODFHelper _odfHelper;
042    
043    @Override
044    public void service(ServiceManager smanager) throws ServiceException
045    {
046        super.service(smanager);
047        _odfHelper = (ODFHelper) smanager.lookup(ODFHelper.ROLE);
048    }
049    
050    @Override
051    public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception
052    {
053        @SuppressWarnings("unchecked")
054        Map<String, Object> jsParameters = (Map<String, Object>) objectModel.get(ObjectModelHelper.PARENT_CONTEXT);
055        
056        @SuppressWarnings("unchecked")
057        List<String> values = (List<String>) jsParameters.get("ids");
058        
059        Map<String, Object> result = new HashMap<>();
060        
061        List<Map<String, Object>> paths = new ArrayList<>();
062        
063        for (String pathIds : values)
064        {
065            EducationalPath educationPath = EducationalPath.of(pathIds.split(EducationalPath.PATH_SEGMENT_SEPARATOR));
066            String educationalPathAsString = _odfHelper.getEducationalPathAsString(educationPath);
067            paths.add(Map.of("id", pathIds, "title", educationalPathAsString));
068        }
069        
070        result.put("contents", paths);
071        
072        Request request = ObjectModelHelper.getRequest(objectModel);
073        request.setAttribute(JSonReader.OBJECT_TO_READ, result);
074        
075        return EMPTY_MAP;
076    }
077}