001: package com.calipso.reportgenerator.common;
002:
003: import com.calipso.reportgenerator.reportcalculator.ExpressionCubeFilter;
004: import com.calipso.reportgenerator.reportcalculator.SharedDate;
005: import com.calipso.reportgenerator.reportcalculator.SharedInteger;
006: import com.calipso.reportgenerator.reportcalculator.SharedFloat;
007: import com.calipso.reportgenerator.reportcalculator.expression.Expression;
008: import com.calipso.reportgenerator.reportdefinitions.ParameterValues;
009: import com.calipso.reportgenerator.reportdefinitions.ParameterValue;
010: import com.calipso.reportgenerator.reportdefinitions.types.ReportDataType;
011: import com.calipso.reportgenerator.common.ReportSpec;
012: import com.calipso.reportgenerator.common.ReportFilter;
013: import com.calipso.common.DateEx;
014:
015: import java.util.*;
016: import java.text.DateFormat;
017: import java.text.SimpleDateFormat;
018:
019: /**
020: * Esta clase se utiliza para construir todos los filtros que se utilizan en la ejecución de un reporte, tanto los
021: * pre-filtros, como los post-filtros, resolver los valores de parámetros.
022: * Se encarga de procesar todos los objetos <code>ReportFilter</code> creados a partir de los <code>FilterDefinition</code>
023: * del <code>ReportDefinition</code> y el <code>ReportSourceDefinition</code> y generar un objeto <code>CubeFilter</code>
024: * que sirve para determinar si un registro del <code>DataSource</code> o de la <code>Matrix</code> del <code>ReportSource<code>
025: * cumplen con todas las condiciones.
026: * El objeto CubeFilter también es capaz de generar una condición para ser utilizada en una consulta OQL.
027: * Provee mecanismos para consolidar los valores de los parámetros ingresados por el usuario y completarlos con los valores
028: * de los parámetros que figuran el las definiciones correspondientes.
029: */
030: public class ReportFilterBuilder {
031:
032: private List reportFilters;
033: private Map paramValues;
034: private int mode;
035: public static final int VARMODE_INDEX = 0;
036: public static final int VARMODE_DATAINDEX = 1;
037: public static final int VARMODE_NAME = 2;
038: public static final int VARMODE_EXTERNALDATA = 3;
039:
040: /**
041: * Constructor que inicializa el ReportFilterBuilder
042: * @param reportFilters
043: * @param paramValues
044: * @param mode
045: */
046: public ReportFilterBuilder(List reportFilters, Map paramValues,
047: int mode) {
048: this .paramValues = paramValues;
049: this .reportFilters = reportFilters;
050: this .mode = mode;
051: }
052:
053: /**
054: * Devuelve un filtro para el cubo
055: * @return
056: */
057: public ExpressionCubeFilter getCubeFilter(ReportSpec reportSpec,
058: ReportDataSourceSpec dataSourceSpec) throws InfoException {
059: Expression exp = null;
060: ExpressionCubeFilter cubeFilter = null;
061: for (Iterator iterator = reportFilters.iterator(); iterator
062: .hasNext();) {
063: ReportFilter reportFilter = (ReportFilter) iterator.next();
064: String dimensionName = reportFilter.getFilterSpec()
065: .getDimensionName();
066: if (dimensionName != null
067: && !dimensionName.equalsIgnoreCase("")) {
068: Expression currentExp = reportFilter.getExpression(
069: paramValues, mode, reportSpec, dataSourceSpec,
070: reportFilter);
071: if (currentExp != null) {
072: if (exp == null) {
073: exp = currentExp;
074: } else {
075: exp = exp.newAnd(currentExp);
076: }
077: }
078: }
079: }
080: if (exp == null) { //!= null) {
081: //exp = new Expression();
082: }
083: cubeFilter = new ExpressionCubeFilter(exp, paramValues);
084: //}
085: return cubeFilter;
086: }
087:
088: /**
089: * Sobrecarga los parameters values default con los valores del diccionario
090: * @param values
091: * @param defaultparameterValues
092: * @return
093: */
094: public static Map mergeParamValues(Map values,
095: Map defaultparameterValues) {
096: Map defParams;
097: if (defaultparameterValues != null) {
098: defParams = defaultparameterValues;
099: } else {
100: defParams = new HashMap();
101: }
102: Map returnValues = new HashMap();
103: Iterator keys;
104: if (values != null) {
105: keys = values.keySet().iterator();
106: while (keys.hasNext()) {
107: String key = (String) keys.next();
108: returnValues.put(key, values.get(key));
109: }
110: }
111: keys = defParams.keySet().iterator();
112: while (keys.hasNext()) {
113: String key = (String) keys.next();
114: returnValues.put(key, defParams.get(key));
115: }
116: return returnValues;
117: }
118:
119: /**
120: * Sobrecarga los parameters values default con los valores del diccionario
121: * @param values
122: * @param defaultParameterValues
123: * @return
124: */
125: public static Map mergeParamValues(Map values,
126: ParameterValues defaultParameterValues) {
127: return mergeParamValues(
128: values,
129: paramValuesDictFromParameterValues(defaultParameterValues));
130: }
131:
132: /**
133: * Retorna un diccionario a partir de un ParameterValues
134: * @param parameterValues
135: * @return
136: */
137: private static Map paramValuesDictFromParameterValues(
138: ParameterValues parameterValues) {
139: Map returnValues = new HashMap();
140: if (parameterValues != null) {
141: for (int i = 0; i < parameterValues
142: .getParameterValueCount(); i++) {
143: ParameterValue parameterValue = parameterValues
144: .getParameterValue(i);
145: String parameterValueKey = parameterValue
146: .getFilterDefinitionName()
147: + parameterValue.getFilterParameter()
148: .toString();
149: returnValues.put(parameterValueKey, parameterValue
150: .getDefaultValue());
151: }
152: }
153: return returnValues;
154: }
155:
156: public static Object getConstatValue(int mode, Object o,
157: ReportDataSourceSpec dataSourceSpec, int dataType) {
158: switch (mode) {
159: case VARMODE_INDEX:
160: return o;
161: case VARMODE_DATAINDEX:
162: return o;
163: case VARMODE_NAME:
164: return o;
165: case VARMODE_EXTERNALDATA:
166: try {
167: return getExternalData(o, dataSourceSpec, dataType);
168: } catch (Exception e) {
169: //Este error se da para objetos "fecha". Puede ser un error de especificación de reporte
170: //Si ocurre, el objeto se guardará en su estado original, para no "romper" el reporte
171: e.printStackTrace();
172: return o;
173: }
174: }
175: return null;
176: }
177:
178: public static Object getExternalData(Object o,
179: ReportDataSourceSpec dataSourceSpec, int dataType)
180: throws InfoException {
181: switch (dataType) {
182: case ReportDataType.DATETIME_TYPE:
183: case ReportDataType.DATE_TYPE:
184: if (o == "" && dataSourceSpec != null) {
185: return new String("' '");
186: } else if (dataSourceSpec != null) {
187: String formatedDate;
188: DateFormat dateFormat;
189: String pattern = dataSourceSpec.getPattern(dataType);
190: if (pattern != null && !pattern.equalsIgnoreCase("")) {
191: dateFormat = new SimpleDateFormat(pattern);
192: } else {
193: dateFormat = new SimpleDateFormat("yyyyMMdd");
194: }
195: if (o instanceof SharedDate) {
196: formatedDate = dateFormat.format(((SharedDate) o)
197: .getDateEx().getDate());
198: } else if (o instanceof Date) {
199: formatedDate = dateFormat.format((Date) o);
200: } else {
201: formatedDate = dateFormat.format(new DateEx(o
202: .toString()).getDate());
203: }
204: return new String("'" + formatedDate + "'");
205: } else {
206: DateFormat dateFormat;
207: if (dataType == ReportDataType.DATETIME_TYPE) {
208: dateFormat = new SimpleDateFormat(
209: "yyyyMMddHHmmssSSS");
210: } else {
211: dateFormat = new SimpleDateFormat("yyyyMMdd");
212: }
213: Date date = (new DateEx(o.toString())).getDate();
214: return dateFormat.format(date);
215: }
216: case ReportDataType.STRING_TYPE:
217: String value = o.toString().replaceAll("'", "''");
218: return new String("'" + value + "'");
219: case ReportDataType.INTEGER_TYPE:
220: return ((SharedInteger) o).getValue();
221: case ReportDataType.FLOAT_TYPE:
222: return ((SharedFloat) o).getValue();
223: case ReportDataType.BOOLEAN_TYPE:
224: return ((Boolean) o).toString();
225: }
226: return null;
227: }
228:
229: }
|