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

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

Some more document maker updates

  • Property svn:executable set to *
File size: 15.6 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.lang.reflect.Type;
28import java.util.ArrayList;
29import java.util.HashMap;
30import java.util.Iterator;
31import java.util.List;
32import java.util.Map;
33import java.util.Set;
34
35import org.apache.log4j.*;
36
37import org.greenstone.gsdl3.util.GSDocumentModel;
38import org.greenstone.gsdl3.util.GSPath;
39import org.greenstone.gsdl3.util.GSXML;
40
41import org.w3c.dom.Element;
42import org.w3c.dom.NodeList;
43
44import com.google.gson.Gson;
45import com.google.gson.reflect.TypeToken;
46
47public class DocumentMaker extends ServiceRack
48{
49 static Logger logger = Logger.getLogger(org.greenstone.gsdl3.service.ArchiveIO.class.getName());
50
51 GSDocumentModel _GSDM = null;
52
53 /****************************************************
54 * The list of services the Document Maker supports *
55 ***************************************************/
56 //Document/section services
57 protected static final String DOCUMENT_CREATE = "DocumentCreate";
58 protected static final String DOCUMENT_DELETE = "DocumentDelete";
59 protected static final String DOCUMENT_DUPLICATE = "DocumentDuplicate";
60 protected static final String DOCUMENT_MOVE = "DocumentMove";
61 protected static final String DOCUMENT_MERGE = "DocumentMerge";
62 protected static final String DOCUMENT_SPLIT = "DocumentSplit";
63 protected static final String DOCUMENT_GET_INFORMATION = "DocumentGetInformation";
64
65 //Other services
66 protected static final String DOCUMENT_EXECUTE_TRANSACTION = "DocumentExecuteTransaction";
67 /***************************************************/
68
69 String[] services = { DOCUMENT_CREATE, DOCUMENT_DELETE, DOCUMENT_DUPLICATE, DOCUMENT_GET_INFORMATION, DOCUMENT_MOVE, DOCUMENT_MERGE, DOCUMENT_SPLIT, DOCUMENT_EXECUTE_TRANSACTION };
70
71 /** configure this service */
72 public boolean configure(Element info, Element extra_info)
73 {
74 if (!super.configure(info, extra_info))
75 {
76 return false;
77 }
78
79 logger.info("Configuring DocumentMaker...");
80 this.config_info = info;
81
82 for (int i = 0; i < services.length; i++)
83 {
84 Element service = this.doc.createElement(GSXML.SERVICE_ELEM);
85 service.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_RETRIEVE);
86 service.setAttribute(GSXML.NAME_ATT, services[i]);
87 this.short_service_info.appendChild(service);
88 }
89
90 _GSDM = new GSDocumentModel(this.site_home, this.doc, this.router);
91
92 return true;
93 }
94
95 protected Element getServiceDescription(String service_id, String lang, String subset)
96 {
97 for (int i = 0; i < services.length; i++)
98 {
99 if (service_id.equals(services[i]))
100 {
101 Element service_elem = this.doc.createElement(GSXML.SERVICE_ELEM);
102 service_elem.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_RETRIEVE);
103 service_elem.setAttribute(GSXML.NAME_ATT, services[i]);
104 return service_elem;
105 }
106 }
107
108 return null;
109 }
110
111 /************
112 * Services *
113 ***********/
114
115 protected Element processDocumentCreate(Element request)
116 {
117 Element result = GSXML.createBasicResponse(this.doc, DOCUMENT_CREATE);
118
119 if (request == null)
120 {
121 GSXML.addError(this.doc, result, DOCUMENT_CREATE + ": Request is null", GSXML.ERROR_TYPE_SYNTAX);
122 return result;
123 }
124
125 String lang = request.getAttribute(GSXML.LANG_ATT);
126 String uid = request.getAttribute(GSXML.USER_ID_ATT);
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, lang, uid);
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 String lang = request.getAttribute(GSXML.LANG_ATT);
158 String uid = request.getAttribute(GSXML.USER_ID_ATT);
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, lang, uid);
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 String lang = request.getAttribute(GSXML.LANG_ATT);
189 String uid = request.getAttribute(GSXML.USER_ID_ATT);
190
191 //Get the list of documents to duplicate
192 NodeList documents = request.getElementsByTagName(GSXML.DOCUMENT_ELEM);
193 for (int i = 0; i < documents.getLength(); i++)
194 {
195 Element currentDoc = (Element) documents.item(i);
196 String oid = currentDoc.getAttribute(GSXML.NODE_ID_ATT);
197 String collection = currentDoc.getAttribute(GSXML.COLLECTION_ATT);
198 String newOID = currentDoc.getAttribute("new" + GSXML.NODE_ID_ATT);
199 String newCollection = currentDoc.getAttribute("new" + GSXML.COLLECTION_ATT);
200 String operation = currentDoc.getAttribute("operation");
201
202 _GSDM.documentMoveOrDuplicate(oid, collection, newOID, newCollection, _GSDM.operationStringToInt(operation), false, 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 String operation = currentDoc.getAttribute("operation");
274
275 _GSDM.documentMoveOrDuplicate(oid, collection, newOID, newCollection, _GSDM.operationStringToInt(operation), true, lang, uid);
276 if (_GSDM.checkError(result, DOCUMENT_MOVE))
277 {
278 return result;
279 }
280 }
281 return result;
282 }
283
284 protected Element processDocumentMerge(Element request)
285 {
286 Element result = GSXML.createBasicResponse(this.doc, DOCUMENT_MERGE);
287
288 if (request == null)
289 {
290 GSXML.addError(this.doc, result, DOCUMENT_MERGE + ": Request is null", GSXML.ERROR_TYPE_SYNTAX);
291 return result;
292 }
293
294 String lang = request.getAttribute(GSXML.LANG_ATT);
295 String uid = request.getAttribute(GSXML.USER_ID_ATT);
296
297 //Get the list of documents to duplicate
298 NodeList documents = request.getElementsByTagName(GSXML.DOCUMENT_ELEM);
299 for (int i = 0; i < documents.getLength(); i++)
300 {
301 Element currentDoc = (Element) documents.item(i);
302 String oid = currentDoc.getAttribute(GSXML.NODE_ID_ATT);
303 String collection = currentDoc.getAttribute(GSXML.COLLECTION_ATT);
304 String mergeOID = currentDoc.getAttribute("merge" + GSXML.NODE_ID_ATT);
305
306 _GSDM.documentMerge(oid, collection, mergeOID, lang, uid);
307 if (_GSDM.checkError(result, DOCUMENT_MERGE))
308 {
309 return result;
310 }
311 }
312
313 return result;
314 }
315
316 protected Element processDocumentSplit(Element request)
317 {
318 Element result = GSXML.createBasicResponse(this.doc, DOCUMENT_SPLIT);
319
320 if (request == null)
321 {
322 GSXML.addError(this.doc, result, DOCUMENT_SPLIT + ": Request is null", GSXML.ERROR_TYPE_SYNTAX);
323 return result;
324 }
325
326 String lang = request.getAttribute(GSXML.LANG_ATT);
327 String uid = request.getAttribute(GSXML.USER_ID_ATT);
328
329 //Get the list of documents to duplicate
330 NodeList documents = request.getElementsByTagName(GSXML.DOCUMENT_ELEM);
331 for (int i = 0; i < documents.getLength(); i++)
332 {
333 Element currentDoc = (Element) documents.item(i);
334 String oid = currentDoc.getAttribute(GSXML.NODE_ID_ATT);
335 String collection = currentDoc.getAttribute(GSXML.COLLECTION_ATT);
336 String splitPoint = currentDoc.getAttribute("splitpoint");
337
338 int split;
339 try
340 {
341 split = Integer.parseInt(splitPoint);
342 }
343 catch (Exception ex)
344 {
345 GSXML.addError(this.doc, result, DOCUMENT_SPLIT + ": The split point was not an integer", GSXML.ERROR_TYPE_SYNTAX);
346 return result;
347 }
348
349 _GSDM.documentSplit(oid, collection, split, lang, uid);
350 if (_GSDM.checkError(result, DOCUMENT_SPLIT))
351 {
352 return result;
353 }
354 }
355
356 return result;
357 }
358
359 protected Element processDocumentExecuteTransaction(Element request)
360 {
361 Element result = GSXML.createBasicResponse(this.doc, DOCUMENT_EXECUTE_TRANSACTION);
362
363 if (request == null)
364 {
365 GSXML.addError(this.doc, result, DOCUMENT_EXECUTE_TRANSACTION + ": Request is null", GSXML.ERROR_TYPE_SYNTAX);
366 return result;
367 }
368
369 String lang = request.getAttribute(GSXML.LANG_ATT);
370 String uid = request.getAttribute(GSXML.USER_ID_ATT);
371
372 Element param_list = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
373 if (param_list == null)
374 {
375 GSXML.addError(this.doc, result, DOCUMENT_EXECUTE_TRANSACTION + ": Request has no parameter list", GSXML.ERROR_TYPE_SYNTAX);
376 return result;
377 }
378
379 HashMap params = GSXML.extractParams(param_list, false);
380 String transactionString = (String) params.get("transactions");
381
382 Gson gson = new Gson();
383 Type type = new TypeToken<List<Map<String, String>>>()
384 {
385 }.getType();
386 List<Map<String, String>> transactions = gson.fromJson(transactionString, type);
387
388 ArrayList<String> collectionsToBuild = new ArrayList<String>();
389 if (transactions != null && transactions.size() > 0)
390 {
391 for (int j = 0; j < transactions.size(); j++)
392 {
393 Map keyValueMap = (Map) transactions.get(j);
394 String operation = (String) keyValueMap.get("operation");
395 if (operation.equals("move") || operation.equals("duplicate"))
396 {
397 String origCollection = (String) keyValueMap.get("collection");
398 String origOID = (String) keyValueMap.get("oid");
399 String newCollection = (String) keyValueMap.get("newCollection");
400 String newOID = (String) keyValueMap.get("newOID");
401 String subOperation = (String) keyValueMap.get("subOperation");
402
403 _GSDM.documentMoveOrDuplicate(origOID, origCollection, newOID, newCollection, _GSDM.operationStringToInt(subOperation), operation.equals("move"), lang, uid);
404
405 if (_GSDM.getErrorStatus() == GSDocumentModel.NO_ERROR && origCollection != null && !collectionsToBuild.contains(origCollection))
406 {
407 collectionsToBuild.add(origCollection);
408 }
409
410 if (_GSDM.getErrorStatus() == GSDocumentModel.NO_ERROR && newCollection != null && !collectionsToBuild.contains(newCollection))
411 {
412 collectionsToBuild.add(newCollection);
413 }
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, lang, uid); <--- Maybe go back to this
422 _GSDM.documentXMLSetSection(oid, collection, this.doc.createElement(GSXML.DOCXML_SECTION_ELEM), _GSDM.operationStringToInt(subOperation), lang, uid);
423
424 if (_GSDM.getErrorStatus() == GSDocumentModel.NO_ERROR && collection != null && !collectionsToBuild.contains(collection))
425 {
426 collectionsToBuild.add(collection);
427 }
428 }
429 else if (operation.equals("delete"))
430 {
431 String oid = (String) keyValueMap.get("oid");
432 String collection = (String) keyValueMap.get("collection");
433
434 _GSDM.documentDelete(oid, collection, lang, uid);
435
436 if (_GSDM.getErrorStatus() == GSDocumentModel.NO_ERROR && collection != null && !collectionsToBuild.contains(collection))
437 {
438 collectionsToBuild.add(collection);
439 }
440 }
441 else if (operation.equals("setText"))
442 {
443 String oid = (String) keyValueMap.get("oid");
444 String collection = (String) keyValueMap.get("collection");
445 String newContent = (String) keyValueMap.get("text");
446
447 _GSDM.documentXMLSetText(oid, collection, newContent, lang, uid);
448
449 System.err.println(newContent);
450 }
451
452 if (_GSDM.checkError(result, DOCUMENT_EXECUTE_TRANSACTION))
453 {
454 return result;
455 }
456 }
457 }
458 return result;
459 }
460}
Note: See TracBrowser for help on using the repository browser.