1 13 package com.tonbeller.jpivot.mondrian; 14 15 import java.util.Hashtable ; 16 import java.util.Map ; 17 18 import mondrian.olap.Category; 19 import mondrian.olap.type.TypeUtil; 20 21 import org.apache.log4j.Logger; 22 23 import com.tonbeller.jpivot.core.ExtensionSupport; 24 import com.tonbeller.jpivot.olap.model.DoubleExpr; 25 import com.tonbeller.jpivot.olap.model.Expression; 26 import com.tonbeller.jpivot.olap.model.IntegerExpr; 27 import com.tonbeller.jpivot.olap.model.StringExpr; 28 import com.tonbeller.jpivot.olap.navi.SetParameter; 29 30 33 public class MondrianSetParameter extends ExtensionSupport implements SetParameter { 34 35 static Logger logger = Logger.getLogger(MondrianSetParameter.class); 36 37 39 public MondrianSetParameter() { 40 super.setId(SetParameter.ID); 41 } 42 43 47 public void setParameter(String paramName, Expression expr) { 48 MondrianModel model = (MondrianModel) getModel(); 49 mondrian.olap.Query monQuery = ((MondrianQueryAdapter) model.getQueryAdapter()).getMonQuery(); 50 mondrian.olap.Parameter[] monParams = monQuery.getParameters(); 51 for (int i = 0; i < monParams.length; i++) { 52 mondrian.olap.Parameter monParam = monParams[i]; 53 int pType = TypeUtil.typeToCategory(monParam.getType()); 54 String monParaName = monParam.getName(); 55 if (paramName.equals(monParaName)) { 56 57 switch (pType) { 59 case Category.Numeric: 60 if (expr instanceof DoubleExpr) { 61 double d = ((DoubleExpr) expr).getValue(); 62 monParam.setValue(new Double (d)); 63 } else if (expr instanceof IntegerExpr) { 64 int ii = ((IntegerExpr) expr).getValue(); 65 monParam.setValue(new Double (ii)); 66 } else { 67 String str = "wrong Numeric parameter type " + paramName + expr.getClass().toString(); 69 logger.error(str); 70 throw new java.lang.IllegalArgumentException (str); 71 } 72 break; 73 74 case Category.String: 75 if (expr instanceof StringExpr) { 76 String s = ((StringExpr) expr).getValue(); 77 monParam.setValue(s); 78 } else { 79 String str = "wrong String parameter type " + paramName + expr.getClass().toString(); 81 logger.error(str); 82 throw new java.lang.IllegalArgumentException (str); 83 } 84 85 break; 86 87 case Category.Member: 88 if (expr instanceof MondrianMember) { 89 MondrianMember m = (MondrianMember) expr; 90 monParam.setValue(m.getMonMember()); 91 } else { 92 String str = "wrong Member parameter type " + paramName + expr.getClass().toString(); 94 logger.error(str); 95 throw new java.lang.IllegalArgumentException (str); 96 } 97 break; 98 default: 99 } 100 model.fireModelChanged(); 101 return; 102 } 103 } 104 } 105 106 111 public Map getDisplayValues() { 112 Map map = new Hashtable (); 113 MondrianModel model = (MondrianModel) getModel(); 114 mondrian.olap.Query monQuery = 115 ((MondrianQueryAdapter) model.getQueryAdapter()).getMonQuery(); 116 mondrian.olap.Parameter[] monParams = monQuery.getParameters(); 117 for (int i = 0; i < monParams.length; i++) { 118 mondrian.olap.Parameter monParam = monParams[i]; 119 int pType = TypeUtil.typeToCategory(monParam.getType()); 120 String monParaName = monParam.getName(); 121 Object value = monParam.getValue(); 122 switch (pType) { 123 case Category.Numeric: 124 map.put(monParaName, value.toString()); 125 break; 126 case Category.String: 127 map.put(monParaName, value); 128 break; 129 case Category.Member: 130 map.put(monParaName, ((mondrian.olap.Member) value).getCaption()); 131 break; 132 } 133 } 134 135 return map; 136 } 137 138 public String [] getParameterNames() { 139 MondrianModel model = (MondrianModel) getModel(); 140 mondrian.olap.Query monQuery = ((MondrianQueryAdapter)model.getQueryAdapter()).getMonQuery(); 141 mondrian.olap.Parameter[] monParams = monQuery.getParameters(); 142 String [] names = new String [monParams.length]; 143 for (int i = 0; i < monParams.length; i++) { 144 names[i] = monParams[i].getName(); 145 } 146 return names; 147 } 148 154 public boolean getHasDisplayValues() { 155 MondrianModel model = (MondrianModel) getModel(); 156 mondrian.olap.Query monQuery = ((MondrianQueryAdapter)model.getQueryAdapter()).getMonQuery(); 157 mondrian.olap.Parameter[] monParams = monQuery.getParameters(); 158 return (monParams.length > 0); 159 } 160 161 } | Popular Tags |