View Javadoc

1   /*
2    * ReportRegistryImpl.java
3    * Created on 30 Август 2006 г., 0:39
4    */
5   
6   package org.weda.report.impl;
7   
8   import java.util.Collection;
9   import java.util.HashMap;
10  import java.util.List;
11  import java.util.Map;
12  import org.weda.data.DataProvider;
13  import org.weda.data.DataProviderServiceException;
14  import org.weda.report.Report;
15  import org.weda.report.ReportFactory;
16  import org.weda.report.ReportRegistry;
17  import org.weda.report.ReportRegistryException;
18  
19  /**
20   *
21   * @author Mikhail Titov
22   */
23  public class ReportRegistryImpl implements ReportRegistry {
24      private List<ReportFactory> reportFactories;
25      private Map<String/*reportGroup*/, Map<String/*reportName*/, Report>> 
26                  groups;
27      
28      public void init() throws ReportRegistryException {        
29          if (reportFactories.size()>0){
30              groups = new HashMap<String, Map<String, Report>>();
31              for (ReportFactory reportFactory: reportFactories){
32                  for (Report report: reportFactory.getReports()){
33                      Map<String, Report> reports = groups.get(report.getGroup());
34                      if (reports==null){
35                          reports = new HashMap<String, Report>();
36                          groups.put(report.getGroup(), reports);
37                      }else {
38                          if (reports.containsKey(report.getName()))
39                              throw new ReportRegistryException(
40                                      String.format(
41                                          "Duplicate report (%s) in group (%s) " +
42                                          "for report factory (%s)"
43                                          , report.getName()
44                                          , report.getGroup()
45                                          , reportFactory.getFactoryName()));                            
46                      }
47                      reports.put(report.getName(), report);
48                  }
49              }
50          }
51      }
52  
53      public Collection<String> getReportNamesForGroup(String group) 
54          throws ReportRegistryException 
55      {
56          return getReports(group).keySet();            
57      }
58  
59      public Report getReport(String group, String reportName) 
60          throws ReportRegistryException 
61      {
62          Map<String, Report> reports = getReports(group);
63          Report report = reports.get(reportName);
64          if (report==null)
65              throw new ReportRegistryException(
66                      String.format(
67                          "Report (%s) not found in report group (%s)"
68                          , reportName, group));
69          else
70              return report;
71      }
72  
73      public void setReportFactories(List<ReportFactory> reportFactories) {
74          this.reportFactories = reportFactories;
75      }
76      
77      private Map<String, Report> getReports(String group)
78          throws ReportRegistryException
79      {
80          Map<String, Report> reports = groups==null? null : groups.get(group);
81          if (reports == null)
82              throw new ReportRegistryException(
83                      String.format(
84                          "Group (%s) not found in report registry", group));
85          else
86              return reports;
87      }
88  
89      public DataProvider getDataProvider(Object dataIdentificator) 
90          throws DataProviderServiceException 
91      {
92          try {
93              ReportDataId repId = (ReportDataId)dataIdentificator;
94              Report report = 
95                      getReport(repId.getReportGroup(), repId.getReportName());
96              return new ReportDataProvider(report);
97          } catch (ReportRegistryException ex) {
98              throw new DataProviderServiceException(
99                          "Error while creating data provider for repor.", ex);
100         }
101     }
102 
103     public Class getDataIdentificatorClass() {
104         return ReportDataId.class;
105     }
106     
107 }