001: // You can redistribute this software and/or modify it under the terms of
002: // the Ozone Library License version 1 published by ozone-db.org.
003: //
004: // The original code and portions created by SMB are
005: // Copyright (C) 1997-@year@ by SMB GmbH. All rights reserved.
006: //
007: // $Id: CollectionImpl.java,v 1.1 2001/12/18 11:03:24 per_nyfelt Exp $
008:
009: package org.ozoneDB.xml.cli;
010:
011: import java.util.Map;
012: import java.util.Set;
013: import java.util.HashMap;
014: import java.io.Serializable;
015:
016: import org.ozoneDB.OzoneInterface; // don't know if using ExternalDb is the best way but the interface does not have close()
017: import org.ozoneDB.ExternalDatabase;
018: import org.ozoneDB.xml.cli.resources.XMLResourceImpl;
019: import org.ozoneDB.xml.core.XMLCollection;
020:
021: import org.ozoneDB.xml.util.XMLContainer;
022:
023: import org.xmldb.api.base.ErrorCodes;
024: import org.xmldb.api.base.Collection;
025: import org.xmldb.api.base.Resource;
026: import org.xmldb.api.base.ResourceIterator;
027: import org.xmldb.api.base.Service;
028: import org.xmldb.api.base.XMLDBException;
029:
030: import org.xmldb.api.modules.XMLResource;
031: import org.xmldb.api.modules.BinaryResource;
032:
033: import java.util.Random;
034:
035: /**
036: * A <code>Collection</code> represents a collection of <code>Resource</code>s
037: * stored within an XML database. An XML database may expose collections as a
038: * hierarchical set of parent and child collections.<p />
039: *
040: * A <code>Collection</code> provides access to the <code>Resource</code>s
041: * stored by the <code>Collection</code> and to <code>Service</code> instances
042: * that can operate against the <code>Collection</code> and the
043: * <code>Resource</code>s stored within it. The <code>Service</code> mechanism
044: * provides the ability to extend the functionality of a <code>Collection</code>
045: * in ways that allows optional functionality to be enabled for the <code>Collection</code>.
046: *
047: * @author <a href="http://www.smb-tec.com">SMB</a>, Per Nyfelt
048: * @version $Revision: 1.1 $
049: */
050: public class CollectionImpl extends AbstractConfigurable implements
051: Collection {
052:
053: //
054: // data
055: //
056: private Map services = null;
057: private ExternalDatabase database = null;
058: private XMLCollection collection = null;
059:
060: //private Map resources = null;
061:
062: /**
063: * gets the <code>Collection</code> using the corresponding name from the given
064: * database
065: */
066: protected static Collection forName(ExternalDatabase database,
067: String collectionName) throws Exception {
068: XMLCollection collection = (XMLCollection) database
069: .objectForName(collectionName);
070: return collection != null ? new CollectionImpl(database,
071: collection) : null;
072: }
073:
074: /**
075: * creates a new <code>Collection</code> and stores it in the database
076: */
077: protected static Collection newCollection(
078: ExternalDatabase database, String collectionName)
079: throws Exception {
080: XMLCollection collection = (XMLCollection) database
081: .createObject(
082: org.ozoneDB.xml.core.XMLCollectionImpl.class
083: .getName(), OzoneInterface.Public,
084: collectionName);
085: collection.setName(collectionName);
086: return new CollectionImpl(database, collection);
087: }
088:
089: /**
090: * Constuctor
091: */
092: protected CollectionImpl(ExternalDatabase database,
093: XMLCollection collection) {
094:
095: this .database = database;
096: this .collection = collection;
097:
098: services = new HashMap();
099: }
100:
101: /**
102: * Returns the name of this Collection. The name is unique.
103: *
104: * @return The name of this Collection.
105: */
106: public String getName() throws XMLDBException {
107: return collection.getName();
108: }
109:
110: /**
111: * @return a String array of Services registered for this Collection.
112: */
113: public synchronized Service[] getServices() throws XMLDBException {
114: return (Service[]) services.values().toArray(
115: new Service[services.values().size()]);
116: }
117:
118: /**
119: * Returns the Service with the provided name and version.
120: *
121: * @param name the name of the requested Service
122: * @param version the version of the requested Service if any
123: */
124: public Service getService(String name, String version)
125: throws XMLDBException {
126: return (Service) services.get(name + version);
127: }
128:
129: /**
130: * Registers the provided Service for this Collection.
131: *
132: * @param service The Service to be registered for this Collection.
133: */
134: public synchronized void registerService(Service service)
135: throws XMLDBException {
136: service.setCollection(this );
137: services.put(service.getName() + service.getVersion(), service);
138: }
139:
140: /**
141: * Returns the parent collection for this collection or null if no parent
142: * collection exists.
143: *
144: * @return the parent Collection instance.
145: * @exception XMLDBException
146: */
147: public Collection getParentCollection() throws XMLDBException {
148: XMLCollection parentCollection = collection
149: .getParentCollection();
150: return parentCollection == null ? null : new CollectionImpl(
151: database, parentCollection);
152: }
153:
154: /**
155: * Returns the number of child collections under this collection or 0 if no
156: * child collections exist.
157: *
158: * @return the number of child collections.
159: * @exception XMLDBException
160: */
161: public int getChildCollectionCount() throws XMLDBException {
162: return collection.getChildCollectionCount();
163: }
164:
165: /**
166: * Returns a list of Collection names naming all child collections
167: * of the current collection. If no child collections exist an empty list is
168: * returned.
169: *
170: * @return an array containing Collection names for all child
171: * collections.
172: * @exception XMLDBException
173: */
174: public String[] listChildCollections() throws XMLDBException {
175: return collection.listChildCollections();
176: }
177:
178: /**
179: * Returns a Collection instance for the requested child collection
180: * if it exists.
181: *
182: * @param name the name of the child collection to retrieve.
183: * @return the requested child collection or null if it couldn't be found.
184: * @exception XMLDBException
185: */
186: public Collection getChildCollection(String name)
187: throws XMLDBException {
188: XMLCollection cCollection = collection.getChildCollection(name);
189: return cCollection == null ? null : new CollectionImpl(
190: database, cCollection);
191: }
192:
193: /**
194: */
195: public int getResourceCount() throws XMLDBException {
196: return collection.getResourceCount();
197: }
198:
199: /**
200: * Returns a list of the ids for all resources stored in the collection.
201: *
202: * @return a string array containing the names for all
203: * <code>Resource</code>s in the collection.
204: * @exception XMLDBException with expected error codes.<br />
205: * <code>ErrorCodes.VENDOR_ERROR</code> for any vendor
206: * specific errors that occur.<br />
207: */
208: public String[] listResources() throws XMLDBException {
209: Set resourceSet = collection.getResources();
210: String[] resourceArray = (String[]) resourceSet.toArray();
211: return resourceArray;
212: }
213:
214: /**
215: * Creates a new empty <code>Resource</code> with the provided id.
216: *
217: * @param id the unique id to associate with the created <code>Resource</code>.
218: * @param type the <code>Resource</code> type to create.
219: * @return an empty <code>Resource</code> instance.
220: */
221: public Resource createResource(String id, String type)
222: throws XMLDBException {
223: try {
224: if ((id == null) || (id.length() == 0)) {
225: id = createId();
226: }
227: if (type.equals(XMLResource.RESOURCE_TYPE)) {
228: //System.out.println("CollectionImpl.createResource() - Creating container");
229: XMLContainer container = XMLContainer.forName(database,
230: id);
231: if (container != null) {
232: System.out.println("container exists already");
233: throw new XMLDBException(
234: ErrorCodes.INVALID_RESOURCE, "resource "
235: + id + " already exists");
236: }
237: container = XMLContainer.newContainer(database, id);
238: //System.out.println("CollectionImpl.createResource() - created XMLContainer, adding id to the XMLCollection");
239: collection.addResource(id);
240: return new XMLResourceImpl(id, database, this ,
241: container);
242: } else if (type.equals(BinaryResource.RESOURCE_TYPE)) {
243: //return new BinaryResourceImpl(this, id);
244: throw new XMLDBException(ErrorCodes.VENDOR_ERROR,
245: "BinaryResource: Not yet implemented");
246: } else {
247: throw new XMLDBException(
248: ErrorCodes.UNKNOWN_RESOURCE_TYPE);
249: }
250: } catch (Exception e) {
251: throw new XMLDBException(ErrorCodes.VENDOR_ERROR, e
252: .getMessage());
253: }
254: }
255:
256: /**
257: * Removes the <code>Resource</code> from the database.
258: *
259: * @param res the resource to remove.
260: * @exception XMLDBException with expected error codes.<br />
261: * <code>ErrorCodes.VENDOR_ERROR</code> for any vendor
262: * specific errors that occur.<br />
263: * <code>ErrorCodes.INVALID_RESOURCE</code> if the <code>Resource</code> is
264: * not valid.<br />
265: * <code>ErrorCodes.NO_SUCH_RESOURCE</code> if the <code>Resource</code> is
266: * not known to this <code>Collection</code>.
267: */
268: public void removeResource(Resource res) throws XMLDBException {
269: try {
270: System.out
271: .println("CollectionImpl.removeResource() - Getting XMLContainer");
272: XMLContainer container = null;
273: String id = res.getId();
274: if (collection.hasResource(id)) {
275: container = XMLContainer.forName(database, id);
276: container.delete();
277: } else {
278: throw new XMLDBException(ErrorCodes.NO_SUCH_RESOURCE,
279: "the resource is not a part of this collection");
280: }
281: } catch (Exception e) {
282: throw new XMLDBException(ErrorCodes.VENDOR_ERROR, e
283: .toString());
284: }
285: }
286:
287: /**
288: * Stores the provided resource into the database.
289: *
290: * @param res the resource to store in the database.
291: */
292: public void storeResource(Resource res) throws XMLDBException {
293: try {
294: String id = res.getId();
295: if ((id == null) || (id.length() == 0)) {
296: id = createId();
297: }
298: System.out
299: .println("CollectionImpl.storeResource() - Dont know what to do here");
300:
301: } catch (Exception e) {
302: throw new XMLDBException(ErrorCodes.VENDOR_ERROR, e
303: .toString());
304: }
305: }
306:
307: /**
308: */
309: public Resource getResource(String id) throws XMLDBException {
310: try {
311: //System.out.println("CollectionImpl.getResource() - Getting XMLContainer");
312: XMLContainer container = null;
313: if (collection.hasResource(id))
314: container = XMLContainer.forName(database, id);
315: else
316: return null;
317: if (container == null) {
318: System.out
319: .println("CollectionImpl.getResource() - container is null");
320: return null;
321: } else {
322: System.out
323: .println("CollectionImpl.getResource() - creating new XMLResourceImpl");
324: Resource resource = new XMLResourceImpl(id, database,
325: this , container);
326: //System.out.println("CollectionImpl.getResource() - created new XMLResourceImpl");
327: return resource;
328: }
329:
330: } catch (Exception e) {
331: throw new XMLDBException(ErrorCodes.VENDOR_ERROR, e
332: .toString());
333: }
334: }
335:
336: /**
337: * Creates a new unique ID within the context of the <code>Collection</code>
338: *
339: * @return the created id as a string.
340: */
341: public String createId() throws XMLDBException {
342: System.out
343: .println("CollectionImpl.createId() - fix the quick hack");
344: return collection.getName() + System.currentTimeMillis()
345: + new Random().nextLong();
346: }
347:
348: /**
349: * Releases all resources consumed by the <code>Collection</code>.
350: * The <code>close</code> method must
351: * always be called when use of a <code>Collection</code> is complete.
352: *
353: * @exception XMLDBException with expected error codes.<br />
354: * <code>ErrorCodes.VENDOR_ERROR</code> for any vendor
355: * specific errors that occur.<br />
356: */
357: public void close() throws XMLDBException {
358: // don't know if this is the best way but OzoneInterface does not have close()
359: if (database instanceof ExternalDatabase) {
360: try {
361: ((ExternalDatabase) database).close();
362: System.out
363: .println("CollectionImpl.close() - connection closed");
364: } catch (Exception e) {
365: throw new XMLDBException(ErrorCodes.VENDOR_ERROR, e
366: .getMessage());
367: }
368: }
369: }
370:
371: }
|