KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > CayenneContextQueryAction


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;
21
22 import java.util.Collection JavaDoc;
23 import java.util.Iterator JavaDoc;
24
25 import org.apache.cayenne.cache.QueryCacheEntryFactory;
26 import org.apache.cayenne.cache.QueryCache;
27 import org.apache.cayenne.query.Query;
28 import org.apache.cayenne.query.RefreshQuery;
29 import org.apache.cayenne.remote.RemoteIncrementalFaultList;
30 import org.apache.cayenne.util.ListResponse;
31 import org.apache.cayenne.util.ObjectContextQueryAction;
32
33 /**
34  * @since 1.2
35  * @author Andrus Adamchik
36  */

37 class CayenneContextQueryAction extends ObjectContextQueryAction {
38
39     CayenneContextQueryAction(CayenneContext actingContext, ObjectContext targetContext,
40             Query query) {
41         super(actingContext, targetContext, query);
42     }
43
44     public QueryResponse execute() {
45
46         if (interceptOIDQuery() != DONE) {
47             if (interceptRelationshipQuery() != DONE) {
48                 if (interceptRefreshQuery() != DONE) {
49                     if (interceptLocalCache() != DONE) {
50                         if (interceptPaginatedQuery() != DONE) {
51                             runQuery();
52                         }
53                     }
54                 }
55             }
56         }
57
58         interceptObjectConversion();
59         return response;
60     }
61
62     private boolean interceptPaginatedQuery() {
63         if (metadata.getPageSize() > 0) {
64             response = new ListResponse(new RemoteIncrementalFaultList(
65                     actingContext,
66                     query));
67             return DONE;
68         }
69
70         return !DONE;
71     }
72
73     protected QueryCache getQueryCache() {
74         return ((CayenneContext) actingContext).getQueryCache();
75     }
76
77     protected QueryCacheEntryFactory getCacheObjectFactory() {
78         return new QueryCacheEntryFactory() {
79
80             public Object JavaDoc createObject() {
81                 if (interceptPaginatedQuery() != DONE) {
82                     runQuery();
83                 }
84                 return response.firstList();
85             }
86         };
87     }
88
89     private boolean interceptRefreshQuery() {
90         if (query instanceof RefreshQuery) {
91             RefreshQuery refreshQuery = (RefreshQuery) query;
92
93             CayenneContext context = (CayenneContext) actingContext;
94
95             // handle 4 separate scenarious, but do not combine them as it will be
96
// unclear how to handle cascading behavior
97

98             // 1. refresh all
99
if (refreshQuery.isRefreshAll()) {
100
101                 invalidateLocally(context.internalGraphManager(), context
102                         .internalGraphManager()
103                         .registeredNodes()
104                         .iterator());
105                 context.getQueryCache().clear();
106
107                 // cascade
108
return !DONE;
109             }
110
111             // 2. invalidate object collection
112
Collection JavaDoc objects = refreshQuery.getObjects();
113             if (objects != null && !objects.isEmpty()) {
114
115                 invalidateLocally(context.internalGraphManager(), objects.iterator());
116
117                 // cascade
118
return !DONE;
119             }
120
121             // 3. refresh query - have to do it eagerly to refresh the objects involved
122
if (refreshQuery.getQuery() != null) {
123                 Query cachedQuery = refreshQuery.getQuery();
124
125                 String JavaDoc cacheKey = cachedQuery
126                         .getMetaData(context.getEntityResolver())
127                         .getCacheKey();
128                 context.getQueryCache().remove(cacheKey);
129
130                 this.response = context.performGenericQuery(cachedQuery);
131
132                 // do not cascade to avoid running query twice
133
return DONE;
134             }
135
136             // 4. refresh groups...
137
if (refreshQuery.getGroupKeys() != null
138                     && refreshQuery.getGroupKeys().length > 0) {
139
140                 String JavaDoc[] groups = refreshQuery.getGroupKeys();
141                 for (int i = 0; i < groups.length; i++) {
142                     context.getQueryCache().removeGroup(groups[i]);
143                 }
144
145                 // cascade group invalidation
146
return !DONE;
147             }
148         }
149
150         return !DONE;
151     }
152
153     private void invalidateLocally(CayenneContextGraphManager graphManager, Iterator JavaDoc it) {
154         while (it.hasNext()) {
155             Persistent object = (Persistent) it.next();
156
157             // we don't care about NEW objects,
158
// but we still do care about HOLLOW, since snapshot might still be
159
// present
160
if (object.getPersistenceState() == PersistenceState.NEW) {
161                 continue;
162             }
163
164             object.setPersistenceState(PersistenceState.HOLLOW);
165
166             // remove cached changes
167
graphManager.changeLog.unregisterNode(object.getObjectId());
168             graphManager.stateLog.unregisterNode(object.getObjectId());
169         }
170     }
171 }
172
Popular Tags