[24211] | 1 | /*
|
---|
[24221] | 2 | * ArchiveRetrieve.java
|
---|
[24211] | 3 | * a base class for retrieval services
|
---|
| 4 |
|
---|
| 5 | * Copyright (C) 2005 New Zealand Digital Library, http://www.nzdl.org
|
---|
| 6 | *
|
---|
| 7 | * This program is free software; you can redistribute it and/or modify
|
---|
| 8 | * it under the terms of the GNU General Public License as published by
|
---|
| 9 | * the Free Software Foundation; either version 2 of the License, or
|
---|
| 10 | * (at your option) any later version.
|
---|
| 11 | *
|
---|
| 12 | * This program is distributed in the hope that it will be useful,
|
---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 15 | * GNU General Public License for more details.
|
---|
| 16 | *
|
---|
| 17 | * You should have received a copy of the GNU General Public License
|
---|
| 18 | * along with this program; if not, write to the Free Software
|
---|
| 19 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
| 20 | */
|
---|
| 21 | package org.greenstone.gsdl3.service;
|
---|
| 22 |
|
---|
[24221] | 23 | import org.greenstone.gsdl3.util.DBInfo;
|
---|
[24211] | 24 | import org.greenstone.gsdl3.util.GSPath;
|
---|
| 25 | import org.greenstone.gsdl3.util.GSXML;
|
---|
| 26 | import org.greenstone.gsdl3.util.SimpleCollectionDatabase;
|
---|
| 27 |
|
---|
| 28 | import org.w3c.dom.Element;
|
---|
| 29 |
|
---|
| 30 | import org.apache.log4j.*;
|
---|
| 31 |
|
---|
| 32 | import java.io.File;
|
---|
| 33 | import java.util.HashMap;
|
---|
[24221] | 34 | import java.util.Vector;
|
---|
[24211] | 35 |
|
---|
[24221] | 36 | public class ArchiveRetrieve extends ServiceRack
|
---|
| 37 | {
|
---|
[24211] | 38 | static Logger logger = Logger.getLogger(org.greenstone.gsdl3.service.ArchiveRetrieve.class.getName());
|
---|
| 39 |
|
---|
[24221] | 40 | protected static final String DOCUMENT_FILE_PATH_RETRIEVE_SERVICE = "DocumentFilePathRetrieve";
|
---|
| 41 | protected static final String ASSOCIATED_IMPORT_FILES_RETRIEVE_SERVICE = "AssociatedImportFilesRetrieve";
|
---|
| 42 | protected static final String SOURCE_FILE_OID_RETRIEVE = "SourceFileOIDRetrieve";
|
---|
[24211] | 43 |
|
---|
| 44 | protected SimpleCollectionDatabase coll_db = null;
|
---|
| 45 |
|
---|
| 46 | /** configure this service */
|
---|
| 47 | public boolean configure(Element info, Element extra_info)
|
---|
| 48 | {
|
---|
[24221] | 49 | if (!super.configure(info, extra_info))
|
---|
| 50 | {
|
---|
[24211] | 51 | return false;
|
---|
| 52 | }
|
---|
| 53 |
|
---|
| 54 | logger.info("Configuring ArchiveRetrieve...");
|
---|
| 55 | this.config_info = info;
|
---|
| 56 |
|
---|
[24221] | 57 | Element documentFilePathRetrieveService = this.doc.createElement(GSXML.SERVICE_ELEM);
|
---|
| 58 | documentFilePathRetrieveService.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_RETRIEVE);
|
---|
| 59 | documentFilePathRetrieveService.setAttribute(GSXML.NAME_ATT, DOCUMENT_FILE_PATH_RETRIEVE_SERVICE);
|
---|
| 60 | this.short_service_info.appendChild(documentFilePathRetrieveService);
|
---|
[24211] | 61 |
|
---|
[24221] | 62 | Element associatedImportFilesRetrieveService = this.doc.createElement(GSXML.SERVICE_ELEM);
|
---|
| 63 | associatedImportFilesRetrieveService.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_RETRIEVE);
|
---|
| 64 | associatedImportFilesRetrieveService.setAttribute(GSXML.NAME_ATT, ASSOCIATED_IMPORT_FILES_RETRIEVE_SERVICE);
|
---|
| 65 | this.short_service_info.appendChild(associatedImportFilesRetrieveService);
|
---|
| 66 |
|
---|
| 67 | Element sourceFileDocIDRetrieveService = this.doc.createElement(GSXML.SERVICE_ELEM);
|
---|
| 68 | sourceFileDocIDRetrieveService.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_RETRIEVE);
|
---|
| 69 | sourceFileDocIDRetrieveService.setAttribute(GSXML.NAME_ATT, SOURCE_FILE_OID_RETRIEVE);
|
---|
| 70 | this.short_service_info.appendChild(sourceFileDocIDRetrieveService);
|
---|
| 71 |
|
---|
[24211] | 72 | return true;
|
---|
| 73 | }
|
---|
| 74 |
|
---|
| 75 | protected Element getServiceDescription(String service_id, String lang, String subset)
|
---|
| 76 | {
|
---|
[24221] | 77 | if (service_id.equals(DOCUMENT_FILE_PATH_RETRIEVE_SERVICE))
|
---|
| 78 | {
|
---|
| 79 | Element service_elem = this.doc.createElement(GSXML.SERVICE_ELEM);
|
---|
| 80 | service_elem.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_RETRIEVE);
|
---|
| 81 | service_elem.setAttribute(GSXML.NAME_ATT, DOCUMENT_FILE_PATH_RETRIEVE_SERVICE);
|
---|
| 82 | return service_elem;
|
---|
| 83 | }
|
---|
| 84 | else if (service_id.equals(ASSOCIATED_IMPORT_FILES_RETRIEVE_SERVICE))
|
---|
| 85 | {
|
---|
| 86 | Element service_elem = this.doc.createElement(GSXML.SERVICE_ELEM);
|
---|
| 87 | service_elem.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_RETRIEVE);
|
---|
| 88 | service_elem.setAttribute(GSXML.NAME_ATT, ASSOCIATED_IMPORT_FILES_RETRIEVE_SERVICE);
|
---|
| 89 | return service_elem;
|
---|
| 90 | }
|
---|
| 91 | else if (service_id.equals(SOURCE_FILE_OID_RETRIEVE))
|
---|
| 92 | {
|
---|
| 93 | Element service_elem = this.doc.createElement(GSXML.SERVICE_ELEM);
|
---|
| 94 | service_elem.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_RETRIEVE);
|
---|
| 95 | service_elem.setAttribute(GSXML.NAME_ATT, SOURCE_FILE_OID_RETRIEVE);
|
---|
| 96 | return service_elem;
|
---|
| 97 | }
|
---|
| 98 | return null;
|
---|
[24211] | 99 | }
|
---|
| 100 |
|
---|
[24221] | 101 | protected Element processDocumentFilePathRetrieve(Element request)
|
---|
[24211] | 102 | {
|
---|
| 103 | // Create a new (empty) result message
|
---|
| 104 | Element result = this.doc.createElement(GSXML.RESPONSE_ELEM);
|
---|
[24221] | 105 | result.setAttribute(GSXML.FROM_ATT, DOCUMENT_FILE_PATH_RETRIEVE_SERVICE);
|
---|
[24211] | 106 | result.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_PROCESS);
|
---|
| 107 |
|
---|
| 108 | String lang = request.getAttribute(GSXML.LANG_ATT);
|
---|
| 109 | String uid = request.getAttribute(GSXML.USER_ID_ATT);
|
---|
| 110 |
|
---|
| 111 | // Get the parameters of the request
|
---|
| 112 | Element param_list = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
|
---|
| 113 | if (param_list == null) {
|
---|
[24221] | 114 | GSXML.addError(this.doc, result, "DocumentFilePathRetrieve: missing "+ GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER, GSXML.ERROR_TYPE_SYNTAX);
|
---|
[24211] | 115 | return result;
|
---|
| 116 | }
|
---|
| 117 | HashMap params = GSXML.extractParams(param_list, false);
|
---|
| 118 |
|
---|
[24221] | 119 | String oid = (String) params.get("oid");
|
---|
[24211] | 120 | String collection = (String) params.get("c");
|
---|
| 121 |
|
---|
[24221] | 122 | String assocFilePath = getAssocFilePathFromDocID(oid, collection, lang, uid);
|
---|
[24211] | 123 |
|
---|
| 124 | String docFilePath = this.site_home + File.separatorChar +
|
---|
| 125 | "collect" + File.separatorChar +
|
---|
| 126 | collection + File.separatorChar +
|
---|
| 127 | "archives" + File.separatorChar +
|
---|
| 128 | assocFilePath + File.separatorChar +
|
---|
| 129 | "doc.xml";
|
---|
[24221] | 130 |
|
---|
| 131 | Element metadataList = this.doc.createElement(GSXML.METADATA_ELEM + GSXML.LIST_MODIFIER);
|
---|
| 132 | metadataList.appendChild(createMetadataElement("docfilepath", docFilePath));
|
---|
| 133 | result.appendChild(metadataList);
|
---|
[24211] | 134 |
|
---|
[24221] | 135 | return result;
|
---|
| 136 | }
|
---|
| 137 |
|
---|
| 138 | protected Element processSourceFileOIDRetrieveService(Element request)
|
---|
| 139 | {
|
---|
| 140 | //Create a new (empty) result message
|
---|
| 141 | Element result = this.doc.createElement(GSXML.RESPONSE_ELEM);
|
---|
| 142 | result.setAttribute(GSXML.FROM_ATT, SOURCE_FILE_OID_RETRIEVE);
|
---|
| 143 | result.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_PROCESS);
|
---|
[24211] | 144 |
|
---|
[24221] | 145 | String lang = request.getAttribute(GSXML.LANG_ATT);
|
---|
| 146 | String uid = request.getAttribute(GSXML.USER_ID_ATT);
|
---|
[24211] | 147 |
|
---|
[24221] | 148 | // Get the parameters of the request
|
---|
| 149 | Element param_list = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
|
---|
| 150 | if (param_list == null)
|
---|
| 151 | {
|
---|
| 152 | GSXML.addError(this.doc, result, "DocumentFilePathRetrieve: missing "+ GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER, GSXML.ERROR_TYPE_SYNTAX);
|
---|
| 153 | return result;
|
---|
| 154 | }
|
---|
| 155 | HashMap params = GSXML.extractParams(param_list, false);
|
---|
[24211] | 156 |
|
---|
[24221] | 157 | String srcFile = (String) params.get("srcfile");
|
---|
| 158 | String collection = (String) params.get("c");
|
---|
| 159 |
|
---|
| 160 | //Find out what kind of database we have
|
---|
| 161 | String databaseType = getDatabaseTypeFromCollection(collection, lang, uid);
|
---|
| 162 | if (databaseType == null || databaseType.equals(""))
|
---|
| 163 | {
|
---|
| 164 | databaseType = "gdbm"; // the default
|
---|
| 165 | }
|
---|
| 166 |
|
---|
| 167 | String dbExt = null;
|
---|
| 168 | if (databaseType.equalsIgnoreCase("jdbm"))
|
---|
| 169 | {
|
---|
| 170 | dbExt = ".jdb";
|
---|
| 171 | }
|
---|
| 172 | else
|
---|
| 173 | {
|
---|
| 174 | dbExt = ".gdb"; // assume gdbm
|
---|
| 175 | }
|
---|
| 176 |
|
---|
| 177 | coll_db = new SimpleCollectionDatabase(databaseType);
|
---|
| 178 | if (!coll_db.databaseOK())
|
---|
| 179 | {
|
---|
| 180 | logger.error("Couldn't create the collection database of type "+databaseType);
|
---|
| 181 | return null;
|
---|
| 182 | }
|
---|
| 183 |
|
---|
| 184 | coll_db.openDatabase
|
---|
| 185 | (
|
---|
| 186 | this.site_home + File.separatorChar +
|
---|
| 187 | "collect" + File.separatorChar +
|
---|
| 188 | collection + File.separatorChar +
|
---|
| 189 | "archives" + File.separatorChar +
|
---|
| 190 | "archiveinf-src" + dbExt,
|
---|
| 191 | SimpleCollectionDatabase.READ
|
---|
| 192 | );
|
---|
| 193 |
|
---|
| 194 | DBInfo info = coll_db.getInfo(srcFile);
|
---|
| 195 |
|
---|
| 196 | if (info == null)
|
---|
| 197 | {
|
---|
| 198 | return result;
|
---|
| 199 | }
|
---|
| 200 |
|
---|
| 201 | String oid = info.getInfo("oid");
|
---|
| 202 |
|
---|
| 203 | Element metadataList = this.doc.createElement(GSXML.METADATA_ELEM + GSXML.LIST_MODIFIER);
|
---|
| 204 | metadataList.appendChild(createMetadataElement("oid", oid));
|
---|
| 205 | result.appendChild(metadataList);
|
---|
| 206 |
|
---|
[24211] | 207 | return result;
|
---|
| 208 | }
|
---|
| 209 |
|
---|
[24221] | 210 | protected Element processAssociatedImportFilesRetrieve(Element request)
|
---|
[24211] | 211 | {
|
---|
[24221] | 212 | //Create a new (empty) result message
|
---|
| 213 | Element result = this.doc.createElement(GSXML.RESPONSE_ELEM);
|
---|
| 214 | result.setAttribute(GSXML.FROM_ATT, ASSOCIATED_IMPORT_FILES_RETRIEVE_SERVICE);
|
---|
| 215 | result.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_PROCESS);
|
---|
| 216 |
|
---|
| 217 | String lang = request.getAttribute(GSXML.LANG_ATT);
|
---|
| 218 | String uid = request.getAttribute(GSXML.USER_ID_ATT);
|
---|
| 219 |
|
---|
| 220 | // Get the parameters of the request
|
---|
| 221 | Element param_list = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
|
---|
| 222 | if (param_list == null)
|
---|
| 223 | {
|
---|
| 224 | GSXML.addError(this.doc, result, "AssociatedImportFilesRetrieve: missing "+ GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER, GSXML.ERROR_TYPE_SYNTAX);
|
---|
| 225 | return result;
|
---|
[24211] | 226 | }
|
---|
[24221] | 227 | HashMap params = GSXML.extractParams(param_list, false);
|
---|
| 228 |
|
---|
| 229 | String oid = (String) params.get("oid");
|
---|
| 230 | String collection = (String) params.get("c");
|
---|
| 231 |
|
---|
| 232 | String databaseType = getDatabaseTypeFromCollection(collection, lang, uid);
|
---|
| 233 | if (databaseType == null || databaseType.equals(""))
|
---|
| 234 | {
|
---|
| 235 | databaseType = "gdbm"; // the default
|
---|
[24211] | 236 | }
|
---|
| 237 |
|
---|
[24221] | 238 | String dbExt = null;
|
---|
| 239 | if (databaseType.equalsIgnoreCase("jdbm"))
|
---|
| 240 | {
|
---|
| 241 | dbExt = ".jdb";
|
---|
| 242 | }
|
---|
| 243 | else
|
---|
| 244 | {
|
---|
| 245 | dbExt = ".gdb"; // assume gdbm
|
---|
[24211] | 246 | }
|
---|
| 247 |
|
---|
[24221] | 248 | coll_db = new SimpleCollectionDatabase(databaseType);
|
---|
| 249 | if (!coll_db.databaseOK())
|
---|
| 250 | {
|
---|
| 251 | logger.error("Couldn't create the collection database of type "+databaseType);
|
---|
[24211] | 252 | return null;
|
---|
| 253 | }
|
---|
[24221] | 254 |
|
---|
| 255 | coll_db.openDatabase
|
---|
| 256 | (
|
---|
[24211] | 257 | this.site_home + File.separatorChar +
|
---|
| 258 | "collect" + File.separatorChar +
|
---|
| 259 | collection + File.separatorChar +
|
---|
| 260 | "archives" + File.separatorChar +
|
---|
[24221] | 261 | "archiveinf-doc" + dbExt,
|
---|
| 262 | SimpleCollectionDatabase.READ
|
---|
| 263 | );
|
---|
[24211] | 264 |
|
---|
[24221] | 265 | DBInfo info = coll_db.getInfo(oid);
|
---|
[24211] | 266 |
|
---|
[24221] | 267 | if (info == null)
|
---|
| 268 | {
|
---|
| 269 | return result;
|
---|
| 270 | }
|
---|
| 271 |
|
---|
| 272 | String srcFile = info.getInfo("src-file");
|
---|
| 273 | Vector data = info.getMultiInfo("assoc-file");
|
---|
| 274 |
|
---|
| 275 | Element metadataList = this.doc.createElement(GSXML.METADATA_ELEM + GSXML.LIST_MODIFIER);
|
---|
| 276 | metadataList.appendChild(createMetadataElement("srcfile", srcFile));
|
---|
| 277 |
|
---|
| 278 | for (int i = 0; i < data.size(); i++)
|
---|
| 279 | {
|
---|
| 280 | metadataList.appendChild(createMetadataElement("assocfile", (String)data.get(i)));
|
---|
| 281 | }
|
---|
| 282 |
|
---|
| 283 | result.appendChild(metadataList);
|
---|
| 284 |
|
---|
| 285 | return result;
|
---|
[24211] | 286 | }
|
---|
| 287 |
|
---|
[24221] | 288 | public Element createMetadataElement(String name, String value)
|
---|
[24211] | 289 | {
|
---|
[24221] | 290 | Element metaElem = this.doc.createElement(GSXML.METADATA_ELEM);
|
---|
| 291 | metaElem.setAttribute("name", name);
|
---|
| 292 | metaElem.setAttribute("value", value);
|
---|
| 293 | return metaElem;
|
---|
| 294 | }
|
---|
| 295 |
|
---|
| 296 | public String getAssocFilePathFromDocID(String oid, String collection, String lang, String uid)
|
---|
| 297 | {
|
---|
[24211] | 298 | Element mr_query_message = this.doc.createElement(GSXML.MESSAGE_ELEM);
|
---|
| 299 | Element mr_query_request = GSXML.createBasicRequest (this.doc, GSXML.REQUEST_TYPE_PAGE, collection + "/DocumentMetadataRetrieve", lang, uid);
|
---|
| 300 | mr_query_message.appendChild(mr_query_request);
|
---|
| 301 |
|
---|
| 302 | Element paramList = this.doc.createElement(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
|
---|
[24221] | 303 | paramList.appendChild(createMetadataElement("metadata", "assocfilepath"));
|
---|
[24211] | 304 |
|
---|
| 305 | mr_query_request.appendChild(paramList);
|
---|
| 306 |
|
---|
[24221] | 307 | Element docListElem = this.doc.createElement(GSXML.DOC_NODE_ELEM+GSXML.LIST_MODIFIER);
|
---|
| 308 | Element docElem = this.doc.createElement(GSXML.DOC_NODE_ELEM);
|
---|
| 309 | docElem.setAttribute(GSXML.NODE_ID_ATT, oid);
|
---|
| 310 | docListElem.appendChild(docElem);
|
---|
| 311 | mr_query_request.appendChild(docListElem);
|
---|
[24211] | 312 |
|
---|
| 313 | Element response = (Element) this.router.process(mr_query_message);
|
---|
| 314 |
|
---|
| 315 | String path = GSPath.appendLink(GSXML.RESPONSE_ELEM, GSXML.DOC_NODE_ELEM+GSXML.LIST_MODIFIER);
|
---|
| 316 | path = GSPath.appendLink(path, GSXML.DOC_NODE_ELEM);
|
---|
| 317 | path = GSPath.appendLink(path, GSXML.METADATA_ELEM+GSXML.LIST_MODIFIER);
|
---|
| 318 | Element metadataListElem = (Element) GSXML.getNodeByPath(response, path);
|
---|
| 319 | Element metadataElem = (Element) metadataListElem.getFirstChild();
|
---|
| 320 |
|
---|
| 321 | return metadataElem.getFirstChild().getNodeValue();
|
---|
| 322 | }
|
---|
[24221] | 323 |
|
---|
| 324 | public String getDatabaseTypeFromCollection(String collection, String lang, String uid)
|
---|
| 325 | {
|
---|
| 326 | //Find out what kind of database we have
|
---|
| 327 | Element dbTypeMessage = this.doc.createElement(GSXML.MESSAGE_ELEM);
|
---|
| 328 | Element dbTypeRequest = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_DESCRIBE, collection, lang, uid);
|
---|
| 329 | dbTypeMessage.appendChild(dbTypeRequest);
|
---|
| 330 | Element dbTypeResponse = (Element)this.router.process(dbTypeMessage);
|
---|
| 331 |
|
---|
| 332 | String path = GSPath.appendLink(GSXML.RESPONSE_ELEM, GSXML.COLLECTION_ELEM);
|
---|
| 333 | Element collectionElem = (Element) GSXML.getNodeByPath(dbTypeResponse, path);
|
---|
| 334 |
|
---|
| 335 | if (collectionElem != null)
|
---|
| 336 | {
|
---|
| 337 | return collectionElem.getAttribute(GSXML.DB_TYPE_ATT);
|
---|
| 338 | }
|
---|
| 339 | return null;
|
---|
| 340 | }
|
---|
[24211] | 341 | } |
---|