001/*
002 *  Copyright 2017 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.runtime.plugins.admin.configuration;
017
018import java.util.ArrayList;
019import java.util.Collections;
020import java.util.HashMap;
021import java.util.LinkedHashMap;
022import java.util.List;
023import java.util.Map;
024import java.util.Set;
025import java.util.TreeMap;
026
027import org.apache.avalon.framework.parameters.Parameters;
028import org.apache.avalon.framework.service.ServiceException;
029import org.apache.avalon.framework.service.ServiceManager;
030import org.apache.cocoon.ProcessingException;
031import org.apache.cocoon.acting.ServiceableAction;
032import org.apache.cocoon.environment.ObjectModelHelper;
033import org.apache.cocoon.environment.Redirector;
034import org.apache.cocoon.environment.Request;
035import org.apache.cocoon.environment.SourceResolver;
036
037import org.ametys.core.cocoon.JSonReader;
038import org.ametys.core.util.I18nUtils;
039import org.ametys.core.util.I18nizableTextComparator;
040import org.ametys.runtime.config.ConfigManager;
041import org.ametys.runtime.config.ConfigParameter;
042import org.ametys.runtime.config.ConfigParameterCategory;
043import org.ametys.runtime.config.ConfigParameterCheckerDescriptor;
044import org.ametys.runtime.config.ConfigParameterGroup;
045import org.ametys.runtime.i18n.I18nizableText;
046import org.ametys.runtime.parameter.ParameterHelper;
047
048/**
049 * Get the configuration model with current values of configuration
050 */
051public class GetConfigAction extends ServiceableAction
052{
053    private I18nUtils _i18nUtils;
054    
055    @Override
056    public void service(ServiceManager smanager) throws ServiceException
057    {
058        _i18nUtils = (I18nUtils) smanager.lookup(I18nUtils.ROLE);
059    }
060
061    public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception
062    {
063        Request request = ObjectModelHelper.getRequest(objectModel);
064        
065        Map<String, Object> jsonObject = new HashMap<>();
066        
067        ConfigManager configManager = ConfigManager.getInstance();
068        Map<I18nizableText, ConfigParameterCategory> categories = _sortCategories(configManager.getCategories());
069        
070        Map<String, Object> configuration = _getConfiguration(categories, configManager.getParameterCheckers());
071        jsonObject.put("configuration", configuration);
072        jsonObject.put("configuration-values", Collections.singletonMap("values", configManager.getValues()));
073        
074        request.setAttribute(JSonReader.OBJECT_TO_READ, jsonObject);
075
076        return EMPTY_MAP;
077    }
078    
079    private Map<I18nizableText, ConfigParameterCategory> _sortCategories(Map<I18nizableText, ConfigParameterCategory> categories)
080    {
081        TreeMap<I18nizableText, ConfigParameterCategory> sortedCategories = new TreeMap<>(new I18nizableTextComparator(_i18nUtils));
082        sortedCategories.putAll(categories);
083
084        return sortedCategories;
085    }
086    
087    private Map<String, Object> _getConfiguration(Map<I18nizableText, ConfigParameterCategory> categories, Map<String, ConfigParameterCheckerDescriptor> checkers) throws ProcessingException
088    {
089        List<Map<String, Object>> fieldsets = new ArrayList<>();
090        
091        for (I18nizableText categoryKey : categories.keySet())
092        {
093            ConfigParameterCategory category = categories.get(categoryKey);
094            fieldsets.add(_getCategoryConfig(categoryKey, category, checkers));
095        }
096        
097        return Collections.singletonMap("fieldsets", fieldsets);
098    }
099    
100    private Map<String, Object> _getCategoryConfig(I18nizableText categoryKey, ConfigParameterCategory category, Map<String, ConfigParameterCheckerDescriptor> checkers) throws ProcessingException
101    {
102        Map<String, Object> categoryConfig = new HashMap<>();
103        
104        categoryConfig.put("role", "tab");
105        categoryConfig.put("label", categoryKey);
106        
107        Map<String, Object> elmts = new LinkedHashMap<>();
108        
109        List<Map<String, Object>> fieldsets = new ArrayList<>();
110        for (I18nizableText groupKey : category.getGroups().keySet())
111        {
112            ConfigParameterGroup group = category.getGroups().get(groupKey);
113            fieldsets.add(_getGroupConfig(group, checkers));
114        }
115        
116        elmts.put("fieldsets", fieldsets);
117        categoryConfig.put("elements", elmts);
118        
119        // Category checkers
120        Set<ConfigParameterCheckerDescriptor> paramCheckersCategories = category.getParamCheckers();
121        if (paramCheckersCategories != null)
122        {
123            List<Map<String, Object>> checkers2json = new ArrayList<>();
124            for (ConfigParameterCheckerDescriptor paramChecker : paramCheckersCategories)
125            {
126                checkers2json.add(paramChecker.toJSON());
127            }
128            
129            categoryConfig.put("field-checker", checkers2json);
130        }
131        
132        return categoryConfig;
133    }
134    
135    private Map<String, Object> _getGroupConfig(ConfigParameterGroup group, Map<String, ConfigParameterCheckerDescriptor> checkers) throws ProcessingException
136    {
137        Map<String, Object> groupConfig = new HashMap<>();
138        
139        groupConfig.put("role", "fieldset");
140        groupConfig.put("label", group.getLabel());
141        
142        // Group switch
143        if (group.getSwitch() != null)
144        {
145            String switchId = group.getSwitch();
146            ConfigParameter switcher = group.getParameter(switchId);
147
148            Map<String, Object> switcher2json = new HashMap<>();
149            switcher2json.put("id", switchId);
150            switcher2json.put("label", switcher.getLabel());
151            switcher2json.put("default-value", switcher.getDefaultValue() != null ? switcher.getDefaultValue() : false);
152                
153            groupConfig.put("switcher", switcher2json);
154        }
155        
156        // Group checkers
157        Set<ConfigParameterCheckerDescriptor> paramCheckersCategories = group.getParamCheckers();
158        if (paramCheckersCategories != null)
159        {
160            List<Map<String, Object>> checkers2json = new ArrayList<>();
161            for (ConfigParameterCheckerDescriptor paramChecker : paramCheckersCategories)
162            {
163                checkers2json.add(paramChecker.toJSON());
164            }
165            
166            groupConfig.put("field-checker", checkers2json);
167        }
168        
169        // Group fields
170        Map<String, Object> elmts = new LinkedHashMap<>();
171        
172        for (ConfigParameter param : group.getParams(false))
173        {
174            String paramId = param.getId();
175            Map<String, Object> paramConfig = _getParamConfig(param, checkers);
176            elmts.put(paramId, paramConfig);
177        }
178        
179        groupConfig.put("elements", elmts);
180        
181        return groupConfig;
182    }
183    
184    private Map<String, Object> _getParamConfig(ConfigParameter param, Map<String, ConfigParameterCheckerDescriptor> checkers) throws ProcessingException
185    {
186        Map<String, Object> paramConfig = ParameterHelper.toJSON(param);
187        
188        // Disable condition
189        if (param.getDisableConditions() != null)
190        {
191            paramConfig.put("disable-conditions", param.disableConditionsToJSON());
192        }
193        
194        // Parameter checker
195        for (String paramCheckerId : checkers.keySet())
196        {
197            ConfigParameterCheckerDescriptor paramChecker = checkers.get(paramCheckerId);
198            String uiRefParamId = paramChecker.getUiRefParamId();
199            if (uiRefParamId != null && uiRefParamId.equals(param.getId()))
200            {
201                paramConfig.put("field-checker", paramChecker.toJSON());
202            }
203        }
204        
205        return paramConfig;
206    }
207
208}