001/*
002 *  Copyright 2015 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.export.indesign;
017
018import java.util.ArrayList;
019import java.util.HashMap;
020import java.util.List;
021import java.util.Map;
022
023import org.apache.avalon.framework.configuration.Configurable;
024import org.apache.avalon.framework.configuration.Configuration;
025import org.apache.avalon.framework.configuration.ConfigurationException;
026import org.apache.avalon.framework.parameters.Parameters;
027import org.apache.avalon.framework.service.ServiceException;
028import org.apache.avalon.framework.service.ServiceManager;
029import org.apache.cocoon.acting.ServiceableAction;
030import org.apache.cocoon.environment.ObjectModelHelper;
031import org.apache.cocoon.environment.Redirector;
032import org.apache.cocoon.environment.Request;
033import org.apache.cocoon.environment.SourceResolver;
034
035import org.ametys.core.cocoon.JSonReader;
036import org.ametys.runtime.i18n.I18nizableText;
037
038/**
039 * Get the indesign stylesheet names and their labels
040 *
041 */
042public class GetIndesignFilesAction extends ServiceableAction implements Configurable
043{
044    /** The helper for indesign transformations */
045    private IndesignTransformationHelper _indesignTransformationHelper;
046    
047    private boolean _global;
048    
049    @Override
050    public void configure(Configuration configuration) throws ConfigurationException
051    {
052        _global = "true".equals(configuration.getChild("global").getValue("true"));
053    }
054    
055    @Override
056    public void service(ServiceManager serviceManager) throws ServiceException
057    {
058        super.service(serviceManager);
059        _indesignTransformationHelper = (IndesignTransformationHelper) serviceManager.lookup(IndesignTransformationHelper.ROLE);
060    }
061
062    public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception
063    {
064        Map<String, Object> result = new HashMap<>();
065        
066        List<Map<String, Object>> files = new ArrayList<>();
067        
068        Request request = ObjectModelHelper.getRequest(objectModel);
069        
070        Map<String, I18nizableText> xsltFiles = _global ? _indesignTransformationHelper.getIndesignGlobalXslt() : _indesignTransformationHelper.getIndesignXslt();
071        
072        for (String fileName : xsltFiles.keySet())
073        {
074            Map<String, Object> file = new HashMap<>();
075            file.put("name", fileName);
076            file.put("label", xsltFiles.get(fileName));
077            files.add(file);
078        }
079        
080        result.put("files", files);
081        
082        request.setAttribute(JSonReader.OBJECT_TO_READ, result);
083        return EMPTY_MAP;
084    }
085
086}