001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019: package org.apache.openjpa.meta;
020:
021: import java.io.File;
022: import java.io.Serializable;
023: import java.util.LinkedList;
024: import java.util.List;
025:
026: import org.apache.commons.lang.StringUtils;
027: import org.apache.openjpa.kernel.Query;
028: import org.apache.openjpa.lib.meta.SourceTracker;
029: import org.apache.openjpa.lib.xml.Commentable;
030:
031: /**
032: * Holds metadata about named queries.
033: * Information stored in this instance gets transfered to
034: * new {@link Query} instances.
035: *
036: * @author Steve Kim
037: */
038: public class QueryMetaData implements MetaDataModes, SourceTracker,
039: Commentable, Serializable {
040:
041: private static final String[] EMPTY_KEYS = new String[0];
042: private static final Object[] EMPTY_VALS = new Object[0];
043:
044: private final String _name;
045: private Boolean _readOnly;
046: private File _file;
047: private Object _scope;
048: private int _srcType;
049: private int _mode = MODE_QUERY;
050: private String _language;
051: private Class _class;
052: private Class _candidate;
053: private Class _res;
054: private String _query;
055: private String[] _comments;
056: private List _hintKeys;
057: private List _hintVals;
058: private String _resultSetMappingName;
059:
060: /**
061: * Construct with the given name.
062: */
063: protected QueryMetaData(String name) {
064: _name = name;
065: }
066:
067: /**
068: * Return the name for this query.
069: */
070: public String getName() {
071: return _name;
072: }
073:
074: /**
075: * The class that defines this query, or null if none.
076: */
077: public Class getDefiningType() {
078: return _class;
079: }
080:
081: /**
082: * The class that defines this query, or null if none.
083: */
084: public void setDefiningType(Class cls) {
085: _class = cls;
086: }
087:
088: /**
089: * Whether the query has been marked read-only.
090: */
091: public boolean isReadOnly() {
092: return _readOnly != null && _readOnly.booleanValue();
093: }
094:
095: /**
096: * Whether the query has been marked read-only.
097: */
098: public void setReadOnly(boolean readOnly) {
099: _readOnly = (readOnly) ? Boolean.TRUE : Boolean.FALSE;
100: }
101:
102: /**
103: * The query candidate class, or null if none.
104: */
105: public Class getCandidateType() {
106: return _candidate;
107: }
108:
109: /**
110: * The query result class, or null if none.
111: */
112: public void setCandidateType(Class cls) {
113: _candidate = cls;
114: }
115:
116: /**
117: * The query result class, or null if none.
118: */
119: public Class getResultType() {
120: return _res;
121: }
122:
123: /**
124: * The query result class, or null if none.
125: */
126: public void setResultType(Class cls) {
127: _res = cls;
128: }
129:
130: /**
131: * Return the query language.
132: */
133: public String getLanguage() {
134: return _language;
135: }
136:
137: /**
138: * Set the language for this query.
139: */
140: public void setLanguage(String language) {
141: _language = language;
142: }
143:
144: /**
145: * The full query string, or null if none.
146: */
147: public String getQueryString() {
148: return _query;
149: }
150:
151: /**
152: * The full query string, or null if none.
153: */
154: public void setQueryString(String query) {
155: _query = query;
156: }
157:
158: /**
159: * Query hints.
160: */
161: public String[] getHintKeys() {
162: return (_hintKeys == null) ? EMPTY_KEYS : (String[]) _hintKeys
163: .toArray(new String[_hintKeys.size()]);
164: }
165:
166: /**
167: * Query hints.
168: */
169: public Object[] getHintValues() {
170: return (_hintVals == null) ? EMPTY_VALS : _hintVals.toArray();
171: }
172:
173: /**
174: * Add a query hint.
175: */
176: public void addHint(String key, Object value) {
177: if (_hintKeys == null) {
178: _hintKeys = new LinkedList();
179: _hintVals = new LinkedList();
180: }
181: _hintKeys.add(key);
182: _hintVals.add(value);
183: }
184:
185: public String getResultSetMappingName() {
186: return _resultSetMappingName;
187: }
188:
189: public void setResultSetMappingName(String setMappingName) {
190: _resultSetMappingName = setMappingName;
191: }
192:
193: /**
194: * Set query template information into the given concrete
195: * query instance. However, the language, query string, and
196: * candidate class are assumed to be declared in the query
197: * instantiation, and hints are not transferred.
198: */
199: public void setInto(Query query) {
200: if (_candidate != null)
201: query.setCandidateType(_candidate, true);
202: if (!StringUtils.isEmpty(_query))
203: query.setQuery(_query);
204: if (_res != null)
205: query.setResultType(_res);
206: if (_readOnly != null)
207: query.setReadOnly(_readOnly.booleanValue());
208: if (_resultSetMappingName != null)
209: query.setResultMapping(null, _resultSetMappingName);
210: }
211:
212: /**
213: * Initialize this instance from the values held in the
214: * specified {@link Query}.
215: */
216: public void setFrom(Query query) {
217: _language = query.getLanguage();
218: _candidate = query.getCandidateType();
219: _res = query.getResultType();
220: _query = query.getQueryString();
221: }
222:
223: /**
224: * The source mode of this query.
225: */
226: public int getSourceMode() {
227: return _mode;
228: }
229:
230: /**
231: * The source mode of this query.
232: */
233: public void setSourceMode(int mode) {
234: _mode = mode;
235: }
236:
237: public String toString() {
238: return _name;
239: }
240:
241: ///////////////
242: // Commentable
243: ///////////////
244:
245: public String[] getComments() {
246: return (_comments == null) ? EMPTY_COMMENTS : _comments;
247: }
248:
249: public void setComments(String[] comments) {
250: _comments = comments;
251: }
252:
253: ////////////////////////////////
254: // SourceTracker implementation
255: ////////////////////////////////
256:
257: public File getSourceFile() {
258: return _file;
259: }
260:
261: public Object getSourceScope() {
262: return _scope;
263: }
264:
265: public int getSourceType() {
266: return _srcType;
267: }
268:
269: public void setSource(File file, Object scope, int srcType) {
270: _file = file;
271: _scope = scope;
272: _srcType = srcType;
273: }
274:
275: public String getResourceName() {
276: return (_class == null) ? _name : _class.getName() + ":"
277: + _name;
278: }
279: }
|