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

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