KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > efs > openreports > util > ORUtil


1 /*
2  * Copyright (C) 2004 Erik Swenson - erik@oreports.com
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License as published by the Free
6  * Software Foundation; either version 2 of the License, or (at your option)
7  * any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12  * more details.
13  *
14  * You should have received a copy of the GNU General Public License along with
15  * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
16  * Place - Suite 330, Boston, MA 02111-1307, USA.
17  *
18  */

19
20 package org.efs.openreports.util;
21
22 import java.util.*;
23
24 import org.efs.openreports.ORException;
25 import org.efs.openreports.ORStatics;
26 import org.efs.openreports.objects.ReportParameter;
27 import org.efs.openreports.objects.ReportParameterMap;
28 import org.efs.openreports.objects.ReportUser;
29 import org.efs.openreports.providers.ParameterProvider;
30
31 import net.sf.jasperreports.engine.design.JRDesignParameter;
32
33 public class ORUtil
34 {
35     public static Map buildJRDesignParameters(Map parameters)
36     {
37         // convert parameters to JRDesignParameters so they can be parsed
38
HashMap jrParameters = new HashMap();
39
40         Iterator iterator = parameters.keySet().iterator();
41         while (iterator.hasNext())
42         {
43             String JavaDoc key = (String JavaDoc) iterator.next();
44             Object JavaDoc value = parameters.get(key);
45             
46             if (value != null)
47             {
48                 JRDesignParameter jrParameter = new JRDesignParameter();
49                 jrParameter.setName(key);
50                 jrParameter.setValueClass(value.getClass());
51                 
52                 jrParameters.put(jrParameter.getName(), jrParameter);
53             }
54         }
55
56         return jrParameters;
57     }
58     
59     /*
60      * Build map containing the parameter name and a test value in order to validate
61      * queries with parameters.
62      */

63     public static Map buildQueryParameterMap(ReportUser reportUser, String JavaDoc queryString, ParameterProvider parameterProvider) throws ORException
64     {
65         String JavaDoc name = queryString.substring(queryString.indexOf("{") + 1, queryString.indexOf("}"));
66         
67         //handle standard report parameters
68
if (name.equals(ORStatics.EXTERNAL_ID))
69         {
70             HashMap map = new HashMap();
71             map.put(ORStatics.EXTERNAL_ID, reportUser.getExternalId());
72             
73             return map;
74         }
75         else if (name.equals(ORStatics.USER_ID))
76         {
77             HashMap map = new HashMap();
78             map.put(ORStatics.USER_ID, reportUser.getId());
79             
80             return map;
81         }
82         else if (name.equals(ORStatics.USER_NAME))
83         {
84             HashMap map = new HashMap();
85             map.put(ORStatics.USER_NAME, reportUser.getName());
86             
87             return map;
88         }
89         //
90

91         ReportParameter queryParameter = parameterProvider.getReportParameter(name);
92         if (queryParameter == null)
93         {
94             throw new ORException(LocalStrings.getString(LocalStrings.ERROR_PARAMETER_NOTFOUND) + ": " + name);
95         }
96         
97         ReportParameterMap rpMap = new ReportParameterMap();
98         rpMap.setReportParameter(queryParameter);
99         
100         ArrayList queryParameters = new ArrayList();
101         queryParameters.add(rpMap);
102         
103         Map parameterMap = new HashMap();
104         if (queryParameter.getData().toUpperCase().indexOf("$P") > -1)
105         {
106             parameterMap = buildQueryParameterMap(reportUser, queryParameter.getData(), parameterProvider);
107         }
108         
109         parameterProvider.loadReportParameterValues(queryParameters, parameterMap);
110         
111         String JavaDoc testValue = queryParameter.getValues()[0].getId().toString();
112         if (queryParameter.isMultipleSelect())
113         {
114             testValue = "'" + testValue + "'";
115         }
116         
117         Map map = new HashMap();
118         map.put(queryParameter.getName(), testValue);
119         
120         return map;
121     }
122
123 }
Popular Tags