source: main/trunk/greenstone3/src/java/org/greenstone/gsdl3/service/ArchiveIO.java@ 26198

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

Fixing Greenstone 3's use (or lack thereof) of generics, this was done automatically so we may want to change it over time. This change will also auto-format any files that have not already been formatted.

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