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.HashMap; 020import java.util.LinkedHashMap; 021import java.util.List; 022import java.util.Map; 023 024import org.apache.avalon.framework.parameters.Parameters; 025import org.apache.avalon.framework.service.ServiceException; 026import org.apache.avalon.framework.service.ServiceManager; 027import org.apache.cocoon.acting.ServiceableAction; 028import org.apache.cocoon.environment.ObjectModelHelper; 029import org.apache.cocoon.environment.Redirector; 030import org.apache.cocoon.environment.Request; 031import org.apache.cocoon.environment.SourceResolver; 032 033import org.ametys.cms.languages.Language; 034import org.ametys.cms.languages.LanguagesManager; 035import org.ametys.core.cocoon.JSonReader; 036import org.ametys.plugins.repository.AmetysObjectIterable; 037import org.ametys.runtime.i18n.I18nizableText; 038import org.ametys.runtime.model.ElementDefinition; 039import org.ametys.runtime.model.View; 040import org.ametys.runtime.model.type.ElementType; 041import org.ametys.runtime.model.type.ModelItemTypeConstants; 042import org.ametys.web.repository.site.Site; 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 private static final String __SKIN_PARAMETER_NAME = "skin"; 052 private static final String __LANG_PARAMETER_NAME = "lang"; 053 054 /** The site manager. */ 055 protected SiteManager _siteManager; 056 057 /** The site configuration. */ 058 protected SiteConfigurationManager _siteConfigurationManager; 059 060 /** The available languages manager */ 061 protected LanguagesManager _languagesManager; 062 063 @Override 064 public void service(ServiceManager serviceManager) throws ServiceException 065 { 066 super.service(serviceManager); 067 _siteManager = (SiteManager) serviceManager.lookup(SiteManager.ROLE); 068 _siteConfigurationManager = (SiteConfigurationManager) serviceManager.lookup(SiteConfigurationManager.ROLE); 069 _languagesManager = (LanguagesManager) serviceManager.lookup(LanguagesManager.ROLE); 070 } 071 072 public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception 073 { 074 Request request = ObjectModelHelper.getRequest(objectModel); 075 076 String siteName = parameters.getParameter("siteName", request.getParameter("siteName")); 077 078 Map<String, Object> result = new HashMap<>(); 079 080 View view = _siteConfigurationManager.getSiteView(siteName); 081 // Insert an input to configure the language (no corresponding site parameter), just after the skin parameter 082 Map<String, Object> view2JSON = _insertLanguageParameterAfterSkinParameter(view.toJSON()); 083 result.put("parameters", view2JSON); 084 085 result.put("values", _values2json(siteName)); 086 087 request.setAttribute(JSonReader.OBJECT_TO_READ, result); 088 return EMPTY_MAP; 089 } 090 091 private Map<String, Object> _values2json (String siteName) 092 { 093 Map<String, Object> form = new HashMap<>(); 094 Map<String, Object> values = new HashMap<>(); 095 096 Site site = _siteManager.getSite(siteName); 097 for (ElementDefinition definition : _siteConfigurationManager.getSiteParameters(site)) 098 { 099 ElementType type = definition.getType(); 100 Object value = site.getValue(definition.getName(), true, null); 101 values.put(definition.getName(), type.valueToJSONForClient(value)); 102 } 103 104 List<String> languages = new ArrayList<>(); 105 AmetysObjectIterable<Sitemap> sitemaps = site.getSitemaps(); 106 for (Sitemap sitemap : sitemaps) 107 { 108 languages.add(sitemap.getName()); 109 } 110 values.put("lang", languages); 111 112 form.put("values", values); 113 return form; 114 } 115 116 @SuppressWarnings("unchecked") 117 private Map<String, Object> _insertLanguageParameterAfterSkinParameter(Map<String, Object> view2JSON) 118 { 119 Map<String, Object> json = new LinkedHashMap<>(); 120 json.putAll(view2JSON); 121 122 List<Map<String, Object>> categories = (List<Map<String, Object>>) json.get("groups"); 123 for (Map<String, Object> category : categories) 124 { 125 Map<String, Object> categoryElements = (Map<String, Object>) category.get("elements"); 126 List<Map<String, Object>> groups = (List<Map<String, Object>>) categoryElements.get("groups"); 127 for (Map<String, Object> group : groups) 128 { 129 Map<String, Object> groupElements = (Map<String, Object>) group.get("elements"); 130 if (groupElements.containsKey(__SKIN_PARAMETER_NAME)) 131 { 132 // Create a new group elements that will contain the languages parameter 133 Map<String, Object> newElements = new LinkedHashMap<>(); 134 for (Map.Entry<String, Object> element : groupElements.entrySet()) 135 { 136 newElements.put(element.getKey(), element.getValue()); 137 138 if (__SKIN_PARAMETER_NAME.equals(element.getKey())) 139 { 140 // Insert languages parameter after the skin parameter 141 newElements.put(__LANG_PARAMETER_NAME, _languages2JsonObject()); 142 } 143 } 144 145 // Replace the group elements by the new one (containing the language parameter) 146 group.replace("elements", newElements); 147 } 148 } 149 } 150 151 return json; 152 } 153 154 private Map<String, Object> _languages2JsonObject () 155 { 156 Map<String, Object> param = new HashMap<>(); 157 Map<String, String> validation = new HashMap<>(); 158 159 param.put("label", new I18nizableText("plugin.web", "PLUGINS_WEB_ADMINISTRATOR_SITES_SITE_LANG")); 160 param.put("description", new I18nizableText("plugin.web", "PLUGINS_WEB_ADMINISTRATOR_SITES_SITE_LANG_HELP")); 161 param.put("type", ModelItemTypeConstants.STRING_TYPE_ID); 162 param.put("widget", "edition.combobox"); 163 164 validation.put("mandatory", "mandatory"); 165 param.put("validation", validation); 166 167 param.put("multiple", true); 168 169 List<Map<String, Object>> options = new ArrayList<>(); 170 171 Map<String, Language> availableLanguages = _languagesManager.getAvailableLanguages(); 172 for (Language availableLanguage : availableLanguages.values()) 173 { 174 Map<String, Object> option = new HashMap<>(); 175 option.put("label", availableLanguage.getLabel()); 176 option.put("value", availableLanguage.getCode()); 177 options.add(option); 178 179 } 180 181 param.put("enumeration", options); 182 183 return param; 184 } 185 186}