KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectstyle > cayenne > query > SelectExecutionProperties


1 /* ====================================================================
2  *
3  * The ObjectStyle Group Software License, version 1.1
4  * ObjectStyle Group - http://objectstyle.org/
5  *
6  * Copyright (c) 2002-2005, Andrei (Andrus) Adamchik and individual authors
7  * of the software. All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution, if any,
22  * must include the following acknowlegement:
23  * "This product includes software developed by independent contributors
24  * and hosted on ObjectStyle Group web site (http://objectstyle.org/)."
25  * Alternately, this acknowlegement may appear in the software itself,
26  * if and wherever such third-party acknowlegements normally appear.
27  *
28  * 4. The names "ObjectStyle Group" and "Cayenne" must not be used to endorse
29  * or promote products derived from this software without prior written
30  * permission. For written permission, email
31  * "andrus at objectstyle dot org".
32  *
33  * 5. Products derived from this software may not be called "ObjectStyle"
34  * or "Cayenne", nor may "ObjectStyle" or "Cayenne" appear in their
35  * names without prior written permission.
36  *
37  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40  * DISCLAIMED. IN NO EVENT SHALL THE OBJECTSTYLE GROUP OR
41  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48  * SUCH DAMAGE.
49  * ====================================================================
50  *
51  * This software consists of voluntary contributions made by many
52  * individuals and hosted on ObjectStyle Group web site. For more
53  * information on the ObjectStyle Group, please see
54  * <http://objectstyle.org/>.
55  */

56 package org.objectstyle.cayenne.query;
57
58 import java.io.Serializable JavaDoc;
59 import java.util.Collection JavaDoc;
60 import java.util.Collections JavaDoc;
61 import java.util.HashSet JavaDoc;
62 import java.util.Map JavaDoc;
63 import java.util.Set JavaDoc;
64
65 import org.objectstyle.cayenne.util.XMLEncoder;
66 import org.objectstyle.cayenne.util.XMLSerializable;
67
68 /**
69  * Helper class that holds parameters for classes implementing {@link GenericSelectQuery}
70  * interface.
71  *
72  * @author Andrei Adamchik
73  * @since 1.1
74  */

75 final class SelectExecutionProperties implements XMLSerializable, Serializable JavaDoc {
76
77     int fetchLimit = GenericSelectQuery.FETCH_LIMIT_DEFAULT;
78     int pageSize = GenericSelectQuery.PAGE_SIZE_DEFAULT;
79     boolean fetchingDataRows = GenericSelectQuery.FETCHING_DATA_ROWS_DEFAULT;
80     boolean refreshingObjects = GenericSelectQuery.REFRESHING_OBJECTS_DEFAULT;
81     boolean resolvingInherited = GenericSelectQuery.RESOLVING_INHERITED_DEFAULT;
82     String JavaDoc cachePolicy = GenericSelectQuery.CACHE_POLICY_DEFAULT;
83     Set JavaDoc jointPrefetches;
84
85     /**
86      * Copies values of this object to another SelectQueryProperties object.
87      */

88     void copyToProperties(SelectExecutionProperties anotherProperties) {
89         anotherProperties.fetchingDataRows = this.fetchingDataRows;
90         anotherProperties.fetchLimit = this.fetchLimit;
91         anotherProperties.pageSize = this.pageSize;
92         anotherProperties.refreshingObjects = this.refreshingObjects;
93         anotherProperties.resolvingInherited = this.resolvingInherited;
94         anotherProperties.cachePolicy = this.cachePolicy;
95         anotherProperties.jointPrefetches = (this.jointPrefetches != null) ? new HashSet JavaDoc(
96                 jointPrefetches) : null;
97     }
98
99     void initWithProperties(Map JavaDoc properties) {
100         // must init defaults even if properties are empty
101
if (properties == null) {
102             properties = Collections.EMPTY_MAP;
103         }
104
105         Object JavaDoc fetchLimit = properties.get(GenericSelectQuery.FETCH_LIMIT_PROPERTY);
106         Object JavaDoc pageSize = properties.get(GenericSelectQuery.PAGE_SIZE_PROPERTY);
107         Object JavaDoc refreshingObjects = properties
108                 .get(GenericSelectQuery.REFRESHING_OBJECTS_PROPERTY);
109         Object JavaDoc fetchingDataRows = properties
110                 .get(GenericSelectQuery.FETCHING_DATA_ROWS_PROPERTY);
111
112         Object JavaDoc resolvingInherited = properties
113                 .get(GenericSelectQuery.RESOLVING_INHERITED_PROPERTY);
114
115         Object JavaDoc cachePolicy = properties.get(GenericSelectQuery.CACHE_POLICY_PROPERTY);
116
117         // init ivars from properties
118
this.fetchLimit = (fetchLimit != null)
119                 ? Integer.parseInt(fetchLimit.toString())
120                 : GenericSelectQuery.FETCH_LIMIT_DEFAULT;
121
122         this.pageSize = (pageSize != null)
123                 ? Integer.parseInt(pageSize.toString())
124                 : GenericSelectQuery.PAGE_SIZE_DEFAULT;
125
126         this.refreshingObjects = (refreshingObjects != null)
127                 ? "true".equalsIgnoreCase(refreshingObjects.toString())
128                 : GenericSelectQuery.REFRESHING_OBJECTS_DEFAULT;
129
130         this.fetchingDataRows = (fetchingDataRows != null)
131                 ? "true".equalsIgnoreCase(fetchingDataRows.toString())
132                 : GenericSelectQuery.FETCHING_DATA_ROWS_DEFAULT;
133
134         this.resolvingInherited = (resolvingInherited != null)
135                 ? "true".equalsIgnoreCase(resolvingInherited.toString())
136                 : GenericSelectQuery.RESOLVING_INHERITED_DEFAULT;
137
138         this.cachePolicy = (cachePolicy != null)
139                 ? cachePolicy.toString()
140                 : GenericSelectQuery.CACHE_POLICY_DEFAULT;
141     }
142
143     public void encodeAsXML(XMLEncoder encoder) {
144         if (refreshingObjects != GenericSelectQuery.REFRESHING_OBJECTS_DEFAULT) {
145             encoder.printProperty(GenericSelectQuery.REFRESHING_OBJECTS_PROPERTY,
146                     refreshingObjects);
147         }
148
149         if (fetchingDataRows != GenericSelectQuery.FETCHING_DATA_ROWS_DEFAULT) {
150             encoder.printProperty(GenericSelectQuery.FETCHING_DATA_ROWS_PROPERTY,
151                     fetchingDataRows);
152         }
153
154         if (resolvingInherited != GenericSelectQuery.RESOLVING_INHERITED_DEFAULT) {
155             encoder.printProperty(GenericSelectQuery.RESOLVING_INHERITED_PROPERTY,
156                     resolvingInherited);
157         }
158
159         if (fetchLimit != GenericSelectQuery.FETCH_LIMIT_DEFAULT) {
160             encoder.printProperty(GenericSelectQuery.FETCH_LIMIT_PROPERTY, fetchLimit);
161         }
162
163         if (pageSize != GenericSelectQuery.PAGE_SIZE_DEFAULT) {
164             encoder.printProperty(GenericSelectQuery.PAGE_SIZE_PROPERTY, pageSize);
165         }
166
167         if (cachePolicy != null
168                 && !GenericSelectQuery.CACHE_POLICY_DEFAULT.equals(cachePolicy)) {
169             encoder.printProperty(GenericSelectQuery.CACHE_POLICY_PROPERTY, cachePolicy);
170         }
171     }
172
173     String JavaDoc getCachePolicy() {
174         return cachePolicy;
175     }
176
177     void setCachePolicy(String JavaDoc policy) {
178         this.cachePolicy = policy;
179     }
180
181     boolean isFetchingDataRows() {
182         return fetchingDataRows;
183     }
184
185     int getFetchLimit() {
186         return fetchLimit;
187     }
188
189     int getPageSize() {
190         return pageSize;
191     }
192
193     boolean isRefreshingObjects() {
194         return refreshingObjects;
195     }
196
197     boolean isResolvingInherited() {
198         return resolvingInherited;
199     }
200
201     void setFetchingDataRows(boolean b) {
202         fetchingDataRows = b;
203     }
204
205     void setFetchLimit(int i) {
206         fetchLimit = i;
207     }
208
209     void setPageSize(int i) {
210         pageSize = i;
211     }
212
213     void setRefreshingObjects(boolean b) {
214         refreshingObjects = b;
215     }
216
217     void setResolvingInherited(boolean b) {
218         resolvingInherited = b;
219     }
220
221     /**
222      * Returns a collection that internally stores join prefetches, creating it on demand.
223      *
224      * @since 1.2
225      */

226     Collection JavaDoc nonNullJointPrefetches() {
227         if (jointPrefetches == null) {
228             jointPrefetches = new HashSet JavaDoc();
229         }
230
231         return jointPrefetches;
232     }
233
234     /**
235      * Returns a collection of joint prefetches.
236      *
237      * @since 1.2
238      */

239     Collection JavaDoc getJointPrefetches() {
240         return (jointPrefetches != null) ? jointPrefetches : Collections.EMPTY_SET;
241     }
242
243     /**
244      * Adds a joint prefetch.
245      *
246      * @since 1.2
247      */

248     void addJointPrefetch(String JavaDoc relationshipPath) {
249         nonNullJointPrefetches().add(relationshipPath);
250     }
251
252     /**
253      * Adds all joint prefetches from a provided collection.
254      *
255      * @since 1.2
256      */

257     void addJointPrefetches(Collection JavaDoc relationshipPaths) {
258         nonNullJointPrefetches().addAll(relationshipPaths);
259     }
260
261     /**
262      * Clears all joint prefetches.
263      *
264      * @since 1.2
265      */

266     void clearJointPrefetches() {
267         jointPrefetches = null;
268     }
269
270     /**
271      * Removes joint prefetch.
272      *
273      * @since 1.2
274      */

275     void removeJointPrefetch(String JavaDoc relationshipPath) {
276         if (jointPrefetches != null) {
277             jointPrefetches.remove(relationshipPath);
278         }
279     }
280 }
Popular Tags