KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tonbeller > jpivot > test > olap > TestOlapModel


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.test.olap;
14
15 import java.text.DecimalFormat JavaDoc;
16 import java.util.ArrayList JavaDoc;
17 import java.util.List JavaDoc;
18 import java.util.Locale JavaDoc;
19 import java.util.Random JavaDoc;
20
21 import javax.servlet.ServletContext JavaDoc;
22
23 import org.apache.log4j.Logger;
24
25 import com.tonbeller.jpivot.core.ModelSupport;
26 import com.tonbeller.jpivot.olap.model.Axis;
27 import com.tonbeller.jpivot.olap.model.Dimension;
28 import com.tonbeller.jpivot.olap.model.Member;
29 import com.tonbeller.jpivot.olap.model.OlapException;
30 import com.tonbeller.jpivot.olap.model.OlapModel;
31 import com.tonbeller.jpivot.olap.model.Property;
32 import com.tonbeller.jpivot.olap.model.Result;
33 import com.tonbeller.jpivot.olap.model.impl.CellImpl;
34 import com.tonbeller.jpivot.olap.model.impl.PropertyImpl;
35 import com.tonbeller.jpivot.olap.model.impl.ResultImpl;
36 import com.tonbeller.jpivot.olap.navi.DrillExpandMember;
37
38 /**
39  * Created on 02.12.2002
40  *
41  * @author av
42  */

43 public class TestOlapModel extends ModelSupport implements OlapModel {
44
45   TestAxis axes[], slicer;
46   TestDimension[] dimensions;
47   TestDimension measures;
48   private static Logger logger = Logger.getLogger(TestOlapModel.class);
49   Random JavaDoc random = new Random JavaDoc();
50   private String JavaDoc ID = null;
51   public String JavaDoc getID() { return ID; }
52   public void setID(String JavaDoc ID) { this.ID = ID; }
53
54   public TestOlapModel() {
55     reset();
56   }
57
58   void reset() {
59     DimensionBuilder db = new DimensionBuilder();
60     measures = db.build("Measures", new String JavaDoc[] { "Measures" }, new int[] { 3 });
61     measures.setMeasure(true);
62     TestDimension region =
63       db.build("Region", new String JavaDoc[] { "All Region", "Region", "City", "Customer" }, new int[] { 1, 5, 8, 200 });
64     TestDimension time = db.build("Time", new String JavaDoc[] { "Year", "Month" }, new int[] { 5, 12 });
65     time.setTime(true);
66     TestDimension products =
67       db.build("Products", new String JavaDoc[] { "All Products", "Category", "Product" }, new int[] { 1, 3, 10 });
68     TestDimension advertising =
69       db.build("Advertising", new String JavaDoc[] { "All Advertising", "Media", "Target", "Type" }, new int[] { 1, 4, 3, 3 });
70     TestDimension material =
71       db.build("Material", new String JavaDoc[] { "All Materials", "Category", "Brand", "Quality" }, new int[] { 1, 4, 3, 3 });
72
73     dimensions = new TestDimension[6];
74     dimensions[0] = measures;
75     dimensions[1] = region;
76     dimensions[2] = time;
77     dimensions[3] = products;
78     dimensions[4] = advertising;
79     dimensions[5] = material;
80
81     PropertyBuilder pb = new PropertyBuilder();
82     pb.build(products);
83     pb.build(region);
84
85     axes = new TestAxis[2];
86     axes[0] = TestOlapModelUtils.createAxis(new TestDimension[] { measures });
87     axes[1] = TestOlapModelUtils.createAxis(new TestDimension[] { products, time });
88
89     TestMember m = ((TestHierarchy) advertising.getHierarchies()[0]).getRootMembers()[0];
90     m = (TestMember) m.getChildMember().get(0);
91     setSlicer(m);
92
93   }
94
95   public void setAxis(int index, TestAxis axis) {
96     axes[index] = axis;
97   }
98
99   public TestAxis getAxis(int index) {
100     return axes[index];
101   }
102
103   public TestAxis[] getAxes() {
104     return axes;
105   }
106
107   public void setAxes(TestAxis[] axes) {
108     this.axes = axes;
109   }
110
111   public int indexOf(Axis axis) {
112     for (int i = 0; i < axes.length; i++)
113       if (axes[i].equals(axis))
114         return i;
115     return -1;
116   }
117
118   public void setSlicer(TestMember m) {
119     slicer = new TestAxis();
120     TestPosition p = new TestPosition(slicer);
121     p.setMembers(new TestMember[] { m });
122     List JavaDoc list = new ArrayList JavaDoc();
123     list.add(p);
124     slicer.setPositions(list);
125   }
126
127   public Result getResult() throws OlapException {
128
129     // make result reproducable for testing purposes
130
random.setSeed(123427);
131
132     int cellCount = 1;
133     for (int i = 0; i < axes.length; i++)
134       cellCount *= axes[i].getPositions().size();
135
136     Locale JavaDoc locale = super.getLocale();
137     if (locale == null)
138       locale = Locale.getDefault();
139     DecimalFormat JavaDoc fmt = (DecimalFormat JavaDoc) DecimalFormat.getInstance(locale);
140     fmt.applyPattern("#,##0.00");
141     ArrayList JavaDoc cells = new ArrayList JavaDoc();
142     for (int i = 0; i < cellCount; i++) {
143       CellImpl c = new CellImpl();
144       if (false) {
145         c.setValue(new Integer JavaDoc(i));
146         c.setFormattedValue(Integer.toString(i));
147       } else {
148         double value = random.nextGaussian() * 100 + 1000;
149         c.setValue(new Double JavaDoc(value));
150         c.setFormattedValue(fmt.format(value));
151       }
152       addProperties(c, i);
153       cells.add(c);
154     }
155
156     ResultImpl res = new ResultImpl();
157     res.setAxes(axes);
158     res.setCells(cells);
159     res.setSlicer(slicer);
160     return res;
161   }
162
163   /**
164    * @see com.tonbeller.jpivot.olap.model.OlapModel#getDimensions()
165    */

166   public Dimension[] getDimensions() {
167     return dimensions;
168   }
169
170   /**
171    * @see com.tonbeller.jpivot.olap.model.OlapModel#getMeasures()
172    */

173   public Member[] getMeasures() {
174     TestHierarchy hier = (TestHierarchy) measures.getHierarchies()[0];
175     return hier.getRootMembers();
176   }
177
178   /** searches for the named dimension */
179   public Dimension getDimension(String JavaDoc name) {
180     for (int i = 0; i < dimensions.length; i++)
181       if (name.equals(dimensions[i].getLabel()))
182         return dimensions[i];
183     return null;
184   }
185
186   /**
187    * which positions in the result contain member
188    */

189   public List JavaDoc findPositions(Member m) {
190     List JavaDoc list = new ArrayList JavaDoc();
191     for (int i = 0; i < axes.length; i++)
192       list.addAll(TestOlapModelUtils.findPositions(axes[i], m));
193     return list;
194   }
195
196   public static void addProperties(CellImpl c, int i) {
197     Property[] props = new Property[1];
198     PropertyImpl p = new PropertyImpl();
199     props[0] = p;
200
201     switch ((i / 2) % 10) {
202       case 0 :
203         p.setName("arrow");
204         p.setValue("up");
205         break;
206       case 1 :
207         p.setName("arrow");
208         p.setValue("down");
209         break;
210       case 2 :
211         p.setName("arrow");
212         p.setValue("none");
213         break;
214       case 4 :
215         p.setName("style");
216         p.setValue("red");
217         break;
218       case 5 :
219         p.setName("style");
220         p.setValue("yellow");
221         break;
222       case 6 :
223         p.setName("style");
224         p.setValue("green");
225         break;
226       default :
227         break;
228     }
229     p.setLabel(p.getName());
230
231     c.setProperties(props);
232   }
233
234   /**
235    * expands top member on rows - gives more interesting result for junit tests.
236    */

237   public void expand0() {
238     expandRow(0);
239   }
240
241   public void expandRow(int positionIndex) {
242     TestPosition p = (TestPosition) axes[1].getPositions().get(positionIndex);
243     Member m = p.getMembers()[0];
244     DrillExpandMember de = (DrillExpandMember) getExtension(DrillExpandMember.ID);
245     de.expand(m);
246   }
247
248   /**
249    * collapses top member on rows (for unit tests)
250    */

251   public void collapse0() {
252     collapseRow(0);
253   }
254
255   public void collapseRow(int positionIndex) {
256     TestPosition p = (TestPosition) axes[1].getPositions().get(positionIndex);
257     Member m = p.getMembers()[0];
258     DrillExpandMember de = (DrillExpandMember) getExtension(DrillExpandMember.ID);
259     de.collapse(m);
260   }
261
262   /**
263    * Returns the slicer.
264    * @return TestAxis
265    */

266   public TestAxis getSlicer() {
267     return slicer;
268   }
269
270   /**
271    * Sets the slicer.
272    * @param slicer The slicer to set
273    */

274   public void setSlicer(TestAxis slicer) {
275     this.slicer = slicer;
276   }
277
278   public Object JavaDoc getRootDecoree() {
279     return this;
280   }
281
282   /**
283    * does nothing
284    */

285   public void initialize() {
286     logger.debug("init");
287   }
288
289   /**
290    * does nothing
291    */

292   public void destroy() {
293     logger.debug("destroy");
294     super.destroy();
295   }
296
297   public TestDimension getMeasuresDim() {
298     return measures;
299   }
300   public void setServletContext(ServletContext JavaDoc servletContext) {
301   }
302  
303 }
304
Popular Tags