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

Revision 18861, 6.5 KB (checked in by kjdon, 11 years ago)

load up the new qualified dublin core metaformat

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