KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > reflect > BaseArcProperty


1 /*****************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  ****************************************************************/

19 package org.apache.cayenne.reflect;
20
21 import org.apache.cayenne.map.ObjRelationship;
22
23 /**
24  * A base implementation of the {@link ArcProperty}.
25  *
26  * @since 3.0
27  * @author Andrus Adamchik
28  */

29 public abstract class BaseArcProperty extends BaseProperty implements ArcProperty {
30
31     protected String JavaDoc complimentaryReverseArcName;
32     protected ClassDescriptor targetDescriptor;
33     protected ObjRelationship relationship;
34
35     public BaseArcProperty(ClassDescriptor owner, ClassDescriptor targetDescriptor,
36             Accessor accessor, String JavaDoc 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 JavaDoc 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     /**
64      * A convenience method to set the reverse arc used by subclasses.
65      */

66     protected void setReverse(
67             final Object JavaDoc source,
68             final Object JavaDoc oldTarget,
69             final Object JavaDoc newTarget) {
70
71         ArcProperty reverseArc = getComplimentaryReverseArc();
72
73         if (reverseArc != null) {
74
75             // unset old
76
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             // set new reverse
99
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