1 | /*
|
---|
2 | * ArchiveRetrieve.java
|
---|
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 |
|
---|
23 | import org.greenstone.gsdl3.util.DBHelper;
|
---|
24 | import org.greenstone.gsdl3.util.DBInfo;
|
---|
25 | import org.greenstone.gsdl3.util.GSPath;
|
---|
26 | import org.greenstone.gsdl3.util.GSXML;
|
---|
27 | import org.greenstone.gsdl3.util.SimpleCollectionDatabase;
|
---|
28 | import org.greenstone.gsdl3.util.UserContext;
|
---|
29 | import org.greenstone.gsdl3.util.XMLConverter;
|
---|
30 |
|
---|
31 | import org.w3c.dom.Document;
|
---|
32 | import org.w3c.dom.Element;
|
---|
33 |
|
---|
34 | import org.apache.log4j.*;
|
---|
35 |
|
---|
36 | import java.io.File;
|
---|
37 | import java.io.Serializable;
|
---|
38 | import java.util.HashMap;
|
---|
39 | import java.util.Vector;
|
---|
40 |
|
---|
41 | public class ArchiveRetrieve extends ServiceRack
|
---|
42 | {
|
---|
43 | static Logger logger = Logger.getLogger(org.greenstone.gsdl3.service.ArchiveRetrieve.class.getName());
|
---|
44 |
|
---|
45 | protected static final String DOCUMENT_FILE_PATH_RETRIEVE_SERVICE = "DocumentFilePathRetrieve";
|
---|
46 | protected static final String ASSOCIATED_IMPORT_FILES_RETRIEVE_SERVICE = "AssociatedImportFilesRetrieve";
|
---|
47 | protected static final String SOURCE_FILE_OID_RETRIEVE = "SourceFileOIDRetrieve";
|
---|
48 |
|
---|
49 | protected SimpleCollectionDatabase coll_db = null;
|
---|
50 |
|
---|
51 | /** configure this service */
|
---|
52 | public boolean configure(Element info, Element extra_info)
|
---|
53 | {
|
---|
54 | if (!super.configure(info, extra_info))
|
---|
55 | {
|
---|
56 | return false;
|
---|
57 | }
|
---|
58 |
|
---|
59 | logger.info("Configuring ArchiveRetrieve...");
|
---|
60 | this.config_info = info;
|
---|
61 |
|
---|
62 | Element documentFilePathRetrieveService = this.desc_doc.createElement(GSXML.SERVICE_ELEM);
|
---|
63 | documentFilePathRetrieveService.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_RETRIEVE);
|
---|
64 | documentFilePathRetrieveService.setAttribute(GSXML.NAME_ATT, DOCUMENT_FILE_PATH_RETRIEVE_SERVICE);
|
---|
65 | this.short_service_info.appendChild(documentFilePathRetrieveService);
|
---|
66 |
|
---|
67 | Element associatedImportFilesRetrieveService = this.desc_doc.createElement(GSXML.SERVICE_ELEM);
|
---|
68 | associatedImportFilesRetrieveService.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_RETRIEVE);
|
---|
69 | associatedImportFilesRetrieveService.setAttribute(GSXML.NAME_ATT, ASSOCIATED_IMPORT_FILES_RETRIEVE_SERVICE);
|
---|
70 | this.short_service_info.appendChild(associatedImportFilesRetrieveService);
|
---|
71 |
|
---|
72 | Element sourceFileDocIDRetrieveService = this.desc_doc.createElement(GSXML.SERVICE_ELEM);
|
---|
73 | sourceFileDocIDRetrieveService.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_RETRIEVE);
|
---|
74 | sourceFileDocIDRetrieveService.setAttribute(GSXML.NAME_ATT, SOURCE_FILE_OID_RETRIEVE);
|
---|
75 | this.short_service_info.appendChild(sourceFileDocIDRetrieveService);
|
---|
76 |
|
---|
77 | return true;
|
---|
78 | }
|
---|
79 |
|
---|
80 | protected Element getServiceDescription(Document doc, String service_id, String lang, String subset)
|
---|
81 | {
|
---|
82 | if (service_id.equals(DOCUMENT_FILE_PATH_RETRIEVE_SERVICE))
|
---|
83 | {
|
---|
84 | Element service_elem = doc.createElement(GSXML.SERVICE_ELEM);
|
---|
85 | service_elem.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_RETRIEVE);
|
---|
86 | service_elem.setAttribute(GSXML.NAME_ATT, DOCUMENT_FILE_PATH_RETRIEVE_SERVICE);
|
---|
87 | return service_elem;
|
---|
88 | }
|
---|
89 | else if (service_id.equals(ASSOCIATED_IMPORT_FILES_RETRIEVE_SERVICE))
|
---|
90 | {
|
---|
91 | Element service_elem = doc.createElement(GSXML.SERVICE_ELEM);
|
---|
92 | service_elem.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_RETRIEVE);
|
---|
93 | service_elem.setAttribute(GSXML.NAME_ATT, ASSOCIATED_IMPORT_FILES_RETRIEVE_SERVICE);
|
---|
94 | return service_elem;
|
---|
95 | }
|
---|
96 | else if (service_id.equals(SOURCE_FILE_OID_RETRIEVE))
|
---|
97 | {
|
---|
98 | Element service_elem = doc.createElement(GSXML.SERVICE_ELEM);
|
---|
99 | service_elem.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_RETRIEVE);
|
---|
100 | service_elem.setAttribute(GSXML.NAME_ATT, SOURCE_FILE_OID_RETRIEVE);
|
---|
101 | return service_elem;
|
---|
102 | }
|
---|
103 | return null;
|
---|
104 | }
|
---|
105 |
|
---|
106 | protected Element processDocumentFilePathRetrieve(Element request)
|
---|
107 | {
|
---|
108 | // Create a new (empty) result message
|
---|
109 | Document result_doc = XMLConverter.newDOM();
|
---|
110 | Element result = result_doc.createElement(GSXML.RESPONSE_ELEM);
|
---|
111 | result.setAttribute(GSXML.FROM_ATT, DOCUMENT_FILE_PATH_RETRIEVE_SERVICE);
|
---|
112 | result.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_PROCESS);
|
---|
113 |
|
---|
114 | UserContext userContext = new UserContext(request);
|
---|
115 |
|
---|
116 | // Get the parameters of the request
|
---|
117 | Element param_list = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
|
---|
118 | if (param_list == null) {
|
---|
119 | GSXML.addError(result, "DocumentFilePathRetrieve: missing "+ GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER, GSXML.ERROR_TYPE_SYNTAX);
|
---|
120 | return result;
|
---|
121 | }
|
---|
122 | HashMap<String, Serializable> params = GSXML.extractParams(param_list, false);
|
---|
123 |
|
---|
124 | String oid = (String) params.get("oid");
|
---|
125 | String collection = (String) params.get("c");
|
---|
126 |
|
---|
127 | String assocFilePath = getAssocFilePathFromDocID(oid, collection, userContext);
|
---|
128 |
|
---|
129 | String docFilePath = this.site_home + File.separatorChar +
|
---|
130 | "collect" + File.separatorChar +
|
---|
131 | collection + File.separatorChar +
|
---|
132 | "archives" + File.separatorChar +
|
---|
133 | assocFilePath + File.separatorChar +
|
---|
134 | "doc.xml";
|
---|
135 |
|
---|
136 | Element metadataList = result_doc.createElement(GSXML.METADATA_ELEM + GSXML.LIST_MODIFIER);
|
---|
137 | metadataList.appendChild(GSXML.createMetadataElement(result_doc, "docfilepath", docFilePath));
|
---|
138 | result.appendChild(metadataList);
|
---|
139 |
|
---|
140 | return result;
|
---|
141 | }
|
---|
142 |
|
---|
143 |
|
---|
144 | /** @function processSourceFileOIDRetrieveService(Element)
|
---|
145 | * @brief
|
---|
146 | * @param request
|
---|
147 | * @return Element
|
---|
148 | */
|
---|
149 | protected Element processSourceFileOIDRetrieveService(Element request)
|
---|
150 | {
|
---|
151 | //Create a new (empty) result message
|
---|
152 | Document result_doc = XMLConverter.newDOM();
|
---|
153 | Element result = result_doc.createElement(GSXML.RESPONSE_ELEM);
|
---|
154 | result.setAttribute(GSXML.FROM_ATT, SOURCE_FILE_OID_RETRIEVE);
|
---|
155 | result.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_PROCESS);
|
---|
156 |
|
---|
157 | UserContext userContext = new UserContext(request);
|
---|
158 |
|
---|
159 | // Get the parameters of the request
|
---|
160 | Element param_list = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
|
---|
161 | if (param_list == null)
|
---|
162 | {
|
---|
163 | GSXML.addError(result, "DocumentFilePathRetrieve: missing "+ GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER, GSXML.ERROR_TYPE_SYNTAX);
|
---|
164 | return result;
|
---|
165 | }
|
---|
166 | HashMap<String, Serializable> params = GSXML.extractParams(param_list, false);
|
---|
167 |
|
---|
168 | String srcFile = (String) params.get("srcfile");
|
---|
169 | String collection = (String) params.get("c");
|
---|
170 |
|
---|
171 | //Find out what kind of database we have
|
---|
172 | String databaseType = getDatabaseTypeFromCollection(collection, userContext);
|
---|
173 | if (databaseType == null || databaseType.equals(""))
|
---|
174 | {
|
---|
175 | databaseType = "gdbm"; // the default
|
---|
176 | }
|
---|
177 |
|
---|
178 | coll_db = new SimpleCollectionDatabase(databaseType);
|
---|
179 | if (!coll_db.databaseOK())
|
---|
180 | {
|
---|
181 | logger.error("Couldn't create the collection database of type "+databaseType);
|
---|
182 | return null;
|
---|
183 | }
|
---|
184 |
|
---|
185 | // Moved to ensure that the appropriate FlatDatabaseWrapper
|
---|
186 | // has been initialised during the SimpleCollectionDatabase
|
---|
187 | // call above. That way we can easily retrieve the database
|
---|
188 | // extension from the DBHelper [jmt12]
|
---|
189 | String dbExt = DBHelper.getDBExtFromDBType(databaseType);
|
---|
190 | if (null == dbExt || dbExt.equals("")) {
|
---|
191 | // assume gdbm
|
---|
192 | logger.warn("Could not recognise database type \"" + databaseType + "\", defaulting to GDBM and extension \".gdb\"");
|
---|
193 | dbExt = ".gdb";
|
---|
194 | }
|
---|
195 |
|
---|
196 | coll_db.openDatabase
|
---|
197 | (
|
---|
198 | this.site_home + File.separatorChar +
|
---|
199 | "collect" + File.separatorChar +
|
---|
200 | collection + File.separatorChar +
|
---|
201 | "archives" + File.separatorChar +
|
---|
202 | "archiveinf-src" + dbExt,
|
---|
203 | SimpleCollectionDatabase.READ
|
---|
204 | );
|
---|
205 |
|
---|
206 | DBInfo info = coll_db.getInfo(srcFile);
|
---|
207 |
|
---|
208 | if (info == null)
|
---|
209 | {
|
---|
210 | return result;
|
---|
211 | }
|
---|
212 |
|
---|
213 | String oid = info.getInfo("oid");
|
---|
214 |
|
---|
215 | Element metadataList = result_doc.createElement(GSXML.METADATA_ELEM + GSXML.LIST_MODIFIER);
|
---|
216 | metadataList.appendChild(GSXML.createMetadataElement(result_doc, "oid", oid));
|
---|
217 | result.appendChild(metadataList);
|
---|
218 |
|
---|
219 | return result;
|
---|
220 | }
|
---|
221 | /** processSourceFileOIDRetrieveService(Element) **/
|
---|
222 |
|
---|
223 | protected Element processAssociatedImportFilesRetrieve(Element request)
|
---|
224 | {
|
---|
225 | //Create a new (empty) result message
|
---|
226 | Document result_doc = XMLConverter.newDOM();
|
---|
227 | Element result = result_doc.createElement(GSXML.RESPONSE_ELEM);
|
---|
228 | result.setAttribute(GSXML.FROM_ATT, ASSOCIATED_IMPORT_FILES_RETRIEVE_SERVICE);
|
---|
229 | result.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_PROCESS);
|
---|
230 |
|
---|
231 | UserContext userContext = new UserContext(request);
|
---|
232 |
|
---|
233 | // Get the parameters of the request
|
---|
234 | Element param_list = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
|
---|
235 | if (param_list == null)
|
---|
236 | {
|
---|
237 | GSXML.addError(result, "AssociatedImportFilesRetrieve: missing "+ GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER, GSXML.ERROR_TYPE_SYNTAX);
|
---|
238 | return result;
|
---|
239 | }
|
---|
240 | HashMap<String, Serializable> params = GSXML.extractParams(param_list, false);
|
---|
241 |
|
---|
242 | String oid = (String) params.get("oid");
|
---|
243 | String collection = (String) params.get("c");
|
---|
244 |
|
---|
245 | String databaseType = getDatabaseTypeFromCollection(collection, userContext);
|
---|
246 | if (databaseType == null || databaseType.equals(""))
|
---|
247 | {
|
---|
248 | databaseType = "gdbm"; // the default
|
---|
249 | }
|
---|
250 |
|
---|
251 | coll_db = new SimpleCollectionDatabase(databaseType);
|
---|
252 | if (!coll_db.databaseOK())
|
---|
253 | {
|
---|
254 | logger.error("Couldn't create the collection database of type "+databaseType);
|
---|
255 | return null;
|
---|
256 | }
|
---|
257 |
|
---|
258 | // Moved to ensure that the appropriate FlatDatabaseWrapper
|
---|
259 | // has been initialised during the SimpleCollectionDatabase
|
---|
260 | // call above. That way we can easily retrieve the database
|
---|
261 | // extension from the DBHelper.
|
---|
262 | String dbExt = DBHelper.getDBExtFromDBType(databaseType);
|
---|
263 | if (null == dbExt || dbExt.equals(""))
|
---|
264 | {
|
---|
265 | // assume gdbm
|
---|
266 | logger.warn("Could not recognise database type \"" + databaseType + "\", defaulting to GDBM and extension \".gdb\"");
|
---|
267 | dbExt = ".gdb";
|
---|
268 | }
|
---|
269 |
|
---|
270 | coll_db.openDatabase
|
---|
271 | (
|
---|
272 | this.site_home + File.separatorChar +
|
---|
273 | "collect" + File.separatorChar +
|
---|
274 | collection + File.separatorChar +
|
---|
275 | "archives" + File.separatorChar +
|
---|
276 | "archiveinf-doc" + dbExt,
|
---|
277 | SimpleCollectionDatabase.READ
|
---|
278 | );
|
---|
279 |
|
---|
280 | DBInfo info = coll_db.getInfo(oid);
|
---|
281 |
|
---|
282 | if (info == null)
|
---|
283 | {
|
---|
284 | return result;
|
---|
285 | }
|
---|
286 |
|
---|
287 | String srcFile = info.getInfo("src-file");
|
---|
288 | Vector<String> data = info.getMultiInfo("assoc-file");
|
---|
289 |
|
---|
290 | Element metadataList = result_doc.createElement(GSXML.METADATA_ELEM + GSXML.LIST_MODIFIER);
|
---|
291 | metadataList.appendChild(GSXML.createMetadataElement(result_doc, "srcfile", srcFile));
|
---|
292 |
|
---|
293 | for (int i = 0; i < data.size(); i++)
|
---|
294 | {
|
---|
295 | metadataList.appendChild(GSXML.createMetadataElement(result_doc, "assocfile", data.get(i)));
|
---|
296 | }
|
---|
297 |
|
---|
298 | result.appendChild(metadataList);
|
---|
299 |
|
---|
300 | return result;
|
---|
301 | }
|
---|
302 |
|
---|
303 |
|
---|
304 | public String getAssocFilePathFromDocID(String oid, String collection, UserContext userContext)
|
---|
305 | {
|
---|
306 | Document doc = XMLConverter.newDOM();
|
---|
307 | Element mr_query_message = doc.createElement(GSXML.MESSAGE_ELEM);
|
---|
308 | Element mr_query_request = GSXML.createBasicRequest (doc, GSXML.REQUEST_TYPE_PAGE, collection + "/DocumentMetadataRetrieve", userContext);
|
---|
309 | mr_query_message.appendChild(mr_query_request);
|
---|
310 |
|
---|
311 | Element paramList = doc.createElement(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
|
---|
312 | paramList.appendChild(GSXML.createMetadataElement(doc, "metadata", "assocfilepath"));
|
---|
313 |
|
---|
314 | mr_query_request.appendChild(paramList);
|
---|
315 |
|
---|
316 | Element docListElem = doc.createElement(GSXML.DOC_NODE_ELEM+GSXML.LIST_MODIFIER);
|
---|
317 | Element docElem = doc.createElement(GSXML.DOC_NODE_ELEM);
|
---|
318 | docElem.setAttribute(GSXML.NODE_ID_ATT, oid);
|
---|
319 | docListElem.appendChild(docElem);
|
---|
320 | mr_query_request.appendChild(docListElem);
|
---|
321 |
|
---|
322 | Element response = (Element) this.router.process(mr_query_message);
|
---|
323 |
|
---|
324 | String path = GSPath.appendLink(GSXML.RESPONSE_ELEM, GSXML.DOC_NODE_ELEM+GSXML.LIST_MODIFIER);
|
---|
325 | path = GSPath.appendLink(path, GSXML.DOC_NODE_ELEM);
|
---|
326 | path = GSPath.appendLink(path, GSXML.METADATA_ELEM+GSXML.LIST_MODIFIER);
|
---|
327 | Element metadataListElem = (Element) GSXML.getNodeByPath(response, path);
|
---|
328 | Element metadataElem = (Element) metadataListElem.getFirstChild();
|
---|
329 |
|
---|
330 | return metadataElem.getFirstChild().getNodeValue();
|
---|
331 | }
|
---|
332 |
|
---|
333 | public String getDatabaseTypeFromCollection(String collection, UserContext userContext)
|
---|
334 | {
|
---|
335 | //Find out what kind of database we have
|
---|
336 | Document doc = XMLConverter.newDOM();
|
---|
337 | Element dbTypeMessage = doc.createElement(GSXML.MESSAGE_ELEM);
|
---|
338 | Element dbTypeRequest = GSXML.createBasicRequest(doc, GSXML.REQUEST_TYPE_DESCRIBE, collection, userContext);
|
---|
339 | dbTypeMessage.appendChild(dbTypeRequest);
|
---|
340 | Element dbTypeResponse = (Element)this.router.process(dbTypeMessage);
|
---|
341 |
|
---|
342 | String path = GSPath.appendLink(GSXML.RESPONSE_ELEM, GSXML.COLLECTION_ELEM);
|
---|
343 | Element collectionElem = (Element) GSXML.getNodeByPath(dbTypeResponse, path);
|
---|
344 |
|
---|
345 | if (collectionElem != null)
|
---|
346 | {
|
---|
347 | return collectionElem.getAttribute(GSXML.DB_TYPE_ATT);
|
---|
348 | }
|
---|
349 | return null;
|
---|
350 | }
|
---|
351 | }
|
---|