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

Last change on this file since 22289 was 22289, checked in by kjdon, 11 years ago

ids are now like oai:repos-id:collection:doc

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