[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 |
|
---|
[18889] | 10 | recordaction::recordaction(text_tset &metadataset) : oaiaction("GetRecord") {
|
---|
[8182] | 11 | metaformatptr fptr;
|
---|
| 12 |
|
---|
[18889] | 13 | if (metadataset.count("oai_dc") > 0) {
|
---|
| 14 | fptr.set_class(new dublin_core());
|
---|
| 15 | this->formatMap[fptr.get_class()->formatName()] = fptr;
|
---|
| 16 | }
|
---|
[8182] | 17 |
|
---|
[18889] | 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) {
|
---|
[18861] | 24 |
|
---|
[18889] | 25 | fptr.set_class(new rfc1807());
|
---|
| 26 | this->formatMap[fptr.get_class()->formatName()] = fptr;
|
---|
| 27 | }
|
---|
[8182] | 28 | }
|
---|
| 29 |
|
---|
| 30 | recordaction::~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();
|
---|
[9608] | 36 | ++here;
|
---|
[8182] | 37 | }
|
---|
| 38 | }
|
---|
| 39 |
|
---|
| 40 |
|
---|
| 41 | bool recordaction::validateAction(recptproto *protocol, oaiargs ¶ms)
|
---|
[15198] | 42 | {
|
---|
[16715] | 43 | // ----------------------------------------------------------------------------
|
---|
| 44 | // 1. Check for invalid arguments
|
---|
| 45 | // ----------------------------------------------------------------------------
|
---|
| 46 | bool invalid_argument_supplied = false;
|
---|
[15198] | 47 | text_tmap::const_iterator param_iterator = params.begin();
|
---|
| 48 | while (param_iterator != params.end())
|
---|
| 49 | {
|
---|
[16715] | 50 | // Check for arguments that aren't valid for this action
|
---|
[15198] | 51 | if (param_iterator->first != "verb" &&
|
---|
| 52 | param_iterator->first != "identifier" &&
|
---|
| 53 | param_iterator->first != "metadataPrefix")
|
---|
| 54 | {
|
---|
[16715] | 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
|
---|
[15198] | 59 | params.erase(param_iterator->first);
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | param_iterator++;
|
---|
| 63 | }
|
---|
| 64 |
|
---|
[16715] | 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 | }
|
---|
[8182] | 71 |
|
---|
[16715] | 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 | {
|
---|
[8182] | 88 | this->errorType = "badArgument";
|
---|
| 89 | return false;
|
---|
| 90 | }
|
---|
[16718] | 91 |
|
---|
[16715] | 92 | // Check that the metadataPrefix is a format we support
|
---|
| 93 | if (this->formatNotSupported(metadataPrefix))
|
---|
| 94 | {
|
---|
[8182] | 95 | this->errorType = "cannotDisseminateFormat";
|
---|
[16715] | 96 | return false;
|
---|
[8182] | 97 | }
|
---|
| 98 |
|
---|
[16715] | 99 | // The "identifier" argument is required
|
---|
| 100 | text_t identifier = params["identifier"];
|
---|
| 101 |
|
---|
[16718] | 102 | // Check that the identifier argument exists
|
---|
| 103 | if (identifier == "")
|
---|
| 104 | {
|
---|
| 105 | this->errorType = "badArgument";
|
---|
| 106 | return false;
|
---|
| 107 | }
|
---|
| 108 |
|
---|
[16715] | 109 | // Extract the collection name from the identifier specification
|
---|
| 110 | text_t collection = "";
|
---|
| 111 | oaiclassifier::toGSDL(collection, identifier);
|
---|
[8182] | 112 |
|
---|
[16715] | 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 | {
|
---|
[8182] | 117 | this->errorType = "idDoesNotExist";
|
---|
[16715] | 118 | return false;
|
---|
[8182] | 119 | }
|
---|
| 120 |
|
---|
[16715] | 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 = "";
|
---|
[8182] | 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 | //
|
---|
| 136 | bool recordaction::output_content(ostream &output, recptproto *protocol, oaiargs ¶ms)
|
---|
| 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 | //
|
---|
| 154 | bool recordaction::output_record(ostream &output, recptproto *protocol, const text_t &collection, const text_t &OID, const text_t &metadataPrefix)
|
---|
| 155 | {
|
---|
| 156 | text_tset metadata;
|
---|
[8303] | 157 | ofstream logout("oai.log", ios::app);
|
---|
[8182] | 158 |
|
---|
| 159 | // get the document information
|
---|
| 160 | if (!get_info(OID, collection, "", metadata, false, protocol, this->gsdlResponse, logout)) {
|
---|
[11311] | 161 |
|
---|
[8182] | 162 | this->errorType = "idDoesNotExist";
|
---|
| 163 |
|
---|
| 164 | if(this->configuration->getOAIVersion() >= 200) {
|
---|
| 165 | this->output_error(output, errorType);
|
---|
| 166 | return false;
|
---|
| 167 | }
|
---|
| 168 | }
|
---|
| 169 |
|
---|
[8306] | 170 | return this->output_record(output, collection, OID, metadataPrefix);
|
---|
[8182] | 171 | }
|
---|
| 172 |
|
---|
| 173 | bool 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();
|
---|
[11311] | 181 | here += 2;
|
---|
[8182] | 182 | childHead = substr(start, here);
|
---|
| 183 |
|
---|
| 184 | // if it isn't a document, kill it now
|
---|
[11311] | 185 | if (childHead == "CL") {
|
---|
[8182] | 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 | }
|
---|