[8182] | 1 | #include "recordaction.h"
|
---|
[15428] | 2 | #include "recptprototools.h"
|
---|
[8182] | 3 | #include "dublincore.h"
|
---|
[18861] | 4 | #include "qualified_dublincore.h"
|
---|
[8182] | 5 | #include "rfc1807.h"
|
---|
| 6 | #include "oaitools.h"
|
---|
| 7 | #include <time.h>
|
---|
| 8 |
|
---|
| 9 |
|
---|
| 10 | recordaction::recordaction() : oaiaction("GetRecord") {
|
---|
| 11 | metaformatptr fptr;
|
---|
| 12 |
|
---|
| 13 | fptr.set_class(new dublin_core());
|
---|
| 14 | this->formatMap[fptr.get_class()->formatName()] = fptr;
|
---|
| 15 |
|
---|
[18861] | 16 | fptr.set_class(new qualified_dublin_core());
|
---|
| 17 | this->formatMap[fptr.get_class()->formatName()] = fptr;
|
---|
| 18 |
|
---|
[8182] | 19 | fptr.set_class(new rfc1807());
|
---|
| 20 | this->formatMap[fptr.get_class()->formatName()] = fptr;
|
---|
| 21 | }
|
---|
| 22 |
|
---|
| 23 | recordaction::~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();
|
---|
[9608] | 29 | ++here;
|
---|
[8182] | 30 | }
|
---|
| 31 | }
|
---|
| 32 |
|
---|
| 33 |
|
---|
| 34 | bool recordaction::validateAction(recptproto *protocol, oaiargs ¶ms)
|
---|
[15198] | 35 | {
|
---|
[16715] | 36 | // ----------------------------------------------------------------------------
|
---|
| 37 | // 1. Check for invalid arguments
|
---|
| 38 | // ----------------------------------------------------------------------------
|
---|
| 39 | bool invalid_argument_supplied = false;
|
---|
[15198] | 40 | text_tmap::const_iterator param_iterator = params.begin();
|
---|
| 41 | while (param_iterator != params.end())
|
---|
| 42 | {
|
---|
[16715] | 43 | // Check for arguments that aren't valid for this action
|
---|
[15198] | 44 | if (param_iterator->first != "verb" &&
|
---|
| 45 | param_iterator->first != "identifier" &&
|
---|
| 46 | param_iterator->first != "metadataPrefix")
|
---|
| 47 | {
|
---|
[16715] | 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
|
---|
[15198] | 52 | params.erase(param_iterator->first);
|
---|
| 53 | }
|
---|
| 54 |
|
---|
| 55 | param_iterator++;
|
---|
| 56 | }
|
---|
| 57 |
|
---|
[16715] | 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 | }
|
---|
[8182] | 64 |
|
---|
[16715] | 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 | {
|
---|
[8182] | 81 | this->errorType = "badArgument";
|
---|
| 82 | return false;
|
---|
| 83 | }
|
---|
[16718] | 84 |
|
---|
[16715] | 85 | // Check that the metadataPrefix is a format we support
|
---|
| 86 | if (this->formatNotSupported(metadataPrefix))
|
---|
| 87 | {
|
---|
[8182] | 88 | this->errorType = "cannotDisseminateFormat";
|
---|
[16715] | 89 | return false;
|
---|
[8182] | 90 | }
|
---|
| 91 |
|
---|
[16715] | 92 | // The "identifier" argument is required
|
---|
| 93 | text_t identifier = params["identifier"];
|
---|
| 94 |
|
---|
[16718] | 95 | // Check that the identifier argument exists
|
---|
| 96 | if (identifier == "")
|
---|
| 97 | {
|
---|
| 98 | this->errorType = "badArgument";
|
---|
| 99 | return false;
|
---|
| 100 | }
|
---|
| 101 |
|
---|
[16715] | 102 | // Extract the collection name from the identifier specification
|
---|
| 103 | text_t collection = "";
|
---|
| 104 | oaiclassifier::toGSDL(collection, identifier);
|
---|
[8182] | 105 |
|
---|
[16715] | 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 | {
|
---|
[8182] | 110 | this->errorType = "idDoesNotExist";
|
---|
[16715] | 111 | return false;
|
---|
[8182] | 112 | }
|
---|
| 113 |
|
---|
[16715] | 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 = "";
|
---|
[8182] | 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 | //
|
---|
| 129 | bool recordaction::output_content(ostream &output, recptproto *protocol, oaiargs ¶ms)
|
---|
| 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 | //
|
---|
| 147 | bool recordaction::output_record(ostream &output, recptproto *protocol, const text_t &collection, const text_t &OID, const text_t &metadataPrefix)
|
---|
| 148 | {
|
---|
| 149 | text_tset metadata;
|
---|
[8303] | 150 | ofstream logout("oai.log", ios::app);
|
---|
[8182] | 151 |
|
---|
| 152 | // get the document information
|
---|
| 153 | if (!get_info(OID, collection, "", metadata, false, protocol, this->gsdlResponse, logout)) {
|
---|
[11311] | 154 |
|
---|
[8182] | 155 | this->errorType = "idDoesNotExist";
|
---|
| 156 |
|
---|
| 157 | if(this->configuration->getOAIVersion() >= 200) {
|
---|
| 158 | this->output_error(output, errorType);
|
---|
| 159 | return false;
|
---|
| 160 | }
|
---|
| 161 | }
|
---|
| 162 |
|
---|
[8306] | 163 | return this->output_record(output, collection, OID, metadataPrefix);
|
---|
[8182] | 164 | }
|
---|
| 165 |
|
---|
| 166 | bool 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();
|
---|
[11311] | 174 | here += 2;
|
---|
[8182] | 175 | childHead = substr(start, here);
|
---|
| 176 |
|
---|
| 177 | // if it isn't a document, kill it now
|
---|
[11311] | 178 | if (childHead == "CL") {
|
---|
[8182] | 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 | }
|
---|