1 | /*
|
---|
2 | * DocumentMaker.java
|
---|
3 | * The Document Maker service that can be used to create/modify custom
|
---|
4 | * documents in Greenstone collection
|
---|
5 | *
|
---|
6 | * Copyright (C) 2005 New Zealand Digital Library, http://www.nzdl.org
|
---|
7 | *
|
---|
8 | * This program is free software; you can redistribute it and/or modify
|
---|
9 | * it under the terms of the GNU General Public License as published by
|
---|
10 | * the Free Software Foundation; either version 2 of the License, or
|
---|
11 | * (at your option) any later version.
|
---|
12 | *
|
---|
13 | * This program is distributed in the hope that it will be useful,
|
---|
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
16 | * GNU General Public License for more details.
|
---|
17 | *
|
---|
18 | * You should have received a copy of the GNU General Public License
|
---|
19 | * along with this program; if not, write to the Free Software
|
---|
20 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
21 | */
|
---|
22 | package org.greenstone.gsdl3.service;
|
---|
23 |
|
---|
24 | import java.io.File;
|
---|
25 | import java.io.FileInputStream;
|
---|
26 | import java.io.FileOutputStream;
|
---|
27 | import java.nio.channels.FileChannel;
|
---|
28 | import java.util.HashMap;
|
---|
29 |
|
---|
30 | import org.apache.log4j.*;
|
---|
31 | import org.apache.tools.ant.util.FileUtils;
|
---|
32 |
|
---|
33 | import org.greenstone.gsdl3.util.GSDocumentModel;
|
---|
34 | import org.greenstone.gsdl3.util.GSXML;
|
---|
35 |
|
---|
36 | import org.w3c.dom.Element;
|
---|
37 | import org.w3c.dom.NodeList;
|
---|
38 |
|
---|
39 | public class DocumentMaker extends ServiceRack
|
---|
40 | {
|
---|
41 | static Logger logger = Logger.getLogger(org.greenstone.gsdl3.service.ArchiveIO.class.getName());
|
---|
42 |
|
---|
43 | GSDocumentModel _GSDM = null;
|
---|
44 |
|
---|
45 | /****************************************************
|
---|
46 | * The list of services the Document Maker supports *
|
---|
47 | ***************************************************/
|
---|
48 | //Document/section services
|
---|
49 | protected static final String DOCUMENT_CREATE = "DocumentCreate";
|
---|
50 | protected static final String DOCUMENT_DELETE = "DocumentDelete";
|
---|
51 | protected static final String DOCUMENT_DUPLICATE = "DocumentDuplicate";
|
---|
52 | protected static final String DOCUMENT_MOVE = "DocumentMove";
|
---|
53 | protected static final String DOCUMENT_MERGE = "DocumentMerge";
|
---|
54 | protected static final String DOCUMENT_SPLIT = "DocumentSplit";
|
---|
55 | protected static final String DOCUMENT_GET_INFORMATION = "DocumentGetInformation";
|
---|
56 |
|
---|
57 | //Metadata services
|
---|
58 | protected static final String DOCUMENT_GET_METADATA = "DocumentGetMetadata";
|
---|
59 | protected static final String DOCUMENT_ADD_METADATA = "DocumentAddMetadata";
|
---|
60 | protected static final String DOCUMENT_DELETE_METADATA = "DocumentDeleteMetadata";
|
---|
61 |
|
---|
62 | //Document text services
|
---|
63 | protected static final String DOCUMENT_GET_TEXT = "DocumentGetText";
|
---|
64 | protected static final String DOCUMENT_SET_TEXT = "DocumentSetText";
|
---|
65 |
|
---|
66 | //Other services
|
---|
67 | protected static final String DOCUMENT_EXECUTE_TRANSACTION = "DocumentExecuteTransaction";
|
---|
68 | /***************************************************/
|
---|
69 |
|
---|
70 | String[] services = { DOCUMENT_CREATE, DOCUMENT_DELETE, DOCUMENT_DUPLICATE, DOCUMENT_GET_INFORMATION, DOCUMENT_MOVE, DOCUMENT_MERGE, DOCUMENT_SPLIT, DOCUMENT_GET_METADATA, DOCUMENT_ADD_METADATA, DOCUMENT_DELETE_METADATA, DOCUMENT_GET_TEXT, DOCUMENT_SET_TEXT, DOCUMENT_EXECUTE_TRANSACTION };
|
---|
71 |
|
---|
72 | /** configure this service */
|
---|
73 | public boolean configure(Element info, Element extra_info)
|
---|
74 | {
|
---|
75 | if (!super.configure(info, extra_info))
|
---|
76 | {
|
---|
77 | return false;
|
---|
78 | }
|
---|
79 |
|
---|
80 | logger.info("Configuring DocumentMaker...");
|
---|
81 | this.config_info = info;
|
---|
82 |
|
---|
83 | for (int i = 0; i < services.length; i++)
|
---|
84 | {
|
---|
85 | Element service = this.doc.createElement(GSXML.SERVICE_ELEM);
|
---|
86 | service.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_RETRIEVE);
|
---|
87 | service.setAttribute(GSXML.NAME_ATT, services[i]);
|
---|
88 | this.short_service_info.appendChild(service);
|
---|
89 | }
|
---|
90 |
|
---|
91 | _GSDM = new GSDocumentModel(this.site_home, this.doc, this.router);
|
---|
92 |
|
---|
93 | return true;
|
---|
94 | }
|
---|
95 |
|
---|
96 | protected Element getServiceDescription(String service_id, String lang, String subset)
|
---|
97 | {
|
---|
98 | for (int i = 0; i < services.length; i++)
|
---|
99 | {
|
---|
100 | if (service_id.equals(services[i]))
|
---|
101 | {
|
---|
102 | Element service_elem = this.doc.createElement(GSXML.SERVICE_ELEM);
|
---|
103 | service_elem.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_RETRIEVE);
|
---|
104 | service_elem.setAttribute(GSXML.NAME_ATT, services[i]);
|
---|
105 | return service_elem;
|
---|
106 | }
|
---|
107 | }
|
---|
108 |
|
---|
109 | return null;
|
---|
110 | }
|
---|
111 |
|
---|
112 | /************
|
---|
113 | * Services *
|
---|
114 | ***********/
|
---|
115 |
|
---|
116 | protected Element processDocumentCreate(Element request)
|
---|
117 | {
|
---|
118 | Element result = GSXML.createBasicResponse(this.doc, DOCUMENT_CREATE);
|
---|
119 |
|
---|
120 | if (request == null)
|
---|
121 | {
|
---|
122 | GSXML.addError(this.doc, result, DOCUMENT_CREATE + ": Request is null", GSXML.ERROR_TYPE_SYNTAX);
|
---|
123 | return result;
|
---|
124 | }
|
---|
125 |
|
---|
126 | String lang = request.getAttribute(GSXML.LANG_ATT);
|
---|
127 | String uid = request.getAttribute(GSXML.USER_ID_ATT);
|
---|
128 |
|
---|
129 | //Get the list of documents to create
|
---|
130 | NodeList documents = request.getElementsByTagName(GSXML.DOCUMENT_ELEM);
|
---|
131 | for (int i = 0; i < documents.getLength(); i++)
|
---|
132 | {
|
---|
133 | //Get information about the current new document
|
---|
134 | Element currentDoc = (Element) documents.item(i);
|
---|
135 | String oid = currentDoc.getAttribute(GSXML.NODE_ID_ATT);
|
---|
136 | String collection = currentDoc.getAttribute(GSXML.COLLECTION_ATT);
|
---|
137 |
|
---|
138 | _GSDM.documentCreate(oid, collection, lang, uid);
|
---|
139 | if (_GSDM.checkError(result, DOCUMENT_CREATE))
|
---|
140 | {
|
---|
141 | return result;
|
---|
142 | }
|
---|
143 | }
|
---|
144 |
|
---|
145 | return result;
|
---|
146 | }
|
---|
147 |
|
---|
148 | protected Element processDocumentDelete(Element request)
|
---|
149 | {
|
---|
150 | Element result = GSXML.createBasicResponse(this.doc, DOCUMENT_DELETE);
|
---|
151 |
|
---|
152 | if (request == null)
|
---|
153 | {
|
---|
154 | GSXML.addError(this.doc, result, DOCUMENT_DELETE + ": Request is null", GSXML.ERROR_TYPE_SYNTAX);
|
---|
155 | return result;
|
---|
156 | }
|
---|
157 |
|
---|
158 | String lang = request.getAttribute(GSXML.LANG_ATT);
|
---|
159 | String uid = request.getAttribute(GSXML.USER_ID_ATT);
|
---|
160 |
|
---|
161 | //Get the list of documents to delete
|
---|
162 | NodeList documents = request.getElementsByTagName(GSXML.DOCUMENT_ELEM);
|
---|
163 | for (int i = 0; i < documents.getLength(); i++)
|
---|
164 | {
|
---|
165 | Element currentDoc = (Element) documents.item(i);
|
---|
166 | String oid = currentDoc.getAttribute(GSXML.NODE_ID_ATT);
|
---|
167 | String collection = currentDoc.getAttribute(GSXML.COLLECTION_ATT);
|
---|
168 |
|
---|
169 | _GSDM.documentDelete(oid, collection, lang, uid);
|
---|
170 | if (_GSDM.checkError(result, DOCUMENT_DELETE))
|
---|
171 | {
|
---|
172 | return result;
|
---|
173 | }
|
---|
174 | }
|
---|
175 |
|
---|
176 | return result;
|
---|
177 | }
|
---|
178 |
|
---|
179 | protected Element processDocumentDuplicate(Element request)
|
---|
180 | {
|
---|
181 | Element result = GSXML.createBasicResponse(this.doc, DOCUMENT_DUPLICATE);
|
---|
182 |
|
---|
183 | if (request == null)
|
---|
184 | {
|
---|
185 | GSXML.addError(this.doc, result, DOCUMENT_DUPLICATE + ": Request is null", GSXML.ERROR_TYPE_SYNTAX);
|
---|
186 | return result;
|
---|
187 | }
|
---|
188 |
|
---|
189 | String lang = request.getAttribute(GSXML.LANG_ATT);
|
---|
190 | String uid = request.getAttribute(GSXML.USER_ID_ATT);
|
---|
191 |
|
---|
192 | //Get the list of documents to duplicate
|
---|
193 | NodeList documents = request.getElementsByTagName(GSXML.DOCUMENT_ELEM);
|
---|
194 | for (int i = 0; i < documents.getLength(); i++)
|
---|
195 | {
|
---|
196 | Element currentDoc = (Element) documents.item(i);
|
---|
197 | String oid = currentDoc.getAttribute(GSXML.NODE_ID_ATT);
|
---|
198 | String collection = currentDoc.getAttribute(GSXML.COLLECTION_ATT);
|
---|
199 | String newOID = currentDoc.getAttribute("new" + GSXML.NODE_ID_ATT);
|
---|
200 | String newCollection = currentDoc.getAttribute("new" + GSXML.COLLECTION_ATT);
|
---|
201 |
|
---|
202 | _GSDM.documentDuplicate(oid, collection, newOID, newCollection, lang, uid);
|
---|
203 | if (_GSDM.checkError(result, DOCUMENT_DUPLICATE))
|
---|
204 | {
|
---|
205 | return result;
|
---|
206 | }
|
---|
207 | }
|
---|
208 |
|
---|
209 | return result;
|
---|
210 | }
|
---|
211 |
|
---|
212 | protected Element processDocumentGetInformation(Element request)
|
---|
213 | {
|
---|
214 | Element result = GSXML.createBasicResponse(this.doc, DOCUMENT_GET_INFORMATION);
|
---|
215 |
|
---|
216 | if (request == null)
|
---|
217 | {
|
---|
218 | GSXML.addError(this.doc, result, DOCUMENT_GET_INFORMATION + ": Request is null", GSXML.ERROR_TYPE_SYNTAX);
|
---|
219 | return result;
|
---|
220 | }
|
---|
221 |
|
---|
222 | String lang = request.getAttribute(GSXML.LANG_ATT);
|
---|
223 | String uid = request.getAttribute(GSXML.USER_ID_ATT);
|
---|
224 |
|
---|
225 | //Get the list of documents to duplicate
|
---|
226 | NodeList documents = request.getElementsByTagName(GSXML.DOCUMENT_ELEM);
|
---|
227 | for (int i = 0; i < documents.getLength(); i++)
|
---|
228 | {
|
---|
229 | Element currentDoc = (Element) documents.item(i);
|
---|
230 | String oid = currentDoc.getAttribute(GSXML.NODE_ID_ATT);
|
---|
231 | String collection = currentDoc.getAttribute(GSXML.COLLECTION_ATT);
|
---|
232 |
|
---|
233 | NodeList requestedInfoList = currentDoc.getElementsByTagName("info"); //TODO: Replace info with a constant
|
---|
234 | String[] requestedInfo = new String[requestedInfoList.getLength()];
|
---|
235 |
|
---|
236 | for(int j = 0; j < requestedInfoList.getLength(); j++)
|
---|
237 | {
|
---|
238 | requestedInfo[j] = ((Element) requestedInfoList.item(j)).getAttribute(GSXML.NAME_ATT);
|
---|
239 | }
|
---|
240 |
|
---|
241 | _GSDM.documentGetInformation(oid, collection, requestedInfo, lang, uid);
|
---|
242 | if (_GSDM.checkError(result, DOCUMENT_GET_INFORMATION))
|
---|
243 | {
|
---|
244 | return result;
|
---|
245 | }
|
---|
246 | }
|
---|
247 |
|
---|
248 | return result;
|
---|
249 | }
|
---|
250 |
|
---|
251 | protected Element processDocumentMove(Element request)
|
---|
252 | {
|
---|
253 | Element result = GSXML.createBasicResponse(this.doc, DOCUMENT_MOVE);
|
---|
254 |
|
---|
255 | if (request == null)
|
---|
256 | {
|
---|
257 | GSXML.addError(this.doc, result, DOCUMENT_MOVE + ": Request is null", GSXML.ERROR_TYPE_SYNTAX);
|
---|
258 | return result;
|
---|
259 | }
|
---|
260 |
|
---|
261 | String lang = request.getAttribute(GSXML.LANG_ATT);
|
---|
262 | String uid = request.getAttribute(GSXML.USER_ID_ATT);
|
---|
263 |
|
---|
264 | //Get the list of documents to duplicate
|
---|
265 | NodeList documents = request.getElementsByTagName(GSXML.DOCUMENT_ELEM);
|
---|
266 | for (int i = 0; i < documents.getLength(); i++)
|
---|
267 | {
|
---|
268 | Element currentDoc = (Element) documents.item(i);
|
---|
269 | String oid = currentDoc.getAttribute(GSXML.NODE_ID_ATT);
|
---|
270 | String collection = currentDoc.getAttribute(GSXML.COLLECTION_ATT);
|
---|
271 | String newOID = currentDoc.getAttribute("new" + GSXML.NODE_ID_ATT);
|
---|
272 | String newCollection = currentDoc.getAttribute("new" + GSXML.COLLECTION_ATT);
|
---|
273 |
|
---|
274 | _GSDM.documentMove(oid, collection, newOID, newCollection, lang, uid);
|
---|
275 | if (_GSDM.checkError(result, DOCUMENT_MOVE))
|
---|
276 | {
|
---|
277 | return result;
|
---|
278 | }
|
---|
279 | }
|
---|
280 | return result;
|
---|
281 | }
|
---|
282 |
|
---|
283 | protected Element processDocumentMerge(Element request)
|
---|
284 | {
|
---|
285 | Element result = GSXML.createBasicResponse(this.doc, DOCUMENT_MERGE);
|
---|
286 |
|
---|
287 | if (request == null)
|
---|
288 | {
|
---|
289 | GSXML.addError(this.doc, result, DOCUMENT_MERGE + ": Request is null", GSXML.ERROR_TYPE_SYNTAX);
|
---|
290 | return result;
|
---|
291 | }
|
---|
292 |
|
---|
293 | String lang = request.getAttribute(GSXML.LANG_ATT);
|
---|
294 | String uid = request.getAttribute(GSXML.USER_ID_ATT);
|
---|
295 |
|
---|
296 | //Get the list of documents to duplicate
|
---|
297 | NodeList documents = request.getElementsByTagName(GSXML.DOCUMENT_ELEM);
|
---|
298 | for (int i = 0; i < documents.getLength(); i++)
|
---|
299 | {
|
---|
300 | Element currentDoc = (Element) documents.item(i);
|
---|
301 | String oid = currentDoc.getAttribute(GSXML.NODE_ID_ATT);
|
---|
302 | String collection = currentDoc.getAttribute(GSXML.COLLECTION_ATT);
|
---|
303 | String mergeOID = currentDoc.getAttribute("merge" + GSXML.NODE_ID_ATT);
|
---|
304 |
|
---|
305 | _GSDM.documentMerge(oid, collection, mergeOID, lang, uid);
|
---|
306 | if(_GSDM.checkError(result, DOCUMENT_MERGE))
|
---|
307 | {
|
---|
308 | return result;
|
---|
309 | }
|
---|
310 | }
|
---|
311 |
|
---|
312 | return result;
|
---|
313 | }
|
---|
314 |
|
---|
315 | protected Element processDocumentSplit(Element request)
|
---|
316 | {
|
---|
317 | Element result = GSXML.createBasicResponse(this.doc, DOCUMENT_SPLIT);
|
---|
318 |
|
---|
319 | if (request == null)
|
---|
320 | {
|
---|
321 | GSXML.addError(this.doc, result, DOCUMENT_SPLIT + ": Request is null", GSXML.ERROR_TYPE_SYNTAX);
|
---|
322 | return result;
|
---|
323 | }
|
---|
324 |
|
---|
325 | String lang = request.getAttribute(GSXML.LANG_ATT);
|
---|
326 | String uid = request.getAttribute(GSXML.USER_ID_ATT);
|
---|
327 |
|
---|
328 | //Get the list of documents to duplicate
|
---|
329 | NodeList documents = request.getElementsByTagName(GSXML.DOCUMENT_ELEM);
|
---|
330 | for (int i = 0; i < documents.getLength(); i++)
|
---|
331 | {
|
---|
332 | Element currentDoc = (Element) documents.item(i);
|
---|
333 | String oid = currentDoc.getAttribute(GSXML.NODE_ID_ATT);
|
---|
334 | String collection = currentDoc.getAttribute(GSXML.COLLECTION_ATT);
|
---|
335 | String splitPoint = currentDoc.getAttribute("splitpoint");
|
---|
336 |
|
---|
337 | int split;
|
---|
338 | try
|
---|
339 | {
|
---|
340 | split = Integer.parseInt(splitPoint);
|
---|
341 | }
|
---|
342 | catch(Exception ex)
|
---|
343 | {
|
---|
344 | GSXML.addError(this.doc, result, DOCUMENT_SPLIT + ": The split point was not an integer", GSXML.ERROR_TYPE_SYNTAX);
|
---|
345 | return result;
|
---|
346 | }
|
---|
347 |
|
---|
348 | _GSDM.documentSplit(oid, collection, split, lang, uid);
|
---|
349 | if(_GSDM.checkError(result, DOCUMENT_SPLIT))
|
---|
350 | {
|
---|
351 | return result;
|
---|
352 | }
|
---|
353 | }
|
---|
354 |
|
---|
355 | return result;
|
---|
356 | }
|
---|
357 |
|
---|
358 | protected Element processDocumentExecuteTransaction(Element request)
|
---|
359 | {
|
---|
360 | return null;
|
---|
361 | }
|
---|
362 |
|
---|
363 | } |
---|