KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > reflect > generic > DataObjectToManyProperty


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
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 /**
33  * A list property that is intended to work with
34  * {@link org.apache.cayenne.access.ToManyList}.
35  *
36  * @since 3.0
37  * @author Andrus Adamchik
38  */

39 class DataObjectToManyProperty extends DataObjectBaseProperty implements ToManyProperty {
40
41     protected ObjRelationship relationship;
42     protected String JavaDoc 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 JavaDoc getName() {
62         return relationship.getName();
63     }
64
65     public ObjRelationship getRelationship() {
66         return relationship;
67     }
68
69     public void addTarget(Object JavaDoc source, Object JavaDoc target, boolean setReverse)
70             throws PropertyException {
71         try {
72             toDataObject(source).addToManyTarget(
73                     getName(),
74                     toDataObject(target),
75                     setReverse);
76         }
77         catch (Throwable JavaDoc th) {
78             throw new PropertyException("Error setting to-many DataObject property: "
79                     + getName(), this, source, th);
80         }
81     }
82
83     public void removeTarget(Object JavaDoc source, Object JavaDoc target, boolean setReverse)
84             throws PropertyException {
85         try {
86             toDataObject(source).removeToManyTarget(
87                     getName(),
88                     toDataObject(target),
89                     setReverse);
90         }
91         catch (Throwable JavaDoc th) {
92             throw new PropertyException("Error unsetting to-many DataObject property: "
93                     + getName(), this, source, th);
94         }
95     }
96
97     public void injectValueHolder(Object JavaDoc 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 JavaDoc 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 JavaDoc object) {
114         Object JavaDoc value = readPropertyDirectly(object);
115         if (value instanceof Fault) {
116             // nothing to do
117
}
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