KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > jasperreports > engine > design > JRDesignDataset


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.design;
29
30 import java.beans.PropertyChangeEvent JavaDoc;
31 import java.beans.PropertyChangeListener JavaDoc;
32 import java.io.Serializable JavaDoc;
33 import java.net.URLStreamHandlerFactory JavaDoc;
34 import java.sql.Connection JavaDoc;
35 import java.util.ArrayList JavaDoc;
36 import java.util.Collections JavaDoc;
37 import java.util.Comparator JavaDoc;
38 import java.util.HashMap JavaDoc;
39 import java.util.List JavaDoc;
40 import java.util.ListIterator JavaDoc;
41 import java.util.Locale JavaDoc;
42 import java.util.Map JavaDoc;
43 import java.util.ResourceBundle JavaDoc;
44 import java.util.TimeZone JavaDoc;
45
46 import net.sf.jasperreports.engine.JRAbstractScriptlet;
47 import net.sf.jasperreports.engine.JRConstants;
48 import net.sf.jasperreports.engine.JRDataSource;
49 import net.sf.jasperreports.engine.JRDataset;
50 import net.sf.jasperreports.engine.JRException;
51 import net.sf.jasperreports.engine.JRExpression;
52 import net.sf.jasperreports.engine.JRField;
53 import net.sf.jasperreports.engine.JRGroup;
54 import net.sf.jasperreports.engine.JRParameter;
55 import net.sf.jasperreports.engine.JRRuntimeException;
56 import net.sf.jasperreports.engine.JRSortField;
57 import net.sf.jasperreports.engine.JRVariable;
58 import net.sf.jasperreports.engine.JRVirtualizer;
59 import net.sf.jasperreports.engine.base.JRBaseDataset;
60 import net.sf.jasperreports.engine.query.JRQueryExecuterFactory;
61 import net.sf.jasperreports.engine.util.FormatFactory;
62 import net.sf.jasperreports.engine.util.JRQueryExecuterUtils;
63
64 /**
65  * Implementation of {@link net.sf.jasperreports.engine.JRDataset JRDataset} to be used for report desing.
66  *
67  * @author Lucian Chirita (lucianc@users.sourceforge.net)
68  * @version $Id: JRDesignDataset.java 1485 2006-11-14 20:23:17 +0200 (Tue, 14 Nov 2006) teodord $
69  */

70 public class JRDesignDataset extends JRBaseDataset
71 {
72     private static final long serialVersionUID = JRConstants.SERIAL_VERSION_UID;
73
74     /**
75      * Parameters mapped by name.
76      */

77     protected Map JavaDoc parametersMap = new HashMap JavaDoc();
78     protected List JavaDoc parametersList = new ArrayList JavaDoc();
79
80     /**
81      * Fields mapped by name.
82      */

83     protected Map JavaDoc fieldsMap = new HashMap JavaDoc();
84     protected List JavaDoc fieldsList = new ArrayList JavaDoc();
85
86
87     /**
88      * Sort fields mapped by name.
89      */

90     protected Map JavaDoc sortFieldsMap = new HashMap JavaDoc();
91     protected List JavaDoc sortFieldsList = new ArrayList JavaDoc();
92
93
94     /**
95      * Variables mapped by name.
96      */

97     protected Map JavaDoc variablesMap = new HashMap JavaDoc();
98     protected List JavaDoc variablesList = new ArrayList JavaDoc();
99
100
101     /**
102      * Groups mapped by name.
103      */

104     protected Map JavaDoc groupsMap = new HashMap JavaDoc();
105     protected List JavaDoc groupsList = new ArrayList JavaDoc();
106
107     private class QueryLanguageChangeListener implements PropertyChangeListener JavaDoc, Serializable JavaDoc
108     {
109         private static final long serialVersionUID = JRConstants.SERIAL_VERSION_UID;
110
111         public void propertyChange(PropertyChangeEvent JavaDoc evt)
112         {
113             queryLanguageChanged((String JavaDoc) evt.getOldValue(), (String JavaDoc) evt.getNewValue());
114         }
115     }
116     
117     private PropertyChangeListener JavaDoc queryLanguageChangeListener = new QueryLanguageChangeListener();
118     
119     
120     /**
121      * An array containing the built-in parameters that can be found and used in any report dataset.
122      */

123     private static final Object JavaDoc[] BUILT_IN_PARAMETERS = new Object JavaDoc[] {
124         JRParameter.REPORT_PARAMETERS_MAP, java.util.Map JavaDoc.class,
125         JRParameter.REPORT_CONNECTION, Connection JavaDoc.class,
126         JRParameter.REPORT_MAX_COUNT, Integer JavaDoc.class,
127         JRParameter.REPORT_DATA_SOURCE, JRDataSource.class,
128         JRParameter.REPORT_SCRIPTLET, JRAbstractScriptlet.class,
129         JRParameter.REPORT_LOCALE, Locale JavaDoc.class,
130         JRParameter.REPORT_RESOURCE_BUNDLE, ResourceBundle JavaDoc.class,
131         JRParameter.REPORT_TIME_ZONE, TimeZone JavaDoc.class,
132         JRParameter.REPORT_FORMAT_FACTORY, FormatFactory.class,
133         JRParameter.REPORT_CLASS_LOADER, ClassLoader JavaDoc.class,
134         JRParameter.REPORT_URL_HANDLER_FACTORY, URLStreamHandlerFactory JavaDoc.class};
135
136
137     
138     /**
139      * An array containing the built-in parameters that can be found and used in any report/main dataset.
140      */

141     private static final Object JavaDoc[] BUILT_IN_PARAMETERS_MAIN = new Object JavaDoc[] {
142         JRParameter.REPORT_VIRTUALIZER, JRVirtualizer.class,
143         JRParameter.IS_IGNORE_PAGINATION, Boolean JavaDoc.class };
144
145     
146     /**
147      * Create a dataset.
148      *
149      * @param isMain whether this is the main dataset of the report or a sub dataset
150      * @see net.sf.jasperreports.engine.JRDataset#isMainDataset()
151      */

152     public JRDesignDataset(boolean isMain)
153     {
154         super(isMain);
155         
156         addBuiltinParameters(BUILT_IN_PARAMETERS);
157         
158         if (isMain)
159         {
160             addBuiltinParameters(BUILT_IN_PARAMETERS_MAIN);
161         }
162
163         try
164         {
165             if (isMain)
166             {
167                 addVariable(createPageNumberVariable());
168                 addVariable(createColumnNumberVariable());
169             }
170             addVariable(createReportCountVariable());
171             if (isMain)
172             {
173                 addVariable(createPageCountVariable());
174                 addVariable(createColumnCountVariable());
175             }
176         }
177         catch (JRException e)
178         {
179             //never reached
180
}
181     }
182
183     private static JRDesignVariable createPageCountVariable()
184     {
185         JRDesignExpression expression;
186         JRDesignVariable variable;
187         variable = new JRDesignVariable();
188         variable.setName(JRVariable.PAGE_COUNT);
189         variable.setValueClass(Integer JavaDoc.class);
190         variable.setResetType(JRVariable.RESET_TYPE_PAGE);
191         variable.setCalculation(JRVariable.CALCULATION_COUNT);
192         variable.setSystemDefined(true);
193         expression = new JRDesignExpression();
194         expression.setValueClass(Integer JavaDoc.class);
195         expression.setText("new Integer(1)");
196         variable.setExpression(expression);
197         expression = new JRDesignExpression();
198         expression.setValueClass(Integer JavaDoc.class);
199         expression.setText("new Integer(0)");
200         variable.setInitialValueExpression(expression);
201         return variable;
202     }
203
204     private static JRDesignVariable createColumnNumberVariable()
205     {
206         JRDesignExpression expression;
207         JRDesignVariable variable;
208         variable = new JRDesignVariable();
209         variable.setName(JRVariable.COLUMN_NUMBER);
210         variable.setValueClass(Integer JavaDoc.class);
211         //variable.setResetType(JRVariable.RESET_TYPE_COLUMN);
212
variable.setResetType(JRVariable.RESET_TYPE_PAGE);
213         variable.setCalculation(JRVariable.CALCULATION_SYSTEM);
214         variable.setSystemDefined(true);
215         expression = new JRDesignExpression();
216         expression.setValueClass(Integer JavaDoc.class);
217         //expression.setText("($V{COLUMN_NUMBER} != null)?(new Integer($V{COLUMN_NUMBER}.intValue() + 1)):(new Integer(1))");
218
expression.setText("new Integer(1)");
219         variable.setInitialValueExpression(expression);
220         return variable;
221     }
222
223     private static JRDesignVariable createPageNumberVariable()
224     {
225         JRDesignVariable variable = new JRDesignVariable();
226         variable.setName(JRVariable.PAGE_NUMBER);
227         variable.setValueClass(Integer JavaDoc.class);
228         //variable.setResetType(JRVariable.RESET_TYPE_PAGE);
229
variable.setResetType(JRVariable.RESET_TYPE_REPORT);
230         variable.setCalculation(JRVariable.CALCULATION_SYSTEM);
231         variable.setSystemDefined(true);
232         JRDesignExpression expression = new JRDesignExpression();
233         expression.setValueClass(Integer JavaDoc.class);
234         //expression.setText("($V{PAGE_NUMBER} != null)?(new Integer($V{PAGE_NUMBER}.intValue() + 1)):(new Integer(1))");
235
expression.setText("new Integer(1)");
236         variable.setInitialValueExpression(expression);
237         return variable;
238     }
239
240     private static JRDesignVariable createColumnCountVariable()
241     {
242         JRDesignVariable variable;
243         JRDesignExpression expression;
244         variable = new JRDesignVariable();
245         variable.setName(JRVariable.COLUMN_COUNT);
246         variable.setValueClass(Integer JavaDoc.class);
247         variable.setResetType(JRVariable.RESET_TYPE_COLUMN);
248         variable.setCalculation(JRVariable.CALCULATION_COUNT);
249         variable.setSystemDefined(true);
250         expression = new JRDesignExpression();
251         expression.setValueClass(Integer JavaDoc.class);
252         expression.setText("new Integer(1)");
253         variable.setExpression(expression);
254         expression = new JRDesignExpression();
255         expression.setValueClass(Integer JavaDoc.class);
256         expression.setText("new Integer(0)");
257         variable.setInitialValueExpression(expression);
258         return variable;
259     }
260
261     private void addBuiltinParameters(Object JavaDoc[] parametersArray)
262     {
263         for (int i = 0; i < parametersArray.length; i++)
264         {
265             JRDesignParameter parameter = new JRDesignParameter();
266             parameter.setName((String JavaDoc) parametersArray[i++]);
267             parameter.setValueClass((Class JavaDoc) parametersArray[i]);
268             parameter.setSystemDefined(true);
269             try
270             {
271                 addParameter(parameter);
272             }
273             catch (JRException e)
274             {
275                 // never reached
276
}
277         }
278     }
279
280     private static JRDesignVariable createReportCountVariable()
281     {
282         JRDesignVariable variable = new JRDesignVariable();
283         variable.setName(JRVariable.REPORT_COUNT);
284         variable.setValueClass(Integer JavaDoc.class);
285         variable.setResetType(JRVariable.RESET_TYPE_REPORT);
286         variable.setCalculation(JRVariable.CALCULATION_COUNT);
287         variable.setSystemDefined(true);
288         JRDesignExpression expression = new JRDesignExpression();
289         expression.setValueClass(Integer JavaDoc.class);
290         expression.setText("new Integer(1)");
291         variable.setExpression(expression);
292         expression = new JRDesignExpression();
293         expression.setValueClass(Integer JavaDoc.class);
294         expression.setText("new Integer(0)");
295         variable.setInitialValueExpression(expression);
296         return variable;
297     }
298
299     
300     /**
301      * Sets the name of the dataset.
302      * @param name the name of the dataset
303      * @see net.sf.jasperreports.engine.JRDataset#getName()
304      */

305     public void setName(String JavaDoc name)
306     {
307         this.name = name;
308     }
309
310     
311     
312     public JRParameter[] getParameters()
313     {
314         JRParameter[] parametersArray = new JRParameter[parametersList.size()];
315
316         parametersList.toArray(parametersArray);
317
318         return parametersArray;
319     }
320
321     
322     /**
323      * Returns the list of parameters, including build-in ones.
324      *
325      * @return list of {@link JRParameter JRParameter} objects
326      */

327     public List JavaDoc getParametersList()
328     {
329         return parametersList;
330     }
331
332     
333     /**
334      * Returns the map of parameters, including build-in ones, indexed by name.
335      *
336      * @return {@link JRParameter JRParameter} objects indexed by name
337      */

338     public Map JavaDoc getParametersMap()
339     {
340         return parametersMap;
341     }
342
343     
344     /**
345      * Adds a parameter to the dataset.
346      * @param parameter the parameter to add
347      * @throws JRException
348      * @see net.sf.jasperreports.engine.JRDataset#getParameters()
349      */

350     public void addParameter(JRParameter parameter) throws JRException
351     {
352         if (parametersMap.containsKey(parameter.getName()))
353         {
354             throw new JRException("Duplicate declaration of parameter : " + parameter.getName());
355         }
356
357         parametersList.add(parameter);
358         parametersMap.put(parameter.getName(), parameter);
359     }
360
361     
362     /**
363      * Removes a parameter from the dataset.
364      *
365      * @param parameterName the parameter name
366      * @return the removed parameter, or <code>null</code> if the parameter was not found
367      */

368     public JRParameter removeParameter(String JavaDoc parameterName)
369     {
370         return removeParameter((JRParameter) parametersMap.get(parameterName));
371     }
372
373     
374     /**
375      * Removes a parameter from the dataset.
376      *
377      * @param parameter the parameter to be removed
378      * @return the parameter to be removed
379      */

380     public JRParameter removeParameter(JRParameter parameter)
381     {
382         if (parameter != null)
383         {
384             parametersList.remove(parameter);
385             parametersMap.remove(parameter.getName());
386         }
387
388         return parameter;
389     }
390
391     
392     /**
393      * Sets the dataset query.
394      *
395      * @param query the query
396      * @see net.sf.jasperreports.engine.JRDataset#getQuery()
397      */

398     public void setQuery(JRDesignQuery query)
399     {
400         String JavaDoc oldLanguage = null;
401         if (this.query != null)
402         {
403             ((JRDesignQuery) this.query).removePropertyChangeListener(JRDesignQuery.PROPERTY_LANGUAGE, queryLanguageChangeListener);
404             oldLanguage = this.query.getLanguage();
405         }
406         this.query = query;
407         String JavaDoc newLanguage = null;
408         if (query != null)
409         {
410             query.addPropertyChangeListener(JRDesignQuery.PROPERTY_LANGUAGE, queryLanguageChangeListener);
411             newLanguage = query.getLanguage();
412         }
413         queryLanguageChanged(oldLanguage, newLanguage);
414     }
415
416     
417     /**
418      * Sets the scriptlet class name.
419      * <p>
420      * If no scriptlet class name is specified, a default scriptlet is used.
421      *
422      * @param scriptletClass the class name of the scriptlet
423      * @see net.sf.jasperreports.engine.JRDataset#getScriptletClass()
424      */

425     public void setScriptletClass(String JavaDoc scriptletClass)
426     {
427         this.scriptletClass = scriptletClass;
428         if (scriptletClass == null)
429         {
430             ((JRDesignParameter) parametersMap.get(JRParameter.REPORT_SCRIPTLET)).setValueClass(JRAbstractScriptlet.class);
431         }
432         else
433         {
434             ((JRDesignParameter) parametersMap.get(JRParameter.REPORT_SCRIPTLET)).setValueClassName(scriptletClass);
435         }
436     }
437
438     
439     public JRField[] getFields()
440     {
441         JRField[] fieldsArray = new JRField[fieldsList.size()];
442
443         fieldsList.toArray(fieldsArray);
444
445         return fieldsArray;
446     }
447
448
449     /**
450      * Returns the list of fields.
451      *
452      * @return list of {@link JRField JRField} objects
453      */

454     public List JavaDoc getFieldsList()
455     {
456         return fieldsList;
457     }
458
459     
460     /**
461      * Returns the map of fields indexed by name.
462      *
463      * @return {@link JRField JRField} objects indexed by name
464      */

465     public Map JavaDoc getFieldsMap()
466     {
467         return fieldsMap;
468     }
469
470     
471     /**
472      * Adds a field to the dataset.
473      * @param field the field to add
474      * @throws JRException
475      * @see net.sf.jasperreports.engine.JRDataset#getFields()
476      */

477     public void addField(JRField field) throws JRException
478     {
479         if (fieldsMap.containsKey(field.getName()))
480         {
481             throw new JRException("Duplicate declaration of field : " + field.getName());
482         }
483
484         fieldsList.add(field);
485         fieldsMap.put(field.getName(), field);
486     }
487
488     
489     /**
490      * Removes a field from the dataset.
491      *
492      * @param fieldName the field name
493      * @return the removed field, or <code>null</code> if the field was not found
494      */

495     public JRField removeField(String JavaDoc fieldName)
496     {
497         return removeField((JRField) fieldsMap.get(fieldName));
498     }
499
500     
501     /**
502      * Removes a field from the dataset.
503      *
504      * @param field the field to be removed
505      * @return the field to be removed
506      */

507     public JRField removeField(JRField field)
508     {
509         if (field != null)
510         {
511             fieldsList.remove(field);
512             fieldsMap.remove(field.getName());
513         }
514
515         return field;
516     }
517
518     
519     public JRSortField[] getSortFields()
520     {
521         JRSortField[] sortFieldsArray = new JRSortField[sortFieldsList.size()];
522
523         sortFieldsList.toArray(sortFieldsArray);
524
525         return sortFieldsArray;
526     }
527
528
529     /**
530      * Returns the list of sort fields.
531      *
532      * @return list of {@link JRSortField JRSortField} objects
533      */

534     public List JavaDoc getSortFieldsList()
535     {
536         return sortFieldsList;
537     }
538
539     
540     /**
541      * Adds a sort field to the dataset.
542      * @param sortField the sort field to add
543      * @throws JRException
544      * @see net.sf.jasperreports.engine.JRDataset#getSortFields()
545      */

546     public void addSortField(JRSortField sortField) throws JRException
547     {
548         if (sortFieldsMap.containsKey(sortField.getName()))
549         {
550             throw new JRException("Duplicate declaration of sort field : " + sortField.getName());
551         }
552
553         sortFieldsList.add(sortField);
554         sortFieldsMap.put(sortField.getName(), sortField);
555     }
556
557     
558     /**
559      * Removes a sort field from the dataset.
560      *
561      * @param fieldName the field name
562      * @return the removed sort field, or <code>null</code> if the sort field was not found
563      */

564     public JRSortField removeSortField(String JavaDoc fieldName)
565     {
566         return removeSortField((JRSortField) sortFieldsMap.get(fieldName));
567     }
568
569     
570     /**
571      * Removes a sort field from the dataset.
572      *
573      * @param sortField the sort field to be removed
574      * @return the sort field to be removed
575      */

576     public JRSortField removeSortField(JRSortField sortField)
577     {
578         if (sortField != null)
579         {
580             sortFieldsList.remove(sortField);
581             sortFieldsMap.remove(sortField.getName());
582         }
583
584         return sortField;
585     }
586
587     
588     public JRVariable[] getVariables()
589     {
590         JRVariable[] variablesArray = new JRVariable[variablesList.size()];
591
592         variablesList.toArray(variablesArray);
593
594         return variablesArray;
595     }
596
597     
598     /**
599      * Returns the list of variables, including build-in ones.
600      *
601      * @return list of {@link JRVariable JRVariable} objects
602      */

603     
604     public List JavaDoc getVariablesList()
605     {
606         return variablesList;
607     }
608
609     
610     /**
611      * Returns the map of variable, including build-in ones, indexed by name.
612      *
613      * @return {@link JRVariable JRVariable} objects indexed by name
614      */

615     public Map JavaDoc getVariablesMap()
616     {
617         return variablesMap;
618     }
619
620     
621     /**
622      * Adds a variable to the dataset.
623      * @param variable the variable to add
624      * @throws JRException
625      * @see net.sf.jasperreports.engine.JRDataset#getVariables()
626      */

627     public void addVariable(JRDesignVariable variable) throws JRException
628     {
629         addVariable(variable, false);
630     }
631     
632     
633     /**
634      * Adds a variable to the dataset.
635      *
636      * @param variable the variable to add
637      * @param system whether the variable should be added before user defined variables
638      * or at the end of the variables list
639      * @throws JRException
640      */

641     protected void addVariable(JRDesignVariable variable, boolean system) throws JRException
642     {
643         if (variablesMap.containsKey(variable.getName()))
644         {
645             throw new JRException("Duplicate declaration of variable : " + variable.getName());
646         }
647
648         if (system)
649         {
650             // add the variable vefore the first non-system variable
651
ListIterator JavaDoc it = variablesList.listIterator();
652             while (it.hasNext())
653             {
654                 JRVariable var = (JRVariable) it.next();
655                 if (!var.isSystemDefined())
656                 {
657                     it.previous();
658                     break;
659                 }
660             }
661             it.add(variable);
662         }
663         else
664         {
665             variablesList.add(variable);
666         }
667         
668         variablesMap.put(variable.getName(), variable);
669     }
670
671     
672     /**
673      * Removes a variable from the dataset.
674      *
675      * @param variableName the variable name
676      * @return the removed variable, or <code>null</code> if the variable was not found
677      */

678     public JRVariable removeVariable(String JavaDoc variableName)
679     {
680         return removeVariable((JRVariable) variablesMap.get(variableName));
681     }
682
683     
684     /**
685      * Removes a variable from the dataset.
686      *
687      * @param variable the variable to be removed
688      * @return the variable to be removed
689      */

690     public JRVariable removeVariable(JRVariable variable)
691     {
692         if (variable != null)
693         {
694             variablesList.remove(variable);
695             variablesMap.remove(variable.getName());
696         }
697
698         return variable;
699     }
700
701     
702     public JRGroup[] getGroups()
703     {
704         JRGroup[] groupsArray = new JRGroup[groupsList.size()];
705
706         groupsList.toArray(groupsArray);
707
708         return groupsArray;
709     }
710
711     
712     /**
713      * Returns the list of groups.
714      *
715      * @return list of {@link JRGroup JRGroup} objects
716      */

717     public List JavaDoc getGroupsList()
718     {
719         return groupsList;
720     }
721
722     
723     /**
724      * Returns the map of groups indexed by name.
725      *
726      * @return {@link JRGroup JRGroup} objects indexed by name
727      */

728     public Map JavaDoc getGroupsMap()
729     {
730         return groupsMap;
731     }
732
733     
734     /**
735      * Adds a group to the dataset.
736      * @param group the group to add
737      * @throws JRException
738      * @see net.sf.jasperreports.engine.JRDataset#getGroups()
739      */

740     public void addGroup(JRDesignGroup group) throws JRException
741     {
742         if (groupsMap.containsKey(group.getName()))
743         {
744             throw new JRException("Duplicate declaration of group : " + group.getName());
745         }
746
747         JRDesignVariable countVariable = new JRDesignVariable();
748         countVariable.setName(group.getName() + "_COUNT");
749         countVariable.setValueClass(Integer JavaDoc.class);
750         countVariable.setResetType(JRVariable.RESET_TYPE_GROUP);
751         countVariable.setResetGroup(group);
752         countVariable.setCalculation(JRVariable.CALCULATION_COUNT);
753         countVariable.setSystemDefined(true);
754         JRDesignExpression expression = new JRDesignExpression();
755         expression.setValueClass(Integer JavaDoc.class);
756         expression.setText("new Integer(1)");
757         countVariable.setExpression(expression);
758         expression = new JRDesignExpression();
759         expression.setValueClass(Integer JavaDoc.class);
760         expression.setText("new Integer(0)");
761         countVariable.setInitialValueExpression(expression);
762
763         addVariable(countVariable, true);
764
765         group.setCountVariable(countVariable);
766
767         groupsList.add(group);
768         groupsMap.put(group.getName(), group);
769     }
770
771     
772     
773     /**
774      * Removes a group from the dataset.
775      *
776      * @param groupName the group name
777      * @return the removed group, or <code>null</code> if the group was not found
778      */

779     public JRGroup removeGroup(String JavaDoc groupName)
780     {
781         return removeGroup((JRGroup) groupsMap.get(groupName));
782     }
783
784     
785     /**
786      * Removes a group from the dataset.
787      *
788      * @param group the group to be removed
789      * @return the group to be removed
790      */

791     public JRGroup removeGroup(JRGroup group)
792     {
793         if (group != null)
794         {
795             removeVariable(group.getCountVariable());
796             groupsList.remove(group);
797             groupsMap.remove(group.getName());
798         }
799
800         return group;
801     }
802     
803     
804     /**
805      * Sets the base name of resource bundle to be used by the dataset.
806      *
807      * @param resourceBundle the resource bundle base name
808      */

809     public void setResourceBundle(String JavaDoc resourceBundle)
810     {
811         this.resourceBundle = resourceBundle;
812     }
813     
814     
815     protected void queryLanguageChanged(String JavaDoc oldLanguage, String JavaDoc newLanguage)
816     {
817         try
818         {
819             if (oldLanguage != null)
820             {
821                 JRQueryExecuterFactory queryExecuterFactory = JRQueryExecuterUtils.getQueryExecuterFactory(oldLanguage);
822                 Object JavaDoc[] builtinParameters = queryExecuterFactory.getBuiltinParameters();
823                 if (builtinParameters != null)
824                 {
825                     removeBuiltinParameters(builtinParameters);
826                 }
827             }
828
829             if (newLanguage != null)
830             {
831                 JRQueryExecuterFactory queryExecuterFactory = JRQueryExecuterUtils.getQueryExecuterFactory(newLanguage);
832                 Object JavaDoc[] builtinParameters = queryExecuterFactory.getBuiltinParameters();
833                 if (builtinParameters != null)
834                 {
835                     addBuiltinParameters(builtinParameters);
836                     sortSystemParamsFirst();
837                 }
838             }
839         }
840         catch (JRException e)
841         {
842             throw new JRRuntimeException(e);
843         }
844     }
845
846     
847     private void sortSystemParamsFirst()
848     {
849         Collections.sort(parametersList, new Comparator JavaDoc()
850                 {
851                     public int compare(Object JavaDoc o1, Object JavaDoc o2)
852                     {
853                         JRParameter p1 = (JRParameter) o1;
854                         JRParameter p2 = (JRParameter) o2;
855                         boolean s1 = p1.isSystemDefined();
856                         boolean s2 = p2.isSystemDefined();
857                         
858                         return s1 ? (s2 ? 0 : -1) : (s2 ? 1 : 0);
859                     }
860                 });
861     }
862
863     private void removeBuiltinParameters(Object JavaDoc[] builtinParameters)
864     {
865         for (int i = 0; i < builtinParameters.length; i += 2)
866         {
867             String JavaDoc parameterName = (String JavaDoc) builtinParameters[i];
868             JRParameter parameter = (JRParameter) parametersMap.get(parameterName);
869             if (parameter.isSystemDefined())
870             {
871                 removeParameter(parameter);
872             }
873         }
874     }
875     
876     
877     /**
878      * Adds/sets a property value.
879      *
880      * @param propName the name of the property
881      * @param value the value of the property
882      */

883     public void setProperty(String JavaDoc propName, String JavaDoc value)
884     {
885         getPropertiesMap().setProperty(propName, value);
886     }
887     
888     
889     /**
890      * Sets the dataset filter expression.
891      * <p>
892      * The expression value class should be <code>java.lang.Boolean</code>.
893      * </p>
894      *
895      * @param expression the boolean expression to use as filter expression
896      * @see JRDataset#getFilterExpression()
897      */

898     public void setFilterExpression(JRExpression expression)
899     {
900         this.filterExpression = expression;
901     }
902 }
903
Popular Tags