1 13 package com.tonbeller.jpivot.test.olap; 14 15 import java.text.DecimalFormat ; 16 import java.util.ArrayList ; 17 import java.util.List ; 18 import java.util.Locale ; 19 import java.util.Random ; 20 21 import javax.servlet.ServletContext ; 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 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 random = new Random (); 50 private String ID = null; 51 public String getID() { return ID; } 52 public void setID(String 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 [] { "Measures" }, new int[] { 3 }); 61 measures.setMeasure(true); 62 TestDimension region = 63 db.build("Region", new String [] { "All Region", "Region", "City", "Customer" }, new int[] { 1, 5, 8, 200 }); 64 TestDimension time = db.build("Time", new String [] { "Year", "Month" }, new int[] { 5, 12 }); 65 time.setTime(true); 66 TestDimension products = 67 db.build("Products", new String [] { "All Products", "Category", "Product" }, new int[] { 1, 3, 10 }); 68 TestDimension advertising = 69 db.build("Advertising", new String [] { "All Advertising", "Media", "Target", "Type" }, new int[] { 1, 4, 3, 3 }); 70 TestDimension material = 71 db.build("Material", new String [] { "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 list = new ArrayList (); 123 list.add(p); 124 slicer.setPositions(list); 125 } 126 127 public Result getResult() throws OlapException { 128 129 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 locale = super.getLocale(); 137 if (locale == null) 138 locale = Locale.getDefault(); 139 DecimalFormat fmt = (DecimalFormat ) DecimalFormat.getInstance(locale); 140 fmt.applyPattern("#,##0.00"); 141 ArrayList cells = new ArrayList (); 142 for (int i = 0; i < cellCount; i++) { 143 CellImpl c = new CellImpl(); 144 if (false) { 145 c.setValue(new Integer (i)); 146 c.setFormattedValue(Integer.toString(i)); 147 } else { 148 double value = random.nextGaussian() * 100 + 1000; 149 c.setValue(new Double (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 166 public Dimension[] getDimensions() { 167 return dimensions; 168 } 169 170 173 public Member[] getMeasures() { 174 TestHierarchy hier = (TestHierarchy) measures.getHierarchies()[0]; 175 return hier.getRootMembers(); 176 } 177 178 179 public Dimension getDimension(String 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 189 public List findPositions(Member m) { 190 List list = new ArrayList (); 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 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 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 266 public TestAxis getSlicer() { 267 return slicer; 268 } 269 270 274 public void setSlicer(TestAxis slicer) { 275 this.slicer = slicer; 276 } 277 278 public Object getRootDecoree() { 279 return this; 280 } 281 282 285 public void initialize() { 286 logger.debug("init"); 287 } 288 289 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 servletContext) { 301 } 302 303 } 304 | Popular Tags |