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; 023import java.util.Map.Entry; 024 025import org.apache.avalon.framework.parameters.Parameters; 026import org.apache.avalon.framework.service.ServiceException; 027import org.apache.avalon.framework.service.ServiceManager; 028import org.apache.cocoon.ProcessingException; 029import org.apache.cocoon.acting.ServiceableAction; 030import org.apache.cocoon.environment.ObjectModelHelper; 031import org.apache.cocoon.environment.Redirector; 032import org.apache.cocoon.environment.Request; 033import org.apache.cocoon.environment.SourceResolver; 034 035import org.ametys.cms.languages.Language; 036import org.ametys.cms.languages.LanguagesManager; 037import org.ametys.core.cocoon.JSonReader; 038import org.ametys.plugins.repository.AmetysObjectIterable; 039import org.ametys.runtime.i18n.I18nizableText; 040import org.ametys.runtime.model.type.ElementType; 041import org.ametys.web.repository.site.Site; 042import org.ametys.web.repository.site.SiteManager; 043import org.ametys.web.repository.sitemap.Sitemap; 044 045/** 046 * Get the site configuration parameters and their values 047 */ 048public class SiteConfigurationAction extends ServiceableAction 049{ 050 /** The site manager. */ 051 protected SiteManager _siteManager; 052 053 /** The site configuration. */ 054 protected SiteConfigurationExtensionPoint _siteConfiguration; 055 056 /** The available languages manager */ 057 protected LanguagesManager _languagesManager; 058 059 @Override 060 public void service(ServiceManager serviceManager) throws ServiceException 061 { 062 super.service(serviceManager); 063 _siteManager = (SiteManager) serviceManager.lookup(SiteManager.ROLE); 064 _siteConfiguration = (SiteConfigurationExtensionPoint) serviceManager.lookup(SiteConfigurationExtensionPoint.ROLE); 065 _languagesManager = (LanguagesManager) serviceManager.lookup(LanguagesManager.ROLE); 066 } 067 068 public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception 069 { 070 Request request = ObjectModelHelper.getRequest(objectModel); 071 072 String siteName = parameters.getParameter("siteName", request.getParameter("siteName")); 073 074 Map<String, Object> result = new HashMap<>(); 075 result.put("parameters", _parameters2json(siteName)); 076 result.put("values", _values2json(siteName)); 077 078 request.setAttribute(JSonReader.OBJECT_TO_READ, result); 079 return EMPTY_MAP; 080 } 081 082 private Map<String, Object> _values2json (String siteName) 083 { 084 Map<String, Object> form = new HashMap<>(); 085 Map<String, Object> values = new HashMap<>(); 086 087 Map<String, SiteParameter> parameters = _siteConfiguration.getParameters(siteName); 088 Site site = _siteManager.getSite(siteName); 089 for (String name : parameters.keySet()) 090 { 091 SiteParameter siteParameter = parameters.get(name); 092 ElementType type = siteParameter.getType(); 093 094 Object value = site.getValue(name, true, null); 095 values.put(name, type.valueToJSONForClient(value)); 096 } 097 098 List<String> languages = new ArrayList<>(); 099 AmetysObjectIterable<Sitemap> sitemaps = _siteManager.getSite(siteName).getSitemaps(); 100 for (Sitemap sitemap : sitemaps) 101 { 102 languages.add(sitemap.getName()); 103 } 104 values.put("lang", languages); 105 106 form.put("values", values); 107 return form; 108 } 109 110 private Map<String, Object> _parameters2json (String siteName) throws ProcessingException 111 { 112 Map<String, Object> parameters = new HashMap<>(); 113 114 List<Map<String, Object>> tabs = new ArrayList<>(); 115 116 Map<I18nizableText, Map<I18nizableText, List<SiteParameter>>> siteParameters = _siteConfiguration.getCategorizedParameters(siteName); 117 118 for (Entry<I18nizableText, Map<I18nizableText, List<SiteParameter>>> categoryEntry : siteParameters.entrySet()) 119 { 120 Map<String, Object> tab = new HashMap<>(); 121 tab.put("role", "tab"); 122 tab.put("label", categoryEntry.getKey()); 123 124 Map<String, Object> tabElmts = new HashMap<>(); 125 126 List<Map<String, Object>> fieldsets = new ArrayList<>(); 127 Map<I18nizableText, List<SiteParameter>> groups = categoryEntry.getValue(); 128 for (Entry<I18nizableText, List<SiteParameter>> groupEntry : groups.entrySet()) 129 { 130 Map<String, Object> fieldset = new HashMap<>(); 131 fieldset.put("role", "fieldset"); 132 fieldset.put("label", groupEntry.getKey()); 133 fieldset.put("elements", _parameters2JsonObject(groupEntry.getValue())); 134 135 fieldsets.add(fieldset); 136 } 137 138 tabElmts.put("fieldsets", fieldsets); 139 tab.put("elements", tabElmts); 140 141 tabs.add(tab); 142 } 143 144 parameters.put("fieldsets", tabs); 145 146 return parameters; 147 } 148 149 private Map<String, Object> _parameters2JsonObject (List<SiteParameter> siteParameters) throws ProcessingException 150 { 151 Map<String, Object> jsonObject = new LinkedHashMap<>(); 152 153 for (SiteParameter siteParameter : siteParameters) 154 { 155 jsonObject.put(siteParameter.getName(), siteParameter.toJSON()); 156 157 if ("skin".equals(siteParameter.getName())) 158 { 159 // Insert available languages 160 jsonObject.put("lang", _languages2JsonObject()); 161 } 162 } 163 164 return jsonObject; 165 } 166 167 private Map<String, Object> _languages2JsonObject () 168 { 169 Map<String, Object> param = new HashMap<>(); 170 Map<String, String> validation = new HashMap<>(); 171 172 param.put("label", new I18nizableText("plugin.web", "PLUGINS_WEB_ADMINISTRATOR_SITES_SITE_LANG")); 173 param.put("description", new I18nizableText("plugin.web", "PLUGINS_WEB_ADMINISTRATOR_SITES_SITE_LANG_HELP")); 174 param.put("type", "STRING"); 175 param.put("widget", "edition.combobox"); 176 177 validation.put("mandatory", "mandatory"); 178 param.put("validation", validation); 179 180 param.put("multiple", true); 181 182 List<Map<String, Object>> options = new ArrayList<>(); 183 184 Map<String, Language> availableLanguages = _languagesManager.getAvailableLanguages(); 185 for (Language availableLanguage : availableLanguages.values()) 186 { 187 Map<String, Object> option = new HashMap<>(); 188 option.put("label", availableLanguage.getLabel()); 189 option.put("value", availableLanguage.getCode()); 190 options.add(option); 191 192 } 193 194 param.put("enumeration", options); 195 196 return param; 197 } 198 199}