[24393] | 1 | /*
|
---|
| 2 | * ArchiveIO.java
|
---|
| 3 | * Used to retrieve information about collection archives
|
---|
| 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 |
|
---|
| 23 | import org.greenstone.gsdl3.util.DBHelper;
|
---|
| 24 | import org.greenstone.gsdl3.util.DBInfo;
|
---|
| 25 | import org.greenstone.gsdl3.util.GSDocumentModel;
|
---|
| 26 | import org.greenstone.gsdl3.util.GSParams;
|
---|
| 27 | import org.greenstone.gsdl3.util.GSPath;
|
---|
| 28 | import org.greenstone.gsdl3.util.GSXML;
|
---|
| 29 | import org.greenstone.gsdl3.util.SimpleCollectionDatabase;
|
---|
[24993] | 30 | import org.greenstone.gsdl3.util.UserContext;
|
---|
[28966] | 31 | import org.greenstone.gsdl3.util.XMLConverter;
|
---|
[24393] | 32 |
|
---|
[28966] | 33 | import org.w3c.dom.Document;
|
---|
[24393] | 34 | import org.w3c.dom.Element;
|
---|
| 35 | import org.w3c.dom.NodeList;
|
---|
| 36 |
|
---|
| 37 | import org.apache.log4j.*;
|
---|
| 38 |
|
---|
| 39 | import java.io.File;
|
---|
[25635] | 40 | import java.io.Serializable;
|
---|
[24393] | 41 | import java.util.ArrayList;
|
---|
| 42 | import java.util.HashMap;
|
---|
| 43 | import java.util.Vector;
|
---|
| 44 |
|
---|
| 45 | public class ArchiveIO extends ServiceRack
|
---|
| 46 | {
|
---|
| 47 | static Logger logger = Logger.getLogger(org.greenstone.gsdl3.service.ArchiveIO.class.getName());
|
---|
| 48 | GSDocumentModel _GSDM = null;
|
---|
| 49 |
|
---|
| 50 | /********************************************************************
|
---|
| 51 | * The list of services the Archive retrieval service rack supports *
|
---|
| 52 | *******************************************************************/
|
---|
| 53 | protected static final String ARCHIVE_GET_DOCUMENT_FILE_PATH = "ArchiveGetDocumentFilePath";
|
---|
| 54 | protected static final String ARCHIVE_GET_ASSOCIATED_IMPORT_FILES = "ArchiveGetAssociatedImportFiles";
|
---|
| 55 | protected static final String ARCHIVE_GET_SOURCE_FILE_OID = "ArchiveGetSourceFileOID";
|
---|
| 56 | protected static final String ARCHIVE_CHECK_DOCUMENT_OR_SECTION_EXISTS = "ArchiveCheckDocumentOrSectionExists";
|
---|
| 57 | protected static final String ARCHIVE_WRITE_ENTRY_TO_DATABASE = "ArchiveWriteEntryToDatabase";
|
---|
| 58 | protected static final String ARCHIVE_REMOVE_ENTRY_FROM_DATABASE = "ArchiveRemoveEntryFromDatabase";
|
---|
| 59 | /*******************************************************************/
|
---|
| 60 |
|
---|
| 61 | String[] _services = { ARCHIVE_GET_DOCUMENT_FILE_PATH, ARCHIVE_GET_ASSOCIATED_IMPORT_FILES, ARCHIVE_GET_SOURCE_FILE_OID, ARCHIVE_CHECK_DOCUMENT_OR_SECTION_EXISTS, ARCHIVE_WRITE_ENTRY_TO_DATABASE, ARCHIVE_REMOVE_ENTRY_FROM_DATABASE };
|
---|
| 62 |
|
---|
| 63 | /** configure this service */
|
---|
| 64 | public boolean configure(Element info, Element extra_info)
|
---|
| 65 | {
|
---|
| 66 | if (!super.configure(info, extra_info))
|
---|
| 67 | {
|
---|
| 68 | return false;
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | logger.info("Configuring DocXMLUtil...");
|
---|
| 72 | this.config_info = info;
|
---|
| 73 |
|
---|
| 74 | for (int i = 0; i < _services.length; i++)
|
---|
| 75 | {
|
---|
[28966] | 76 | Element service = this.desc_doc.createElement(GSXML.SERVICE_ELEM);
|
---|
[24393] | 77 | service.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_RETRIEVE);
|
---|
| 78 | service.setAttribute(GSXML.NAME_ATT, _services[i]);
|
---|
| 79 | this.short_service_info.appendChild(service);
|
---|
| 80 | }
|
---|
| 81 |
|
---|
[28966] | 82 | _GSDM = new GSDocumentModel(this.site_home, this.router);
|
---|
[24393] | 83 |
|
---|
| 84 | return true;
|
---|
| 85 | }
|
---|
| 86 |
|
---|
[28966] | 87 | protected Element getServiceDescription(Document doc, String service_id, String lang, String subset)
|
---|
[24393] | 88 | {
|
---|
| 89 | for (int i = 0; i < _services.length; i++)
|
---|
| 90 | {
|
---|
| 91 | if (service_id.equals(_services[i]))
|
---|
| 92 | {
|
---|
[28966] | 93 | Element service_elem = doc.createElement(GSXML.SERVICE_ELEM);
|
---|
[24393] | 94 | service_elem.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_RETRIEVE);
|
---|
| 95 | service_elem.setAttribute(GSXML.NAME_ATT, _services[i]);
|
---|
| 96 | return service_elem;
|
---|
| 97 | }
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 100 | return null;
|
---|
| 101 | }
|
---|
| 102 |
|
---|
| 103 | /************
|
---|
| 104 | * Services *
|
---|
| 105 | ***********/
|
---|
| 106 |
|
---|
| 107 | protected Element processArchiveGetDocumentFilePath(Element request)
|
---|
| 108 | {
|
---|
| 109 | // Create a new (empty) result message
|
---|
[28966] | 110 | Document result_doc = XMLConverter.newDOM();
|
---|
| 111 | Element result = result_doc.createElement(GSXML.RESPONSE_ELEM);
|
---|
[24393] | 112 | result.setAttribute(GSXML.FROM_ATT, ARCHIVE_GET_DOCUMENT_FILE_PATH);
|
---|
| 113 | result.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_PROCESS);
|
---|
| 114 |
|
---|
[24993] | 115 | UserContext userContext = new UserContext(request);
|
---|
[24393] | 116 |
|
---|
| 117 | // Get the parameters of the request
|
---|
| 118 | Element param_list = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
|
---|
| 119 | if (param_list == null)
|
---|
| 120 | {
|
---|
[28966] | 121 | GSXML.addError(result, ARCHIVE_GET_DOCUMENT_FILE_PATH + ": Missing " + GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER, GSXML.ERROR_TYPE_SYNTAX);
|
---|
[24393] | 122 | return result;
|
---|
| 123 | }
|
---|
[25635] | 124 | HashMap<String, Serializable> params = GSXML.extractParams(param_list, false);
|
---|
[24393] | 125 |
|
---|
| 126 | String oid = (String) params.get(GSXML.NODE_ID_ATT);
|
---|
| 127 | String collection = (String) params.get(GSXML.COLLECTION_ATT);
|
---|
| 128 |
|
---|
[24993] | 129 | String filePath = _GSDM.archiveGetDocumentFilePath(oid, collection, userContext);
|
---|
[24393] | 130 |
|
---|
[28966] | 131 | Element metadataList = result_doc.createElement(GSXML.METADATA_ELEM + GSXML.LIST_MODIFIER);
|
---|
| 132 | metadataList.appendChild(GSXML.createMetadataElement(result_doc, "docfilepath", filePath)); //TODO: Replace "docfilepath" with a constant
|
---|
[24393] | 133 | result.appendChild(metadataList);
|
---|
| 134 |
|
---|
| 135 | return result;
|
---|
| 136 | }
|
---|
| 137 |
|
---|
| 138 | protected Element processArchiveGetSourceFileOID(Element request)
|
---|
| 139 | {
|
---|
| 140 | //Create a new (empty) result message
|
---|
[28966] | 141 | Document result_doc = XMLConverter.newDOM();
|
---|
| 142 | Element result = result_doc.createElement(GSXML.RESPONSE_ELEM);
|
---|
[24393] | 143 | result.setAttribute(GSXML.FROM_ATT, ARCHIVE_GET_SOURCE_FILE_OID);
|
---|
| 144 | result.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_PROCESS);
|
---|
| 145 |
|
---|
[24993] | 146 | UserContext userContext = new UserContext(request);
|
---|
[24393] | 147 |
|
---|
| 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 | {
|
---|
[28966] | 152 | GSXML.addError(result, ARCHIVE_GET_SOURCE_FILE_OID + ": Missing " + GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER, GSXML.ERROR_TYPE_SYNTAX);
|
---|
[24393] | 153 | return result;
|
---|
| 154 | }
|
---|
[25635] | 155 | HashMap<String, Serializable> params = GSXML.extractParams(param_list, false);
|
---|
[24393] | 156 |
|
---|
| 157 | String srcFile = (String) params.get("sourcefile"); //TODO: Replace with a constant
|
---|
| 158 | String collection = (String) params.get(GSXML.COLLECTION_ATT);
|
---|
| 159 |
|
---|
[24993] | 160 | String oid = _GSDM.archiveGetSourceFileOID(srcFile, collection, userContext);
|
---|
[24393] | 161 | if(_GSDM.checkError(result, ARCHIVE_GET_SOURCE_FILE_OID))
|
---|
| 162 | {
|
---|
| 163 | return result;
|
---|
| 164 | }
|
---|
| 165 |
|
---|
[28966] | 166 | Element metadataList = result_doc.createElement(GSXML.METADATA_ELEM + GSXML.LIST_MODIFIER);
|
---|
| 167 | metadataList.appendChild(GSXML.createMetadataElement(result_doc, GSXML.NODE_ID_ATT, oid));
|
---|
[24393] | 168 | result.appendChild(metadataList);
|
---|
| 169 |
|
---|
| 170 | return result;
|
---|
| 171 | }
|
---|
| 172 |
|
---|
| 173 | protected Element processArchiveCheckDocumentOrSectionExists(Element request)
|
---|
| 174 | {
|
---|
| 175 | //Create a new (empty) result message
|
---|
[28966] | 176 | Document result_doc = XMLConverter.newDOM();
|
---|
| 177 | Element result = result_doc.createElement(GSXML.RESPONSE_ELEM);
|
---|
[24393] | 178 | result.setAttribute(GSXML.FROM_ATT, ARCHIVE_CHECK_DOCUMENT_OR_SECTION_EXISTS);
|
---|
| 179 | result.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_PROCESS);
|
---|
| 180 |
|
---|
[24993] | 181 | UserContext userContext = new UserContext(request);
|
---|
[24393] | 182 |
|
---|
| 183 | // Get the parameters of the request
|
---|
| 184 | Element param_list = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
|
---|
| 185 | if (param_list == null)
|
---|
| 186 | {
|
---|
[28966] | 187 | GSXML.addError(result, ARCHIVE_CHECK_DOCUMENT_OR_SECTION_EXISTS + ": Missing " + GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER, GSXML.ERROR_TYPE_SYNTAX);
|
---|
[24393] | 188 | return result;
|
---|
| 189 | }
|
---|
[25635] | 190 | HashMap<String, Serializable> params = GSXML.extractParams(param_list, false);
|
---|
[24393] | 191 |
|
---|
| 192 | String oid = (String) params.get(GSXML.NODE_ID_ATT);
|
---|
| 193 | String collection = (String) params.get(GSXML.COLLECTION_ATT);
|
---|
| 194 |
|
---|
[24993] | 195 | boolean exists = _GSDM.archiveCheckDocumentOrSectionExists(oid, collection, userContext);
|
---|
[24393] | 196 | if(_GSDM.checkError(result, ARCHIVE_CHECK_DOCUMENT_OR_SECTION_EXISTS))
|
---|
| 197 | {
|
---|
| 198 | return result;
|
---|
| 199 | }
|
---|
| 200 |
|
---|
| 201 | result.setAttribute("check", exists ? "true" : "false");
|
---|
| 202 |
|
---|
| 203 | return result;
|
---|
| 204 | }
|
---|
| 205 |
|
---|
| 206 | protected Element processArchiveWriteEntryToDatabase(Element request)
|
---|
| 207 | {
|
---|
| 208 | //Create a new (empty) result message
|
---|
[28966] | 209 | Document result_doc = XMLConverter.newDOM();
|
---|
| 210 | Element result = result_doc.createElement(GSXML.RESPONSE_ELEM);
|
---|
[24393] | 211 | result.setAttribute(GSXML.FROM_ATT, ARCHIVE_WRITE_ENTRY_TO_DATABASE);
|
---|
| 212 | result.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_PROCESS);
|
---|
| 213 |
|
---|
[24993] | 214 | UserContext userContext = new UserContext(request);
|
---|
[24393] | 215 |
|
---|
| 216 | // Get the parameters of the request
|
---|
| 217 | Element param_list = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
|
---|
| 218 | if (param_list == null)
|
---|
| 219 | {
|
---|
[28966] | 220 | GSXML.addError(result, ARCHIVE_WRITE_ENTRY_TO_DATABASE + ": Missing " + GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER, GSXML.ERROR_TYPE_SYNTAX);
|
---|
[24393] | 221 | return result;
|
---|
| 222 | }
|
---|
| 223 |
|
---|
| 224 | HashMap<String, ArrayList<String>> info = new HashMap<String, ArrayList<String>>();
|
---|
| 225 | String collection = null;
|
---|
| 226 | String oid = null;
|
---|
| 227 |
|
---|
| 228 | NodeList params = param_list.getElementsByTagName(GSXML.PARAM_ELEM);
|
---|
| 229 | for (int i = 0; i < params.getLength(); i++)
|
---|
| 230 | {
|
---|
| 231 | Element currentParam = (Element) params.item(i);
|
---|
| 232 | String type = currentParam.getAttribute(GSXML.TYPE_ATT);
|
---|
| 233 | String name = currentParam.getAttribute(GSXML.NAME_ATT);
|
---|
| 234 | String value = currentParam.getAttribute(GSXML.VALUE_ATT);
|
---|
| 235 | if (type != null && type.equals("entry"))
|
---|
| 236 | {
|
---|
| 237 | if(info.get(name) != null)
|
---|
| 238 | {
|
---|
| 239 | info.get(name).add(value);
|
---|
| 240 | }
|
---|
| 241 | else
|
---|
| 242 | {
|
---|
| 243 | ArrayList<String> values = new ArrayList<String>();
|
---|
| 244 | values.add(value);
|
---|
| 245 | info.put(name, values);
|
---|
| 246 | }
|
---|
| 247 | }
|
---|
| 248 | else
|
---|
| 249 | {
|
---|
| 250 | if (name.equals(GSXML.COLLECTION_ATT))
|
---|
| 251 | {
|
---|
| 252 | collection = value;
|
---|
| 253 | }
|
---|
| 254 | else if (name.equals(GSXML.NODE_ID_ATT))
|
---|
| 255 | {
|
---|
| 256 | oid = value;
|
---|
| 257 | }
|
---|
| 258 | }
|
---|
| 259 | }
|
---|
| 260 |
|
---|
[24993] | 261 | _GSDM.archiveWriteEntryToDatabase(oid, collection, info, userContext);
|
---|
[24393] | 262 | _GSDM.checkError(result, ARCHIVE_WRITE_ENTRY_TO_DATABASE);
|
---|
| 263 |
|
---|
| 264 | return result;
|
---|
| 265 | }
|
---|
| 266 |
|
---|
| 267 | protected Element processArchiveRemoveEntryFromDatabase(Element request)
|
---|
| 268 | {
|
---|
| 269 | //Create a new (empty) result message
|
---|
[28966] | 270 | Document result_doc = XMLConverter.newDOM();
|
---|
| 271 | Element result = result_doc.createElement(GSXML.RESPONSE_ELEM);
|
---|
[24393] | 272 | result.setAttribute(GSXML.FROM_ATT, ARCHIVE_REMOVE_ENTRY_FROM_DATABASE);
|
---|
| 273 | result.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_PROCESS);
|
---|
| 274 |
|
---|
[24993] | 275 | UserContext userContext = new UserContext(request);
|
---|
[24393] | 276 |
|
---|
| 277 | // Get the parameters of the request
|
---|
| 278 | Element param_list = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
|
---|
| 279 | if (param_list == null)
|
---|
| 280 | {
|
---|
[28966] | 281 | GSXML.addError(result, ARCHIVE_REMOVE_ENTRY_FROM_DATABASE + ": Missing " + GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER, GSXML.ERROR_TYPE_SYNTAX);
|
---|
[24393] | 282 | return result;
|
---|
| 283 | }
|
---|
| 284 |
|
---|
| 285 | String collection = null;
|
---|
| 286 | String oid = null;
|
---|
| 287 |
|
---|
| 288 | NodeList params = param_list.getElementsByTagName(GSXML.PARAM_ELEM);
|
---|
| 289 | for (int i = 0; i < params.getLength(); i++)
|
---|
| 290 | {
|
---|
| 291 | Element currentParam = (Element) params.item(i);
|
---|
| 292 | String name = currentParam.getAttribute(GSXML.NAME_ATT);
|
---|
| 293 | String value = currentParam.getAttribute(GSXML.VALUE_ATT);
|
---|
| 294 |
|
---|
| 295 | if (name.equals(GSXML.COLLECTION_ATT))
|
---|
| 296 | {
|
---|
| 297 | collection = value;
|
---|
| 298 | }
|
---|
| 299 | else if (name.equals(GSXML.NODE_ID_ATT))
|
---|
| 300 | {
|
---|
| 301 | oid = value;
|
---|
| 302 | }
|
---|
| 303 | }
|
---|
| 304 |
|
---|
[24993] | 305 | _GSDM.archiveRemoveEntryFromDatabase(oid, collection, userContext);
|
---|
[24393] | 306 | _GSDM.checkError(result, ARCHIVE_REMOVE_ENTRY_FROM_DATABASE);
|
---|
| 307 |
|
---|
| 308 | return result;
|
---|
| 309 | }
|
---|
| 310 |
|
---|
| 311 | protected Element processArchiveGetAssociatedImportFiles(Element request)
|
---|
| 312 | {
|
---|
| 313 | //Create a new (empty) result message
|
---|
[28966] | 314 | Document result_doc = XMLConverter.newDOM();
|
---|
| 315 | Element result = result_doc.createElement(GSXML.RESPONSE_ELEM);
|
---|
[24393] | 316 | result.setAttribute(GSXML.FROM_ATT, ARCHIVE_GET_ASSOCIATED_IMPORT_FILES);
|
---|
| 317 | result.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_PROCESS);
|
---|
| 318 |
|
---|
[24993] | 319 | UserContext userContext = new UserContext(request);
|
---|
[24393] | 320 |
|
---|
| 321 | // Get the parameters of the request
|
---|
| 322 | Element param_list = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
|
---|
| 323 | if (param_list == null)
|
---|
| 324 | {
|
---|
[28966] | 325 | GSXML.addError(result, ARCHIVE_GET_ASSOCIATED_IMPORT_FILES + ": Missing " + GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER, GSXML.ERROR_TYPE_SYNTAX);
|
---|
[24393] | 326 | return result;
|
---|
| 327 | }
|
---|
[25635] | 328 | HashMap<String, Serializable> params = GSXML.extractParams(param_list, false);
|
---|
[24393] | 329 |
|
---|
| 330 | String oid = (String) params.get(GSXML.NODE_ID_ATT);
|
---|
| 331 | String collection = (String) params.get(GSXML.COLLECTION_ATT);
|
---|
| 332 |
|
---|
[24993] | 333 | ArrayList<String> assocFiles = _GSDM.archiveGetAssociatedImportFiles(oid, collection, userContext);
|
---|
[24393] | 334 | if(_GSDM.checkError(result, ARCHIVE_GET_ASSOCIATED_IMPORT_FILES))
|
---|
| 335 | {
|
---|
| 336 | return result;
|
---|
| 337 | }
|
---|
| 338 |
|
---|
[28966] | 339 | Element metadataList = result_doc.createElement(GSXML.METADATA_ELEM + GSXML.LIST_MODIFIER);
|
---|
| 340 | metadataList.appendChild(GSXML.createMetadataElement(result_doc, "srcfile", assocFiles.get(0)));
|
---|
[24393] | 341 |
|
---|
| 342 | for (int i = 1; i < assocFiles.size(); i++)
|
---|
| 343 | {
|
---|
[28966] | 344 | metadataList.appendChild(GSXML.createMetadataElement(result_doc, "assocfile", assocFiles.get(i)));
|
---|
[24393] | 345 | }
|
---|
| 346 |
|
---|
| 347 | result.appendChild(metadataList);
|
---|
| 348 |
|
---|
| 349 | return result;
|
---|
| 350 | }
|
---|
| 351 | } |
---|