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:
016: Contributors:
017: 2004 Kikuchi Kousuke - org.jpox.enhancer.conf.JDOConfig
018: ...
019: **********************************************************************/package org.jpox.metadata;
020:
021: import java.util.ArrayList;
022: import java.util.Collection;
023: import java.util.HashSet;
024: import java.util.Iterator;
025: import java.util.List;
026:
027: /**
028: * Representation of a Meta-Data file.
029: * Contains a list of package meta-data, together with any named queries and fetch plans etc.
030: *
031: * @since 1.1
032: * @version $Revision: 1.24 $
033: */
034: public class FileMetaData extends MetaData {
035: /** JDO file **/
036: public static final int JDO_FILE = 1;
037: /** ORM file **/
038: public static final int ORM_FILE = 2;
039: /** JDOQUERY file **/
040: public static final int JDOQUERY_FILE = 3;
041: /** annotations **/
042: public static final int ANNOTATIONS = 4;
043: /** JPA file **/
044: public static final int JPA_FILE = 5;
045:
046: /** Manager for this MetaData object. */
047: transient protected final MetaDataManager metaDataManager;
048:
049: /** Type of file (JDO, ORM, JDOQUERY, etc) */
050: protected int type;
051:
052: /** Name of file */
053: protected String filename;
054:
055: /** Catalog name for all classes in this file */
056: protected String catalog;
057:
058: /** Schema name for all classes in this file */
059: protected String schema;
060:
061: /** Named queries defined in this file. */
062: protected Collection queries = null;
063:
064: /** List of query result MetaData defined for this file. */
065: protected Collection queryResultMetaData = null;
066:
067: /** Named FetchPlans in this file. */
068: protected Collection fetchPlans = null;
069:
070: /** List of packages in this file (uses List to retain file positioning) */
071: protected List packages = null;
072:
073: /** List of event listeners defined for this file. */
074: protected List listeners = null;
075:
076: /**
077: * Constructor.
078: * @param filename The file where this is stored (or null).
079: * @param metaDataManager the manager for this MetaData object.
080: * @param catalog Name of the catalog for all classes in this file
081: * @param schema Name of the schema for all classes in this file
082: */
083: public FileMetaData(String filename,
084: MetaDataManager metaDataManager, String catalog,
085: String schema) {
086: super (null);
087:
088: this .filename = filename;
089: this .metaDataManager = metaDataManager;
090: }
091:
092: // ------------------------------ Accessors --------------------------------
093:
094: /**
095: * Accessor for the file type (JDO, ORM, etc)
096: * @return The file type
097: */
098: public int getType() {
099: return type;
100: }
101:
102: /**
103: * Accessor for the number of named queries.
104: * @return no of named queries
105: */
106: public int getNoOfQueries() {
107: return queries != null ? queries.size() : 0;
108: }
109:
110: /**
111: * Accessor for the metadata of the named queries.
112: * @return Meta-Data for the named queries.
113: */
114: public QueryMetaData[] getQueries() {
115: return (queries == null ? null : (QueryMetaData[]) queries
116: .toArray(new QueryMetaData[queries.size()]));
117: }
118:
119: /**
120: * Accessor for the number of named fetch plans.
121: * @return no of named fetch plans
122: */
123: public int getNoOfFetchPlans() {
124: return fetchPlans != null ? fetchPlans.size() : 0;
125: }
126:
127: /**
128: * Accessor for the metadata of the named fetch plans.
129: * @return Meta-Data for the named fetch plans.
130: */
131: public FetchPlanMetaData[] getFetchPlans() {
132: return (fetchPlans == null ? null
133: : (FetchPlanMetaData[]) fetchPlans
134: .toArray(new FetchPlanMetaData[fetchPlans
135: .size()]));
136: }
137:
138: /**
139: * Accessor for the MetaDataManager for this file.
140: * @return The MetaDataManager for this file.
141: */
142: public MetaDataManager getMetaDataManager() {
143: return metaDataManager;
144: }
145:
146: /**
147: * Accessor for the filename
148: * @return The filename of this MetaData file.
149: */
150: public String getFilename() {
151: return filename;
152: }
153:
154: /**
155: * Accessor for the catalog name for all classes in this file
156: * @return Name of the catalog to use.
157: */
158: public String getCatalog() {
159: return catalog;
160: }
161:
162: /**
163: * Accessor for the schema name for all classes in this file
164: * @return Name of the schema to use
165: */
166: public String getSchema() {
167: return schema;
168: }
169:
170: /**
171: * Accessor for the number of packages.
172: * @return no of packages.
173: */
174: public int getNoOfPackages() {
175: return packages != null ? packages.size() : 0;
176: }
177:
178: /**
179: * Accessor for the meta-data of a package.
180: * @param i index number
181: * @return Meta-Data for a package.
182: */
183: public PackageMetaData getPackage(int i) {
184: return (PackageMetaData) packages.get(i);
185: }
186:
187: /**
188: * Accessor for the Meta-Data of a package with a given name.
189: * @param name Name of the package
190: * @return Meta-Data for the package
191: */
192: public PackageMetaData getPackage(String name) {
193: Iterator iter = packages.iterator();
194: while (iter.hasNext()) {
195: PackageMetaData p = (PackageMetaData) iter.next();
196: if (p.name.equals(name)) {
197: return p;
198: }
199: }
200: return null;
201: }
202:
203: /**
204: * Utility method to check if the MetaData for a class is contained in this
205: * file.
206: * @param pkg_name Name of package
207: * @param class_name Name of class
208: * @return The MetaData for the class
209: */
210: public ClassMetaData getClass(String pkg_name, String class_name) {
211: if (pkg_name == null || class_name == null) {
212: return null;
213: }
214:
215: PackageMetaData pmd = getPackage(pkg_name);
216: if (pmd != null) {
217: return pmd.getClass(class_name);
218: }
219:
220: return null;
221: }
222:
223: // -------------------------------- Mutators -------------------------------
224:
225: /**
226: * Mutator for the file type.
227: * @param type The file type
228: */
229: public void setType(int type) {
230: this .type = type;
231: }
232:
233: /**
234: * Method to add a named query to this class. Rejects the addition of
235: * duplicate named queries.
236: * @param qmd Meta-Data for the query.
237: */
238: public void addQuery(QueryMetaData qmd) {
239: if (qmd == null) {
240: return;
241: }
242: if (queries == null) {
243: queries = new HashSet();
244: }
245:
246: queries.add(qmd);
247: }
248:
249: /**
250: * Method to add a named fetch plan to this class.
251: * Rejects the addition of duplicate named fetch plans.
252: * @param fpmd Meta-Data for the fetch plan.
253: */
254: public void addFetchPlan(FetchPlanMetaData fpmd) {
255: if (fpmd == null) {
256: return;
257: }
258: if (fetchPlans == null) {
259: fetchPlans = new HashSet();
260: }
261:
262: fetchPlans.add(fpmd);
263: }
264:
265: /**
266: * Mutator for the filename for this MetaData file.
267: * @param filename The filename of this MetaData file.
268: */
269: public void setFilename(String filename) {
270: this .filename = filename;
271: }
272:
273: /**
274: * Mutator for the catalog for all classes in this file
275: * @param catalog Catalog name to use
276: */
277: public void setCatalog(String catalog) {
278: this .catalog = catalog;
279: }
280:
281: /**
282: * Mutator for the schema for all classes in this file
283: * @param schema Schema name to use
284: */
285: public void setSchema(String schema) {
286: this .schema = schema;
287: }
288:
289: /**
290: * Method to add a package
291: * @param pkg The PackageMetaData to add.
292: **/
293: public void addPackage(PackageMetaData pkg) {
294: if (pkg == null) {
295: return;
296: }
297: if (packages == null) {
298: packages = new ArrayList();
299: } else {
300: // Check if already exists
301: Iterator iter = packages.iterator();
302: while (iter.hasNext()) {
303: PackageMetaData p = (PackageMetaData) iter.next();
304: if (pkg.getName().equals(p.getName())) {
305: return;
306: }
307: }
308: }
309: packages.add(pkg);
310: }
311:
312: // -------------------------------- Utilities ------------------------------
313:
314: /**
315: * Returns a string representation of the object.
316: * @param indent The indent
317: * @return a string representation of the object.
318: */
319: public String toString(String indent) {
320: if (indent == null) {
321: indent = "";
322: }
323:
324: StringBuffer sb = new StringBuffer();
325: sb.append("<jdo");
326: if (catalog != null) {
327: sb.append(" catalog=\"" + catalog + "\"");
328: }
329: if (schema != null) {
330: sb.append(" schema=\"" + schema + "\"");
331: }
332: sb.append(">\n");
333:
334: // Add packages
335: if (packages != null) {
336: Iterator iter = packages.iterator();
337: while (iter.hasNext()) {
338: sb.append(((PackageMetaData) iter.next()).toString(
339: indent, indent));
340: }
341: }
342:
343: // Add queries
344: if (queries != null) {
345: Iterator iter = queries.iterator();
346: while (iter.hasNext()) {
347: sb.append(((QueryMetaData) iter.next()).toString(
348: indent, indent));
349: }
350: }
351:
352: // Add fetch plans
353: if (fetchPlans != null) {
354: Iterator iter = fetchPlans.iterator();
355: while (iter.hasNext()) {
356: sb.append(((FetchPlanMetaData) iter.next()).toString(
357: indent, indent));
358: }
359: }
360:
361: // Add extensions
362: sb.append(super .toString(indent, indent));
363:
364: sb.append("</jdo>");
365: return sb.toString();
366: }
367:
368: /**
369: * Add a listener class name
370: * @param listener the listener metadata. Duplicated classes are ignored
371: */
372: public void addListener(EventListenerMetaData listener) {
373: if (listeners == null) {
374: listeners = new ArrayList();
375: }
376: if (!listeners.contains(listener)) {
377: listeners.add(listener);
378: }
379: }
380:
381: /**
382: * Get the event listeners registered against the file.
383: * @return the event listeners
384: */
385: public List getListeners() {
386: return listeners;
387: }
388:
389: /**
390: * Method to register a query-result MetaData.
391: * @param resultMetaData Query-Result MetaData to register
392: */
393: public void addQueryResultMetaData(
394: QueryResultMetaData resultMetaData) {
395: if (queryResultMetaData == null) {
396: queryResultMetaData = new HashSet();
397: }
398: if (!queryResultMetaData.contains(resultMetaData)) {
399: queryResultMetaData.add(resultMetaData);
400: }
401: }
402:
403: /**
404: * Get the query result MetaData.
405: * @return Query Result MetaData
406: */
407: public QueryResultMetaData[] getQueryResultMetaData() {
408: if (queryResultMetaData == null) {
409: return null;
410: }
411: return (QueryResultMetaData[]) queryResultMetaData
412: .toArray(new QueryResultMetaData[queryResultMetaData
413: .size()]);
414: }
415: }
|