001/*
002 *  Copyright 2012 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.io.IOException;
019import java.util.Map;
020import java.util.Set;
021import java.util.TreeMap;
022
023import org.apache.avalon.framework.service.ServiceException;
024import org.apache.avalon.framework.service.ServiceManager;
025import org.apache.avalon.framework.service.Serviceable;
026import org.apache.cocoon.ProcessingException;
027import org.apache.cocoon.generation.AbstractGenerator;
028import org.apache.cocoon.xml.AttributesImpl;
029import org.apache.cocoon.xml.XMLUtils;
030import org.xml.sax.SAXException;
031
032import org.ametys.core.util.I18nUtils;
033import org.ametys.core.util.I18nizableTextComparator;
034import org.ametys.runtime.config.ConfigManager;
035import org.ametys.runtime.config.ConfigParameter;
036import org.ametys.runtime.config.ConfigParameterCategory;
037import org.ametys.runtime.config.ConfigParameterCheckerDescriptor;
038import org.ametys.runtime.config.ConfigParameterGroup;
039import org.ametys.runtime.i18n.I18nizableText;
040import org.ametys.runtime.parameter.ParameterHelper;
041
042
043/**
044 * SAX the configuration model with current values of configuration
045 */
046public class ConfigGenerator extends AbstractGenerator implements Serviceable
047{
048    private I18nUtils _i18nUtils;
049    
050    public void service(ServiceManager manager) throws ServiceException
051    {
052        _i18nUtils = (I18nUtils) manager.lookup(I18nUtils.ROLE);
053    }
054    
055    public void generate() throws IOException, SAXException, ProcessingException
056    {
057        contentHandler.startDocument();
058        XMLUtils.startElement(contentHandler, "config");
059        
060        ConfigManager configManager = ConfigManager.getInstance();
061
062        XMLUtils.startElement(contentHandler, "configuration");
063        Map<I18nizableText, ConfigParameterCategory> categories = _sortCategories(configManager.getCategories());
064        _saxConfiguration(categories, configManager.getParameterCheckers());
065        XMLUtils.endElement(contentHandler, "configuration");
066        
067        XMLUtils.startElement(contentHandler, "configuration-values");
068        _saxValues(configManager.getValues());
069        XMLUtils.endElement(contentHandler, "configuration-values");
070        
071        XMLUtils.endElement(contentHandler, "config");
072        contentHandler.endDocument();
073    }
074    
075    private void _saxValues(Map<String, Object> values) throws SAXException
076    {
077        XMLUtils.startElement(contentHandler, "values");
078        
079        for (String parameterId : values.keySet())
080        {
081            Object value = values.get(parameterId);
082            
083            if (value != null)
084            {
085                XMLUtils.createElement(contentHandler, parameterId, ParameterHelper.valueToString(value)); 
086            }
087        }
088        
089        XMLUtils.endElement(contentHandler, "values");
090    }
091    
092    private  void _saxConfiguration(Map<I18nizableText, ConfigParameterCategory> categories, Map<String, ConfigParameterCheckerDescriptor> checkers) throws SAXException, ProcessingException
093    {
094        for (I18nizableText categoryKey : categories.keySet())
095        {
096            AttributesImpl tabAttrs = new AttributesImpl();
097            tabAttrs.addCDATAAttribute("role", "tabs");
098            XMLUtils.startElement(contentHandler, "fieldsets", tabAttrs);
099            
100            categoryKey.toSAX(contentHandler, "label");
101            
102            ConfigParameterCategory category = categories.get(categoryKey);
103            
104            // Category checkers
105            Set<ConfigParameterCheckerDescriptor> paramCheckersCategories = category.getParamCheckers();
106            if (paramCheckersCategories != null)
107            {
108                for (ConfigParameterCheckerDescriptor paramChecker : paramCheckersCategories)
109                {
110                    XMLUtils.startElement(contentHandler, "field-checker");
111                    paramChecker.toSAX(contentHandler);
112                    XMLUtils.endElement(contentHandler, "field-checker");
113                }
114            }
115            
116            XMLUtils.startElement(contentHandler, "elements");
117
118            for (I18nizableText groupKey : category.getGroups().keySet())
119            {
120                ConfigParameterGroup group = category.getGroups().get(groupKey);
121
122                AttributesImpl fieldsetAttrs = new AttributesImpl();
123                fieldsetAttrs.addCDATAAttribute("role", "fieldset");
124                XMLUtils.startElement(contentHandler, "fieldsets", fieldsetAttrs);
125
126                groupKey.toSAX(contentHandler, "label");
127
128                // Group checkers
129                Set<ConfigParameterCheckerDescriptor> paramCheckersGroups = group.getParamCheckers();
130                if (paramCheckersGroups != null)
131                {
132                    for (ConfigParameterCheckerDescriptor paramChecker : paramCheckersGroups)
133                    {
134                        XMLUtils.startElement(contentHandler, "field-checker");
135                        paramChecker.toSAX(contentHandler);
136                        XMLUtils.endElement(contentHandler, "field-checker");
137                    }
138                }
139                
140                // Group switch
141                String switchId = null;
142                if (group.getSwitch() != null)
143                {
144                    switchId = group.getSwitch();
145                    ConfigParameter switcher = group.getParameter(switchId);
146
147                    XMLUtils.startElement(contentHandler, "switcher");
148                    XMLUtils.createElement(contentHandler, "id", switchId); 
149                    switcher.getLabel().toSAX(contentHandler, "label");
150                    XMLUtils.createElement(contentHandler, "default-value", switcher.getDefaultValue() != null ? ParameterHelper.valueToString(switcher.getDefaultValue()) : "false");
151                    XMLUtils.endElement(contentHandler, "switcher");
152                }
153                
154                XMLUtils.startElement(contentHandler, "elements");
155                for (ConfigParameter param : group.getParams(false))
156                {
157                    String paramId = param.getId();
158                    
159                    XMLUtils.startElement(contentHandler, paramId);   
160                    
161                    ParameterHelper.toSAXParameterInternal(contentHandler, param, null);
162                    
163                    // Disable condition
164                    if (param.getDisableConditions() != null)
165                    {
166                        XMLUtils.createElement(contentHandler, "disable-conditions", param.disableConditionsToJSON());
167                    }
168                    
169                    // Sax parameter checkers attached to a single parameter
170                    for (String paramCheckerId : checkers.keySet())
171                    {
172                        ConfigParameterCheckerDescriptor paramChecker = checkers.get(paramCheckerId);
173                        String uiRefParamId = paramChecker.getUiRefParamId();
174                        if (uiRefParamId != null && uiRefParamId.equals(paramId))
175                        {
176                            XMLUtils.startElement(contentHandler, "field-checker");
177                            paramChecker.toSAX(contentHandler);
178                            XMLUtils.endElement(contentHandler, "field-checker");
179                        }
180                    }
181                    
182                    XMLUtils.endElement(contentHandler, paramId); 
183                }
184                XMLUtils.endElement(contentHandler, "elements");
185
186                XMLUtils.endElement(contentHandler, "fieldsets");
187            }
188            
189            XMLUtils.endElement(contentHandler, "elements");
190            XMLUtils.endElement(contentHandler, "fieldsets");
191        }
192    }
193
194    private Map<I18nizableText, ConfigParameterCategory> _sortCategories(Map<I18nizableText, ConfigParameterCategory> categories)
195    {
196        TreeMap<I18nizableText, ConfigParameterCategory> sortedCategories = new TreeMap<>(new I18nizableTextComparator(_i18nUtils));
197        sortedCategories.putAll(categories);
198
199        return sortedCategories;
200    }
201}