KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tonbeller > jpivot > mondrian > MondrianSetParameter


1 /*
2  * ====================================================================
3  * This software is subject to the terms of the Common Public License
4  * Agreement, available at the following URL:
5  * http://www.opensource.org/licenses/cpl.html .
6  * Copyright (C) 2003-2004 TONBELLER AG.
7  * All Rights Reserved.
8  * You must accept the terms of that agreement to use this software.
9  * ====================================================================
10  *
11  *
12  */

13 package com.tonbeller.jpivot.mondrian;
14
15 import java.util.Hashtable JavaDoc;
16 import java.util.Map JavaDoc;
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 /**
31  * set parameter to query
32  */

33 public class MondrianSetParameter extends ExtensionSupport implements SetParameter {
34
35   static Logger logger = Logger.getLogger(MondrianSetParameter.class);
36
37   /**
38    */

39   public MondrianSetParameter() {
40     super.setId(SetParameter.ID);
41   }
42
43   /**
44    * set parameter value to mondrian query
45    * @see SetParameter#setParameter(String, Expression)
46    */

47   public void setParameter(String JavaDoc 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 JavaDoc monParaName = monParam.getName();
55       if (paramName.equals(monParaName)) {
56
57         // found the parameter with the given name in the query
58
switch (pType) {
59         case Category.Numeric:
60           if (expr instanceof DoubleExpr) {
61             double d = ((DoubleExpr) expr).getValue();
62             monParam.setValue(new Double JavaDoc(d));
63           } else if (expr instanceof IntegerExpr) {
64             int ii = ((IntegerExpr) expr).getValue();
65             monParam.setValue(new Double JavaDoc(ii));
66           } else {
67             // wrong parameter type
68
String JavaDoc str = "wrong Numeric parameter type " + paramName + expr.getClass().toString();
69             logger.error(str);
70             throw new java.lang.IllegalArgumentException JavaDoc(str);
71           }
72           break;
73
74         case Category.String:
75           if (expr instanceof StringExpr) {
76             String JavaDoc s = ((StringExpr) expr).getValue();
77             monParam.setValue(s);
78           } else {
79             // wrong parameter type
80
String JavaDoc str = "wrong String parameter type " + paramName + expr.getClass().toString();
81             logger.error(str);
82             throw new java.lang.IllegalArgumentException JavaDoc(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             // wrong parameter type
93
String JavaDoc str = "wrong Member parameter type " + paramName + expr.getClass().toString();
94             logger.error(str);
95             throw new java.lang.IllegalArgumentException JavaDoc(str);
96           }
97           break;
98         default:
99         }
100         model.fireModelChanged();
101         return;
102       }
103     }
104   }
105
106   /**
107    * FIXME - this crashes if the parameter contains an expression like "LastChild" or "DefaultMember".
108    * @return Map containing parameter names (= keys) and strings to display value (= value)
109    * @see com.tonbeller.jpivot.olap.navi.SetParameter#getDisplayValues()
110    */

111   public Map JavaDoc getDisplayValues() {
112     Map JavaDoc map = new Hashtable JavaDoc();
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 JavaDoc monParaName = monParam.getName();
121       Object JavaDoc 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 JavaDoc[] getParameterNames() {
139     MondrianModel model = (MondrianModel) getModel();
140     mondrian.olap.Query monQuery = ((MondrianQueryAdapter)model.getQueryAdapter()).getMonQuery();
141     mondrian.olap.Parameter[] monParams = monQuery.getParameters();
142     String JavaDoc[] names = new String JavaDoc[monParams.length];
143     for (int i = 0; i < monParams.length; i++) {
144       names[i] = monParams[i].getName();
145     }
146     return names;
147   }
148   /**
149    * Returns true if the query has one or more parameters. This does not
150    * evaluate the parameters.
151    *
152    * @return
153    */

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 } // MondrianSetParameter
162
Popular Tags