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;
|
---|
30 | import org.greenstone.gsdl3.util.UserContext;
|
---|
31 | import org.greenstone.gsdl3.util.XMLConverter;
|
---|
32 |
|
---|
33 | import org.w3c.dom.Document;
|
---|
34 | import org.w3c.dom.Element;
|
---|
35 | import org.w3c.dom.NodeList;
|
---|
36 |
|
---|
37 | import org.apache.log4j.*;
|
---|
38 |
|
---|
39 | import java.io.File;
|
---|
40 | import java.io.Serializable;
|
---|
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 | {
|
---|
76 | Element service = this.desc_doc.createElement(GSXML.SERVICE_ELEM);
|
---|
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 |
|
---|
82 | _GSDM = new GSDocumentModel(this.site_home, this.router);
|
---|
83 |
|
---|
84 | return true;
|
---|
85 | }
|
---|
86 |
|
---|
87 | protected Element getServiceDescription(Document doc, String service_id, String lang, String subset)
|
---|
88 | {
|
---|
89 | for (int i = 0; i < _services.length; i++)
|
---|
90 | {
|
---|
91 | if (service_id.equals(_services[i]))
|
---|
92 | {
|
---|
93 | Element service_elem = doc.createElement(GSXML.SERVICE_ELEM);
|
---|
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
|
---|
110 | Document result_doc = XMLConverter.newDOM();
|
---|
111 | Element result = result_doc.createElement(GSXML.RESPONSE_ELEM);
|
---|
112 | result.setAttribute(GSXML.FROM_ATT, ARCHIVE_GET_DOCUMENT_FILE_PATH);
|
---|
113 | result.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_PROCESS);
|
---|
114 |
|
---|
115 | UserContext userContext = new UserContext(request);
|
---|
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 | {
|
---|
121 | GSXML.addError(result, ARCHIVE_GET_DOCUMENT_FILE_PATH + ": Missing " + GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER, GSXML.ERROR_TYPE_SYNTAX);
|
---|
122 | return result;
|
---|
123 | }
|
---|
124 | HashMap<String, Serializable> params = GSXML.extractParams(param_list, false);
|
---|
125 |
|
---|
126 | String oid = (String) params.get(GSXML.NODE_ID_ATT);
|
---|
127 | String collection = (String) params.get(GSXML.COLLECTION_ATT);
|
---|
128 |
|
---|
129 | String filePath = _GSDM.archiveGetDocumentFilePath(oid, collection, userContext);
|
---|
130 |
|
---|
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
|
---|
133 | result.appendChild(metadataList);
|
---|
134 |
|
---|
135 | return result;
|
---|
136 | }
|
---|
137 |
|
---|
138 | protected Element processArchiveGetSourceFileOID(Element request)
|
---|
139 | {
|
---|
140 | //Create a new (empty) result message
|
---|
141 | Document result_doc = XMLConverter.newDOM();
|
---|
142 | Element result = result_doc.createElement(GSXML.RESPONSE_ELEM);
|
---|
143 | result.setAttribute(GSXML.FROM_ATT, ARCHIVE_GET_SOURCE_FILE_OID);
|
---|
144 | result.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_PROCESS);
|
---|
145 |
|
---|
146 | UserContext userContext = new UserContext(request);
|
---|
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 | {
|
---|
152 | GSXML.addError(result, ARCHIVE_GET_SOURCE_FILE_OID + ": Missing " + GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER, GSXML.ERROR_TYPE_SYNTAX);
|
---|
153 | return result;
|
---|
154 | }
|
---|
155 | HashMap<String, Serializable> params = GSXML.extractParams(param_list, false);
|
---|
156 |
|
---|
157 | String srcFile = (String) params.get("sourcefile"); //TODO: Replace with a constant
|
---|
158 | String collection = (String) params.get(GSXML.COLLECTION_ATT);
|
---|
159 |
|
---|
160 | String oid = _GSDM.archiveGetSourceFileOID(srcFile, collection, userContext);
|
---|
161 | if(_GSDM.checkError(result, ARCHIVE_GET_SOURCE_FILE_OID))
|
---|
162 | {
|
---|
163 | return result;
|
---|
164 | }
|
---|
165 |
|
---|
166 | Element metadataList = result_doc.createElement(GSXML.METADATA_ELEM + GSXML.LIST_MODIFIER);
|
---|
167 | metadataList.appendChild(GSXML.createMetadataElement(result_doc, GSXML.NODE_ID_ATT, oid));
|
---|
168 | result.appendChild(metadataList);
|
---|
169 |
|
---|
170 | return result;
|
---|
171 | }
|
---|
172 |
|
---|
173 | protected Element processArchiveCheckDocumentOrSectionExists(Element request)
|
---|
174 | {
|
---|
175 | //Create a new (empty) result message
|
---|
176 | Document result_doc = XMLConverter.newDOM();
|
---|
177 | Element result = result_doc.createElement(GSXML.RESPONSE_ELEM);
|
---|
178 | result.setAttribute(GSXML.FROM_ATT, ARCHIVE_CHECK_DOCUMENT_OR_SECTION_EXISTS);
|
---|
179 | result.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_PROCESS);
|
---|
180 |
|
---|
181 | UserContext userContext = new UserContext(request);
|
---|
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 | {
|
---|
187 | GSXML.addError(result, ARCHIVE_CHECK_DOCUMENT_OR_SECTION_EXISTS + ": Missing " + GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER, GSXML.ERROR_TYPE_SYNTAX);
|
---|
188 | return result;
|
---|
189 | }
|
---|
190 | HashMap<String, Serializable> params = GSXML.extractParams(param_list, false);
|
---|
191 |
|
---|
192 | String oid = (String) params.get(GSXML.NODE_ID_ATT);
|
---|
193 | String collection = (String) params.get(GSXML.COLLECTION_ATT);
|
---|
194 |
|
---|
195 | boolean exists = _GSDM.archiveCheckDocumentOrSectionExists(oid, collection, userContext);
|
---|
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
|
---|
209 | Document result_doc = XMLConverter.newDOM();
|
---|
210 | Element result = result_doc.createElement(GSXML.RESPONSE_ELEM);
|
---|
211 | result.setAttribute(GSXML.FROM_ATT, ARCHIVE_WRITE_ENTRY_TO_DATABASE);
|
---|
212 | result.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_PROCESS);
|
---|
213 |
|
---|
214 | UserContext userContext = new UserContext(request);
|
---|
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 | {
|
---|
220 | GSXML.addError(result, ARCHIVE_WRITE_ENTRY_TO_DATABASE + ": Missing " + GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER, GSXML.ERROR_TYPE_SYNTAX);
|
---|
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 |
|
---|
261 | _GSDM.archiveWriteEntryToDatabase(oid, collection, info, userContext);
|
---|
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
|
---|
270 | Document result_doc = XMLConverter.newDOM();
|
---|
271 | Element result = result_doc.createElement(GSXML.RESPONSE_ELEM);
|
---|
272 | result.setAttribute(GSXML.FROM_ATT, ARCHIVE_REMOVE_ENTRY_FROM_DATABASE);
|
---|
273 | result.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_PROCESS);
|
---|
274 |
|
---|
275 | UserContext userContext = new UserContext(request);
|
---|
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 | {
|
---|
281 | GSXML.addError(result, ARCHIVE_REMOVE_ENTRY_FROM_DATABASE + ": Missing " + GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER, GSXML.ERROR_TYPE_SYNTAX);
|
---|
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 |
|
---|
305 | _GSDM.archiveRemoveEntryFromDatabase(oid, collection, userContext);
|
---|
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
|
---|
314 | Document result_doc = XMLConverter.newDOM();
|
---|
315 | Element result = result_doc.createElement(GSXML.RESPONSE_ELEM);
|
---|
316 | result.setAttribute(GSXML.FROM_ATT, ARCHIVE_GET_ASSOCIATED_IMPORT_FILES);
|
---|
317 | result.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_PROCESS);
|
---|
318 |
|
---|
319 | UserContext userContext = new UserContext(request);
|
---|
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 | {
|
---|
325 | GSXML.addError(result, ARCHIVE_GET_ASSOCIATED_IMPORT_FILES + ": Missing " + GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER, GSXML.ERROR_TYPE_SYNTAX);
|
---|
326 | return result;
|
---|
327 | }
|
---|
328 | HashMap<String, Serializable> params = GSXML.extractParams(param_list, false);
|
---|
329 |
|
---|
330 | String oid = (String) params.get(GSXML.NODE_ID_ATT);
|
---|
331 | String collection = (String) params.get(GSXML.COLLECTION_ATT);
|
---|
332 |
|
---|
333 | ArrayList<String> assocFiles = _GSDM.archiveGetAssociatedImportFiles(oid, collection, userContext);
|
---|
334 | if(_GSDM.checkError(result, ARCHIVE_GET_ASSOCIATED_IMPORT_FILES))
|
---|
335 | {
|
---|
336 | return result;
|
---|
337 | }
|
---|
338 |
|
---|
339 | Element metadataList = result_doc.createElement(GSXML.METADATA_ELEM + GSXML.LIST_MODIFIER);
|
---|
340 | metadataList.appendChild(GSXML.createMetadataElement(result_doc, "srcfile", assocFiles.get(0)));
|
---|
341 |
|
---|
342 | for (int i = 1; i < assocFiles.size(); i++)
|
---|
343 | {
|
---|
344 | metadataList.appendChild(GSXML.createMetadataElement(result_doc, "assocfile", assocFiles.get(i)));
|
---|
345 | }
|
---|
346 |
|
---|
347 | result.appendChild(metadataList);
|
---|
348 |
|
---|
349 | return result;
|
---|
350 | }
|
---|
351 | } |
---|