View Javadoc

1   /**
2    * 
3    */
4   package ar.com.jiji.kaya.reports;
5   
6   import java.util.Collection;
7   import java.util.Collections;
8   import java.util.Enumeration;
9   import java.util.HashMap;
10  import java.util.Map;
11  import java.util.PropertyResourceBundle;
12  import java.util.ResourceBundle;
13  import java.util.regex.Matcher;
14  import java.util.regex.Pattern;
15  
16  /**
17   * Mantiene una lista de los reportes definidos para el sistema.
18   * 
19   * TODO: documentar y hacer tests
20   * 
21   * @author lparra
22   * 
23   */
24  public class ReportSpecManager {
25  	private static final Object REPORT_PKG = "package";
26  
27  	private static final Object DS_PROPERTY = "ds";
28  
29  	private static final Object LOCATION_PROPERTY = "location";
30  
31  	private static final Object DOWNLOAD_NAME_PROPERTY = "downloadName";
32  
33  	private static final Object COLUMNS_PROPERTY = "columns";
34  
35  	private String reportPkg = "";
36  
37  	private Map<String, ReportSpec> specs = new HashMap<String, ReportSpec>();
38  
39  	public ReportSpecManager() {
40  	}
41  
42  	public ReportSpecManager(String resource) throws ReportException {
43  		PropertyResourceBundle props = (PropertyResourceBundle) ResourceBundle
44  				.getBundle(resource);
45  
46  		Map<String, ReportSpec> aux = new HashMap<String, ReportSpec>();
47  
48  		Pattern p = Pattern.compile("([\\w_-]+)\\.(\\w+)");
49  		for (Enumeration e = props.getKeys(); e.hasMoreElements();) {
50  			String k = (String) e.nextElement();
51  			String value = props.getString(k);
52  
53  			if (REPORT_PKG.equals(k)) {
54  				setReportPkg(value);
55  				continue;
56  			}
57  
58  			Matcher m = p.matcher(k);
59  			if (m.matches()) {
60  				String rep = m.group(1);
61  				String prop = m.group(2);
62  
63  				ReportSpec spec = aux.get(rep);
64  				if (spec == null)
65  					spec = new ReportSpec(rep);
66  				if (DS_PROPERTY.equals(prop))
67  					spec.setDsExtractor(value);
68  				else if (LOCATION_PROPERTY.equals(prop))
69  					spec.setLocation(value);
70  				else if (DOWNLOAD_NAME_PROPERTY.equals(prop))
71  					spec.setDownloadName(value);
72  				else if (COLUMNS_PROPERTY.equals(prop))
73  					spec.setColumns(value.split(","));
74  				aux.put(rep, spec);
75  			}
76  		}
77  
78  		for (ReportSpec spec : aux.values()) {
79  			if (spec.getDsExtractor() == null)
80  				throw new ReportException("Report " + spec.getName()
81  						+ " has no data source extractor.");
82  			if (spec.getLocation() == null)
83  				spec.setLocation(getDefaultLocation(spec.getName()));
84  		}
85  
86  		specs.putAll(aux);
87  	}
88  
89  	public void add(String name, ReportSpec spec) {
90  		specs.put(name, spec);
91  	}
92  
93  	public void add(String name, String dsExtractor) throws ReportException {
94  		add(name, dsExtractor, getDefaultLocation(name));
95  	}
96  
97  	private String getDefaultLocation(String name) {
98  		return reportPkg + '/' + name + ".jasper";
99  	}
100 
101 	public void add(String name, String dsExtractor, String location)
102 			throws ReportException {
103 		specs.put(name, new ReportSpec(name, dsExtractor, location));
104 	}
105 
106 	public ReportSpec get(String name) {
107 		return specs.get(name);
108 	}
109 
110 	public void remove(String name) {
111 		specs.remove(name);
112 	}
113 
114 	public Collection<ReportSpec> getReports() {
115 		return Collections.unmodifiableCollection(specs.values());
116 	}
117 
118 	public String getReportPkg() {
119 		return reportPkg;
120 	}
121 
122 	public void setReportPkg(String reportPkg) {
123 		this.reportPkg = reportPkg;
124 	}
125 }