1 19 20 package org.apache.cayenne.reflect.generic; 21 22 import org.apache.cayenne.Fault; 23 import org.apache.cayenne.Persistent; 24 import org.apache.cayenne.ValueHolder; 25 import org.apache.cayenne.map.ObjRelationship; 26 import org.apache.cayenne.reflect.ArcProperty; 27 import org.apache.cayenne.reflect.ClassDescriptor; 28 import org.apache.cayenne.reflect.PropertyException; 29 import org.apache.cayenne.reflect.PropertyVisitor; 30 import org.apache.cayenne.reflect.ToManyProperty; 31 32 39 class DataObjectToManyProperty extends DataObjectBaseProperty implements ToManyProperty { 40 41 protected ObjRelationship relationship; 42 protected String reverseName; 43 protected ClassDescriptor targetDescriptor; 44 45 DataObjectToManyProperty(ObjRelationship relationship, 46 ClassDescriptor targetDescriptor) { 47 this.relationship = relationship; 48 this.targetDescriptor = targetDescriptor; 49 this.reverseName = relationship.getReverseRelationshipName(); 50 } 51 52 public ArcProperty getComplimentaryReverseArc() { 53 return reverseName != null ? (ArcProperty) targetDescriptor 54 .getProperty(reverseName) : null; 55 } 56 57 public ClassDescriptor getTargetDescriptor() { 58 return targetDescriptor; 59 } 60 61 public String getName() { 62 return relationship.getName(); 63 } 64 65 public ObjRelationship getRelationship() { 66 return relationship; 67 } 68 69 public void addTarget(Object source, Object target, boolean setReverse) 70 throws PropertyException { 71 try { 72 toDataObject(source).addToManyTarget( 73 getName(), 74 toDataObject(target), 75 setReverse); 76 } 77 catch (Throwable th) { 78 throw new PropertyException("Error setting to-many DataObject property: " 79 + getName(), this, source, th); 80 } 81 } 82 83 public void removeTarget(Object source, Object target, boolean setReverse) 84 throws PropertyException { 85 try { 86 toDataObject(source).removeToManyTarget( 87 getName(), 88 toDataObject(target), 89 setReverse); 90 } 91 catch (Throwable th) { 92 throw new PropertyException("Error unsetting to-many DataObject property: " 93 + getName(), this, source, th); 94 } 95 } 96 97 public void injectValueHolder(Object object) throws PropertyException { 98 if (readPropertyDirectly(object) == null) { 99 writePropertyDirectly(object, null, Fault.getToManyFault().resolveFault( 100 (Persistent) object, 101 getName())); 102 } 103 } 104 105 public boolean isFault(Object source) { 106 return readPropertyDirectly(source) instanceof Fault; 107 } 108 109 public boolean visit(PropertyVisitor visitor) { 110 return visitor.visitToMany(this); 111 } 112 113 public void invalidate(Object object) { 114 Object value = readPropertyDirectly(object); 115 if (value instanceof Fault) { 116 } 118 else if (value instanceof ValueHolder) { 119 ((ValueHolder) value).invalidate(); 120 } 121 else { 122 writePropertyDirectly(object, null, Fault.getToManyFault()); 123 } 124 } 125 } 126 | Popular Tags |