1 package com.calipso.reportgenerator.reportcalculator; 2 3 import com.calipso.reportgenerator.reportdefinitions.types.ParameterValueFilterParameterType; 4 import com.calipso.reportgenerator.common.*; 5 6 import java.util.Collection ; 7 import java.util.Vector ; 8 import java.util.Iterator ; 9 10 import com.calipso.reportgenerator.common.InfoException; 11 12 19 public abstract class FilterOperation { 20 21 public static FilterOperation newFrom(ReportResult reportResult, ReportQuery reportQuery, ReportFilterSpec filter) throws InfoException{ 22 String type; 23 float total; 24 Float value = null, from = null, to = null; 25 try{ 26 type = getParam(reportQuery, filter, ParameterValueFilterParameterType.OPERATION).toString(); 27 total = getTotal(reportResult); 28 if(type.toUpperCase().startsWith("RANGE")){ 29 to = new Float (getParam(reportQuery, filter, ParameterValueFilterParameterType.TO).toString()); 30 from = new Float (getParam(reportQuery, filter, ParameterValueFilterParameterType.FROM).toString()); 31 }else{ 32 value = new Float (getParam(reportQuery, filter, ParameterValueFilterParameterType.VALUE).toString()); 33 } 34 }catch (Exception e){ 35 throw new InfoException(e); 36 } 37 if(type.equalsIgnoreCase("GreaterThan")){ 38 return new GreaterThanOperation(value.floatValue()); 39 }else if(type.equalsIgnoreCase("LesserThan")){ 40 return new LesserThanOperation(value.floatValue()); 41 }else if(type.equalsIgnoreCase("GreaterAverage")){ 42 return new GreaterAverageOperation(value.floatValue(), total); 43 }else if(type.equalsIgnoreCase("LesserAverage")){ 44 return new LesserAverageOperation(value.floatValue(), total); 45 }else if(type.equalsIgnoreCase("Top")){ 46 return new TopOperation(reportResult, reportQuery, filter, value.intValue()); 47 }else if(type.equalsIgnoreCase("RangeAverage")){ 48 return new RangeAverageOperation(from.floatValue(), to.floatValue(), total); 49 }else if(type.equalsIgnoreCase("Range")){ 50 return new RangeOperation(from.floatValue(), to.floatValue()); 51 } 52 throw new InfoException(LanguageTraslator.traslate("403") + type); 53 } 54 55 public static Object getParam(ReportQuery reportQuery, ReportFilterSpec filter, ParameterValueFilterParameterType type) { 56 String paramName = filter.getName() + type.toString(); 57 int index = filter.getParamNames().indexOf(paramName); 58 return reportQuery.getParamValue(filter.getParamNames().get(index).toString()); 59 } 60 61 private static float getTotal(ReportResult result){ 62 float total; 63 if(result instanceof CubeReportResult){ 64 total = ((SharedFloat)((Vector )((CubeReportResult)result).getDataVector().lastElement()).firstElement()).floatValue(); 65 }else{ 66 total = ((StaticReportResult)result).getDataTree().getRoot().getMetrics()[0].floatValue(); 67 } 68 return total; 69 } 70 71 public Collection operate(ReportResult result) { 72 if(result instanceof CubeReportResult){ 73 return doOperate((CubeReportResult)result); 74 }else{ 75 return doOperate((StaticReportResult)result); 76 } 77 } 78 79 private Collection doOperate(StaticReportResult reportResult) { 80 return null; 81 } 82 83 private Collection doOperate(CubeReportResult reportResult) { 84 Collection result = new Vector (); 85 Vector data = reportResult.getDataVector(); 86 for(int i=0; i < data.size()-1; i++){ 87 if(accept((SharedFloat)((Vector )data.elementAt(i)).firstElement())){ 88 Iterator it = reportResult.getRowsModel().getDimensionValueNode().getSubNodesList().iterator(); 89 for(int j=0; j!=i; j++){ 91 it.next(); 92 } 93 result.add(((DimensionValueNode)it.next()).getValue()); 94 } 95 } 96 return result; 97 } 98 99 public static String getOperationName(ReportSpec spec, ReportFilterSpec filter){ 100 String paramName = filter.getName() + ParameterValueFilterParameterType.OPERATION.toString(); 101 String paramValue = spec.getParamValues(false).get(paramName).toString(); 102 if(paramValue.equalsIgnoreCase("GreaterThan")){ 103 return LanguageTraslator.traslate("407"); 104 }else if(paramValue.equalsIgnoreCase("LesserThan")){ 105 return LanguageTraslator.traslate("408"); 106 }else if(paramValue.equalsIgnoreCase("GreaterAverage")){ 107 return LanguageTraslator.traslate("409"); 108 }else if(paramValue.equalsIgnoreCase("LesserAverage")){ 109 return LanguageTraslator.traslate("410"); 110 }else if(paramValue.equalsIgnoreCase("Top")){ 111 return LanguageTraslator.traslate("411"); 112 }else if(paramValue.equalsIgnoreCase("RangeAverage")){ 113 return LanguageTraslator.traslate("412"); 114 }else if(paramValue.equalsIgnoreCase("Range")){ 115 return ""; 116 } 117 return ""; 118 } 119 120 protected abstract boolean accept(SharedFloat value); 121 122 } 123 | Popular Tags |