001/* 002 * Copyright 2010 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.service; 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.ProcessingException; 028import org.apache.cocoon.acting.ServiceableAction; 029import org.apache.cocoon.environment.ObjectModelHelper; 030import org.apache.cocoon.environment.Redirector; 031import org.apache.cocoon.environment.Request; 032import org.apache.cocoon.environment.SourceResolver; 033 034import org.ametys.core.cocoon.JSonReader; 035import org.ametys.runtime.parameter.ParameterHelper; 036 037/** 038 * Get service parameters as JSON object 039 * 040 */ 041public class GetServiceParametersAction extends ServiceableAction 042{ 043 /** The {@link ServiceExtensionPoint} */ 044 protected ServiceExtensionPoint _serviceExtPt; 045 046 @Override 047 public void service(ServiceManager sManager) throws ServiceException 048 { 049 super.service(sManager); 050 _serviceExtPt = (ServiceExtensionPoint) sManager.lookup(ServiceExtensionPoint.ROLE); 051 } 052 053 public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception 054 { 055 Request request = ObjectModelHelper.getRequest(objectModel); 056 String serviceId = request.getParameter("serviceId"); 057 058 Map<String, Object> jsonObject = new HashMap<>(); 059 060 Service service = _serviceExtPt.getExtension(serviceId); 061 062 jsonObject.put("id", serviceId); 063 jsonObject.put("url", service.getURL()); 064 jsonObject.put("label", service.getLabel()); 065 066 jsonObject.put("parameters", parameters2JsonObject(service)); 067 068 request.setAttribute(JSonReader.OBJECT_TO_READ, jsonObject); 069 return EMPTY_MAP; 070 } 071 072 /** 073 * Convert service parameters to JSON object 074 * @param service true the service 075 * @return The JSON object representing the user preferences 076 * @throws ProcessingException if an error occurred 077 */ 078 @SuppressWarnings("unchecked") 079 protected Map<String, Object> parameters2JsonObject (Service service) throws ProcessingException 080 { 081 Map<String, Object> jsonObject = new LinkedHashMap<>(); 082 jsonObject.put("groups", new ArrayList<Map<String, Object>>()); 083 084 for (ServiceParameterGroup group : service.getParameterGroups()) 085 { 086 Map<String, Object> parameters = new LinkedHashMap<>(); 087 088 for (ServiceParameterOrRepeater serviceParameter : group.getParameters()) 089 { 090 if (serviceParameter instanceof ServiceParameter) 091 { 092 parameters.put(serviceParameter.getId(), parameter2JsonObject((ServiceParameter) serviceParameter, null)); 093 } 094 else 095 { 096 parameters.put(serviceParameter.getId(), repeater2JsonObject((ServiceParameterRepeater) serviceParameter)); 097 } 098 099 } 100 101 List<Map<String, Object>> groups = (List<Map<String, Object>>) jsonObject.get("groups"); 102 groups.add(parameters); 103 } 104 105 return jsonObject; 106 } 107 108 /** 109 * Convert a service parameter to JSON object 110 * @param parameter the service parameter. 111 * @param value The value 112 * @return The JSON object representing the user preferences 113 * @throws ProcessingException if an error occurred 114 */ 115 protected Map<String, Object> parameter2JsonObject (ServiceParameter parameter, Object value) throws ProcessingException 116 { 117 Map<String, Object> jsonObject = ParameterHelper.toJSON(parameter); 118 119 jsonObject.put("multiple", parameter.isMultiple()); 120 121 if (value != null) 122 { 123 jsonObject.put("value", value); 124 } 125 else 126 { 127 jsonObject.put("value", parameter.getDefaultValue()); 128 } 129 130 return jsonObject; 131 } 132 133 /** 134 * Convert the repeater object to JSON 135 * @param repeater the repeater of service parameters 136 * @return the repeater in JSON 137 * @throws ProcessingException if something goes wrong while gathering the properties of the repeater 138 */ 139 protected Map<String, Object> repeater2JsonObject (ServiceParameterRepeater repeater) throws ProcessingException 140 { 141 Map<String, Object> jsonObject = new LinkedHashMap<>(); 142 143 Map<String, Object> repeater2json = new HashMap<>(); 144 145 jsonObject.put("label", repeater.getLabel()); 146 jsonObject.put("description", repeater.getDescription()); 147 jsonObject.put("type", "COMPOSITE"); 148 149 repeater2json.put("initial-size", repeater.getInitialSize()); 150 repeater2json.put("min-size", repeater.getMinSize()); 151 152 int maxSize = repeater.getMaxSize(); 153 if (maxSize >= 0) 154 { 155 repeater2json.put("max-size", maxSize); 156 } 157 158 159 repeater2json.put("add-label", repeater.getAddLabel()); 160 repeater2json.put("del-label", repeater.getDeleteLabel()); 161 162 Map<String, Object> composition = new LinkedHashMap<>(); 163 164 Map<String, ServiceParameter> parameters = repeater.getChildrenParameters(); 165 for (ServiceParameter parameter : parameters.values()) 166 { 167 composition.put(parameter.getId(), parameter2JsonObject(parameter, null)); 168 } 169 170 repeater2json.put("composition", composition); 171 jsonObject.put("repeater", repeater2json); 172 173 return jsonObject; 174 } 175}