1 28 package net.sf.jasperreports.engine.design; 29 30 import java.beans.PropertyChangeEvent ; 31 import java.beans.PropertyChangeListener ; 32 import java.io.Serializable ; 33 import java.net.URLStreamHandlerFactory ; 34 import java.sql.Connection ; 35 import java.util.ArrayList ; 36 import java.util.Collections ; 37 import java.util.Comparator ; 38 import java.util.HashMap ; 39 import java.util.List ; 40 import java.util.ListIterator ; 41 import java.util.Locale ; 42 import java.util.Map ; 43 import java.util.ResourceBundle ; 44 import java.util.TimeZone ; 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 70 public class JRDesignDataset extends JRBaseDataset 71 { 72 private static final long serialVersionUID = JRConstants.SERIAL_VERSION_UID; 73 74 77 protected Map parametersMap = new HashMap (); 78 protected List parametersList = new ArrayList (); 79 80 83 protected Map fieldsMap = new HashMap (); 84 protected List fieldsList = new ArrayList (); 85 86 87 90 protected Map sortFieldsMap = new HashMap (); 91 protected List sortFieldsList = new ArrayList (); 92 93 94 97 protected Map variablesMap = new HashMap (); 98 protected List variablesList = new ArrayList (); 99 100 101 104 protected Map groupsMap = new HashMap (); 105 protected List groupsList = new ArrayList (); 106 107 private class QueryLanguageChangeListener implements PropertyChangeListener , Serializable 108 { 109 private static final long serialVersionUID = JRConstants.SERIAL_VERSION_UID; 110 111 public void propertyChange(PropertyChangeEvent evt) 112 { 113 queryLanguageChanged((String ) evt.getOldValue(), (String ) evt.getNewValue()); 114 } 115 } 116 117 private PropertyChangeListener queryLanguageChangeListener = new QueryLanguageChangeListener(); 118 119 120 123 private static final Object [] BUILT_IN_PARAMETERS = new Object [] { 124 JRParameter.REPORT_PARAMETERS_MAP, java.util.Map .class, 125 JRParameter.REPORT_CONNECTION, Connection .class, 126 JRParameter.REPORT_MAX_COUNT, Integer .class, 127 JRParameter.REPORT_DATA_SOURCE, JRDataSource.class, 128 JRParameter.REPORT_SCRIPTLET, JRAbstractScriptlet.class, 129 JRParameter.REPORT_LOCALE, Locale .class, 130 JRParameter.REPORT_RESOURCE_BUNDLE, ResourceBundle .class, 131 JRParameter.REPORT_TIME_ZONE, TimeZone .class, 132 JRParameter.REPORT_FORMAT_FACTORY, FormatFactory.class, 133 JRParameter.REPORT_CLASS_LOADER, ClassLoader .class, 134 JRParameter.REPORT_URL_HANDLER_FACTORY, URLStreamHandlerFactory .class}; 135 136 137 138 141 private static final Object [] BUILT_IN_PARAMETERS_MAIN = new Object [] { 142 JRParameter.REPORT_VIRTUALIZER, JRVirtualizer.class, 143 JRParameter.IS_IGNORE_PAGINATION, Boolean .class }; 144 145 146 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 } 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 .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 .class); 195 expression.setText("new Integer(1)"); 196 variable.setExpression(expression); 197 expression = new JRDesignExpression(); 198 expression.setValueClass(Integer .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 .class); 211 variable.setResetType(JRVariable.RESET_TYPE_PAGE); 213 variable.setCalculation(JRVariable.CALCULATION_SYSTEM); 214 variable.setSystemDefined(true); 215 expression = new JRDesignExpression(); 216 expression.setValueClass(Integer .class); 217 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 .class); 228 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 .class); 234 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 .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 .class); 252 expression.setText("new Integer(1)"); 253 variable.setExpression(expression); 254 expression = new JRDesignExpression(); 255 expression.setValueClass(Integer .class); 256 expression.setText("new Integer(0)"); 257 variable.setInitialValueExpression(expression); 258 return variable; 259 } 260 261 private void addBuiltinParameters(Object [] parametersArray) 262 { 263 for (int i = 0; i < parametersArray.length; i++) 264 { 265 JRDesignParameter parameter = new JRDesignParameter(); 266 parameter.setName((String ) parametersArray[i++]); 267 parameter.setValueClass((Class ) parametersArray[i]); 268 parameter.setSystemDefined(true); 269 try 270 { 271 addParameter(parameter); 272 } 273 catch (JRException e) 274 { 275 } 277 } 278 } 279 280 private static JRDesignVariable createReportCountVariable() 281 { 282 JRDesignVariable variable = new JRDesignVariable(); 283 variable.setName(JRVariable.REPORT_COUNT); 284 variable.setValueClass(Integer .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 .class); 290 expression.setText("new Integer(1)"); 291 variable.setExpression(expression); 292 expression = new JRDesignExpression(); 293 expression.setValueClass(Integer .class); 294 expression.setText("new Integer(0)"); 295 variable.setInitialValueExpression(expression); 296 return variable; 297 } 298 299 300 305 public void setName(String 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 327 public List getParametersList() 328 { 329 return parametersList; 330 } 331 332 333 338 public Map getParametersMap() 339 { 340 return parametersMap; 341 } 342 343 344 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 368 public JRParameter removeParameter(String parameterName) 369 { 370 return removeParameter((JRParameter) parametersMap.get(parameterName)); 371 } 372 373 374 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 398 public void setQuery(JRDesignQuery query) 399 { 400 String 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 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 425 public void setScriptletClass(String 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 454 public List getFieldsList() 455 { 456 return fieldsList; 457 } 458 459 460 465 public Map getFieldsMap() 466 { 467 return fieldsMap; 468 } 469 470 471 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 495 public JRField removeField(String fieldName) 496 { 497 return removeField((JRField) fieldsMap.get(fieldName)); 498 } 499 500 501 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 534 public List getSortFieldsList() 535 { 536 return sortFieldsList; 537 } 538 539 540 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 564 public JRSortField removeSortField(String fieldName) 565 { 566 return removeSortField((JRSortField) sortFieldsMap.get(fieldName)); 567 } 568 569 570 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 603 604 public List getVariablesList() 605 { 606 return variablesList; 607 } 608 609 610 615 public Map getVariablesMap() 616 { 617 return variablesMap; 618 } 619 620 621 627 public void addVariable(JRDesignVariable variable) throws JRException 628 { 629 addVariable(variable, false); 630 } 631 632 633 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 ListIterator 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 678 public JRVariable removeVariable(String variableName) 679 { 680 return removeVariable((JRVariable) variablesMap.get(variableName)); 681 } 682 683 684 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 717 public List getGroupsList() 718 { 719 return groupsList; 720 } 721 722 723 728 public Map getGroupsMap() 729 { 730 return groupsMap; 731 } 732 733 734 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 .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 .class); 756 expression.setText("new Integer(1)"); 757 countVariable.setExpression(expression); 758 expression = new JRDesignExpression(); 759 expression.setValueClass(Integer .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 779 public JRGroup removeGroup(String groupName) 780 { 781 return removeGroup((JRGroup) groupsMap.get(groupName)); 782 } 783 784 785 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 809 public void setResourceBundle(String resourceBundle) 810 { 811 this.resourceBundle = resourceBundle; 812 } 813 814 815 protected void queryLanguageChanged(String oldLanguage, String newLanguage) 816 { 817 try 818 { 819 if (oldLanguage != null) 820 { 821 JRQueryExecuterFactory queryExecuterFactory = JRQueryExecuterUtils.getQueryExecuterFactory(oldLanguage); 822 Object [] 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 [] 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 () 850 { 851 public int compare(Object o1, Object 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 [] builtinParameters) 864 { 865 for (int i = 0; i < builtinParameters.length; i += 2) 866 { 867 String parameterName = (String ) builtinParameters[i]; 868 JRParameter parameter = (JRParameter) parametersMap.get(parameterName); 869 if (parameter.isSystemDefined()) 870 { 871 removeParameter(parameter); 872 } 873 } 874 } 875 876 877 883 public void setProperty(String propName, String value) 884 { 885 getPropertiesMap().setProperty(propName, value); 886 } 887 888 889 898 public void setFilterExpression(JRExpression expression) 899 { 900 this.filterExpression = expression; 901 } 902 } 903 | Popular Tags |