source: gs3-extensions/audioDB/trunk/src/src/java/org/greenstone/gsdl3/service/GS2AudioDBSearch.java@ 24398

Last change on this file since 24398 was 24398, checked in by davidb, 13 years ago

Latest round of changes to code, in preparing for demo to Goldsmiths

  • Property svn:executable set to *
File size: 6.8 KB
Line 
1/*
2 * GS2AudioDBSearch.java
3 * Copyright (C) 2011 New Zealand Digital Library, http://www.nzdl.org
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 */
18package org.greenstone.gsdl3.service;
19
20
21// Greenstone classes
22import org.greenstone.gsdl3.util.*;
23
24// XML classes
25import org.w3c.dom.Document;
26import org.w3c.dom.Element;
27import org.w3c.dom.NodeList;
28
29// java
30import java.util.Vector;
31import java.util.ArrayList;
32import java.util.HashMap;
33import java.util.Map;
34import java.util.Set;
35import java.util.Iterator;
36import java.io.File;
37
38import org.apache.log4j.*;
39
40public class GS2AudioDBSearch extends AbstractGS2AudioSearch {
41
42 static Logger logger = Logger.getLogger (org.greenstone.gsdl3.service.GS2AudioDBSearch.class.getName ());
43
44 protected static final String OFFSET_PARAM = "offset";
45 protected static final String LENGTH_PARAM = "length";
46 protected static final String RADIUS_PARAM = "radius";
47 protected static final String MAXDOCS_PARAM = "maxDocs";
48
49 protected static final String AUDIODB_DEFAULT_DIRECTORY = "audioDB";
50 protected static final String ADB_FILENAME = "lsh-features.adb";
51
52 protected AudioDBWrapper audiodb_src = null;
53
54 /** constructor */
55 public GS2AudioDBSearch () {
56 if(this.audiodb_src == null){
57 this.audiodb_src = new AudioDBWrapper ();
58 }
59 }
60
61 /** do the actual query */
62 protected Element processAudioQuery (Element request) {
63 // MG needs to be synchronized (this inspiration for this class)
64 // Since it is not known how concurrent audioDB can be, play it safe for
65 // now and also require synchronization
66
67 synchronized(this.audiodb_src) {
68 // Create a new (empty) result message ('doc' is in ServiceRack.java)
69 Element result = this.doc.createElement (GSXML.RESPONSE_ELEM);
70
71 // Rather than QUERY_SERVICE use "TextQuery"
72 // => makes the result looks the same as a text query
73 result.setAttribute (GSXML.FROM_ATT, "TextQuery");
74 result.setAttribute (GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_PROCESS);
75
76 // Get the parameters of the request
77 Element param_list = (Element) GSXML.getChildByTagName (request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
78 if (param_list == null) {
79 logger.error ("AudioDB AudioQuery request had no paramList.");
80 return result; // Return the empty result
81 }
82
83 // Process the request parameters
84 HashMap params = GSXML.extractParams (param_list, false);
85
86 // Make sure a query has been specified
87 String query = (String) params.get (QUERY_PARAM);
88 if (query == null || query.equals ("")) {
89 return result; // Return the empty result
90 }
91
92 // If an index hasn't been specified, use the default
93 String index = (String) params.get (INDEX_PARAM);
94 if (index == null) {
95 index = AUDIODB_DEFAULT_DIRECTORY;
96 }
97
98 // The location of the audioDB index
99 String toplevel_index_dir = GSFile.collectionIndexDir (this.site_home, this.cluster_name);
100 String audioDB_index_dir = toplevel_index_dir + File.separatorChar + index;
101 String assoc_index_dir = toplevel_index_dir + File.separatorChar + "assoc";
102
103 // set the audioDB query parameters to the values the user has specified
104 setStandardQueryParams (params);
105
106 this.audiodb_src.runQuery (audioDB_index_dir, ADB_FILENAME, assoc_index_dir, query);
107 Vector docs = this.audiodb_src.getQueryResult ();
108
109 if (docs.isEmpty()) {
110 // something has gone wrong
111 GSXML.addError (this.doc, result, "Couldn't query the audioDB database", GSXML.ERROR_TYPE_SYSTEM);
112 return result;
113 }
114 long totalDocs = docs.size();
115
116 // Get the docnums out, and convert to HASH ids
117 if (docs.size () == 0) {
118 logger.error ("No results found...\n");
119 }
120
121 // Create a metadata list to store information about the query results
122 Element metadata_list = this.doc.createElement (GSXML.METADATA_ELEM+GSXML.LIST_MODIFIER);
123 result.appendChild (metadata_list);
124
125 // Add a metadata element specifying the number of matching documents
126 // because the total number is just the number returned, use numDocsReturned, not numDocsMatched
127 GSXML.addMetadata (this.doc, metadata_list, "numDocsReturned", ""+totalDocs);
128 // add a metadata item to specify what actual query was done - eg if stuff was stripped out etc. and then we can use the query later, cos we don't know which parameter was the query
129 GSXML.addMetadata (this.doc, metadata_list, "query", query);
130
131 if (docs.size () > 0) {
132 // Create a document list to store the matching documents, and add them
133 Element document_list = this.doc.createElement (GSXML.DOC_NODE_ELEM+GSXML.LIST_MODIFIER);
134 result.appendChild (document_list);
135 for (int d = 0; d < docs.size (); d++) {
136 AudioDBDocInfo adb_doc = (AudioDBDocInfo) docs.elementAt(d);
137
138 String doc_id = adb_doc.getDocID();
139 double rank = adb_doc.getTopRank();
140 String offsets = adb_doc.getOffsetList();
141
142 Element doc_node = createDocNode (doc_id, Double.toString (rank));
143 doc_node.setAttribute("frameOffset", offsets);
144
145 document_list.appendChild (doc_node);
146 }
147 }
148
149 // Create an empty term list as a place holder for the term information
150 Element term_list = this.doc.createElement (GSXML.TERM_ELEM+GSXML.LIST_MODIFIER);
151 result.appendChild (term_list);
152
153 return result;
154 }//end of synchronized
155 }
156
157 // should probably use a list rather than map
158 protected boolean setStandardQueryParams(HashMap params)
159 {
160 Set entries = params.entrySet();
161 Iterator i = entries.iterator();
162 while (i.hasNext()) {
163 Map.Entry m = (Map.Entry)i.next();
164 String name = (String)m.getKey();
165 String value = (String)m.getValue();
166
167 if (name.equals(OFFSET_PARAM)) {
168 int offset = Integer.parseInt(value);
169 this.audiodb_src.setOffset(offset);
170 }
171 else if (name.equals(LENGTH_PARAM)) {
172 int length = Integer.parseInt(value);
173 this.audiodb_src.setLength(length);
174 }
175 else if (name.equals(RADIUS_PARAM)) {
176 double radius = Double.parseDouble(value);
177 this.audiodb_src.setRadius(radius);
178 }
179 else if (name.equals(MAXDOCS_PARAM)) {
180 int docs = Integer.parseInt(value);
181 this.audiodb_src.setMaxDocs(docs);
182 } // ignore any others
183 }
184 return true;
185 }
186
187
188}
189
190
Note: See TracBrowser for help on using the repository browser.