KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > calipso > xmleditor > XmlEditorDefaultReports


1 package com.calipso.xmleditor;
2
3 import com.calipso.reportgenerator.reportdefinitions.*;
4 import com.calipso.reportgenerator.reportdefinitions.types.*;
5 import com.calipso.reportgenerator.reportmanager.SQLReportDataSource;
6 import com.calipso.reportgenerator.common.ReportGeneratorConfiguration;
7
8 import javax.swing.*;
9 import java.util.*;
10 import java.math.BigDecimal JavaDoc;
11
12 /**
13  *
14  * User: jbassino
15  * Date: 22/06/2004
16  * Time: 16:55:43
17  *
18  */

19 public class XmlEditorDefaultReports {
20   public static final String JavaDoc defaultEntity = "ANY", defaultRol = "ANY";
21   public static final String JavaDoc defaultDefinitionTag = "RD";
22   public static final String JavaDoc defaultSourceTag = "RSD";
23   public static final String JavaDoc defaultLayoutTag = "RL";
24   public static final String JavaDoc defaultViewTag = "RV";
25   public static final int DEFAULT_COLUMN_WIDTH = 75;
26
27   /**
28    * Genera un report definition seteando los valores segun los parametros recibidos
29    * @param reportName
30    * @param dimensionMap
31    * @param metricMap
32    * @return
33    */

34   public static ReportDefinition getReportDefinition(ReportGeneratorConfiguration configuration, String JavaDoc reportName, Map dimensionMap, Map metricMap){
35     ReportDefinition report = new ReportDefinition();
36     setHeader(report, reportName);
37     setRoles(report);
38     setDimensionsDefinitions(report, dimensionMap);
39     setMetricsDefinitions(report, metricMap);
40     setActionsDefinitions(report);
41     setDrillDownDefinitions(report, reportName);
42     setLocalizations(report);
43     setFilterDefinitions(report);
44     setParameterValues(configuration, report);
45     return report;
46   }
47
48   private static void setLocalizations(Object JavaDoc report) {
49     Localizations localizations = new Localizations();
50 /* Localization localization = new Localization();
51     localization.setLocale("");
52     localization.addLiterals(getLiterals());
53     localizations.addLocalization(localization);*/

54     if(report instanceof ReportDefinition){
55       ((ReportDefinition)report).setLocalizations(localizations);
56     }else if (report instanceof ReportSourceDefinition){
57       ((ReportSourceDefinition)report).setLocalizations(localizations);
58     }
59   }
60
61   private static Literals getLiterals() {
62     Literals literals = new Literals();
63     literals.setCode("");
64     literals.setValue("");
65     return literals;
66   }
67
68   private static void setDrillDownDefinitions(ReportDefinition report, String JavaDoc reportName) {
69     DrillDownDefinitions drills = new DrillDownDefinitions();
70 /* DrillDownDefinition drill = new DrillDownDefinition();
71     drill.setDescription("");
72     drill.setName("");
73     drill.setTargetReportDefinitionID(getReportId(reportName));
74     drill.addDimensionBind(getDimensionBind());
75     drill.addParameterBind(getParameterBind());
76     drills.addDrillDownDefinition(drill);*/

77     report.setDrillDownDefinitions(drills);
78   }
79
80   private static ParameterBind getParameterBind() {
81     ParameterBind param = new ParameterBind();
82     param.setSourceFilterName("");
83     param.setSourceParameterName("");
84     param.setTargetFilterName("");
85     param.setTargetParameterName("");
86     return param;
87   }
88
89   private static DimensionBind getDimensionBind() {
90     DimensionBind bind = new DimensionBind();
91     bind.setSourceDimensionName("");
92     bind.setTargetFilterName("");
93     bind.setTargetParameterName("");
94     return bind;
95   }
96
97   private static void setParameterValues(ReportGeneratorConfiguration config, ReportDefinition report) {
98     ParameterValues params = new ParameterValues();
99     addStyleParams(config, params);
100     report.setParameterValues(params);
101   }
102
103   private static void addStyleParams(ReportGeneratorConfiguration config, ParameterValues params) {
104     String JavaDoc style = config.getReportLayoutStyle();
105     if(style != null && style.equalsIgnoreCase("CALIPSO")){
106       addCalipsoParams(params);
107     }
108   }
109
110   private static void addCalipsoParams(ParameterValues params) {
111     Collection calipsoParams = Arrays.asList(new String JavaDoc[] {"countryparam", "zoneuser", "module"});
112     for (Iterator iterator = calipsoParams.iterator(); iterator.hasNext();) {
113       String JavaDoc paramName = (String JavaDoc) iterator.next();
114       ParameterValue param = new ParameterValue();
115       param.setDefaultValue("");
116       param.setFilterDefinitionName(paramName);
117       param.setFilterParameter(ParameterValueFilterParameterType.VALUE);
118       params.addParameterValue(param);
119     }
120   }
121
122   private static void setFilterDefinitions(Object JavaDoc report) {
123     FilterDefinitions filters = new FilterDefinitions();
124 /* FilterDefinition filter = new FilterDefinition();
125     filter.setDimensionName("");
126     filter.setFilterType(FilterDefinitionFilterTypeType.EQUALTO);
127     filter.setName("");
128     filter.setVisual(false);
129     filters.addFilterDefinition(filter);*/

130     if(report instanceof ReportDefinition){
131       ((ReportDefinition)report).setFilterDefinitions(filters);
132     }else if(report instanceof ReportSourceDefinition){
133       ((ReportSourceDefinition)report).setFilterDefinitions(filters);
134     }
135   }
136
137   private static void setActionsDefinitions(ReportDefinition report) {
138     ActionDefinitions actions = new ActionDefinitions();
139 /* ActionDefinition action = new ActionDefinition();
140     action.setDescription("");
141     action.setExpression("");
142     action.setMetric("");
143     action.setName("");
144     actions.addActionDefinition(action);*/

145     report.setActionDefinitions(actions);
146   }
147
148   private static void setMetricsDefinitions(ReportDefinition report, Map metricMap) {
149     MetricDefinitions metrics = new MetricDefinitions();
150     for (Iterator iterator = metricMap.entrySet().iterator(); iterator.hasNext();) {
151       Map.Entry metric = (Map.Entry) iterator.next();
152       System.out.println(metric.toString());
153       metrics.addMetricDefinition(getMetricDefinition(metric));
154     }
155     report.setMetricDefinitions(metrics);
156   }
157
158   private static MetricDefinition getMetricDefinition(Map.Entry metric) {
159     MetricDefinition metricDefinition = new MetricDefinition();
160     metricDefinition.setAccumulable(false);
161     metricDefinition.setDescription("");
162
163     Vector vector = (Vector)metric.getValue();
164     metricDefinition.setName(getId(((String JavaDoc)vector.get(1)).toString()));
165     metricDefinition.setVisible(true);
166     return metricDefinition;
167   }
168
169   private static void setDimensionsDefinitions(ReportDefinition report, Map dimensionMap) {
170     DimensionDefinitions dimensions = new DimensionDefinitions();
171     for (Iterator iterator = dimensionMap.entrySet().iterator(); iterator.hasNext();) {
172       Map.Entry dimension = (Map.Entry) iterator.next();
173       System.out.println(dimension.toString());
174       dimensions.addDimensionDefinition(getDimensionDefinition(dimension));
175     }
176     report.setDimensionDefinitions(dimensions);
177   }
178
179   private static DimensionDefinition getDimensionDefinition(Map.Entry dimension) {
180     DimensionDefinition dimensionDefinition = new DimensionDefinition();
181     dimensionDefinition.setDescription("");
182     dimensionDefinition.setGroupFooterCaption("");
183     dimensionDefinition.setGroups(false);
184     dimensionDefinition.setLocation(DimensionDefinitionLocationType.PAGE);
185     Vector vector = (Vector)dimension.getValue();
186     dimensionDefinition.setName(getId(((String JavaDoc)vector.get(1)).toString()));
187     dimensionDefinition.setOrder(DimensionDefinitionOrderType.A);
188     dimensionDefinition.setRankMetricName("");
189     return dimensionDefinition;
190   }
191
192   private static String JavaDoc getId(String JavaDoc name) {
193     StringTokenizer stringTokenizer = new StringTokenizer(name, " ");
194     String JavaDoc id = stringTokenizer.nextToken().toUpperCase();
195     for (; stringTokenizer.hasMoreTokens();) {
196       id += ("_" + stringTokenizer.nextToken().toUpperCase());
197     }
198     return id;
199   }
200
201   private static void setRoles(ReportDefinition report) {
202     Roles roles = new Roles();
203     Rol rol = new Rol();
204     rol.setName(defaultRol);
205     roles.addRol(rol);
206     report.setRoles(roles);
207   }
208
209   private static void setHeader(ReportDefinition report, String JavaDoc reportName) {
210     report.setId(getReportId(reportName));
211     report.setReportSource(getSourceId(reportName));
212     report.setTitle(reportName);
213     report.setDescription(reportName);
214     report.setLayoutDesign("");
215     report.setEntity(defaultEntity);
216     report.setIsTransient(false);
217     report.setReportType(ReportDefinitionReportTypeType.CUBE);
218     report.setPageFooter("");
219     report.setPageHeding("");
220     report.setVisibleTotals(true);
221     report.setMultilanguage(false);
222     report.setMaxRowCount(Integer.MAX_VALUE);
223     report.setMaxRowsDimensionReference("");
224   }
225
226   public static String JavaDoc getViewId(String JavaDoc reportName){
227     return (defaultViewTag + "_" + getId(reportName));
228   }
229
230   /**
231    * Obtiene un id para un layout del estilo RL_NOMBRE_NOMBRE
232    * @param reportName
233    * @return
234    */

235   public static String JavaDoc getLayoutId(String JavaDoc reportName) {
236     reportName = removeTag(reportName);
237     return (defaultLayoutTag + "_" + getId(reportName));
238   }
239
240   private static String JavaDoc removeTag(String JavaDoc reportName) {
241     String JavaDoc[] tags = new String JavaDoc[] {defaultDefinitionTag, defaultSourceTag, defaultViewTag};
242     for (int i = 0; i < tags.length; i++) {
243       String JavaDoc tag = tags[i];
244       if(reportName.startsWith(tag + "_")){
245         return reportName.substring(tag.length() + 1);
246       }
247     }
248     return reportName;
249   }
250
251   /**
252    * Obtiene un id para un ReportSourceDefinition del estilo RSD_NOMBRE_NOMBRE
253    * @param reportName
254    * @return
255    */

256   public static String JavaDoc getSourceId(String JavaDoc reportName) {
257     return (defaultSourceTag + "_" + getId(reportName));
258   }
259
260   /**
261    * Obtiene un id para un ReportDefinition del estilo RD_NOMBRE_NOMBRE
262    * @param reportName
263    * @return
264    */

265   public static String JavaDoc getReportId(String JavaDoc reportName) {
266     return (defaultDefinitionTag + "_" + getId(reportName));
267   }
268
269   /**
270    * Obtiene un ReportSourceDefinition seteando los valores segun los parametros recibidos
271    * @param reportName
272    * @param dimensionMap
273    * @param metricMap
274    * @param query
275    * @param className
276    * @param localUrl
277    * @param user
278    * @param password
279    * @return
280    */

281   public static ReportSourceDefinition getReportSourceDefinition(String JavaDoc reportName, Map dimensionMap, Map metricMap, String JavaDoc query, String JavaDoc className, String JavaDoc localUrl, String JavaDoc user, String JavaDoc password) {
282     ReportSourceDefinition report = new ReportSourceDefinition();
283     setSourceHeader(report, reportName);
284     setDimensionsSourceDefinitions(report, dimensionMap);
285     setMetricsSourceDefinitions(report, metricMap);
286     setLocalizations(report);
287     setFilterDefinitions(report);
288     setSourceParameterValues(report);
289     setDataSourceDefinitions(report, reportName, query, className, localUrl, user, password);
290     return report;
291   }
292
293   private static void setSourceParameterValues(ReportSourceDefinition report) {
294     ParameterValues params = new ParameterValues();
295     report.setParameterValues(params);
296   }
297
298   private static void setDataSourceDefinitions(ReportSourceDefinition report, String JavaDoc reportName, String JavaDoc query, String JavaDoc className, String JavaDoc localUrl, String JavaDoc user, String JavaDoc password) {
299     DataSourceDefinitions datas = new DataSourceDefinitions();
300     datas.addDataSourceDefinition(getDataSourceDefinition(reportName, query, className, localUrl, user, password));
301     report.setDataSourceDefinitions(datas);
302   }
303
304   private static DataSourceDefinition getDataSourceDefinition(String JavaDoc reportName, String JavaDoc query, String JavaDoc className, String JavaDoc localUrl, String JavaDoc user, String JavaDoc password) {
305     DataSourceDefinition data = new DataSourceDefinition();
306     data.setDatePattern("");
307     data.setDateTimePattern("");
308     data.setExpression(addSqlTags(query));
309     data.setExternalClassName(className);
310     data.setExternalConnectionClassName(className);
311     data.setExternalConnectionDistributedUrl("");
312     data.setExternalConnectionLocalUrl(localUrl);
313     data.setExternalConnectionPassword(password);
314     data.setExternalConnectionString("");
315     data.setExternalConnectionUser(user);
316     data.setExternalSourceData("");
317     data.setName(getId(reportName));
318     data.setSourceType(DataSourceDefinitionSourceTypeType.SQL);
319     return data;
320   }
321
322   private static String JavaDoc addSqlTags(String JavaDoc query) {
323     String JavaDoc result = query;
324     if(query.toUpperCase().indexOf("WHERE")>0){
325       result = query.substring(0, getIndexForTag(query)) + " " + SQLReportDataSource.AND_FILTER_TAG + " " + query.substring(getIndexForTag(query));
326     }else{
327       result = query.substring(0, getIndexForTag(query)) + " " + SQLReportDataSource.WHERE_FILTER_TAG + " " + query.substring(getIndexForTag(query));
328     }
329     return result;
330   }
331
332   /**
333    * Obtiene el indice donde se debe agregar el tag de prefiltros en la query
334    * @param query
335    * @return
336    */

337   private static int getIndexForTag(String JavaDoc query) {
338     int firstIndex = query.toUpperCase().indexOf("GROUP BY");
339     int secondIndex = query.toUpperCase().indexOf("ORDER BY");
340     int result = Math.max(firstIndex, secondIndex);
341     if(result > 0){
342       return result;
343     }else{
344       return query.length();
345     }
346   }
347
348   private static void setMetricsSourceDefinitions(ReportSourceDefinition report, Map metricMap) {
349     MetricSourceDefinitions metrics = new MetricSourceDefinitions();
350     for (Iterator iterator = metricMap.entrySet().iterator(); iterator.hasNext();) {
351       Map.Entry metric = (Map.Entry) iterator.next();
352       metrics.addMetricSourceDefinition(getMetricSourceDefinition(metric));
353     }
354     report.setMetricSourceDefinitions(metrics);
355   }
356
357   private static MetricSourceDefinition getMetricSourceDefinition(Map.Entry metric) {
358     MetricSourceDefinition definition = new MetricSourceDefinition();
359     definition.setAggregateFunction("");
360     definition.setAggregateType(CalculationType.SUM);
361     definition.setCalculated(false);
362     definition.setDescription(metric.getKey().toString());
363     Vector vector = (Vector)metric.getValue();
364     definition.setExternalCalculated(false);
365     definition.setExternalData(((String JavaDoc)vector.get(1)).toString());
366     definition.setGroupFooterFunction("");
367     definition.setGroupFooterType(CalculationType.NONE);
368     definition.setName(((String JavaDoc)vector.get(1)).toString());
369     return definition;
370   }
371
372   private static void setDimensionsSourceDefinitions(ReportSourceDefinition report, Map dimensionMap) {
373     DimensionSourceDefinitions dimensions = new DimensionSourceDefinitions();
374     for (Iterator iterator = dimensionMap.entrySet().iterator(); iterator.hasNext();) {
375       Map.Entry dimension = (Map.Entry) iterator.next();
376       dimensions.addDimensionSourceDefinition(getDimensionSourceDefinition(dimension));
377     }
378     report.setDimensionSourceDefinitions(dimensions);
379   }
380
381   private static DimensionSourceDefinition getDimensionSourceDefinition(Map.Entry dimension) {
382     DimensionSourceDefinition definition = new DimensionSourceDefinition();
383     definition.setCalculated(false);
384     Vector vector = (Vector)dimension.getValue();
385     definition.setDataType(getType(((Integer JavaDoc)vector.get(0)).intValue()));
386     definition.setDescription(dimension.getKey().toString());
387
388     definition.setExpression("");
389     definition.setExternalData(((String JavaDoc)vector.get(1)).toString());
390     definition.setName(((String JavaDoc)vector.get(1)).toString());
391     //definition.setQueryConvertToStringPattern("");
392
return definition;
393   }
394  /**
395   *
396   * @param type identificador del tipo de dato
397    */

398   private static ReportDataType getType(int type) {
399     switch(type){
400       case 4 : return ReportDataType.INTEGER;
401       case 6 : return ReportDataType.FLOAT;
402       case 12: return ReportDataType.STRING;
403       case 16: return ReportDataType.BOOLEAN;
404       case 91: return ReportDataType.DATETIME;
405       default: return ReportDataType.STRING;
406     }
407   }
408
409   private static void setSourceHeader(ReportSourceDefinition report, String JavaDoc reportName) {
410     report.setCached(false);
411     report.setExpiration(new BigDecimal JavaDoc(0));
412     report.setId(getSourceId(reportName));
413     report.setIncrementalDimension("");
414     report.setMaxRowCount(Integer.MAX_VALUE);
415     report.setMaxRowsDimensionReference("");
416     report.setMultilanguage(false);
417   }
418
419   public static ReportView getReportView(XmlEditorTreeModelNode dimensions, XmlEditorTreeModelNode metrics, String JavaDoc reportDefinitionId) {
420     ReportView reportView = new ReportView();
421     setViewHeader(reportView, reportDefinitionId);
422     reportView.setLightBoxer(getViewLightBoxer());
423     reportView.setDimensionProperties(getViewDimensionProperties(dimensions));
424     reportView.setMetricProperties(getViewMetricProperties(metrics));
425     return reportView;
426   }
427
428   private static MetricProperties getViewMetricProperties(XmlEditorTreeModelNode metrics) {
429     MetricProperties properties = new MetricProperties();
430     Enumeration enumeration = metrics.children();
431     while (enumeration.hasMoreElements()) {
432       XmlEditorTreeModelNode metricNode = (XmlEditorTreeModelNode) enumeration.nextElement();
433       properties.addMetricProperty(getMetricPropertyFromDefinitionNode(metricNode));
434     }
435     return properties;
436   }
437
438   private static MetricProperty getMetricPropertyFromDefinitionNode(XmlEditorTreeModelNode metricNode) {
439     MetricProperty property = new MetricProperty();
440     property.setMetricName(metricNode.getAttribute("Name").toString());
441     property.setVisible(Boolean.valueOf(metricNode.getAttribute("Visible")).booleanValue()); //Boolean.getBoolean falla
442
return property;
443   }
444
445   private static DimensionProperties getViewDimensionProperties(XmlEditorTreeModelNode dimensions) {
446     DimensionProperties properties = new DimensionProperties();
447     Enumeration enumeration = dimensions.children();
448     while (enumeration.hasMoreElements()) {
449       XmlEditorTreeModelNode dimensionNode = (XmlEditorTreeModelNode) enumeration.nextElement();
450       properties.addDimensionProperty(getDimensionPropertyFromDefinitionNode(dimensionNode));
451     }
452     return properties;
453   }
454
455
456   private static int getDefaultColumnSize(XmlEditorTreeModelNode dimensionNode) {
457     return DEFAULT_COLUMN_WIDTH;
458   }
459
460   private static DimensionProperty getDimensionPropertyFromDefinitionNode(XmlEditorTreeModelNode dimensionNode) {
461     DimensionProperty property = new DimensionProperty();
462     property.setDimensionName(dimensionNode.getUserObject().toString());
463     //property.setExcludeValue(null);
464
property.setLocation(DimensionPropertyLocationType.valueOf(dimensionNode.getAttribute("Location")));
465     property.setOrder(DimensionPropertyOrderType.valueOf(dimensionNode.getAttribute("Order")));
466     property.setRankMetricName(dimensionNode.getAttribute("RankMetricName"));
467     property.setUbication(0);
468     property.setWidth(getDefaultColumnSize(dimensionNode));
469     return property;
470   }
471   private static LightBoxer getViewLightBoxer() {
472     LightBoxer boxer = new LightBoxer();
473     return boxer;
474   }
475
476   private static void setViewHeader(ReportView reportView, String JavaDoc reportDefinitionId) {
477     reportView.setDefault(false);
478     reportDefinitionId = JOptionPane.showInputDialog(null, "Confirme el nombre del reporte", reportDefinitionId);
479     reportView.setReportDefinitionId(reportDefinitionId);
480     String JavaDoc reportViewName = JOptionPane.showInputDialog(null, "Ingrese el nombre de la vista", reportDefinitionId);
481     reportView.setDescription(reportViewName);
482     reportView.setId(getViewId(reportViewName));
483     String JavaDoc userName = JOptionPane.showInputDialog(null, "Ingrese su nombre de usuario", "Report View", JOptionPane.INFORMATION_MESSAGE);
484     reportView.setUserID(userName);
485     reportView.setUserName(userName);
486     reportView.setVisibleTotals(true);
487   }
488
489 }
490
Popular Tags