source: main/trunk/greenstone3/src/java/org/greenstone/gsdl3/service/ArchiveRetrieve.java@ 24993

Last change on this file since 24993 was 24993, checked in by sjm84, 12 years ago

Adding UserContext to replace the use of lang and uid

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