KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > jasperreports > engine > query > JRAbstractQueryExecuter


1 /*
2  * ============================================================================
3  * GNU Lesser General Public License
4  * ============================================================================
5  *
6  * JasperReports - Free Java report-generating library.
7  * Copyright (C) 2001-2006 JasperSoft Corporation http://www.jaspersoft.com
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
22  *
23  * JasperSoft Corporation
24  * 303 Second Street, Suite 450 North
25  * San Francisco, CA 94107
26  * http://www.jaspersoft.com
27  */

28 package net.sf.jasperreports.engine.query;
29
30 import java.util.ArrayList JavaDoc;
31 import java.util.List JavaDoc;
32 import java.util.Map JavaDoc;
33
34 import net.sf.jasperreports.engine.JRDataset;
35 import net.sf.jasperreports.engine.JRQuery;
36 import net.sf.jasperreports.engine.JRQueryChunk;
37 import net.sf.jasperreports.engine.JRRuntimeException;
38 import net.sf.jasperreports.engine.JRValueParameter;
39 import net.sf.jasperreports.engine.fill.JRFillParameter;
40
41 /**
42  * Base abstract query executer.
43  *
44  * @author Lucian Chirita (lucianc@users.sourceforge.net)
45  * @version $Id: JRAbstractQueryExecuter.java 1401 2006-09-21 11:04:34 +0300 (Thu, 21 Sep 2006) lucianc $
46  */

47 public abstract class JRAbstractQueryExecuter implements JRQueryExecuter
48 {
49     protected final JRDataset dataset;
50     private final Map JavaDoc parametersMap;
51     
52     private String JavaDoc queryString;
53     private List JavaDoc parameterNames;
54     
55     
56     protected JRAbstractQueryExecuter(JRDataset dataset, Map JavaDoc parametersMap)
57     {
58         this.dataset = dataset;
59         this.parametersMap = parametersMap;
60         
61         queryString = "";
62         parameterNames = new ArrayList JavaDoc();
63     }
64     
65     
66     /**
67      * Parses the query and replaces the parameter clauses by the paramter values and
68      * the parameters by the return value of {@link #getParameterReplacement(String) getParameterReplacement}.
69      *
70      */

71     protected void parseQuery()
72     {
73         JRQuery query = dataset.getQuery();
74         
75         if (query != null)
76         {
77             JRQueryChunk[] chunks = query.getChunks();
78             if (chunks != null && chunks.length > 0)
79             {
80                 StringBuffer JavaDoc sbuffer = new StringBuffer JavaDoc();
81                 JRQueryChunk chunk = null;
82                 for(int i = 0; i < chunks.length; i++)
83                 {
84                     chunk = chunks[i];
85                     switch (chunk.getType())
86                     {
87                         case JRQueryChunk.TYPE_PARAMETER_CLAUSE :
88                         {
89                             String JavaDoc parameterName = chunk.getText();
90                             Object JavaDoc parameterValue = getParameterValue(parameterName);
91                             sbuffer.append(String.valueOf(parameterValue));
92                             break;
93                         }
94                         case JRQueryChunk.TYPE_PARAMETER :
95                         {
96                             sbuffer.append(getParameterReplacement(chunk.getText()));
97                             parameterNames.add(chunk.getText());
98                             break;
99                         }
100                         case JRQueryChunk.TYPE_TEXT :
101                         default :
102                         {
103                             sbuffer.append(chunk.getText());
104                             break;
105                         }
106                     }
107                 }
108
109                 queryString = sbuffer.toString();
110             }
111         }
112     }
113
114     
115     /**
116      * Returns the parsed query string with the paramter clauses replaced by the paramter values and
117      * the parameters replaced by {@link #getParameterReplacement(String) getParameterReplacement}.
118      *
119      * @return the parsed query string
120      */

121     protected String JavaDoc getQueryString()
122     {
123         return queryString;
124     }
125     
126     
127     /**
128      * Returns the list of parameter names in the order in which they appear in the query.
129      *
130      * @return the list of parameter names
131      */

132     protected List JavaDoc getCollectedParameterNames()
133     {
134         return parameterNames;
135     }
136     
137     
138     /**
139      * Returns the value of a fill paramter.
140      * @param parameterName the paramter name
141      * @return the parameter value
142      */

143     protected Object JavaDoc getParameterValue(String JavaDoc parameterName)
144     {
145         JRValueParameter parameter = getValueParameter(parameterName);
146         return parameter.getValue();
147     }
148     
149     
150     /**
151      * Return a fill parameter from the paramter map.
152      *
153      * @param parameterName the paramter name
154      * @return the parameter
155      * @deprecated {@link #getValueParameter(String) getValueParameter(String)} should be used instead
156      */

157     protected JRFillParameter getParameter(String JavaDoc parameterName)
158     {
159         JRFillParameter parameter = (JRFillParameter) parametersMap.get(parameterName);
160         
161         if (parameter == null)
162         {
163             throw new JRRuntimeException("Parameter \"" + parameterName + "\" does not exist.");
164         }
165         
166         return parameter;
167     }
168
169     
170     /**
171      * Return a value parameter from the paramters map.
172      *
173      * @param parameterName the paramter name
174      * @return the parameter
175      */

176     protected JRValueParameter getValueParameter(String JavaDoc parameterName)
177     {
178         JRValueParameter parameter = (JRValueParameter) parametersMap.get(parameterName);
179         
180         if (parameter == null)
181         {
182             throw new JRRuntimeException("Parameter \"" + parameterName + "\" does not exist.");
183         }
184         
185         return parameter;
186     }
187
188     
189     /**
190      * Returns the replacement text for a query paramter.
191      *
192      * @param parameterName the paramter name
193      * @return the replacement text
194      * @see JRQueryChunk#TYPE_PARAMETER
195      */

196     protected abstract String JavaDoc getParameterReplacement(String JavaDoc parameterName);
197 }
198
Popular Tags