1 20 package org.openi.xmla; 21 22 import com.tonbeller.jpivot.olap.model.Cell; 23 import com.tonbeller.jpivot.olap.model.Member; 24 import com.tonbeller.jpivot.olap.model.OlapException; 25 import com.tonbeller.jpivot.olap.model.OlapModel; 26 import com.tonbeller.jpivot.olap.model.Position; 27 import com.tonbeller.jpivot.olap.navi.MemberTree; 28 29 import org.apache.log4j.Logger; 30 31 import org.jfree.data.category.DefaultCategoryDataset; 32 import org.jfree.data.time.Day; 33 import org.jfree.data.time.Month; 34 import org.jfree.data.time.Quarter; 35 import org.jfree.data.time.RegularTimePeriod; 36 import org.jfree.data.time.TimeSeries; 37 import org.jfree.data.time.TimeSeriesCollection; 38 import org.jfree.data.xy.XYDataset; 39 40 import java.text.DecimalFormat ; 41 import java.text.DecimalFormatSymbols ; 42 import java.text.NumberFormat ; 43 import java.text.ParseException ; 44 import java.text.SimpleDateFormat ; 45 46 import java.util.Calendar ; 47 import java.util.Date ; 48 import java.util.HashMap ; 49 import java.util.List ; 50 import java.util.Locale ; 51 52 53 56 public class DatasetAdapter { 57 private static Logger logger = Logger.getLogger(DatasetAdapter.class); 58 private Locale locale; 59 private NumberFormat numberFormatter; 60 61 64 public DatasetAdapter(Locale locale) { 65 this.locale = locale; 66 } 67 68 72 public DefaultCategoryDataset buildCategoryDataset(OlapModel olapModel) 73 throws OlapException { 74 long start = System.currentTimeMillis(); 75 76 DefaultCategoryDataset dataset = null; 77 int dimCount = olapModel.getResult().getAxes().length; 78 79 switch (dimCount) { 80 case 1: 81 logger.info("1-dim data"); 82 dataset = build1dimDataset(olapModel); 83 84 break; 85 86 case 2: 87 logger.info("2-dim data"); 88 dataset = build2dimDataset(olapModel); 89 90 break; 91 92 default: 93 logger.error("less than 1 or more than 2 dimensions"); 94 throw new IllegalArgumentException ( 95 "ChartRenderer requires a 1 or 2 dimensional result"); 96 } 97 98 logger.debug("built datset in: " + 99 (System.currentTimeMillis() - start) + "ms"); 100 101 return dataset; 102 } 103 104 109 private DefaultCategoryDataset build1dimDataset(OlapModel olapModel) 110 throws OlapException { 111 DefaultCategoryDataset dataset = new DefaultCategoryDataset(); 112 113 List columnPositions = olapModel.getResult().getAxes()[0].getPositions(); 115 int colCount = columnPositions.size(); 116 117 List cells = olapModel.getResult().getCells(); 119 120 String series = "Series"; 121 122 for (int i = 0; i < colCount; i++) { 124 Member[] colMembers = ((Position) columnPositions.get(i)).getMembers(); 125 126 StringBuffer key = new StringBuffer (); 127 128 for (int j = 0; j < colMembers.length; j++) { 130 key.append(colMembers[j].getLabel() + "."); 132 } 133 134 dataset.addValue(getNumberValue((Cell) cells.get(i)), series, 135 key.toString()); 136 } 137 138 return dataset; 139 } 140 141 147 private DefaultCategoryDataset build2dimDataset(OlapModel olapModel) 148 throws OlapException { 149 DefaultCategoryDataset dataset = new DefaultCategoryDataset(); 150 151 List columnPositions = olapModel.getResult().getAxes()[0].getPositions(); int colCount = columnPositions.size(); 154 155 List rowPositions = olapModel.getResult().getAxes()[1].getPositions(); int rowCount = rowPositions.size(); 158 List cells = olapModel.getResult().getCells(); 159 160 MemberTree myTree = ((MemberTree) olapModel.getExtension(MemberTree.ID)); 162 163 174 for (int i = 0; i < colCount; i++) { 175 Position p = (Position) columnPositions.get(i); 176 Member[] colMembers = p.getMembers(); 177 178 String label = buildName(myTree, colMembers); 180 181 for (int k = 0; k < rowCount; k++) { 183 Position rp = (Position) rowPositions.get(k); 184 Member[] rowMembers = rp.getMembers(); 185 186 String key = buildName(myTree, rowMembers); 188 189 Cell cell = (Cell) cells.get((k * colCount) + i); 190 191 dataset.addValue(getNumberValue(cell), label.toString(), 192 key.toString()); 193 } 194 } 195 196 return dataset; 197 } 198 199 205 private Number getNumberValue(Cell cell) { 206 Object value = cell.getValue(); 209 210 if ((cell.getFormattedValue() != null) && 212 (cell.getFormattedValue() != "")) { 213 String fmtValue = cell.getFormattedValue(); 214 fmtValue = fmtValue.trim(); 215 216 if(numberFormatter == null){ 217 DecimalFormatSymbols dfs = new DecimalFormatSymbols (this.locale); 218 if(fmtValue.endsWith(String.valueOf(dfs.getPercent()))){ 219 numberFormatter = NumberFormat.getPercentInstance(this.locale); 220 } 223 else if(fmtValue.startsWith(dfs.getCurrencySymbol())){ 224 numberFormatter = NumberFormat.getCurrencyInstance(this.locale); 225 numberFormatter.setMaximumFractionDigits(0); 226 numberFormatter.setMinimumFractionDigits(0); 227 } 228 229 230 } 231 232 } 233 234 NumberFormat formatter = NumberFormat.getInstance(this.locale); 236 237 Number number = null; 239 240 try { 241 number = formatter.parse(String.valueOf(value)); 243 } catch (Exception e) { 244 number = null; 246 } 247 248 return number; 249 } 250 251 258 private String buildName(MemberTree myTree, Member[] members) { 259 String retValue = new String (); 260 HashMap levelMap = new HashMap (); 261 HashMap hierarchyMap = new HashMap (); 262 263 for (int j = members.length - 1; j >= 0; j--) { 264 Member member = members[j]; 265 266 while (member != null) { 267 if (!levelMap.containsValue(member.getLevel())) { 269 levelMap.put(member.getLevel().toString(), member.getLevel()); 270 271 if (member.getRootDistance() == 0) { 272 if (!hierarchyMap.containsValue( 274 member.getLevel().getHierarchy()) || 275 (myTree.getRootMembers( 276 member.getLevel().getHierarchy()).length > 1)) { 277 hierarchyMap.put(member.getLevel().getHierarchy() 278 .toString(), 279 member.getLevel().getHierarchy()); 280 retValue = member.getLabel() + "." + retValue; 281 } 282 } else { 283 hierarchyMap.put(member.getLevel().getHierarchy() 284 .toString(), 285 member.getLevel().getHierarchy()); 286 retValue = member.getLabel() + "." + retValue; 287 } 288 } 289 290 member = myTree.getParent(member); 291 } 292 } 293 294 return retValue; 295 } 296 297 303 public XYDataset buildXYDataset(OlapModel olapModel) 304 throws OlapException { 305 long start = System.currentTimeMillis(); 306 307 TimeSeriesCollection dataset = new TimeSeriesCollection(); 308 309 List columnPositions = olapModel.getResult().getAxes()[0].getPositions(); int colCount = columnPositions.size(); 312 313 List rowPositions = olapModel.getResult().getAxes()[1].getPositions(); int rowCount = rowPositions.size(); 316 List cells = olapModel.getResult().getCells(); 317 318 MemberTree myTree = ((MemberTree) olapModel.getExtension(MemberTree.ID)); 320 321 for (int i = 0; i < colCount; i++) { 323 Position p = (Position) columnPositions.get(i); 324 Member[] colMembers = p.getMembers(); 325 326 String label = buildName(myTree, colMembers); 328 TimeSeries series = createTimeSeries(label, 329 parseRootLevel(rowPositions)); 330 331 for (int k = 0; k < rowCount; k++) { 332 Position rp = (Position) rowPositions.get(k); 333 Member[] rowMembers = rp.getMembers(); 334 Cell cell = (Cell) cells.get((k * colCount) + i); 335 336 try { 337 RegularTimePeriod current = null; 342 343 if (series.getTimePeriodClass() == Quarter.class) { 344 current = createQuarter(myTree, rowMembers); 345 } else if (series.getTimePeriodClass() == Month.class) { 346 current = createMonth(myTree, rowMembers); 347 } else if (series.getTimePeriodClass() == Day.class) { 348 current = createDay(myTree, rowMembers); 349 } 350 351 series.add(current, getNumberValue(cell)); 352 } catch (ParseException e) { 353 } 355 } 356 357 dataset.addSeries(series); 358 } 359 360 dataset.setDomainIsPointsInTime(true); 361 logger.debug("created XY Dataset in: " + 362 (System.currentTimeMillis() - start) + " ms"); 363 364 return dataset; 365 } 366 367 private TimeSeries createTimeSeries(String label, String rootLevel) { 368 logger.debug("creating timeseries for: " + rootLevel); 369 370 TimeSeries series = null; 371 372 if ("quarter".equalsIgnoreCase(rootLevel)) { 373 series = new TimeSeries(label, Quarter.class); 374 } else if ("month".equalsIgnoreCase(rootLevel)) { 375 series = new TimeSeries(label, Month.class); 376 } else if ("day".equalsIgnoreCase(rootLevel)) { 377 series = new TimeSeries(label, Day.class); 378 } 379 380 return series; 381 } 382 383 private RegularTimePeriod createQuarter(MemberTree memberTree, 384 Member[] members) { 385 String year = memberTree.getParent(members[0]).getLabel(); 386 int quarter = 1; 387 String memberLabel = members[0].getLabel(); 388 389 if ("Q1".equalsIgnoreCase(memberLabel)) { 391 quarter = 1; 392 } else if ("Q2".equalsIgnoreCase(memberLabel)) { 393 quarter = 2; 394 } else if ("Q3".equalsIgnoreCase(memberLabel)) { 395 quarter = 3; 396 } else if ("Q4".equalsIgnoreCase(memberLabel)) { 397 quarter = 4; 398 } 399 400 return new Quarter(quarter, Integer.parseInt(year)); 401 } 402 403 406 private Month createMonth(MemberTree memberTree, Member[] members) 407 throws ParseException { 408 String year = memberTree.getParent(members[0]).getLabel(); 409 int month = 1; 410 String memberLabel = members[0].getLabel(); 411 month = parseMonth(memberLabel); 412 413 return new Month(month, Integer.parseInt(year)); 414 } 415 416 417 private RegularTimePeriod createDay(MemberTree memberTree, Member[] members) { 418 int day = 1; 419 String dayLabel = members[0].getLabel(); 420 day = Integer.parseInt(dayLabel); 421 422 int month = 1; 423 String monthLabel = memberTree.getParent(members[0]).getLabel(); 424 month = parseMonth(monthLabel); 425 426 int year = 1900; 427 String yearLabel = memberTree.getParent(members[0]).getLabel(); 428 429 return new Day(day, month, year); 430 } 431 432 private int parseMonth(String memberLabel) { 433 Integer month = null; 434 435 try { 437 month = new Integer (memberLabel); 438 } catch (NumberFormatException e) { 439 } 441 442 if (month == null) { 444 try { 445 SimpleDateFormat formatter = new SimpleDateFormat ("MMM"); 446 Date date = formatter.parse(memberLabel); 447 month = new Integer (date.getMonth() + 1); 448 } catch (ParseException e) { 449 } 451 } 452 453 return month.intValue(); 454 } 455 456 private String parseRootLevel(List rowPositions) { 457 String rootLevel = null; 458 459 try { 460 rootLevel = ((Position) rowPositions.get(0)).getMembers()[0].getLevel() 461 .getLabel(); 462 } catch (Exception e) { 463 logger.debug(e); 464 } 465 466 return rootLevel; 467 } 468 469 public NumberFormat getNumberFormatter() { 470 return numberFormatter; 471 } 472 } 473 | Popular Tags |