1 19 20 package org.apache.cayenne.map; 21 22 import java.io.Serializable ; 23 24 import org.apache.commons.lang.builder.ToStringBuilder; 25 import org.apache.cayenne.CayenneRuntimeException; 26 import org.apache.cayenne.util.CayenneMapEntry; 27 import org.apache.cayenne.util.XMLSerializable; 28 29 34 public abstract class Relationship implements CayenneMapEntry, XMLSerializable, 35 Serializable { 36 37 protected String name; 38 protected Entity sourceEntity; 39 40 protected String targetEntityName; 41 protected boolean toMany; 42 43 46 public Relationship() { 47 } 48 49 52 public Relationship(String name) { 53 setName(name); 54 } 55 56 public String getName() { 57 return name; 58 } 59 60 public void setName(String name) { 61 this.name = name; 62 } 63 64 67 public Entity getSourceEntity() { 68 return sourceEntity; 69 } 70 71 74 public void setSourceEntity(Entity sourceEntity) { 75 this.sourceEntity = sourceEntity; 76 } 77 78 81 public abstract Entity getTargetEntity(); 82 83 86 public void setTargetEntity(Entity targetEntity) { 87 if (targetEntity != null) { 88 setTargetEntityName(targetEntity.getName()); 89 } 90 else { 91 setTargetEntityName(null); 92 } 93 } 94 95 98 public String getTargetEntityName() { 99 return targetEntityName; 100 } 101 102 105 public void setTargetEntityName(String targetEntityName) { 106 this.targetEntityName = targetEntityName; 107 } 108 109 116 public boolean isToMany() { 117 return toMany; 118 } 119 120 public Object getParent() { 121 return getSourceEntity(); 122 } 123 124 public void setParent(Object parent) { 125 if (parent != null && !(parent instanceof Entity)) { 126 throw new IllegalArgumentException ("Expected null or Entity, got: " + parent); 127 } 128 129 setSourceEntity((Entity) parent); 130 } 131 132 137 final MappingNamespace getNonNullNamespace() { 138 Entity entity = getSourceEntity(); 139 140 if (entity == null) { 141 throw new CayenneRuntimeException("Relationship '" 142 + getName() 143 + "' has no parent Entity."); 144 } 145 146 return entity.getNonNullNamespace(); 147 } 148 149 152 public String toString() { 153 return new ToStringBuilder(this).append("name", getName()).append( 154 "toMany", 155 isToMany()).toString(); 156 } 157 } 158 | Popular Tags |