Source code: org/acs/damsel/srvr/collection/CollectionMgr.java
1 package org.acs.damsel.srvr.collection;
2
3 import java.io.*;
4 import java.sql.*;
5 import java.util.Vector;
6 import java.util.Iterator;
7
8 import org.acs.damsel.srvr.*;
9 import org.acs.damsel.srvr.db.*;
10 import org.acs.damsel.srvr.schema.*;
11 import org.acs.damsel.srvr.user.*;
12 import org.apache.log4j.*;
13
14 /**
15 * <p>Class Name: Collection Manager</p>
16 * <p>Description: This class is a manager that grabs collections from the database
17 * and passes them to anyone who reqests them. Also adds, removes and edits collections
18 * on the database. Note that this class does not maintain a list of Collections,
19 * nor does it deal with Collection objects, it deals with the database directly
20 * @version 1.0
21 */
22 public class CollectionMgr implements Serializable{
23
24 private static Logger log = Logger.getLogger(CollectionMgr.class);
25 private AssetDB assetDB = null;
26
27 /**
28 * Basic constructor
29 */
30 public CollectionMgr() {
31 BasicConfigurator.resetConfiguration();
32 PropertyConfigurator.configure(Config.instance().getLogPropertiesFileName());
33 try {
34 assetDB = AssetDB.instance();
35 }
36 catch (SQLException ex1) {
37 log.error(ex1.getSQLState());
38 }
39 }
40
41 /**
42 * Checks for the presence of a collection in the repository.
43 * @param c Collection that is being searched for in the repository
44 * @returns true if found, otherwise false
45 */
46 public boolean isInRepository(Collection c) {
47 log.debug("enter/exit isInRepository.");
48 try {
49 return assetDB.isCollectionInDB(c);
50 }
51 catch (SQLException ex) {
52 log.error("Unexpected SQL error in isInRepository.");
53 ex.printStackTrace();
54 return false;
55 }
56 }
57
58 /**
59 * Adds the specified collection information to the database
60 * Note!! This method does not add all the assets associated with the specified
61 * collection, it simply updates the CollectionTable to include all its information
62 * @param collection the collection to be added
63 * @throws CollectionMgrException
64 */
65 public void addCollection(Collection collection) throws
66 CollectionMgrException {
67 log.debug("enter addCollection");
68 try {
69 if (!isInRepository(collection)){
70 assetDB.addCollection(collection);
71 }
72 }
73 catch (SQLException ex) {
74 log.error("Unexpected SQL error in addCollection.");
75 ex.printStackTrace();
76 }
77 log.debug("exit addCollection");
78 }
79
80 /**
81 * Removes the specified collection information from the database's CollectionTable
82 * Note!! This method does not remove all the assets associated with the specified
83 * collection, it simply updates the CollectionTable to not include all its information
84 * @param collection the collection to be removed
85 * @throws CollectionMgrException
86 * @todo Change to return the same type as the AssetDB method
87 */
88 public void removeCollection(Collection collection) throws
89 CollectionMgrException {
90 log.debug("enter removeCollection");
91 try {
92 if (isInRepository(collection)){
93 assetDB.deleteCollection(collection);
94 }
95 }
96 catch (SQLException ex) {
97 log.error("Unexpected SQL error in deleteCollection.");
98 ex.printStackTrace();
99 }
100 catch (SchemaException ex) {
101 log.error("Unexpected Schema error in deleteCollection.");
102 ex.printStackTrace();
103 }
104
105 log.debug("exit removeCollection");
106 }
107
108 /**
109 * Builds a Collection by populating all the Collection information from the
110 * database and returns the Collection.
111 * Note!! The Collection returned will not contain assets. Rather, it has methods
112 * that allow the assets to be pulled from the database.
113 * @param collectionName String containg the name of desired Collection
114 * @throws CollectionMgrException
115 * @return Collection the collection populated from the db.
116 */
117 public Collection getCollection(String collectionName) {
118 try {
119 return assetDB.getCollection(collectionName);
120 }
121 catch (SQLException ex) {
122 log.error("Unexpected SQL error in getCollection.");
123 ex.printStackTrace();
124 return null;
125 }
126 }
127
128 /**
129 * @returns int, the Number of Collections in the database's CollectionTable.
130 */
131 public int size() {
132 return assetDB.getNumberOfCollectionsInDB();
133 }
134
135 /**
136 * @returns Iterator containing all Collections in the Repository (database's
137 * CollectionTable)
138 */
139 public Iterator collectionIterator() {
140 try {
141 Vector allCollectionNames = assetDB.getCollectionNames();
142 Vector allCollections = new Vector();
143 Iterator i = allCollectionNames.iterator();
144 int count = 0;
145 while (i.hasNext())
146 {
147 allCollections.add(assetDB.getCollection((String)allCollectionNames.elementAt(count)));
148 count ++;
149 i.next();
150 }
151 return allCollections.iterator();
152 }
153 catch (SQLException ex) {
154 log.warn("Caught unexpected SQLException in collectionIterator");
155 ex.printStackTrace();
156 return null;
157 }
158 }
159
160 /**
161 * @returns Vector containing the names of all the collections in the Repository
162 * (database's CollectionTable)
163 */
164 public Vector collectionNames() {
165 log.debug("enter collectionNames");
166 try {
167 return AssetDB.instance().getCollectionNames();
168 }
169 catch (SQLException ex) {
170 log.warn("Caught unexpected SQLException in collectionIterator");
171 ex.printStackTrace();
172 return null;
173 }
174 }
175
176 /**
177 * This method will take in a User object and search the database for all possible
178 * collections that the user could edit. This will return a Vector of Strings
179 * containing collection names of the collections that are editable by the user.
180 * @return Vector of collections editable by the user.
181 * @param user the User who's editable collections are questioned.
182 */
183 public Vector getEditableCollections (User user) {
184 try {
185 DBUtils dbUtils = DBUtils.instance();
186 return dbUtils.getEditableCollections(user);
187 }
188 catch (SQLException ex) {
189 log.warn("Caught unexpected SQLException in getEditableCollections: "
190 + ex.getMessage());
191 return null;
192 }
193 }
194
195 /**
196 * This method returns all of the assets associated with a collection specified
197 * by collectionName in the form of a CollectionView.
198 * @param collectionName the name of the collection
199 * @return CollectionView containing all the assets in the specified collection
200 * @throws SQLException
201 * @throws SchemaException
202 */
203 public CollectionView getAllAssetsFromCollection(String collectionName)
204 throws SQLException, SchemaException{
205 return assetDB.getAllAssetsFromCollection(collectionName);
206 }
207
208 /**
209 * Gets the CollectionTable from the database.
210 * @return Table containing the entire CollectionTable
211 */
212 public Table getCollectionTable() {
213 return assetDB.getCollectionTable();
214 }
215
216 public int updateCollection(Collection c){
217 try {
218 return assetDB.updateCollection(c);
219 }
220 catch (SQLException ex) {
221 log.warn("Caught unexpected SQLException in updateCollection: "
222 + ex.getMessage());
223 return 0;
224 }
225 }
226
227 /**
228 * Method to add indicated existing asset to indicated existing collection
229 * @param fileName String containing fileName of asset to be added
230 * @param collectionName String containing collectionName of collection to add to
231 */
232 public void addAssetToCollection(String fileName, String collectionName) throws SQLException{
233 DBUtils.instance().addAssetToCollection(fileName, collectionName);
234
235 }
236
237 /**
238 * Method checks database to discover if the permissions set for the specified
239 * collection signal that it is private and returns true if private, false otherwise.
240 * @param collectionName String containing collectionName of collection to check for privacy.
241 * @return true if private, false otherwise
242 */
243 public boolean isCollectionPrivate(String collectionName) {
244 try {
245 return DBUtils.instance().isCollectionPrivate(collectionName);
246 }
247 catch (SQLException ex) {
248 log.warn("Unexpected SQLException caught in isCollectionPrivate: " + ex.getMessage());
249 }
250 return false;
251 }
252 }