Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/acs/damsel/srvr/collection/Collection.java


1   package org.acs.damsel.srvr.collection;
2   
3   import java.io.*;
4   import java.sql.*;
5   import java.util.*;
6   
7   import org.acs.damsel.client.*;
8   import org.acs.damsel.srvr.*;
9   import org.acs.damsel.srvr.asset.*;
10  import org.acs.damsel.srvr.auth.*;
11  import org.acs.damsel.srvr.db.*;
12  import org.acs.damsel.srvr.schema.*;
13  import org.apache.log4j.*;
14  
15  /**
16   * <p>Class Name: Collection</p>
17   * <p>Description: This class provides services to interface with the database by
18   * setting and getting any collection information including assets located within
19   * a collection. Instances maintain a copy of all information stored in the
20   * database about a collection.</p>
21   * @version 1.1
22   */
23  
24  public class Collection
25      implements Serializable {
26  
27    public String collectionName;
28    public String schemaName;
29    public String ownerName;
30    public String description;
31    public String repositoryName;
32    public String permissionID;
33    public String groupName;
34  
35    private static Logger log = Logger.getLogger(Collection.class);
36  
37    /**
38     * Default constructor. Initializes the collection's instance variables to null
39     * and sets up the default configuration.
40     */
41    public Collection() {
42      BasicConfigurator.resetConfiguration();
43      PropertyConfigurator.configure(Config.instance().getLogPropertiesFileName());
44  
45      collectionName = null;
46      schemaName = null;
47      ownerName = null;
48      description = null;
49      repositoryName = null;
50      permissionID = null;
51      groupName = null;
52    }
53  
54    /**
55     * Constructor with name as parameter. NOTE: Only sets the name of the collection to
56     * be name! Does not grab from the database the named collection.
57     * @param name String containing the name of a collection.
58     */
59    public Collection(String name) {
60      this();
61      this.collectionName = name;
62    }
63  
64    /**
65     * Adds the specified asset to the database and associates it with this collection.
66     * The asset will not be added if it already exists in the database.  If it is
67     * already associated with the given collection, then it won't be associated twice.
68     * @param asset the asset to be added
69     * @return true if asset was added successfully, false otherwise
70     */
71    public boolean addAsset(Asset asset) {
72      log.debug("enter/exit addAsset.");
73      AssetDB assetDB = null;
74  
75      try {
76        /*Insert into the database */
77        assetDB = AssetDB.instance();
78        assetDB.addAsset(asset, this.collectionName);
79      }
80      catch (SQLException ex) {
81        log.warn("Caught SQLException in addAsset " + ex.getMessage());
82        return false;
83      }
84      return true;
85    }
86  
87    /**
88     * Returns the asset with the specified fileName
89     * @param fileName String containing the name of the assets primary key
90     * @return the asset with the specified fileName, null if the asset was not found
91     * @throws SQLException
92     */
93    public Asset getAsset(String fileName) {
94      try {
95        AssetDB assetDB = AssetDB.instance();
96        Asset a = assetDB.getAssetFromCollection(fileName, this.collectionName);
97        if (this.contains(a)) {
98          return a;
99        }
100     }
101     catch (SQLException ex) {
102       log.warn("Caught unexpected SQLException in Collection.getAsset: " +
103                ex.getMessage());
104     }
105     catch (SchemaException ex) {
106       log.warn("Caught unexpected SchemaException in Collection.getAsset: " +
107          ex.getMessage());
108 
109     }
110     return null;
111   }
112 
113   /**
114    * Returns a CollectionView containing all of the Assets associated with this
115    * Collection. CollectionView will be empty if this Collection's collectionName
116    * is not found in the Repository (db's CollectionTable) (or if the collectionName
117    * is blank!!)
118    * @return CollectionView containing all of the Assets
119    */
120   public CollectionView getAllAssets() {
121     try {
122       return AssetDB.instance().getAllAssetsFromCollection(this.collectionName);
123     }
124     catch (SchemaException ex) {
125       return null;
126     }
127     catch (SQLException ex) {
128       return null;
129     }
130   }
131 
132   /**
133    * Removes the specified asset from this collection and the database by
134    * searching for the fileName
135    * @param asset to be removed
136    * @return true if asset was removed successfully, false otherwise
137    */
138   public boolean removeAsset(Asset asset) {
139      log.debug("enter/exit removeAsset.");
140      try {
141        AssetDB assetDB = AssetDB.instance();
142        return assetDB.removeAssetFromCollection(asset, this.collectionName);
143      }
144      catch (Exception ex) {
145      }
146      return false;
147    }
148 
149   /**
150    * @return int, number of assets in this collection
151    */
152   public int size() {
153     CollectionView results = null;
154     try {
155       results = AssetDB.instance().getAllAssetsFromCollection(this.
156           collectionName);
157     }
158     catch (SchemaException ex) {
159       log.warn("Unexpected SchemaException in Collection.size(): " +
160                ex.getMessage());
161       return -1;
162     }
163     catch (SQLException ex) {
164       log.warn("Unexpected SQLException in Collection.size(): " + ex.getMessage());
165       return -1;
166     }
167     return results.size();
168   }
169 
170   /**
171    * Checks if collection is empty.
172    * @return true if collection is empty, false otherwise
173    * @throws SchemaException
174    * @throws SQLException
175    * @todo Rather than getting all assets from the collection and checking the result,
176    * write a method in DBUtils or AssetDB that executes only ONE line of SQL to
177    * determine whether any assets are associated with this collection.
178    */
179   public boolean isEmpty() throws SchemaException, SQLException {
180     log.debug("enter/exit isEmpty.");
181     CollectionView results = null;
182     results = AssetDB.instance().getAllAssetsFromCollection(this.
183           collectionName);
184     if (results.size() > 0)
185       return false;
186     return true;
187   }
188 
189   /**
190    * Check if the Collection can be viewed by the default user (definition of public)
191    * @return true if viewable by default user, false otherwise
192    * @throws SQLException
193    */
194   public boolean isPublic() throws SQLException {
195     AuthMgr aMgr = ClientApp.instance().getAuthMgr();
196     return aMgr.can("default", "Read", "CollectionTable", "CollectionName", this.collectionName);
197   }
198 
199   /**
200    * Checks if current collection contains specified asset.
201    * @param asset in question
202    * @return true if asset is contained in collection, false otherwise
203    */
204   public boolean contains(Asset asset) {
205     log.debug("enter/exit contains.");
206     try {
207       return AssetDB.instance().isAssetInCollection(asset.getFileName(), this.collectionName);
208     }
209     catch (SQLException ex) {
210       log.warn("Unexpected SQLException in Collection.contains(): " +
211                ex.getMessage());
212       return false;
213     }
214   }
215 
216   /* Getters and Setters*/
217 
218   public String getCollectionName() {
219     return collectionName;
220   }
221 
222   public void setCollectionName(String collectionName) {
223     this.collectionName = collectionName;
224   }
225 
226   public Enumeration enumerator() {
227     return null;
228   }
229 
230   public boolean equals(Object col) {
231     return collectionName.equals( ( (Collection) col).getCollectionName());
232   }
233 
234   public String getDescription() {
235     return description;
236   }
237 
238   public void setDescription(String description) {
239     this.description = description;
240   }
241 
242   public String getGroupName() {
243     return groupName;
244   }
245 
246   public void setGroupName(String groupName) {
247     this.groupName = groupName;
248   }
249 
250   public String getOwnerName() {
251     return ownerName;
252   }
253 
254   public void setOwnerName(String ownerName) {
255     this.ownerName = ownerName;
256   }
257 
258   public String getPermissionID() {
259     return permissionID;
260   }
261 
262   public void setPermissionID(String permissionID) {
263     this.permissionID = permissionID;
264   }
265 
266   public String getRepositoryName() {
267     return repositoryName;
268   }
269 
270   public void setRepositoryName(String repositoryName) {
271     this.repositoryName = repositoryName;
272   }
273 
274   public String getSchemaName() {
275     return schemaName;
276   }
277 
278   public void setSchemaName(String schemaName) {
279     this.schemaName = schemaName;
280   }
281 
282 }