1 19 20 package org.apache.cayenne.map; 21 22 import org.apache.commons.lang.builder.ToStringBuilder; 23 import org.apache.cayenne.CayenneRuntimeException; 24 import org.apache.cayenne.util.XMLEncoder; 25 import org.apache.cayenne.util.XMLSerializable; 26 27 33 public class DbJoin implements XMLSerializable { 34 35 protected DbRelationship relationship; 36 protected String sourceName; 37 protected String targetName; 38 39 protected DbJoin() { 40 } 41 42 public DbJoin(DbRelationship relationship) { 43 this.relationship = relationship; 44 } 45 46 public DbJoin(DbRelationship relationship, String sourceName, String targetName) { 47 this.relationship = relationship; 48 this.sourceName = sourceName; 49 this.targetName = targetName; 50 } 51 52 56 public DbJoin createReverseJoin() { 57 DbJoin reverse = new DbJoin(); 58 reverse.setTargetName(sourceName); 59 reverse.setSourceName(targetName); 60 return reverse; 61 } 62 63 66 public DbAttribute getSource() { 67 if (sourceName == null) { 68 return null; 69 } 70 71 Relationship r = getNonNullRelationship(); 72 Entity entity = r.getSourceEntity(); 73 if (entity == null) { 74 return null; 75 } 76 77 return (DbAttribute) entity.getAttribute(sourceName); 78 } 79 80 public DbAttribute getTarget() { 81 if (targetName == null) { 82 return null; 83 } 84 85 Relationship r = getNonNullRelationship(); 86 Entity entity = r.getTargetEntity(); 87 if (entity == null) { 88 return null; 89 } 90 91 return (DbAttribute) entity.getAttribute(targetName); 92 } 93 94 97 public void encodeAsXML(XMLEncoder encoder) { 98 encoder.print("<db-attribute-pair"); 99 100 if (getSourceName() != null) { 102 encoder.print(" source=\""); 103 encoder.print(getSourceName()); 104 encoder.print("\""); 105 } 106 107 if (getTargetName() != null) { 108 encoder.print(" target=\""); 109 encoder.print(getTargetName()); 110 encoder.print("\""); 111 } 112 113 encoder.println("/>"); 114 } 115 116 public DbRelationship getRelationship() { 117 return relationship; 118 } 119 120 public String getSourceName() { 121 return sourceName; 122 } 123 124 public String getTargetName() { 125 return targetName; 126 } 127 128 public void setRelationship(DbRelationship relationship) { 129 this.relationship = relationship; 130 } 131 132 public void setSourceName(String string) { 133 sourceName = string; 134 } 135 136 public void setTargetName(String string) { 137 targetName = string; 138 } 139 140 private final DbRelationship getNonNullRelationship() { 141 if (relationship == null) { 142 throw new CayenneRuntimeException("Join has no parent Relationship."); 143 } 144 145 return relationship; 146 } 147 148 public String toString() { 149 ToStringBuilder builder = new ToStringBuilder(this); 150 builder.append("source", getSourceName()); 151 builder.append("target", getTargetName()); 152 return builder.toString(); 153 } 154 } 155 | Popular Tags |