KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > access > DataContextFaults


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.access;
21
22 import java.util.List JavaDoc;
23
24 import org.apache.cayenne.CayenneRuntimeException;
25 import org.apache.cayenne.Fault;
26 import org.apache.cayenne.ObjectContext;
27 import org.apache.cayenne.PersistenceState;
28 import org.apache.cayenne.Persistent;
29 import org.apache.cayenne.query.RelationshipQuery;
30
31 /**
32  * A helper class that initializes server-side fault singletons.
33  *
34  * @since 1.2
35  * @author Andrus Adamchik
36  */

37 class DataContextFaults {
38
39     /**
40      * Resets super singletons.
41      */

42     static void init() {
43         ToManyFault.init();
44         ToOneFault.init();
45     }
46
47     final static class ToManyFault extends Fault {
48
49         /**
50          * Resets super singletons.
51          */

52         static void init() {
53             if (Fault.toManyFault == null) {
54                 Fault.toManyFault = new ToManyFault();
55             }
56         }
57
58         /**
59          * Resolves this fault to a List of objects.
60          */

61         public Object JavaDoc resolveFault(Persistent sourceObject, String JavaDoc relationshipName) {
62             return new ToManyList(sourceObject, relationshipName);
63         }
64     }
65
66     final static class ToOneFault extends Fault {
67
68         /**
69          * Resets super singletons.
70          */

71         static void init() {
72             if (Fault.toOneFault == null) {
73                 Fault.toOneFault = new ToOneFault();
74             }
75         }
76
77         /**
78          * Resolves this fault to a DataObject.
79          */

80         public Object JavaDoc resolveFault(Persistent sourceObject, String JavaDoc relationshipName) {
81             int state = sourceObject.getPersistenceState();
82             if(state == PersistenceState.NEW) {
83                 return null;
84             }
85             
86             Object JavaDoc target = doResolveFault(sourceObject, relationshipName);
87
88             // must update the diff for the object
89

90             ObjectContext context = sourceObject.getObjectContext();
91             if ((state == PersistenceState.MODIFIED || state == PersistenceState.DELETED)
92                     && context instanceof DataContext) {
93
94                 ObjectDiff diff = (ObjectDiff) ((DataContext) context)
95                         .getObjectStore()
96                         .getChangesByObjectId()
97                         .get(sourceObject.getObjectId());
98
99                 if (diff != null) {
100                     diff.updateArcSnapshot(relationshipName, (Persistent) target);
101                 }
102             }
103
104             return target;
105         }
106
107         Object JavaDoc doResolveFault(Persistent sourceObject, String JavaDoc relationshipName) {
108             RelationshipQuery query = new RelationshipQuery(
109                     sourceObject.getObjectId(),
110                     relationshipName,
111                     false);
112
113             List JavaDoc objects = sourceObject.getObjectContext().performQuery(query);
114
115             if (objects.isEmpty()) {
116                 return null;
117             }
118             else if (objects.size() == 1) {
119                 return objects.get(0);
120             }
121             else {
122                 throw new CayenneRuntimeException("Error resolving to-one fault. "
123                         + "More than one object found. "
124                         + "Source Id: "
125                         + sourceObject.getObjectId()
126                         + ", relationship: "
127                         + relationshipName);
128             }
129         }
130     }
131 }
132
Popular Tags