1 13 package com.tonbeller.jpivot.xmla; 14 15 import java.util.ArrayList ; 16 import java.util.List ; 17 18 import org.apache.log4j.Logger; 19 20 import com.tonbeller.jpivot.olap.mdxparse.CompoundId; 21 import com.tonbeller.jpivot.olap.mdxparse.Exp; 22 import com.tonbeller.jpivot.olap.mdxparse.ExpVisitor; 23 import com.tonbeller.jpivot.olap.model.Dimension; 24 import com.tonbeller.jpivot.olap.model.Hierarchy; 25 import com.tonbeller.jpivot.olap.model.Level; 26 import com.tonbeller.jpivot.olap.model.Member; 27 import com.tonbeller.jpivot.olap.model.OlapDiscoverer; 28 import com.tonbeller.jpivot.olap.model.OlapException; 29 import com.tonbeller.jpivot.olap.model.Property; 30 import com.tonbeller.jpivot.olap.model.Visitor; 31 import com.tonbeller.jpivot.olap.query.MDXMember; 32 import com.tonbeller.jpivot.util.StringUtil; 33 34 37 public class XMLA_Member implements Member, MDXMember, Exp { 38 39 static Logger logger = Logger.getLogger(XMLA_Member.class); 40 41 private XMLA_Model model; 42 43 private long ordinal; 44 private String name; 45 private int type; 46 private String caption; 47 private long childrenCardinality = -1; 48 private long parentLevel; 49 private String uniqueName; 50 private String parentUniqueName = null; 51 private String key; 52 private boolean isPlaceHolderMember; 53 private boolean isDataMember; 54 private long displayInfo = -1; 55 private String dimUName = null; 56 58 private XMLA_Level level; 59 private XMLA_Member parent = null; 60 private ArrayList aChildren = null; 61 62 private List properties = new ArrayList (); 64 65 private boolean isCalculated; 67 private boolean complete = false; private boolean propsOk = false; 69 private boolean childrenOk = false; 70 private boolean parentOk = false; 71 72 81 public XMLA_Member( 82 XMLA_Model model, 83 String uName, 84 String caption, 85 XMLA_Level lev, 86 boolean isCalculated) { 87 88 if (!(model.lookupMemberByUName(uName) == null)) { 89 logger.fatal("cannot create member doubly " + uName); 91 throw new IllegalArgumentException ("cannot create member doubly " + uName); 92 } 93 94 this.model = model; 95 this.uniqueName = uName; 96 this.caption = caption; 97 this.level = lev; 98 this.isCalculated = isCalculated; 99 100 logger.debug("<init>: uName - " + uName + ", level - " + lev + ", isCalculated - " + isCalculated); 101 102 model.addMember(this); 103 104 if (model.isMicrosoft() || isCalculated) { 105 parentUniqueName = StringUtil.parentFromUName(uName); 106 if (parentUniqueName == null) { 107 parent = null; 108 parentOk = true; 109 logger.debug("parentUniqueName from uName: " + uName + " == null"); 110 } else { 111 parent = (XMLA_Member) model.lookupMemberByUName(parentUniqueName); 112 logger.debug("lookupMemberByUName(" + parentUniqueName + "): " + parent); 113 if (parent != null) 114 parentOk = true; 115 } 116 } 117 118 if (level == null && isCalculated) { 123 String dimUname = StringUtil.dimFromUName(uniqueName); 125 XMLA_Dimension dim = model.lookupDimByUName(dimUname); 126 logger.debug("looked up dimension name: " + uniqueName + " = " + dim); 127 if (dim != null) { 128 if (dim.isMeasure()) { 129 Hierarchy[] hiers = dim.getHierarchies(); 131 Level[] levs = hiers[0].getLevels(); 132 level = (XMLA_Level) levs[0]; 133 logger.debug("isMeasure: " + level); 134 } else { 135 XMLA_Member pm = null; 139 if (model.isMicrosoft() && parentUniqueName != null) { 140 pm = (XMLA_Member) model.lookupMemberByUName(parentUniqueName); 141 if (pm != null) 142 level = ((XMLA_Level) pm.getLevel()).getChildLevel(); 143 logger.debug("normal dimension: " + level); 144 } 145 if (level == null) { 146 Hierarchy hier = null; 149 if (pm != null) { 150 hier = pm.getHierarchy(); 151 logger.debug("hierarchy from member: " + hier); 152 } else { 153 String hierUname = dim.getDefaultHier(); 154 hier = model.lookupHierByUName(hierUname); 155 logger.debug("hierarchy from DefaultHier: " + hier); 156 } 157 if (hier != null) { 158 logger.debug("trying default hierarchy: " + hier); 159 Level[] levs = hier.getLevels(); 161 for (int i = 0; i < levs.length; i++) { 162 if (((XMLA_Level) levs[i]).getDepth() == 0) { 163 level = (XMLA_Level) levs[i]; 164 break; 165 } 166 } } 168 } 169 } } } 173 if (level == null) 176 throw new IllegalArgumentException ("Member " + uName + " Level=null"); 177 178 if (level.getChildLevel() == null || isCalculated) { 179 childrenCardinality = 0; 180 childrenOk = true; 181 } 182 } 183 184 189 public boolean isEqual(Member otherM) { 190 XMLA_Member other = (XMLA_Member) otherM; 191 return this.uniqueName.equals(other.uniqueName); 192 } 193 194 199 public boolean isChildOf(Member otherM) throws OlapException { 200 XMLA_Member other = (XMLA_Member) otherM; 201 XMLA_Hierarchy thisHier = (XMLA_Hierarchy) level.getHierarchy(); 203 XMLA_Hierarchy otherHier = (XMLA_Hierarchy) other.getLevel().getHierarchy(); 204 if (!thisHier.isEqual(otherHier)) 205 return false; 206 long otherLevelNumber = ((XMLA_Level) other.getLevel()).getDepth(); 208 if (!(level.getDepth() > otherLevelNumber)) 209 return false; 210 211 XMLA_Member m = this; 213 while (m.level.getDepth() > otherLevelNumber) { 214 m = (XMLA_Member) m.getParent(); 215 } 216 217 if (m.isEqual(other)) 218 return true; 219 220 return false; 221 } 222 223 226 public void accept(Visitor visitor) { 227 visitor.visitMember(this); 228 } 229 230 public Object getRootDecoree() { 231 return this; 232 } 233 234 238 public String getLabel() { 239 return caption; 240 } 241 242 246 public String getCaption() { 247 return caption; 248 } 249 250 254 public long getChildrenCardinality() { 255 return childrenCardinality; 256 } 257 258 262 public boolean isDataMember() { 263 return isDataMember; 264 } 265 266 270 public boolean isPlaceHolderMember() { 271 return isPlaceHolderMember; 272 } 273 274 278 public String getKey() { 279 return key; 280 } 281 282 286 public String getName() { 287 return name; 288 } 289 290 294 public long getOrdinal() { 295 return ordinal; 296 } 297 298 302 public long getParentLevel() { 303 return parentLevel; 304 } 305 306 310 public int getType() { 311 return type; 312 } 313 314 318 public String getUniqueName() { 319 return uniqueName; 320 } 321 322 334 public void complete( 335 String name, 336 int type, 337 long ordinal, 338 String parentUniqueName, 339 long childrenCardinality, 340 long parentLevel, 341 boolean isDataMember, 342 boolean isPlaceHolderMember, 343 String key) { 344 this.childrenCardinality = childrenCardinality; 345 this.isDataMember = isDataMember; 346 this.isPlaceHolderMember = isPlaceHolderMember; 347 this.key = key; 348 this.name = name; 349 this.ordinal = ordinal; 350 this.parentLevel = parentLevel; 351 this.type = type; 352 this.parentUniqueName = parentUniqueName; 353 complete = true; 354 } 355 356 360 public String getParentUniqueName() { 361 return parentUniqueName; 362 } 363 364 368 public Level getLevel() { 369 return level; 370 } 371 372 375 public void setLevel(XMLA_Level level) { 376 this.level = level; 377 } 378 379 382 public Property[] getProperties() { 383 384 400 if (isCalculated || properties.size() == 0) 401 return new Property[0]; 403 return (Property[]) properties.toArray(new Property[0]); 404 } 405 406 409 public Property getProperty(String name) { 410 411 428 429 if (isCalculated || properties.size() == 0) 430 return null; 431 432 for (int i = 0; i < properties.size(); i++) { 433 Property prop = (Property) properties.get(i); 434 if (name.equals(prop.getName())) 435 return prop; 436 } 437 438 return null; } 440 441 444 public int getRootDistance() { 445 if (level == null && isCalculated) 446 return 0; 447 return level.getDepth(); 448 } 449 450 454 public int getDepth() { 455 if (level == null && isCalculated) 456 return 0; 457 return this.level.getDepth(); } 459 460 464 public XMLA_Member[] getChildren() throws OlapException { 465 if (childrenOk) { 466 if (childrenCardinality == 0) 467 return new XMLA_Member[0]; 468 else 469 return (XMLA_Member[]) aChildren.toArray(new XMLA_Member[0]); 470 } 471 472 model.retrieveMemberChildren(this); 475 return (XMLA_Member[]) aChildren.toArray(new XMLA_Member[0]); 476 } 477 478 482 public void setChildren(ArrayList aChildren) { 483 this.aChildren = aChildren; 484 if (aChildren == null) 485 childrenCardinality = 0; 486 else 487 childrenCardinality = aChildren.size(); 488 } 489 490 494 public boolean isComplete() { 495 return complete; 496 } 497 498 502 public Member getParent() throws OlapException { 503 504 if (parentOk) 505 return parent; 506 507 529 530 if (model.isMicrosoft() || isCalculated) { 531 parentUniqueName = StringUtil.parentFromUName(uniqueName); 532 if (parentUniqueName == null) { 533 parent = null; 534 parentOk = true; 535 return parent; 536 } else { 537 parent = (XMLA_Member) model.lookupMemberByUName(parentUniqueName); 538 if (parent != null) { 539 parentOk = true; 540 return parent; 541 } 542 } 543 } 544 545 model.retrieveMemberParent(this); 547 return parent; 548 } 549 550 554 public Dimension getDimension() { 555 if (level != null) 556 return level.getHierarchy().getDimension(); 557 Dimension dim = model.lookupDimByUName(dimUName); 558 return dim; 559 } 560 561 565 public void setParent(XMLA_Member parent) { 566 this.parent = parent; 567 this.parentOk = true; 568 } 569 570 574 public Hierarchy getHierarchy() { 575 if (level != null) 576 return level.getHierarchy(); 577 XMLA_Dimension dim = (XMLA_Dimension) getDimension(); 578 String defHier = dim.getDefaultHier(); 579 if (defHier != null && defHier.length() > 0) 580 return model.lookupHierByUName(defHier); 581 else 582 return null; 583 } 584 585 589 public boolean isCalculated() { 590 return isCalculated; 591 } 592 593 596 public String toMdx() { 597 return this.uniqueName; 598 } 599 600 604 public Object clone() { 605 String [] nameParts = StringUtil.splitUniqueName(uniqueName); 606 CompoundId clone = new CompoundId(nameParts[0], false); 607 for (int i = 1; i < nameParts.length; i++) { 608 clone.append(nameParts[i], false); 609 } 610 return clone; 611 } 612 613 616 public void addProp(Property prop) { 617 properties.add(prop); 618 } 619 620 623 public void clearProps() { 624 properties.clear(); 625 } 626 627 630 public boolean isPropsOk() { 631 return propsOk; 632 } 633 634 637 public void setPropsOk(boolean b) { 638 propsOk = b; 639 } 640 641 644 public boolean isChildrenOk() { 645 return childrenOk; 646 } 647 648 651 public boolean isParentOk() { 652 return parentOk; 653 } 654 655 658 public void setChildrenOk(boolean b) { 659 childrenOk = b; 660 } 661 662 665 public void setParentOk(boolean b) { 666 parentOk = b; 667 } 668 669 672 public boolean isSAP() { 673 return model.isSAP(); 674 } 675 676 public boolean isMicrosoft() { 677 return model.isMicrosoft(); 678 } 679 680 public boolean isMondrian() { 681 return model.isMondrian(); 682 } 683 684 687 public void accept(ExpVisitor visitor) { 688 visitor.visitMember(this); 689 } 690 691 694 public long getDisplayInfo() { 695 return displayInfo; 696 } 697 698 701 public void setDisplayInfo(String strDisplayInfo) { 702 if (strDisplayInfo == null || displayInfo != -1) 703 return; 704 displayInfo = Long.parseLong(strDisplayInfo); 705 if (childrenCardinality == -1) 707 childrenCardinality = Math.abs(displayInfo) % 65536; 708 } 709 710 713 public boolean isAll() { 714 return ((XMLA_Level)this.getLevel()).isAll(); 715 } 716 717 } | Popular Tags |