001: package com.calipso.xmleditor;
002:
003: import com.calipso.reportgenerator.reportdefinitions.*;
004: import com.calipso.reportgenerator.reportdefinitions.types.*;
005: import com.calipso.reportgenerator.reportmanager.SQLReportDataSource;
006: import com.calipso.reportgenerator.common.ReportGeneratorConfiguration;
007:
008: import javax.swing.*;
009: import java.util.*;
010: import java.math.BigDecimal;
011:
012: /**
013: *
014: * User: jbassino
015: * Date: 22/06/2004
016: * Time: 16:55:43
017: *
018: */
019: public class XmlEditorDefaultReports {
020: public static final String defaultEntity = "ANY",
021: defaultRol = "ANY";
022: public static final String defaultDefinitionTag = "RD";
023: public static final String defaultSourceTag = "RSD";
024: public static final String defaultLayoutTag = "RL";
025: public static final String defaultViewTag = "RV";
026: public static final int DEFAULT_COLUMN_WIDTH = 75;
027:
028: /**
029: * Genera un report definition seteando los valores segun los parametros recibidos
030: * @param reportName
031: * @param dimensionMap
032: * @param metricMap
033: * @return
034: */
035: public static ReportDefinition getReportDefinition(
036: ReportGeneratorConfiguration configuration,
037: String reportName, Map dimensionMap, Map metricMap) {
038: ReportDefinition report = new ReportDefinition();
039: setHeader(report, reportName);
040: setRoles(report);
041: setDimensionsDefinitions(report, dimensionMap);
042: setMetricsDefinitions(report, metricMap);
043: setActionsDefinitions(report);
044: setDrillDownDefinitions(report, reportName);
045: setLocalizations(report);
046: setFilterDefinitions(report);
047: setParameterValues(configuration, report);
048: return report;
049: }
050:
051: private static void setLocalizations(Object report) {
052: Localizations localizations = new Localizations();
053: /* Localization localization = new Localization();
054: localization.setLocale("");
055: localization.addLiterals(getLiterals());
056: localizations.addLocalization(localization);*/
057: if (report instanceof ReportDefinition) {
058: ((ReportDefinition) report).setLocalizations(localizations);
059: } else if (report instanceof ReportSourceDefinition) {
060: ((ReportSourceDefinition) report)
061: .setLocalizations(localizations);
062: }
063: }
064:
065: private static Literals getLiterals() {
066: Literals literals = new Literals();
067: literals.setCode("");
068: literals.setValue("");
069: return literals;
070: }
071:
072: private static void setDrillDownDefinitions(
073: ReportDefinition report, String reportName) {
074: DrillDownDefinitions drills = new DrillDownDefinitions();
075: /* DrillDownDefinition drill = new DrillDownDefinition();
076: drill.setDescription("");
077: drill.setName("");
078: drill.setTargetReportDefinitionID(getReportId(reportName));
079: drill.addDimensionBind(getDimensionBind());
080: drill.addParameterBind(getParameterBind());
081: drills.addDrillDownDefinition(drill);*/
082: report.setDrillDownDefinitions(drills);
083: }
084:
085: private static ParameterBind getParameterBind() {
086: ParameterBind param = new ParameterBind();
087: param.setSourceFilterName("");
088: param.setSourceParameterName("");
089: param.setTargetFilterName("");
090: param.setTargetParameterName("");
091: return param;
092: }
093:
094: private static DimensionBind getDimensionBind() {
095: DimensionBind bind = new DimensionBind();
096: bind.setSourceDimensionName("");
097: bind.setTargetFilterName("");
098: bind.setTargetParameterName("");
099: return bind;
100: }
101:
102: private static void setParameterValues(
103: ReportGeneratorConfiguration config, ReportDefinition report) {
104: ParameterValues params = new ParameterValues();
105: addStyleParams(config, params);
106: report.setParameterValues(params);
107: }
108:
109: private static void addStyleParams(
110: ReportGeneratorConfiguration config, ParameterValues params) {
111: String style = config.getReportLayoutStyle();
112: if (style != null && style.equalsIgnoreCase("CALIPSO")) {
113: addCalipsoParams(params);
114: }
115: }
116:
117: private static void addCalipsoParams(ParameterValues params) {
118: Collection calipsoParams = Arrays.asList(new String[] {
119: "countryparam", "zoneuser", "module" });
120: for (Iterator iterator = calipsoParams.iterator(); iterator
121: .hasNext();) {
122: String paramName = (String) iterator.next();
123: ParameterValue param = new ParameterValue();
124: param.setDefaultValue("");
125: param.setFilterDefinitionName(paramName);
126: param
127: .setFilterParameter(ParameterValueFilterParameterType.VALUE);
128: params.addParameterValue(param);
129: }
130: }
131:
132: private static void setFilterDefinitions(Object report) {
133: FilterDefinitions filters = new FilterDefinitions();
134: /* FilterDefinition filter = new FilterDefinition();
135: filter.setDimensionName("");
136: filter.setFilterType(FilterDefinitionFilterTypeType.EQUALTO);
137: filter.setName("");
138: filter.setVisual(false);
139: filters.addFilterDefinition(filter);*/
140: if (report instanceof ReportDefinition) {
141: ((ReportDefinition) report).setFilterDefinitions(filters);
142: } else if (report instanceof ReportSourceDefinition) {
143: ((ReportSourceDefinition) report)
144: .setFilterDefinitions(filters);
145: }
146: }
147:
148: private static void setActionsDefinitions(ReportDefinition report) {
149: ActionDefinitions actions = new ActionDefinitions();
150: /* ActionDefinition action = new ActionDefinition();
151: action.setDescription("");
152: action.setExpression("");
153: action.setMetric("");
154: action.setName("");
155: actions.addActionDefinition(action);*/
156: report.setActionDefinitions(actions);
157: }
158:
159: private static void setMetricsDefinitions(ReportDefinition report,
160: Map metricMap) {
161: MetricDefinitions metrics = new MetricDefinitions();
162: for (Iterator iterator = metricMap.entrySet().iterator(); iterator
163: .hasNext();) {
164: Map.Entry metric = (Map.Entry) iterator.next();
165: System.out.println(metric.toString());
166: metrics.addMetricDefinition(getMetricDefinition(metric));
167: }
168: report.setMetricDefinitions(metrics);
169: }
170:
171: private static MetricDefinition getMetricDefinition(Map.Entry metric) {
172: MetricDefinition metricDefinition = new MetricDefinition();
173: metricDefinition.setAccumulable(false);
174: metricDefinition.setDescription("");
175:
176: Vector vector = (Vector) metric.getValue();
177: metricDefinition.setName(getId(((String) vector.get(1))
178: .toString()));
179: metricDefinition.setVisible(true);
180: return metricDefinition;
181: }
182:
183: private static void setDimensionsDefinitions(
184: ReportDefinition report, Map dimensionMap) {
185: DimensionDefinitions dimensions = new DimensionDefinitions();
186: for (Iterator iterator = dimensionMap.entrySet().iterator(); iterator
187: .hasNext();) {
188: Map.Entry dimension = (Map.Entry) iterator.next();
189: System.out.println(dimension.toString());
190: dimensions
191: .addDimensionDefinition(getDimensionDefinition(dimension));
192: }
193: report.setDimensionDefinitions(dimensions);
194: }
195:
196: private static DimensionDefinition getDimensionDefinition(
197: Map.Entry dimension) {
198: DimensionDefinition dimensionDefinition = new DimensionDefinition();
199: dimensionDefinition.setDescription("");
200: dimensionDefinition.setGroupFooterCaption("");
201: dimensionDefinition.setGroups(false);
202: dimensionDefinition
203: .setLocation(DimensionDefinitionLocationType.PAGE);
204: Vector vector = (Vector) dimension.getValue();
205: dimensionDefinition.setName(getId(((String) vector.get(1))
206: .toString()));
207: dimensionDefinition.setOrder(DimensionDefinitionOrderType.A);
208: dimensionDefinition.setRankMetricName("");
209: return dimensionDefinition;
210: }
211:
212: private static String getId(String name) {
213: StringTokenizer stringTokenizer = new StringTokenizer(name, " ");
214: String id = stringTokenizer.nextToken().toUpperCase();
215: for (; stringTokenizer.hasMoreTokens();) {
216: id += ("_" + stringTokenizer.nextToken().toUpperCase());
217: }
218: return id;
219: }
220:
221: private static void setRoles(ReportDefinition report) {
222: Roles roles = new Roles();
223: Rol rol = new Rol();
224: rol.setName(defaultRol);
225: roles.addRol(rol);
226: report.setRoles(roles);
227: }
228:
229: private static void setHeader(ReportDefinition report,
230: String reportName) {
231: report.setId(getReportId(reportName));
232: report.setReportSource(getSourceId(reportName));
233: report.setTitle(reportName);
234: report.setDescription(reportName);
235: report.setLayoutDesign("");
236: report.setEntity(defaultEntity);
237: report.setIsTransient(false);
238: report.setReportType(ReportDefinitionReportTypeType.CUBE);
239: report.setPageFooter("");
240: report.setPageHeding("");
241: report.setVisibleTotals(true);
242: report.setMultilanguage(false);
243: report.setMaxRowCount(Integer.MAX_VALUE);
244: report.setMaxRowsDimensionReference("");
245: }
246:
247: public static String getViewId(String reportName) {
248: return (defaultViewTag + "_" + getId(reportName));
249: }
250:
251: /**
252: * Obtiene un id para un layout del estilo RL_NOMBRE_NOMBRE
253: * @param reportName
254: * @return
255: */
256: public static String getLayoutId(String reportName) {
257: reportName = removeTag(reportName);
258: return (defaultLayoutTag + "_" + getId(reportName));
259: }
260:
261: private static String removeTag(String reportName) {
262: String[] tags = new String[] { defaultDefinitionTag,
263: defaultSourceTag, defaultViewTag };
264: for (int i = 0; i < tags.length; i++) {
265: String tag = tags[i];
266: if (reportName.startsWith(tag + "_")) {
267: return reportName.substring(tag.length() + 1);
268: }
269: }
270: return reportName;
271: }
272:
273: /**
274: * Obtiene un id para un ReportSourceDefinition del estilo RSD_NOMBRE_NOMBRE
275: * @param reportName
276: * @return
277: */
278: public static String getSourceId(String reportName) {
279: return (defaultSourceTag + "_" + getId(reportName));
280: }
281:
282: /**
283: * Obtiene un id para un ReportDefinition del estilo RD_NOMBRE_NOMBRE
284: * @param reportName
285: * @return
286: */
287: public static String getReportId(String reportName) {
288: return (defaultDefinitionTag + "_" + getId(reportName));
289: }
290:
291: /**
292: * Obtiene un ReportSourceDefinition seteando los valores segun los parametros recibidos
293: * @param reportName
294: * @param dimensionMap
295: * @param metricMap
296: * @param query
297: * @param className
298: * @param localUrl
299: * @param user
300: * @param password
301: * @return
302: */
303: public static ReportSourceDefinition getReportSourceDefinition(
304: String reportName, Map dimensionMap, Map metricMap,
305: String query, String className, String localUrl,
306: String user, String password) {
307: ReportSourceDefinition report = new ReportSourceDefinition();
308: setSourceHeader(report, reportName);
309: setDimensionsSourceDefinitions(report, dimensionMap);
310: setMetricsSourceDefinitions(report, metricMap);
311: setLocalizations(report);
312: setFilterDefinitions(report);
313: setSourceParameterValues(report);
314: setDataSourceDefinitions(report, reportName, query, className,
315: localUrl, user, password);
316: return report;
317: }
318:
319: private static void setSourceParameterValues(
320: ReportSourceDefinition report) {
321: ParameterValues params = new ParameterValues();
322: report.setParameterValues(params);
323: }
324:
325: private static void setDataSourceDefinitions(
326: ReportSourceDefinition report, String reportName,
327: String query, String className, String localUrl,
328: String user, String password) {
329: DataSourceDefinitions datas = new DataSourceDefinitions();
330: datas
331: .addDataSourceDefinition(getDataSourceDefinition(
332: reportName, query, className, localUrl, user,
333: password));
334: report.setDataSourceDefinitions(datas);
335: }
336:
337: private static DataSourceDefinition getDataSourceDefinition(
338: String reportName, String query, String className,
339: String localUrl, String user, String password) {
340: DataSourceDefinition data = new DataSourceDefinition();
341: data.setDatePattern("");
342: data.setDateTimePattern("");
343: data.setExpression(addSqlTags(query));
344: data.setExternalClassName(className);
345: data.setExternalConnectionClassName(className);
346: data.setExternalConnectionDistributedUrl("");
347: data.setExternalConnectionLocalUrl(localUrl);
348: data.setExternalConnectionPassword(password);
349: data.setExternalConnectionString("");
350: data.setExternalConnectionUser(user);
351: data.setExternalSourceData("");
352: data.setName(getId(reportName));
353: data.setSourceType(DataSourceDefinitionSourceTypeType.SQL);
354: return data;
355: }
356:
357: private static String addSqlTags(String query) {
358: String result = query;
359: if (query.toUpperCase().indexOf("WHERE") > 0) {
360: result = query.substring(0, getIndexForTag(query)) + " "
361: + SQLReportDataSource.AND_FILTER_TAG + " "
362: + query.substring(getIndexForTag(query));
363: } else {
364: result = query.substring(0, getIndexForTag(query)) + " "
365: + SQLReportDataSource.WHERE_FILTER_TAG + " "
366: + query.substring(getIndexForTag(query));
367: }
368: return result;
369: }
370:
371: /**
372: * Obtiene el indice donde se debe agregar el tag de prefiltros en la query
373: * @param query
374: * @return
375: */
376: private static int getIndexForTag(String query) {
377: int firstIndex = query.toUpperCase().indexOf("GROUP BY");
378: int secondIndex = query.toUpperCase().indexOf("ORDER BY");
379: int result = Math.max(firstIndex, secondIndex);
380: if (result > 0) {
381: return result;
382: } else {
383: return query.length();
384: }
385: }
386:
387: private static void setMetricsSourceDefinitions(
388: ReportSourceDefinition report, Map metricMap) {
389: MetricSourceDefinitions metrics = new MetricSourceDefinitions();
390: for (Iterator iterator = metricMap.entrySet().iterator(); iterator
391: .hasNext();) {
392: Map.Entry metric = (Map.Entry) iterator.next();
393: metrics
394: .addMetricSourceDefinition(getMetricSourceDefinition(metric));
395: }
396: report.setMetricSourceDefinitions(metrics);
397: }
398:
399: private static MetricSourceDefinition getMetricSourceDefinition(
400: Map.Entry metric) {
401: MetricSourceDefinition definition = new MetricSourceDefinition();
402: definition.setAggregateFunction("");
403: definition.setAggregateType(CalculationType.SUM);
404: definition.setCalculated(false);
405: definition.setDescription(metric.getKey().toString());
406: Vector vector = (Vector) metric.getValue();
407: definition.setExternalCalculated(false);
408: definition.setExternalData(((String) vector.get(1)).toString());
409: definition.setGroupFooterFunction("");
410: definition.setGroupFooterType(CalculationType.NONE);
411: definition.setName(((String) vector.get(1)).toString());
412: return definition;
413: }
414:
415: private static void setDimensionsSourceDefinitions(
416: ReportSourceDefinition report, Map dimensionMap) {
417: DimensionSourceDefinitions dimensions = new DimensionSourceDefinitions();
418: for (Iterator iterator = dimensionMap.entrySet().iterator(); iterator
419: .hasNext();) {
420: Map.Entry dimension = (Map.Entry) iterator.next();
421: dimensions
422: .addDimensionSourceDefinition(getDimensionSourceDefinition(dimension));
423: }
424: report.setDimensionSourceDefinitions(dimensions);
425: }
426:
427: private static DimensionSourceDefinition getDimensionSourceDefinition(
428: Map.Entry dimension) {
429: DimensionSourceDefinition definition = new DimensionSourceDefinition();
430: definition.setCalculated(false);
431: Vector vector = (Vector) dimension.getValue();
432: definition.setDataType(getType(((Integer) vector.get(0))
433: .intValue()));
434: definition.setDescription(dimension.getKey().toString());
435:
436: definition.setExpression("");
437: definition.setExternalData(((String) vector.get(1)).toString());
438: definition.setName(((String) vector.get(1)).toString());
439: //definition.setQueryConvertToStringPattern("");
440: return definition;
441: }
442:
443: /**
444: *
445: * @param type identificador del tipo de dato
446: */
447: private static ReportDataType getType(int type) {
448: switch (type) {
449: case 4:
450: return ReportDataType.INTEGER;
451: case 6:
452: return ReportDataType.FLOAT;
453: case 12:
454: return ReportDataType.STRING;
455: case 16:
456: return ReportDataType.BOOLEAN;
457: case 91:
458: return ReportDataType.DATETIME;
459: default:
460: return ReportDataType.STRING;
461: }
462: }
463:
464: private static void setSourceHeader(ReportSourceDefinition report,
465: String reportName) {
466: report.setCached(false);
467: report.setExpiration(new BigDecimal(0));
468: report.setId(getSourceId(reportName));
469: report.setIncrementalDimension("");
470: report.setMaxRowCount(Integer.MAX_VALUE);
471: report.setMaxRowsDimensionReference("");
472: report.setMultilanguage(false);
473: }
474:
475: public static ReportView getReportView(
476: XmlEditorTreeModelNode dimensions,
477: XmlEditorTreeModelNode metrics, String reportDefinitionId) {
478: ReportView reportView = new ReportView();
479: setViewHeader(reportView, reportDefinitionId);
480: reportView.setLightBoxer(getViewLightBoxer());
481: reportView
482: .setDimensionProperties(getViewDimensionProperties(dimensions));
483: reportView
484: .setMetricProperties(getViewMetricProperties(metrics));
485: return reportView;
486: }
487:
488: private static MetricProperties getViewMetricProperties(
489: XmlEditorTreeModelNode metrics) {
490: MetricProperties properties = new MetricProperties();
491: Enumeration enumeration = metrics.children();
492: while (enumeration.hasMoreElements()) {
493: XmlEditorTreeModelNode metricNode = (XmlEditorTreeModelNode) enumeration
494: .nextElement();
495: properties
496: .addMetricProperty(getMetricPropertyFromDefinitionNode(metricNode));
497: }
498: return properties;
499: }
500:
501: private static MetricProperty getMetricPropertyFromDefinitionNode(
502: XmlEditorTreeModelNode metricNode) {
503: MetricProperty property = new MetricProperty();
504: property.setMetricName(metricNode.getAttribute("Name")
505: .toString());
506: property.setVisible(Boolean.valueOf(
507: metricNode.getAttribute("Visible")).booleanValue()); //Boolean.getBoolean falla
508: return property;
509: }
510:
511: private static DimensionProperties getViewDimensionProperties(
512: XmlEditorTreeModelNode dimensions) {
513: DimensionProperties properties = new DimensionProperties();
514: Enumeration enumeration = dimensions.children();
515: while (enumeration.hasMoreElements()) {
516: XmlEditorTreeModelNode dimensionNode = (XmlEditorTreeModelNode) enumeration
517: .nextElement();
518: properties
519: .addDimensionProperty(getDimensionPropertyFromDefinitionNode(dimensionNode));
520: }
521: return properties;
522: }
523:
524: private static int getDefaultColumnSize(
525: XmlEditorTreeModelNode dimensionNode) {
526: return DEFAULT_COLUMN_WIDTH;
527: }
528:
529: private static DimensionProperty getDimensionPropertyFromDefinitionNode(
530: XmlEditorTreeModelNode dimensionNode) {
531: DimensionProperty property = new DimensionProperty();
532: property.setDimensionName(dimensionNode.getUserObject()
533: .toString());
534: //property.setExcludeValue(null);
535: property.setLocation(DimensionPropertyLocationType
536: .valueOf(dimensionNode.getAttribute("Location")));
537: property.setOrder(DimensionPropertyOrderType
538: .valueOf(dimensionNode.getAttribute("Order")));
539: property.setRankMetricName(dimensionNode
540: .getAttribute("RankMetricName"));
541: property.setUbication(0);
542: property.setWidth(getDefaultColumnSize(dimensionNode));
543: return property;
544: }
545:
546: private static LightBoxer getViewLightBoxer() {
547: LightBoxer boxer = new LightBoxer();
548: return boxer;
549: }
550:
551: private static void setViewHeader(ReportView reportView,
552: String reportDefinitionId) {
553: reportView.setDefault(false);
554: reportDefinitionId = JOptionPane.showInputDialog(null,
555: "Confirme el nombre del reporte", reportDefinitionId);
556: reportView.setReportDefinitionId(reportDefinitionId);
557: String reportViewName = JOptionPane.showInputDialog(null,
558: "Ingrese el nombre de la vista", reportDefinitionId);
559: reportView.setDescription(reportViewName);
560: reportView.setId(getViewId(reportViewName));
561: String userName = JOptionPane.showInputDialog(null,
562: "Ingrese su nombre de usuario", "Report View",
563: JOptionPane.INFORMATION_MESSAGE);
564: reportView.setUserID(userName);
565: reportView.setUserName(userName);
566: reportView.setVisibleTotals(true);
567: }
568:
569: }
|