1 13 package com.tonbeller.jpivot.xmla; 14 15 import java.io.Reader ; 16 import java.io.StringReader ; 17 import java.util.ArrayList ; 18 import java.util.Collection ; 19 import java.util.HashMap ; 20 import java.util.Iterator ; 21 import java.util.List ; 22 import java.util.Locale ; 23 import java.util.Map ; 24 25 import java_cup.runtime.Symbol; 26 27 import javax.servlet.ServletContext ; 28 import javax.servlet.http.HttpSessionBindingEvent ; 29 30 import org.apache.log4j.Logger; 31 32 import com.tonbeller.jpivot.core.Extension; 33 import com.tonbeller.jpivot.core.ModelChangeEvent; 34 import com.tonbeller.jpivot.core.ModelChangeListener; 35 import com.tonbeller.jpivot.olap.mdxparse.Exp; 36 import com.tonbeller.jpivot.olap.mdxparse.Formula; 37 import com.tonbeller.jpivot.olap.mdxparse.FunCall; 38 import com.tonbeller.jpivot.olap.mdxparse.Lexer; 39 import com.tonbeller.jpivot.olap.mdxparse.Literal; 40 import com.tonbeller.jpivot.olap.mdxparse.ParsedQuery; 41 import com.tonbeller.jpivot.olap.mdxparse.parser; 42 import com.tonbeller.jpivot.olap.model.Dimension; 43 import com.tonbeller.jpivot.olap.model.Hierarchy; 44 import com.tonbeller.jpivot.olap.model.Level; 45 import com.tonbeller.jpivot.olap.model.Member; 46 import com.tonbeller.jpivot.olap.model.OlapDiscoverer; 47 import com.tonbeller.jpivot.olap.model.OlapException; 48 import com.tonbeller.jpivot.olap.model.OlapItem; 49 import com.tonbeller.jpivot.olap.model.OlapModel; 50 import com.tonbeller.jpivot.olap.model.Result; 51 import com.tonbeller.jpivot.olap.navi.SortRank; 52 import com.tonbeller.jpivot.olap.query.ExpBean; 53 import com.tonbeller.jpivot.olap.query.MdxOlapModel; 54 import com.tonbeller.jpivot.olap.query.PositionNodeBean; 55 import com.tonbeller.jpivot.olap.query.Quax; 56 import com.tonbeller.jpivot.olap.query.QuaxBean; 57 import com.tonbeller.jpivot.olap.query.QueryAdapter; 58 import com.tonbeller.jpivot.util.TreeNode; 59 62 public class XMLA_Model extends MdxOlapModel implements OlapModel, QueryAdapter.QueryAdapterHolder { 63 64 static Logger logger = Logger.getLogger(XMLA_Model.class); 65 66 private String ID = null; 67 68 private String uri = null; private String user = null; 70 private String password = null; 71 private String catalog = null; private String dataSource = null; 74 private String mdxQuery; 75 private String currentMdx; 76 77 private ParsedQuery pQuery = null; 78 79 private XMLA_Result result = null; 80 81 private List aDimensions = new ArrayList (); 82 private List aHierarchies = new ArrayList (); 83 private List aLevels = new ArrayList (); 84 private List aMeasures = new ArrayList (); 85 private List aMembers = new ArrayList (); 86 87 private XMLA_QueryAdapter queryAdapter = null; 88 89 private String cube = null; private boolean isNewCube = false; 91 private XMLA_SOAP soap = null; 92 93 private boolean isInitialized = false; 94 private Locale loc = null; 95 96 private Map calcMeasurePropMap; 98 99 101 public XMLA_Model() { 102 103 this.mdxQuery = null; 106 this.currentMdx = null; 107 108 addModelChangeListener(new ModelChangeListener() { 109 public void modelChanged(ModelChangeEvent e) { 110 result = null; } 112 113 public void structureChanged(ModelChangeEvent e) { 114 result = null; } 116 }); 117 } 118 119 123 public void initialize() throws OlapException { 124 logger.info(this); 125 126 boolean logInfo = logger.isInfoEnabled(); 127 128 if (catalog == null) 129 throw new OlapException("XMLA Model requires catalog specification"); 130 loc = getLocale(); if (loc != null) { 134 if (logInfo) { 135 String msg = "Locale language=" + loc.getLanguage() + " Country=" + loc.getCountry(); 136 logger.info(msg); 137 } 138 } 139 140 if (dataSource != null && dataSource.length() > 0) 141 soap = new XMLA_SOAP(uri, user, password, dataSource); 142 else 143 soap = new XMLA_SOAP(uri, user, password); 145 146 if (logInfo) { 147 List dsprops = soap.discoverDSProps(); 148 for (Iterator iter = dsprops.iterator(); iter.hasNext();) { 149 OlapItem oi = (OlapItem) iter.next(); 150 Map pmap = oi.getProperties(); 151 logger.info("Property: " + oi.getName()); 152 for (Iterator iterator = pmap.keySet().iterator(); iterator.hasNext();) { 153 Object keyo = iterator.next(); 154 logger.info(keyo + "=" + pmap.get(keyo)); 155 } 156 } 157 } 158 parse(mdxQuery); 159 queryAdapter = new XMLA_QueryAdapter(this); 160 XMLA_SortRank sortExt = (XMLA_SortRank) getExtension(SortRank.ID); 161 if (sortExt != null) 162 sortExt.reset(); 163 164 isInitialized = true; 165 166 Map extMap = getExtensions(); 168 Collection extensions = extMap.values(); 169 for (Iterator iter = extensions.iterator(); iter.hasNext();) { 170 Extension extension = (Extension) iter.next(); 171 extension.modelInitialized(); 172 } 173 initCubeMetaData(); 174 isNewCube = false; 175 calcMeasurePropMap = new HashMap (); 176 } 177 178 182 public void setMdxQuery(String mdxQuery) { 183 this.mdxQuery = mdxQuery; 184 this.currentMdx = mdxQuery.replace('\r', ' '); 185 if (logger.isInfoEnabled()) 186 logger.info("setMdxQuery:" + mdxQuery); 187 } 188 189 194 public synchronized Result getResult() throws OlapException { 195 196 if (result != null) 197 return result; 198 199 if (!isInitialized) { 200 logger.fatal("Model not initialized"); 201 throw new OlapException("Model not initialized"); 202 } 203 204 if (isNewCube) 206 initCubeMetaData(); 207 isNewCube = false; 208 209 queryAdapter.onExecute(); 210 211 long lBefore = System.currentTimeMillis(); 212 logger.debug(currentMdx); 213 XMLA_Result res = new XMLA_Result(this, soap, catalog, currentMdx); 214 long lTime = System.currentTimeMillis() - lBefore; 215 logger.debug("Time for executeQuery(ms)=" + lTime); 216 result = res; 218 219 queryAdapter.afterExecute(result); 220 221 return result; 222 } 223 224 229 XMLA_Result currentResult() { 230 return result; 231 } 232 233 public synchronized Result getDrillResult() throws OlapException { 235 236 if (result != null) 237 return result; 238 239 if (!isInitialized) { 240 logger.fatal("Model not initialized"); 241 throw new OlapException("Model not initialized"); 242 } 243 244 if (isNewCube) 246 initCubeMetaData(); 247 isNewCube = false; 248 249 queryAdapter.onExecuteDrill(); 251 252 long lBefore = System.currentTimeMillis(); 253 logger.debug(currentMdx); 254 XMLA_Result res = new XMLA_Result(this, soap, catalog, currentMdx, true); 255 long lTime = System.currentTimeMillis() - lBefore; 256 logger.debug("Time for executeQuery(ms)=" + lTime); 257 result = res; 259 queryAdapter.afterExecute(result); 260 return result; 261 } 262 263 268 public String getCurrentMdx() { 269 if (queryAdapter != null) 270 return queryAdapter.getCurrentMdx(); 271 else 272 return this.mdxQuery; 273 } 274 275 281 public void setUserMdx(String mdxQuery) throws OlapException { 282 if (this.currentMdx.equals(mdxQuery)) 283 return; 284 285 parse(mdxQuery); 286 287 this.mdxQuery = mdxQuery; 288 result = null; 289 queryAdapter = new XMLA_QueryAdapter(this); 290 XMLA_SortRank sortExt = (XMLA_SortRank) getExtension(SortRank.ID); 291 if (sortExt != null) 292 sortExt.reset(); 293 294 if (logger.isInfoEnabled()) 295 logger.info("setUserMdx =" + mdxQuery); 296 this.currentMdx = mdxQuery.replace('\r', ' '); 297 } 298 299 303 String getMdxQuery() { 304 return mdxQuery; 305 } 306 307 311 void setCurrentMdx(String currentMdx) { 312 this.currentMdx = currentMdx.replaceAll("\r", ""); 313 } 314 315 319 public Dimension[] getDimensions() { 320 return (Dimension[]) aDimensions.toArray(new Dimension[0]); 321 } 322 323 326 public Member[] getMeasures() { 327 return (Member[]) aMeasures.toArray(new Member[0]); 328 } 329 330 334 public void destroy() { 335 super.destroy(); 336 } 337 338 public Object getRootDecoree() { 339 return this; 340 } 341 342 347 XMLA_Dimension lookupDimByUName(String uName) { 348 349 for (Iterator iter = aDimensions.iterator(); iter.hasNext();) { 350 XMLA_Dimension dim = (XMLA_Dimension) iter.next(); 351 if (dim.getUniqueName().equals(uName)) 352 return dim; 353 } 354 355 return null; 356 } 357 358 363 XMLA_Hierarchy lookupHierByUName(String uName) { 364 365 for (Iterator iter = aHierarchies.iterator(); iter.hasNext();) { 366 XMLA_Hierarchy hier = (XMLA_Hierarchy) iter.next(); 367 if (hier.getUniqueName().equals(uName)) 368 return hier; 369 } 370 371 return null; 372 } 373 374 379 XMLA_Level lookupLevelByUName(String uName) { 380 381 for (Iterator iter = aLevels.iterator(); iter.hasNext();) { 382 XMLA_Level lev = (XMLA_Level) iter.next(); 383 if (lev.getUniqueName().equals(uName)) 384 return lev; 385 } 386 387 return null; 388 } 389 390 395 public Member lookupMemberByUName(String uName) { 396 397 for (Iterator iter = aMembers.iterator(); iter.hasNext();) { 398 XMLA_Member mem = (XMLA_Member) iter.next(); 399 if (mem.getUniqueName().equals(uName)) 400 return mem; 401 } 402 403 return null; 404 } 405 406 410 422 423 425 void addMember(XMLA_Member mem) { 426 aMembers.add(mem); 427 } 428 429 431 public void removeMember(XMLA_Member mem) { 432 if (aMembers.contains(mem)) 433 aMembers.remove(mem); 434 } 435 436 440 public Object getBookmarkState(int levelOfDetail) { 441 try { 442 XMLA_Memento memento = new XMLA_Memento(); 443 memento.setVersion(XMLA_Memento.CURRENT_VERSION); 444 memento.setUri(uri); 445 memento.setDataSource(dataSource); 446 memento.setCatalog(catalog); 447 memento.setUser(user); 448 memento.setPassword(password); 449 memento.setMdxQuery(currentMdx); 453 boolean useQuax = queryAdapter.isUseQuax(); 454 memento.setUseQuax(useQuax); 455 if (useQuax) { 456 XMLA_Quax[] quaxes = (XMLA_Quax[]) queryAdapter.getQuaxes(); 457 458 QuaxBean[] quaxBeans = new QuaxBean[quaxes.length]; 459 for (int i = 0; i < quaxes.length; i++) { 460 quaxBeans[i] = new QuaxBean(); 461 beanFromQuax(quaxBeans[i], quaxes[i]); 462 } 464 memento.setQuaxes(quaxBeans); 466 } 467 memento.setAxesSwapped(queryAdapter.isSwapAxes()); 469 XMLA_SortRank sortExt = (XMLA_SortRank) getExtension(SortRank.ID); 471 if (sortExt != null) 472 storeSort(sortExt, memento); 473 if (calcMeasurePropMap != null) 475 memento.setCalcMeasurePropMap(calcMeasurePropMap); 476 return memento; 477 } catch (OlapException e) { 478 logger.error(null, e); 479 throw new RuntimeException (e); 480 } 481 } 482 483 487 public void setBookmarkState(Object state) { 488 489 XMLA_Memento memento = (XMLA_Memento) state; 490 491 mdxQuery = memento.getMdxQuery(); 492 try { 493 if (isInitialized) { 494 496 parse(mdxQuery); 497 queryAdapter = new XMLA_QueryAdapter(this); 498 XMLA_SortRank sortExt = (XMLA_SortRank) getExtension(SortRank.ID); 499 if (sortExt != null) 500 sortExt.reset(); 501 isNewCube = false; 502 } else { 503 504 uri = memento.getUri(); 505 dataSource = memento.getDataSource(); 506 catalog = memento.getCatalog(); 507 mdxQuery = memento.getMdxQuery(); 508 509 513 initialize(); 514 } 515 516 result = (XMLA_Result) getResult(); 518 } catch (OlapException e) { 519 String err = e.getMessage(); 521 logger.fatal(err); 522 throw new RuntimeException (err); 523 } 524 boolean useQuax = memento.isUseQuax(); 525 queryAdapter.setUseQuax(useQuax); 526 527 if (useQuax) { 528 QuaxBean[] quaxBeans = memento.getQuaxes(); 530 Quax quaxes[] = queryAdapter.getQuaxes(); 531 try { 533 quaxesFromBeans(quaxes, quaxBeans); 534 } catch (OlapException e) { 535 throw new IllegalArgumentException (e.toString()); 536 } 537 } 538 XMLA_SortRank sortExt = (XMLA_SortRank) getExtension(SortRank.ID); 540 restoreSort(sortExt, memento); 541 542 queryAdapter.setSwapAxes(memento.isAxesSwapped()); 544 545 calcMeasurePropMap = memento.getCalcMeasurePropMap(); 547 if (calcMeasurePropMap == null) 548 calcMeasurePropMap = new HashMap (); 549 result = null; 550 } 551 552 557 private TreeNode createPosTreeFromBean(PositionNodeBean rootBean) throws OlapException { 558 ExpBean expBean = rootBean.getReference(); Exp exp; 560 if (expBean == null) 561 exp = null; 562 else 563 exp = (Exp) createExpFromBean(expBean); 564 TreeNode node = new TreeNode(exp); 565 PositionNodeBean[] beanChildren = rootBean.getChildren(); 566 for (int i = 0; i < beanChildren.length; i++) { 567 TreeNode childNode = createPosTreeFromBean(beanChildren[i]); 568 node.addChildNode(childNode); 569 } 570 return node; 571 } 572 573 579 protected Object createExpFromBean(ExpBean expBean) throws OlapException { 580 if (expBean.getType() == ExpBean.TYPE_MEMBER) { 581 XMLA_Member member = (XMLA_Member) lookupMemberByUName(expBean.getName()); 582 if (member == null) { 583 retrieveMember(expBean.getName()); 584 } 585 member = (XMLA_Member) lookupMemberByUName(expBean.getName()); 586 if (member == null) { 587 throw new OlapException("could not find member " + expBean.getName()); 589 } 590 return member; 591 } else if (expBean.getType() == ExpBean.TYPE_FUNCALL) { 592 String name = expBean.getName(); 594 ExpBean[] argBeans = expBean.getArgs(); 595 Exp[] args = new Exp[argBeans.length]; 596 for (int i = 0; i < argBeans.length; i++) { 597 args[i] = (Exp) createExpFromBean(argBeans[i]); 598 } 599 600 int synType = XMLA_Util.funCallSyntax(name); 601 FunCall f = new FunCall(name, args, synType); 602 return f; 603 } else if (expBean.getType() == ExpBean.TYPE_LEVEL) { 604 XMLA_Level lev = lookupLevelByUName(expBean.getName()); 606 if (lev == null) { 607 throw new OlapException("could not find Level " + expBean.getName()); 609 } 610 return lev; 611 } else if (expBean.getType() == ExpBean.TYPE_HIER) { 612 XMLA_Hierarchy hier = lookupHierByUName(expBean.getName()); 614 if (hier == null) { 615 throw new OlapException("could not find Hierarchy " + expBean.getName()); 617 } 618 return hier; 619 } else if (expBean.getType() == ExpBean.TYPE_DIM) { 620 XMLA_Dimension dim = lookupDimByUName(expBean.getName()); 622 if (dim == null) { 623 throw new OlapException("could not find Dimension " + expBean.getName()); 625 } 626 return dim; 627 } else if (expBean.getType() == ExpBean.TYPE_STRING_LITERAL) { 628 String str = (String ) expBean.getLiteralValue(); 630 return Literal.createString(str); 631 } else if (expBean.getType() == ExpBean.TYPE_INTEGER_LITERAL) { 632 Integer iii = (Integer ) expBean.getLiteralValue(); 634 return Literal.create(iii); 635 } else if (expBean.getType() == ExpBean.TYPE_DOUBLE_LITERAL) { 636 Double ddd = (Double ) expBean.getLiteralValue(); 638 return Literal.create(ddd); 639 } else 640 throw new OlapException("Invalid ExpBean Type " + expBean.getType()); 641 } 642 643 protected ExpBean createBeanFromExp(Object exp) throws OlapException { 644 ExpBean bean = new ExpBean(); 645 if (exp instanceof Member) { 646 XMLA_Member m = (XMLA_Member) exp; 647 bean.setType(ExpBean.TYPE_MEMBER); 648 bean.setName(m.getUniqueName()); 649 bean.setArgs(new ExpBean[0]); 650 } else if (exp instanceof FunCall) { 651 FunCall f = (FunCall) exp; 652 bean.setType(ExpBean.TYPE_FUNCALL); 653 bean.setName(f.getFunction()); 654 ExpBean[] args = new ExpBean[f.getArgs().length]; 655 for (int i = 0; i < args.length; i++) { 656 args[i] = createBeanFromExp(f.getArgs()[i]); 657 } 658 bean.setArgs(args); 659 } else if (exp instanceof Level) { 660 XMLA_Level lev = (XMLA_Level) exp; 661 bean.setType(ExpBean.TYPE_LEVEL); 662 bean.setName(lev.getUniqueName()); 663 bean.setArgs(new ExpBean[0]); 664 } else if (exp instanceof Hierarchy) { 665 XMLA_Hierarchy hier = (XMLA_Hierarchy) exp; 666 bean.setType(ExpBean.TYPE_HIER); 667 bean.setName(hier.getUniqueName()); 668 bean.setArgs(new ExpBean[0]); 669 } else if (exp instanceof Dimension) { 670 XMLA_Dimension dim = (XMLA_Dimension) exp; 671 bean.setType(ExpBean.TYPE_DIM); 672 bean.setName(dim.getUniqueName()); 673 bean.setArgs(new ExpBean[0]); 674 } else if (exp instanceof Literal) { 675 Literal lit = (Literal) exp; 676 Object val = lit.getValueObject(); 677 if (lit.type == Literal.TYPE_NUMERIC) { 678 if (val instanceof Integer ) 679 bean.setType(ExpBean.TYPE_INTEGER_LITERAL); 680 else 681 bean.setType(ExpBean.TYPE_DOUBLE_LITERAL); 682 } else { 683 bean.setType(ExpBean.TYPE_STRING_LITERAL); 684 } 685 bean.setLiteralValue(val); 686 bean.setArgs(new ExpBean[0]); 687 } else { 688 logger.fatal("cannot create ExpBean type =" + exp.getClass().toString()); 689 } 690 return bean; 691 } 692 693 697 private void parse(String mdxQuery) throws OlapException { 698 699 parser parser_obj; 701 Reader reader = new StringReader (mdxQuery); 702 parser_obj = new parser(new Lexer(reader)); 703 704 Symbol parse_tree = null; 705 pQuery = null; 706 try { 707 parse_tree = parser_obj.parse(); 708 } catch (Exception e) { 709 throw new OlapException(e); 710 } 711 pQuery = (ParsedQuery) parse_tree.value; 712 pQuery.afterParse(); 713 714 String newCube = pQuery.getCube(); 715 if (cube == null || !cube.equals(newCube)) { 716 isNewCube = true; 717 cube = newCube; 718 } 719 } 720 721 724 private void initCubeMetaData() throws OlapException { 725 726 735 736 List dims = soap.discoverDim(catalog, pQuery.getCube()); 738 evaluateDimensions(dims); 739 740 List hiers = soap.discoverHier(catalog, pQuery.getCube(), null); 742 evaluateHiers(hiers); 743 744 HashMap hDim = new HashMap (); 747 for (Iterator iter = aDimensions.iterator(); iter.hasNext();) { 748 XMLA_Dimension dim = (XMLA_Dimension) iter.next(); 749 hDim.put(dim.getUniqueName(), dim); 750 } 751 for (Iterator iter = aHierarchies.iterator(); iter.hasNext();) { 752 XMLA_Hierarchy hier = (XMLA_Hierarchy) iter.next(); 753 XMLA_Dimension dim = (XMLA_Dimension) hDim.get(hier.getDimUniqueName()); 754 if (dim != null) { 755 dim.addHier(hier); 756 hier.setDimension(dim); 757 } 758 } 759 760 List levels = soap.discoverLev(catalog, pQuery.getCube(), null, null); 762 evaluateLevels(levels); 763 764 HashMap hHier = new HashMap (); 767 for (Iterator iter = aHierarchies.iterator(); iter.hasNext();) { 768 XMLA_Hierarchy hier = (XMLA_Hierarchy) iter.next(); 769 hHier.put(hier.getUniqueName(), hier); 770 } 771 for (Iterator iter = aLevels.iterator(); iter.hasNext();) { 772 XMLA_Level lev = (XMLA_Level) iter.next(); 773 XMLA_Hierarchy hier = (XMLA_Hierarchy) hHier.get(lev.getHierUniqueName()); 774 if (hier != null) { 775 hier.addLevel(lev); 776 lev.setHierarchy(hier); 777 } 778 } 779 780 for (Iterator iter = aHierarchies.iterator(); iter.hasNext();) { 782 XMLA_Hierarchy hier = (XMLA_Hierarchy) iter.next(); 783 adjustLevels(hier); 784 } 785 786 List propList = null; 790 try { 791 propList = soap.discoverProp(getCatalog(), getCube(), null, null, null); 792 } catch (OlapException e) { 793 logger.error("?", e); 795 } 796 797 if (propList != null && propList.size() > 0) { 798 PropLoop: for (Iterator iterator = propList.iterator(); iterator.hasNext();) { 799 OlapItem poi = (OlapItem) iterator.next(); 800 String name = poi.getName(); 801 String propType = poi.getProperty("PROPERTY_TYPE"); 804 if (!"1".equals(propType)) 807 continue PropLoop; String levUName = poi.getProperty("LEVEL_UNIQUE_NAME"); 809 String dimUName = poi.getProperty("DIMENSION_UNIQUE_NAME"); 810 String caption = poi.getCaption(); 811 if (caption == null) 812 caption = name; 813 XMLA_Dimension dim = this.lookupDimByUName(dimUName); 814 if (this.isSAP() || this.isMondrian()) { 815 XMLA_MemberProp prop = new XMLA_MemberProp(name, caption, dim); 817 dim.addProp(prop); 818 819 } else { 820 if (levUName != null && levUName.length() > 0) { 822 XMLA_Level level = lookupLevelByUName(levUName); 823 XMLA_MemberProp prop = new XMLA_MemberProp(name, caption, level); 824 level.addProp(prop); 825 } 826 } 827 828 } 829 } 830 831 } 832 833 837 private void adjustLevels(XMLA_Hierarchy hier) { 838 XMLA_Level[] levels = (XMLA_Level[]) hier.getLevels(); 839 XMLA_Level[] orderedLevels = new XMLA_Level[levels.length]; 840 for (int i = 0; i < levels.length; i++) { 841 int num = levels[i].getDepth(); 842 orderedLevels[num] = levels[i]; 843 } 844 for (int i = 0; i < levels.length; i++) { 845 int num = levels[i].getDepth(); 846 if (num > 0) { 847 levels[i].setParentLevel(orderedLevels[num - 1]); 848 } else { 849 levels[i].setParentLevel(null); 850 } 851 852 if (num < levels.length - 1) { 853 levels[i].setChildLevel(orderedLevels[num + 1]); 854 } else { 855 levels[i].setChildLevel(null); 856 } 857 } 858 859 } 860 861 865 private void evaluateDimensions(List dims) { 866 867 for (Iterator iter = dims.iterator(); iter.hasNext();) { 868 OlapItem dimit = (OlapItem) iter.next(); 869 870 XMLA_Dimension dim = new XMLA_Dimension(); 871 dim.setName(dimit.getName()); 872 dim.setCaption(dimit.getCaption()); 873 dim.setUniqueName(dimit.getUniqueName()); 874 875 String str = dimit.getProperty("DIMENSION_ORDINAL"); 876 if (str != null) 877 dim.setOrdinal(Integer.parseInt(str)); 878 879 str = dimit.getProperty("DIMENSION_TYPE"); 880 if (str != null) 881 dim.setType(Integer.parseInt(str)); 882 883 str = dimit.getProperty("DIMENSION_CARDINALITY"); 884 if (str != null) 885 dim.setCardinality(Integer.parseInt(str)); 886 887 str = dimit.getProperty("DEFAULT_HIERARCHY"); 888 if (str != null) 889 dim.setDefaultHier(str); 890 891 str = dimit.getProperty("IS_VIRTUAL"); 892 if (str != null) 893 dim.setVirtual(str.equals("true")); 894 895 str = dimit.getProperty("IS_READWRITE"); 896 if (str != null) 897 dim.setReadWrite(str.equals("true")); 898 899 str = dimit.getProperty("DIMENSION_UNIQUE_SETTINGS"); 900 if (str != null) 901 dim.setUniqueSettings(Integer.parseInt(str)); 902 903 str = dimit.getProperty("DIMENSION_IS_VISIBLE"); 904 if (str != null) 905 dim.setVisible(str.equals("true")); 906 907 aDimensions.add(dim); 908 } 909 910 } 911 912 916 private void evaluateHiers(List hiers) { 917 918 for (Iterator iter = hiers.iterator(); iter.hasNext();) { 919 OlapItem hierit = (OlapItem) iter.next(); 920 921 XMLA_Hierarchy hier = new XMLA_Hierarchy(this); 922 hier.setCaption(hierit.getCaption()); 924 hier.setUniqueName(hierit.getUniqueName()); 925 926 String str = hierit.getProperty("HIERARCHY_CAPTION"); 927 if (str != null) 928 hier.setCaption(str); 929 930 str = hierit.getProperty("DIMENSION_UNIQUE_NAME"); 931 if (str != null) 932 hier.setDimUniqueName(str); 933 934 str = hierit.getProperty("DIMENSION_TYPE"); 935 if (str != null) 936 hier.setDimType(Integer.parseInt(str)); 937 938 str = hierit.getProperty("HIERARCHY_CARDINALITY"); 939 if (str != null) 940 hier.setCardinality(Integer.parseInt(str)); 941 942 str = hierit.getProperty("DEFAULT_MEMBER"); 943 if (str != null) 944 hier.setDefaultMember(str); 945 946 str = hierit.getProperty("ALL_MEMBER"); 947 if (str != null) 948 hier.setAllMember(str); 949 950 str = hierit.getProperty("STRUCTURE"); 951 if (str != null) 952 hier.setStructure(Integer.parseInt(str)); 953 954 str = hierit.getProperty("IS_VIRTUAL"); 955 if (str != null) 956 hier.setVirtual(str.equals("true")); 957 958 str = hierit.getProperty("IS_READWRITE"); 959 if (str != null) 960 hier.setReadWrite(str.equals("true")); 961 962 str = hierit.getProperty("DIMENSION_UNIQUE_SETTINGS"); 963 if (str != null) 964 hier.setDimUniqueSettings(Integer.parseInt(str)); 965 966 str = hierit.getProperty("DIMENSION_IS_VISIBLE"); 967 if (str != null) 968 hier.setDimVisible(str.equals("true")); 969 970 str = hierit.getProperty("HIERARCHY_ORDINAL"); 971 if (str != null) 972 hier.setOrdinal(Integer.parseInt(str)); 973 974 str = hierit.getProperty("DIMENSION_IS_SHARED"); 975 if (str != null) 976 hier.setDimShared(str.equals("true")); 977 978 aHierarchies.add(hier); 979 } 980 } 981 982 986 private void evaluateLevels(List levels) { 987 988 for (Iterator iter = levels.iterator(); iter.hasNext();) { 989 OlapItem levit = (OlapItem) iter.next(); 990 XMLA_Level lev = new XMLA_Level(this); 991 lev.setName(levit.getName()); 992 lev.setCaption(levit.getCaption()); 993 lev.setUniqueName(levit.getUniqueName()); 994 995 String str = levit.getProperty("DIMENSION_UNIQUE_NAME"); 996 if (str != null) 997 lev.setDimUniqueName(str); 998 999 str = levit.getProperty("HIERARCHY_UNIQUE_NAME"); 1000 if (str != null) 1001 lev.setHierUniqueName(str); 1002 1003 str = levit.getProperty("LEVEL_NUMBER"); 1004 if (str != null) 1005 lev.setNumber(Integer.parseInt(str)); 1006 1007 str = levit.getProperty("LEVEL_CARDINALITY"); 1008 if (str != null) 1009 lev.setCardinality(Integer.parseInt(str)); 1010 1011 str = levit.getProperty("LEVEL_TYPE"); 1012 if (str != null) 1013 lev.setType(Integer.parseInt(str)); 1014 1015 str = levit.getProperty("CUSTOM_ROLLUP_SETTINGS"); 1016 if (str != null) 1017 lev.setCustomRollupSettings(Integer.parseInt(str)); 1018 1019 str = levit.getProperty("LEVEL_UNIQUE_SETTINGS"); 1020 if (str != null) 1021 lev.setUniqueSettings(Integer.parseInt(str)); 1022 1023 str = levit.getProperty("LEVEL_IS_VISIBLE"); 1024 if (str != null) 1025 lev.setVisible(str.equals("true")); 1026 1027 str = levit.getProperty("LEVEL_ORDERING_PROPERTY"); 1028 if (str != null) 1029 lev.setOrderingProperty(str); 1030 1031 str = levit.getProperty("LEVEL_DBTYPE"); 1032 if (str != null) 1033 lev.setDbType(Integer.parseInt(str)); 1034 1035 str = levit.getProperty("LEVEL_NAME_SQL_COLUMN_NAME"); 1036 if (str != null) 1037 lev.setNameSqlColumnName(str); 1038 1039 str = levit.getProperty("LEVEL_KEY_SQL_COLUMN_NAME"); 1040 if (str != null) 1041 lev.setKeySqlColumnName(str); 1042 1043 str = levit.getProperty("LEVEL_UNIQUE_NAME_SQL_COLUMN_NAME"); 1044 if (str != null) 1045 lev.setUniqueNameSqlColumnName(str); 1046 1047 aLevels.add(lev); 1048 } 1049 1050 } 1051 1052 1057 void completeLevel(XMLA_Level level) throws OlapException { 1058 1059 List mems = soap.discoverMem(catalog, cube, null, level.getHierUniqueName(), level 1060 .getUniqueName()); 1061 ArrayList aNewMembers = new ArrayList (); 1063 ArrayList aAllMembers = new ArrayList (); 1064 evaluateMembers(mems, aNewMembers, aAllMembers); 1065 aMembers.addAll(aNewMembers); 1066 level.setMembers(aAllMembers); 1067 } 1068 1069 1073 void retrieveMemberChildren(XMLA_Member member) throws OlapException { 1074 1076 List mems = soap.discoverMemTree(catalog, cube, member.getUniqueName(), 1); 1078 1079 ArrayList aNewMembers = new ArrayList (); 1081 ArrayList aAllMembers = new ArrayList (); 1082 evaluateMembers(mems, aNewMembers, aAllMembers); 1084 1085 aMembers.addAll(aNewMembers); 1086 setMemberChildren(member, aAllMembers); 1088 1089 } 1090 1091 1095 void retrieveMemberParent(XMLA_Member member) throws OlapException { 1096 1097 member.setParentOk(true); 1098 if (((XMLA_Level) member.getLevel()).getDepth() == 0 1099 || ((XMLA_Level) member.getLevel()).getParentLevel() == null) { 1100 member.setParent(null); 1101 return; 1102 } 1103 1104 if (member.isComplete()) { 1105 String pUname = member.getParentUniqueName(); 1107 if (pUname == null || pUname.length() == 0) { 1108 member.setParent(null); 1109 return; 1110 } 1111 } 1112 1113 List mems = soap.discoverMemTree(catalog, cube, member.getUniqueName(), 4); 1116 1117 for (Iterator iter = mems.iterator(); iter.hasNext();) { 1119 OlapItem oli = (OlapItem) iter.next(); 1120 if (oli.getUniqueName().equals(member.getUniqueName())) { 1121 member.setParent(null); 1123 return; 1124 } 1125 } 1126 1127 if (mems.size() == 0) { 1128 member.setParent(null); 1129 return; 1130 } 1131 1132 ArrayList aNewMembers = new ArrayList (); 1134 ArrayList aAllMembers = new ArrayList (); 1135 evaluateMembers(mems, aNewMembers, aAllMembers); 1137 aMembers.addAll(aNewMembers); 1138 1139 XMLA_Member pmem = (XMLA_Member) aAllMembers.get(0); 1141 member.setParent(pmem); 1142 1143 } 1144 1145 1149 void completeMember(XMLA_Member member) throws OlapException { 1150 List mems = soap.discoverMemTree(catalog, cube, member.getUniqueName(), 8); 1152 ArrayList aNewMembers = new ArrayList (); 1154 ArrayList aAllMembers = new ArrayList (); 1155 evaluateMembers(mems, aNewMembers, aAllMembers); 1156 1157 1185 } 1186 1187 1191 1192 public void retrieveMember(String uniqueName) throws OlapException { 1193 1194 List mems = soap.discoverMemTree(catalog, cube, uniqueName, 8); 1196 1197 ArrayList aNewMembers = new ArrayList (); 1199 ArrayList aAllMembers = new ArrayList (); 1200 evaluateMembers(mems, aNewMembers, aAllMembers); 1201 aMembers.addAll(aNewMembers); 1202 XMLA_Member member = (XMLA_Member) this.lookupMemberByUName(uniqueName); 1203 if (member == null) { throw new OlapException("could not find member " + uniqueName); } 1204 } 1205 1206 1211 private void setMemberChildren(XMLA_Member member, ArrayList aAllMembers) { 1212 ArrayList aChildren = new ArrayList (); 1213 1214 for (Iterator iter = aAllMembers.iterator(); iter.hasNext();) { 1215 XMLA_Member mem = (XMLA_Member) iter.next(); 1216 if (member.getUniqueName().equals(mem.getParentUniqueName())) 1217 aChildren.add(mem); } 1219 1220 member.setChildren(aChildren); 1221 member.setChildrenOk(true); 1222 1223 for (Iterator iter = aChildren.iterator(); iter.hasNext();) { 1224 XMLA_Member child = (XMLA_Member) iter.next(); 1225 child.setParent(member); 1226 child.setParentOk(true); 1227 } 1228 } 1229 1230 1235 1258 1263 private void evaluateMembers(List mems, List aNewMembers, List aAllMembers) { 1264 1265 MLoop: for (Iterator iter = mems.iterator(); iter.hasNext();) { 1266 OlapItem oi = (OlapItem) iter.next(); 1267 1268 String uName = oi.getUniqueName(); 1269 XMLA_Member mem = (XMLA_Member) this.lookupMemberByUName(uName); 1270 if (mem != null) { 1271 aAllMembers.add(mem); } else { 1273 String caption = oi.getCaption(); 1275 String levUName = oi.getProperty("LEVEL_UNIQUE_NAME"); 1276 XMLA_Level lev = this.lookupLevelByUName(levUName); 1279 boolean isCalc = isMemberInFormulas(uName); 1280 mem = new XMLA_Member(this, uName, caption, lev, isCalc); 1281 aNewMembers.add(mem); 1282 aAllMembers.add(mem); 1283 } 1284 if (!mem.isComplete()) { 1285 long ordinal = 0; 1287 String sOrdinal = oi.getProperty("MEMBER_ORDINAL"); 1288 if (sOrdinal != null) 1289 ordinal = Long.parseLong(sOrdinal); 1290 String name = oi.getName(); 1292 int type = 0; 1293 String str = oi.getProperty("MEMBER_TYPE"); 1294 if (str != null) 1295 type = Integer.parseInt(str); 1296 long childrenCard = 0; 1297 String sCard = oi.getProperty("CHILDREN_CARDINALITY"); 1298 if (sCard != null) 1299 childrenCard = Long.parseLong(sCard); 1300 long parentLevel = 0; 1302 String sLev = oi.getProperty("PARENT_LEVEL"); 1303 if (sLev != null) 1304 parentLevel = Long.parseLong(sLev); 1305 String parentUniqueName = oi.getProperty("PARENT_UNIQUE_NAME"); 1307 String key = oi.getProperty("MEMBER_KEY"); 1308 boolean isPlaceHolderMember = ("true".equals(oi.getProperty("IS_PLACEHOLDERMEMBER"))); 1309 boolean isDataMember = ("true".equals(oi.getProperty("IS_DATAMEMBER"))); 1310 mem.complete(name, type, ordinal, parentUniqueName, childrenCard, parentLevel, 1311 isDataMember, isPlaceHolderMember, key); 1312 } 1313 1332 } } 1334 1335 1340 public boolean isMemberInFormulas(String uName) { 1341 if (pQuery == null) 1342 return false; 1343 Formula[] formulas = pQuery.getFormulas(); 1344 for (int i = 0; i < formulas.length; i++) { 1345 if (formulas[i].isMember() && uName.equals(formulas[i].getUniqeName())) 1346 return true; 1347 } 1348 return false; 1349 } 1350 1351 1355 public String getCatalog() { 1356 return catalog; 1357 } 1358 1359 1363 public String getUri() { 1364 return uri; 1365 } 1366 1367 1371 public void setCatalog(String catalog) { 1372 this.catalog = catalog; 1373 } 1374 1375 1379 public String getDataSource() { 1380 return dataSource; 1381 } 1382 1383 1387 public void setDataSource(String dataSource) { 1388 this.dataSource = dataSource; 1389 } 1390 1391 1395 public void setUri(String uri) { 1396 this.uri = uri; 1397 } 1398 1399 1403 public ParsedQuery getPQuery() { 1404 return pQuery; 1405 } 1406 1407 1411 public QueryAdapter getQueryAdapter() { 1412 return queryAdapter; 1413 } 1414 1415 1418 public String getID() { 1419 return ID; 1420 } 1421 1422 1425 public void setID(String string) { 1426 ID = string; 1427 } 1428 1429 1432 public String getUser() { 1433 return user; 1434 } 1435 1436 1439 public void setUser(String string) { 1440 user = string; 1441 } 1442 1443 1446 public String getPassword() { 1447 return password; 1448 } 1449 1450 1453 public void setPassword(String string) { 1454 password = string; 1455 } 1456 1457 public boolean isSAP() { 1458 return (soap.getProvider() == OlapDiscoverer.PROVIDER_SAP); 1459 } 1460 1461 public boolean isMicrosoft() { 1462 return (soap.getProvider() == OlapDiscoverer.PROVIDER_MICROSOFT); 1463 } 1464 1465 public boolean isMondrian() { 1466 return (soap.getProvider() == OlapDiscoverer.PROVIDER_MONDRIAN); 1467 } 1468 1469 1472 public String getCube() { 1473 if (pQuery == null) 1474 return null; 1475 else 1476 return pQuery.getCube(); 1477 } 1478 1479 1482 public Map getCalcMeasurePropMap() { 1483 return calcMeasurePropMap; 1484 } 1485 1486 public void setServletContext(ServletContext servletContext) { 1487 } 1489 1490} 1492 1493 | Popular Tags |