001: package com.calipso.reportgenerator.reportcalculator;
002:
003: import com.calipso.reportgenerator.reportdefinitions.types.ParameterValueFilterParameterType;
004: import com.calipso.reportgenerator.common.*;
005:
006: import java.util.Collection;
007: import java.util.Vector;
008: import java.util.Iterator;
009:
010: import com.calipso.reportgenerator.common.InfoException;
011:
012: /**
013: *
014: * User: jbassino
015: * Date: 12-may-2005
016: * Time: 15:44:59
017: * Calipso Software
018: */
019: public abstract class FilterOperation {
020:
021: public static FilterOperation newFrom(ReportResult reportResult,
022: ReportQuery reportQuery, ReportFilterSpec filter)
023: throws InfoException {
024: String type;
025: float total;
026: Float value = null, from = null, to = null;
027: try {
028: type = getParam(reportQuery, filter,
029: ParameterValueFilterParameterType.OPERATION)
030: .toString();
031: total = getTotal(reportResult);
032: if (type.toUpperCase().startsWith("RANGE")) {
033: to = new Float(getParam(reportQuery, filter,
034: ParameterValueFilterParameterType.TO)
035: .toString());
036: from = new Float(getParam(reportQuery, filter,
037: ParameterValueFilterParameterType.FROM)
038: .toString());
039: } else {
040: value = new Float(getParam(reportQuery, filter,
041: ParameterValueFilterParameterType.VALUE)
042: .toString());
043: }
044: } catch (Exception e) {
045: throw new InfoException(e);
046: }
047: if (type.equalsIgnoreCase("GreaterThan")) {
048: return new GreaterThanOperation(value.floatValue());
049: } else if (type.equalsIgnoreCase("LesserThan")) {
050: return new LesserThanOperation(value.floatValue());
051: } else if (type.equalsIgnoreCase("GreaterAverage")) {
052: return new GreaterAverageOperation(value.floatValue(),
053: total);
054: } else if (type.equalsIgnoreCase("LesserAverage")) {
055: return new LesserAverageOperation(value.floatValue(), total);
056: } else if (type.equalsIgnoreCase("Top")) {
057: return new TopOperation(reportResult, reportQuery, filter,
058: value.intValue());
059: } else if (type.equalsIgnoreCase("RangeAverage")) {
060: return new RangeAverageOperation(from.floatValue(), to
061: .floatValue(), total);
062: } else if (type.equalsIgnoreCase("Range")) {
063: return new RangeOperation(from.floatValue(), to
064: .floatValue());
065: }
066: throw new InfoException(LanguageTraslator.traslate("403")
067: + type);
068: }
069:
070: public static Object getParam(ReportQuery reportQuery,
071: ReportFilterSpec filter,
072: ParameterValueFilterParameterType type) {
073: String paramName = filter.getName() + type.toString();
074: int index = filter.getParamNames().indexOf(paramName);
075: return reportQuery.getParamValue(filter.getParamNames().get(
076: index).toString());
077: }
078:
079: private static float getTotal(ReportResult result) {
080: float total;
081: if (result instanceof CubeReportResult) {
082: total = ((SharedFloat) ((Vector) ((CubeReportResult) result)
083: .getDataVector().lastElement()).firstElement())
084: .floatValue();
085: } else {
086: total = ((StaticReportResult) result).getDataTree()
087: .getRoot().getMetrics()[0].floatValue();
088: }
089: return total;
090: }
091:
092: public Collection operate(ReportResult result) {
093: if (result instanceof CubeReportResult) {
094: return doOperate((CubeReportResult) result);
095: } else {
096: return doOperate((StaticReportResult) result);
097: }
098: }
099:
100: private Collection doOperate(StaticReportResult reportResult) {
101: return null;
102: }
103:
104: private Collection doOperate(CubeReportResult reportResult) {
105: Collection result = new Vector();
106: Vector data = reportResult.getDataVector();
107: for (int i = 0; i < data.size() - 1; i++) {
108: if (accept((SharedFloat) ((Vector) data.elementAt(i))
109: .firstElement())) {
110: Iterator it = reportResult.getRowsModel()
111: .getDimensionValueNode().getSubNodesList()
112: .iterator();
113: //DimensionValueNode value = null;
114: for (int j = 0; j != i; j++) {
115: /*value = (DimensionValueNode)*/it.next();
116: }
117: result.add(((DimensionValueNode) it.next()).getValue());
118: }
119: }
120: return result;
121: }
122:
123: public static String getOperationName(ReportSpec spec,
124: ReportFilterSpec filter) {
125: String paramName = filter.getName()
126: + ParameterValueFilterParameterType.OPERATION
127: .toString();
128: String paramValue = spec.getParamValues(false).get(paramName)
129: .toString();
130: if (paramValue.equalsIgnoreCase("GreaterThan")) {
131: return LanguageTraslator.traslate("407");
132: } else if (paramValue.equalsIgnoreCase("LesserThan")) {
133: return LanguageTraslator.traslate("408");
134: } else if (paramValue.equalsIgnoreCase("GreaterAverage")) {
135: return LanguageTraslator.traslate("409");
136: } else if (paramValue.equalsIgnoreCase("LesserAverage")) {
137: return LanguageTraslator.traslate("410");
138: } else if (paramValue.equalsIgnoreCase("Top")) {
139: return LanguageTraslator.traslate("411");
140: } else if (paramValue.equalsIgnoreCase("RangeAverage")) {
141: return LanguageTraslator.traslate("412");
142: } else if (paramValue.equalsIgnoreCase("Range")) {
143: return "";
144: }
145: return "";
146: }
147:
148: protected abstract boolean accept(SharedFloat value);
149:
150: }
|