KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jaspersoft > jasperserver > war > action > ReportParametersAction


1 /*
2  * Copyright (C) 2006 JasperSoft http://www.jaspersoft.com
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed WITHOUT ANY WARRANTY; and without the
10  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11  * See the GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, see http://www.gnu.org/licenses/gpl.txt
15  * or write to:
16  *
17  * Free Software Foundation, Inc.,
18  * 59 Temple Place - Suite 330,
19  * Boston, MA USA 02111-1307
20  */

21 package com.jaspersoft.jasperserver.war.action;
22
23
24 import java.math.BigDecimal JavaDoc;
25 import java.text.DateFormat JavaDoc;
26 import java.text.DecimalFormat JavaDoc;
27 import java.text.Format JavaDoc;
28 import java.text.ParseException JavaDoc;
29 import java.text.SimpleDateFormat JavaDoc;
30 import java.util.ArrayList JavaDoc;
31 import java.util.Collection JavaDoc;
32 import java.util.Date JavaDoc;
33 import java.util.HashMap JavaDoc;
34 import java.util.Iterator JavaDoc;
35 import java.util.List JavaDoc;
36 import java.util.Locale JavaDoc;
37 import java.util.Map JavaDoc;
38 import java.util.Set JavaDoc;
39 import java.util.TimeZone JavaDoc;
40 import java.util.regex.Pattern JavaDoc;
41
42 import net.sf.jasperreports.engine.JRParameter;
43 import net.sf.jasperreports.engine.JRReport;
44
45 import org.apache.commons.collections.OrderedMap;
46 import org.apache.commons.collections.map.LinkedMap;
47 import org.apache.commons.collections.set.ListOrderedSet;
48 import org.apache.commons.logging.Log;
49 import org.apache.commons.logging.LogFactory;
50 import org.springframework.context.MessageSource;
51 import org.springframework.context.i18n.LocaleContextHolder;
52 import org.springframework.webflow.Event;
53 import org.springframework.webflow.ParameterMap;
54 import org.springframework.webflow.RequestContext;
55 import org.springframework.webflow.action.FormAction;
56
57 import com.jaspersoft.jasperserver.api.JSException;
58 import com.jaspersoft.jasperserver.api.common.domain.impl.ExecutionContextImpl;
59 import com.jaspersoft.jasperserver.api.engine.common.service.EngineService;
60 import com.jaspersoft.jasperserver.api.metadata.common.domain.DataType;
61 import com.jaspersoft.jasperserver.api.metadata.common.domain.InputControl;
62 import com.jaspersoft.jasperserver.api.metadata.common.domain.ListOfValues;
63 import com.jaspersoft.jasperserver.api.metadata.common.domain.ResourceReference;
64 import com.jaspersoft.jasperserver.api.metadata.common.service.RepositoryService;
65 import com.jaspersoft.jasperserver.api.metadata.jasperreports.domain.ReportUnit;
66 import com.jaspersoft.jasperserver.war.dto.RuntimeInputControlWrapper;
67 import com.jaspersoft.jasperserver.war.util.CalendarFormatProvider;
68
69 /**
70  * @author Ionut Nedelcu (ionutned@users.sourceforge.net)
71  * @version $Id: ReportParametersAction.java 4728 2006-09-25 09:09:53Z lucian $
72  */

73 public abstract class ReportParametersAction extends FormAction
74 {
75     private static final Log log = LogFactory.getLog(ReportParametersAction.class);
76     
77     public static final String JavaDoc INPUTWRAPPERS_ATTR = "wrappers";
78     
79     private static final String JavaDoc COLUMN_VALUE_SEPARATOR = " | ";
80     private static final int COLUMN_VALUE_SEPARATOR_LENGTH = COLUMN_VALUE_SEPARATOR.length();
81
82     private String JavaDoc reportUnitAttrName;
83     private String JavaDoc reportUnitObjectAttrName;
84     private String JavaDoc controlsDisplayFormAttrName;
85     private String JavaDoc reportDisplayFormAttrName;
86     private String JavaDoc calendarDatePatternAttrName;
87     private String JavaDoc calendarDatetimePatternAttrName;
88     private EngineService engine;
89     private RepositoryService repository;
90     private MessageSource messages;
91     private String JavaDoc hasInputControlsAttrName;
92     private String JavaDoc staticDatePattern;
93     private CalendarFormatProvider calendarFormatProvider;
94
95     protected Event createWrappers(RequestContext context, boolean parseRequest) throws Exception JavaDoc
96     {
97         String JavaDoc reportUnitUri = (String JavaDoc) context.getFlowScope().get(getReportUnitAttrName());
98
99         if (reportUnitUri == null) {
100             reportUnitUri = context.getRequestParameters().get(getReportUnitAttrName());
101             if (reportUnitUri != null) {
102                 context.getFlowScope().put(getReportUnitAttrName(), reportUnitUri);
103             }
104         }
105         
106         if (reportUnitUri == null || reportUnitUri.trim().length() == 0) {
107             return error();
108         }
109
110         ReportUnit reportUnit = (ReportUnit) repository.getResource(null, reportUnitUri);
111         
112         if (reportUnit == null) {
113             return error();
114         }
115
116         context.getFlowScope().put(getReportUnitObjectAttrName(), reportUnit);
117         context.getFlowScope().put(getControlsDisplayFormAttrName(), reportUnit.getInputControlRenderingView());
118         context.getFlowScope().put(getReportDisplayFormAttrName(), reportUnit.getReportRenderingView());
119         
120         context.getFlowScope().put(getCalendarDatePatternAttrName(), getCalendarDatePattern());
121         context.getFlowScope().put(getCalendarDatetimePatternAttrName(), getCalendarDatetimePattern());
122         
123         List JavaDoc wrappers = createWrappers(context, reportUnit);
124         context.getFlowScope().put(INPUTWRAPPERS_ATTR, wrappers);
125
126         boolean needsInput;
127         if (parseRequest) {
128             needsInput = !wrappers.isEmpty() && !parseRequest(context, wrappers, false);
129         } else {
130             needsInput = !wrappers.isEmpty();
131         }
132         
133         context.getFlowScope().put(getHasInputControlsAttrName(), Boolean.valueOf(needsInput));
134
135         return needsInput ? yes() : no();
136     }
137
138     protected Map JavaDoc getParameterValues(RequestContext context, boolean requestParsed)
139     {
140         List JavaDoc wrappers = (List JavaDoc) context.getFlowScope().get(INPUTWRAPPERS_ATTR);
141         String JavaDoc reportUnitUri = (String JavaDoc) context.getFlowScope().get(getReportUnitAttrName());
142         return getParameterValues(context, wrappers, reportUnitUri, requestParsed);
143     }
144
145     protected Map JavaDoc getParameterValues(RequestContext context, List JavaDoc wrappers, String JavaDoc reportUnitUri, boolean requestParsed) {
146         boolean valid = requestParsed || parseRequest(context, wrappers, true);
147
148         Map JavaDoc parameterValues;
149         if (valid || wrappers.size() == 0) {
150             adjustDateTimezone(context, wrappers);
151             parameterValues = bindParameterValues(reportUnitUri, wrappers);
152             addCustomParameters(context, parameterValues);
153         } else {
154             parameterValues = null;
155         }
156
157         return parameterValues;
158     }
159
160     protected void adjustDateTimezone(RequestContext context, List JavaDoc wrappers) {
161         boolean convert = false;
162         int timezone = 0;
163         String JavaDoc tzParam = context.getRequestParameters().get("timezone");
164         if (tzParam != null && tzParam.length() > 0) {
165             try {
166                 timezone = Integer.parseInt(tzParam) * 60000;
167                 convert = true;
168             } catch (NumberFormatException JavaDoc e) {
169                 log.warn("Error parsing timezone offset. Ignoring client timezone", e);
170             }
171         }
172
173         for (int i = 0; i < wrappers.size(); i++) {
174             RuntimeInputControlWrapper wrapper = (RuntimeInputControlWrapper) wrappers.get(i);
175
176             Object JavaDoc value = wrapper.getValue();
177             if (!(value instanceof Date JavaDoc))
178                 continue;
179
180             Date JavaDoc clientDate = (Date JavaDoc) value;
181             wrapper.setClientDate(clientDate);
182             
183             if (convert) {
184                 int serverTimezone = TimeZone.getDefault().getOffset(clientDate.getTime());
185                 int dif = timezone + serverTimezone;
186                 Date JavaDoc serverDate = new Date JavaDoc(clientDate.getTime() + dif);
187                 wrapper.setValue(serverDate);
188             }
189         }
190     }
191
192     protected abstract void addCustomParameters(RequestContext context, Map JavaDoc parameterValues);
193
194     protected List JavaDoc createWrappers(RequestContext context, ReportUnit reportUnit) throws Exception JavaDoc
195     {
196         List JavaDoc wrappers = new ArrayList JavaDoc();
197
198         //TODO use other repository control that brings the required data
199
List JavaDoc inputControls = reportUnit.getInputControls();
200         for (int i = 0; i < inputControls.size(); i++)
201         {
202             ResourceReference inputControlRef = (ResourceReference) inputControls.get(i);
203             InputControl control;
204             if (inputControlRef.isLocal()) {
205                 control = (InputControl) inputControlRef.getLocalResource();
206             } else {
207                 control = (InputControl) repository.getResource(new ExecutionContextImpl(), inputControlRef.getReferenceURI());
208             }
209             
210             Format JavaDoc format = null;
211             ResourceReference dataTypeRef = control.getDataType();
212             if (dataTypeRef != null) {
213                 DataType dataType;
214                 if (!dataTypeRef.isLocal()) {
215                     dataType = (DataType) repository.getResource(new ExecutionContextImpl(), dataTypeRef.getReferenceURI());
216                     control.setDataType(dataType);
217                 }
218                 else
219                     dataType = (DataType) dataTypeRef.getLocalResource();
220
221                 switch (dataType.getType()) {
222                     case DataType.TYPE_DATE:
223                         format = getDateFormat(true);
224                         break;
225                     case DataType.TYPE_DATE_TIME:
226                         format = getDatetimeFormat(true);
227                         break;
228                     case DataType.TYPE_NUMBER:
229                         if (dataType.getRegularExpr() != null)
230                             format = new DecimalFormat JavaDoc(dataType.getRegularExpr());
231                 }
232             }
233             ResourceReference listOfValuesRef = control.getListOfValues();
234             if (listOfValuesRef != null && !listOfValuesRef.isLocal()) {
235                 ListOfValues listOfValues = (ListOfValues) repository.getResource(new ExecutionContextImpl(), listOfValuesRef.getReferenceURI());
236                 control.setListOfValues(listOfValues);
237             }
238
239             RuntimeInputControlWrapper wrapper = new RuntimeInputControlWrapper(control);
240             wrapper.setFormat(format);
241
242             ResourceReference queryRef = control.getQuery();
243             if (queryRef != null) {
244                 OrderedMap results = executeQuery(control.getQuery(), reportUnit.getDataSource(), wrapper);
245                 wrapper.setQueryResults(results);
246             }
247
248             wrappers.add(wrapper);
249         }
250
251         return wrappers;
252     }
253
254
255     /**
256      *
257      */

258     protected OrderedMap executeQuery(ResourceReference queryReference, ResourceReference dataSourceReference, RuntimeInputControlWrapper wrapper) throws Exception JavaDoc
259     {
260         InputControl control = wrapper.getInputControl();
261         String JavaDoc valueColumn = control.getQueryValueColumn();
262         String JavaDoc[] visibleColumns = control.getQueryVisibleColumns();
263
264         OrderedMap results = engine.executeQuery(null, queryReference, valueColumn, visibleColumns, dataSourceReference);
265         
266         if (results == null) {
267             return null;
268         }
269         
270         OrderedMap inputData = new LinkedMap();
271         for (Iterator JavaDoc it = results.entrySet().iterator(); it.hasNext();) {
272             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) it.next();
273             Object JavaDoc keyValue = entry.getKey();
274             String JavaDoc[] columnValues = (String JavaDoc[]) entry.getValue();
275
276             String JavaDoc columnValuesString = "";
277             if (columnValues != null && columnValues.length > 0) {
278                 StringBuffer JavaDoc visibleColumnBuffer = new StringBuffer JavaDoc();
279                 for (int i = 0; i < columnValues.length; i++) {
280                     visibleColumnBuffer.append(COLUMN_VALUE_SEPARATOR);
281                     visibleColumnBuffer.append(columnValues[i] != null ? columnValues[i] : "");
282                 }
283                 columnValuesString = visibleColumnBuffer.substring(COLUMN_VALUE_SEPARATOR_LENGTH);
284             }
285
286             inputData.put(keyValue.toString(), new Object JavaDoc[] {keyValue, columnValuesString});
287         }
288         
289         return inputData;
290     }
291
292
293     /**
294      *
295      */

296     protected boolean parseRequest(RequestContext context, List JavaDoc wrappers, boolean interactiveParameters)
297     {
298         boolean isValid = true;
299
300         for (int i = 0; i < wrappers.size(); i++)
301         {
302             RuntimeInputControlWrapper wrapper = (RuntimeInputControlWrapper) wrappers.get(i);
303             InputControl control = wrapper.getInputControl();
304
305             if (wrapper.isMulti()) {
306                 ParameterMap requestParameters = context.getRequestParameters();
307
308                 // workaround to requestParameters.getArray() throwing error on single param value. fixed in webflow 1.0-rc1
309
Object JavaDoc paramValue = requestParameters.getMap().get(control.getName());
310                 String JavaDoc[] values;
311                 if (paramValue == null) {
312                     values = null;
313                 }
314                 else if (paramValue.getClass().isArray()) {
315                     values = (String JavaDoc[]) paramValue;
316                 } else {
317                     values = new String JavaDoc[]{(String JavaDoc) paramValue};
318                 }
319                 
320                 parseRequestValues(values, wrapper, interactiveParameters);
321             } else {
322                 String JavaDoc value = context.getRequestParameters().get(control.getName());
323                 parseRequestValue(value, wrapper, interactiveParameters);
324             }
325
326             isValid = isValid && (wrapper.getErrorMessage() == null);
327         }
328
329         return isValid;
330     }
331
332     protected void parseRequestValue(String JavaDoc strValue, RuntimeInputControlWrapper wrapper, boolean interactiveParameters)
333     {
334         InputControl control = wrapper.getInputControl();
335         ResourceReference dataTypeRef = control.getDataType();
336         DataType dataType;
337         if (dataTypeRef == null) {
338             dataType = null;
339         } else if (dataTypeRef.isLocal()) {
340             dataType = (DataType) dataTypeRef.getLocalResource();
341         } else {
342             dataType = (DataType) repository.getResource(new ExecutionContextImpl(), dataTypeRef.getReferenceURI());
343         }
344
345         if (control.getType() == InputControl.TYPE_BOOLEAN)
346         {
347             wrapper.setValue(Boolean.valueOf(strValue != null));
348             return;
349         }
350
351
352         if (strValue != null && strValue.trim().length() == 0)
353         {
354             strValue = null;
355         }
356
357         if (control.isMandatory() && strValue == null)
358         {
359             wrapper.setErrorMessage(messages.getMessage("fillParameters.error.mandatoryField", null, Locale.getDefault()));
360             return;
361         }
362
363         if (control.getType() == InputControl.TYPE_SINGLE_SELECT_LIST_OF_VALUES
364                 || control.getType() == InputControl.TYPE_SINGLE_SELECT_LIST_OF_VALUES_RADIO) {
365             wrapper.setErrorMessage(null);
366             wrapper.setValue(getLovValue(wrapper, strValue));
367             return;
368         }
369
370         if (control.getType() == InputControl.TYPE_SINGLE_SELECT_QUERY
371                 || control.getType() == InputControl.TYPE_SINGLE_SELECT_QUERY_RADIO) {
372             wrapper.setErrorMessage(null);
373             wrapper.setValue(getQueryValue(wrapper, strValue));
374             return;
375         }
376
377         if (dataType == null || strValue == null)
378         {
379             wrapper.setErrorMessage(null);
380             wrapper.setValue(null);
381             return;
382         }
383
384         switch(dataType.getType())
385         {
386             case DataType.TYPE_TEXT :
387             {
388                 if (
389                     dataType.getMaxLength() != null
390                     && dataType.getMaxLength().intValue() < strValue.length()
391                     )
392                 {
393                     wrapper.setErrorMessage(messages.getMessage("fillParameters.error.invalidType", null, Locale.getDefault()));
394                 }
395                 else if (
396                     dataType.getRegularExpr() != null
397                     && dataType.getRegularExpr().trim().length() > 0
398                     && !Pattern.matches(dataType.getRegularExpr(), strValue)
399                     )
400                 {
401                     wrapper.setErrorMessage(messages.getMessage("fillParameters.error.invalidPattern", null, Locale.getDefault()));
402                 }
403                 else {
404                     wrapper.setErrorMessage(null);
405                 }
406                 wrapper.setValue(strValue);
407                 break;
408             }
409             case DataType.TYPE_NUMBER :
410             {
411                 //FIXME take care of input mask
412
try
413                 {
414                     wrapper.setValue(new BigDecimal JavaDoc(strValue));
415                     wrapper.setErrorMessage(null);
416                     return;
417                 }
418                 catch(NumberFormatException JavaDoc e)
419                 {
420                     wrapper.setErrorMessage(messages.getMessage("fillParameters.error.invalidFloat", null, Locale.getDefault()));
421                     wrapper.setValue(strValue);
422                 }
423                 break;
424             }
425             case DataType.TYPE_DATE :
426             {
427                 DateFormat JavaDoc format = getDateFormat(interactiveParameters);
428
429                 try
430                 {
431                     wrapper.setValue(format.parse(strValue));
432                     wrapper.setErrorMessage(null);
433                 }
434                 catch (ParseException JavaDoc e)
435                 {
436                     wrapper.setErrorMessage(messages.getMessage("fillParameters.error.invalidDate", null, LocaleContextHolder.getLocale()));
437                     wrapper.setValue(strValue);
438                 }
439                 break;
440             }
441             case DataType.TYPE_DATE_TIME :
442             {
443                 DateFormat JavaDoc format = getDatetimeFormat(interactiveParameters);
444
445                 try
446                 {
447                     wrapper.setValue(format.parse(strValue));
448                     wrapper.setErrorMessage(null);
449                 }
450                 catch (ParseException JavaDoc e)
451                 {
452                     wrapper.setErrorMessage(messages.getMessage("fillParameters.error.invalidDateTime", null, LocaleContextHolder.getLocale()));
453                     wrapper.setValue(strValue);
454                 }
455                 break;
456             }
457         }
458
459         if (wrapper.getErrorMessage() != null)
460             return;
461
462         Comparable JavaDoc value = (Comparable JavaDoc) wrapper.getValue();
463
464         if (dataType.getMinValue() != null) {
465             if (dataType.isStrictMin()) {
466                 if (dataType.getMinValue().compareTo(value) > 0)
467                     wrapper.setErrorMessage(messages.getMessage("fillParameters.error.smallerThan", null, LocaleContextHolder.getLocale()));
468             }
469             else if (dataType.getMinValue().compareTo(value) >= 0)
470                 wrapper.setErrorMessage(messages.getMessage("fillParameters.error.smallerOrEqual", null, LocaleContextHolder.getLocale()));
471         }
472
473         if (dataType.getMaxValue() != null) {
474             if (dataType.isStrictMax()) {
475                 if (dataType.getMaxValue().compareTo(value) < 0)
476                     wrapper.setErrorMessage(messages.getMessage("fillParameters.error.greaterThan", null, LocaleContextHolder.getLocale()));
477             }
478             else if (dataType.getMaxValue().compareTo(value) <= 0)
479                 wrapper.setErrorMessage(messages.getMessage("fillParameters.error.greaterOrEqual", null, LocaleContextHolder.getLocale()));
480         }
481
482         final Comparable JavaDoc minValue = dataType.getMinValue();
483         if (minValue != null
484                 && !((minValue instanceof String JavaDoc) && ((String JavaDoc) minValue).length() == 0)) {
485             if (dataType.isStrictMin()) {
486                 if (minValue.compareTo(value) > 0)
487                     wrapper.setErrorMessage(messages.getMessage(
488                             "fillParameters.error.smallerThan", null, Locale.getDefault()));
489             } else if (minValue.compareTo(value) >= 0)
490
491                 wrapper.setErrorMessage(messages.getMessage(
492                         "fillParameters.error.smallerOrEqual", null, Locale.getDefault()));
493         }
494
495         final Comparable JavaDoc maxValue = dataType.getMinValue();
496         if (maxValue != null
497                 && !((maxValue instanceof String JavaDoc) && ((String JavaDoc) maxValue).length() == 0)) {
498             if (dataType.isStrictMax()) {
499                 if (maxValue.compareTo(value) < 0)
500                     wrapper.setErrorMessage(messages.getMessage(
501                             "fillParameters.error.greaterThan", null, Locale.getDefault()));
502             } else if (maxValue.compareTo(value) <= 0)
503
504                 wrapper.setErrorMessage(messages.getMessage(
505                         "fillParameters.error.greaterOrEqual", null, Locale.getDefault()));
506         }
507         
508         wrapper.setValue(value);
509     }
510
511     
512     protected Object JavaDoc getLovValue(RuntimeInputControlWrapper wrapper, String JavaDoc strValue) {
513         return strValue;
514     }
515
516     
517     protected Object JavaDoc getQueryValue(RuntimeInputControlWrapper wrapper, String JavaDoc strValue) {
518         Object JavaDoc[] result = (Object JavaDoc[]) wrapper.getQueryResults().get(strValue);
519         Object JavaDoc value;
520         if (result != null) {
521             value = result[0];
522         } else {
523             value = null;
524         }
525         return value;
526     }
527
528     
529     protected void parseRequestValues(String JavaDoc[] strValues, RuntimeInputControlWrapper wrapper, boolean interactiveParameters) {
530         InputControl control = wrapper.getInputControl();
531         
532         if (strValues == null) {
533             strValues = new String JavaDoc[0];
534         }
535
536         if (control.isMandatory() && strValues.length == 0)
537         {
538             wrapper.setErrorMessage(messages.getMessage("fillParameters.error.mandatoryField", null, Locale.getDefault()));
539             return;
540         }
541
542         if (control.getType() == InputControl.TYPE_MULTI_SELECT_LIST_OF_VALUES
543                 || control.getType() == InputControl.TYPE_MULTI_SELECT_LIST_OF_VALUES_CHECKBOX) {
544             wrapper.setErrorMessage(null);
545             
546             Set JavaDoc values = new ListOrderedSet();
547             for (int i = 0; i < strValues.length; i++) {
548                 values.add(getLovValue(wrapper, strValues[i]));
549             }
550             wrapper.setValue(values);
551             return;
552         }
553
554         if (control.getType() == InputControl.TYPE_MULTI_SELECT_QUERY
555                 || control.getType() == InputControl.TYPE_MULTI_SELECT_QUERY_CHECKBOX) {
556             wrapper.setErrorMessage(null);
557             
558             Set JavaDoc values = new ListOrderedSet();
559             for (int i = 0; i < strValues.length; i++) {
560                 values.add(getQueryValue(wrapper, strValues[i]));
561             }
562             wrapper.setValue(values);
563             return;
564         }
565     }
566
567     
568     protected DateFormat JavaDoc getDateFormat(boolean interactiveParameters) {
569         DateFormat JavaDoc format;
570
571         if (interactiveParameters) {
572             format = getCalendarFormatProvider().getDateFormat();
573         } else {
574             format = new SimpleDateFormat JavaDoc(getStaticDatePattern());
575         }
576         return format;
577     }
578
579     protected DateFormat JavaDoc getDatetimeFormat(boolean interactiveParameters) {
580         DateFormat JavaDoc format;
581         if (interactiveParameters) {
582             format = getCalendarFormatProvider().getDatetimeFormat();
583         } else {
584             format = new SimpleDateFormat JavaDoc(getStaticDatePattern());
585         }
586         return format;
587     }
588
589
590     /**
591      * Converts BigDecimal numbers to the the type they should be based on
592      * what the JRParameters say.
593      *
594      * @todo add float and dobule
595      * @todo move this routine to a shared static location so others can use it
596      *
597      * @param reportName the name of the report
598      * @param wrappers Wrappers around InputControls which allow it to store values and error messages
599      */

600     protected Map JavaDoc bindParameterValues(String JavaDoc reportName, List JavaDoc wrappers)
601     {
602         Map JavaDoc parametersMap = new HashMap JavaDoc();
603
604         JRReport report = getEngine().getMainJasperReport(null, reportName);
605         JRParameter[] parameters = report.getParameters();
606         if (parameters != null)
607         {
608             for(int i = 0; i < parameters.length; i++)
609             {
610                 JRParameter parameter = parameters[i];
611                 parametersMap.put(parameter.getName(), parameter);
612             }
613         }
614
615         Map JavaDoc parameterValues = new HashMap JavaDoc();
616
617         for (Iterator JavaDoc it = wrappers.iterator(); it.hasNext();)
618         {
619             RuntimeInputControlWrapper wrapper = (RuntimeInputControlWrapper)it.next();
620             String JavaDoc parameterName = wrapper.getInputControl().getName();//FIXME naming convention to replace when adding mapping
621
JRParameter parameter = (JRParameter)parametersMap.get(parameterName);
622             if (parameter != null)
623             {
624                 Object JavaDoc value = wrapper.getValue();
625                 
626                 if (value != null) {
627                     if (Number JavaDoc.class.isAssignableFrom(parameter.getValueClass())) {
628                         value = getParameterNumberValue(wrapper, parameter, value);
629                     }
630                     
631                     if (wrapper.isMulti()) {
632                         value = getParameterMultiValue(wrapper, parameter, (Set JavaDoc) value);
633                     }
634                     
635                     parameterValues.put(parameterName, value);
636                 }
637             }
638         }
639         
640         return parameterValues;
641     }
642
643     protected Object JavaDoc getParameterNumberValue(RuntimeInputControlWrapper wrapper, JRParameter parameter, Object JavaDoc value) {
644         if (Byte JavaDoc.class.getName().equals(parameter.getValueClassName()))
645         {
646             value = new Byte JavaDoc(((Number JavaDoc)value).byteValue());
647         }
648         else if (Short JavaDoc.class.getName().equals(parameter.getValueClassName()))
649         {
650             value = new Short JavaDoc(((Number JavaDoc)value).shortValue());
651         }
652         else if (Integer JavaDoc.class.getName().equals(parameter.getValueClassName()))
653         {
654             value = new Integer JavaDoc(((Number JavaDoc)value).intValue());
655         }
656         else if (Long JavaDoc.class.getName().equals(parameter.getValueClassName()))
657         {
658             value = new Long JavaDoc(((Number JavaDoc)value).longValue());
659         }
660         else if (Float JavaDoc.class.getName().equals(parameter.getValueClassName()))
661         {
662             value = new Float JavaDoc(((Number JavaDoc)value).floatValue());
663         }
664         else if (Double JavaDoc.class.getName().equals(parameter.getValueClassName()))
665         {
666             value = new Double JavaDoc(((Double JavaDoc)value).longValue());
667         }
668         return value;
669     }
670
671     protected Object JavaDoc getParameterMultiValue(RuntimeInputControlWrapper wrapper, JRParameter parameter, Set JavaDoc values) {
672         Class JavaDoc paramType = parameter.getValueClass();
673         Collection JavaDoc value;
674         if (paramType.equals(Object JavaDoc.class)
675                 || paramType.equals(Collection JavaDoc.class)
676                 || paramType.equals(Set JavaDoc.class)) {
677             value = values;
678         } else if (paramType.equals(List JavaDoc.class)) {
679             value = new ArrayList JavaDoc(values);
680         } else {
681             throw new JSException("Unknown parameter type " + paramType.getName() + "for multiple value input");
682         }
683         return value;
684     }
685
686     public Event checkParameters(RequestContext context)
687     {
688         List JavaDoc wrappers = (List JavaDoc) context.getFlowScope().get(INPUTWRAPPERS_ATTR);
689         for (int i = 0; i < wrappers.size(); i++) {
690             RuntimeInputControlWrapper wrapper = (RuntimeInputControlWrapper) wrappers.get(i);
691             ResourceReference dataTypeRef = wrapper.getInputControl().getDataType();
692             DataType dataType;
693             if (dataTypeRef == null) {
694                 dataType = null;
695             } else if (dataTypeRef.isLocal()) {
696                 dataType = (DataType) dataTypeRef.getLocalResource();
697             } else {
698                 dataType = (DataType) repository.getResource(new ExecutionContextImpl(), dataTypeRef.getReferenceURI());
699             }
700             if (dataType != null && dataType.getType() == DataType.TYPE_DATE_TIME)
701                 wrapper.setValue(wrapper.getClientDate());
702         }
703
704         return success();
705     }
706     
707     
708     protected String JavaDoc getCalendarDatePattern()
709     {
710         return getCalendarFormatProvider().getCalendarDatePattern();
711     }
712     
713     
714     protected String JavaDoc getCalendarDatetimePattern()
715     {
716         return getCalendarFormatProvider().getCalendarDatetimePattern();
717     }
718
719     /*
720          * method to get the reposervice object arguments: none returns:
721          * RepositoryService
722          */

723     public RepositoryService getRepository() {
724         return repository;
725     }
726
727     /*
728      * method to set the reposervice object arguments: RepositoryService
729      * returns: void
730      */

731     public void setRepository(RepositoryService repository) {
732         this.repository = repository;
733     }
734
735
736     public MessageSource getMessages()
737     {
738         return messages;
739     }
740
741     public void setMessages(MessageSource messages)
742     {
743         this.messages = messages;
744     }
745
746     public EngineService getEngine() {
747         return engine;
748     }
749
750     public void setEngine(EngineService engine) {
751         this.engine = engine;
752     }
753
754     public String JavaDoc getReportUnitAttrName() {
755         return reportUnitAttrName;
756     }
757
758     public void setReportUnitAttrName(String JavaDoc reportUnitAttrName) {
759         this.reportUnitAttrName = reportUnitAttrName;
760     }
761
762     public String JavaDoc getHasInputControlsAttrName() {
763         return hasInputControlsAttrName;
764     }
765
766     public void setHasInputControlsAttrName(String JavaDoc hasInputControlsAttrName) {
767         this.hasInputControlsAttrName = hasInputControlsAttrName;
768     }
769
770     public String JavaDoc getStaticDatePattern() {
771         return staticDatePattern;
772     }
773
774     public void setStaticDatePattern(String JavaDoc staticDatePattern) {
775         this.staticDatePattern = staticDatePattern;
776     }
777     
778     /**
779      * @return Returns the reportUnitObjectAttrName.
780      */

781     public String JavaDoc getReportUnitObjectAttrName() {
782         return reportUnitObjectAttrName;
783     }
784
785     /**
786      * @param reportUnitObjectAttrName The reportUnitObjectAttrName to set.
787      */

788     public void setReportUnitObjectAttrName(String JavaDoc reportUnitObjectAttrName) {
789         this.reportUnitObjectAttrName = reportUnitObjectAttrName;
790     }
791
792     /**
793      * @return Returns the controlsDisplayFormAttrName.
794      */

795     public String JavaDoc getControlsDisplayFormAttrName() {
796         return controlsDisplayFormAttrName;
797     }
798
799     /**
800      * @param controlsDisplayFormName The controlsDisplayFormName to set.
801      */

802     public void setControlsDisplayFormAttrName(String JavaDoc controlsDisplayFormAttrName) {
803         this.controlsDisplayFormAttrName = controlsDisplayFormAttrName;
804     }
805
806     /**
807      * @return Returns the reportDisplayFormAttrName.
808      */

809     public String JavaDoc getReportDisplayFormAttrName() {
810         return reportDisplayFormAttrName;
811     }
812
813     /**
814      * @param reportDisplayFormAttrName The reportDisplayFormAttrName to set.
815      */

816     public void setReportDisplayFormAttrName(String JavaDoc reportDisplayFormAttrName) {
817         this.reportDisplayFormAttrName = reportDisplayFormAttrName;
818     }
819     
820     /**
821      * @return Returns the calendarDatePatternAttrName.
822      */

823     public String JavaDoc getCalendarDatePatternAttrName()
824     {
825         return calendarDatePatternAttrName;
826     }
827
828     /**
829      * @param calendarDatePatternAttrName The calendarDatePatternAttrName to set.
830      */

831     public void setCalendarDatePatternAttrName(String JavaDoc calendarDatePatternAttrName)
832     {
833         this.calendarDatePatternAttrName = calendarDatePatternAttrName;
834     }
835
836     public CalendarFormatProvider getCalendarFormatProvider() {
837         return calendarFormatProvider;
838     }
839
840     public void setCalendarFormatProvider(
841             CalendarFormatProvider calendarFormatProvider) {
842         this.calendarFormatProvider = calendarFormatProvider;
843     }
844
845     public String JavaDoc getCalendarDatetimePatternAttrName() {
846         return calendarDatetimePatternAttrName;
847     }
848
849     public void setCalendarDatetimePatternAttrName(
850             String JavaDoc calendarDatetimePatternAttrName) {
851         this.calendarDatetimePatternAttrName = calendarDatetimePatternAttrName;
852     }
853 }
854
Popular Tags