source: gsdl/trunk/runtime-src/src/oaiservr/recordaction.cpp@ 17546

Last change on this file since 17546 was 16718, checked in by mdewsnip, 16 years ago

Minor fixes for validation.

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