source: main/trunk/greenstone2/runtime-src/src/oaiservr/recordaction.cpp@ 22739

Last change on this file since 22739 was 22739, checked in by mdewsnip, 14 years ago

Added copyright header to runtime-src/src/oaiserver/*.cpp and runtime-src/src/oaiserver/*.h.

  • Property svn:keywords set to Author Date Id Revision
File size: 7.8 KB
Line 
1/**********************************************************************
2 *
3 * recordaction.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
27#include "recordaction.h"
28#include "recptprototools.h"
29#include "dublincore.h"
30#include "qualified_dublincore.h"
31#include "rfc1807.h"
32#include "oaitools.h"
33
34
35recordaction::recordaction(text_tset &metadataset) : oaiaction("GetRecord") {
36 metaformatptr fptr;
37
38 if (metadataset.count("oai_dc") > 0) {
39 fptr.set_class(new dublin_core());
40 this->formatMap[fptr.get_class()->formatName()] = fptr;
41 }
42
43 if (metadataset.count("gsdl_qdc") > 0) {
44 fptr.set_class(new qualified_dublin_core());
45 this->formatMap[fptr.get_class()->formatName()] = fptr;
46 }
47
48 if (metadataset.count("rfc1807") > 0) {
49
50 fptr.set_class(new rfc1807());
51 this->formatMap[fptr.get_class()->formatName()] = fptr;
52 }
53}
54
55recordaction::~recordaction() {
56 metaformat_map::iterator here = this->formatMap.begin();
57 metaformat_map::iterator end = this->formatMap.end();
58
59 while (here != end) {
60 here->second.clear();
61 ++here;
62 }
63}
64
65
66bool recordaction::validateAction(recptproto *protocol, oaiargs &params)
67{
68 // ----------------------------------------------------------------------------
69 // 1. Check for invalid arguments
70 // ----------------------------------------------------------------------------
71 bool invalid_argument_supplied = false;
72 text_tmap::const_iterator param_iterator = params.begin();
73 while (param_iterator != params.end())
74 {
75 // Check for arguments that aren't valid for this action
76 if (param_iterator->first != "verb" &&
77 param_iterator->first != "identifier" &&
78 param_iterator->first != "metadataPrefix")
79 {
80 // We've found an invalid argument
81 invalid_argument_supplied = true;
82
83 // Delete the invalid argument from the list so it doesn't end up in the <request> tag that is returned
84 params.erase(param_iterator->first);
85 }
86
87 param_iterator++;
88 }
89
90 // If we found an invalid argument it's an error, so don't go any further
91 if (invalid_argument_supplied)
92 {
93 this->errorType = "badArgument";
94 return false;
95 }
96
97 // ----------------------------------------------------------------------------
98 // 2. Handle any exclusive arguments
99 // ----------------------------------------------------------------------------
100
101 // None!
102
103 // ----------------------------------------------------------------------------
104 // 3. Handle any required arguments
105 // ----------------------------------------------------------------------------
106
107 // The "metadataPrefix" argument is required
108 text_t metadataPrefix = params["metadataPrefix"];
109
110 // Check that the metadataPrefix argument exists
111 if (metadataPrefix == "")
112 {
113 this->errorType = "badArgument";
114 return false;
115 }
116
117 // Check that the metadataPrefix is a format we support
118 if (this->formatNotSupported(metadataPrefix))
119 {
120 this->errorType = "cannotDisseminateFormat";
121 return false;
122 }
123
124 // The "identifier" argument is required
125 text_t identifier = params["identifier"];
126
127 // Check that the identifier argument exists
128 if (identifier == "")
129 {
130 this->errorType = "badArgument";
131 return false;
132 }
133
134 // Extract the collection name from the identifier specification
135 text_t collection = "";
136 oaiclassifier::toGSDL(collection, identifier);
137
138 // Check a document with the specified identifier exists
139 text_tset metadata;
140 if (!get_info(identifier, collection, "", metadata, false, protocol, this->gsdlResponse, *logout))
141 {
142 this->errorType = "idDoesNotExist";
143 return false;
144 }
145
146 // ----------------------------------------------------------------------------
147 // 4. Check any remaining arguments
148 // ----------------------------------------------------------------------------
149
150 // None!
151
152 // If we've reached here everything must be fine
153 this->errorType = "";
154 return true;
155}
156
157//
158// Output the content of a GetRecord request; in this case, the static member
159// output_record below is used to fulfill most of the request
160//
161bool recordaction::output_content(ostream &output, recptproto *protocol, oaiargs &params)
162{
163 // validateAction will already have set up the correct response content
164 text_t gsdlId = params["identifier"];
165 text_t gsdlCollect;
166
167 // convert record identifier into GSDL format from OAI
168 oaiclassifier::toGSDL(gsdlCollect, gsdlId);
169
170 // go direct to output_record
171 return output_record(output, gsdlCollect, gsdlId, params["metadataPrefix"]);
172}
173
174//
175// A static member that does everything the output_content method above needs,
176// but can be called when individual records are being output for another
177// action.
178//
179bool recordaction::output_record(ostream &output, recptproto *protocol, const text_t &collection, const text_t &OID, const text_t &metadataPrefix)
180{
181 text_tset metadata;
182 ofstream logout("oai.log", ios::app);
183
184 // get the document information
185 if (!get_info(OID, collection, "", metadata, false, protocol, this->gsdlResponse, logout)) {
186
187 this->errorType = "idDoesNotExist";
188
189 if(this->configuration->getOAIVersion() >= 200) {
190 this->output_error(output, errorType);
191 return false;
192 }
193 }
194
195 return this->output_record(output, collection, OID, metadataPrefix);
196}
197
198bool recordaction::output_record(ostream &output, const text_t &collection, const text_t &OID,
199 const text_t &metadataPrefix)
200{ int oaiVersion = this->configuration->getOAIVersion();
201
202 // check to see if it's a classifier
203 text_t childHead;
204 text_t::const_iterator start = OID.begin();
205 text_t::const_iterator here = OID.begin();
206 here += 2;
207 childHead = substr(start, here);
208
209 // if it isn't a document, kill it now
210 if (childHead == "CL") {
211 cerr << "Not a document" << endl;
212 return false;
213 }
214
215 ResultDocInfo_t doc_info = this->gsdlResponse.docInfo[0];
216 text_t lastModified = "";
217
218 // Fills lastModified with the date from the document in doc_info, in the format YYYY-MM-DD
219 this->getLastModifiedDate(doc_info, lastModified);
220
221 // If the ID exists, output record for oai response (OAI v1.1)
222 // OAI v2.0 will already have bailed if ID doesn't exist (yes?)
223 if (this->errorType != "idDoesNotExist") {
224 text_t oaiLabel = OID;
225 oaiclassifier::toOAI(this->configuration->getRepositoryId(), collection, oaiLabel); // Concatenates HASH id to collection, which OAI needs
226
227 // output a record
228 output << " <record>\n";
229
230 // output header part of oai response
231 this->output_record_header(output, oaiLabel, lastModified,
232 doc_info.metadata["memberof"].values, oaiVersion);
233
234 if (this->errorType != "cannotDisseminateFormat"){
235 if (this->formatMap[metadataPrefix].get_class()->output_metadata(output, collection, doc_info)) {
236 // output 'about' part of oai response - we probably won't ever use this
237 //output << " <about>\n";
238 //output << " </about>\n";
239 }
240 }
241 // close record
242 output << " </record>\n\n";
243 }
244 return true;
245}
Note: See TracBrowser for help on using the repository browser.