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/CollectionView.java


1   package org.acs.damsel.srvr.collection;
2   
3   import java.sql.*;
4   import java.util.*;
5   
6   import org.acs.damsel.srvr.*;
7   import org.acs.damsel.srvr.asset.*;
8   import org.apache.log4j.*;
9   import org.acs.damsel.srvr.group.*;
10  
11  /**
12   * <p>Class Name: Collection View</p>
13   * <p>Description: This class provides methods to add, remove and iterate through assets in a
14   * particular collection view.
15   * More features to be added later, i.e. the ability to apply a Mask object to
16   * this view, etc.</p>
17   * @version 1.0
18   */
19  
20  public class CollectionView {
21    private Vector myView;
22    private static Logger log = Logger.getLogger(CollectionView.class);
23  
24    /* no parameter constructor */
25    public CollectionView() {
26      BasicConfigurator.resetConfiguration();
27      PropertyConfigurator.configure(Config.instance().getLogPropertiesFileName());
28      myView = new Vector();
29    }
30  
31    /**
32     * Merges specified collection view assets with the current collection view.
33     * @param collView CollectionView to be merged
34     * @throws SQLException
35     */
36    public void merge(CollectionView collView) throws SQLException {
37  
38        for (Iterator it = collView.iterator(); it.hasNext(); ) {
39          Asset asset = (Asset) it.next();
40          if (!this.contains(asset)) {
41            this.addAsset(asset);
42          }
43        }
44    }
45  
46    /**
47     * Adds an Asset to this CollectionView
48     * @param as Asset to be added
49     */
50    public void addAsset(Asset as) {
51      log.debug("enter addAsset");
52      myView.add(as);
53      log.debug("exit addAsset");
54    }
55  
56    /**
57     * Removes the specified Asset from this CollectionView
58     * @param as the Asset to be removed
59     * @return true if removed, false otherwise
60     */
61    public boolean removeAsset(Asset as) {
62      log.debug("enter/exit removeAsset");
63      Asset asset;
64      for(int i = 0; i < myView.size(); i ++ ) {
65        asset = (Asset) myView.elementAt(i);
66        try {
67          if (asset.getFileName().equals(as.getFileName())) {
68            myView.remove(i);
69            return true;
70          }
71        }
72        catch (SQLException ex) {
73          log.warn("Caught unexpected SQLException in CollectionView.removeAsset(): " + ex.getMessage());
74        }
75      }
76      return false;
77    }
78  
79    /**
80     * Returns the asset sitting at the specified index in the CollectionView(Vector).
81     * @param index int, the index of the desired asset
82     * @return Asset the asset found at the specified index
83     */
84    public Asset returnAsset(int index) {
85      log.debug("enter/exit returnAsset");
86      return (Asset) myView.elementAt(index);
87    }
88  
89    /**
90     * Iterate through the assets in this collection view and determine what asset
91     * descriptors are common for all.
92     * @return Vector list of MetadataTags (as Strings)
93     */
94    public Vector getMetadataTagsCommonToAllAssets() {
95      /*We need to grab the first asset in the list and compare it to all the other
96      assets.... */
97      Vector results = new Vector();
98      if (this.size()>0) {
99        AssetDescriptorCollection adc;
100       AssetDescriptor ad1, ad2;
101       Iterator it;
102       Asset asset = this.returnAsset(0);
103       Iterator firstAssetAdcIterator = asset.getAssetDescriptors().iterator();
104       if (this.size() == 1) {
105         adc = asset.getAssetDescriptors();
106         it = adc.iterator();
107         while (it.hasNext())
108           results.add(((AssetDescriptor)it.next()).getTag());
109       return results;
110       }
111       while (firstAssetAdcIterator.hasNext()) { //iterate through each adc in the first asset
112         ad1 = (AssetDescriptor) firstAssetAdcIterator.next();
113         for (int i = 1; i < myView.size(); i++) { //iterate through the assets
114           adc = this.returnAsset(i).getAssetDescriptors();
115           it = adc.iterator();
116           while (it.hasNext()) { //iterate through tags in an asset's adc
117             ad2 = (AssetDescriptor) it.next();
118             if (ad2.getTag().equals(ad1.getTag()))
119               if (!results.contains(ad1.getTag()))
120                   results.add(ad1.getTag());
121           }
122         }
123       }
124     }
125     return results;
126   }
127 
128   /**
129    * @return size of CollectionView
130    */
131   public int size() {
132     return myView.size();
133   }
134 
135   /**
136    * @return an Iterator with all the elements in this view
137    */
138   public Iterator iterator() {
139     return myView.iterator();
140   }
141 
142   /**
143    * Method checks whether the specified asset is in this CollectionView
144    * @param as Asset to be checked whether CollectionView contains
145    * @return true if the specified object exists in this collection view, false otherwise
146    * @throws SQLException
147    */
148   public boolean contains(Asset as) throws SQLException {
149     if (as == null)
150       return false;
151     String fileName = as.getFileName();
152     for (Iterator i = this.iterator(); i.hasNext(); ) {
153       Asset a = (Asset) i.next();
154       if (a != null) {
155         if (fileName.equals(a.getFileName())) {
156           return true;
157         }
158       }
159     }
160     return false;
161   }
162 
163 
164 }