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