1 package com.calipso.reportgenerator.reportmanager; 2 3 import com.calipso.reportgenerator.reportdefinitions.ReportDefinition; 4 import com.calipso.reportgenerator.reportdefinitions.ReportSourceDefinition; 5 import com.calipso.reportgenerator.reportdefinitions.ReportView; 6 import com.calipso.reportgenerator.reportdefinitions.types.ReportDefinitionReportTypeType; 7 import com.calipso.reportgenerator.services.FileSystemResolver; 8 import com.calipso.reportgenerator.reportcalculator.Matrix; 9 import com.calipso.reportgenerator.common.*; 10 import com.calipso.reportgenerator.reportmanager.UsersRepository; 11 12 import java.util.*; 13 import java.io.*; 14 15 import org.apache.commons.logging.Log; 16 import org.apache.commons.vfs.FileSystemManager; 17 import org.apache.commons.vfs.FileObject; 18 import org.exolab.castor.xml.Unmarshaller; 19 import com.calipso.reportgenerator.common.InfoException; 20 import net.sf.jasperreports.engine.*; 21 import net.sf.jasperreports.engine.export.JRXlsExporter; 22 import net.sf.jasperreports.engine.export.JRCsvExporter; 23 24 32 33 public class ReportManager implements IReportManager, Serializable { 34 35 private HashMap activeReports; 36 private int lastHandle; 37 private ReportDefinitionRepository reportDefinitionRepository; 38 private ReportSourceDefinitionRepository reportSourceDefinitionRepository; 39 private ReportSourceRepository reportSourceRepository; 40 private ReportViewRepository reportViewRepository; 41 private ReportGeneratorConfiguration reportGeneratorConfiguration; 42 private TempRepository tempRepository; 43 44 45 49 public ReportManager(ReportGeneratorConfiguration reportGeneratorConfiguration) throws InfoException { 50 ReportManagerLogger.debug(LanguageTraslator.traslate("159")); 51 if (reportGeneratorConfiguration == null) { 52 throw new InfoException(LanguageTraslator.traslate("1")); 53 } 54 else { 55 this.reportGeneratorConfiguration = reportGeneratorConfiguration; 56 } 57 } 58 59 63 public ReportManager() throws InfoException { 64 65 } 66 67 74 public ReportSpec getReportSpec(ReportDefinition reportDefinition) throws InfoException { 75 ReportManagerLogger.debug(LanguageTraslator.traslate("158")+":"+reportDefinition.getId()); 76 ReportSpec reportSpec = new ReportSpec(reportGeneratorConfiguration); 77 ReportSourceDefinition reportSourceDefinitionTmp; 78 reportSourceDefinitionTmp = getReportSourceDefinitionFromID(reportDefinition.getReportSource()); 79 reportSpec.fillFrom(reportSourceDefinitionTmp); 80 reportSpec.fillFrom(reportDefinition); 81 return reportSpec; 82 } 83 84 92 public ReportSpec getReportSpec(ReportDefinition reportDefinition, ReportSourceDefinition reportSourceDef) throws InfoException { 93 ReportManagerLogger.debug(LanguageTraslator.traslate("158")+":"+reportDefinition.getId()); 94 ReportSpec reportSpec = new ReportSpec(reportGeneratorConfiguration); 95 reportSpec.fillFrom(reportSourceDef); 96 reportSpec.fillFrom(reportDefinition); 97 return reportSpec; 98 } 99 100 107 public ReportSpec getReportSpec(ReportSourceDefinition reportSourceDefinition) throws InfoException { 108 ReportSpec reportSpec = new ReportSpec(reportGeneratorConfiguration); 109 reportSpec.fillFrom(reportSourceDefinition); 110 return reportSpec; 111 } 112 113 119 protected Report newReportFrom(String reportDefId) throws InfoException { 120 if (reportDefId.equals("")) { 121 throw new InfoException(LanguageTraslator.traslate("2")); 122 } 123 else { 124 return newReportFrom(getReportDefinitionFromID(reportDefId)); 125 } 126 } 127 128 129 135 protected Report newReportFrom(ReportDefinition reportDef) throws InfoException { 136 ReportSource source; 137 if (reportDef == null) { 138 throw new InfoException(LanguageTraslator.traslate("3")); 139 } 140 else { 141 ReportSpec reportSpec = null; 142 try { 143 reportSpec = getReportSpec(reportDef); 144 source = getReportSource(reportSpec, getReportSourceDefinitionFromID(reportDef.getReportSource())); 145 } catch (InfoException e) { 146 throw new InfoException(LanguageTraslator.traslate("4")+":"+reportDef.getId(), e); 147 } 148 Report report = null; 149 if((reportSpec.getReportType().toString().equalsIgnoreCase(ReportDefinitionReportTypeType.CUBE.toString())) || 150 (reportSpec.getReportType().toString().equalsIgnoreCase(ReportDefinitionReportTypeType.CHARTCUBE.toString()))) { 151 report = new CubeReport(reportSpec, source, reportGeneratorConfiguration); 152 } else if(reportSpec.getReportType().toString().equalsIgnoreCase(ReportDefinitionReportTypeType.ACCUM.toString())) { 153 report = new StaticReport(reportSpec, source, reportGeneratorConfiguration); 154 } 155 return report; 156 } 157 } 158 159 168 protected Report newReportFrom(String reportDefId, Map paramValues) throws InfoException { 169 if (reportDefId.equals("")) { 170 throw new InfoException(LanguageTraslator.traslate("5")); 171 } 172 else { 173 return newReportFrom(getReportDefinitionFromID(reportDefId), paramValues); 174 } 175 } 176 177 186 187 protected Report newReportFrom(ReportDefinition reportDef, Map paramValues) throws InfoException { 188 ReportSource source; 189 if (reportDef == null) { 190 throw new InfoException(LanguageTraslator.traslate("6")); 191 } 192 else { 193 ReportSpec reportSpec = getReportSpec(reportDef); 194 ReportSourceDefinition reportSourceDefinition = getReportSourceDefinitionFromID(reportDef.getReportSource()); 195 Report report = null; 196 if (reportSourceDefinition.getCached()) { 197 report = newReportFrom(reportDef); 198 } 199 else { 200 source = getReportSource(reportSpec, reportSourceDefinition, paramValues); 201 if((reportSpec.getReportType().toString().equalsIgnoreCase(ReportDefinitionReportTypeType.CUBE.toString())) || 202 (reportSpec.getReportType().toString().equalsIgnoreCase(ReportDefinitionReportTypeType.CHARTCUBE.toString()))) { 203 report = new CubeReport(reportSpec, source, reportGeneratorConfiguration); 204 } else if(reportSpec.getReportType().toString().equalsIgnoreCase(ReportDefinitionReportTypeType.ACCUM.toString())) { 205 report = new StaticReport(reportSpec, source, reportGeneratorConfiguration); 206 } 207 } 208 if (report != null) { 209 return report; 210 } 211 else { 212 throw new InfoException(LanguageTraslator.traslate("7")+":"+reportDef.getId()); 213 } 214 } 215 } 216 217 218 226 protected ReportSource getReportSource(ReportSpec reportSpec, ReportSourceDefinition reportSourceDefinition, Map paramValues) throws InfoException { 227 ReportSource reportSource = null; 228 if (reportSourceDefinition == null) { 229 throw new InfoException(LanguageTraslator.traslate("8")); 230 } 231 else { 232 reportSource = new ReportSource(reportSpec, paramValues, reportGeneratorConfiguration); 233 return reportSource; 234 } 235 } 236 237 245 protected ReportSource getReportSource(ReportSpec reportSpec, ReportSourceDefinition sourceDefinition) throws InfoException { 246 ReportManagerLogger.debug(LanguageTraslator.traslate("160")+":"+reportSpec.getDescription()); 247 ReportSource reportSource = null; 248 if (reportSpec == null) { 249 throw new InfoException(LanguageTraslator.traslate("9")); 250 } 251 else { 252 boolean cached = reportSpec.getCached(); 253 boolean incremental = !reportSpec.getIncrementalDimension().equals(""); 254 if (cached) { 255 try { 256 reportSource = getReportSourceRepository().load(getReportGeneratorConfiguration(), reportSpec, sourceDefinition); 258 } catch (InfoException e) { 259 e.printStackTrace(); 260 throw new InfoException(LanguageTraslator.traslate("10"), e); 261 } 262 if (incremental) { 263 if (reportSource == null) { 264 try { 265 reportSource = new ReportSource(reportSpec, reportGeneratorConfiguration); 266 } catch (Exception e) { 267 throw new InfoException(LanguageTraslator.traslate("11"), e); 268 } 269 boolean saved = getReportSourceRepository().saveIncrementalSource(reportSource, getReportGeneratorConfiguration().isCsvSerialized()); 270 if (!saved) { 271 getReportSourceRepository().saveNewSource(reportSource, getReportGeneratorConfiguration().isCsvSerialized()); 272 } 273 } 274 } 275 } 276 if (reportSource == null) { 277 reportSource = new ReportSource(reportSpec, reportGeneratorConfiguration); 278 if (cached) { 279 getReportSourceRepository().saveNewSource(reportSource , getReportGeneratorConfiguration().isCsvSerialized()); 280 } 281 } 282 return reportSource; 283 } 284 } 285 286 294 public int PrepareReport(ReportDefinition reportDef) throws InfoException { 295 Report report = null; 296 if (reportDef == null) { 297 throw new InfoException(LanguageTraslator.traslate("12")); 298 } 299 else { 300 ReportManagerLogger.debug(LanguageTraslator.traslate("196") + ":"+reportDef.getId()); 301 report = newReportFrom(reportDef); 302 if (report != null) { 303 int handle = registerReport(report); 304 return handle; 305 } 306 throw new InfoException(LanguageTraslator.traslate("13")+ ":"+reportDef.getId()); 307 } 308 } 309 310 311 319 public int PrepareReport(ReportDefinition reportDef, Map paramValues) throws InfoException { 320 Report report = null; 321 if (reportDef == null) { 322 throw new InfoException(LanguageTraslator.traslate("14")); 323 } 324 else { 325 ReportManagerLogger.debug(LanguageTraslator.traslate("196") + ":"+reportDef.getId()); 326 report = newReportFrom(reportDef, paramValues); 327 if (report != null) { 328 int handle = registerReport(report); 329 return handle; 330 } 331 throw new InfoException(LanguageTraslator.traslate("15") + ":"+reportDef.getId()); 332 } 333 } 334 335 336 340 public void ReleaseReport(int handle) { 341 try { 342 ReportManagerLogger.debug(LanguageTraslator.traslate("197")+ ":"+getReportFrom(handle).getReportSpec().getDefinitionId()); 343 } catch (InfoException e) { 344 ReportManagerLogger.error(LanguageTraslator.traslate("198")); 345 } 346 unRegisterReport(handle); 347 } 348 349 355 public ReportDefinition getReportDefinitionFromID(String id) throws InfoException { 356 if (id.equals("")) { 357 throw new InfoException(LanguageTraslator.traslate("16")); 358 } 359 else { 360 return getReportDefinitionRepository().loadFromID(id); 361 } 362 } 363 364 370 371 public ReportSourceDefinition getReportSourceDefinitionFromID(String reportSourceDefinitionId) throws InfoException { 372 if (reportSourceDefinitionId.equals("")) { 373 throw new InfoException(LanguageTraslator.traslate("17")); 374 } 375 else { 376 return getReportSourceDefinitionRepository().loadFromID(reportSourceDefinitionId); 377 } 378 } 379 380 385 public void init(ReportGeneratorConfiguration reportGeneratorConfiguration) throws InfoException { 386 if(this.reportGeneratorConfiguration == null) { 387 this.reportGeneratorConfiguration = reportGeneratorConfiguration; 388 } 389 } 390 391 397 public void prepareReportSource(String reportSourceDefinitionId) throws InfoException { 398 ReportSource source = null; 399 if (reportSourceDefinitionId.equals("")) { 400 throw new InfoException(LanguageTraslator.traslate("18")); 401 } 402 else { 403 ReportManagerLogger.debug(LanguageTraslator.traslate("161")+":"+reportSourceDefinitionId); 404 ReportSourceDefinition reportSourceDefinition = getReportSourceDefinitionFromID(reportSourceDefinitionId); 405 ReportSpec reportSpec = new ReportSpec(getReportGeneratorConfiguration()); 406 reportSpec.fillFrom(reportSourceDefinition); 407 source = getReportSource(reportSpec, reportSourceDefinition); 408 } 409 if (source == null) { 410 throw new InfoException(LanguageTraslator.traslate("19")+":"+reportSourceDefinitionId); 411 } 412 413 } 414 415 416 421 422 public Map getReportDefinitions() throws InfoException { 423 try { 424 return getReportDefinitionRepository().getAllDefinitions(); 425 } catch (Exception e) { 426 throw new InfoException(LanguageTraslator.traslate("20"), e); 427 } 428 } 429 430 435 public Map getReportSourceDefinitions() throws InfoException { 436 try { 437 return getReportSourceDefinitionRepository().getAllDfefinitions(); 438 } catch (Exception e) { 439 throw new InfoException(LanguageTraslator.traslate("21"), e); 440 } 441 442 } 443 444 451 public Map getReportsForEntity(String entityID) throws InfoException { 452 if (entityID.equals("")) { 453 throw new InfoException(LanguageTraslator.traslate("22")); 454 } 455 else { 456 try { 457 return getReportDefinitionRepository().getAllDfefinitionForEntity(entityID); 458 } catch (Exception e) { 459 throw new InfoException(LanguageTraslator.traslate("23")+":"+entityID, e); 460 } 461 } 462 } 463 464 473 public void ExecuteAction(int handle, String actionName, Object params) { 474 475 } 476 477 public void saveReportDefinition(String reportDefinitionId) throws InfoException { 478 } 479 480 public void saveReportSourceDefinition(String reportSourceDefinitionId) throws InfoException { 481 } 482 483 488 private Object keyFromHandle(int handle) { 489 Object key = new Integer (handle); 490 return key; 491 } 492 493 494 498 private int getNewHandle() { 499 return lastHandle++; 500 } 501 502 503 507 protected HashMap getActiveReports() { 508 if (activeReports == null) { 509 activeReports = new HashMap(); 510 } 511 return activeReports; 512 } 513 514 515 520 protected int registerReport(Report report) { 521 int handle = getNewHandle(); 522 getActiveReports().put(keyFromHandle(handle), report); 523 return handle; 524 } 525 526 530 protected void unRegisterReport(int handle) { 531 getActiveReports().remove(keyFromHandle(handle)); 532 } 533 534 540 protected Report getReportFrom(int handle) throws InfoException { 541 Object key = keyFromHandle(handle); 542 if (getActiveReports().containsKey(key)) { 543 return (Report) getActiveReports().get(key); 544 } 545 throw new InfoException(LanguageTraslator.traslate("24")); 546 } 547 548 549 553 protected ReportDefinitionRepository getReportDefinitionRepository() { 554 if (reportDefinitionRepository == null) { 555 reportDefinitionRepository = new ReportDefinitionRepository(reportGeneratorConfiguration.getReportDefinitionRepositoryPath(), reportGeneratorConfiguration); 556 } 557 return reportDefinitionRepository; 558 } 559 560 561 565 protected ReportSourceDefinitionRepository getReportSourceDefinitionRepository() { 566 if (reportSourceDefinitionRepository == null) { 567 reportSourceDefinitionRepository = new ReportSourceDefinitionRepository(reportGeneratorConfiguration.getReportSourceDefinitionRepositoryPath(), reportGeneratorConfiguration); 568 } 569 return reportSourceDefinitionRepository; 570 } 571 572 577 protected ReportSourceRepository getReportSourceRepository() { 578 if (reportSourceRepository == null) { 579 reportSourceRepository = new ReportSourceRepository(reportGeneratorConfiguration.getReportSourceRepositoryPath(), reportGeneratorConfiguration); 580 } 581 return reportSourceRepository; 582 } 583 584 588 protected ReportViewRepository getReportViewRepository() { 589 if (reportViewRepository == null) { 590 reportViewRepository = new ReportViewRepository(reportGeneratorConfiguration.getReportViewRepositoryPath(), reportGeneratorConfiguration); 591 } 592 return reportViewRepository; 593 } 594 595 601 public void saveReportDefinition(ReportDefinition reportDefinition) throws InfoException { 602 if (reportDefinition == null) { 603 throw new InfoException(LanguageTraslator.traslate("25")); 604 } 605 else { 606 try { 607 ReportManagerLogger.debug(LanguageTraslator.traslate("162")+":"+reportDefinition.getId()); 608 getReportDefinitionRepository().save(reportDefinition); 609 } catch (InfoException e) { 610 throw new InfoException(LanguageTraslator.traslate("26")+":"+reportDefinition.getId(), e); 611 } 612 } 613 } 614 615 621 622 public void saveReportSourceDefinition(ReportSourceDefinition reportSourceDefinition) throws InfoException { 623 if (reportSourceDefinition == null) { 624 throw new InfoException(LanguageTraslator.traslate("27")); 625 } 626 else { 627 ReportManagerLogger.debug(LanguageTraslator.traslate("163")+":"+reportSourceDefinition.getId()); 628 getReportSourceDefinitionRepository().save(reportSourceDefinition); 629 } 630 } 631 632 638 public void invalidateReportSource(String reportSourceDefinitionId) throws InfoException { 639 ReportManagerLogger.debug(LanguageTraslator.traslate("164")+":"+reportSourceDefinitionId); 640 ReportSourceDefinition reportSourceDefinition = null; 641 reportSourceDefinition = getReportSourceDefinitionRepository().loadFromID(reportSourceDefinitionId); 642 if (reportSourceDefinition != null) { 643 invalidateReportSource(reportSourceDefinition); 644 } 645 else { 646 throw new InfoException(LanguageTraslator.traslate("28")+":"+reportSourceDefinitionId); 647 } 648 } 649 650 656 public void invalidateReportSource(ReportSourceDefinition reportSourceDefinition) throws InfoException { 657 if (reportSourceDefinition == null) { 658 throw new InfoException(LanguageTraslator.traslate("29")); 659 } 660 else { 661 ReportManagerLogger.debug(LanguageTraslator.traslate("164")+":"+reportSourceDefinition.getId()); 662 getReportSourceRepository().invalidateReportSource(getReportSpec(reportSourceDefinition)); 663 } 664 } 665 666 674 675 public ReportQuery getReportQuery(int handle) throws InfoException { 676 ReportManagerLogger.debug(LanguageTraslator.traslate("165")); 677 Report report = getReportFrom(handle); 678 return getReportQuery(report); 679 } 680 681 687 public ReportQuery getDefaultReportQuery(int handle) throws InfoException { 688 ReportManagerLogger.debug(LanguageTraslator.traslate("166")); 689 Report report = getReportFrom(handle); 690 return getDefaultReportQuery(report); 691 } 692 693 public ReportQuery getReportQuery(String reportDefinitionId) throws InfoException { 694 return new ReportQuery(getReportSpec(reportDefinitionId)); 695 } 696 697 public ReportQuery getDefaultReportQuery(String reportDefinitionId) throws InfoException { 698 return new ReportQuery(getReportSpec(reportDefinitionId)); 699 } 700 701 707 public ReportQuery getDefaultReportQuery(int handle,String userID) throws InfoException { 708 ReportManagerLogger.debug(LanguageTraslator.traslate("166")); 709 Report report = getReportFrom(handle); 710 return getDefaultReportQuery(report,userID); 711 } 712 713 720 public Map getReportViews(String reportDefinitionID, String userID) throws InfoException { 721 return getReportViewRepository().getAllViewForReportUser(reportDefinitionID,userID); 722 } 723 724 730 public Map getReportViews(String reportDefinitionID) throws InfoException { 731 return getReportViewRepository().getAllViewForDefinition(reportDefinitionID,this); 732 } 733 734 public ReportResult ExecReportQuery(int handle, String reportViewId) throws InfoException { 735 return ExecReportQuery(handle,getReportView(reportViewId)); 736 } 737 738 public String getDefaultReportViewId(String reportDefinitionId, String userId) throws InfoException { 739 ReportView reportView = getDefaultView(reportDefinitionId, userId); 740 if(reportView != null){ 741 return reportView.getId(); 742 } 743 return null; 744 } 745 746 public void saveReportView(String reportViewId) throws InfoException { 747 saveReportView(getReportView(reportViewId)); 748 } 749 750 public ReportSpec getReportSpec(String reportDefinitionId, String reportSourceDefId) throws InfoException { 751 ReportDefinition reportDefinition = getReportDefinitionFromID(reportDefinitionId); 752 ReportSourceDefinition reportSourceDefinition = getReportSourceDefinitionFromID(reportSourceDefId); 753 return getReportSpec(reportDefinition, reportSourceDefinition); 754 } 755 756 public ReportSpec getReportSpec(String reportDefinitionId) throws InfoException { 757 ReportDefinition reportDefinition = getReportDefinitionFromID(reportDefinitionId); 758 ReportSourceDefinition reportSourceDefinition = getReportSourceDefinitionFromID(reportDefinition.getReportSource()); 759 return getReportSpec(reportDefinition, reportSourceDefinition); 760 } 761 762 public ReportView getReportView(String reportViewId) throws InfoException { 763 return null; 764 } 765 766 773 public ReportResult ExecReportQuery(int handle, ReportView reportView) throws InfoException { 774 ReportManagerLogger.debug(LanguageTraslator.traslate("167")+":"+((reportView!=null)?reportView.getId():"")); 775 Report report = getReportFrom(handle); 776 ReportQuery reportQuery = new ReportQuery(report.getReportSpec(),reportView); 777 return ExecReportQuery(report, reportQuery); 778 } 779 780 781 public ReportView getReportViewFromID(String id,String reportDefinitionId,String userId) throws InfoException { 782 ReportManagerLogger.debug(LanguageTraslator.traslate("168")+":"+id); 783 return getReportViewRepository().loadFromID(id,reportDefinitionId,userId); 784 } 785 786 791 public void saveReportView(ReportView reportView) throws InfoException { 792 ReportManagerLogger.debug(LanguageTraslator.traslate("169")+":"+reportView.getId()); 793 getReportViewRepository().save(reportView); 794 } 795 796 797 805 806 public ReportQuery getReportQuery(ReportDefinition reportDefinition) throws InfoException { 807 if (reportDefinition == null) { 808 throw new InfoException(LanguageTraslator.traslate("30")); 809 } 810 else { 811 ReportManagerLogger.debug(LanguageTraslator.traslate("165")+":"+reportDefinition.getId()); 812 ReportQuery query = new ReportQuery(getReportSpec(reportDefinition), false); 813 return query; 814 } 815 } 816 817 824 public ReportQuery getDefaultReportQuery(ReportDefinition reportDefinition) throws InfoException { 825 if (reportDefinition == null) { 826 throw new InfoException(LanguageTraslator.traslate("31")); 827 } 828 else { 829 ReportQuery query = new ReportQuery(getReportSpec(reportDefinition)); 830 return query; 831 } 832 } 833 834 835 843 protected ReportQuery getReportQuery(Report report) throws InfoException { 844 if (report == null) { 845 throw new InfoException(LanguageTraslator.traslate("32")); 846 } 847 else { 848 return report.getQuery(); 849 } 850 } 851 852 859 protected ReportQuery getDefaultReportQuery(Report report) throws InfoException { 860 if (report == null) { 861 throw new InfoException(LanguageTraslator.traslate("33")); 862 } 863 else { 864 return report.getDefaultQuery(); 865 } 866 } 867 868 875 protected ReportQuery getDefaultReportQuery(Report report, String userID) throws InfoException { 876 if (report == null) { 877 throw new InfoException(LanguageTraslator.traslate("34")); 878 } 879 else { 880 return report.getDefaultQuery(getDefaultView(report.getReportSpec().getDefinitionId(),userID)); 881 } 882 } 883 884 887 protected ReportView getDefaultView(String reportDefinitionID, String userID) throws InfoException { 888 return getReportViewRepository().getDefaultViewForReportUser(reportDefinitionID,userID); 889 890 } 891 892 908 909 public ReportResult ExecReportQuery(int handle, Map paramValues) throws InfoException { 910 Report report = getReportFrom(handle); 911 return ExecReportQuery(report, paramValues); 912 } 913 914 921 922 public ReportResult ExecReportQuery(int handle, ReportQuery query) throws InfoException { 923 Report report = getReportFrom(handle); 924 return ExecReportQuery(report, query); 925 } 926 927 935 936 protected ReportResult ExecReportQuery(Report report, Map paramValues) throws InfoException { 937 if (report == null) { 938 throw new InfoException(LanguageTraslator.traslate("35")); 939 } 940 else { 941 ReportManagerLogger.debug(LanguageTraslator.traslate("172")); 942 return report.ExecQuery(paramValues); 943 } 944 } 945 946 953 protected ReportResult ExecReportQuery(Report report, ReportQuery query) throws InfoException { 954 return report.ExecQuery(query); 955 } 956 957 965 966 public ReportResult ExecReportQuery(String reportDefinitionID, Map paramValues) throws InfoException { 967 if (reportDefinitionID.equals("")) { 968 throw new InfoException(LanguageTraslator.traslate("36")); 969 } 970 else { 971 return ExecReportQuery(getReportDefinitionFromID(reportDefinitionID), paramValues); 972 } 973 } 974 975 983 984 public ReportResult ExecReportQuery(String reportDefinitionID, ReportQuery query) throws InfoException { 985 return ExecReportQuery(getReportDefinitionFromID(reportDefinitionID), query); 986 } 987 988 995 public ReportResult ExecReportQuery(ReportDefinition reportDef, Map paramValues) throws InfoException { 996 Report report = null; 997 if (reportDef == null) { 998 throw new InfoException(LanguageTraslator.traslate("37")); 999 } 1000 else { 1001 try { 1002 report = newReportFrom(reportDef, paramValues); 1003 } catch (InfoException e) { 1004 throw new InfoException(LanguageTraslator.traslate("38")+":"+reportDef.getId(), e); 1005 } 1006 if (report != null) { 1007 return ExecReportQuery(report, paramValues); 1008 } 1009 throw new InfoException(LanguageTraslator.traslate("39")+":"+reportDef.getId()); 1010 } 1011 } 1012 1013 1021 1022 public ReportResult ExecReportQuery(ReportDefinition reportDef, ReportQuery query) throws InfoException { 1023 Report report = null; 1024 if (reportDef == null) { 1025 throw new InfoException(LanguageTraslator.traslate("40")); 1026 } 1027 else { 1028 report = newReportFrom(reportDef); 1029 if (report != null) { 1030 return ExecReportQuery(report, query); 1031 } 1032 throw new InfoException(LanguageTraslator.traslate("41")+":"+reportDef.getId()); 1033 } 1034 } 1035 1036 1044 public int PrepareReport(String reportDefinitionID) throws InfoException { 1045 ReportDefinition reportDefinition = getReportDefinitionFromID(reportDefinitionID); 1046 return PrepareReport(reportDefinition); 1047 } 1048 1049 public int PrepareReport(String reportDefinitionId, Map paramValues) throws InfoException { 1050 return PrepareReport(getReportDefinitionFromID(reportDefinitionId),paramValues); 1051 } 1052 1053 1057 public Log getLogger() { 1058 return ReportManagerLogger.getLog(); 1059 } 1060 1061 1065 public void setLogger(Log log) { 1066 ReportManagerLogger.setLog(log); 1067 } 1068 1069 1073 public void registerDefinitions() throws InfoException{ 1074 registerReportDefinitions(new Vector()); 1075 registerReportSourceDefinitions(new Vector()); 1076 registerReportViews(new Vector()); 1077 } 1078 1079 1083 public void deleteAllRepositories() throws InfoException{ 1084 getReportDefinitionRepository().deleteAll(); 1085 getReportSourceRepository().deleteAll(); 1086 getReportSourceDefinitionRepository().deleteAll(); 1087 getReportViewRepository().getCache().deleteAll(); 1088 }; 1090 1091 1095 public void deleteAllDefinitions() throws InfoException{ 1096 getReportDefinitionRepository().deleteAll(); 1097 getReportSourceDefinitionRepository().deleteAll(); 1098 getReportViewRepository().getCache().deleteAll(); 1099 }; 1101 1102 1106 public void deleteReportSourceRepository() throws InfoException{ 1107 getReportSourceRepository().deleteAll(); 1108 }; 1109 1110 1114 public void deleteReportSourceDefinitionRepository() throws InfoException{ 1115 getReportSourceRepository().deleteAll(); 1116 }; 1117 1118 1122 public void deleteReportDefinitionRepository() throws InfoException{ 1123 getReportDefinitionRepository().deleteAll(); 1124 }; 1125 1126 1130 public void deleteReportViewRepository() throws InfoException{ 1131 getReportViewRepository().deleteAll(); 1132 }; 1133 1134 1141 public void deleteReportView(String id, String reportDefinitionId, String userId) throws InfoException{ 1142 getReportViewRepository().delete(id,reportDefinitionId,userId); 1143 } 1144 1145 public void deleteReportSource(String reportSourceDefinitionId) throws InfoException { 1146 } 1147 ; 1148 1149 1154 public void deleteReportSource(ReportSourceDefinition reportSourceDefinition) throws InfoException{ 1155 getReportSourceRepository().deleteReportSource(getReportSpec(reportSourceDefinition)); 1156 }; 1157 1158 1163 public void deleteReportSourceDefinition(String reportSourceDefinitionID) throws InfoException{ 1164 getReportSourceDefinitionRepository().delete(reportSourceDefinitionID); 1165 }; 1166 1167 1172 public void deleteReportDefinition(String reportDefinitionID) throws InfoException{ 1173 getReportDefinitionRepository().delete(reportDefinitionID); 1174 }; 1175 1176 1183 public void assingDefaultView(String id, String reportDefinitionId, String userId) throws InfoException{ 1184 getReportViewRepository().assingDefaultView(id,reportDefinitionId,userId); 1185 }; 1186 1187 1190 public Vector registerReportDefinitions(Vector exceptions) throws InfoException { 1191 try { 1192 FileSystemManager fileSystemManager = FileSystemResolver.getFileSystemManager(getReportGeneratorConfiguration()); 1193 FileObject fileObject = fileSystemManager.resolveFile(getReportGeneratorConfiguration().getSourceReportDefinitionsPath()); 1194 String fileName; 1195 for (int i = 0; i < fileObject.getChildren().length; i++) { 1196 fileName = fileObject.getChildren()[i].getName().getBaseName(); 1197 if(!fileName.endsWith(".xml")) { 1198 continue; 1199 } 1200 ReportManagerLogger.debug(LanguageTraslator.traslate("276")+":"+fileName); 1201 try { 1202 saveReportDefinition((ReportDefinition) Unmarshaller.unmarshal(ReportDefinition.class, new FileReader(getReportGeneratorConfiguration().getSourceReportDefinitionsPath() + "/" + fileName))); 1203 } 1204 catch (Exception e) { 1205 exceptions.add(e); 1206 } 1207 } 1208 } 1209 catch (Exception e) { 1210 throw new InfoException(LanguageTraslator.traslate("210"), e); 1211 } 1212 return exceptions; 1213 } 1214 1215 1216 1219 public Vector registerReportSourceDefinitions(Vector exceptions) throws InfoException { 1220 try { 1221 FileSystemManager fileSystemManager = FileSystemResolver.getFileSystemManager(getReportGeneratorConfiguration()); 1222 FileObject fileObject = fileSystemManager.resolveFile(getReportGeneratorConfiguration().getSourceReportSourceDefinitionsPath()); 1223 String fileName; 1224 for (int i = 0; i < fileObject.getChildren().length; i++) { 1225 fileName = fileObject.getChildren()[i].getName().getBaseName(); 1226 if(!fileName.endsWith(".xml")) { 1227 continue; 1228 } 1229 ReportManagerLogger.debug(LanguageTraslator.traslate("275")+":"+fileName); 1230 try { 1231 ReportSourceDefinitionVersion.validateVersion(fileObject.getChildren()[i]); 1232 saveReportSourceDefinition((ReportSourceDefinition) Unmarshaller.unmarshal(ReportSourceDefinition.class, new FileReader(getReportGeneratorConfiguration().getSourceReportSourceDefinitionsPath() + "/" + fileName))); 1233 } 1234 catch (Exception e) { 1235 exceptions.add(e); 1236 } 1237 } 1238 } 1239 catch (Exception e) { 1240 throw new InfoException(LanguageTraslator.traslate("209"), e); 1241 } 1242 return exceptions; 1243 } 1244 1245 1248 public Vector registerReportViews(Vector exceptions) throws InfoException { 1249 try { 1250 FileSystemManager fileSystemManager = FileSystemResolver.getFileSystemManager(getReportGeneratorConfiguration()); 1251 FileObject fileObject = fileSystemManager.resolveFile(getReportGeneratorConfiguration().getSourceReportViewsPath()); 1252 String fileName; 1253 for (int i = 0; i < fileObject.getChildren().length; i++) { 1254 fileName = fileObject.getChildren()[i].getName().getBaseName(); 1255 if(!fileName.endsWith(".xml")) { 1256 continue; 1257 } 1258 ReportManagerLogger.debug(LanguageTraslator.traslate("277")+":"+fileName); 1259 try { 1260 saveReportView((ReportView) Unmarshaller.unmarshal(ReportView.class, new FileReader(getReportGeneratorConfiguration().getSourceReportViewsPath() + "/" + fileName))); 1261 } 1262 catch (Exception e) { 1263 exceptions.add(e); 1264 } 1265 } 1266 return exceptions; 1267 } 1268 catch (Exception e) { 1269 throw new InfoException(LanguageTraslator.traslate("252"), e); 1270 } 1271 } 1272 1273 1274 public ReportGeneratorConfiguration getReportGeneratorConfiguration() { 1275 return reportGeneratorConfiguration; 1276 } 1277 1278 1285 public ReportView getDefaultReportView(String reportDefinitionId, String userId) throws InfoException{ 1286 return getReportViewRepository().getDefaultViewForReportUser(reportDefinitionId,userId); 1287 }; 1288 1289 1290 1296 public ReportResult ExecReportQuery(MicroReport microReport) throws InfoException{ 1297 Map map = new HashMap(); 1298 return this.ExecReportQuery(microReport.getReportDefinition().getId(),map); 1299 } 1300 1301 1307 public int PrepareReport(MicroReport microReport) throws InfoException{ 1308 Report report = null; 1309 if (microReport.getReportDefinition() == null) { 1310 throw new InfoException(LanguageTraslator.traslate("12")); 1311 } 1312 else { 1313 ReportManagerLogger.debug(LanguageTraslator.traslate("196") + ":"+microReport.getReportDefinition().getId()); 1314 ReportSpec reportSpec = getReportSpec(microReport.getReportDefinition(),microReport.getReportSourceDefinition()); 1315 ReportSource reportSource = new ReportSource(reportSpec, microReport.getMatrix(), reportGeneratorConfiguration) ; 1316 if((reportSpec.getReportType().toString().equalsIgnoreCase(ReportDefinitionReportTypeType.CUBE.toString())) || 1317 (reportSpec.getReportType().toString().equalsIgnoreCase(ReportDefinitionReportTypeType.CHARTCUBE.toString()))) { 1318 report = new CubeReport(reportSpec, reportSource, reportGeneratorConfiguration); 1319 } else if(reportSpec.getReportType().toString().equalsIgnoreCase(ReportDefinitionReportTypeType.ACCUM.toString())) { 1320 report = new StaticReport(reportSpec, reportSource, reportGeneratorConfiguration); 1321 } 1322 if (report != null) { 1323 int handle = registerReport(report); 1324 return handle; 1325 } 1326 } 1327 return 0; 1328 } 1329 1330 1339 1350 1356 public Matrix getMatrix(int handle) throws InfoException{ 1357 return getReportFrom(handle).getPivot().getMatrix(); 1358 } 1359 1360 public String getXML(int handle) throws InfoException { 1361 return getReportFrom(handle).getXml(); 1362 } 1363 1364 public String getXML(String reportDefinitionID, Map paramValues) throws InfoException { 1365 int handle = PrepareReport(getReportDefinitionFromID(reportDefinitionID),paramValues); 1366 return getXML(handle); 1367 } 1368 1369 public Set getDimensionValues(int handle, String name) throws InfoException { 1370 return getReportFrom(handle).getDimensionValues(name); 1371 } 1372 1373 public Set getDimensionValues(String reportDefinitionID, Map paramValues, String name) throws InfoException { 1374 int handle = PrepareReport(getReportDefinitionFromID(reportDefinitionID),paramValues); 1375 return getDimensionValues(handle,name); 1376 } 1377 1378 public Vector getUpdatedDataModel(int handle, int mode, int row, int col, boolean isDistributed) throws InfoException { 1379 return ((CubeReport)getReportFrom(handle)).getUpdatedDataModel(mode, row,col, isDistributed); 1380 } 1381 1382 1390 public boolean validateUser(String userName, String password, String userRepositoryPath) throws InfoException { 1391 boolean authenticated = false; 1392 UsersRepository repository = new UsersRepository(userRepositoryPath); 1393 File file = new File(userRepositoryPath); 1394 if(file.exists()) { 1395 authenticated = repository.validate(userName, password); 1396 } else { 1397 repository.addNewUser("root", password); 1398 authenticated = true; 1399 } 1400 return authenticated; 1401 } 1402 1403 public boolean validateRol(String [] roles, String userName, String rolRepositoryPath) throws InfoException { 1404 boolean authenticated = false; 1405 RolsRepository repository = new RolsRepository(rolRepositoryPath); 1406 File file = new File(rolRepositoryPath); 1407 if(file.exists()) { 1408 int index = 0; 1409 while(index < roles.length && !authenticated){ 1410 authenticated = repository.validateRol(userName, roles[index]); 1411 index++; 1412 } 1413 } 1414 return authenticated; 1415 } 1416 1417 public void exportReport(String userName, String password,String userRepositoryPath, String reportDefinitionId, Map paramValues, boolean isLandscape, int type, String destinationPath, String name) throws InfoException { 1418 if (validateUser(userName,password,userRepositoryPath)){ 1419 try { 1420 ReportDefinition reportDefinition = getReportDefinitionFromID(reportDefinitionId); 1421 ReportSourceDefinition reportSource = getReportSourceDefinitionFromID(reportDefinition.getReportSource()); 1422 ReportSpec reportSpec = getReportSpec(reportDefinition, reportSource); 1423 ReportResult reportResult = null; 1424 if(!(reportDefinition.getReportType()==ReportDefinitionReportTypeType.STATICSQL)){ 1425 reportResult = ExecReportQuery(reportDefinition,paramValues); 1426 } 1427 ReportLayoutBuilder builder = new ReportLayoutBuilder(getReportGeneratorConfiguration(), reportSpec, reportResult, paramValues); 1428 JasperPrint print = builder.getJasperPrint(isLandscape); 1429 exportReport(print, (destinationPath + name), type); 1430 System.out.println(LanguageTraslator.traslate("533")); 1431 }catch(Exception e){ 1432 throw new InfoException(LanguageTraslator.traslate("312"), e); 1433 } 1434 } else{ 1435 throw new InfoException(LanguageTraslator.traslate("295")); 1436 } 1437 } 1438 1439 public byte[] exportReport(Map params) throws InfoException { 1440 try{ 1441 String reportDefinitionId = (String )params.get("ReportDefinitionId"); 1442 String reportViewId = (String )params.get("ReportViewId"); 1443 String userId = (String )params.get("UserId"); 1444 ReportDefinition definition; 1445 ReportView view = null; 1446 if(reportViewId!=null && !reportViewId.toString().equalsIgnoreCase("") 1447 && userId != null && reportDefinitionId!=null){ 1448 view = getReportViewFromID(reportViewId, reportDefinitionId, userId); 1449 definition = getReportDefinitionFromID(view.getReportDefinitionId()); 1450 }else if(reportDefinitionId!=null && !reportDefinitionId.toString().equalsIgnoreCase("")){ 1451 definition = getReportDefinitionFromID(reportDefinitionId.toString()); 1452 }else{ 1453 throw new InfoException(LanguageTraslator.traslate("2")); 1454 } 1455 ReportSpec spec = getReportSpec(definition); 1456 ReportResult result = null; 1457 if(definition.getReportType()!=ReportDefinitionReportTypeType.STATICSQL){ 1458 int handle = PrepareReport(definition, params); 1459 if(view!=null){ 1460 result = ExecReportQuery(handle, view); 1461 }else{ 1462 result = ExecReportQuery(handle, params); 1463 } 1464 } 1465 ReportLayoutBuilder builder = new ReportLayoutBuilder(getReportGeneratorConfiguration(), spec, result, params); 1466 boolean isLandscape = true; 1467 if(params.get("IsLandscape")!=null){ 1468 if(params.get("IsLandscape").toString().equalsIgnoreCase("false")){ 1469 isLandscape = false; 1470 } 1471 } 1472 JasperPrint print = builder.getJasperPrint(isLandscape); 1473 return JasperExportManager.exportReportToPdf(print); 1474 }catch (Exception e){ 1475 throw new InfoException(LanguageTraslator.traslate("311"), e); 1476 } 1477 } 1478 1479 1480 1541 1542 1543 private void exportReport(JasperPrint print, String destFile, int type) throws JRException, InfoException { 1544 switch(type){ 1545 case ReportExportFormatType.PDF_TYPE: 1546 JasperExportManager.exportReportToPdfFile(print,destFile); 1547 break; 1548 case ReportExportFormatType.XML_TYPE: 1549 JasperExportManager.exportReportToXmlFile(print,destFile, false); 1550 break; 1551 case ReportExportFormatType.HTML_TYPE: 1552 JasperExportManager.exportReportToHtmlFile(print,destFile); 1553 break; 1554 case ReportExportFormatType.EXCEL_TYPE: 1555 JRXlsExporter xlsExporter = new JRXlsExporter(); 1556 xlsExporter.setParameter(JRExporterParameter.JASPER_PRINT, print); 1557 xlsExporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, destFile); 1558 xlsExporter.exportReport(); 1559 break; 1560 case ReportExportFormatType.COMMA_TYPE: 1561 JRCsvExporter CsvExporter = new JRCsvExporter(); 1562 CsvExporter.setParameter(JRExporterParameter.JASPER_PRINT, print); 1563 CsvExporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, destFile); 1564 CsvExporter.exportReport(); 1565 break; 1566 default: throw new InfoException("312"); 1567 } 1568 } 1569 1570 1594 1595 public boolean addNewUser(String rootPasswd, String userName, String password, String userRepositoryPath) throws InfoException { 1596 UsersRepository repository = new UsersRepository(userRepositoryPath); 1597 if(repository.validate("root", rootPasswd)) { 1598 repository.addNewUser(userName, password); 1599 return true; 1600 } 1601 return false; 1602 } 1603 1604 public void addUserData(String userName, String name, String company, String userDataRepositoryPath) throws InfoException { 1605 UserDataRepository repository = new UserDataRepository(userDataRepositoryPath); 1606 repository.addNewUser(userName, name, company); 1607 } 1608 1609 public MicroReport getMicroReport(String fileName) throws InfoException { 1610 return new MicroReport(fileName, getReportGeneratorConfiguration()); 1611 } 1612 1613 public MicroReport getMicroReport(String reportDefinitionId, Map param) throws InfoException { 1614 MicroReportRepository microReportRepository = new MicroReportRepository(reportGeneratorConfiguration.getMicroReportRepositoryPath(),reportGeneratorConfiguration); 1615 return microReportRepository.findMicroReport(reportDefinitionId,param); 1616 } 1617 1618 public Collection getUserData(String userId, String userDataRepositoryPath) throws InfoException { 1619 UserDataRepository repository = new UserDataRepository(userDataRepositoryPath); 1620 return repository.getUserData(userId); 1621 } 1622 1623 public void logClientData(String clientData) throws InfoException{ 1624 clientData = "Time: " + new Date() + " - " + clientData; 1625 try { 1626 OutputStream stream = getClientLogger(); 1627 stream.write((clientData + "\n").getBytes()); 1628 } catch (IOException e) { 1629 throw new InfoException(LanguageTraslator.traslate("465"), e); 1630 } 1631 System.out.println(clientData); 1632 } 1633 1634 private OutputStream getClientLogger() throws FileNotFoundException { 1635 String fileName = getReportGeneratorConfiguration().getClientLogFile(); 1636 FileOutputStream stream = new FileOutputStream(fileName, true); 1637 return stream; 1638 } 1639 1640 public boolean changePasswd(String userName, String oldPasswd, String newPasswd, String userRepositoryPath) throws InfoException { 1641 UsersRepository repository = new UsersRepository(userRepositoryPath); 1642 if(repository.validate(userName, oldPasswd)) { 1643 repository.changePasswd(userName, newPasswd); 1644 return true; 1645 } 1646 return false; 1647 } 1648 1649 public boolean deleteUser(String userName, String password, String userRepositoryPath) throws InfoException{ 1650 UsersRepository repository = new UsersRepository(userRepositoryPath); 1651 if(repository.validate(userName, password)) { 1652 repository.deleteUser(userName); 1653 return true; 1654 } 1655 return false; 1656 } 1657 1658 public void addUserRol(String userName, String rol, String rolsRepositoryPath) throws InfoException { 1659 RolsRepository repository = new RolsRepository(rolsRepositoryPath); 1660 repository.addUserRol(userName, rol); 1661 } 1662 1663 private TempRepository getTempRepository() throws InfoException { 1664 if (tempRepository == null){ 1665 tempRepository = new TempRepository(getReportGeneratorConfiguration().getTempPath()); 1666 } 1667 return tempRepository; 1668 } 1669 1670 public boolean isAcceptedLicence() throws InfoException { 1671 return getTempRepository().isAcceptedLicence(); 1672 } 1673 1674 public void acceptedLicence(boolean value) throws InfoException { 1675 getTempRepository().acceptedLicence(value); 1676 } 1677 1678} 1679 1680 | Popular Tags |