source: main/trunk/greenstone3/src/java/org/greenstone/gsdl3/service/DocumentMaker.java@ 29318

Last change on this file since 29318 was 28966, checked in by kjdon, 10 years ago

Lots of changes. Mainly to do with removing this.doc from everywhere. Document is not thread safe. Now we tend to create a new Document everytime we are starting a new page/message etc. in service this.desc_doc is available as teh document to create service info stuff. But it should only be used for this and not for other messages. newDOM is now static for XMLConverter. method param changes for some GSXML methods.

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