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

Last change on this file since 19294 was 18889, checked in by kjdon, 15 years ago

constructor now takes a text_tset containing a list of metadata set names. Only those listed will be used

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