Source Code Cross Referenced for JRDesignDataset.java in  » Report » jasperreports-2.0.1 » net » sf » jasperreports » engine » design » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Report » jasperreports 2.0.1 » net.sf.jasperreports.engine.design 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * ============================================================================
003:         * GNU Lesser General Public License
004:         * ============================================================================
005:         *
006:         * JasperReports - Free Java report-generating library.
007:         * Copyright (C) 2001-2006 JasperSoft Corporation http://www.jaspersoft.com
008:         * 
009:         * This library is free software; you can redistribute it and/or
010:         * modify it under the terms of the GNU Lesser General Public
011:         * License as published by the Free Software Foundation; either
012:         * version 2.1 of the License, or (at your option) any later version.
013:         * 
014:         * This library is distributed in the hope that it will be useful,
015:         * but WITHOUT ANY WARRANTY; without even the implied warranty of
016:         * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
017:         * Lesser General Public License for more details.
018:         * 
019:         * You should have received a copy of the GNU Lesser General Public
020:         * License along with this library; if not, write to the Free Software
021:         * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307, USA.
022:         * 
023:         * JasperSoft Corporation
024:         * 303 Second Street, Suite 450 North
025:         * San Francisco, CA 94107
026:         * http://www.jaspersoft.com
027:         */
028:        package net.sf.jasperreports.engine.design;
029:
030:        import java.beans.PropertyChangeEvent;
031:        import java.beans.PropertyChangeListener;
032:        import java.io.IOException;
033:        import java.io.Serializable;
034:        import java.net.URLStreamHandlerFactory;
035:        import java.sql.Connection;
036:        import java.util.ArrayList;
037:        import java.util.Collection;
038:        import java.util.Collections;
039:        import java.util.Comparator;
040:        import java.util.HashMap;
041:        import java.util.List;
042:        import java.util.ListIterator;
043:        import java.util.Locale;
044:        import java.util.Map;
045:        import java.util.ResourceBundle;
046:        import java.util.TimeZone;
047:
048:        import net.sf.jasperreports.engine.JRAbstractScriptlet;
049:        import net.sf.jasperreports.engine.JRConstants;
050:        import net.sf.jasperreports.engine.JRDataSource;
051:        import net.sf.jasperreports.engine.JRDataset;
052:        import net.sf.jasperreports.engine.JRException;
053:        import net.sf.jasperreports.engine.JRExpression;
054:        import net.sf.jasperreports.engine.JRField;
055:        import net.sf.jasperreports.engine.JRGroup;
056:        import net.sf.jasperreports.engine.JRParameter;
057:        import net.sf.jasperreports.engine.JRRuntimeException;
058:        import net.sf.jasperreports.engine.JRSortField;
059:        import net.sf.jasperreports.engine.JRVariable;
060:        import net.sf.jasperreports.engine.JRVirtualizer;
061:        import net.sf.jasperreports.engine.base.JRBaseDataset;
062:        import net.sf.jasperreports.engine.query.JRQueryExecuterFactory;
063:        import net.sf.jasperreports.engine.util.FormatFactory;
064:        import net.sf.jasperreports.engine.util.JRQueryExecuterUtils;
065:
066:        /**
067:         * Implementation of {@link net.sf.jasperreports.engine.JRDataset JRDataset} to be used for report desing.
068:         * 
069:         * @author Lucian Chirita (lucianc@users.sourceforge.net)
070:         * @version $Id: JRDesignDataset.java 1759 2007-06-20 16:47:34Z lucianc $
071:         */
072:        public class JRDesignDataset extends JRBaseDataset {
073:            private static final long serialVersionUID = JRConstants.SERIAL_VERSION_UID;
074:
075:            /**
076:             * Parameters mapped by name.
077:             */
078:            protected Map parametersMap = new HashMap();
079:            protected List parametersList = new ArrayList();
080:
081:            /**
082:             * Fields mapped by name.
083:             */
084:            protected Map fieldsMap = new HashMap();
085:            protected List fieldsList = new ArrayList();
086:
087:            /**
088:             * Sort fields mapped by name.
089:             */
090:            protected Map sortFieldsMap = new HashMap();
091:            protected List sortFieldsList = new ArrayList();
092:
093:            /**
094:             * Variables mapped by name.
095:             */
096:            protected Map variablesMap = new HashMap();
097:            protected List variablesList = new ArrayList();
098:
099:            /**
100:             * Groups mapped by name.
101:             */
102:            protected Map groupsMap = new HashMap();
103:            protected List groupsList = new ArrayList();
104:
105:            private class QueryLanguageChangeListener implements 
106:                    PropertyChangeListener, Serializable {
107:                private static final long serialVersionUID = JRConstants.SERIAL_VERSION_UID;
108:
109:                public void propertyChange(PropertyChangeEvent evt) {
110:                    queryLanguageChanged((String) evt.getOldValue(),
111:                            (String) evt.getNewValue());
112:                }
113:            }
114:
115:            private PropertyChangeListener queryLanguageChangeListener = new QueryLanguageChangeListener();
116:
117:            /**
118:             * An array containing the built-in parameters that can be found and used in any report dataset.
119:             */
120:            private static final Object[] BUILT_IN_PARAMETERS = new Object[] {
121:                    JRParameter.REPORT_PARAMETERS_MAP, java.util.Map.class,
122:                    JRParameter.REPORT_CONNECTION, Connection.class,
123:                    JRParameter.REPORT_MAX_COUNT, Integer.class,
124:                    JRParameter.REPORT_DATA_SOURCE, JRDataSource.class,
125:                    JRParameter.REPORT_SCRIPTLET, JRAbstractScriptlet.class,
126:                    JRParameter.REPORT_LOCALE, Locale.class,
127:                    JRParameter.REPORT_RESOURCE_BUNDLE, ResourceBundle.class,
128:                    JRParameter.REPORT_TIME_ZONE, TimeZone.class,
129:                    JRParameter.REPORT_FORMAT_FACTORY, FormatFactory.class,
130:                    JRParameter.REPORT_CLASS_LOADER, ClassLoader.class,
131:                    JRParameter.REPORT_URL_HANDLER_FACTORY,
132:                    URLStreamHandlerFactory.class };
133:
134:            /**
135:             * An array containing the built-in parameters that can be found and used in any report/main dataset.
136:             */
137:            private static final Object[] BUILT_IN_PARAMETERS_MAIN = new Object[] {
138:                    JRParameter.REPORT_VIRTUALIZER, JRVirtualizer.class,
139:                    JRParameter.IS_IGNORE_PAGINATION, Boolean.class,
140:                    JRParameter.REPORT_TEMPLATES, Collection.class };
141:
142:            /**
143:             * Create a dataset.
144:             * 
145:             * @param isMain whether this is the main dataset of the report or a sub dataset
146:             * @see net.sf.jasperreports.engine.JRDataset#isMainDataset()
147:             */
148:            public JRDesignDataset(boolean isMain) {
149:                super (isMain);
150:
151:                addBuiltinParameters(BUILT_IN_PARAMETERS);
152:
153:                if (isMain) {
154:                    addBuiltinParameters(BUILT_IN_PARAMETERS_MAIN);
155:                }
156:
157:                try {
158:                    if (isMain) {
159:                        addVariable(createPageNumberVariable());
160:                        addVariable(createColumnNumberVariable());
161:                    }
162:                    addVariable(createReportCountVariable());
163:                    if (isMain) {
164:                        addVariable(createPageCountVariable());
165:                        addVariable(createColumnCountVariable());
166:                    }
167:                } catch (JRException e) {
168:                    //never reached
169:                }
170:            }
171:
172:            private static JRDesignVariable createPageCountVariable() {
173:                JRDesignExpression expression;
174:                JRDesignVariable variable;
175:                variable = new JRDesignVariable();
176:                variable.setName(JRVariable.PAGE_COUNT);
177:                variable.setValueClass(Integer.class);
178:                variable.setResetType(JRVariable.RESET_TYPE_PAGE);
179:                variable.setCalculation(JRVariable.CALCULATION_COUNT);
180:                variable.setSystemDefined(true);
181:                expression = new JRDesignExpression();
182:                expression.setValueClass(Integer.class);
183:                expression.setText("new Integer(1)");
184:                variable.setExpression(expression);
185:                expression = new JRDesignExpression();
186:                expression.setValueClass(Integer.class);
187:                expression.setText("new Integer(0)");
188:                variable.setInitialValueExpression(expression);
189:                return variable;
190:            }
191:
192:            private static JRDesignVariable createColumnNumberVariable() {
193:                JRDesignExpression expression;
194:                JRDesignVariable variable;
195:                variable = new JRDesignVariable();
196:                variable.setName(JRVariable.COLUMN_NUMBER);
197:                variable.setValueClass(Integer.class);
198:                //variable.setResetType(JRVariable.RESET_TYPE_COLUMN);
199:                variable.setResetType(JRVariable.RESET_TYPE_PAGE);
200:                variable.setCalculation(JRVariable.CALCULATION_SYSTEM);
201:                variable.setSystemDefined(true);
202:                expression = new JRDesignExpression();
203:                expression.setValueClass(Integer.class);
204:                //expression.setText("($V{COLUMN_NUMBER} != null)?(new Integer($V{COLUMN_NUMBER}.intValue() + 1)):(new Integer(1))");
205:                expression.setText("new Integer(1)");
206:                variable.setInitialValueExpression(expression);
207:                return variable;
208:            }
209:
210:            private static JRDesignVariable createPageNumberVariable() {
211:                JRDesignVariable variable = new JRDesignVariable();
212:                variable.setName(JRVariable.PAGE_NUMBER);
213:                variable.setValueClass(Integer.class);
214:                //variable.setResetType(JRVariable.RESET_TYPE_PAGE);
215:                variable.setResetType(JRVariable.RESET_TYPE_REPORT);
216:                variable.setCalculation(JRVariable.CALCULATION_SYSTEM);
217:                variable.setSystemDefined(true);
218:                JRDesignExpression expression = new JRDesignExpression();
219:                expression.setValueClass(Integer.class);
220:                //expression.setText("($V{PAGE_NUMBER} != null)?(new Integer($V{PAGE_NUMBER}.intValue() + 1)):(new Integer(1))");
221:                expression.setText("new Integer(1)");
222:                variable.setInitialValueExpression(expression);
223:                return variable;
224:            }
225:
226:            private static JRDesignVariable createColumnCountVariable() {
227:                JRDesignVariable variable;
228:                JRDesignExpression expression;
229:                variable = new JRDesignVariable();
230:                variable.setName(JRVariable.COLUMN_COUNT);
231:                variable.setValueClass(Integer.class);
232:                variable.setResetType(JRVariable.RESET_TYPE_COLUMN);
233:                variable.setCalculation(JRVariable.CALCULATION_COUNT);
234:                variable.setSystemDefined(true);
235:                expression = new JRDesignExpression();
236:                expression.setValueClass(Integer.class);
237:                expression.setText("new Integer(1)");
238:                variable.setExpression(expression);
239:                expression = new JRDesignExpression();
240:                expression.setValueClass(Integer.class);
241:                expression.setText("new Integer(0)");
242:                variable.setInitialValueExpression(expression);
243:                return variable;
244:            }
245:
246:            private void addBuiltinParameters(Object[] parametersArray) {
247:                for (int i = 0; i < parametersArray.length; i++) {
248:                    JRDesignParameter parameter = new JRDesignParameter();
249:                    parameter.setName((String) parametersArray[i++]);
250:                    parameter.setValueClass((Class) parametersArray[i]);
251:                    parameter.setSystemDefined(true);
252:                    try {
253:                        addParameter(parameter);
254:                    } catch (JRException e) {
255:                        // never reached
256:                    }
257:                }
258:            }
259:
260:            private static JRDesignVariable createReportCountVariable() {
261:                JRDesignVariable variable = new JRDesignVariable();
262:                variable.setName(JRVariable.REPORT_COUNT);
263:                variable.setValueClass(Integer.class);
264:                variable.setResetType(JRVariable.RESET_TYPE_REPORT);
265:                variable.setCalculation(JRVariable.CALCULATION_COUNT);
266:                variable.setSystemDefined(true);
267:                JRDesignExpression expression = new JRDesignExpression();
268:                expression.setValueClass(Integer.class);
269:                expression.setText("new Integer(1)");
270:                variable.setExpression(expression);
271:                expression = new JRDesignExpression();
272:                expression.setValueClass(Integer.class);
273:                expression.setText("new Integer(0)");
274:                variable.setInitialValueExpression(expression);
275:                return variable;
276:            }
277:
278:            /**
279:             * Sets the name of the dataset.
280:             * @param name the name of the dataset
281:             * @see net.sf.jasperreports.engine.JRDataset#getName()
282:             */
283:            public void setName(String name) {
284:                this .name = name;
285:            }
286:
287:            public JRParameter[] getParameters() {
288:                JRParameter[] parametersArray = new JRParameter[parametersList
289:                        .size()];
290:
291:                parametersList.toArray(parametersArray);
292:
293:                return parametersArray;
294:            }
295:
296:            /**
297:             * Returns the list of parameters, including build-in ones.
298:             * 
299:             * @return list of {@link JRParameter JRParameter} objects
300:             */
301:            public List getParametersList() {
302:                return parametersList;
303:            }
304:
305:            /**
306:             * Returns the map of parameters, including build-in ones, indexed by name.
307:             * 
308:             * @return {@link JRParameter JRParameter} objects indexed by name
309:             */
310:            public Map getParametersMap() {
311:                return parametersMap;
312:            }
313:
314:            /**
315:             * Adds a parameter to the dataset.
316:             * @param parameter the parameter to add
317:             * @throws JRException
318:             * @see net.sf.jasperreports.engine.JRDataset#getParameters()
319:             */
320:            public void addParameter(JRParameter parameter) throws JRException {
321:                if (parametersMap.containsKey(parameter.getName())) {
322:                    throw new JRException(
323:                            "Duplicate declaration of parameter : "
324:                                    + parameter.getName());
325:                }
326:
327:                parametersList.add(parameter);
328:                parametersMap.put(parameter.getName(), parameter);
329:            }
330:
331:            /**
332:             * Removes a parameter from the dataset.
333:             * 
334:             * @param parameterName the parameter name
335:             * @return the removed parameter, or <code>null</code> if the parameter was not found
336:             */
337:            public JRParameter removeParameter(String parameterName) {
338:                return removeParameter((JRParameter) parametersMap
339:                        .get(parameterName));
340:            }
341:
342:            /**
343:             * Removes a parameter from the dataset.
344:             * 
345:             * @param parameter the parameter to be removed
346:             * @return the parameter to be removed
347:             */
348:            public JRParameter removeParameter(JRParameter parameter) {
349:                if (parameter != null) {
350:                    parametersList.remove(parameter);
351:                    parametersMap.remove(parameter.getName());
352:                }
353:
354:                return parameter;
355:            }
356:
357:            /**
358:             * Sets the dataset query.
359:             * 
360:             * @param query the query
361:             * @see net.sf.jasperreports.engine.JRDataset#getQuery()
362:             */
363:            public void setQuery(JRDesignQuery query) {
364:                String oldLanguage = null;
365:                if (this .query != null) {
366:                    ((JRDesignQuery) this .query).removePropertyChangeListener(
367:                            JRDesignQuery.PROPERTY_LANGUAGE,
368:                            queryLanguageChangeListener);
369:                    oldLanguage = this .query.getLanguage();
370:                }
371:                this .query = query;
372:                String newLanguage = null;
373:                if (query != null) {
374:                    query.addPropertyChangeListener(
375:                            JRDesignQuery.PROPERTY_LANGUAGE,
376:                            queryLanguageChangeListener);
377:                    newLanguage = query.getLanguage();
378:                }
379:                queryLanguageChanged(oldLanguage, newLanguage);
380:            }
381:
382:            /**
383:             * Sets the scriptlet class name.
384:             * <p>
385:             * If no scriptlet class name is specified, a default scriptlet is used.
386:             * 
387:             * @param scriptletClass the class name of the scriptlet
388:             * @see net.sf.jasperreports.engine.JRDataset#getScriptletClass()
389:             */
390:            public void setScriptletClass(String scriptletClass) {
391:                this .scriptletClass = scriptletClass;
392:                if (scriptletClass == null) {
393:                    ((JRDesignParameter) parametersMap
394:                            .get(JRParameter.REPORT_SCRIPTLET))
395:                            .setValueClass(JRAbstractScriptlet.class);
396:                } else {
397:                    ((JRDesignParameter) parametersMap
398:                            .get(JRParameter.REPORT_SCRIPTLET))
399:                            .setValueClassName(scriptletClass);
400:                }
401:            }
402:
403:            public JRField[] getFields() {
404:                JRField[] fieldsArray = new JRField[fieldsList.size()];
405:
406:                fieldsList.toArray(fieldsArray);
407:
408:                return fieldsArray;
409:            }
410:
411:            /**
412:             * Returns the list of fields.
413:             * 
414:             * @return list of {@link JRField JRField} objects
415:             */
416:            public List getFieldsList() {
417:                return fieldsList;
418:            }
419:
420:            /**
421:             * Returns the map of fields indexed by name.
422:             * 
423:             * @return {@link JRField JRField} objects indexed by name
424:             */
425:            public Map getFieldsMap() {
426:                return fieldsMap;
427:            }
428:
429:            /**
430:             * Adds a field to the dataset.
431:             * @param field the field to add
432:             * @throws JRException
433:             * @see net.sf.jasperreports.engine.JRDataset#getFields()
434:             */
435:            public void addField(JRField field) throws JRException {
436:                if (fieldsMap.containsKey(field.getName())) {
437:                    throw new JRException("Duplicate declaration of field : "
438:                            + field.getName());
439:                }
440:
441:                fieldsList.add(field);
442:                fieldsMap.put(field.getName(), field);
443:            }
444:
445:            /**
446:             * Removes a field from the dataset.
447:             * 
448:             * @param fieldName the field name
449:             * @return the removed field, or <code>null</code> if the field was not found
450:             */
451:            public JRField removeField(String fieldName) {
452:                return removeField((JRField) fieldsMap.get(fieldName));
453:            }
454:
455:            /**
456:             * Removes a field from the dataset.
457:             * 
458:             * @param field the field to be removed
459:             * @return the field to be removed
460:             */
461:            public JRField removeField(JRField field) {
462:                if (field != null) {
463:                    fieldsList.remove(field);
464:                    fieldsMap.remove(field.getName());
465:                }
466:
467:                return field;
468:            }
469:
470:            public JRSortField[] getSortFields() {
471:                JRSortField[] sortFieldsArray = new JRSortField[sortFieldsList
472:                        .size()];
473:
474:                sortFieldsList.toArray(sortFieldsArray);
475:
476:                return sortFieldsArray;
477:            }
478:
479:            /**
480:             * Returns the list of sort fields.
481:             * 
482:             * @return list of {@link JRSortField JRSortField} objects
483:             */
484:            public List getSortFieldsList() {
485:                return sortFieldsList;
486:            }
487:
488:            /**
489:             * Adds a sort field to the dataset.
490:             * @param sortField the sort field to add
491:             * @throws JRException
492:             * @see net.sf.jasperreports.engine.JRDataset#getSortFields()
493:             */
494:            public void addSortField(JRSortField sortField) throws JRException {
495:                if (sortFieldsMap.containsKey(sortField.getName())) {
496:                    throw new JRException(
497:                            "Duplicate declaration of sort field : "
498:                                    + sortField.getName());
499:                }
500:
501:                sortFieldsList.add(sortField);
502:                sortFieldsMap.put(sortField.getName(), sortField);
503:            }
504:
505:            /**
506:             * Removes a sort field from the dataset.
507:             * 
508:             * @param fieldName the field name
509:             * @return the removed sort field, or <code>null</code> if the sort field was not found
510:             */
511:            public JRSortField removeSortField(String fieldName) {
512:                return removeSortField((JRSortField) sortFieldsMap
513:                        .get(fieldName));
514:            }
515:
516:            /**
517:             * Removes a sort field from the dataset.
518:             * 
519:             * @param sortField the sort field to be removed
520:             * @return the sort field to be removed
521:             */
522:            public JRSortField removeSortField(JRSortField sortField) {
523:                if (sortField != null) {
524:                    sortFieldsList.remove(sortField);
525:                    sortFieldsMap.remove(sortField.getName());
526:                }
527:
528:                return sortField;
529:            }
530:
531:            public JRVariable[] getVariables() {
532:                JRVariable[] variablesArray = new JRVariable[variablesList
533:                        .size()];
534:
535:                variablesList.toArray(variablesArray);
536:
537:                return variablesArray;
538:            }
539:
540:            /**
541:             * Returns the list of variables, including build-in ones.
542:             * 
543:             * @return list of {@link JRVariable JRVariable} objects
544:             */
545:
546:            public List getVariablesList() {
547:                return variablesList;
548:            }
549:
550:            /**
551:             * Returns the map of variable, including build-in ones, indexed by name.
552:             * 
553:             * @return {@link JRVariable JRVariable} objects indexed by name
554:             */
555:            public Map getVariablesMap() {
556:                return variablesMap;
557:            }
558:
559:            /**
560:             * Adds a variable to the dataset.
561:             * @param variable the variable to add
562:             * @throws JRException
563:             * @see net.sf.jasperreports.engine.JRDataset#getVariables()
564:             */
565:            public void addVariable(JRDesignVariable variable)
566:                    throws JRException {
567:                addVariable(variable, false);
568:            }
569:
570:            /**
571:             * Adds a variable to the dataset.
572:             * 
573:             * @param variable the variable to add
574:             * @param system whether the variable should be added before user defined variables
575:             * or at the end of the variables list
576:             * @throws JRException
577:             */
578:            protected void addVariable(JRDesignVariable variable, boolean system)
579:                    throws JRException {
580:                if (variablesMap.containsKey(variable.getName())) {
581:                    throw new JRException(
582:                            "Duplicate declaration of variable : "
583:                                    + variable.getName());
584:                }
585:
586:                if (system) {
587:                    // add the variable vefore the first non-system variable
588:                    ListIterator it = variablesList.listIterator();
589:                    while (it.hasNext()) {
590:                        JRVariable var = (JRVariable) it.next();
591:                        if (!var.isSystemDefined()) {
592:                            it.previous();
593:                            break;
594:                        }
595:                    }
596:                    it.add(variable);
597:                } else {
598:                    variablesList.add(variable);
599:                }
600:
601:                variablesMap.put(variable.getName(), variable);
602:            }
603:
604:            /**
605:             * Removes a variable from the dataset.
606:             * 
607:             * @param variableName the variable name
608:             * @return the removed variable, or <code>null</code> if the variable was not found
609:             */
610:            public JRVariable removeVariable(String variableName) {
611:                return removeVariable((JRVariable) variablesMap
612:                        .get(variableName));
613:            }
614:
615:            /**
616:             * Removes a variable from the dataset.
617:             * 
618:             * @param variable the variable to be removed
619:             * @return the variable to be removed
620:             */
621:            public JRVariable removeVariable(JRVariable variable) {
622:                if (variable != null) {
623:                    variablesList.remove(variable);
624:                    variablesMap.remove(variable.getName());
625:                }
626:
627:                return variable;
628:            }
629:
630:            public JRGroup[] getGroups() {
631:                JRGroup[] groupsArray = new JRGroup[groupsList.size()];
632:
633:                groupsList.toArray(groupsArray);
634:
635:                return groupsArray;
636:            }
637:
638:            /**
639:             * Returns the list of groups.
640:             * 
641:             * @return list of {@link JRGroup JRGroup} objects
642:             */
643:            public List getGroupsList() {
644:                return groupsList;
645:            }
646:
647:            /**
648:             * Returns the map of groups indexed by name.
649:             * 
650:             * @return {@link JRGroup JRGroup} objects indexed by name
651:             */
652:            public Map getGroupsMap() {
653:                return groupsMap;
654:            }
655:
656:            /**
657:             * Adds a group to the dataset.
658:             * @param group the group to add
659:             * @throws JRException
660:             * @see net.sf.jasperreports.engine.JRDataset#getGroups()
661:             */
662:            public void addGroup(JRDesignGroup group) throws JRException {
663:                if (groupsMap.containsKey(group.getName())) {
664:                    throw new JRException("Duplicate declaration of group : "
665:                            + group.getName());
666:                }
667:
668:                JRDesignVariable countVariable = new JRDesignVariable();
669:                countVariable.setName(group.getName() + "_COUNT");
670:                countVariable.setValueClass(Integer.class);
671:                countVariable.setResetType(JRVariable.RESET_TYPE_GROUP);
672:                countVariable.setResetGroup(group);
673:                countVariable.setCalculation(JRVariable.CALCULATION_COUNT);
674:                countVariable.setSystemDefined(true);
675:                JRDesignExpression expression = new JRDesignExpression();
676:                expression.setValueClass(Integer.class);
677:                expression.setText("new Integer(1)");
678:                countVariable.setExpression(expression);
679:                expression = new JRDesignExpression();
680:                expression.setValueClass(Integer.class);
681:                expression.setText("new Integer(0)");
682:                countVariable.setInitialValueExpression(expression);
683:
684:                addVariable(countVariable, true);
685:
686:                group.setCountVariable(countVariable);
687:
688:                groupsList.add(group);
689:                groupsMap.put(group.getName(), group);
690:            }
691:
692:            /**
693:             * Removes a group from the dataset.
694:             * 
695:             * @param groupName the group name
696:             * @return the removed group, or <code>null</code> if the group was not found
697:             */
698:            public JRGroup removeGroup(String groupName) {
699:                return removeGroup((JRGroup) groupsMap.get(groupName));
700:            }
701:
702:            /**
703:             * Removes a group from the dataset.
704:             * 
705:             * @param group the group to be removed
706:             * @return the group to be removed
707:             */
708:            public JRGroup removeGroup(JRGroup group) {
709:                if (group != null) {
710:                    removeVariable(group.getCountVariable());
711:                    groupsList.remove(group);
712:                    groupsMap.remove(group.getName());
713:                }
714:
715:                return group;
716:            }
717:
718:            /**
719:             * Sets the base name of resource bundle to be used by the dataset.
720:             * 
721:             * @param resourceBundle the resource bundle base name
722:             */
723:            public void setResourceBundle(String resourceBundle) {
724:                this .resourceBundle = resourceBundle;
725:            }
726:
727:            protected void queryLanguageChanged(String oldLanguage,
728:                    String newLanguage) {
729:                try {
730:                    if (oldLanguage != null) {
731:                        JRQueryExecuterFactory queryExecuterFactory = JRQueryExecuterUtils
732:                                .getQueryExecuterFactory(oldLanguage);
733:                        Object[] builtinParameters = queryExecuterFactory
734:                                .getBuiltinParameters();
735:                        if (builtinParameters != null) {
736:                            removeBuiltinParameters(builtinParameters);
737:                        }
738:                    }
739:
740:                    if (newLanguage != null) {
741:                        JRQueryExecuterFactory queryExecuterFactory = JRQueryExecuterUtils
742:                                .getQueryExecuterFactory(newLanguage);
743:                        Object[] builtinParameters = queryExecuterFactory
744:                                .getBuiltinParameters();
745:                        if (builtinParameters != null) {
746:                            addBuiltinParameters(builtinParameters);
747:                            sortSystemParamsFirst();
748:                        }
749:                    }
750:                } catch (JRException e) {
751:                    throw new JRRuntimeException(e);
752:                }
753:            }
754:
755:            private void sortSystemParamsFirst() {
756:                Collections.sort(parametersList, new Comparator() {
757:                    public int compare(Object o1, Object o2) {
758:                        JRParameter p1 = (JRParameter) o1;
759:                        JRParameter p2 = (JRParameter) o2;
760:                        boolean s1 = p1.isSystemDefined();
761:                        boolean s2 = p2.isSystemDefined();
762:
763:                        return s1 ? (s2 ? 0 : -1) : (s2 ? 1 : 0);
764:                    }
765:                });
766:            }
767:
768:            private void removeBuiltinParameters(Object[] builtinParameters) {
769:                for (int i = 0; i < builtinParameters.length; i += 2) {
770:                    String parameterName = (String) builtinParameters[i];
771:                    JRParameter parameter = (JRParameter) parametersMap
772:                            .get(parameterName);
773:                    if (parameter.isSystemDefined()) {
774:                        removeParameter(parameter);
775:                    }
776:                }
777:            }
778:
779:            /**
780:             * Adds/sets a property value.
781:             * 
782:             * @param propName the name of the property
783:             * @param value the value of the property
784:             */
785:            public void setProperty(String propName, String value) {
786:                getPropertiesMap().setProperty(propName, value);
787:            }
788:
789:            /**
790:             * Sets the dataset filter expression.
791:             * <p>
792:             * The expression value class should be <code>java.lang.Boolean</code>.
793:             * </p>
794:             * 
795:             * @param expression the boolean expression to use as filter expression
796:             * @see JRDataset#getFilterExpression()
797:             */
798:            public void setFilterExpression(JRExpression expression) {
799:                this .filterExpression = expression;
800:            }
801:
802:            /**
803:             * 
804:             */
805:            private void readObject(java.io.ObjectInputStream in)
806:                    throws IOException, ClassNotFoundException {
807:                in.defaultReadObject();
808:
809:                if (sortFieldsMap == null)
810:                    sortFieldsMap = new HashMap();
811:
812:                if (sortFieldsList == null)
813:                    sortFieldsList = new ArrayList();
814:            }
815:
816:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.