KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > query > RefreshQuery


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 package org.apache.cayenne.query;
20
21 import java.util.Arrays JavaDoc;
22 import java.util.Collection JavaDoc;
23
24 import org.apache.cayenne.CayenneRuntimeException;
25 import org.apache.cayenne.Persistent;
26 import org.apache.cayenne.map.EntityResolver;
27
28 /**
29  * A query that allows to clear both object and list caches either via refetch (eager
30  * refresh) or invalidate (lazy refresh).
31  *
32  * @since 3.0
33  * @author Andrus Adamchik
34  */

35 public class RefreshQuery implements Query {
36
37     protected Collection JavaDoc objects;
38     protected Query query;
39     protected String JavaDoc[] groupKeys;
40
41     /**
42      * Creates a RefreshQuery that does full refresh of all registered objects, cascading
43      * refresh all the way to the shared cache.
44      */

45     public RefreshQuery() {
46
47     }
48
49     /**
50      * Creates a RefreshQuery that refreshes a collection of objects, including
51      * invalidation of their relationships.
52      */

53     public RefreshQuery(Collection JavaDoc objects) {
54         this.objects = objects;
55     }
56
57     /**
58      * Creates a RefreshQuery that refreshes a single object, including invalidation of
59      * its relationships.
60      */

61     public RefreshQuery(Persistent object) {
62         this(Arrays.asList(new Object JavaDoc[] {
63             object
64         }));
65     }
66
67     /**
68      * Creates a RefreshQuery that refreshes results of a query and individual objects in
69      * the result.
70      */

71     public RefreshQuery(Query query) {
72         this.query = query;
73     }
74
75     /**
76      * Creates a RefreshQuery that refreshes query results identified by group keys.
77      */

78     public RefreshQuery(String JavaDoc[] groupKeys) {
79         this.groupKeys = groupKeys;
80     }
81
82     public QueryMetadata getMetaData(EntityResolver resolver) {
83         return new BaseQueryMetadata();
84     }
85
86     public String JavaDoc getName() {
87         return null;
88     }
89
90     public void route(QueryRouter router, EntityResolver resolver, Query substitutedQuery) {
91         // noop
92
}
93
94     public SQLAction createSQLAction(SQLActionVisitor visitor) {
95         throw new CayenneRuntimeException("Unsupported operation");
96     }
97
98     public boolean isRefreshAll() {
99         return objects == null && query == null && groupKeys == null;
100     }
101
102     public String JavaDoc[] getGroupKeys() {
103         return groupKeys;
104     }
105
106     public Collection JavaDoc getObjects() {
107         return objects;
108     }
109
110     /**
111      * Returns an internal query, overriding cache policy to force a refresh. Returns null
112      * if no query was set.
113      */

114     public Query getQuery() {
115
116         if (query == null) {
117             return null;
118         }
119
120         return new Query() {
121
122             public SQLAction createSQLAction(SQLActionVisitor visitor) {
123                 throw new CayenneRuntimeException("Unsupported");
124             }
125
126             public QueryMetadata getMetaData(EntityResolver resolver) {
127                 QueryMetadata md = query.getMetaData(resolver);
128
129                 QueryMetadataWrapper wrappedMd = new QueryMetadataWrapper(md);
130                 if (QueryMetadata.LOCAL_CACHE.equals(md.getCachePolicy())) {
131                     wrappedMd.override(
132                             QueryMetadata.CACHE_POLICY_PROPERTY,
133                             QueryMetadata.LOCAL_CACHE_REFRESH);
134                 }
135                 else if (QueryMetadata.SHARED_CACHE.equals(md.getCachePolicy())) {
136                     wrappedMd.override(
137                             QueryMetadata.CACHE_POLICY_PROPERTY,
138                             QueryMetadata.SHARED_CACHE_REFRESH);
139                 }
140
141                 return wrappedMd;
142             }
143
144             public String JavaDoc getName() {
145                 return query.getName();
146             }
147
148             public void route(
149                     QueryRouter router,
150                     EntityResolver resolver,
151                     Query substitutedQuery) {
152                 query.route(router, resolver, this);
153             }
154         };
155     }
156 }
157
Popular Tags