KickJava   Java API By Example, From Geeks To Geeks.

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


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;
21
22 import java.util.Collection JavaDoc;
23
24 import org.apache.cayenne.Fault;
25 import org.apache.cayenne.ValueHolder;
26
27 /**
28  * A generic superclass of CollectionProperty implementations.
29  *
30  * @since 1.2
31  * @author Andrus Adamchik
32  */

33 public abstract class BaseToManyProperty extends BaseArcProperty implements
34         ToManyProperty {
35
36     public BaseToManyProperty(ClassDescriptor owner, ClassDescriptor targetDescriptor,
37             Accessor accessor, String JavaDoc reverseName) {
38         super(owner, targetDescriptor, accessor, reverseName);
39     }
40
41     public Object JavaDoc readProperty(Object JavaDoc object) throws PropertyException {
42         return ensureCollectionValueHolderSet(object);
43     }
44
45     /**
46      * Wraps list in a value holder that performs lazy faulting.
47      */

48     public void writePropertyDirectly(Object JavaDoc object, Object JavaDoc oldValue, Object JavaDoc newValue)
49             throws PropertyException {
50
51         if (newValue instanceof Fault) {
52             super.writePropertyDirectly(object, null, newValue);
53         }
54         else {
55             // must resolve value holder...
56
ValueHolder holder = (ValueHolder) readProperty(object);
57             holder.setValueDirectly(newValue);
58         }
59     }
60
61     public void addTarget(Object JavaDoc source, Object JavaDoc target, boolean setReverse) {
62         if (target == null) {
63             throw new NullPointerException JavaDoc("Attempt to add null object.");
64         }
65
66         // TODO, Andrus, 2/9/2006 - CayenneDataObject differences:
67
// * invokes "willConnect"
68
// * has a callback to ObjectStore to handle flattened
69
// * has a callback to ObjectStore to retain snapshot
70
// * changes object state to modified
71

72         // Now do the rest of the normal handling (regardless of whether it was
73
// flattened or not)
74
Collection JavaDoc collection = (Collection JavaDoc) readProperty(source);
75         collection.add(target);
76
77         if (target != null && setReverse) {
78             setReverse(source, null, target);
79         }
80     }
81
82     public void removeTarget(Object JavaDoc source, Object JavaDoc target, boolean setReverse) {
83
84         // TODO, Andrus, 2/9/2006 - CayenneDataObject differences:
85
// * has a callback to ObjectStore to handle flattened
86
// * changes object state to modified
87

88         // Now do the rest of the normal handling (regardless of whether it was
89
// flattened or not)
90
Collection JavaDoc collection = (Collection JavaDoc) readProperty(source);
91         collection.remove(target);
92
93         if (target != null && setReverse) {
94             setReverse(source, target, null);
95         }
96     }
97
98     public boolean visit(PropertyVisitor visitor) {
99         return visitor.visitToMany(this);
100     }
101
102     /**
103      * Injects a List in the object if it hasn't been done yet.
104      */

105     public void injectValueHolder(Object JavaDoc object) throws PropertyException {
106         ensureCollectionValueHolderSet(object);
107     }
108
109     /**
110      * Checks that an object's List field described by this property is set, injecting a
111      * List if needed.
112      */

113     protected ValueHolder ensureCollectionValueHolderSet(Object JavaDoc object)
114             throws PropertyException {
115
116         Object JavaDoc value = accessor.getValue(object);
117
118         if (value == null || value instanceof Fault) {
119             value = createCollectionValueHolder(object);
120             accessor.setValue(object, value);
121         }
122
123         return (ValueHolder) value;
124     }
125
126     /**
127      * Creates a Collection for an object.
128      */

129     protected abstract ValueHolder createCollectionValueHolder(Object JavaDoc object)
130             throws PropertyException;
131 }
132
Popular Tags