root/gsdl/trunk/runtime-src/src/oaiservr/recordaction.cpp @ 16715

Revision 16715, 6.2 KB (checked in by mdewsnip, 12 years ago)

Tidied up the validateAction() function to be consistently structured.

  • Property svn:keywords set to Author Date Id Revision
Line 
1#include "recordaction.h"
2#include "recptprototools.h"
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();
25    ++here;
26  }
27}
28
29
30bool recordaction::validateAction(recptproto *protocol, oaiargs &params)
31{
32  // ----------------------------------------------------------------------------
33  //  1. Check for invalid arguments
34  // ----------------------------------------------------------------------------
35  bool invalid_argument_supplied = false;
36  text_tmap::const_iterator param_iterator = params.begin();
37  while (param_iterator != params.end())
38  {
39    // Check for arguments that aren't valid for this action
40    if (param_iterator->first != "verb" &&
41    param_iterator->first != "identifier" &&
42    param_iterator->first != "metadataPrefix")
43    {
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
48      params.erase(param_iterator->first);
49    }
50
51    param_iterator++;
52  }
53
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  }
60
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  {
77    this->errorType = "badArgument";
78    return false;
79  }
80  // Check that the metadataPrefix is a format we support
81  if (this->formatNotSupported(metadataPrefix))
82  {
83    this->errorType = "cannotDisseminateFormat";
84    return false;
85  }
86
87  // The "identifier" argument is required
88  text_t identifier = params["identifier"];
89
90  // Extract the collection name from the identifier specification
91  text_t collection = "";
92  oaiclassifier::toGSDL(collection, identifier);
93 
94  // Check a document with the specified identifier exists
95  text_tset metadata;
96  if (!get_info(identifier, collection, "", metadata, false, protocol, this->gsdlResponse, *logout))
97  {
98    this->errorType = "idDoesNotExist";
99    return false;
100  }
101
102  // ----------------------------------------------------------------------------
103  // 4. Check any remaining arguments
104  // ----------------------------------------------------------------------------
105
106  // None!
107
108  // If we've reached here everything must be fine
109  this->errorType = "";
110  return true;
111}
112
113//
114// Output the content of a GetRecord request; in this case, the static member
115// output_record below is used to fulfill most of the request
116//
117bool recordaction::output_content(ostream &output, recptproto *protocol, oaiargs &params)
118{
119  // validateAction will already have set up the correct response content
120  text_t gsdlId = params["identifier"];
121  text_t gsdlCollect;
122
123  // convert record identifier into GSDL format from OAI
124  oaiclassifier::toGSDL(gsdlCollect, gsdlId);
125 
126  // go direct to output_record
127  return output_record(output, gsdlCollect, gsdlId, params["metadataPrefix"]);
128}
129
130//
131// A static member that does everything the output_content method above needs,
132// but can be called when individual records are being output for another
133// action.
134//
135bool recordaction::output_record(ostream &output, recptproto *protocol, const text_t &collection, const text_t &OID, const text_t &metadataPrefix)
136{
137  text_tset        metadata;
138  ofstream         logout("oai.log", ios::app);
139
140  // get the document information
141  if (!get_info(OID, collection, "", metadata, false, protocol, this->gsdlResponse, logout)) {
142
143    this->errorType = "idDoesNotExist";
144
145    if(this->configuration->getOAIVersion() >= 200) {
146      this->output_error(output, errorType);
147      return false;
148    }
149  }
150
151  return this->output_record(output, collection, OID, metadataPrefix);
152
153
154bool recordaction::output_record(ostream &output, const text_t &collection, const text_t &OID,
155                 const text_t &metadataPrefix)
156{ int oaiVersion = this->configuration->getOAIVersion();
157
158  // check to see if it's a classifier
159  text_t childHead;
160  text_t::const_iterator start = OID.begin();
161  text_t::const_iterator here  = OID.begin();
162  here += 2;
163  childHead = substr(start, here);
164 
165  // if it isn't a document, kill it now
166  if (childHead == "CL") {
167    cerr << "Not a document" << endl;
168    return false;
169  }
170
171  ResultDocInfo_t doc_info = this->gsdlResponse.docInfo[0];
172  text_t lastModified = "";
173
174  // Fills lastModified with the date from the document in doc_info, in the format YYYY-MM-DD
175  this->getLastModifiedDate(doc_info, lastModified);
176
177  // If the ID exists, output record for oai response (OAI v1.1)
178  // OAI v2.0 will already have bailed if ID doesn't exist (yes?)
179  if (this->errorType != "idDoesNotExist") {
180    text_t oaiLabel = OID;
181    oaiclassifier::toOAI(collection, oaiLabel); // Concatenates HASH id to collection, which OAI needs
182   
183    // output a record
184    output << "  <record>\n";
185   
186    // output header part of oai response
187    this->output_record_header(output, oaiLabel, lastModified,
188                   doc_info.metadata["memberof"].values, oaiVersion);
189   
190    if (this->errorType != "cannotDisseminateFormat"){
191      if (this->formatMap[metadataPrefix].get_class()->output_metadata(output, collection, doc_info)) {
192    //  output 'about' part of oai response - we probably won't ever use this
193    //output << "    <about>\n";
194    //output << "    </about>\n";
195      }
196    }
197    // close record
198    output << "  </record>\n\n";
199  }
200  return true;
201}
Note: See TracBrowser for help on using the browser.