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.ParameterHelper;
042import org.ametys.runtime.parameter.ParameterHelper.ParameterType;
043import org.ametys.web.repository.site.SiteManager;
044import org.ametys.web.repository.sitemap.Sitemap;
045
046/**
047 * Get the site configuration parameters and their values
048 */
049public class SiteConfigurationAction extends ServiceableAction
050{
051    /** The site manager. */
052    protected SiteManager _siteManager;
053    
054    /** The site configuration. */
055    protected SiteConfigurationExtensionPoint _siteConfiguration;
056    
057    /** The available languages manager */
058    protected LanguagesManager _languagesManager;
059    
060    @Override
061    public void service(ServiceManager serviceManager) throws ServiceException
062    {
063        super.service(serviceManager);
064        _siteManager = (SiteManager) serviceManager.lookup(SiteManager.ROLE);
065        _siteConfiguration = (SiteConfigurationExtensionPoint) serviceManager.lookup(SiteConfigurationExtensionPoint.ROLE);
066        _languagesManager = (LanguagesManager) serviceManager.lookup(LanguagesManager.ROLE);
067    }
068    
069    public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception
070    {
071        Request request = ObjectModelHelper.getRequest(objectModel);
072        
073        String siteName = parameters.getParameter("siteName", request.getParameter("siteName"));
074        
075        Map<String, Object> result = new HashMap<>();
076        result.put("parameters", _parameters2json(siteName));
077        result.put("values", _values2json(siteName));
078        
079        request.setAttribute(JSonReader.OBJECT_TO_READ, result);
080        return EMPTY_MAP;
081    }
082    
083    private Map<String, Object> _values2json (String siteName)
084    {
085        Map<String, Object> form = new HashMap<>();
086        Map<String, Object> values = new HashMap<>();
087        
088        Map<String, SiteParameter> parameters = _siteConfiguration.getParameters(siteName);
089        for (String name : parameters.keySet())
090        {
091            ParameterType type = parameters.get(name).getType();
092            switch (type)
093            {
094                case LONG:
095                    Long valueAsLong = _siteConfiguration.getValueAsLong(siteName, name);
096                    if (valueAsLong != null)
097                    {
098                        values.put(name, valueAsLong);
099                    }
100                    break;
101                case DOUBLE:
102                    Double valueAsDouble = _siteConfiguration.getValueAsDouble(siteName, name);
103                    if (valueAsDouble != null)
104                    {
105                        values.put(name, valueAsDouble);
106                    }
107                    break;
108                case BOOLEAN:
109                    Boolean valueAsBoolean = _siteConfiguration.getValueAsBoolean(siteName, name);
110                    if (valueAsBoolean != null)
111                    {
112                        values.put(name, valueAsBoolean);
113                    }
114                    break;
115                case DATE:
116                    Date valueAsDate = _siteConfiguration.getValueAsDate(siteName, name);
117                    if (valueAsDate != null)
118                    {
119                        values.put(name, valueAsDate);
120                    }
121                    break;
122                case STRING:
123                default:
124                    String valueAsString = _siteConfiguration.getValueAsString(siteName, name);
125                    if (valueAsString != null)
126                    {
127                        values.put(name, valueAsString);
128                    }
129                    break;
130            }
131        }
132        
133        List<String> languages = new ArrayList<>();
134        AmetysObjectIterable<Sitemap> sitemaps = _siteManager.getSite(siteName).getSitemaps();
135        for (Sitemap sitemap : sitemaps)
136        {
137            languages.add(sitemap.getName());
138        }
139        values.put("lang", languages);
140        
141        form.put("values", values);
142        return form;
143    }
144    
145    private Map<String, Object> _parameters2json (String siteName) throws ProcessingException
146    {
147        Map<String, Object> parameters = new HashMap<>();
148        
149        List<Map<String, Object>> tabs = new ArrayList<>();
150        
151        Map<I18nizableText, Map<I18nizableText, List<SiteParameter>>> siteParameters = _siteConfiguration.getCategorizedParameters(siteName);
152        
153        for (Entry<I18nizableText, Map<I18nizableText, List<SiteParameter>>> categoryEntry : siteParameters.entrySet())
154        {
155            Map<String, Object> tab = new HashMap<>();
156            tab.put("role", "tab");
157            tab.put("label", categoryEntry.getKey());
158            
159            Map<String, Object> tabElmts = new HashMap<>();
160            
161            List<Map<String, Object>> fieldsets = new ArrayList<>();
162            Map<I18nizableText, List<SiteParameter>> groups = categoryEntry.getValue();
163            for (Entry<I18nizableText, List<SiteParameter>> groupEntry : groups.entrySet())
164            {
165                Map<String, Object> fieldset = new HashMap<>();
166                fieldset.put("role", "fieldset");
167                fieldset.put("label", groupEntry.getKey());
168                fieldset.put("elements", _parameters2JsonObject(groupEntry.getValue()));
169                
170                fieldsets.add(fieldset);
171            }
172            
173            tabElmts.put("fieldsets", fieldsets);
174            tab.put("elements", tabElmts);
175            
176            tabs.add(tab);
177        }
178        
179        parameters.put("fieldsets", tabs);
180        
181        return parameters;
182    }
183    
184    private Map<String, Object> _parameters2JsonObject (List<SiteParameter> siteParameters) throws ProcessingException
185    {
186        Map<String, Object> jsonObject = new LinkedHashMap<>();
187        
188        for (SiteParameter siteParameter : siteParameters)
189        {
190            jsonObject.put(siteParameter.getId(), _parameter2JsonObject(siteParameter));
191            
192            if ("skin".equals(siteParameter.getId()))
193            {
194                // Insert available languages
195                jsonObject.put("lang", _languages2JsonObject());
196            }
197        }
198        
199        return jsonObject;
200    }
201    
202    private Map<String, Object> _parameter2JsonObject (SiteParameter siteParameter) throws ProcessingException
203    {
204        Map<String, Object> param = ParameterHelper.toJSON(siteParameter);
205        return param;
206    }
207    
208    private Map<String, Object> _languages2JsonObject ()
209    {
210        Map<String, Object> param = new HashMap<>();
211        Map<String, String> validation = new HashMap<>();  
212        
213        param.put("label", new I18nizableText("plugin.web", "PLUGINS_WEB_ADMINISTRATOR_SITES_SITE_LANG"));
214        param.put("description", new I18nizableText("plugin.web", "PLUGINS_WEB_ADMINISTRATOR_SITES_SITE_LANG_HELP"));
215        param.put("type", ParameterType.STRING);
216        param.put("widget", "edition.combobox");
217        
218        validation.put("mandatory", "mandatory"); 
219        param.put("validation", validation);
220        
221        param.put("multiple", true);
222        
223        List<Map<String, Object>> options = new ArrayList<>();
224        
225        Map<String, Language> availableLanguages = _languagesManager.getAvailableLanguages();
226        for (Language availableLanguage : availableLanguages.values())
227        {
228            Map<String, Object> option = new HashMap<>();
229            option.put("label", availableLanguage.getLabel());
230            option.put("value", availableLanguage.getCode());
231            options.add(option);
232            
233        }
234        
235        param.put("enumeration", options);
236        
237        return param;
238    }
239
240}