KickJava   Java API By Example, From Geeks To Geeks.

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


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.query;
21
22 import java.util.HashMap JavaDoc;
23 import java.util.Map JavaDoc;
24
25 import org.apache.cayenne.map.DataMap;
26 import org.apache.cayenne.map.DbEntity;
27 import org.apache.cayenne.map.ObjEntity;
28 import org.apache.cayenne.map.Procedure;
29 import org.apache.cayenne.reflect.ClassDescriptor;
30
31 /**
32  * A wrapper for a QueryMetadata instance allowing that may override a subset of metadata
33  * properties.
34  *
35  * @since 1.2
36  * @author Andrus Adamchik
37  */

38 class QueryMetadataWrapper implements QueryMetadata {
39
40     static final String JavaDoc CACHE_KEY_PROPERTY = "QueryMetadataWrapper.CacheKey";
41
42     QueryMetadata info;
43     Map JavaDoc overrides;
44
45     public QueryMetadataWrapper(QueryMetadata info) {
46         this.info = info;
47     }
48
49     /**
50      * Overrides a property with an alternative value. Property names are defined in the
51      * {@link QueryMetadata} interface.
52      */

53     void override(String JavaDoc key, Object JavaDoc value) {
54         if (overrides == null) {
55             overrides = new HashMap JavaDoc();
56         }
57
58         overrides.put(key, value);
59     }
60
61     boolean overrideExists(String JavaDoc key) {
62         return overrides != null && overrides.containsKey(key);
63     }
64     
65     /**
66      * @since 3.0
67      */

68     public SQLResultSetMapping getResultSetMapping() {
69         return info.getResultSetMapping();
70     }
71
72     public DataMap getDataMap() {
73         return info.getDataMap();
74     }
75
76     public Procedure getProcedure() {
77         return info.getProcedure();
78     }
79
80     public DbEntity getDbEntity() {
81         return info.getDbEntity();
82     }
83
84     public ObjEntity getObjEntity() {
85         return info.getObjEntity();
86     }
87     
88     /**
89      * @since 3.0
90      */

91     public ClassDescriptor getClassDescriptor() {
92         return info.getClassDescriptor();
93     }
94
95     public String JavaDoc getCacheKey() {
96         return (overrideExists(CACHE_KEY_PROPERTY)) ? (String JavaDoc) overrides
97                 .get(CACHE_KEY_PROPERTY) : info.getCacheKey();
98     }
99
100     public String JavaDoc getCachePolicy() {
101         return (overrideExists(QueryMetadata.CACHE_POLICY_PROPERTY)) ? (String JavaDoc) overrides
102                 .get(QueryMetadata.CACHE_POLICY_PROPERTY) : info.getCachePolicy();
103     }
104     
105     public String JavaDoc[] getCacheGroups() {
106         return (overrideExists(QueryMetadata.CACHE_GROUPS_PROPERTY))
107                 ? (String JavaDoc[]) overrides.get(QueryMetadata.CACHE_GROUPS_PROPERTY)
108                 : info.getCacheGroups();
109     }
110
111     public boolean isFetchingDataRows() {
112         if (!overrideExists(QueryMetadata.FETCHING_DATA_ROWS_PROPERTY)) {
113             return info.isFetchingDataRows();
114         }
115
116         Boolean JavaDoc b = (Boolean JavaDoc) overrides.get(QueryMetadata.FETCHING_DATA_ROWS_PROPERTY);
117         return b != null && b.booleanValue();
118     }
119
120     public boolean isRefreshingObjects() {
121         if (!overrideExists(QueryMetadata.REFRESHING_OBJECTS_PROPERTY)) {
122             return info.isRefreshingObjects();
123         }
124
125         Boolean JavaDoc b = (Boolean JavaDoc) overrides.get(QueryMetadata.REFRESHING_OBJECTS_PROPERTY);
126         return b != null && b.booleanValue();
127     }
128
129     public boolean isResolvingInherited() {
130         if (!overrideExists(QueryMetadata.RESOLVING_INHERITED_PROPERTY)) {
131             return info.isResolvingInherited();
132         }
133
134         Boolean JavaDoc b = (Boolean JavaDoc) overrides.get(QueryMetadata.RESOLVING_INHERITED_PROPERTY);
135         return b != null && b.booleanValue();
136     }
137
138     public int getPageSize() {
139         if (!overrideExists(QueryMetadata.PAGE_SIZE_PROPERTY)) {
140             return info.getPageSize();
141         }
142
143         Number JavaDoc n = (Number JavaDoc) overrides.get(QueryMetadata.PAGE_SIZE_PROPERTY);
144         return n != null ? n.intValue() : 0;
145     }
146     
147     public int getFetchStartIndex() {
148         return info.getFetchStartIndex();
149     }
150
151     public int getFetchLimit() {
152         if (!overrideExists(QueryMetadata.FETCH_LIMIT_PROPERTY)) {
153             return info.getFetchLimit();
154         }
155
156         Number JavaDoc n = (Number JavaDoc) overrides.get(QueryMetadata.FETCH_LIMIT_PROPERTY);
157         return n != null ? n.intValue() : 0;
158     }
159
160     public PrefetchTreeNode getPrefetchTree() {
161         return info.getPrefetchTree();
162     }
163 }
164
Popular Tags