1 56 package org.objectstyle.cayenne.map; 57 58 import org.apache.commons.lang.builder.ToStringBuilder; 59 import org.objectstyle.cayenne.CayenneRuntimeException; 60 import org.objectstyle.cayenne.util.XMLEncoder; 61 import org.objectstyle.cayenne.util.XMLSerializable; 62 63 69 public class DbJoin implements XMLSerializable { 70 71 protected DbRelationship relationship; 72 protected String sourceName; 73 protected String targetName; 74 75 protected DbJoin() { 76 } 77 78 public DbJoin(DbRelationship relationship) { 79 this.relationship = relationship; 80 } 81 82 public DbJoin(DbRelationship relationship, String sourceName, String targetName) { 83 this.relationship = relationship; 84 this.sourceName = sourceName; 85 this.targetName = targetName; 86 } 87 88 92 public DbJoin createReverseJoin() { 93 DbJoin reverse = new DbJoin(); 94 reverse.setTargetName(sourceName); 95 reverse.setSourceName(targetName); 96 return reverse; 97 } 98 99 102 public DbAttribute getSource() { 103 if (sourceName == null) { 104 return null; 105 } 106 107 Relationship r = getNonNullRelationship(); 108 Entity entity = r.getSourceEntity(); 109 if (entity == null) { 110 return null; 111 } 112 113 return (DbAttribute) entity.getAttribute(sourceName); 114 } 115 116 public DbAttribute getTarget() { 117 if (targetName == null) { 118 return null; 119 } 120 121 Relationship r = getNonNullRelationship(); 122 Entity entity = r.getTargetEntity(); 123 if (entity == null) { 124 return null; 125 } 126 127 return (DbAttribute) entity.getAttribute(targetName); 128 } 129 130 133 public void encodeAsXML(XMLEncoder encoder) { 134 encoder.print("<db-attribute-pair"); 135 136 if (getSourceName() != null) { 138 encoder.print(" source=\""); 139 encoder.print(getSourceName()); 140 encoder.print("\""); 141 } 142 143 if (getTargetName() != null) { 144 encoder.print(" target=\""); 145 encoder.print(getTargetName()); 146 encoder.print("\""); 147 } 148 149 encoder.println("/>"); 150 } 151 152 public DbRelationship getRelationship() { 153 return relationship; 154 } 155 156 public String getSourceName() { 157 return sourceName; 158 } 159 160 public String getTargetName() { 161 return targetName; 162 } 163 164 public void setRelationship(DbRelationship relationship) { 165 this.relationship = relationship; 166 } 167 168 public void setSourceName(String string) { 169 sourceName = string; 170 } 171 172 public void setTargetName(String string) { 173 targetName = string; 174 } 175 176 private final DbRelationship getNonNullRelationship() { 177 if (relationship == null) { 178 throw new CayenneRuntimeException("Join has no parent Relationship."); 179 } 180 181 return relationship; 182 } 183 184 public String toString() { 185 ToStringBuilder builder = new ToStringBuilder(this); 186 builder.append("source", getSourceName()); 187 builder.append("target", getTargetName()); 188 return builder.toString(); 189 } 190 } | Popular Tags |