source: trunk/gsdl3/src/java/org/greenstone/gsdl3/service/GS2Construct.java@ 3868

Last change on this file since 3868 was 3868, checked in by kjdon, 21 years ago

relects GSXML changes

  • Property svn:keywords set to Author Date Id Revision
File size: 25.4 KB
Line 
1/*
2 * GS2Construct.java
3 * Copyright (C) 2002 New Zealand Digital Library, http://www.nzdl.org
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19package org.greenstone.gsdl3.service;
20
21import org.greenstone.gsdl3.util.*;
22import org.greenstone.gsdl3.build.*;
23
24import org.w3c.dom.Document;
25import org.w3c.dom.Node;
26import org.w3c.dom.Text;
27import org.w3c.dom.Element;
28import org.w3c.dom.NodeList;
29
30import java.util.Collections;
31import java.util.HashMap;
32import java.util.Map;
33import java.util.List;
34import java.util.ArrayList;
35import java.io.File;
36import java.util.Locale;
37
38/**
39 * A Services class for building collections
40 * provides a wrapper around the old perl scripts
41 *
42 * @author <a href="mailto:[email protected]">Katherine Don</a>
43 * @version $Revision: 3868 $
44 */
45public class GS2Construct
46 extends ServiceRack {
47
48 // services offered
49 private static final String NEW_SERVICE = "NewCollection";
50 private static final String ADD_DOC_SERVICE = "AddDocument";
51 private static final String IMPORT_SERVICE = "ImportCollection";
52 private static final String BUILD_SERVICE = "BuildCollection";
53 private static final String ACTIVATE_SERVICE = "ActivateCollection";
54 private static final String DELETE_SERVICE = "DeleteCollection";
55 private static final String RELOAD_SERVICE = "ReloadCollection";
56
57 // params used
58 private static final String COL_PARAM = "collection";
59 private static final String NEW_COL_TITLE_PARAM = "collTitle";
60 private static final String NEW_COL_ABOUT_PARAM = "collAbout";
61 private static final String CREATOR_PARAM = "creator";
62 private static final String NEW_FILE_PARAM = "newfile";
63 private static final String HANDLE_PARAM = "id";
64 private static final String BUILDTYPE_PARAM = "buildType";
65 private static final String BUILDTYPE_MG = "mg";
66 private static final String BUILDTYPE_MGPP = "mgpp";
67
68 // the list of the collections - store between some method calls
69 private String[] collection_list_ = null;
70
71 // set of listeners for any construction commands
72 protected Map listeners_ = null;
73
74 public GS2Construct() {
75 listeners_ = Collections.synchronizedMap(new HashMap());
76
77 }
78
79 /** returns the service list for the subclass */
80 protected Element getServiceList(String lang) {
81 // for now, it is static and there is no lang stuff
82 return short_service_info_;
83 }
84
85 /** returns a specific service description */
86 protected Element getServiceDescription(String service, String lang) {
87 Element descript = (Element)((Element)service_info_map_.get(service)).cloneNode(true);
88 // need to add in collection param list
89 if (!service.equals(NEW_SERVICE)) {//new doesn't need a collection list
90 Element param_list = (Element) GSXML.getChildByTagName(descript, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
91 Element old_param = GSXML.getNamedElement(param_list, GSXML.PARAM_ELEM, GSXML.NAME_ATT, COL_PARAM);
92 collection_list_ = getCollectionList();
93 Element param = GSXML.createParameter(doc_, COL_PARAM, GSXML.PARAM_TYPE_ENUM_SINGLE, null, collection_list_);
94 // put at front of the list
95 param_list.replaceChild(param, old_param);
96 }
97 // for now, create it on the fly - look at caching it later
98 Element display = createServiceDisplay(service, lang);
99 addServiceDisplay(descript, display);
100 return descript;
101
102 }
103
104 /** creates a display element containing all the text strings needed to display the service page, in the language specified */
105 protected Element createServiceDisplay(String service, String lang) {
106 Element display = doc_.createElement(GSXML.DISPLAY_ELEM);
107 display.appendChild(GSXML.createTextElement(doc_, GSXML.DISPLAY_NAME_ELEM, getTextString(service+".name", lang)));
108 display.appendChild(GSXML.createTextElement(doc_, GSXML.DISPLAY_SUBMIT_ELEM, getTextString(service+".submit", lang)));
109
110 Element param;
111 if (service.equals(NEW_SERVICE)) {
112 // have the coll title, about, buildtype and creator params
113 param = GSXML.createParameterDisplay(doc_, NEW_COL_TITLE_PARAM, getTextString("param."+NEW_COL_TITLE_PARAM, lang), null, null);
114 display.appendChild(param);
115 param = GSXML.createParameterDisplay(doc_, CREATOR_PARAM, getTextString("param."+CREATOR_PARAM, lang), null, null);
116 display.appendChild(param);
117 param = GSXML.createParameterDisplay(doc_, NEW_COL_ABOUT_PARAM, getTextString("param."+NEW_COL_ABOUT_PARAM, lang), null, null);
118 display.appendChild(param);
119
120 String[] types = {BUILDTYPE_MGPP, BUILDTYPE_MG};
121 String[] type_texts = {getTextString("param."+BUILDTYPE_PARAM+"."+BUILDTYPE_MGPP, lang), getTextString("param."+BUILDTYPE_PARAM+"."+BUILDTYPE_MG, lang)};
122 param = GSXML.createParameterDisplay(doc_, BUILDTYPE_PARAM, getTextString("param."+BUILDTYPE_PARAM, lang), types, type_texts);
123 display.appendChild(param);
124 } else {
125 // have just the coll param
126 //use the collection list already created - this method is only called directly after getServiceDescription()
127 param = GSXML.createParameterDisplay(doc_, COL_PARAM, getTextString("param."+COL_PARAM, lang), collection_list_, collection_list_);
128 display.appendChild(param);
129 if (service.equals(ADD_DOC_SERVICE)) {
130 // also have the file param
131 param = GSXML.createParameterDisplay(doc_, NEW_FILE_PARAM, getTextString("param."+NEW_FILE_PARAM, lang), null, null);
132 display.appendChild(param);
133 }
134 }
135
136 return display;
137
138 }
139
140 // each service must have a method "process<New service name>"
141
142 protected Element processNewCollection(Element request) {
143 return runCommand(request, CollectionConstructor.NEW);
144 }
145
146 /** TODO:implement this */
147 protected Element processAddDocument(Element request) {
148 // decode the file name, add it to the import directory
149 String name = GSPath.getFirstLink(request.getAttribute(GSXML.TO_ATT));
150 Element response = doc_.createElement(GSXML.RESPONSE_ELEM);
151 response.setAttribute(GSXML.FROM_ATT, name);
152 Element status = doc_.createElement(GSXML.STATUS_ELEM);
153 response.appendChild(status);
154 //String lang = request.getAttribute(GSXML.LANG_ATT);
155 //String request_type = request.getAttribute(GSXML.TYPE_ATT);
156 Text t = doc_.createTextNode("AddDocument: not implemented yet");
157 status.appendChild(t);
158 status.setAttribute(GSXML.STATUS_ERROR_CODE_ATT, Integer.toString(GSStatus.ERROR));
159 return response;
160 }
161 protected Element processImportCollection(Element request) {
162 return runCommand(request, CollectionConstructor.IMPORT);
163 }
164
165 protected Element processBuildCollection(Element request) {
166 return runCommand(request, CollectionConstructor.BUILD);
167 }
168 protected Element processActivateCollection(Element request) {
169 Element response = runCommand(request, CollectionConstructor.ACTIVATE);
170 // this activates the collection on disk. but now we need to tell
171 // the MR about it. but we have to wait until the process is finished.
172 Element status = (Element)GSXML.getChildByTagName(response, GSXML.STATUS_ELEM);
173 Element param_list = (Element)GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
174 HashMap params = GSXML.extractParams(param_list, false);
175 String coll_name = (String) params.get(COL_PARAM);
176 String lang = request.getAttribute(GSXML.LANG_ATT);
177 // check for finished
178 int status_code = Integer.parseInt(status.getAttribute(GSXML.STATUS_ERROR_CODE_ATT));
179 if (GSStatus.isCompleted(status_code) &&
180 GSStatus.isError(status_code)) {
181 // we shouldn't carry out the next bit, just return the response
182 return response;
183 }
184 String id = status.getAttribute(GSXML.STATUS_PROCESS_ID_ATT);
185 GS2PerlListener listener = (GS2PerlListener)listeners_.get(id);
186 if (listener==null) {
187 System.err.println("somethings gone wrong, couldn't find the listener");
188 status.setAttribute(GSXML.STATUS_ERROR_CODE_ATT, Integer.toString(GSStatus.ERROR));
189 return response;
190 }
191 while (!GSStatus.isCompleted(status_code)) {
192 // wait for the process, and keep checking the status code
193 // there is probably a better way to do this.
194 try {
195 Thread.currentThread().sleep(100);
196 } catch (Exception e) { // ignore
197 }
198 status_code = listener.getStatus();
199 }
200
201 // add the rest of the messages to the status node
202 Text t = doc_.createTextNode("\n"+listener.getUpdate());
203 status.appendChild(t);
204 status.setAttribute(GSXML.STATUS_ERROR_CODE_ATT, Integer.toString(listener.getStatus()));
205 if (GSStatus.isError(status_code)) {
206 return response; // without doing the next bit
207 }
208
209 t = doc_.createTextNode("\n");
210 status.appendChild(t);
211 // once have got here, we assume
212 // the first bit proceeded successfully, now reload the collection
213 configureRequest("reload", coll_name, status, lang); // this will append more messages to the status, and overwrite the error code att
214 return response;
215
216 }
217 protected Element processDeleteCollection(Element request) {
218
219 // the response to send back
220 String name = GSPath.getFirstLink(request.getAttribute(GSXML.TO_ATT));
221 Element response = doc_.createElement(GSXML.RESPONSE_ELEM);
222 response.setAttribute(GSXML.FROM_ATT, name);
223 Element status = doc_.createElement(GSXML.STATUS_ELEM);
224 response.appendChild(status);
225 Text t = null; // the text node for the error/success message
226 String lang = request.getAttribute(GSXML.LANG_ATT);
227 String request_type = request.getAttribute(GSXML.TYPE_ATT);
228
229 Element param_list = (Element)GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
230 HashMap params = GSXML.extractParams(param_list, false);
231
232 boolean get_status_only = false;
233 if (request_type.equals(GSXML.REQUEST_TYPE_STATUS)) {
234 get_status_only = true;
235 }
236 System.out.println("get status only = "+get_status_only);
237 if (get_status_only) {
238 // at the moment, delete is synchronous. but it may take ages so should do the command in another thread maybe? in which case we will want to ask for status
239 System.err.println("had a status request for delete - this shouldn't happen!!");
240 //t = doc_.createTextNode("");
241 //status.appendChild(t);
242 status.setAttribute(GSXML.STATUS_ERROR_CODE_ATT, Integer.toString(GSStatus.ERROR));
243 return response;
244 }
245 String coll_name = (String)params.get(COL_PARAM);
246 String[] args={coll_name};
247 File coll_dir = new File(GSFile.collectionBaseDir(site_home_, coll_name));
248 // check that the coll is there in the first place
249 if (!coll_dir.exists()) {
250 t = doc_.createTextNode(getTextString("delete.exists_error", args, lang));
251 status.appendChild(t);
252 status.setAttribute(GSXML.STATUS_ERROR_CODE_ATT, Integer.toString(GSStatus.ERROR));
253 return response;
254 }
255
256 // try to delete the directory
257 if (!GSFile.deleteFile(coll_dir)) {
258 t = doc_.createTextNode(getTextString("delete.delete_error", args, lang));
259 status.setAttribute(GSXML.STATUS_ERROR_CODE_ATT, Integer.toString(GSStatus.ERROR));
260 status.appendChild(t);
261 return response;
262 }
263 configureRequest("delete", coll_name, status, lang);
264 return response;
265 }
266
267 protected Element processReloadCollection(Element request) {
268
269 // the response to send back
270 String name = GSPath.getFirstLink(request.getAttribute(GSXML.TO_ATT));
271 Element response = doc_.createElement(GSXML.RESPONSE_ELEM);
272 response.setAttribute(GSXML.FROM_ATT, name);
273 Element status = doc_.createElement(GSXML.STATUS_ELEM);
274 response.appendChild(status);
275 Text t = null; // the text node for the error/success message
276
277 String lang = request.getAttribute(GSXML.LANG_ATT);
278 String request_type = request.getAttribute(GSXML.TYPE_ATT);
279
280 Element param_list = (Element)GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
281 HashMap params = GSXML.extractParams(param_list, false);
282
283 boolean get_status_only = false;
284 if (request_type.equals(GSXML.REQUEST_TYPE_STATUS)) {
285 get_status_only = true;
286 }
287 if (get_status_only) {
288 // reload is synchronous - this makes no sense
289 System.err.println("had a status request for reload - this shouldn't happen!!");
290 //t = doc_.createTextNode("");
291 //status.appendChild(t);
292 status.setAttribute(GSXML.STATUS_ERROR_CODE_ATT, Integer.toString(GSStatus.ERROR));
293 return response;
294 }
295
296 String coll_name = (String)params.get(COL_PARAM);
297 configureRequest("reload", coll_name, status, lang);
298 return response;
299
300 }
301
302 /** send a configure request to the message router
303 * action name should be "delete" or "reload"
304 * response will be put into the status element
305 */
306 protected void configureRequest(String action_name, String coll_name, Element status, String lang) {
307
308 // send the request to the MR
309 Element message = doc_.createElement(GSXML.MESSAGE_ELEM);
310 Element request = doc_.createElement(GSXML.REQUEST_ELEM);
311 request.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_CONFIGURE);
312 request.setAttribute(GSXML.TO_ATT, "");
313 message.appendChild(request);
314 Element command = doc_.createElement(GSXML.CONFIGURE_ELEM);
315 command.setAttribute(GSXML.TYPE_ATT, GSXML.COLLECTION_ELEM);
316 command.setAttribute(GSXML.NAME_ATT, coll_name);
317 if (action_name.equals("delete")) {
318 command.setAttribute(GSXML.ACTION_ATT, GSXML.CONFIG_ACTION_DEACTIVATE);
319 } else if (action_name.equals("reload")) {
320 command.setAttribute(GSXML.ACTION_ATT, GSXML.CONFIG_ACTION_ACTIVATE);
321 } else {
322 System.err.println("invalid action name passed to configureRequest:"+action_name);
323 return;
324 }
325 request.appendChild(command);
326 Element response = router_.process(message); // at the moment, get no info in response so ignore it
327 Text t;
328 String[] args={coll_name};
329
330 if (response == null) {
331 t = doc_.createTextNode(getTextString(action_name+".configure_error", args , lang));
332 status.setAttribute(GSXML.STATUS_ERROR_CODE_ATT, Integer.toString(GSStatus.ERROR));
333 status.appendChild(t);
334 return;
335 }
336
337 // if we got here, we have succeeded!
338 t = doc_.createTextNode(getTextString(action_name+".success", args, lang));
339 status.setAttribute(GSXML.STATUS_ERROR_CODE_ATT, Integer.toString(GSStatus.SUCCESS));
340 status.appendChild(t);
341 }
342
343
344 /** configure the service module
345 * for now, all services have type=build - need to think about this
346 */
347 public boolean configure(Element info) {
348 System.out.println("configuring GS2Construct");
349
350 Element e = null;
351 // hard code in the services for now
352
353 // set up short_service_info_ - for now just has name and type
354
355 e = doc_.createElement(GSXML.SERVICE_ELEM);
356 e.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_PROCESS);
357 e.setAttribute(GSXML.NAME_ATT, NEW_SERVICE);
358 short_service_info_.appendChild(e);
359
360 e = doc_.createElement(GSXML.SERVICE_ELEM);
361 e.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_PROCESS);
362 e.setAttribute(GSXML.NAME_ATT, IMPORT_SERVICE);
363 short_service_info_.appendChild(e);
364
365 e = doc_.createElement(GSXML.SERVICE_ELEM);
366 e.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_PROCESS);
367 e.setAttribute(GSXML.NAME_ATT, BUILD_SERVICE);
368 short_service_info_.appendChild(e);
369
370 e = doc_.createElement(GSXML.SERVICE_ELEM);
371 e.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_PROCESS);
372 e.setAttribute(GSXML.NAME_ATT, ACTIVATE_SERVICE);
373 short_service_info_.appendChild(e);
374
375 e = doc_.createElement(GSXML.SERVICE_ELEM);
376 e.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_PROCESS);
377 e.setAttribute(GSXML.NAME_ATT, DELETE_SERVICE);
378 short_service_info_.appendChild(e);
379
380 e = doc_.createElement(GSXML.SERVICE_ELEM);
381 e.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_PROCESS);
382 e.setAttribute(GSXML.NAME_ATT, RELOAD_SERVICE);
383 short_service_info_.appendChild(e);
384
385 e = doc_.createElement(GSXML.SERVICE_ELEM);
386 e.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_PROCESS);
387 e.setAttribute(GSXML.NAME_ATT, ADD_DOC_SERVICE);
388 short_service_info_.appendChild(e);
389
390 // set up service_info_map_
391 // should have full details about each service incl params lists etc.
392
393 Element param_list;
394 Element param;
395
396 e = doc_.createElement(GSXML.SERVICE_ELEM);
397 e.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_PROCESS);
398 e.setAttribute(GSXML.NAME_ATT, NEW_SERVICE);
399 param_list = doc_.createElement(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
400 param = GSXML.createParameter(doc_, NEW_COL_TITLE_PARAM, GSXML.PARAM_TYPE_STRING, "", null);
401 param_list.appendChild(param);
402 param = GSXML.createParameter(doc_, CREATOR_PARAM, GSXML.PARAM_TYPE_STRING, "", null);
403 param_list.appendChild(param);
404 param = GSXML.createParameter(doc_, NEW_COL_ABOUT_PARAM, GSXML.PARAM_TYPE_TEXT, "", null);
405 param_list.appendChild(param);
406 String[] types = {BUILDTYPE_MGPP, BUILDTYPE_MG};
407 param = GSXML.createParameter(doc_, BUILDTYPE_PARAM, GSXML.PARAM_TYPE_ENUM_SINGLE, BUILDTYPE_MGPP, types);
408 param_list.appendChild(param);
409
410 e.appendChild(param_list);
411 service_info_map_.put(NEW_SERVICE, e);
412
413 e = doc_.createElement(GSXML.SERVICE_ELEM);
414 e.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_PROCESS);
415 e.setAttribute(GSXML.NAME_ATT, IMPORT_SERVICE);
416 param_list = doc_.createElement(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
417 param = GSXML.createParameter(doc_, COL_PARAM, GSXML.PARAM_TYPE_ENUM_SINGLE, "", null);
418 param_list.appendChild(param);
419 e.appendChild(param_list);
420 service_info_map_.put(IMPORT_SERVICE, e);
421
422 e = doc_.createElement(GSXML.SERVICE_ELEM);
423 e.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_PROCESS);
424 e.setAttribute(GSXML.NAME_ATT, BUILD_SERVICE);
425 param_list = doc_.createElement(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
426 param = GSXML.createParameter(doc_, COL_PARAM, GSXML.PARAM_TYPE_ENUM_SINGLE, "", null);
427 param_list.appendChild(param);
428 e.appendChild(param_list);
429 service_info_map_.put(BUILD_SERVICE, e);
430
431 e = doc_.createElement(GSXML.SERVICE_ELEM);
432 e.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_PROCESS);
433 e.setAttribute(GSXML.NAME_ATT, ACTIVATE_SERVICE);
434 param_list = doc_.createElement(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
435 param = GSXML.createParameter(doc_, COL_PARAM, GSXML.PARAM_TYPE_ENUM_SINGLE, "", null);
436 param_list.appendChild(param);
437 e.appendChild(param_list);
438 service_info_map_.put(ACTIVATE_SERVICE, e);
439
440 e = doc_.createElement(GSXML.SERVICE_ELEM);
441 e.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_PROCESS);
442 e.setAttribute(GSXML.NAME_ATT, DELETE_SERVICE);
443 param_list = doc_.createElement(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
444 param = GSXML.createParameter(doc_, COL_PARAM, GSXML.PARAM_TYPE_ENUM_SINGLE, "", null);
445 param_list.appendChild(param);
446 e.appendChild(param_list);
447 service_info_map_.put(DELETE_SERVICE, e);
448
449 e = doc_.createElement(GSXML.SERVICE_ELEM);
450 e.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_PROCESS);
451 e.setAttribute(GSXML.NAME_ATT, RELOAD_SERVICE);
452 param_list = doc_.createElement(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
453 param = GSXML.createParameter(doc_, COL_PARAM, GSXML.PARAM_TYPE_ENUM_SINGLE, "", null);
454 param_list.appendChild(param);
455 e.appendChild(param_list);
456 service_info_map_.put(RELOAD_SERVICE, e);
457
458
459 return true;
460 }
461
462 /** returns a response element */
463 protected Element runCommand(Element request, int type ) {
464
465 // the response to send back
466 String name = GSPath.getFirstLink(request.getAttribute(GSXML.TO_ATT));
467 Element response = doc_.createElement(GSXML.RESPONSE_ELEM);
468 response.setAttribute(GSXML.FROM_ATT, name);
469 Element status = doc_.createElement(GSXML.STATUS_ELEM);
470 response.appendChild(status);
471
472 String lang = request.getAttribute(GSXML.LANG_ATT);
473 String request_type = request.getAttribute(GSXML.TYPE_ATT);
474
475 Element param_list = (Element)GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
476 HashMap params = GSXML.extractParams(param_list, false);
477
478 boolean get_status_only = false;
479 if (request_type.equals(GSXML.REQUEST_TYPE_STATUS)) {
480 get_status_only = true;
481 }
482 System.out.println("get status only = "+get_status_only);
483
484 // just check for status messages if that's all that's required
485 if (get_status_only) {
486 System.out.println("in get status only bit");
487 String id = (String)params.get(HANDLE_PARAM);
488 GS2PerlListener listener = (GS2PerlListener)listeners_.get(id);
489 if (listener==null) {
490 Text t = doc_.createTextNode(getTextString("general.process_id_error", lang));
491 status.appendChild(t);
492 status.setAttribute(GSXML.STATUS_ERROR_CODE_ATT, Integer.toString(GSStatus.ERROR));
493 } else {
494 Text t = doc_.createTextNode(listener.getUpdate());
495 status.appendChild(t);
496 status.setAttribute(GSXML.STATUS_ERROR_CODE_ATT, Integer.toString(listener.getStatus()));
497 // check that we actually should be removing the listener here
498 if (listener.isFinished()) { // remove this listener - its job is done
499 listeners_.remove(id); // not working
500 }
501 }
502 return response;
503
504 }
505 System.out.println("not in get status only bit");
506
507 // do teh actual command
508 String coll_name=null;
509 if (type==CollectionConstructor.NEW) {
510 String coll_title = (String)params.get(NEW_COL_TITLE_PARAM);
511 coll_name = createNewCollName(coll_title);
512 } else {
513 coll_name = (String)params.get(COL_PARAM);
514 }
515 System.err.println("Coll name = "+coll_name);
516 // makes a paramList of the relevant params
517 Element other_params = extractOtherParams(params, type);
518
519 //create the constructor to do the work
520
521 CollectionConstructor constructor = new GS2PerlConstructor("perl_build");
522 if (!constructor.configure()) {
523 Text t = doc_.createTextNode(getTextString("general.configure_constructor_error", lang));
524 status.appendChild(t);
525 status.setAttribute(GSXML.STATUS_ERROR_CODE_ATT, Integer.toString(GSStatus.ERROR));
526 return response;
527 }
528
529 constructor.setSiteHome(site_home_);
530 constructor.setCollectionName(coll_name);
531 constructor.setActionType(type);
532 constructor.setProcessParams(other_params);
533 // the listener
534 GS2PerlListener listener = new GS2PerlListener();
535 constructor.addListener(listener);
536 constructor.start();
537
538 String id = newID();
539 listeners_.put(id, listener);
540
541 status.setAttribute(GSXML.STATUS_PROCESS_ID_ATT, id);
542 status.setAttribute(GSXML.STATUS_ERROR_CODE_ATT, Integer.toString(GSStatus.ACCEPTED));
543 Text t = doc_.createTextNode(getTextString("general.process_start", lang));
544 status.appendChild(t);
545 return response;
546
547 }
548
549
550 //************************
551 // some helper functions
552 //************************
553
554 /** parse the collect directory and return a list of collection names */
555 protected String[] getCollectionList() {
556
557 File collectDir = new File(GSFile.collectDir(site_home_));
558 if (!collectDir.exists()) {
559 System.err.println("couldn't find collect dir: "+collectDir.toString());
560 return null;
561 }
562 System.out.println("GS2Construct: reading thru directory "+collectDir.getPath()+" to find collections.");
563 File[] contents = collectDir.listFiles();
564 int num_colls=0;
565 for (int i=0; i<contents.length;i++) {
566 if(contents[i].isDirectory() && !contents[i].getName().startsWith("CVS")) {
567 num_colls ++;
568 }
569 }
570
571 String[] names= new String[num_colls];
572
573 for (int i=0, j=0; i<contents.length;i++) {
574 if(contents[i].isDirectory()) {
575 String colName = contents[i].getName();
576 if (!colName.startsWith("CVS")) {
577 names[j] = colName;
578 j++;
579 }
580
581 }
582 }
583
584 return names;
585
586 }
587
588 /** ids used for process id */
589 private int current_id = 0;
590 private String newID() {
591 current_id++;
592 return Integer.toString(current_id);
593 }
594
595 /** creates a new short name from the collection title */
596 protected String createNewCollName(String coll_title) {
597
598 String base_name = null;
599 // take the first 6 letters
600 if (coll_title.length()<6) {
601 base_name = coll_title;
602 } else {
603 base_name = coll_title.substring(0,6);
604 }
605 File coll_dir = new File(GSFile.collectionBaseDir(site_home_, base_name));
606 if (!coll_dir.exists()) { // this name is ok - not used yet
607 return base_name;
608 }
609
610 // now we have to make a new name until we get a good one
611 // try name1, name2 name3 etc
612 int i=0;
613 while(coll_dir.exists()) {
614 i++;
615 coll_dir = new File(GSFile.collectionBaseDir(site_home_, base_name+Integer.toString(i)));
616 }
617 return base_name+Integer.toString(i);
618
619 }
620
621 /** takes the params from the request (in the HashMap) and extracts any
622 * that need to be passed to the constructor and puts them into a
623 * paramList element */
624 protected Element extractOtherParams(HashMap params, int type) {
625
626 Element param_list = doc_.createElement(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
627 if (type == CollectionConstructor.NEW) {
628 Element param = doc_.createElement(GSXML.PARAM_ELEM);
629 param.setAttribute(GSXML.NAME_ATT, "creator");
630 param.setAttribute(GSXML.VALUE_ATT, (String)params.get(CREATOR_PARAM));
631
632 param_list.appendChild(param);
633 param = doc_.createElement(GSXML.PARAM_ELEM);
634 param.setAttribute(GSXML.NAME_ATT, "about");
635 param.setAttribute(GSXML.VALUE_ATT, (String)params.get(NEW_COL_ABOUT_PARAM));
636 param_list.appendChild(param);
637 param = doc_.createElement(GSXML.PARAM_ELEM);
638 param.setAttribute(GSXML.NAME_ATT, "title");
639 param.setAttribute(GSXML.VALUE_ATT, (String)params.get(NEW_COL_TITLE_PARAM));
640 param_list.appendChild(param);
641 param = doc_.createElement(GSXML.PARAM_ELEM);
642 param.setAttribute(GSXML.NAME_ATT, "buildtype");
643 param.setAttribute(GSXML.VALUE_ATT, (String)params.get(BUILDTYPE_PARAM));
644 param_list.appendChild(param);
645 return param_list;
646 }
647
648 // other ones dont have params yet
649 return null;
650 }
651
652}
653
654
Note: See TracBrowser for help on using the repository browser.