001: /**********************************************************************
002: Copyright (c) 2004 Andy Jefferson and others. All rights reserved.
003: Licensed under the Apache License, Version 2.0 (the "License");
004: you may not use this file except in compliance with the License.
005: You may obtain a copy of the License at
006:
007: http://www.apache.org/licenses/LICENSE-2.0
008:
009: Unless required by applicable law or agreed to in writing, software
010: distributed under the License is distributed on an "AS IS" BASIS,
011: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: See the License for the specific language governing permissions and
013: limitations under the License.
014:
015: Contributors:
016: ...
017: **********************************************************************/package org.jpox.metadata;
018:
019: import org.jpox.util.StringUtils;
020:
021: /**
022: * Representation of the MetaData of a named Query.
023: *
024: * @since 1.1
025: * @version $Revision: 1.12 $
026: */
027: public class QueryMetaData extends MetaData {
028: /** Scope of this query (if any). */
029: protected String scope;
030:
031: /** Name of the query. */
032: protected final String name;
033:
034: /** Query language. */
035: protected QueryLanguage language;
036:
037: /** Whether the query is unmodifiable. */
038: protected boolean unmodifiable = false;
039:
040: /** The single string query */
041: protected String query;
042:
043: /** The result class to use. Only applies to SQL. */
044: protected String resultClass = null;
045:
046: /** Name for the MetaData defining the mapping of the result set (for JPA SQL). */
047: protected String resultMetaDataName = null;
048:
049: /** Whether the query returns unique. Only applies to SQL. */
050: protected boolean unique = false;
051:
052: /** Name of any fetch-plan to use. */
053: protected String fetchPlanName = null;
054:
055: /**
056: * Constructor.
057: * @param parent the parent of the Query
058: * @param scope Scope of the query (if any)
059: * @param name The Query name
060: * @param language The language name
061: * @param unmodifiable The unmodifiable tag
062: * @param resultClass The result class (optional)
063: * @param resultMetaDataName name of the result MetaData to use (optional)
064: * @param unique The unique tag
065: * @param fetchPlanName Name of any named fetchPlan to use with this query
066: */
067: public QueryMetaData(final MetaData parent, final String scope,
068: final String name, final String language,
069: final String unmodifiable, final String resultClass,
070: final String resultMetaDataName, final String unique,
071: final String fetchPlanName) {
072: super (parent);
073:
074: if (StringUtils.isWhitespace(name)) {
075: throw new InvalidMetaDataException(LOCALISER, "044154");
076: }
077:
078: this .scope = (StringUtils.isWhitespace(scope) ? null : scope);
079: this .name = name;
080:
081: // include-subclasses
082: if (unmodifiable != null
083: && unmodifiable.equalsIgnoreCase("true")) {
084: this .unmodifiable = true;
085: } else {
086: this .unmodifiable = false;
087: }
088:
089: // language
090: this .language = QueryLanguage.getQueryLanguage(language);
091:
092: this .resultClass = (StringUtils.isWhitespace(resultClass) ? null
093: : resultClass);
094: this .resultMetaDataName = resultMetaDataName;
095:
096: if (unique != null && unique.equalsIgnoreCase("true")) {
097: this .unique = true;
098: }
099: if (!StringUtils.isWhitespace(fetchPlanName)) {
100: this .fetchPlanName = fetchPlanName;
101: }
102: }
103:
104: /**
105: * Accessor for the scope of the query
106: * @return scope of the query (if any).
107: */
108: public String getScope() {
109: return scope;
110: }
111:
112: /**
113: * Accessor for the query name.
114: * @return query name
115: */
116: public String getName() {
117: return name;
118: }
119:
120: /**
121: * Accessor for the language
122: * @return language tag value
123: */
124: public QueryLanguage getLanguage() {
125: return language;
126: }
127:
128: /**
129: * Accessor for the unmodifiable tag value.
130: * @return unmodifiable tag value
131: */
132: public boolean isUnmodifiable() {
133: return unmodifiable;
134: }
135:
136: /**
137: * Accessor for the query
138: * @return The query
139: */
140: public String getQuery() {
141: return query;
142: }
143:
144: /**
145: * Accessor for the result class
146: * @return result class
147: */
148: public String getResultClass() {
149: return resultClass;
150: }
151:
152: /**
153: * Accessor for the name of the QueryResult MetaData to use.
154: * @return name of the QueryResult MetaData
155: */
156: public String getResultMetaDataName() {
157: return resultMetaDataName;
158: }
159:
160: /**
161: * Accessor for the unique tag value.
162: * @return unique tag value
163: */
164: public boolean isUnique() {
165: return unique;
166: }
167:
168: /**
169: * Mutator for the query
170: * @param query The query
171: */
172: public void setQuery(String query) {
173: this .query = query;
174: }
175:
176: /**
177: * Accessor for the name of any FetchPlan to use.
178: * @return name of the FetchPlan.
179: */
180: public String getFetchPlanName() {
181: return fetchPlanName;
182: }
183:
184: /**
185: * Returns a string representation of the object.
186: * @param prefix prefix string
187: * @param indent indent string
188: * @return a string representation of the object.
189: */
190: public String toString(String prefix, String indent) {
191: StringBuffer sb = new StringBuffer();
192: sb.append(prefix).append("<query name=\"" + name + "\"\n");
193: sb.append(prefix).append(
194: " language=\"" + language + "\"\n");
195: if (unique) {
196: sb.append(prefix).append(" unique=\"true\"\n");
197: }
198: if (resultClass != null) {
199: sb.append(prefix).append(
200: " result-class=\"" + resultClass + "\"\n");
201: }
202: if (fetchPlanName != null) {
203: sb.append(prefix).append(
204: " fetch-plan=\"" + fetchPlanName + "\"\n");
205: }
206: sb.append(prefix).append(
207: " unmodifiable=\"" + unmodifiable + "\">\n");
208: sb.append(prefix).append(query).append("\n");
209:
210: // Add extensions
211: sb.append(super .toString(prefix + indent, indent));
212:
213: sb.append(prefix + "</query>\n");
214: return sb.toString();
215: }
216: }
|