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.web.site;
017
018import java.util.ArrayList;
019import java.util.Date;
020import java.util.HashMap;
021import java.util.LinkedHashMap;
022import java.util.List;
023import java.util.Map;
024import java.util.Map.Entry;
025
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.ProcessingException;
030import org.apache.cocoon.acting.ServiceableAction;
031import org.apache.cocoon.environment.ObjectModelHelper;
032import org.apache.cocoon.environment.Redirector;
033import org.apache.cocoon.environment.Request;
034import org.apache.cocoon.environment.SourceResolver;
035
036import org.ametys.cms.languages.Language;
037import org.ametys.cms.languages.LanguagesManager;
038import org.ametys.core.cocoon.JSonReader;
039import org.ametys.plugins.repository.AmetysObjectIterable;
040import org.ametys.runtime.i18n.I18nizableText;
041import org.ametys.runtime.parameter.Enumerator;
042import org.ametys.runtime.parameter.ParameterHelper;
043import org.ametys.runtime.parameter.ParameterHelper.ParameterType;
044import org.ametys.runtime.parameter.Validator;
045import org.ametys.web.repository.site.SiteManager;
046import org.ametys.web.repository.sitemap.Sitemap;
047
048/**
049 * Get the site configuration parameters and their values
050 */
051public class SiteConfigurationAction extends ServiceableAction
052{
053    /** The site manager. */
054    protected SiteManager _siteManager;
055    
056    /** The site configuration. */
057    protected SiteConfigurationExtensionPoint _siteConfiguration;
058    
059    /** The available languages manager */
060    protected LanguagesManager _languagesManager;
061    
062    @Override
063    public void service(ServiceManager serviceManager) throws ServiceException
064    {
065        super.service(serviceManager);
066        _siteManager = (SiteManager) serviceManager.lookup(SiteManager.ROLE);
067        _siteConfiguration = (SiteConfigurationExtensionPoint) serviceManager.lookup(SiteConfigurationExtensionPoint.ROLE);
068        _languagesManager = (LanguagesManager) serviceManager.lookup(LanguagesManager.ROLE);
069    }
070    
071    public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception
072    {
073        Request request = ObjectModelHelper.getRequest(objectModel);
074        
075        String siteName = parameters.getParameter("siteName", request.getParameter("siteName"));
076        
077        Map<String, Object> result = new HashMap<>();
078        result.put("parameters", _parameters2json(siteName));
079        result.put("values", _values2json(siteName));
080        
081        request.setAttribute(JSonReader.OBJECT_TO_READ, result);
082        return EMPTY_MAP;
083    }
084    
085    private Map<String, Object> _values2json (String siteName)
086    {
087        Map<String, Object> form = new HashMap<>();
088        Map<String, Object> values = new HashMap<>();
089        
090        Map<String, SiteParameter> parameters = _siteConfiguration.getParameters(siteName);
091        for (String name : parameters.keySet())
092        {
093            ParameterType type = parameters.get(name).getType();
094            switch (type)
095            {
096                case LONG:
097                    Long valueAsLong = _siteConfiguration.getValueAsLong(siteName, name);
098                    if (valueAsLong != null)
099                    {
100                        values.put(name, valueAsLong);
101                    }
102                    break;
103                case DOUBLE:
104                    Double valueAsDouble = _siteConfiguration.getValueAsDouble(siteName, name);
105                    if (valueAsDouble != null)
106                    {
107                        values.put(name, valueAsDouble);
108                    }
109                    break;
110                case BOOLEAN:
111                    Boolean valueAsBoolean = _siteConfiguration.getValueAsBoolean(siteName, name);
112                    if (valueAsBoolean != null)
113                    {
114                        values.put(name, valueAsBoolean);
115                    }
116                    break;
117                case DATE:
118                    Date valueAsDate = _siteConfiguration.getValueAsDate(siteName, name);
119                    if (valueAsDate != null)
120                    {
121                        values.put(name, valueAsDate);
122                    }
123                    break;
124                case STRING:
125                default:
126                    String valueAsString = _siteConfiguration.getValueAsString(siteName, name);
127                    if (valueAsString != null)
128                    {
129                        values.put(name, valueAsString);
130                    }
131                    break;
132            }
133        }
134        
135        List<String> languages = new ArrayList<>();
136        AmetysObjectIterable<Sitemap> sitemaps = _siteManager.getSite(siteName).getSitemaps();
137        for (Sitemap sitemap : sitemaps)
138        {
139            languages.add(sitemap.getName());
140        }
141        values.put("lang", languages);
142        
143        form.put("values", values);
144        return form;
145    }
146    
147    private Map<String, Object> _parameters2json (String siteName) throws ProcessingException
148    {
149        Map<String, Object> parameters = new HashMap<>();
150        
151        List<Map<String, Object>> tabs = new ArrayList<>();
152        
153        Map<I18nizableText, Map<I18nizableText, List<SiteParameter>>> siteParameters = _siteConfiguration.getCategorizedParameters(siteName);
154        
155        for (Entry<I18nizableText, Map<I18nizableText, List<SiteParameter>>> categoryEntry : siteParameters.entrySet())
156        {
157            Map<String, Object> tab = new HashMap<>();
158            tab.put("role", "tab");
159            tab.put("label", categoryEntry.getKey());
160            
161            Map<String, Object> tabElmts = new HashMap<>();
162            
163            List<Map<String, Object>> fieldsets = new ArrayList<>();
164            Map<I18nizableText, List<SiteParameter>> groups = categoryEntry.getValue();
165            for (Entry<I18nizableText, List<SiteParameter>> groupEntry : groups.entrySet())
166            {
167                Map<String, Object> fieldset = new HashMap<>();
168                fieldset.put("role", "fieldset");
169                fieldset.put("label", groupEntry.getKey());
170                fieldset.put("elements", _parameters2JsonObject(groupEntry.getValue()));
171                
172                fieldsets.add(fieldset);
173            }
174            
175            tabElmts.put("fieldsets", fieldsets);
176            tab.put("elements", tabElmts);
177            
178            tabs.add(tab);
179        }
180        
181        parameters.put("fieldsets", tabs);
182        
183        return parameters;
184    }
185    
186    private Map<String, Object> _parameters2JsonObject (List<SiteParameter> siteParameters) throws ProcessingException
187    {
188        Map<String, Object> jsonObject = new LinkedHashMap<>();
189        
190        for (SiteParameter siteParameter : siteParameters)
191        {
192            jsonObject.put(siteParameter.getId(), _parameter2JsonObject(siteParameter));
193            
194            if ("skin".equals(siteParameter.getId()))
195            {
196                // Insert available languages
197                jsonObject.put("lang", _languages2JsonObject());
198            }
199        }
200        
201        return jsonObject;
202    }
203    
204    private Map<String, Object> _parameter2JsonObject (SiteParameter siteParameter) throws ProcessingException
205    {
206        Map<String, Object> param = new HashMap<>();
207        
208        param.put("label", siteParameter.getLabel());
209        param.put("description", siteParameter.getDescription());
210        param.put("type", siteParameter.getType());
211        
212        Validator validator = siteParameter.getValidator();
213        if (validator != null)
214        {
215            param.put("validation", validator.toJson());
216        }
217        
218        String widget = siteParameter.getWidget();
219        if (widget != null)
220        {
221            param.put("widget", widget);
222        }
223        
224        Map<String, I18nizableText> widgetParameters = siteParameter.getWidgetParameters();
225        if (widgetParameters != null && widgetParameters.size() > 0)
226        {
227            param.put("widget-params", siteParameter.getWidgetParameters());
228        }
229        
230        Object defaultValue = siteParameter.getDefaultValue();
231        if (defaultValue != null)
232        {
233            param.put("default-value", siteParameter.getDefaultValue());
234        }
235
236        Enumerator enumerator = siteParameter.getEnumerator();
237        
238        if (enumerator != null)
239        {
240            try
241            {
242                List<Map<String, Object>> options = new ArrayList<>();
243                
244                for (Map.Entry<Object, I18nizableText> entry : enumerator.getEntries().entrySet())
245                {
246                    String valueAsString = ParameterHelper.valueToString(entry.getKey());
247                    I18nizableText entryLabel = entry.getValue();
248                    
249                    Map<String, Object> option = new HashMap<>();
250                    option.put("label", entryLabel != null ? entryLabel : valueAsString);
251                    option.put("value", valueAsString);
252                    options.add(option);
253                }
254                
255                param.put("enumeration", options);
256            }
257            catch (Exception e)
258            {
259                throw new ProcessingException("Unable to enumerate entries with enumerator: " + enumerator, e);
260            }
261        }
262        
263        return param;
264    }
265    
266    private Map<String, Object> _languages2JsonObject ()
267    {
268        Map<String, Object> param = new HashMap<>();
269        Map<String, String> validation = new HashMap<>();  
270        
271        param.put("label", new I18nizableText("plugin.web", "PLUGINS_WEB_ADMINISTRATOR_SITES_SITE_LANG"));
272        param.put("description", new I18nizableText("plugin.web", "PLUGINS_WEB_ADMINISTRATOR_SITES_SITE_LANG_HELP"));
273        param.put("type", ParameterType.STRING);
274        param.put("widget", "edition.combobox");
275        
276        validation.put("mandatory", "mandatory"); 
277        param.put("validation", validation);
278        
279        param.put("multiple", true);
280        
281        List<Map<String, Object>> options = new ArrayList<>();
282        
283        Map<String, Language> availableLanguages = _languagesManager.getAvailableLanguages();
284        for (Language availableLanguage : availableLanguages.values())
285        {
286            Map<String, Object> option = new HashMap<>();
287            option.put("label", availableLanguage.getLabel());
288            option.put("value", availableLanguage.getCode());
289            options.add(option);
290            
291        }
292        
293        param.put("enumeration", options);
294        
295        return param;
296    }
297
298}