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

Last change on this file since 24427 was 24427, checked in by sjm84, 13 years ago

More tidying

  • Property svn:executable set to *
File size: 11.4 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.File;
25import java.io.FileInputStream;
26import java.io.FileOutputStream;
27import java.nio.channels.FileChannel;
28import java.util.HashMap;
29
30import org.apache.log4j.*;
31import org.apache.tools.ant.util.FileUtils;
32
33import org.greenstone.gsdl3.util.GSDocumentModel;
34import org.greenstone.gsdl3.util.GSXML;
35
36import org.w3c.dom.Element;
37import org.w3c.dom.NodeList;
38
39public 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 //Other services
58 protected static final String DOCUMENT_EXECUTE_TRANSACTION = "DocumentExecuteTransaction";
59 /***************************************************/
60
61 String[] services = { DOCUMENT_CREATE, DOCUMENT_DELETE, DOCUMENT_DUPLICATE, DOCUMENT_GET_INFORMATION, DOCUMENT_MOVE, DOCUMENT_MERGE, DOCUMENT_SPLIT, DOCUMENT_EXECUTE_TRANSACTION };
62
63 /** configure this service */
64 public boolean configure(Element info, Element extra_info)
65 {
66 if (!super.configure(info, extra_info))
67 {
68 return false;
69 }
70
71 logger.info("Configuring DocumentMaker...");
72 this.config_info = info;
73
74 for (int i = 0; i < services.length; i++)
75 {
76 Element service = this.doc.createElement(GSXML.SERVICE_ELEM);
77 service.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_RETRIEVE);
78 service.setAttribute(GSXML.NAME_ATT, services[i]);
79 this.short_service_info.appendChild(service);
80 }
81
82 _GSDM = new GSDocumentModel(this.site_home, this.doc, this.router);
83
84 return true;
85 }
86
87 protected Element getServiceDescription(String service_id, String lang, String subset)
88 {
89 for (int i = 0; i < services.length; i++)
90 {
91 if (service_id.equals(services[i]))
92 {
93 Element service_elem = this.doc.createElement(GSXML.SERVICE_ELEM);
94 service_elem.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_RETRIEVE);
95 service_elem.setAttribute(GSXML.NAME_ATT, services[i]);
96 return service_elem;
97 }
98 }
99
100 return null;
101 }
102
103 /************
104 * Services *
105 ***********/
106
107 protected Element processDocumentCreate(Element request)
108 {
109 Element result = GSXML.createBasicResponse(this.doc, DOCUMENT_CREATE);
110
111 if (request == null)
112 {
113 GSXML.addError(this.doc, result, DOCUMENT_CREATE + ": Request is null", GSXML.ERROR_TYPE_SYNTAX);
114 return result;
115 }
116
117 String lang = request.getAttribute(GSXML.LANG_ATT);
118 String uid = request.getAttribute(GSXML.USER_ID_ATT);
119
120 //Get the list of documents to create
121 NodeList documents = request.getElementsByTagName(GSXML.DOCUMENT_ELEM);
122 for (int i = 0; i < documents.getLength(); i++)
123 {
124 //Get information about the current new document
125 Element currentDoc = (Element) documents.item(i);
126 String oid = currentDoc.getAttribute(GSXML.NODE_ID_ATT);
127 String collection = currentDoc.getAttribute(GSXML.COLLECTION_ATT);
128
129 _GSDM.documentCreate(oid, collection, lang, uid);
130 if (_GSDM.checkError(result, DOCUMENT_CREATE))
131 {
132 return result;
133 }
134 }
135
136 return result;
137 }
138
139 protected Element processDocumentDelete(Element request)
140 {
141 Element result = GSXML.createBasicResponse(this.doc, DOCUMENT_DELETE);
142
143 if (request == null)
144 {
145 GSXML.addError(this.doc, result, DOCUMENT_DELETE + ": Request is null", GSXML.ERROR_TYPE_SYNTAX);
146 return result;
147 }
148
149 String lang = request.getAttribute(GSXML.LANG_ATT);
150 String uid = request.getAttribute(GSXML.USER_ID_ATT);
151
152 //Get the list of documents to delete
153 NodeList documents = request.getElementsByTagName(GSXML.DOCUMENT_ELEM);
154 for (int i = 0; i < documents.getLength(); i++)
155 {
156 Element currentDoc = (Element) documents.item(i);
157 String oid = currentDoc.getAttribute(GSXML.NODE_ID_ATT);
158 String collection = currentDoc.getAttribute(GSXML.COLLECTION_ATT);
159
160 _GSDM.documentDelete(oid, collection, lang, uid);
161 if (_GSDM.checkError(result, DOCUMENT_DELETE))
162 {
163 return result;
164 }
165 }
166
167 return result;
168 }
169
170 protected Element processDocumentDuplicate(Element request)
171 {
172 Element result = GSXML.createBasicResponse(this.doc, DOCUMENT_DUPLICATE);
173
174 if (request == null)
175 {
176 GSXML.addError(this.doc, result, DOCUMENT_DUPLICATE + ": Request is null", GSXML.ERROR_TYPE_SYNTAX);
177 return result;
178 }
179
180 String lang = request.getAttribute(GSXML.LANG_ATT);
181 String uid = request.getAttribute(GSXML.USER_ID_ATT);
182
183 //Get the list of documents to duplicate
184 NodeList documents = request.getElementsByTagName(GSXML.DOCUMENT_ELEM);
185 for (int i = 0; i < documents.getLength(); i++)
186 {
187 Element currentDoc = (Element) documents.item(i);
188 String oid = currentDoc.getAttribute(GSXML.NODE_ID_ATT);
189 String collection = currentDoc.getAttribute(GSXML.COLLECTION_ATT);
190 String newOID = currentDoc.getAttribute("new" + GSXML.NODE_ID_ATT);
191 String newCollection = currentDoc.getAttribute("new" + GSXML.COLLECTION_ATT);
192
193 _GSDM.documentDuplicate(oid, collection, newOID, newCollection, lang, uid);
194 if (_GSDM.checkError(result, DOCUMENT_DUPLICATE))
195 {
196 return result;
197 }
198 }
199
200 return result;
201 }
202
203 protected Element processDocumentGetInformation(Element request)
204 {
205 Element result = GSXML.createBasicResponse(this.doc, DOCUMENT_GET_INFORMATION);
206
207 if (request == null)
208 {
209 GSXML.addError(this.doc, result, DOCUMENT_GET_INFORMATION + ": Request is null", GSXML.ERROR_TYPE_SYNTAX);
210 return result;
211 }
212
213 String lang = request.getAttribute(GSXML.LANG_ATT);
214 String uid = request.getAttribute(GSXML.USER_ID_ATT);
215
216 //Get the list of documents to duplicate
217 NodeList documents = request.getElementsByTagName(GSXML.DOCUMENT_ELEM);
218 for (int i = 0; i < documents.getLength(); i++)
219 {
220 Element currentDoc = (Element) documents.item(i);
221 String oid = currentDoc.getAttribute(GSXML.NODE_ID_ATT);
222 String collection = currentDoc.getAttribute(GSXML.COLLECTION_ATT);
223
224 NodeList requestedInfoList = currentDoc.getElementsByTagName("info"); //TODO: Replace info with a constant
225 String[] requestedInfo = new String[requestedInfoList.getLength()];
226
227 for(int j = 0; j < requestedInfoList.getLength(); j++)
228 {
229 requestedInfo[j] = ((Element) requestedInfoList.item(j)).getAttribute(GSXML.NAME_ATT);
230 }
231
232 _GSDM.documentGetInformation(oid, collection, requestedInfo, lang, uid);
233 if (_GSDM.checkError(result, DOCUMENT_GET_INFORMATION))
234 {
235 return result;
236 }
237 }
238
239 return result;
240 }
241
242 protected Element processDocumentMove(Element request)
243 {
244 Element result = GSXML.createBasicResponse(this.doc, DOCUMENT_MOVE);
245
246 if (request == null)
247 {
248 GSXML.addError(this.doc, result, DOCUMENT_MOVE + ": Request is null", GSXML.ERROR_TYPE_SYNTAX);
249 return result;
250 }
251
252 String lang = request.getAttribute(GSXML.LANG_ATT);
253 String uid = request.getAttribute(GSXML.USER_ID_ATT);
254
255 //Get the list of documents to duplicate
256 NodeList documents = request.getElementsByTagName(GSXML.DOCUMENT_ELEM);
257 for (int i = 0; i < documents.getLength(); i++)
258 {
259 Element currentDoc = (Element) documents.item(i);
260 String oid = currentDoc.getAttribute(GSXML.NODE_ID_ATT);
261 String collection = currentDoc.getAttribute(GSXML.COLLECTION_ATT);
262 String newOID = currentDoc.getAttribute("new" + GSXML.NODE_ID_ATT);
263 String newCollection = currentDoc.getAttribute("new" + GSXML.COLLECTION_ATT);
264
265 _GSDM.documentMove(oid, collection, newOID, newCollection, lang, uid);
266 if (_GSDM.checkError(result, DOCUMENT_MOVE))
267 {
268 return result;
269 }
270 }
271 return result;
272 }
273
274 protected Element processDocumentMerge(Element request)
275 {
276 Element result = GSXML.createBasicResponse(this.doc, DOCUMENT_MERGE);
277
278 if (request == null)
279 {
280 GSXML.addError(this.doc, result, DOCUMENT_MERGE + ": Request is null", GSXML.ERROR_TYPE_SYNTAX);
281 return result;
282 }
283
284 String lang = request.getAttribute(GSXML.LANG_ATT);
285 String uid = request.getAttribute(GSXML.USER_ID_ATT);
286
287 //Get the list of documents to duplicate
288 NodeList documents = request.getElementsByTagName(GSXML.DOCUMENT_ELEM);
289 for (int i = 0; i < documents.getLength(); i++)
290 {
291 Element currentDoc = (Element) documents.item(i);
292 String oid = currentDoc.getAttribute(GSXML.NODE_ID_ATT);
293 String collection = currentDoc.getAttribute(GSXML.COLLECTION_ATT);
294 String mergeOID = currentDoc.getAttribute("merge" + GSXML.NODE_ID_ATT);
295
296 _GSDM.documentMerge(oid, collection, mergeOID, lang, uid);
297 if(_GSDM.checkError(result, DOCUMENT_MERGE))
298 {
299 return result;
300 }
301 }
302
303 return result;
304 }
305
306 protected Element processDocumentSplit(Element request)
307 {
308 Element result = GSXML.createBasicResponse(this.doc, DOCUMENT_SPLIT);
309
310 if (request == null)
311 {
312 GSXML.addError(this.doc, result, DOCUMENT_SPLIT + ": Request is null", GSXML.ERROR_TYPE_SYNTAX);
313 return result;
314 }
315
316 String lang = request.getAttribute(GSXML.LANG_ATT);
317 String uid = request.getAttribute(GSXML.USER_ID_ATT);
318
319 //Get the list of documents to duplicate
320 NodeList documents = request.getElementsByTagName(GSXML.DOCUMENT_ELEM);
321 for (int i = 0; i < documents.getLength(); i++)
322 {
323 Element currentDoc = (Element) documents.item(i);
324 String oid = currentDoc.getAttribute(GSXML.NODE_ID_ATT);
325 String collection = currentDoc.getAttribute(GSXML.COLLECTION_ATT);
326 String splitPoint = currentDoc.getAttribute("splitpoint");
327
328 int split;
329 try
330 {
331 split = Integer.parseInt(splitPoint);
332 }
333 catch(Exception ex)
334 {
335 GSXML.addError(this.doc, result, DOCUMENT_SPLIT + ": The split point was not an integer", GSXML.ERROR_TYPE_SYNTAX);
336 return result;
337 }
338
339 _GSDM.documentSplit(oid, collection, split, lang, uid);
340 if(_GSDM.checkError(result, DOCUMENT_SPLIT))
341 {
342 return result;
343 }
344 }
345
346 return result;
347 }
348
349 protected Element processDocumentExecuteTransaction(Element request)
350 {
351 return null;
352 }
353}
Note: See TracBrowser for help on using the repository browser.