KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > util > RelationshipFault


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.util;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.List JavaDoc;
25
26 import org.apache.cayenne.PersistenceState;
27 import org.apache.cayenne.Persistent;
28 import org.apache.cayenne.map.EntityResolver;
29 import org.apache.cayenne.map.ObjEntity;
30 import org.apache.cayenne.map.ObjRelationship;
31 import org.apache.cayenne.query.RelationshipQuery;
32 import org.apache.cayenne.reflect.Property;
33
34 /**
35  * An abstract superlcass of lazily faulted to-one and to-many relationships.
36  *
37  * @since 1.2
38  * @author Andrus Adamchik
39  */

40 public abstract class RelationshipFault {
41
42     protected Persistent relationshipOwner;
43     protected String JavaDoc relationshipName;
44
45     protected RelationshipFault() {
46
47     }
48
49     public RelationshipFault(Persistent relationshipOwner, String JavaDoc relationshipName) {
50         if (relationshipOwner == null) {
51             throw new NullPointerException JavaDoc("'relationshipOwner' can't be null.");
52         }
53
54         if (relationshipName == null) {
55             throw new NullPointerException JavaDoc("'relationshipName' can't be null.");
56         }
57
58         this.relationshipOwner = relationshipOwner;
59         this.relationshipName = relationshipName;
60     }
61
62     public String JavaDoc getRelationshipName() {
63         return relationshipName;
64     }
65
66     public Persistent getRelationshipOwner() {
67         return relationshipOwner;
68     }
69
70     protected boolean isTransientParent() {
71         int state = relationshipOwner.getPersistenceState();
72         return state == PersistenceState.NEW || state == PersistenceState.TRANSIENT;
73     }
74
75     protected boolean isUncommittedParent() {
76         int state = relationshipOwner.getPersistenceState();
77         return state == PersistenceState.MODIFIED || state == PersistenceState.DELETED;
78     }
79
80     /**
81      * Executes a query that returns related objects. Subclasses would invoke this method
82      * whenever they need to resolve a fault.
83      */

84     protected List JavaDoc resolveFromDB() {
85         // non-persistent objects shouldn't trigger a fetch
86
if (isTransientParent()) {
87             return new ArrayList JavaDoc();
88         }
89
90         List JavaDoc resolved = relationshipOwner.getObjectContext().performQuery(
91                 new RelationshipQuery(
92                         relationshipOwner.getObjectId(),
93                         relationshipName,
94                         false));
95
96         if (resolved.isEmpty()) {
97             return resolved;
98         }
99
100         // see if reverse relationship is to-one and we can connect source to results....
101

102         EntityResolver resolver = relationshipOwner
103                 .getObjectContext()
104                 .getEntityResolver();
105         ObjEntity sourceEntity = resolver.getObjEntity(relationshipOwner
106                 .getObjectId()
107                 .getEntityName());
108
109         ObjRelationship relationship = (ObjRelationship) sourceEntity
110                 .getRelationship(relationshipName);
111         ObjRelationship reverse = relationship.getReverseRelationship();
112
113         if (reverse != null && !reverse.isToMany()) {
114             Property property = resolver.getClassDescriptor(
115                     reverse.getSourceEntity().getName()).getProperty(reverse.getName());
116
117             Iterator JavaDoc it = resolved.iterator();
118             while (it.hasNext()) {
119                 property.writePropertyDirectly(it.next(), null, relationshipOwner);
120             }
121         }
122
123         return resolved;
124     }
125 }
126
Popular Tags