ZorbaXQCollectionManager.java
Go to the documentation of this file.
1 /*
2  * Copyright 2006-2012 The FLWOR Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.zorbaxquery.api.xqj;
17 
18 import java.util.ArrayList;
19 import java.util.Collection;
20 import javax.xml.xquery.XQException;
21 import javax.xml.xquery.XQItem;
22 import javax.xml.xquery.XQSequence;
23 import org.zorbaxquery.api.CollectionManager;
24 
25 
26  /** \brief This class defines a set of functions for managing persistent
27  * collections.
28  *
29  */
31 
32  private boolean closed = false;
33  private CollectionManager collectionManager = null;
34  private Collection<XQSequence> sequences = new ArrayList<XQSequence>();
35  private Collection<ZorbaXQCollection> collections = new ArrayList<ZorbaXQCollection>();
36 
37  protected ZorbaXQCollectionManager (CollectionManager cm) {
38  collectionManager = cm;
39  }
40 
41  /** \brief Closes the collection manager.
42  *
43  * Once the collection manager is closed, no method other than close or the isClosed method may be called on the collection manager object. Calling close on an ZorbaXQCollectionManager object that is already closed has no effect.
44  *
45  * @throw XQException - if there is an error during closing the collection.
46  */
47  public void close() throws XQException {
48  for (XQSequence exp : sequences ){
49  exp.close(); // Notify the dependents objects to close
50  }
51  for (ZorbaXQCollection exp : collections ){
52  exp.close(); // Notify the dependents objects to close
53  }
54  if (collectionManager!=null) {
55  collectionManager.delete();
56  }
57  closed = true;
58  }
59 
60  /** \brief Checks if the collection manager is closed.
61  *
62  * @return true if the collection manager is in a closed state, false otherwise
63  */
64  public boolean isClosed() {
65  return closed;
66  }
67 
68  /**
69  * This function returns a sequence of names of the collections
70  * that are available. If this is an instance of the StaticCollectionManager
71  * class (i.e. returned by any of the getStaticCollectionManager methods),
72  * the collections returned by this function are also
73  * statically declared.
74  *
75  * @return The list of names of the available collections.
76  * @throw XQException if any error occurs retreiving the collections
77  *
78  */
79  public XQSequence availableCollections() throws XQException {
80  isClosedXQException();
81  XQSequence result = new org.zorbaxquery.api.xqj.ZorbaXQSequence(collectionManager.availableCollections());
82  sequences.add(result);
83  return result;
84  }
85 
86  /**
87  * This function creates the collection with the given name.
88  *
89  * @param aName The name of the collection to create.
90  *
91  * @throw XQException if a collection with the given name already exists.
92  *
93  */
94  public void createCollection(XQItem aName ) throws XQException {
95  isClosedXQException();
96  collectionManager.createCollection(((org.zorbaxquery.api.xqj.ZorbaXQItem)aName).getZorbaItem());
97  }
98 
99  /**
100  * This function removes the collection with the given name.
101  *
102  * @param aName The name of the collection to delete.
103  *
104  * @throw XQException if the collection does not exist.
105  */
106  public void deleteCollection(XQItem aName ) throws XQException {
107  isClosedXQException();
108  collectionManager.deleteCollection(((org.zorbaxquery.api.xqj.ZorbaXQItem)aName).getZorbaItem());
109  }
110 
111  /**
112  * Returns a instance of the Collection class which can
113  * be used to modify and retrieve the contents of the collection
114  * identified by the given name.
115  *
116  * @param aName The name of the collection to retrieve.
117  *
118  * @throw XQException if the collection does not exist.
119  */
120  public ZorbaXQCollection getCollection(XQItem aName ) throws XQException {
121  isClosedXQException();
122  ZorbaXQCollection result = new ZorbaXQCollection ( collectionManager.getCollection(((org.zorbaxquery.api.xqj.ZorbaXQItem)aName).getZorbaItem()) );
123  collections.add(result);
124  return result;
125  }
126 
127  /**
128  * This function returns true if a collection with the given name is available.
129  * If this is an instance of the StaticCollectionManager class (i.e.
130  * returned by any of the getStaticCollectionManager() methods),
131  * the collection also needs to be statically declared.
132  *
133  * @param aName The name of the collection that is being checked.
134  *
135  * @return true if the collection is available and false otherwise.
136  * @throw XQException if any error occurs verifying the collection
137  *
138  */
139  public boolean isAvailableCollection(XQItem aName ) throws XQException {
140  isClosedXQException();
141  return collectionManager.isAvailableCollection( ((org.zorbaxquery.api.xqj.ZorbaXQItem)aName).getZorbaItem() );
142  }
143 
144  private void isClosedXQException() throws XQException {
145  if (closed) {
146  throw new XQException("CollectionManager is closed");
147  }
148  }
149 
150 }
XQSequence availableCollections()
This function returns a sequence of names of the collections that are available.
A Collection is a persistent sequence of node items.
This class defines a set of functions for managing persistent collections.
boolean isAvailableCollection(XQItem aName)
This function returns true if a collection with the given name is available.
boolean isClosed()
Checks if the collection manager is closed.
void deleteCollection(XQItem aName)
This function removes the collection with the given name.
This interface represents a sequence of items as defined in the XDM.
void createCollection(XQItem aName)
This function creates the collection with the given name.
ZorbaXQCollection getCollection(XQItem aName)
Returns a instance of the Collection class which can be used to modify and retrieve the contents of t...