1 19 package org.apache.cayenne.reflect; 20 21 import org.apache.cayenne.map.ObjRelationship; 22 23 29 public abstract class BaseArcProperty extends BaseProperty implements ArcProperty { 30 31 protected String complimentaryReverseArcName; 32 protected ClassDescriptor targetDescriptor; 33 protected ObjRelationship relationship; 34 35 public BaseArcProperty(ClassDescriptor owner, ClassDescriptor targetDescriptor, 36 Accessor accessor, String reverseName) { 37 38 super(owner, accessor); 39 40 this.targetDescriptor = targetDescriptor; 41 this.complimentaryReverseArcName = reverseName; 42 this.relationship = (ObjRelationship) owner 43 .getEntity() 44 .getRelationship(getName()); 45 } 46 47 public abstract boolean visit(PropertyVisitor visitor); 48 49 public abstract boolean isFault(Object source); 50 51 public ObjRelationship getRelationship() { 52 return relationship; 53 } 54 55 public ArcProperty getComplimentaryReverseArc() { 56 return (ArcProperty) targetDescriptor.getProperty(complimentaryReverseArcName); 57 } 58 59 public ClassDescriptor getTargetDescriptor() { 60 return targetDescriptor; 61 } 62 63 66 protected void setReverse( 67 final Object source, 68 final Object oldTarget, 69 final Object newTarget) { 70 71 ArcProperty reverseArc = getComplimentaryReverseArc(); 72 73 if (reverseArc != null) { 74 75 if (oldTarget != null) { 77 78 PropertyVisitor visitor = new PropertyVisitor() { 79 80 public boolean visitToMany(ToManyProperty property) { 81 property.removeTarget(oldTarget, source, false); 82 return false; 83 } 84 85 public boolean visitToOne(ToOneProperty property) { 86 property.setTarget(oldTarget, null, false); 87 return false; 88 } 89 90 public boolean visitAttribute(AttributeProperty property) { 91 return false; 92 } 93 }; 94 95 reverseArc.visit(visitor); 96 } 97 98 if (newTarget != null) { 100 PropertyVisitor visitor = new PropertyVisitor() { 101 102 public boolean visitToMany(ToManyProperty property) { 103 property.addTarget(newTarget, source, false); 104 return false; 105 } 106 107 public boolean visitToOne(ToOneProperty property) { 108 property.setTarget(newTarget, source, false); 109 return false; 110 } 111 112 public boolean visitAttribute(AttributeProperty property) { 113 return false; 114 } 115 }; 116 117 reverseArc.visit(visitor); 118 } 119 } 120 } 121 } 122 | Popular Tags |