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

Quick Search    Search Deep

Source code: org/acs/damsel/srvr/search/SearchMgr.java


1   package org.acs.damsel.srvr.search;
2   
3   import java.sql.*;
4   import java.util.*;
5   
6   import org.acs.damsel.srvr.*;
7   import org.acs.damsel.srvr.collection.*;
8   import org.acs.damsel.srvr.collection.Collection;
9   import org.acs.damsel.srvr.db.*;
10  import org.acs.damsel.srvr.schema.*;
11  import org.apache.log4j.*;
12  
13  /**
14   * <p>Title: SearchMgr</p>
15   * <p>Description: This class contains methods used to facilitate searching
16   * and browsing of assets in the database.  It includes methods for simple and
17   * advance searches, as well as browsing.</p>
18   * @version 1.0
19   */
20  public class SearchMgr {
21    /*myAssets is the collection of all the assets  */
22    private static Logger log = Logger.getLogger(SearchMgr.class);
23    private CollectionView myView = null;
24    private String collectionName;
25    private AssetDB assetdb = null;
26  
27    /**
28     * Default no argument constructor.
29     */
30    public SearchMgr() {
31      BasicConfigurator.resetConfiguration();
32      PropertyConfigurator.configure(Config.instance().getLogPropertiesFileName());
33    }
34  
35    /**
36     * Constructure that takes the collection to be searched or browsed as a
37     * parameter.
38     * @param c Collection to be searched/browsed
39     */
40    public SearchMgr(Collection c) {
41      BasicConfigurator.resetConfiguration();
42      PropertyConfigurator.configure(Config.instance().getLogPropertiesFileName());
43      collectionName = c.getCollectionName();
44    }
45  
46    /**
47     * Method to search for multiple keywords across all asset descriptor fields
48     * included in the collection schema. Converts the searchWord using
49     * the convert method.
50     * @param searchString the keyword to be searched for
51     * @param collectionSelect the collection to be searched
52     * @return CollectionView of all assets matching search
53     * @throws SQLException
54     */
55    public CollectionView simpleSearch(String searchString, String collectionSelect)
56        throws SQLException {
57  
58      log.debug("enter simpleSearch");
59      searchString = this.convert(searchString);
60      CollectionView myView = new CollectionView();
61      StringTokenizer st = new StringTokenizer(searchString,
62                                               " \t\n\r\f.:,;\"!?()-");
63      while (st.hasMoreTokens()) {
64        CollectionView temp = simpleSearchHelper(st.nextToken(), collectionSelect);
65        myView.merge(temp);
66      }
67      log.debug("exit simpleSearch");
68  
69      return myView;
70    }
71  
72    /**
73     * Method to search for a specific keyword across all asset descriptor
74     * fields included in the collection schema.
75     * @param searchWord the keyword to be searched for
76     * @param collectionSelect the collection to be searched
77     * @return CollectionView of all assets matching search
78     * @throws SQLException
79     */
80    private CollectionView simpleSearchHelper(String searchWord, String collectionSelect)
81        throws SQLException {
82  
83      log.debug("enter simpleSearchHelper");
84      CollectionView myView = new CollectionView();
85      String mySchemaName = null;
86      Schema mySchema = null;
87      try {
88        mySchema = AssetDB.instance().getSchemaOfCollection(collectionSelect);
89      }
90      catch (SchemaException ex) {
91        log.error("Caught unexpected SchemaException in simpleSearchHelper.");
92      }
93      mySchemaName = mySchema.getName();
94      Iterator it = mySchema.tagsIterator();
95      while (it.hasNext()) {
96        MetaDataTag mdt = (MetaDataTag) it.next();
97        String tagName = mdt.getName();
98        CollectionView temp = searchByKeyword(searchWord, tagName,
99                                              collectionSelect);
100       myView.merge(temp);
101     }
102     log.debug("exit simpleSearchHelper");
103 
104     return myView;
105   }
106 
107   /**
108    * This method browses through a collection by certain parameters and returns
109    * only the collection matching that parameter. Converts the searchWord using
110    * the convert method.
111    * @param searchWord the keyword to be searched for
112    * @param tag the tag being searched within i.e. Author or Title
113    * @param collectionSelect the collection to be searched
114    * @return CollectionView of assets matching search criteria
115    */
116   public CollectionView searchByKeyword(String searchWord, String tag, String collectionSelect) {
117     log.debug("enter searchByKeyword");
118     try {
119       log.debug("exit searchByKeyword");
120       searchWord = this.convert(searchWord);
121       myView = DBUtils.instance().simpleSearch(searchWord, tag,
122                                                collectionSelect);
123       return myView;
124 
125     }
126     catch (SQLException ex) {
127       log.error(ex.getMessage());
128       log.debug("exit searchByKeywordException");
129       return null;
130     }
131   }
132 
133   /**
134    * Method to perform an advanced search given an AdvancedSearchParams object.
135    * Note that the collection to be searched is not taken in as a parameter
136    * because it is stored in the AdvancedSearchParams object.
137    * @param params instance of AdvancedSearchParams
138    * @return CollectionView of assets matching search criteria
139    */
140   public CollectionView advancedSearch(AdvancedSearchParams params) {
141     log.debug("enter advancedSearch");
142     try {
143       myView = DBUtils.instance().advancedSearch(params);
144     }
145     catch (SQLException ex) {
146       log.warn("SQLException caught in SearchMgr: " + ex.getMessage());
147     }
148     log.debug("exit advancedSearch");
149     return myView;
150   }
151 
152   /**
153    * This method browses through a collection for entries in the specified
154    * field that start with the specified browse character.
155    * @param browseChar a String containing the character to browse by
156    * @param tag a String containing the field to browse in
157    * @param collectionSelect a String containing the collection to browse through
158    * @return CollectionView of assets matching search criteria
159    */
160   public CollectionView browseByKeyField(String browseChar, String tag, String collectionSelect) {
161     log.debug("enter browseByKeyField");
162     try {
163       myView = DBUtils.instance().browseDB(browseChar, tag, collectionSelect);
164     }
165     catch (SQLException ex) {
166       log.warn("SQLException caught in browseByKeyField method in SearchMgr: " + ex.getMessage());
167     }
168     log.debug("exit browseByKeyField");
169     return myView;
170   }
171 
172   /*Convert user-friendly * wildcard to sequel-friendly % wildcard */
173   private String convert(String searchString) {
174     searchString = searchString.replaceAll("\\*", "%");
175     searchString = searchString.replaceAll("\\?", "_");
176     return searchString;
177   }
178 }