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.BufferedReader;
|
---|
25 | import java.io.InputStreamReader;
|
---|
26 | import java.io.OutputStream;
|
---|
27 | import java.lang.reflect.Type;
|
---|
28 | import java.util.ArrayList;
|
---|
29 | import java.util.HashMap;
|
---|
30 | import java.util.Iterator;
|
---|
31 | import java.util.List;
|
---|
32 | import java.util.Map;
|
---|
33 | import java.util.Set;
|
---|
34 |
|
---|
35 | import org.apache.log4j.*;
|
---|
36 |
|
---|
37 | import org.greenstone.gsdl3.util.GSDocumentModel;
|
---|
38 | import org.greenstone.gsdl3.util.GSPath;
|
---|
39 | import org.greenstone.gsdl3.util.GSXML;
|
---|
40 | import org.greenstone.gsdl3.util.UserContext;
|
---|
41 |
|
---|
42 | import org.w3c.dom.Element;
|
---|
43 | import org.w3c.dom.NodeList;
|
---|
44 |
|
---|
45 | import com.google.gson.Gson;
|
---|
46 | import com.google.gson.reflect.TypeToken;
|
---|
47 |
|
---|
48 | public class DocumentMaker extends ServiceRack
|
---|
49 | {
|
---|
50 | static Logger logger = Logger.getLogger(org.greenstone.gsdl3.service.ArchiveIO.class.getName());
|
---|
51 |
|
---|
52 | GSDocumentModel _GSDM = null;
|
---|
53 |
|
---|
54 | /****************************************************
|
---|
55 | * The list of services the Document Maker supports *
|
---|
56 | ***************************************************/
|
---|
57 | //Document/section services
|
---|
58 | protected static final String DOCUMENT_CREATE = "DocumentCreate";
|
---|
59 | protected static final String DOCUMENT_DELETE = "DocumentDelete";
|
---|
60 | protected static final String DOCUMENT_DUPLICATE = "DocumentDuplicate";
|
---|
61 | protected static final String DOCUMENT_MOVE = "DocumentMove";
|
---|
62 | protected static final String DOCUMENT_MERGE = "DocumentMerge";
|
---|
63 | protected static final String DOCUMENT_SPLIT = "DocumentSplit";
|
---|
64 | protected static final String DOCUMENT_GET_INFORMATION = "DocumentGetInformation";
|
---|
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_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 | UserContext userContext = new UserContext(request);
|
---|
127 |
|
---|
128 | //Get the list of documents to create
|
---|
129 | NodeList documents = request.getElementsByTagName(GSXML.DOCUMENT_ELEM);
|
---|
130 | for (int i = 0; i < documents.getLength(); i++)
|
---|
131 | {
|
---|
132 | //Get information about the current new document
|
---|
133 | Element currentDoc = (Element) documents.item(i);
|
---|
134 | String oid = currentDoc.getAttribute(GSXML.NODE_ID_ATT);
|
---|
135 | String collection = currentDoc.getAttribute(GSXML.COLLECTION_ATT);
|
---|
136 |
|
---|
137 | _GSDM.documentCreate(oid, collection, userContext);
|
---|
138 | if (_GSDM.checkError(result, DOCUMENT_CREATE))
|
---|
139 | {
|
---|
140 | return result;
|
---|
141 | }
|
---|
142 | }
|
---|
143 |
|
---|
144 | return result;
|
---|
145 | }
|
---|
146 |
|
---|
147 | protected Element processDocumentDelete(Element request)
|
---|
148 | {
|
---|
149 | Element result = GSXML.createBasicResponse(this.doc, DOCUMENT_DELETE);
|
---|
150 |
|
---|
151 | if (request == null)
|
---|
152 | {
|
---|
153 | GSXML.addError(this.doc, result, DOCUMENT_DELETE + ": Request is null", GSXML.ERROR_TYPE_SYNTAX);
|
---|
154 | return result;
|
---|
155 | }
|
---|
156 |
|
---|
157 | UserContext userContext = new UserContext(request);
|
---|
158 |
|
---|
159 | //Get the list of documents to delete
|
---|
160 | NodeList documents = request.getElementsByTagName(GSXML.DOCUMENT_ELEM);
|
---|
161 | for (int i = 0; i < documents.getLength(); i++)
|
---|
162 | {
|
---|
163 | Element currentDoc = (Element) documents.item(i);
|
---|
164 | String oid = currentDoc.getAttribute(GSXML.NODE_ID_ATT);
|
---|
165 | String collection = currentDoc.getAttribute(GSXML.COLLECTION_ATT);
|
---|
166 |
|
---|
167 | _GSDM.documentDelete(oid, collection, userContext);
|
---|
168 | if (_GSDM.checkError(result, DOCUMENT_DELETE))
|
---|
169 | {
|
---|
170 | return result;
|
---|
171 | }
|
---|
172 | }
|
---|
173 |
|
---|
174 | return result;
|
---|
175 | }
|
---|
176 |
|
---|
177 | protected Element processDocumentDuplicate(Element request)
|
---|
178 | {
|
---|
179 | Element result = GSXML.createBasicResponse(this.doc, DOCUMENT_DUPLICATE);
|
---|
180 |
|
---|
181 | if (request == null)
|
---|
182 | {
|
---|
183 | GSXML.addError(this.doc, result, DOCUMENT_DUPLICATE + ": Request is null", GSXML.ERROR_TYPE_SYNTAX);
|
---|
184 | return result;
|
---|
185 | }
|
---|
186 |
|
---|
187 | UserContext userContext = new UserContext(request);
|
---|
188 |
|
---|
189 | //Get the list of documents to duplicate
|
---|
190 | NodeList documents = request.getElementsByTagName(GSXML.DOCUMENT_ELEM);
|
---|
191 | for (int i = 0; i < documents.getLength(); i++)
|
---|
192 | {
|
---|
193 | Element currentDoc = (Element) documents.item(i);
|
---|
194 | String oid = currentDoc.getAttribute(GSXML.NODE_ID_ATT);
|
---|
195 | String collection = currentDoc.getAttribute(GSXML.COLLECTION_ATT);
|
---|
196 | String newOID = currentDoc.getAttribute("new" + GSXML.NODE_ID_ATT);
|
---|
197 | String newCollection = currentDoc.getAttribute("new" + GSXML.COLLECTION_ATT);
|
---|
198 | String operation = currentDoc.getAttribute("operation");
|
---|
199 |
|
---|
200 | _GSDM.documentMoveOrDuplicate(oid, collection, newOID, newCollection, _GSDM.operationStringToInt(operation), false, userContext);
|
---|
201 | if (_GSDM.checkError(result, DOCUMENT_DUPLICATE))
|
---|
202 | {
|
---|
203 | return result;
|
---|
204 | }
|
---|
205 | }
|
---|
206 |
|
---|
207 | return result;
|
---|
208 | }
|
---|
209 |
|
---|
210 | protected Element processDocumentGetInformation(Element request)
|
---|
211 | {
|
---|
212 | Element result = GSXML.createBasicResponse(this.doc, DOCUMENT_GET_INFORMATION);
|
---|
213 |
|
---|
214 | if (request == null)
|
---|
215 | {
|
---|
216 | GSXML.addError(this.doc, result, DOCUMENT_GET_INFORMATION + ": Request is null", GSXML.ERROR_TYPE_SYNTAX);
|
---|
217 | return result;
|
---|
218 | }
|
---|
219 |
|
---|
220 | UserContext userContext = new UserContext(request);
|
---|
221 |
|
---|
222 | //Get the list of documents to duplicate
|
---|
223 | NodeList documents = request.getElementsByTagName(GSXML.DOCUMENT_ELEM);
|
---|
224 | for (int i = 0; i < documents.getLength(); i++)
|
---|
225 | {
|
---|
226 | Element currentDoc = (Element) documents.item(i);
|
---|
227 | String oid = currentDoc.getAttribute(GSXML.NODE_ID_ATT);
|
---|
228 | String collection = currentDoc.getAttribute(GSXML.COLLECTION_ATT);
|
---|
229 |
|
---|
230 | NodeList requestedInfoList = currentDoc.getElementsByTagName("info"); //TODO: Replace info with a constant
|
---|
231 | String[] requestedInfo = new String[requestedInfoList.getLength()];
|
---|
232 |
|
---|
233 | for (int j = 0; j < requestedInfoList.getLength(); j++)
|
---|
234 | {
|
---|
235 | requestedInfo[j] = ((Element) requestedInfoList.item(j)).getAttribute(GSXML.NAME_ATT);
|
---|
236 | }
|
---|
237 |
|
---|
238 | _GSDM.documentGetInformation(oid, collection, requestedInfo, userContext);
|
---|
239 | if (_GSDM.checkError(result, DOCUMENT_GET_INFORMATION))
|
---|
240 | {
|
---|
241 | return result;
|
---|
242 | }
|
---|
243 | }
|
---|
244 |
|
---|
245 | return result;
|
---|
246 | }
|
---|
247 |
|
---|
248 | protected Element processDocumentMove(Element request)
|
---|
249 | {
|
---|
250 | Element result = GSXML.createBasicResponse(this.doc, DOCUMENT_MOVE);
|
---|
251 |
|
---|
252 | if (request == null)
|
---|
253 | {
|
---|
254 | GSXML.addError(this.doc, result, DOCUMENT_MOVE + ": Request is null", GSXML.ERROR_TYPE_SYNTAX);
|
---|
255 | return result;
|
---|
256 | }
|
---|
257 |
|
---|
258 | UserContext userContext = new UserContext(request);
|
---|
259 |
|
---|
260 | //Get the list of documents to duplicate
|
---|
261 | NodeList documents = request.getElementsByTagName(GSXML.DOCUMENT_ELEM);
|
---|
262 | for (int i = 0; i < documents.getLength(); i++)
|
---|
263 | {
|
---|
264 | Element currentDoc = (Element) documents.item(i);
|
---|
265 | String oid = currentDoc.getAttribute(GSXML.NODE_ID_ATT);
|
---|
266 | String collection = currentDoc.getAttribute(GSXML.COLLECTION_ATT);
|
---|
267 | String newOID = currentDoc.getAttribute("new" + GSXML.NODE_ID_ATT);
|
---|
268 | String newCollection = currentDoc.getAttribute("new" + GSXML.COLLECTION_ATT);
|
---|
269 | String operation = currentDoc.getAttribute("operation");
|
---|
270 |
|
---|
271 | _GSDM.documentMoveOrDuplicate(oid, collection, newOID, newCollection, _GSDM.operationStringToInt(operation), true, userContext);
|
---|
272 | if (_GSDM.checkError(result, DOCUMENT_MOVE))
|
---|
273 | {
|
---|
274 | return result;
|
---|
275 | }
|
---|
276 | }
|
---|
277 | return result;
|
---|
278 | }
|
---|
279 |
|
---|
280 | protected Element processDocumentMerge(Element request)
|
---|
281 | {
|
---|
282 | Element result = GSXML.createBasicResponse(this.doc, DOCUMENT_MERGE);
|
---|
283 |
|
---|
284 | if (request == null)
|
---|
285 | {
|
---|
286 | GSXML.addError(this.doc, result, DOCUMENT_MERGE + ": Request is null", GSXML.ERROR_TYPE_SYNTAX);
|
---|
287 | return result;
|
---|
288 | }
|
---|
289 |
|
---|
290 | UserContext userContext = new UserContext(request);
|
---|
291 |
|
---|
292 | //Get the list of documents to duplicate
|
---|
293 | NodeList documents = request.getElementsByTagName(GSXML.DOCUMENT_ELEM);
|
---|
294 | for (int i = 0; i < documents.getLength(); i++)
|
---|
295 | {
|
---|
296 | Element currentDoc = (Element) documents.item(i);
|
---|
297 | String oid = currentDoc.getAttribute(GSXML.NODE_ID_ATT);
|
---|
298 | String collection = currentDoc.getAttribute(GSXML.COLLECTION_ATT);
|
---|
299 | String mergeOID = currentDoc.getAttribute("merge" + GSXML.NODE_ID_ATT);
|
---|
300 |
|
---|
301 | _GSDM.documentMerge(oid, collection, mergeOID, userContext);
|
---|
302 | if (_GSDM.checkError(result, DOCUMENT_MERGE))
|
---|
303 | {
|
---|
304 | return result;
|
---|
305 | }
|
---|
306 | }
|
---|
307 |
|
---|
308 | return result;
|
---|
309 | }
|
---|
310 |
|
---|
311 | protected Element processDocumentSplit(Element request)
|
---|
312 | {
|
---|
313 | Element result = GSXML.createBasicResponse(this.doc, DOCUMENT_SPLIT);
|
---|
314 |
|
---|
315 | if (request == null)
|
---|
316 | {
|
---|
317 | GSXML.addError(this.doc, result, DOCUMENT_SPLIT + ": Request is null", GSXML.ERROR_TYPE_SYNTAX);
|
---|
318 | return result;
|
---|
319 | }
|
---|
320 |
|
---|
321 | UserContext userContext = new UserContext(request);
|
---|
322 |
|
---|
323 | //Get the list of documents to duplicate
|
---|
324 | NodeList documents = request.getElementsByTagName(GSXML.DOCUMENT_ELEM);
|
---|
325 | for (int i = 0; i < documents.getLength(); i++)
|
---|
326 | {
|
---|
327 | Element currentDoc = (Element) documents.item(i);
|
---|
328 | String oid = currentDoc.getAttribute(GSXML.NODE_ID_ATT);
|
---|
329 | String collection = currentDoc.getAttribute(GSXML.COLLECTION_ATT);
|
---|
330 | String splitPoint = currentDoc.getAttribute("splitpoint");
|
---|
331 |
|
---|
332 | int split;
|
---|
333 | try
|
---|
334 | {
|
---|
335 | split = Integer.parseInt(splitPoint);
|
---|
336 | }
|
---|
337 | catch (Exception ex)
|
---|
338 | {
|
---|
339 | GSXML.addError(this.doc, result, DOCUMENT_SPLIT + ": The split point was not an integer", GSXML.ERROR_TYPE_SYNTAX);
|
---|
340 | return result;
|
---|
341 | }
|
---|
342 |
|
---|
343 | _GSDM.documentSplit(oid, collection, split, userContext);
|
---|
344 | if (_GSDM.checkError(result, DOCUMENT_SPLIT))
|
---|
345 | {
|
---|
346 | return result;
|
---|
347 | }
|
---|
348 | }
|
---|
349 |
|
---|
350 | return result;
|
---|
351 | }
|
---|
352 |
|
---|
353 | protected Element processDocumentExecuteTransaction(Element request)
|
---|
354 | {
|
---|
355 | Element result = GSXML.createBasicResponse(this.doc, DOCUMENT_EXECUTE_TRANSACTION);
|
---|
356 |
|
---|
357 | if (request == null)
|
---|
358 | {
|
---|
359 | GSXML.addError(this.doc, result, DOCUMENT_EXECUTE_TRANSACTION + ": Request is null", GSXML.ERROR_TYPE_SYNTAX);
|
---|
360 | return result;
|
---|
361 | }
|
---|
362 |
|
---|
363 | UserContext userContext = new UserContext(request);
|
---|
364 |
|
---|
365 | Element param_list = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
|
---|
366 | if (param_list == null)
|
---|
367 | {
|
---|
368 | GSXML.addError(this.doc, result, DOCUMENT_EXECUTE_TRANSACTION + ": Request has no parameter list", GSXML.ERROR_TYPE_SYNTAX);
|
---|
369 | return result;
|
---|
370 | }
|
---|
371 |
|
---|
372 | HashMap params = GSXML.extractParams(param_list, false);
|
---|
373 | String transactionString = (String) params.get("transactions");
|
---|
374 | transactionString = transactionString.replace("%26", "&");
|
---|
375 |
|
---|
376 | List<Map<String, String>> transactions = null;
|
---|
377 | try
|
---|
378 | {
|
---|
379 | Gson gson = new Gson();
|
---|
380 | Type type = new TypeToken<List<Map<String, String>>>()
|
---|
381 | {
|
---|
382 | }.getType();
|
---|
383 | transactions = gson.fromJson(transactionString, type);
|
---|
384 | }
|
---|
385 | catch(Exception ex)
|
---|
386 | {
|
---|
387 | ex.printStackTrace();
|
---|
388 | }
|
---|
389 |
|
---|
390 | ArrayList<String> collectionsToBuild = new ArrayList<String>();
|
---|
391 | if (transactions != null && transactions.size() > 0)
|
---|
392 | {
|
---|
393 | for (int j = 0; j < transactions.size(); j++)
|
---|
394 | {
|
---|
395 | Map keyValueMap = (Map) transactions.get(j);
|
---|
396 | String operation = (String) keyValueMap.get("operation");
|
---|
397 | if (operation.equals("move") || operation.equals("duplicate"))
|
---|
398 | {
|
---|
399 | String origCollection = (String) keyValueMap.get("collection");
|
---|
400 | String origOID = (String) keyValueMap.get("oid");
|
---|
401 | String newCollection = (String) keyValueMap.get("newCollection");
|
---|
402 | String newOID = (String) keyValueMap.get("newOID");
|
---|
403 | String subOperation = (String) keyValueMap.get("subOperation");
|
---|
404 |
|
---|
405 | _GSDM.documentMoveOrDuplicate(origOID, origCollection, newOID, newCollection, _GSDM.operationStringToInt(subOperation), operation.equals("move"), userContext);
|
---|
406 |
|
---|
407 | if (_GSDM.getErrorStatus() == GSDocumentModel.NO_ERROR && origCollection != null && !collectionsToBuild.contains(origCollection))
|
---|
408 | {
|
---|
409 | collectionsToBuild.add(origCollection);
|
---|
410 | }
|
---|
411 |
|
---|
412 | if (_GSDM.getErrorStatus() == GSDocumentModel.NO_ERROR && newCollection != null && !collectionsToBuild.contains(newCollection))
|
---|
413 | {
|
---|
414 | collectionsToBuild.add(newCollection);
|
---|
415 | }
|
---|
416 | }
|
---|
417 | else if (operation.equals("create"))
|
---|
418 | {
|
---|
419 | String oid = (String) keyValueMap.get("oid");
|
---|
420 | String collection = (String) keyValueMap.get("collection");
|
---|
421 | String subOperation = (String) keyValueMap.get("subOperation");
|
---|
422 |
|
---|
423 | //_GSDM.documentCreate(oid, collection, userContext); <--- Maybe go back to this
|
---|
424 | _GSDM.documentXMLSetSection(oid, collection, this.doc.createElement(GSXML.DOCXML_SECTION_ELEM), _GSDM.operationStringToInt(subOperation), userContext);
|
---|
425 |
|
---|
426 | if (_GSDM.getErrorStatus() == GSDocumentModel.NO_ERROR && collection != null && !collectionsToBuild.contains(collection))
|
---|
427 | {
|
---|
428 | collectionsToBuild.add(collection);
|
---|
429 | }
|
---|
430 | }
|
---|
431 | else if (operation.equals("delete"))
|
---|
432 | {
|
---|
433 | String oid = (String) keyValueMap.get("oid");
|
---|
434 | String collection = (String) keyValueMap.get("collection");
|
---|
435 |
|
---|
436 | _GSDM.documentDelete(oid, collection, userContext);
|
---|
437 |
|
---|
438 | if (_GSDM.getErrorStatus() == GSDocumentModel.NO_ERROR && collection != null && !collectionsToBuild.contains(collection))
|
---|
439 | {
|
---|
440 | collectionsToBuild.add(collection);
|
---|
441 | }
|
---|
442 | }
|
---|
443 | else if (operation.equals("setText"))
|
---|
444 | {
|
---|
445 | String oid = (String) keyValueMap.get("oid");
|
---|
446 | String collection = (String) keyValueMap.get("collection");
|
---|
447 | String newContent = (String) keyValueMap.get("text");
|
---|
448 |
|
---|
449 | _GSDM.documentXMLSetText(oid, collection, newContent, userContext);
|
---|
450 | }
|
---|
451 |
|
---|
452 | if (_GSDM.checkError(result, DOCUMENT_EXECUTE_TRANSACTION))
|
---|
453 | {
|
---|
454 | return result;
|
---|
455 | }
|
---|
456 | }
|
---|
457 | }
|
---|
458 | return result;
|
---|
459 | }
|
---|
460 | } |
---|