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 | */
|
---|
18 | package org.greenstone.gsdl3.service;
|
---|
19 |
|
---|
20 |
|
---|
21 | // Greenstone classes
|
---|
22 | import org.greenstone.gsdl3.util.*;
|
---|
23 |
|
---|
24 | // XML classes
|
---|
25 | import org.w3c.dom.Document;
|
---|
26 | import org.w3c.dom.Element;
|
---|
27 | import org.w3c.dom.NodeList;
|
---|
28 |
|
---|
29 | // java
|
---|
30 | import java.util.Vector;
|
---|
31 | import java.util.ArrayList;
|
---|
32 | import java.util.HashMap;
|
---|
33 | import java.util.Map;
|
---|
34 | import java.util.Set;
|
---|
35 | import java.util.Iterator;
|
---|
36 | import java.io.File;
|
---|
37 |
|
---|
38 | import org.apache.log4j.*;
|
---|
39 |
|
---|
40 | public class GS2AudioDBSearch extends AbstractGS2Search {
|
---|
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 = "lhs-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 processTextQuery (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 | result.setAttribute (GSXML.FROM_ATT, TEXT_QUERY_SERVICE); // Result looks the same as a text query
|
---|
71 | result.setAttribute (GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_PROCESS);
|
---|
72 |
|
---|
73 | // Get the parameters of the request
|
---|
74 | Element param_list = (Element) GSXML.getChildByTagName (request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
|
---|
75 | if (param_list == null) {
|
---|
76 | logger.error ("AudioDB TextQuery request had no paramList.");
|
---|
77 | return result; // Return the empty result
|
---|
78 | }
|
---|
79 |
|
---|
80 | // Process the request parameters
|
---|
81 | HashMap params = GSXML.extractParams (param_list, false);
|
---|
82 |
|
---|
83 | // Make sure a query has been specified
|
---|
84 | String query = (String) params.get (QUERY_PARAM);
|
---|
85 | if (query == null || query.equals ("")) {
|
---|
86 | return result; // Return the empty result
|
---|
87 | }
|
---|
88 |
|
---|
89 | // If an index hasn't been specified, use the default
|
---|
90 | String index = (String) params.get (INDEX_PARAM);
|
---|
91 | if (index == null) {
|
---|
92 | index = AUDIODB_DEFAULT_DIRECTORY;
|
---|
93 | }
|
---|
94 |
|
---|
95 | // The location of the audioDB index
|
---|
96 | String index_dir = GSFile.collectionIndexDir (this.site_home, this.cluster_name) + File.separatorChar + index;
|
---|
97 |
|
---|
98 | // set the audioDB query parameters to the values the user has specified
|
---|
99 | setStandardQueryParams (params);
|
---|
100 |
|
---|
101 | this.audiodb_src.runQuery (index_dir, ADB_FILENAME, query);
|
---|
102 | Vector docs = this.audiodb_src.getQueryResult ();
|
---|
103 |
|
---|
104 | if (docs.isEmpty()) {
|
---|
105 | // something has gone wrong
|
---|
106 | GSXML.addError (this.doc, result, "Couldn't query the mg database", GSXML.ERROR_TYPE_SYSTEM);
|
---|
107 | return result;
|
---|
108 | }
|
---|
109 | long totalDocs = docs.size();
|
---|
110 |
|
---|
111 | // Get the docnums out, and convert to HASH ids
|
---|
112 | if (docs.size () == 0) {
|
---|
113 | logger.error ("No results found...\n");
|
---|
114 | }
|
---|
115 |
|
---|
116 | // Create a metadata list to store information about the query results
|
---|
117 | Element metadata_list = this.doc.createElement (GSXML.METADATA_ELEM+GSXML.LIST_MODIFIER);
|
---|
118 | result.appendChild (metadata_list);
|
---|
119 |
|
---|
120 | // Add a metadata element specifying the number of matching documents
|
---|
121 | // because the total number is just the number returned, use numDocsReturned, not numDocsMatched
|
---|
122 | GSXML.addMetadata (this.doc, metadata_list, "numDocsReturned", ""+totalDocs);
|
---|
123 | // 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
|
---|
124 | GSXML.addMetadata (this.doc, metadata_list, "query", query);
|
---|
125 |
|
---|
126 | if (docs.size () > 0) {
|
---|
127 | // Create a document list to store the matching documents, and add them
|
---|
128 | Element document_list = this.doc.createElement (GSXML.DOC_NODE_ELEM+GSXML.LIST_MODIFIER);
|
---|
129 | result.appendChild (document_list);
|
---|
130 | for (int d = 0; d < docs.size (); d++) {
|
---|
131 | String doc_id = ((AudioDBDocInfo) docs.elementAt (d)).oid_;
|
---|
132 | float rank = ((AudioDBDocInfo) docs.elementAt (d)).rank_;
|
---|
133 | Element doc_node = createDocNode (doc_id, Float.toString (rank));
|
---|
134 | document_list.appendChild (doc_node);
|
---|
135 | }
|
---|
136 | }
|
---|
137 |
|
---|
138 | // Create an empty term list as a place holder for the term information
|
---|
139 | Element term_list = this.doc.createElement (GSXML.TERM_ELEM+GSXML.LIST_MODIFIER);
|
---|
140 | result.appendChild (term_list);
|
---|
141 |
|
---|
142 | return result;
|
---|
143 | }//end of synchronized
|
---|
144 | }
|
---|
145 |
|
---|
146 | // should probably use a list rather than map
|
---|
147 | protected boolean setStandardQueryParams(HashMap params)
|
---|
148 | {
|
---|
149 | Set entries = params.entrySet();
|
---|
150 | Iterator i = entries.iterator();
|
---|
151 | while (i.hasNext()) {
|
---|
152 | Map.Entry m = (Map.Entry)i.next();
|
---|
153 | String name = (String)m.getKey();
|
---|
154 | String value = (String)m.getValue();
|
---|
155 |
|
---|
156 | if (name.equals(OFFSET_PARAM)) {
|
---|
157 | int offset = Integer.parseInt(value);
|
---|
158 | this.audiodb_src.setOffset(offset);
|
---|
159 | }
|
---|
160 | else if (name.equals(LENGTH_PARAM)) {
|
---|
161 | int length = Integer.parseInt(value);
|
---|
162 | this.audiodb_src.setLength(length);
|
---|
163 | }
|
---|
164 | else if (name.equals(RADIUS_PARAM)) {
|
---|
165 | double radius = Double.parseDouble(value);
|
---|
166 | this.audiodb_src.setRadius(radius);
|
---|
167 | }
|
---|
168 | else if (name.equals(MAXDOCS_PARAM)) {
|
---|
169 | int docs = Integer.parseInt(value);
|
---|
170 | this.audiodb_src.setMaxDocs(docs);
|
---|
171 | } // ignore any others
|
---|
172 | }
|
---|
173 | return true;
|
---|
174 | }
|
---|
175 |
|
---|
176 |
|
---|
177 | }
|
---|
178 |
|
---|
179 |
|
---|