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.skin.actions; 017 018import java.io.File; 019import java.util.Collection; 020import java.util.Collections; 021import java.util.HashMap; 022import java.util.Map; 023 024import org.apache.avalon.framework.context.Context; 025import org.apache.avalon.framework.context.ContextException; 026import org.apache.avalon.framework.context.Contextualizable; 027import org.apache.avalon.framework.parameters.Parameters; 028import org.apache.avalon.framework.service.ServiceException; 029import org.apache.avalon.framework.service.ServiceManager; 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; 035import org.apache.commons.io.FileUtils; 036import org.apache.commons.lang3.StringUtils; 037 038import org.ametys.core.cocoon.JSonReader; 039import org.ametys.core.util.I18nizableTextKeyComparator; 040import org.ametys.runtime.model.CategorizedElementDefinitionHelper; 041import org.ametys.runtime.model.DefinitionAndValue; 042import org.ametys.runtime.model.ElementDefinition; 043import org.ametys.runtime.model.I18nizableTextModelItemComparator; 044import org.ametys.runtime.model.ModelItem; 045import org.ametys.runtime.model.View; 046import org.ametys.runtime.util.AmetysHomeHelper; 047import org.ametys.web.skin.Skin; 048import org.ametys.web.skin.SkinDAO; 049import org.ametys.web.skin.SkinParameterTypeExtensionPoint; 050import org.ametys.web.skin.SkinParametersModel; 051import org.ametys.web.skin.SkinsManager; 052 053/** 054 * Get the skin configuration parameters and their values 055 */ 056public class SkinConfigurationAction extends ServiceableAction implements Contextualizable 057{ 058 /** The service manager instance */ 059 private SkinsManager _skinsManager; 060 private Context _context; 061 private SkinParameterTypeExtensionPoint _skinParameterTypeEP; 062 private SkinDAO _skinDAO; 063 064 @Override 065 public void service(ServiceManager serviceManager) throws ServiceException 066 { 067 super.service(serviceManager); 068 _skinsManager = (SkinsManager) serviceManager.lookup(SkinsManager.ROLE); 069 _skinParameterTypeEP = (SkinParameterTypeExtensionPoint) serviceManager.lookup(SkinParameterTypeExtensionPoint.ROLE); 070 _skinDAO = (SkinDAO) serviceManager.lookup(SkinDAO.ROLE); 071 } 072 073 public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception 074 { 075 Request request = ObjectModelHelper.getRequest(objectModel); 076 077 String skinName = request.getParameter("skinName"); 078 String tempDir = request.getParameter("tempDir"); 079 String skinDirName = request.getParameter("skinDir"); 080 081 File skinDir = null; 082 if (StringUtils.isEmpty(tempDir)) 083 { 084 Skin skin = _skinsManager.getSkin(skinName); 085 skinDir = new File(skin.getLocation()); 086 } 087 else 088 { 089 File ametysTmpDir = AmetysHomeHelper.getAmetysHomeTmp(); 090 skinDir = FileUtils.getFile(ametysTmpDir, tempDir.replace('/', File.separatorChar), skinDirName); 091 } 092 093 SkinParametersModel model = new SkinParametersModel(skinName, skinDir, _skinParameterTypeEP, _context, manager); 094 095 Map<String, ElementDefinition> flatDefinitions = model.getFlatDefinitions(); 096 Collection<? extends ModelItem> modelItems = model.getModelItems(); 097 I18nizableTextModelItemComparator groupComparator = new I18nizableTextModelItemComparator(new I18nizableTextKeyComparator()); 098 View view = CategorizedElementDefinitionHelper.buildViewFromCategories(modelItems, null, groupComparator); 099 100 Map<String, Object> result = new HashMap<>(); 101 result.put("parameters", view.toJSON()); 102 result.put("values", _values2json(skinDir, flatDefinitions)); 103 104 request.setAttribute(JSonReader.OBJECT_TO_READ, result); 105 return Collections.EMPTY_MAP; 106 } 107 108 /** 109 * Read configuration file and return a flat map of values 110 * @param skinDir The skin directory 111 * @param flatDefinitions The parameters definitions 112 * @return the configuration definition and values 113 * @throws Exception if a problem occurs reading values 114 */ 115 private Map<String, Object> _values2json(File skinDir, Map<String, ElementDefinition> flatDefinitions) throws Exception 116 { 117 Collection<DefinitionAndValue> definitionAndValues = _skinDAO.readValues(skinDir, flatDefinitions); 118 119 Map<String, Object> values = new HashMap<>(); 120 121 for (DefinitionAndValue definitionAndValue : definitionAndValues) 122 { 123 ModelItem definition = definitionAndValue.getDefinition(); 124 125 //We have a flat map so we always have an ElementDefinition and never an ModelItemGroup 126 if (definition instanceof ElementDefinition) 127 { 128 ElementDefinition elementDefinition = (ElementDefinition) definition; 129 Object jsonValue = elementDefinition.getType().valueToJSONForClient(definitionAndValue.getValue()); 130 values.put(elementDefinition.getName(), jsonValue); 131 } 132 } 133 134 Map<String, Object> form = new HashMap<>(); 135 form.put("values", values); 136 return form; 137 } 138 139 public void contextualize(Context context) throws ContextException 140 { 141 _context = context; 142 } 143 144}