001: /*
002: * $Header: /export/home/cvsroot/MyPersonalizerRepository/MyPersonalizer/Subsystems/Portal/Sources/es/udc/mypersonalizer/portal/controller/wizards/WizardEventFactory.java,v 1.1.1.1 2004/03/25 12:08:40 fbellas Exp $
003: * $Revision: 1.1.1.1 $
004: * $Date: 2004/03/25 12:08:40 $
005: *
006: * =============================================================================
007: *
008: * Copyright (c) 2003, The MyPersonalizer Development Group
009: * (http://www.tic.udc.es/~fbellas/mypersonalizer/index.html) at
010: * University Of A Coruna
011: * All rights reserved.
012: *
013: * Redistribution and use in source and binary forms, with or without
014: * modification, are permitted provided that the following conditions are met:
015: *
016: * - Redistributions of source code must retain the above copyright notice,
017: * this list of conditions and the following disclaimer.
018: *
019: * - Redistributions in binary form must reproduce the above copyright notice,
020: * this list of conditions and the following disclaimer in the documentation
021: * and/or other materials provided with the distribution.
022: *
023: * - Neither the name of the University Of A Coruna nor the names of its
024: * contributors may be used to endorse or promote products derived from
025: * this software without specific prior written permission.
026: *
027: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
028: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
029: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
030: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
031: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
032: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
033: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
034: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
035: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
036: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
037: * POSSIBILITY OF SUCH DAMAGE.
038: *
039: */
040:
041: package es.udc.mypersonalizer.portal.controller.wizards;
042:
043: import es.udc.mypersonalizer.portal.conventions.RequestParameterConventions;
044: import es.udc.mypersonalizer.kernel.util.exceptions.InternalErrorException;
045: import es.udc.mypersonalizer.portal.controller.session.SessionManager;
046: import es.udc.mypersonalizer.portal.model.permissions.UserCredentials;
047: import es.udc.mypersonalizer.portal.model.types.WizardEvent;
048: import java.util.*;
049: import javax.servlet.ServletException;
050: import javax.servlet.http.HttpServletRequest;
051: import org.apache.commons.beanutils.BeanUtils;
052: import org.apache.commons.beanutils.PropertyUtils;
053: import org.apache.struts.action.ActionForm;
054:
055: /**
056: * This class creates a WizardEvent containing the data that the user may
057: * input in an html form. It takes the request and the ActionForm which is
058: * supposed to fit the data that has been just input.<p>
059: *
060: * It takes from the request the following information besides the actual data:
061: * <ul>
062: * <li>The names of the properties that may have been set in the form</li>
063: * <li>The names of the properties that should not be trimmed (have their
064: * heading and trailing spaces removed).</li>
065: * <li>The name of the "service identifier" to which this form
066: * corresponds</li>
067: * <li>The "property identifier" designating the property on which to
068: * work, if necessary</li>
069: * <li>The "structure name" which corresponds to the input data</li>
070: * <li>Optionally, the "property updater name" which will define the actions
071: * to execute involving this data</li>
072: * </ul>
073: *
074: * Once this data is get from the request, this class tries to get data for
075: * each of the fields that are defined in the ActionForm that it takes as a
076: * parameter. Then, after trimming (if necessary), the data is put into the
077: * WizardEvent.
078: *
079: * @author Daniel Fernandez
080: * @author Fernando Bellas
081: * @since 1.0
082: */
083: public class WizardEventFactory {
084:
085: /**
086: * Prevents this class from being instantiated
087: */
088: private WizardEventFactory() {
089: }
090:
091: /**
092: * Creates a WizardEvent from the request and a defined ActionForm trying
093: * to get all the "control parameters" from the request. These commands are:
094: * <ul>
095: * <li>serviceIdentifier</li>
096: * <li>propertyIdentifier</li>
097: * <li>propertyUpdaterName</li>
098: * <li>propertyStructureName</li>
099: * <li>simplePropertyNames</li>
100: * <li>simplePropertyNamesNotToTrim</li>
101: * </ul>
102: *
103: * @return a WizardEvent with the input data correctly formatted
104: * @param request the request
105: * @param form the action form which defines the structure of
106: * the data we should have received
107: * @throws InternalErrorException if any fatal errors occur
108: */
109: public static WizardEvent createWizardEvent(
110: HttpServletRequest request, ActionForm form)
111: throws InternalErrorException {
112:
113: Map controlParameters = new HashMap();
114:
115: return createWizardEvent(request, form, controlParameters);
116: }
117:
118: /**
119: * Creates a WizardEvent from the request and a defined ActionForm trying
120: * to get some of the "control parameters" from the request. These commands
121: * are:
122: * <ul>
123: * <li>serviceIdentifier</li>
124: * <li>propertyIdentifier</li>
125: * <li>propertyUpdaterName</li>
126: * <li>propertyStructureName</li>
127: * <li>simplePropertyNames</li>
128: * <li>simplePropertyNamesNotToTrim</li>
129: * </ul>
130: * If a command comes as an entry in the controlParameters Map, it is
131: * not taken from the request. If not, it is taken.
132: *
133: * @return a WizardEvent with the input data correctly formatted
134: * @param request the request
135: * @param form the action form which defines the structure of
136: * the data we should have received
137: * @param defaultParameters the Map with the control parameters that
138: * are not going to be taken from the request
139: * @throws InternalErrorException if any fatal errors occur
140: */
141: public static WizardEvent createWizardEvent(
142: HttpServletRequest request, ActionForm form,
143: Map defaultParameters) throws InternalErrorException {
144:
145: if (!defaultParameters
146: .containsKey(RequestParameterConventions.SERVICE_IDENTIFIER)) {
147:
148: getControlParameter(request,
149: RequestParameterConventions.SERVICE_IDENTIFIER,
150: defaultParameters, false, true, true);
151:
152: }
153:
154: if (!defaultParameters
155: .containsKey(RequestParameterConventions.PROPERTY_IDENTIFIER)) {
156:
157: getControlParameter(request,
158: RequestParameterConventions.PROPERTY_IDENTIFIER,
159: defaultParameters, false, false, true);
160:
161: }
162:
163: if (!defaultParameters
164: .containsKey(RequestParameterConventions.PROPERTY_UPDATER_NAME)) {
165:
166: getControlParameter(request,
167: RequestParameterConventions.PROPERTY_UPDATER_NAME,
168: defaultParameters, false, true, true);
169:
170: }
171:
172: if (!defaultParameters
173: .containsKey(RequestParameterConventions.PROPERTY_STRUCTURE_NAME)) {
174:
175: getControlParameter(
176: request,
177: RequestParameterConventions.PROPERTY_STRUCTURE_NAME,
178: defaultParameters, false, true, true);
179:
180: }
181:
182: if (!defaultParameters
183: .containsKey(RequestParameterConventions.SIMPLE_PROPERTY_NAMES)) {
184:
185: getControlParameter(request,
186: RequestParameterConventions.SIMPLE_PROPERTY_NAMES,
187: defaultParameters, true, true, true);
188:
189: }
190:
191: if (!defaultParameters
192: .containsKey(RequestParameterConventions.SIMPLE_PROPERTY_NAMES_NOT_TO_TRIM)) {
193:
194: getControlParameter(
195: request,
196: RequestParameterConventions.SIMPLE_PROPERTY_NAMES_NOT_TO_TRIM,
197: defaultParameters, true, true, false);
198:
199: }
200:
201: return doCreateWizardEvent(request, form, defaultParameters);
202: }
203:
204: /**
205: * Creates a WizardEvent from the request and a defined ActionForm, and
206: * uses the control parameters that come in the controlParameters Map.
207: *
208: * @return a WizardEvent with the input data correctly formatted
209: * @param request the request
210: * @param form the action form which defines the structure of
211: * the data we should have received
212: * @param controlParameters the control parameters for this WizardEvent
213: * @throws InternalErrorException if any fatal errors occur
214: */
215: private static WizardEvent doCreateWizardEvent(
216: HttpServletRequest request, ActionForm form,
217: Map controlParameters) throws InternalErrorException {
218:
219: WizardEvent wizardEvent = null;
220: Map wizardEventProperties = new HashMap();
221:
222: /* This will be invalid if we're at SignUp */
223: UserCredentials userCredentials = SessionManager
224: .getUserCredentials(request);
225:
226: Collection simplePropertyNames = (Collection) controlParameters
227: .get(RequestParameterConventions.SIMPLE_PROPERTY_NAMES);
228:
229: Collection simplePropertyNamesNotToTrim = (Collection) controlParameters
230: .get(RequestParameterConventions.SIMPLE_PROPERTY_NAMES_NOT_TO_TRIM);
231:
232: String serviceIdentifier = (String) controlParameters
233: .get(RequestParameterConventions.SERVICE_IDENTIFIER);
234:
235: String structureName = (String) controlParameters
236: .get(RequestParameterConventions.PROPERTY_STRUCTURE_NAME);
237:
238: String propertyUpdaterName = (String) controlParameters
239: .get(RequestParameterConventions.PROPERTY_UPDATER_NAME);
240:
241: Long propertyIdentifier = null;
242: String propertyIdentifierParameter = (String) controlParameters
243: .get(RequestParameterConventions.PROPERTY_IDENTIFIER);
244: if (propertyIdentifierParameter != null) {
245: try {
246: propertyIdentifier = new Long(
247: propertyIdentifierParameter);
248: } catch (NumberFormatException e) {
249: throw new InternalErrorException(e);
250: }
251: }
252:
253: try {
254:
255: /*
256: * We get an Iterator on the fields of the ActionForm
257: * so that we can get from the request only the data we need
258: */
259: Map formFields = BeanUtils.describe(form);
260: Iterator fieldIter = formFields.entrySet().iterator();
261:
262: while (fieldIter.hasNext()) {
263:
264: /*
265: * As the iterator is a list of Map.Entry, which entries
266: * have the field name as key and the field value as value,
267: * we must get the entry key
268: */
269: Map.Entry fieldEntry = (Map.Entry) fieldIter.next();
270: String fieldName = (String) fieldEntry.getKey();
271:
272: if (simplePropertyNames.contains(fieldName)) {
273:
274: boolean multiValued = PropertyUtils
275: .getPropertyType(form, fieldName).isArray();
276:
277: String[] values = null;
278:
279: if (multiValued) {
280: String[] compoundValue = BeanUtils
281: .getArrayProperty(form, fieldName);
282: if (compoundValue == null) {
283: values = new String[0];
284: } else {
285: values = compoundValue;
286: }
287: } else {
288: String simpleValue = BeanUtils
289: .getSimpleProperty(form, fieldName);
290: if (simpleValue == null) {
291: values = new String[0];
292: } else {
293: values = new String[1];
294: values[0] = simpleValue;
295: }
296: }
297:
298: if (!(simplePropertyNamesNotToTrim
299: .contains(fieldName))) {
300: for (int i = 0; i < values.length; i++) {
301: values[i] = values[i].trim();
302: }
303: }
304:
305: wizardEventProperties.put(fieldName, values);
306: }
307:
308: }
309:
310: } catch (Exception e) {
311: throw new InternalErrorException(e);
312: }
313:
314: wizardEvent = new WizardEvent(userCredentials,
315: wizardEventProperties, serviceIdentifier,
316: propertyUpdaterName, structureName, propertyIdentifier);
317:
318: return wizardEvent;
319: }
320:
321: /**
322: * Gets a control parameter from the request
323: *
324: * @param request the request
325: * @param parameterName the name of the control parameter to get
326: * @param controlParameters the Map into which the control parameter
327: * will be pushed
328: * @param isArray which will be true if the control parameter to get
329: * is multi-valued (several parameters with the same
330: * name).
331: * @param mandatory raises an exception if the field is mandatory and
332: * it is not found in the request
333: * @param notEmpty raises an exception if the field cannot be empty
334: * and it is equal to ""
335: * @throws InternalErrorException if any fatal errors occur
336: */
337: private static void getControlParameter(HttpServletRequest request,
338: String parameterName, Map controlParameters,
339: boolean isArray, boolean mandatory, boolean notEmpty)
340: throws InternalErrorException {
341:
342: String result = null;
343: String[] parameter = (request.getParameterValues(parameterName));
344: if (parameter != null) {
345:
346: if (isArray) {
347:
348: if (notEmpty && (parameter.length <= 0)) {
349: throw new InternalErrorException(
350: new ServletException("\"" + parameterName
351: + "\" "
352: + "parameter missing from request"));
353: }
354:
355: for (int i = 0; i < parameter.length; i++) {
356: parameter[i].trim();
357: }
358:
359: List parameterAsList = Arrays.asList(parameter);
360: controlParameters.put(parameterName, parameterAsList);
361:
362: } else {
363:
364: result = parameter[0];
365: result = result.trim();
366: if (notEmpty && (result.equals(""))) {
367: throw new InternalErrorException(
368: new ServletException("\"" + parameterName
369: + "\" "
370: + "parameter missing from request"));
371: }
372:
373: controlParameters.put(parameterName, result);
374:
375: }
376:
377: } else {
378: if (mandatory) {
379: throw new InternalErrorException(new ServletException(
380: "\"" + parameterName
381: + "\" parameter missing from request"));
382: } else {
383: if (isArray) {
384: controlParameters.put(parameterName,
385: new ArrayList());
386: } else {
387: controlParameters.put(parameterName, result);
388: }
389: }
390: }
391:
392: }
393:
394: }
|