1 56 package org.objectstyle.cayenne.map; 57 58 import java.io.PrintWriter ; 59 import java.util.ArrayList ; 60 import java.util.Collection ; 61 import java.util.Collections ; 62 import java.util.Iterator ; 63 import java.util.Map ; 64 import java.util.SortedMap ; 65 66 import org.apache.commons.lang.builder.ToStringBuilder; 67 import org.objectstyle.cayenne.map.event.AttributeEvent; 68 import org.objectstyle.cayenne.map.event.DbEntityListener; 69 import org.objectstyle.cayenne.map.event.DbAttributeListener; 70 import org.objectstyle.cayenne.map.event.DbRelationshipListener; 71 import org.objectstyle.cayenne.map.event.EntityEvent; 72 import org.objectstyle.cayenne.map.event.ObjEntityListener; 73 import org.objectstyle.cayenne.map.event.ObjAttributeListener; 74 import org.objectstyle.cayenne.map.event.ObjRelationshipListener; 75 import org.objectstyle.cayenne.map.event.RelationshipEvent; 76 import org.objectstyle.cayenne.project.Project; 77 import org.objectstyle.cayenne.query.Query; 78 import org.objectstyle.cayenne.util.CayenneMap; 79 import org.objectstyle.cayenne.util.Util; 80 import org.objectstyle.cayenne.util.XMLEncoder; 81 import org.objectstyle.cayenne.util.XMLSerializable; 82 83 92 public class DataMap implements XMLSerializable, MappingNamespace, 93 DbEntityListener, DbAttributeListener, DbRelationshipListener, 94 ObjEntityListener, ObjAttributeListener, ObjRelationshipListener 95 { 96 97 102 public static final String DEFAULT_SCHEMA_PROPERTY = "defaultSchema"; 103 104 109 public static final String DEFAULT_PACKAGE_PROPERTY = "defaultPackage"; 110 111 116 public static final String DEFAULT_SUPERCLASS_PROPERTY = "defaultSuperclass"; 117 118 123 public static final String DEFAULT_LOCK_TYPE_PROPERTY = "defaultLockType"; 124 125 protected String name; 126 protected String location; 127 protected MappingNamespace namespace; 128 129 protected String defaultSchema; 130 protected String defaultPackage; 131 protected String defaultSuperclass; 132 protected int defaultLockType; 133 134 private CayenneMap objEntityMap = new CayenneMap(this); 138 139 private SortedMap objEntityMapRef = Collections.unmodifiableSortedMap(objEntityMap); 141 142 private Collection objEntityValuesRef = Collections 144 .unmodifiableCollection(objEntityMap.values()); 145 146 private CayenneMap dbEntityMap = new CayenneMap(this); 150 151 private SortedMap dbEntityMapRef = Collections.unmodifiableSortedMap(dbEntityMap); 153 154 private Collection dbEntityValuesRef = Collections.unmodifiableCollection(dbEntityMap 156 .values()); 157 158 private CayenneMap procedureMap = new CayenneMap(this); 162 163 private SortedMap procedureMapRef = Collections.unmodifiableSortedMap(procedureMap); 165 166 private Collection procedureValuesRef = Collections 168 .unmodifiableCollection(procedureMap.values()); 169 170 private CayenneMap queryMap = new CayenneMap(this); 174 175 private SortedMap queryMapRef = Collections.unmodifiableSortedMap(queryMap); 177 178 private Collection queriesValuesRef = Collections.unmodifiableCollection(queryMap 180 .values()); 181 182 185 public DataMap() { 186 initWithProperties(Collections.EMPTY_MAP); 188 } 189 190 193 public DataMap(String mapName) { 194 this(); 195 this.setName(mapName); 196 } 197 198 204 public void initWithProperties(Map properties) { 205 if (properties == null) { 207 properties = Collections.EMPTY_MAP; 208 } 209 210 Object lockType = properties.get(DEFAULT_LOCK_TYPE_PROPERTY); 211 Object packageName = properties.get(DEFAULT_PACKAGE_PROPERTY); 212 Object schema = properties.get(DEFAULT_SCHEMA_PROPERTY); 213 Object superclass = properties.get(DEFAULT_SUPERCLASS_PROPERTY); 214 215 this.defaultLockType = "optimistic".equals(lockType) 216 ? ObjEntity.LOCK_TYPE_OPTIMISTIC 217 : ObjEntity.LOCK_TYPE_NONE; 218 219 this.defaultPackage = (packageName != null) ? packageName.toString() : null; 220 this.defaultSchema = (schema != null) ? schema.toString() : null; 221 this.defaultSuperclass = (superclass != null) ? superclass.toString() : null; 222 } 223 224 231 public void encodeAsXML(PrintWriter pw) { 232 XMLEncoder encoder = new XMLEncoder(pw, "\t"); 233 encoder.println("<?xml version=\"1.0\" encoding=\"utf-8\"?>"); 234 encodeAsXML(encoder); 235 } 236 237 242 public void encodeAsXML(XMLEncoder encoder) { 243 encoder.print("<data-map project-version=\""); 244 encoder.print(String.valueOf(Project.CURRENT_PROJECT_VERSION)); 245 encoder.println("\">"); 246 247 encoder.indent(1); 248 249 if (defaultLockType == ObjEntity.LOCK_TYPE_OPTIMISTIC) { 251 encoder.printProperty(DEFAULT_LOCK_TYPE_PROPERTY, "optimistic"); 252 } 253 254 if (!Util.isEmptyString(defaultPackage)) { 255 encoder.printProperty(DEFAULT_PACKAGE_PROPERTY, defaultPackage); 256 } 257 258 if (!Util.isEmptyString(defaultSchema)) { 259 encoder.printProperty(DEFAULT_SCHEMA_PROPERTY, defaultSchema); 260 } 261 262 if (!Util.isEmptyString(defaultSuperclass)) { 263 encoder.printProperty(DEFAULT_SUPERCLASS_PROPERTY, defaultSuperclass); 264 } 265 266 encoder.print(getProcedureMap()); 268 269 boolean hasDerived = false; 271 Iterator dbEntities = getDbEntityMap().entrySet().iterator(); 272 while (dbEntities.hasNext()) { 273 Map.Entry entry = (Map.Entry ) dbEntities.next(); 274 DbEntity dbe = (DbEntity) entry.getValue(); 275 276 if (dbe instanceof DerivedDbEntity) { 278 hasDerived = true; 279 } 280 else { 281 dbe.encodeAsXML(encoder); 282 } 283 } 284 285 if (hasDerived) { 287 Iterator derivedDbEntities = getDbEntityMap().entrySet().iterator(); 288 while (derivedDbEntities.hasNext()) { 289 Map.Entry entry = (Map.Entry ) derivedDbEntities.next(); 290 DbEntity dbe = (DbEntity) entry.getValue(); 291 292 if (dbe instanceof DerivedDbEntity) { 294 dbe.encodeAsXML(encoder); 295 } 296 } 297 } 298 299 encoder.print(getObjEntityMap()); 301 encodeDBRelationshipsAsXML(getDbEntityMap(), encoder); 302 encodeOBJRelationshipsAsXML(getObjEntityMap(), encoder); 303 encoder.print(getQueryMap()); 304 305 encoder.indent(-1); 306 encoder.println("</data-map>"); 307 } 308 309 private final void encodeDBRelationshipsAsXML(Map entityMap, XMLEncoder encoder) { 311 Iterator it = entityMap.entrySet().iterator(); 312 while (it.hasNext()) { 313 Map.Entry entry = (Map.Entry ) it.next(); 314 Entity entity = (Entity) entry.getValue(); 315 encoder.print(entity.getRelationships()); 316 } 317 } 318 319 private final void encodeOBJRelationshipsAsXML(Map entityMap, XMLEncoder encoder) { 321 Iterator it = entityMap.entrySet().iterator(); 322 while (it.hasNext()) { 323 Map.Entry entry = (Map.Entry ) it.next(); 324 ObjEntity entity = (ObjEntity) entry.getValue(); 325 encoder.print(entity.getDeclaredRelationships()); 326 } 327 } 328 329 public String toString() { 330 return new ToStringBuilder(this).append("name", getName()).toString(); 331 } 332 333 336 public String getName() { 337 return name; 338 } 339 340 public void setName(String name) { 341 this.name = (name != null ? name : "unnamed"); 342 } 343 344 352 public void mergeWithDataMap(DataMap map) { 353 Iterator dbs = new ArrayList (map.getDbEntities()).iterator(); 354 while (dbs.hasNext()) { 355 DbEntity ent = (DbEntity) dbs.next(); 356 this.removeDbEntity(ent.getName()); 357 this.addDbEntity(ent); 358 } 359 360 Iterator objs = new ArrayList (map.getObjEntities()).iterator(); 361 while (objs.hasNext()) { 362 ObjEntity ent = (ObjEntity) objs.next(); 363 this.removeObjEntity(ent.getName()); 364 this.addObjEntity(ent); 365 } 366 367 Iterator queries = new ArrayList (map.getQueries()).iterator(); 368 while (queries.hasNext()) { 369 Query query = (Query) queries.next(); 370 this.removeQuery(query.getName()); 371 this.addQuery(query); 372 } 373 } 374 375 380 public String getLocation() { 381 return location; 382 } 383 384 387 public void setLocation(String location) { 388 this.location = location; 389 } 390 391 395 public SortedMap getObjEntityMap() { 396 return objEntityMapRef; 397 } 398 399 403 public SortedMap getDbEntityMap() { 404 return dbEntityMapRef; 405 } 406 407 412 public Query getQuery(String queryName) { 413 Query query = (Query) queryMap.get(queryName); 414 if (query != null) { 415 return query; 416 } 417 418 return namespace != null ? namespace.getQuery(queryName) : null; 419 } 420 421 426 public void addQuery(Query query) { 427 if (query == null) { 428 throw new NullPointerException ("Can't add null query."); 429 } 430 431 if (query.getName() == null) { 432 throw new NullPointerException ("Query name can't be null."); 433 } 434 435 queryMap.put(query.getName(), query); 436 } 437 438 443 public void removeQuery(String queryName) { 444 queryMap.remove(queryName); 445 } 446 447 450 public void clearQueries() { 451 queryMap.clear(); 452 } 453 454 457 public SortedMap getQueryMap() { 458 return queryMapRef; 459 } 460 461 464 public Collection getQueries() { 465 return queriesValuesRef; 466 } 467 468 471 public void addObjEntity(ObjEntity objEntity) { 472 if (objEntity.getName() == null) { 473 throw new NullPointerException ("Attempt to add ObjEntity with no name."); 474 } 475 476 objEntityMap.put(objEntity.getName(), objEntity); 477 } 478 479 482 public void addDbEntity(DbEntity dbEntity) { 483 if (dbEntity.getName() == null) { 484 throw new NullPointerException ("Attempt to add DbEntity with no name."); 485 } 486 487 dbEntityMap.put(dbEntity.getName(), dbEntity); 488 } 489 490 493 public Collection getObjEntities() { 494 return objEntityValuesRef; 495 } 496 497 503 public Collection getObjEntities(boolean includeDeps) { 504 return getObjEntities(); 505 } 506 507 510 public Collection getDbEntities() { 511 return dbEntityValuesRef; 512 } 513 514 518 public DbEntity getDbEntity(String dbEntityName) { 519 DbEntity entity = (DbEntity) dbEntityMap.get(dbEntityName); 520 521 if (entity != null) { 522 return entity; 523 } 524 525 return namespace != null ? namespace.getDbEntity(dbEntityName) : null; 526 } 527 528 533 public ObjEntity getObjEntityForJavaClass(String javaClassName) { 534 if (javaClassName == null) { 535 return null; 536 } 537 538 Iterator it = getObjEntityMap().entrySet().iterator(); 539 while (it.hasNext()) { 540 Map.Entry entry = (Map.Entry ) it.next(); 541 ObjEntity entity = (ObjEntity) entry.getValue(); 542 if (javaClassName.equals(entity.getClassName())) { 543 return entity; 544 } 545 } 546 547 return null; 548 } 549 550 554 public ObjEntity getObjEntity(String objEntityName) { 555 ObjEntity entity = (ObjEntity) objEntityMap.get(objEntityName); 556 if (entity != null) { 557 return entity; 558 } 559 560 return namespace != null ? namespace.getObjEntity(objEntityName) : null; 561 } 562 563 566 public Collection getMappedEntities(DbEntity dbEntity) { 567 if (dbEntity == null) { 568 return Collections.EMPTY_LIST; 569 } 570 571 Collection allEntities = (namespace != null) 572 ? namespace.getObjEntities() 573 : objEntityValuesRef; 574 575 if (allEntities.isEmpty()) { 576 return Collections.EMPTY_LIST; 577 } 578 579 Collection result = new ArrayList (); 580 Iterator iter = allEntities.iterator(); 581 while (iter.hasNext()) { 582 ObjEntity objEnt = (ObjEntity) iter.next(); 583 if (objEnt.getDbEntity() == dbEntity) { 584 result.add(objEnt); 585 } 586 } 587 588 return result; 589 } 590 591 594 public void removeDbEntity(String dbEntityName) { 595 removeDbEntity(dbEntityName, false); 596 } 597 598 605 public void removeDbEntity(String dbEntityName, boolean clearDependencies) { 606 DbEntity dbEntityToDelete = (DbEntity) dbEntityMap.remove(dbEntityName); 607 608 if (dbEntityToDelete != null && clearDependencies) { 609 Iterator dbEnts = this.getDbEntities().iterator(); 610 while (dbEnts.hasNext()) { 611 DbEntity dbEnt = (DbEntity) dbEnts.next(); 612 Iterator rels = new ArrayList (dbEnt.getRelationships()).iterator(); 614 while (rels.hasNext()) { 615 DbRelationship rel = (DbRelationship) rels.next(); 616 if (dbEntityName.equals(rel.getTargetEntityName())) { 617 dbEnt.removeRelationship(rel.getName()); 618 } 619 } 620 } 621 622 Iterator objEnts = this.getObjEntities().iterator(); 624 while (objEnts.hasNext()) { 625 ObjEntity objEnt = (ObjEntity) objEnts.next(); 626 if (objEnt.getDbEntity() == dbEntityToDelete) { 627 objEnt.clearDbMapping(); 628 } 629 else { 630 Iterator iter = objEnt.getRelationships().iterator(); 631 while (iter.hasNext()) { 632 ObjRelationship rel = (ObjRelationship) iter.next(); 633 Iterator dbRels = rel.getDbRelationships().iterator(); 634 while (dbRels.hasNext()) { 635 DbRelationship dbRel = (DbRelationship) dbRels.next(); 636 if (dbRel.getTargetEntity() == dbEntityToDelete) { 637 rel.clearDbRelationships(); 638 break; 639 } 640 } 641 } 642 } 643 } 644 } 645 } 646 647 650 public void removeObjEntity(String objEntityName) { 651 removeObjEntity(objEntityName, false); 652 } 653 654 660 public void removeObjEntity(String objEntityName, boolean clearDependencies) { 661 ObjEntity entity = (ObjEntity) objEntityMap.remove(objEntityName); 662 663 if (entity != null && clearDependencies) { 664 Iterator entities = this.getObjEntityMap().values().iterator(); 665 while (entities.hasNext()) { 666 ObjEntity ent = (ObjEntity) entities.next(); 667 Iterator rels = new ArrayList (ent.getRelationships()).iterator(); 669 while (rels.hasNext()) { 670 ObjRelationship rel = (ObjRelationship) rels.next(); 671 if (objEntityName.equals(rel.getTargetEntityName()) 672 || objEntityName.equals(rel.getTargetEntityName())) { 673 ent.removeRelationship(rel.getName()); 674 } 675 } 676 } 677 } 678 } 679 680 683 public Collection getProcedures() { 684 return procedureValuesRef; 685 } 686 687 691 public Procedure getProcedure(String procedureName) { 692 Procedure procedure = (Procedure) procedureMap.get(procedureName); 693 if (procedure != null) { 694 return procedure; 695 } 696 697 return namespace != null ? namespace.getProcedure(procedureName) : null; 698 } 699 700 704 public void addProcedure(Procedure procedure) { 705 if (procedure.getName() == null) { 706 throw new NullPointerException ("Attempt to add procedure with no name."); 707 } 708 709 procedureMap.put(procedure.getName(), procedure); 710 } 711 712 public void removeProcedure(String name) { 713 procedureMap.remove(name); 714 } 715 716 719 public SortedMap getProcedureMap() { 720 return procedureMapRef; 721 } 722 723 729 public MappingNamespace getNamespace() { 730 return namespace; 731 } 732 733 739 public void setNamespace(MappingNamespace namespace) { 740 this.namespace = namespace; 741 } 742 743 746 public int getDefaultLockType() { 747 return defaultLockType; 748 } 749 750 753 public void setDefaultLockType(int defaultLockType) { 754 this.defaultLockType = defaultLockType; 755 } 756 757 762 public String getDefaultClientPackage() { 763 return Util.isEmptyString(getDefaultPackage()) ? null : getDefaultPackage() 764 + ".client"; 765 } 766 767 770 public String getDefaultPackage() { 771 return defaultPackage; 772 } 773 774 777 public void setDefaultPackage(String defaultPackage) { 778 this.defaultPackage = defaultPackage; 779 } 780 781 784 public String getDefaultSchema() { 785 return defaultSchema; 786 } 787 788 791 public void setDefaultSchema(String defaultSchema) { 792 this.defaultSchema = defaultSchema; 793 } 794 795 798 public String getDefaultSuperclass() { 799 return defaultSuperclass; 800 } 801 802 805 public void setDefaultSuperclass(String defaultSuperclass) { 806 this.defaultSuperclass = defaultSuperclass; 807 } 808 809 816 public void dbEntityChanged(EntityEvent e){ 817 Entity entity = e.getEntity(); 818 if (entity instanceof DbEntity){ 819 ((DbEntity)entity).dbEntityChanged(e); 820 821 if (e.isNameChange()){ 824 dbEntityMap.remove(e.getOldName()); 826 827 dbEntityMap.put(e.getNewName(), entity); 829 830 MappingNamespace ns = getNamespace(); 832 if (ns instanceof EntityResolver) { 833 ((EntityResolver) ns).clearCache(); 834 } 835 } 836 } 837 } 838 839 840 public void dbEntityAdded(EntityEvent e){ 841 } 843 844 public void dbEntityRemoved(EntityEvent e){ 845 } 847 848 849 public void dbAttributeChanged(AttributeEvent e){ 850 Entity entity = e.getEntity(); 851 if (entity instanceof DbEntity){ 852 ((DbEntity)entity).dbAttributeChanged(e); 853 } 854 } 855 856 857 public void dbAttributeAdded(AttributeEvent e){ 858 } 860 861 862 public void dbAttributeRemoved(AttributeEvent e){ 863 } 865 866 867 public void dbRelationshipChanged(RelationshipEvent e){ 868 Entity entity = e.getEntity(); 869 if (entity instanceof DbEntity){ 870 ((DbEntity)entity).dbRelationshipChanged(e); 871 } 872 } 873 874 875 public void dbRelationshipAdded(RelationshipEvent e){ 876 } 878 879 880 public void dbRelationshipRemoved(RelationshipEvent e){ 881 } 883 884 891 public void objEntityChanged(EntityEvent e){ 892 Entity entity = e.getEntity(); 893 if (entity instanceof ObjEntity){ 894 ((ObjEntity)entity).objEntityChanged(e); 895 896 if (e.isNameChange()){ 899 objEntityMap.remove(e.getOldName()); 901 902 objEntityMap.put(e.getNewName(), entity); 904 905 MappingNamespace ns = getNamespace(); 907 if (ns instanceof EntityResolver) { 908 ((EntityResolver) ns).clearCache(); 909 } 910 } 911 } 912 } 913 914 915 public void objEntityAdded(EntityEvent e){ 916 } 918 919 920 public void objEntityRemoved(EntityEvent e){ 921 } 923 924 925 public void objAttributeChanged(AttributeEvent e){ 926 } 928 929 930 public void objAttributeAdded(AttributeEvent e){ 931 } 933 934 935 public void objAttributeRemoved(AttributeEvent e){ 936 } 938 939 940 public void objRelationshipChanged(RelationshipEvent e){ 941 } 943 944 945 public void objRelationshipAdded(RelationshipEvent e){ 946 } 948 949 950 public void objRelationshipRemoved(RelationshipEvent e){ 951 } 953 } | Popular Tags |