1 56 package org.objectstyle.cayenne.map; 57 58 import java.util.Collection ; 59 import java.util.Iterator ; 60 import java.util.List ; 61 import java.util.Map ; 62 import java.util.SortedMap ; 63 import java.util.TreeMap ; 64 65 import org.apache.commons.collections.Transformer; 66 import org.objectstyle.cayenne.CayenneRuntimeException; 67 import org.objectstyle.cayenne.exp.Expression; 68 import org.objectstyle.cayenne.exp.ExpressionException; 69 import org.objectstyle.cayenne.exp.ExpressionFactory; 70 import org.objectstyle.cayenne.map.event.AttributeEvent; 71 import org.objectstyle.cayenne.map.event.EntityEvent; 72 import org.objectstyle.cayenne.map.event.ObjAttributeListener; 73 import org.objectstyle.cayenne.map.event.ObjEntityListener; 74 import org.objectstyle.cayenne.map.event.ObjRelationshipListener; 75 import org.objectstyle.cayenne.map.event.RelationshipEvent; 76 import org.objectstyle.cayenne.util.Util; 77 import org.objectstyle.cayenne.util.XMLEncoder; 78 79 87 public class ObjEntity extends Entity implements ObjEntityListener, 88 ObjAttributeListener, 89 ObjRelationshipListener { 90 final public static int LOCK_TYPE_NONE = 0; 91 final public static int LOCK_TYPE_OPTIMISTIC = 1; 92 93 protected String superClassName; 94 protected String className; 95 protected String dbEntityName; 96 protected String superEntityName; 97 protected Expression qualifier; 98 protected boolean readOnly; 99 protected int lockType = LOCK_TYPE_NONE; 100 101 protected String clientClassName; 102 protected String clientSuperClassName; 103 104 public ObjEntity() { 105 super(); 106 } 107 108 public ObjEntity(String name) { 109 this(); 110 this.setName(name); 111 } 112 113 118 public void encodeAsXML(XMLEncoder encoder) { 119 encoder.print("<obj-entity name=\""); 120 encoder.print(getName()); 121 122 if (getSuperEntityName() != null && getSuperEntity() != null) { 124 encoder.print("\" superEntityName=\""); 125 encoder.print(getSuperEntityName()); 126 } 127 128 if (getClassName() != null) { 129 encoder.print("\" className=\""); 130 encoder.print(getClassName()); 131 } 132 133 if (getClientClassName() != null) { 134 encoder.print("\" clientClassName=\""); 135 encoder.print(getClientClassName()); 136 } 137 138 if (isReadOnly()) { 139 encoder.print("\" readOnly=\"true"); 140 } 141 142 if (getDeclaredLockType() == LOCK_TYPE_OPTIMISTIC) { 143 encoder.print("\" lock-type=\"optimistic"); 144 } 145 146 if (getSuperEntityName() == null && getDbEntity() != null) { 147 encoder.print("\" dbEntityName=\""); 148 encoder.print(Util.encodeXmlAttribute(getDbEntityName())); 149 } 150 151 if (getSuperEntityName() == null && getSuperClassName() != null) { 152 encoder.print("\" superClassName=\""); 153 encoder.print(getSuperClassName()); 154 } 155 156 if (getSuperEntityName() == null && getClientSuperClassName() != null) { 157 encoder.print("\" clientSuperClassName=\""); 158 encoder.print(getClientSuperClassName()); 159 } 160 161 encoder.println("\">"); 162 encoder.indent(1); 163 164 if (qualifier != null) { 165 encoder.print("<qualifier>"); 166 qualifier.encodeAsXML(encoder); 167 encoder.println("</qualifier>"); 168 } 169 170 encoder.print(getDeclaredAttributes()); 172 173 encoder.indent(-1); 174 encoder.println("</obj-entity>"); 175 } 176 177 183 public Class getJavaClass(ClassLoader classLoader) { 184 if(this.getClassName() == null) { 185 return null; 186 } 187 188 try { 189 if (classLoader == null) { 191 return Class.forName(this.getClassName()); 192 } 193 else { 194 return classLoader.loadClass(this.getClassName()); 195 } 196 } 197 catch (ClassNotFoundException e) { 198 throw new CayenneRuntimeException( 199 "Failed to load class for name '" 200 + this.getClassName() 201 + "': " 202 + e.getMessage(), 203 e); 204 } 205 } 206 207 215 public int getLockType() { 216 if (lockType != LOCK_TYPE_NONE) { 219 return lockType; 220 } 221 222 ObjEntity superEntity = getSuperEntity(); 223 return (superEntity != null) ? superEntity.getLockType() : lockType; 224 } 225 226 232 public int getDeclaredLockType() { 233 return lockType; 234 } 235 236 241 public void setDeclaredLockType(int i) { 242 lockType = i; 243 } 244 245 253 public Expression getDeclaredQualifier() { 254 return qualifier; 255 } 256 257 262 public String getSuperEntityName() { 263 return superEntityName; 264 } 265 266 272 public void setDeclaredQualifier(Expression qualifier) { 273 this.qualifier = qualifier; 274 } 275 276 281 public void setSuperEntityName(String superEntityName) { 282 this.superEntityName = superEntityName; 283 } 284 285 288 public String getClassName() { 289 return className; 290 } 291 292 295 public void setClassName(String className) { 296 this.className = className; 297 } 298 299 300 305 public String getClientClassName() { 306 return clientClassName; 307 } 308 309 314 public void setClientClassName(String clientClassName) { 315 this.clientClassName = clientClassName; 316 } 317 318 319 324 public String getSuperClassName() { 325 ObjEntity superEntity = getSuperEntity(); 326 return (superEntity != null) ? superEntity.getClassName() : superClassName; 327 } 328 329 336 public void setSuperClassName(String superClassName) { 337 this.superClassName = superClassName; 338 } 339 340 347 public String getClientSuperClassName() { 348 ObjEntity superEntity = getSuperEntity(); 349 return (superEntity != null) 350 ? superEntity.getClientSuperClassName() 351 : clientSuperClassName; 352 } 353 354 364 public void setClientSuperClassName(String clientSuperClassName) { 365 this.clientSuperClassName = clientSuperClassName; 366 } 367 368 373 public ObjEntity getSuperEntity() { 374 return (superEntityName != null) 375 ? getNonNullNamespace().getObjEntity(superEntityName) 376 : null; 377 } 378 379 382 public DbEntity getDbEntity() { 383 384 if (dbEntityName != null) { 386 return getNonNullNamespace().getDbEntity(dbEntityName); 387 } 388 389 ObjEntity superEntity = getSuperEntity(); 390 if (superEntity != null) { 391 return superEntity.getDbEntity(); 392 } 393 394 return null; 395 } 396 397 404 public void setDbEntity(DbEntity dbEntity) { 405 this.dbEntityName = (dbEntity != null) ? dbEntity.getName() : null; 406 } 407 408 412 public Attribute getAttribute(String name) { 413 Attribute attribute = super.getAttribute(name); 414 if (attribute != null) { 415 return attribute; 416 } 417 418 if (superEntityName == null) { 419 return null; 420 } 421 422 ObjEntity superEntity = getSuperEntity(); 423 return (superEntity != null) ? superEntity.getAttribute(name) : null; 424 } 425 426 430 public SortedMap getAttributeMap() { 431 if (superEntityName == null) { 432 return super.getAttributeMap(); 433 } 434 435 SortedMap attributeMap = new TreeMap (); 436 appendAttributes(attributeMap); 437 return attributeMap; 438 } 439 440 443 final void appendAttributes(Map map) { 444 map.putAll(super.getAttributeMap()); 445 446 ObjEntity superEntity = getSuperEntity(); 447 if (superEntity != null) { 448 superEntity.appendAttributes(map); 449 } 450 } 451 452 456 public Collection getAttributes() { 457 if (superEntityName == null) { 458 return super.getAttributes(); 459 } 460 461 return getAttributeMap().values(); 462 } 463 464 470 public Collection getDeclaredAttributes() { 471 return super.getAttributes(); 472 } 473 474 478 public Relationship getRelationship(String name) { 479 Relationship relationship = super.getRelationship(name); 480 if (relationship != null) { 481 return relationship; 482 } 483 484 if (superEntityName == null) { 485 return null; 486 } 487 488 ObjEntity superEntity = getSuperEntity(); 489 return (superEntity != null) ? superEntity.getRelationship(name) : null; 490 } 491 492 public SortedMap getRelationshipMap() { 493 if (superEntityName == null) { 494 return super.getRelationshipMap(); 495 } 496 497 SortedMap relationshipMap = new TreeMap (); 498 appendRelationships(relationshipMap); 499 return relationshipMap; 500 } 501 502 505 final void appendRelationships(Map map) { 506 map.putAll(super.getRelationshipMap()); 507 508 ObjEntity superEntity = getSuperEntity(); 509 if (superEntity != null) { 510 superEntity.appendRelationships(map); 511 } 512 } 513 514 public Collection getRelationships() { 515 if (superEntityName == null) { 516 return super.getRelationships(); 517 } 518 519 return getRelationshipMap().values(); 520 } 521 522 528 public Collection getDeclaredRelationships() { 529 return super.getRelationships(); 530 } 531 532 536 public ObjAttribute getAttributeForDbAttribute(DbAttribute dbAttribute) { 537 Iterator it = getAttributeMap().entrySet().iterator(); 538 while (it.hasNext()) { 539 Map.Entry entry = (Map.Entry ) it.next(); 540 ObjAttribute objAttr = (ObjAttribute) entry.getValue(); 541 if (objAttr.getDbAttribute() == dbAttribute) 542 return objAttr; 543 } 544 return null; 545 } 546 547 552 public ObjRelationship getRelationshipForDbRelationship(DbRelationship dbRelationship) { 553 Iterator it = getRelationshipMap().entrySet().iterator(); 554 while (it.hasNext()) { 555 Map.Entry entry = (Map.Entry ) it.next(); 556 ObjRelationship objRel = (ObjRelationship) entry.getValue(); 557 558 List relList = objRel.getDbRelationships(); 559 if (relList.size() != 1) { 560 continue; 561 } 562 563 if (relList.get(0) == dbRelationship) { 564 return objRel; 565 } 566 } 567 return null; 568 } 569 570 574 public void clearDbMapping() { 575 if (dbEntityName == null) 576 return; 577 578 Iterator it = getAttributeMap().values().iterator(); 579 while (it.hasNext()) { 580 ObjAttribute objAttr = (ObjAttribute) it.next(); 581 DbAttribute dbAttr = objAttr.getDbAttribute(); 582 if (null != dbAttr) { 583 objAttr.setDbAttribute(null); 584 } 585 } 586 587 Iterator rels = this.getRelationships().iterator(); 588 while (rels.hasNext()) { 589 ((ObjRelationship) rels.next()).clearDbRelationships(); 590 } 591 592 dbEntityName = null; 593 } 594 595 601 public boolean isReadOnly() { 602 return readOnly; 603 } 604 605 public void setReadOnly(boolean readOnly) { 606 this.readOnly = readOnly; 607 } 608 609 615 public boolean isSubentityOf(ObjEntity entity) { 616 if (entity == null) { 617 return false; 618 } 619 620 if (entity == this) { 621 return false; 622 } 623 624 ObjEntity superEntity = getSuperEntity(); 625 if (superEntity == entity) { 626 return true; 627 } 628 629 return (superEntity != null) ? superEntity.isSubentityOf(entity) : false; 630 } 631 632 public Iterator resolvePathComponents(Expression pathExp) 633 throws ExpressionException { 634 635 if (pathExp.getType() == Expression.DB_PATH) { 637 if (getDbEntity() == null) { 638 throw new ExpressionException( 639 "Can't resolve DB_PATH '" + pathExp + "', DbEntity is not set."); 640 } 641 642 return getDbEntity().resolvePathComponents(pathExp); 643 } 644 645 if (pathExp.getType() == Expression.OBJ_PATH) { 646 return new PathIterator((String ) pathExp.getOperand(0)); 647 } 648 649 throw new ExpressionException( 650 "Invalid expression type: '" 651 + pathExp.expName() 652 + "', OBJ_PATH is expected."); 653 } 654 655 661 public Expression translateToDbPath(Expression expression) { 662 663 if (expression == null) { 664 return null; 665 } 666 667 if (getDbEntity() == null) { 668 throw new CayenneRuntimeException( 669 "Can't translate expression to DB_PATH, no DbEntity for '" 670 + getName() 671 + "'."); 672 } 673 674 return expression.transform(new DBPathConverter()); 677 } 678 679 685 public Expression translateToRelatedEntity( 686 Expression expression, 687 String relationshipPath) { 688 689 if (expression == null) { 690 return null; 691 } 692 693 if (relationshipPath == null) { 694 return expression; 695 } 696 697 if (getDbEntity() == null) { 698 throw new CayenneRuntimeException( 699 "Can't transform expression, no DbEntity for '" + getName() + "'."); 700 } 701 702 705 DBPathConverter transformer = new DBPathConverter(); 706 707 String dbPath = transformer.toDbPath(resolvePathComponents(relationshipPath)); 708 Expression dbClone = expression.transform(transformer); 709 710 return getDbEntity().translateToRelatedEntity(dbClone, dbPath); 711 } 712 713 final class DBPathConverter implements Transformer { 714 String toDbPath(Iterator objectPathComponents) { 717 StringBuffer buf = new StringBuffer (); 718 while (objectPathComponents.hasNext()) { 719 Object component = objectPathComponents.next(); 720 721 Iterator dbSubpath; 722 723 if (component instanceof ObjRelationship) { 724 dbSubpath = 725 ((ObjRelationship) component).getDbRelationships().iterator(); 726 } 727 else if (component instanceof ObjAttribute) { 728 dbSubpath = ((ObjAttribute) component).getDbPathIterator(); 729 } 730 else { 731 throw new CayenneRuntimeException( 732 "Unknown path component: " + component); 733 } 734 735 while (dbSubpath.hasNext()) { 736 MapObject subComponent = (MapObject) dbSubpath.next(); 737 if (buf.length() > 0) { 738 buf.append(Entity.PATH_SEPARATOR); 739 } 740 741 buf.append(subComponent.getName()); 742 } 743 } 744 745 return buf.toString(); 746 } 747 748 public Object transform(Object input) { 749 750 if (!(input instanceof Expression)) { 751 return input; 752 } 753 754 Expression expression = (Expression) input; 755 756 if (expression.getType() != Expression.OBJ_PATH) { 757 return input; 758 } 759 760 762 String converted = toDbPath(resolvePathComponents(expression)); 763 Expression exp = ExpressionFactory.expressionOfType(Expression.DB_PATH); 764 exp.setOperand(0, converted); 765 return exp; 766 } 767 } 768 769 774 public String getDbEntityName() { 775 return dbEntityName; 776 } 777 778 783 public void setDbEntityName(String string) { 784 dbEntityName = string; 785 } 786 787 794 public void objEntityChanged(EntityEvent e){ 795 if ((e == null) || (e.getEntity() != this)) { 796 return; 798 } 799 800 if (e.getId() == EntityEvent.CHANGE && e.isNameChange()){ 802 String oldName = e.getOldName(); 803 String newName = e.getNewName(); 804 805 DataMap map = getDataMap(); 806 if (map != null) { 807 ObjEntity oe = (ObjEntity) e.getEntity(); 808 Iterator rit = oe.getRelationships().iterator(); 809 while (rit.hasNext()){ 810 ObjRelationship or = (ObjRelationship) rit.next(); 811 or = or.getReverseRelationship(); 812 if (null != or && or.targetEntityName.equals(oldName)){ 813 or.targetEntityName = newName; 814 } 815 } 816 } 817 } 818 } 819 820 821 public void objEntityAdded(EntityEvent e){ 822 } 824 825 826 public void objEntityRemoved(EntityEvent e){ 827 } 829 830 831 public void objAttributeChanged(AttributeEvent e){ 832 } 834 835 836 public void objAttributeAdded(AttributeEvent e){ 837 } 839 840 841 public void objAttributeRemoved(AttributeEvent e){ 842 } 844 845 846 public void objRelationshipChanged(RelationshipEvent e){ 847 } 849 850 851 public void objRelationshipAdded(RelationshipEvent e){ 852 } 854 855 856 public void objRelationshipRemoved(RelationshipEvent e){ 857 } 859 860 } 861 | Popular Tags |