1 | /*
|
---|
2 | * NzdlResponse.java
|
---|
3 | * Copyright (C) 2001 New Zealand Digital Library Project
|
---|
4 | *
|
---|
5 | * This program is free software; you can redistribute it and/or modify
|
---|
6 | * it under the terms of the GNU General Public License as published by
|
---|
7 | * the Free Software Foundation; either version 2 of the License, or
|
---|
8 | * (at your option) any later version.
|
---|
9 | *
|
---|
10 | * This program is distributed in the hope that it will be useful,
|
---|
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
13 | * GNU General Public License for more details.
|
---|
14 | *
|
---|
15 | * You should have received a copy of the GNU General Public License
|
---|
16 | * along with this program; if not, write to the Free Software
|
---|
17 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
18 | */
|
---|
19 |
|
---|
20 | //the package we're in
|
---|
21 | package org.nzdl.gsdl.service;
|
---|
22 |
|
---|
23 | import java.util.HashMap;
|
---|
24 | import java.util.HashSet;
|
---|
25 | import java.util.Map;
|
---|
26 | import java.util.Set;
|
---|
27 | import java.util.List;
|
---|
28 | import java.util.ArrayList;
|
---|
29 |
|
---|
30 | import org.nzdl.gsdl.service.*;
|
---|
31 | import org.nzdl.gsdl.corba.gsdlInterface.*;
|
---|
32 | import org.nzdl.gsdl.util.*;
|
---|
33 |
|
---|
34 | /**
|
---|
35 | * An object that holds data from a response after a
|
---|
36 | * {@link NzdlRequest NzdlRequest} has been serviced by
|
---|
37 | * {@link NzdlService NzdlService}.
|
---|
38 | *
|
---|
39 | * @author Stuart Yeates ([email protected])
|
---|
40 | * @author Aziz Mahoui ([email protected])
|
---|
41 | * @author Gordon Paynter ([email protected])
|
---|
42 | * @author Brett Sheeran ([email protected]) (comments)
|
---|
43 | * @version $Revision: 2232 $
|
---|
44 | */
|
---|
45 |
|
---|
46 | public class NzdlResponse extends java.lang.Object {
|
---|
47 |
|
---|
48 | private corbaFilterResponseHolder m_responseHolder = null;
|
---|
49 | private corbaFilterResponse m_response = null;
|
---|
50 | private NzdlResultSet m_resultSet = null;
|
---|
51 | private Map m_frequencies = null;
|
---|
52 | private List m_queryTerms = null;
|
---|
53 |
|
---|
54 | /**
|
---|
55 | * Create an empty instance of a NzdlResponse object.
|
---|
56 | */
|
---|
57 | public NzdlResponse( ) {
|
---|
58 | m_responseHolder = NzdlCorbaFactory.createFilterResponseHolder();
|
---|
59 | m_response = m_responseHolder.value;
|
---|
60 | }
|
---|
61 |
|
---|
62 | /**
|
---|
63 | *
|
---|
64 | */
|
---|
65 | public void copyData(NzdlResponse old) {
|
---|
66 | m_responseHolder = old.m_responseHolder ;
|
---|
67 | m_response = old.m_response;
|
---|
68 | m_resultSet = old.m_resultSet;
|
---|
69 | m_frequencies = old.m_frequencies;
|
---|
70 | m_queryTerms = old.m_queryTerms;
|
---|
71 | }
|
---|
72 |
|
---|
73 | /**
|
---|
74 | * Get the corbaFilterReponse.
|
---|
75 | */
|
---|
76 | public corbaFilterResponse getResponse() {
|
---|
77 | return m_response;
|
---|
78 | }
|
---|
79 |
|
---|
80 | /**
|
---|
81 | * Set the corbaFilterReponse.
|
---|
82 | */
|
---|
83 | public void setResponse(corbaFilterResponse response) {
|
---|
84 | m_response = response;
|
---|
85 | m_responseHolder.value = response;
|
---|
86 | parseResponse();
|
---|
87 | }
|
---|
88 |
|
---|
89 | /**
|
---|
90 | * Get the corbaFilterReponse holder.
|
---|
91 | */
|
---|
92 | public corbaFilterResponseHolder getHolder() {
|
---|
93 | return m_responseHolder;
|
---|
94 | }
|
---|
95 |
|
---|
96 | /**
|
---|
97 | * Get the result of {@link NzdlService NzdlService} servicing a
|
---|
98 | * {@link NzdlRequest NzdlRequest}. <br>
|
---|
99 | * Pre: <br>
|
---|
100 | * - Created instances of: NzdlRequest, NzdlService and
|
---|
101 | * {@link NzdlResponse NzdlResponse}. <br>
|
---|
102 | * - NzdlService has serviced NzdlRequest. <br>
|
---|
103 | * - Created instance of {@link NzdlResultSet NzdlResultSet}. <br>
|
---|
104 | * Post: Copied result data from a NzdlResponse object into a NzdlResult
|
---|
105 | * set object <br><br>
|
---|
106 | *
|
---|
107 | * <B>Example Code</B> <br>
|
---|
108 | * <TT>NzdlQuery query = new NzdlQuery( "snail farming" ); <br>
|
---|
109 | * NzdlRequest request = new NzdlRequest( query ); <br>
|
---|
110 | * NzdlResponse response = new NzdlResponse( ); <br><br>
|
---|
111 | * // use service to get response from request to "demo" collection <br>
|
---|
112 | * NzdlService myService = new NzdlServiceClient( _args ); <br>
|
---|
113 | * myService.service( "demo", request, response ); <br><br>
|
---|
114 | * // use getResultSet to get "results" from "response" <br>
|
---|
115 | * NzdlResultSet results = <B>response.getResultSet();</B> </TT>
|
---|
116 | */
|
---|
117 | public NzdlResultSet getResultSet() {
|
---|
118 | if ( m_resultSet == null ) {
|
---|
119 | parseResponse();
|
---|
120 | }
|
---|
121 | return m_resultSet;
|
---|
122 | }
|
---|
123 |
|
---|
124 | /*
|
---|
125 | This is a short expansion of the content of a corbaFilterResponse object.
|
---|
126 | Some of the fields are not that clear to me (those with ?????). Therefore
|
---|
127 | I am ignoring some of them for the time being.
|
---|
128 |
|
---|
129 | o Number of Documents
|
---|
130 | o Type of results
|
---|
131 | o Info on each of the search terms used:
|
---|
132 |
|
---|
133 | o Term string
|
---|
134 | o Term frequency
|
---|
135 | o Matched terms ?????
|
---|
136 |
|
---|
137 | o Info on each of the result docs returned:
|
---|
138 |
|
---|
139 | o Document ID
|
---|
140 | o Result number ?????
|
---|
141 | o Number of terms matched in this document
|
---|
142 | o Number of phrases matched in this document
|
---|
143 | o Array of document frequencies ?????
|
---|
144 | o Info on the metadata:
|
---|
145 |
|
---|
146 | o Array of the names of the meta tags
|
---|
147 | o Info on each of the meta data:
|
---|
148 |
|
---|
149 | o Params string ?????
|
---|
150 | o Is the metadata a reference ?????
|
---|
151 | o Array of values
|
---|
152 | o Id ?????
|
---|
153 | o Parentid ?????
|
---|
154 | */
|
---|
155 |
|
---|
156 |
|
---|
157 | /**
|
---|
158 | * Parse the CORBA filter response object
|
---|
159 | */
|
---|
160 | private void parseResponse () {
|
---|
161 | m_response = m_responseHolder.value;
|
---|
162 | m_resultSet = new NzdlResultSet();
|
---|
163 | m_resultSet.setNumOfDocs( m_response.numDocs );
|
---|
164 | if (m_response.isApprox == null)
|
---|
165 | throw new Error ("null m_response.isApprox in parseResponse");
|
---|
166 | m_resultSet.setResultType( m_response.isApprox.value() );
|
---|
167 | /** HAS SIDE EFFECT ! */
|
---|
168 | parseTermsInfo( m_response.termInfo );
|
---|
169 | /** HAS SIDE EFFECT ! */
|
---|
170 | parseDocsInfo( m_response.docInfo );
|
---|
171 | }
|
---|
172 |
|
---|
173 | private void parseTermsInfo( corbaTermInfo [] _termsInfo ) {
|
---|
174 | String term = null;
|
---|
175 | m_frequencies = new HashMap();
|
---|
176 | m_queryTerms = new ArrayList();
|
---|
177 | for (int i=0; i<_termsInfo.length; i++) {
|
---|
178 | term = NzdlCorbaFactory.toString(_termsInfo[i].term);
|
---|
179 | m_queryTerms.add( term );
|
---|
180 | m_frequencies.put( term, new Long(_termsInfo[i].frequency) );
|
---|
181 | }
|
---|
182 | m_resultSet.setQueryTerms( m_queryTerms );
|
---|
183 | m_resultSet.setTermFrequencies( m_frequencies );
|
---|
184 | }
|
---|
185 |
|
---|
186 | private void parseDocsInfo( corbaResultDocInfo [] _docsInfo ) {
|
---|
187 | NzdlQueryHit queryHit = null;
|
---|
188 | //System.err.println("Size of DocsInfo: " + _docsInfo.length);
|
---|
189 | for (int i=0; i < _docsInfo.length; i++) {
|
---|
190 | queryHit = new NzdlQueryHit();
|
---|
191 | queryHit.setDocumentID( NzdlCorbaFactory.toString(_docsInfo[i].OID) );
|
---|
192 | queryHit.setResultNumber( _docsInfo[i].resultNum );
|
---|
193 | queryHit.setRanking( _docsInfo[i].ranking );
|
---|
194 | queryHit.setNumOfTermsMatched( _docsInfo[i].termsMatched );
|
---|
195 | queryHit.setNumOfPhrasesMatched( _docsInfo[i].phraseMatched );
|
---|
196 | /** TO DO: extract corbaDocFreq_array value */
|
---|
197 | queryHit.setMetaData( parseMetaData(_docsInfo[i].metadata) );
|
---|
198 | // add to the result set
|
---|
199 | m_resultSet.add( queryHit ) ;
|
---|
200 | }
|
---|
201 | }
|
---|
202 |
|
---|
203 | private Map parseMetaData( corbaMetadataInfo_map _metaData ) {
|
---|
204 | /*
|
---|
205 | * The key in the map will be the metaData Tag
|
---|
206 | * The value for the key will be a Set of strings
|
---|
207 | * Ex: key = "Author", value = {A. Mahoui, S. Franks}
|
---|
208 | */
|
---|
209 | Map metaData = new HashMap();
|
---|
210 | // For each meta tag can be associated more than one value (example above)
|
---|
211 | Set metaTagValues;
|
---|
212 | corbaMetadataInfo c_metaDataInfo = null;
|
---|
213 |
|
---|
214 | for (int i=0; i < _metaData.names.length; i++) {
|
---|
215 | // one meta data at a time please ...
|
---|
216 | c_metaDataInfo = _metaData.values[i];
|
---|
217 |
|
---|
218 | // get a fresh meta tag values item
|
---|
219 | metaTagValues = new HashSet();
|
---|
220 |
|
---|
221 | // get the set of values associated with this meta tag ...
|
---|
222 | for (int j=0; j < c_metaDataInfo.values.length; j++) {
|
---|
223 | metaTagValues.add( NzdlCorbaFactory.toString(c_metaDataInfo.values[j]) );
|
---|
224 | }
|
---|
225 |
|
---|
226 | // map the meta tag with its set of values ...
|
---|
227 | metaData.put( NzdlCorbaFactory.toString(_metaData.names[i]), metaTagValues);
|
---|
228 | }
|
---|
229 | return metaData;
|
---|
230 | }
|
---|
231 |
|
---|
232 | }
|
---|
233 |
|
---|