source: main/trunk/greenstone2/runtime-src/src/oaiservr/listsetsaction.cpp

Last change on this file was 27553, checked in by ak19, 11 years ago

Function needed to return a bool in order to compile.

  • Property svn:keywords set to Author Date Id Revision
File size: 10.8 KB
RevLine 
[22739]1/**********************************************************************
2 *
3 * listsetsaction.cpp --
4 *
5 * Copyright (C) 2004-2010 The New Zealand Digital Library Project
6 *
7 * A component of the Greenstone digital library software
8 * from the New Zealand Digital Library Project at the
9 * University of Waikato, New Zealand.
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 *
25 *********************************************************************/
26
[8182]27#include "listsetsaction.h"
28
[20590]29#include "resumptiontoken.h"
[15428]30#include "recptprototools.h"
[8182]31#include "oaitools.h"
32
[20590]33
[8182]34bool listsetsaction::validateAction(recptproto *protocol, oaiargs &params)
[15198]35{
[16717]36 // ----------------------------------------------------------------------------
37 // 1. Check for invalid arguments
38 // ----------------------------------------------------------------------------
39 bool invalid_argument_supplied = false;
[15198]40 text_tmap::const_iterator param_iterator = params.begin();
41 while (param_iterator != params.end())
42 {
[16717]43 // Check for arguments that aren't valid for this action
44 if (param_iterator->first != "verb" &&
45 param_iterator->first != "resumptionToken")
[15198]46 {
[16717]47 // We've found an invalid argument
48 invalid_argument_supplied = true;
49
50 // Delete the invalid argument from the list so it doesn't end up in the <request> tag that is returned
[15198]51 params.erase(param_iterator->first);
52 }
53
54 param_iterator++;
55 }
56
[16717]57 // If we found an invalid argument it's an error, so don't go any further
58 if (invalid_argument_supplied)
59 {
[8182]60 this->errorType = "badArgument";
61 return false;
62 }
63
[16717]64 // ----------------------------------------------------------------------------
65 // 2. Handle any exclusive arguments
66 // ----------------------------------------------------------------------------
67
68 // The resumptionToken argument is exclusive
69 if (params["resumptionToken"] != "")
70 {
71 // This argument is exclusive, so no other arguments are allowed (except "verb" of course)
72 if (params.getSize() != 2)
73 {
74 this->errorType = "badArgument";
75 return false;
76 }
77
78 // Check the resumption token is valid
[15387]79 ResumptionToken token(params["resumptionToken"]);
[20590]80 if (token.isValid())
[16717]81 {
82 // Everything is fine, and we don't continue further because this is an exclusive argument
83 this->errorType = "";
84 return true;
85 }
86 else
87 {
88 // There was an error with the resumption token
89 this->errorType = "badResumptionToken";
90 return false;
91 }
[15387]92 }
93
[16717]94 // ----------------------------------------------------------------------------
95 // 3. Handle any required arguments
96 // ----------------------------------------------------------------------------
97
98 // None!
99
100 // ----------------------------------------------------------------------------
101 // 4. Check any remaining arguments
102 // ----------------------------------------------------------------------------
103
104 // None!
105
106 // If we've reached here everything must be fine
107 this->errorType = "";
[8182]108 return true;
109}
110
111
112bool listsetsaction::output_content(ostream &output, recptproto *protocol, oaiargs &params)
113{
[20590]114 // Reset variables
115 this->setsOutput = 0;
[8182]116
[20626]117 text_t position = "";
[20590]118
119 // Process the resumptionToken if there is one
120 if (params["resumptionToken"] != "")
121 {
122 ResumptionToken resumption_token(params["resumptionToken"]);
[20626]123 position = resumption_token.getPosition();
[8303]124 }
[8182]125
[27528]126 // if no resumption token, output the super colls. Otherwise, start from
127 // the collection list correct position.
[27534]128 // Assume for now that super coll list is always first in the list of sets,
129 // and we never issue a resumption token part way through the list of super colls
130 // TODO handle resumption tokens properly.
[27528]131 text_t set_name ="";
132 if (position == "") {
133 text_tarray& supercolls = this->configuration->getSuperCollectionsList();
[27534]134 text_tarray::iterator supercoll_iterator = supercolls.begin();
135 while (supercoll_iterator != supercolls.end()) {
136 set_name = (*supercoll_iterator);
137 output_content_for_supercol(output, set_name);
138 supercoll_iterator++;
[27528]139 }
140 }
[27534]141
142
143 // Get a list of the collections available
144 text_tarray& collections = this->configuration->getCollectionsList();
145 if (collections.size() == 0)
146 {
147 if (this->setsOutput ==0) {
148 // we had no super colls either
149 return false;
150 }
151 return true;
152 }
153
[20626]154 // Get the current collection from the position value
155 text_t collection_name = "";
156 oaiclassifier::toGSDL(collection_name, position);
157
[20590]158 // Find the starting collection
159 text_tarray::iterator collection_iterator = collections.begin();
160 while (collection_iterator != collections.end())
161 {
[20626]162 if (collection_name == "" || collection_name == *collection_iterator)
[15387]163 {
164 break;
165 }
[8182]166
[20590]167 collection_iterator++;
168 }
169
170 // Now loop through the remaining collections
171 while (collection_iterator != collections.end())
172 {
[20626]173 collection_name = (*collection_iterator);
[20590]174
175 // If we've output the desired number of records return a resumptionToken and we're done
176 if (this->setsOutput == this->configuration->resumeAfter())
[15387]177 {
[20626]178 ResumptionToken resumption_token("", "", "", "", "", collection_name);
[20590]179
180 // Don't add any whitespace around the resumption token as it can confuse harvesters/validators
181 output << " <resumptionToken>" << resumption_token.getResumptionTokenString() << "</resumptionToken>" << endl;
182 return true;
[15387]183 }
184
[20590]185 // If output_content_for_col() returns false a resumption token has been output, so it's time to stop
[20626]186 if (output_content_for_col(output, protocol, params, collection_name) == false)
[20590]187 {
188 return true;
[8182]189 }
190
[20590]191 collection_iterator++;
[15387]192 }
193
[8182]194 return true;
195}
196
[27528]197bool listsetsaction::output_content_for_supercol(ostream &output, text_t supercoll) {
198 utf8outconvertclass utf8convert;
199 output << utf8convert << " <set>\n";
200 output << utf8convert << " <setSpec>" << supercoll << "</setSpec>\n";
201 if (this->configuration->getSetName(supercoll) != "")
202 {
203 output << utf8convert << " <setName>" << this->configuration->getSetName(supercoll) << "</setName>\n";
204 }
205 else
206 {
207 output << utf8convert << " <setName>" << supercoll << "</setName>\n";
208 }
209 if (this->configuration->getSetDescription(supercoll) != "")
210 {
211 output << utf8convert << " <setDescription>" << this->configuration->getSetDescription(supercoll) << "</setDescription>\n";
212 }
213 output << utf8convert << " </set>\n";
214 this->setsOutput++;
[27553]215 return true;
[27528]216}
217
[20590]218
[27528]219
220
[20590]221bool listsetsaction::output_content_for_col(ostream &output, recptproto *protocol, oaiargs &params, text_t collection)
[8182]222{
[20708]223 utf8outconvertclass utf8convert;
[20590]224 text_t position = "";
[8182]225
[20590]226 // Process the resumptionToken if there is one
227 if (params["resumptionToken"] != "")
[15387]228 {
[20590]229 ResumptionToken resumption_token(params["resumptionToken"]);
230 position = resumption_token.getPosition();
[15387]231 }
232
[20590]233 // Get the list of sets in this collection
234 // Collections should not contain too many sets otherwise this will use a lot of time and memory
235 text_tset metadata; // Must be empty for efficiency
236 FilterResponse_t sets_response;
237 get_children("browse", collection, "", metadata, false, protocol, sets_response, *this->logout);
[8182]238
[20590]239 // Find the starting position, if necessary
240 ResultDocInfo_tarray::iterator set_iterator = sets_response.docInfo.begin();
241 if (this->setsOutput == 0)
242 {
243 while (set_iterator != sets_response.docInfo.end())
244 {
[20626]245 if (position == "" || position == (collection + ":" + (*set_iterator).OID))
[20590]246 {
247 break;
[8182]248 }
249
[20590]250 set_iterator++;
[8182]251 }
252 }
253
[20590]254 // Output the collection as a set
[20626]255 if (position == "" || position == collection)
[15387]256 {
[20708]257 output << utf8convert << " <set>\n";
258 output << utf8convert << " <setSpec>" << collection << "</setSpec>\n";
[20629]259 if (this->configuration->getSetName(collection) != "")
260 {
[20708]261 output << utf8convert << " <setName>" << this->configuration->getSetName(collection) << "</setName>\n";
[20629]262 }
263 else
264 {
[20708]265 output << utf8convert << " <setName>" << collection << "</setName>\n";
[20629]266 }
267 if (this->configuration->getSetDescription(collection) != "")
268 {
[20708]269 output << utf8convert << " <setDescription>" << this->configuration->getSetDescription(collection) << "</setDescription>\n";
[20629]270 }
[20708]271 output << utf8convert << " </set>\n";
[15387]272 this->setsOutput++;
[8182]273 }
274
[20590]275 // Now loop through displaying the next matching records
276 while (set_iterator != sets_response.docInfo.end())
277 {
278 text_t set = (*set_iterator).OID;
[8182]279
[20590]280 // Only classifiers with supportsmemberof become OAI sets, for reasons I don't really understand
281 text_tset set_metadata;
282 set_metadata.insert("supportsmemberof");
283 set_metadata.insert("Title");
284 FilterResponse_t set_response;
285 get_info(set, collection, "", set_metadata, false, protocol, set_response, *this->logout);
[8182]286
[20590]287 if (set_response.docInfo[0].metadata["supportsmemberof"].values.size() > 0 && set_response.docInfo[0].metadata["supportsmemberof"].values[0] == "true")
288 {
289 // If we've output the desired number of records return a resumptionToken and we're done
290 if (this->setsOutput == this->configuration->resumeAfter())
291 {
[20626]292 ResumptionToken resumption_token("", "", "", "", "", collection + ":" + set);
[8182]293
[20590]294 // Don't add any whitespace around the resumption token as it can confuse harvesters/validators
295 output << " <resumptionToken>" << resumption_token.getResumptionTokenString() << "</resumptionToken>" << endl;
296 return false;
297 }
298
299 // Otherwise output this set and increment the count
[20708]300 output << utf8convert << " <set>\n";
301 output << utf8convert << " <setSpec>" << collection << ":" << set << "</setSpec>\n";
[20629]302 if (this->configuration->getSetName(collection + ":" + set) != "")
303 {
[20708]304 output << utf8convert << " <setName>" << this->configuration->getSetName(collection + ":" + set) << "</setName>\n";
[20629]305 }
306 else
307 {
[20708]308 output << utf8convert << " <setName>" << set_response.docInfo[0].metadata["Title"].values[0] << "</setName>\n";
[20629]309 }
310 if (this->configuration->getSetDescription(collection + ":" + set) != "")
311 {
[20708]312 output << utf8convert << " <setDescription>" << this->configuration->getSetDescription(collection + ":" + set) << "</setDescription>\n";
[20629]313 }
[20708]314 output << utf8convert << " </set>\n";
[20590]315 this->setsOutput++;
316 }
317
318 set_iterator++;
[8182]319 }
[20590]320
321 return true;
[8182]322}
Note: See TracBrowser for help on using the repository browser.