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

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

changed handle param to pid

  • Property svn:keywords set to Author Date Id Revision
File size: 25.3 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: 3974 $
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 PROCESS_ID_PARAM = GSCGI.PROCESS_ID_ARG;
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 if (get_status_only) {
237 // 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
238 System.err.println("had a status request for delete - this shouldn't happen!!");
239 //t = doc_.createTextNode("");
240 //status.appendChild(t);
241 status.setAttribute(GSXML.STATUS_ERROR_CODE_ATT, Integer.toString(GSStatus.ERROR));
242 return response;
243 }
244 String coll_name = (String)params.get(COL_PARAM);
245 String[] args={coll_name};
246 File coll_dir = new File(GSFile.collectionBaseDir(site_home_, coll_name));
247 // check that the coll is there in the first place
248 if (!coll_dir.exists()) {
249 t = doc_.createTextNode(getTextString("delete.exists_error", args, lang));
250 status.appendChild(t);
251 status.setAttribute(GSXML.STATUS_ERROR_CODE_ATT, Integer.toString(GSStatus.ERROR));
252 return response;
253 }
254
255 // try to delete the directory
256 if (!GSFile.deleteFile(coll_dir)) {
257 t = doc_.createTextNode(getTextString("delete.delete_error", args, lang));
258 status.setAttribute(GSXML.STATUS_ERROR_CODE_ATT, Integer.toString(GSStatus.ERROR));
259 status.appendChild(t);
260 return response;
261 }
262 configureRequest("delete", coll_name, status, lang);
263 return response;
264 }
265
266 protected Element processReloadCollection(Element request) {
267
268 // the response to send back
269 String name = GSPath.getFirstLink(request.getAttribute(GSXML.TO_ATT));
270 Element response = doc_.createElement(GSXML.RESPONSE_ELEM);
271 response.setAttribute(GSXML.FROM_ATT, name);
272 Element status = doc_.createElement(GSXML.STATUS_ELEM);
273 response.appendChild(status);
274 Text t = null; // the text node for the error/success message
275
276 String lang = request.getAttribute(GSXML.LANG_ATT);
277 String request_type = request.getAttribute(GSXML.TYPE_ATT);
278
279 Element param_list = (Element)GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
280 HashMap params = GSXML.extractParams(param_list, false);
281
282 boolean get_status_only = false;
283 if (request_type.equals(GSXML.REQUEST_TYPE_STATUS)) {
284 get_status_only = true;
285 }
286 if (get_status_only) {
287 // reload is synchronous - this makes no sense
288 System.err.println("had a status request for reload - this shouldn't happen!!");
289 //t = doc_.createTextNode("");
290 //status.appendChild(t);
291 status.setAttribute(GSXML.STATUS_ERROR_CODE_ATT, Integer.toString(GSStatus.ERROR));
292 return response;
293 }
294
295 String coll_name = (String)params.get(COL_PARAM);
296 configureRequest("reload", coll_name, status, lang);
297 return response;
298
299 }
300
301 /** send a configure request to the message router
302 * action name should be "delete" or "reload"
303 * response will be put into the status element
304 */
305 protected void configureRequest(String action_name, String coll_name, Element status, String lang) {
306
307 // send the request to the MR
308 Element message = doc_.createElement(GSXML.MESSAGE_ELEM);
309 Element request = doc_.createElement(GSXML.REQUEST_ELEM);
310 request.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_CONFIGURE);
311 request.setAttribute(GSXML.TO_ATT, "");
312 message.appendChild(request);
313 Element command = doc_.createElement(GSXML.CONFIGURE_ELEM);
314 command.setAttribute(GSXML.TYPE_ATT, GSXML.COLLECTION_ELEM);
315 command.setAttribute(GSXML.NAME_ATT, coll_name);
316 if (action_name.equals("delete")) {
317 command.setAttribute(GSXML.ACTION_ATT, GSXML.CONFIG_ACTION_DEACTIVATE);
318 } else if (action_name.equals("reload")) {
319 command.setAttribute(GSXML.ACTION_ATT, GSXML.CONFIG_ACTION_ACTIVATE);
320 } else {
321 System.err.println("invalid action name passed to configureRequest:"+action_name);
322 return;
323 }
324 request.appendChild(command);
325 Element response = router_.process(message); // at the moment, get no info in response so ignore it
326 Text t;
327 String[] args={coll_name};
328
329 if (response == null) {
330 t = doc_.createTextNode(getTextString(action_name+".configure_error", args , lang));
331 status.setAttribute(GSXML.STATUS_ERROR_CODE_ATT, Integer.toString(GSStatus.ERROR));
332 status.appendChild(t);
333 return;
334 }
335
336 // if we got here, we have succeeded!
337 t = doc_.createTextNode(getTextString(action_name+".success", args, lang));
338 status.setAttribute(GSXML.STATUS_ERROR_CODE_ATT, Integer.toString(GSStatus.SUCCESS));
339 status.appendChild(t);
340 }
341
342
343 /** configure the service module
344 * for now, all services have type=build - need to think about this
345 */
346 public boolean configure(Element info, Element extra_info) {
347 System.out.println("configuring GS2Construct");
348
349 Element e = null;
350 // hard code in the services for now
351
352 // set up short_service_info_ - for now just has name and type
353
354 e = doc_.createElement(GSXML.SERVICE_ELEM);
355 e.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_PROCESS);
356 e.setAttribute(GSXML.NAME_ATT, NEW_SERVICE);
357 short_service_info_.appendChild(e);
358
359 e = doc_.createElement(GSXML.SERVICE_ELEM);
360 e.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_PROCESS);
361 e.setAttribute(GSXML.NAME_ATT, IMPORT_SERVICE);
362 short_service_info_.appendChild(e);
363
364 e = doc_.createElement(GSXML.SERVICE_ELEM);
365 e.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_PROCESS);
366 e.setAttribute(GSXML.NAME_ATT, BUILD_SERVICE);
367 short_service_info_.appendChild(e);
368
369 e = doc_.createElement(GSXML.SERVICE_ELEM);
370 e.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_PROCESS);
371 e.setAttribute(GSXML.NAME_ATT, ACTIVATE_SERVICE);
372 short_service_info_.appendChild(e);
373
374 e = doc_.createElement(GSXML.SERVICE_ELEM);
375 e.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_PROCESS);
376 e.setAttribute(GSXML.NAME_ATT, DELETE_SERVICE);
377 short_service_info_.appendChild(e);
378
379 e = doc_.createElement(GSXML.SERVICE_ELEM);
380 e.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_PROCESS);
381 e.setAttribute(GSXML.NAME_ATT, RELOAD_SERVICE);
382 short_service_info_.appendChild(e);
383
384 e = doc_.createElement(GSXML.SERVICE_ELEM);
385 e.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_PROCESS);
386 e.setAttribute(GSXML.NAME_ATT, ADD_DOC_SERVICE);
387 short_service_info_.appendChild(e);
388
389 // set up service_info_map_
390 // should have full details about each service incl params lists etc.
391
392 Element param_list;
393 Element param;
394
395 e = doc_.createElement(GSXML.SERVICE_ELEM);
396 e.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_PROCESS);
397 e.setAttribute(GSXML.NAME_ATT, NEW_SERVICE);
398 param_list = doc_.createElement(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
399 param = GSXML.createParameter(doc_, NEW_COL_TITLE_PARAM, GSXML.PARAM_TYPE_STRING, "", null);
400 param_list.appendChild(param);
401 param = GSXML.createParameter(doc_, CREATOR_PARAM, GSXML.PARAM_TYPE_STRING, "", null);
402 param_list.appendChild(param);
403 param = GSXML.createParameter(doc_, NEW_COL_ABOUT_PARAM, GSXML.PARAM_TYPE_TEXT, "", null);
404 param_list.appendChild(param);
405 String[] types = {BUILDTYPE_MGPP, BUILDTYPE_MG};
406 param = GSXML.createParameter(doc_, BUILDTYPE_PARAM, GSXML.PARAM_TYPE_ENUM_SINGLE, BUILDTYPE_MGPP, types);
407 param_list.appendChild(param);
408
409 e.appendChild(param_list);
410 service_info_map_.put(NEW_SERVICE, e);
411
412 e = doc_.createElement(GSXML.SERVICE_ELEM);
413 e.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_PROCESS);
414 e.setAttribute(GSXML.NAME_ATT, IMPORT_SERVICE);
415 param_list = doc_.createElement(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
416 param = GSXML.createParameter(doc_, COL_PARAM, GSXML.PARAM_TYPE_ENUM_SINGLE, "", null);
417 param_list.appendChild(param);
418 e.appendChild(param_list);
419 service_info_map_.put(IMPORT_SERVICE, e);
420
421 e = doc_.createElement(GSXML.SERVICE_ELEM);
422 e.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_PROCESS);
423 e.setAttribute(GSXML.NAME_ATT, BUILD_SERVICE);
424 param_list = doc_.createElement(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
425 param = GSXML.createParameter(doc_, COL_PARAM, GSXML.PARAM_TYPE_ENUM_SINGLE, "", null);
426 param_list.appendChild(param);
427 e.appendChild(param_list);
428 service_info_map_.put(BUILD_SERVICE, e);
429
430 e = doc_.createElement(GSXML.SERVICE_ELEM);
431 e.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_PROCESS);
432 e.setAttribute(GSXML.NAME_ATT, ACTIVATE_SERVICE);
433 param_list = doc_.createElement(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
434 param = GSXML.createParameter(doc_, COL_PARAM, GSXML.PARAM_TYPE_ENUM_SINGLE, "", null);
435 param_list.appendChild(param);
436 e.appendChild(param_list);
437 service_info_map_.put(ACTIVATE_SERVICE, e);
438
439 e = doc_.createElement(GSXML.SERVICE_ELEM);
440 e.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_PROCESS);
441 e.setAttribute(GSXML.NAME_ATT, DELETE_SERVICE);
442 param_list = doc_.createElement(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
443 param = GSXML.createParameter(doc_, COL_PARAM, GSXML.PARAM_TYPE_ENUM_SINGLE, "", null);
444 param_list.appendChild(param);
445 e.appendChild(param_list);
446 service_info_map_.put(DELETE_SERVICE, e);
447
448 e = doc_.createElement(GSXML.SERVICE_ELEM);
449 e.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_PROCESS);
450 e.setAttribute(GSXML.NAME_ATT, RELOAD_SERVICE);
451 param_list = doc_.createElement(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
452 param = GSXML.createParameter(doc_, COL_PARAM, GSXML.PARAM_TYPE_ENUM_SINGLE, "", null);
453 param_list.appendChild(param);
454 e.appendChild(param_list);
455 service_info_map_.put(RELOAD_SERVICE, e);
456
457
458 return true;
459 }
460
461 /** returns a response element */
462 protected Element runCommand(Element request, int type ) {
463
464 // the response to send back
465 String name = GSPath.getFirstLink(request.getAttribute(GSXML.TO_ATT));
466 Element response = doc_.createElement(GSXML.RESPONSE_ELEM);
467 response.setAttribute(GSXML.FROM_ATT, name);
468 Element status = doc_.createElement(GSXML.STATUS_ELEM);
469 response.appendChild(status);
470
471 String lang = request.getAttribute(GSXML.LANG_ATT);
472 String request_type = request.getAttribute(GSXML.TYPE_ATT);
473
474 Element param_list = (Element)GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
475 HashMap params = GSXML.extractParams(param_list, false);
476
477 boolean get_status_only = false;
478 if (request_type.equals(GSXML.REQUEST_TYPE_STATUS)) {
479 get_status_only = true;
480 }
481
482 // just check for status messages if that's all that's required
483 if (get_status_only) {
484 String id = (String)params.get(PROCESS_ID_PARAM);
485 status.setAttribute(GSXML.STATUS_PROCESS_ID_ATT, id);
486 GS2PerlListener listener = (GS2PerlListener)listeners_.get(id);
487 if (listener==null) {
488 Text t = doc_.createTextNode(getTextString("general.process_id_error", lang));
489 status.appendChild(t);
490 status.setAttribute(GSXML.STATUS_ERROR_CODE_ATT, Integer.toString(GSStatus.ERROR));
491 } else {
492 Text t = doc_.createTextNode(listener.getUpdate());
493 status.appendChild(t);
494 status.setAttribute(GSXML.STATUS_ERROR_CODE_ATT, Integer.toString(listener.getStatus()));
495 // check that we actually should be removing the listener here
496 if (listener.isFinished()) { // remove this listener - its job is done
497 listeners_.remove(id); // not working
498 }
499 }
500 return response;
501
502 }
503
504 // do teh actual command
505 String coll_name=null;
506 if (type==CollectionConstructor.NEW) {
507 String coll_title = (String)params.get(NEW_COL_TITLE_PARAM);
508 coll_name = createNewCollName(coll_title);
509 } else {
510 coll_name = (String)params.get(COL_PARAM);
511 }
512 System.err.println("Coll name = "+coll_name);
513 // makes a paramList of the relevant params
514 Element other_params = extractOtherParams(params, type);
515
516 //create the constructor to do the work
517
518 CollectionConstructor constructor = new GS2PerlConstructor("perl_build");
519 if (!constructor.configure()) {
520 Text t = doc_.createTextNode(getTextString("general.configure_constructor_error", lang));
521 status.appendChild(t);
522 status.setAttribute(GSXML.STATUS_ERROR_CODE_ATT, Integer.toString(GSStatus.ERROR));
523 return response;
524 }
525
526 constructor.setSiteHome(site_home_);
527 constructor.setCollectionName(coll_name);
528 constructor.setActionType(type);
529 constructor.setProcessParams(other_params);
530 // the listener
531 GS2PerlListener listener = new GS2PerlListener();
532 constructor.addListener(listener);
533 constructor.start();
534
535 String id = newID();
536 listeners_.put(id, listener);
537
538 status.setAttribute(GSXML.STATUS_PROCESS_ID_ATT, id);
539 status.setAttribute(GSXML.STATUS_ERROR_CODE_ATT, Integer.toString(GSStatus.ACCEPTED));
540 Text t = doc_.createTextNode(getTextString("general.process_start", lang));
541 status.appendChild(t);
542 return response;
543
544 }
545
546
547 //************************
548 // some helper functions
549 //************************
550
551 /** parse the collect directory and return a list of collection names */
552 protected String[] getCollectionList() {
553
554 File collectDir = new File(GSFile.collectDir(site_home_));
555 if (!collectDir.exists()) {
556 System.err.println("couldn't find collect dir: "+collectDir.toString());
557 return null;
558 }
559 System.out.println("GS2Construct: reading thru directory "+collectDir.getPath()+" to find collections.");
560 File[] contents = collectDir.listFiles();
561 int num_colls=0;
562 for (int i=0; i<contents.length;i++) {
563 if(contents[i].isDirectory() && !contents[i].getName().startsWith("CVS")) {
564 num_colls ++;
565 }
566 }
567
568 String[] names= new String[num_colls];
569
570 for (int i=0, j=0; i<contents.length;i++) {
571 if(contents[i].isDirectory()) {
572 String colName = contents[i].getName();
573 if (!colName.startsWith("CVS")) {
574 names[j] = colName;
575 j++;
576 }
577
578 }
579 }
580
581 return names;
582
583 }
584
585 /** ids used for process id */
586 private int current_id = 0;
587 private String newID() {
588 current_id++;
589 return Integer.toString(current_id);
590 }
591
592 /** creates a new short name from the collection title */
593 protected String createNewCollName(String coll_title) {
594
595 String base_name = null;
596 // take the first 6 letters
597 if (coll_title.length()<6) {
598 base_name = coll_title;
599 } else {
600 base_name = coll_title.substring(0,6);
601 }
602 File coll_dir = new File(GSFile.collectionBaseDir(site_home_, base_name));
603 if (!coll_dir.exists()) { // this name is ok - not used yet
604 return base_name;
605 }
606
607 // now we have to make a new name until we get a good one
608 // try name1, name2 name3 etc
609 int i=0;
610 while(coll_dir.exists()) {
611 i++;
612 coll_dir = new File(GSFile.collectionBaseDir(site_home_, base_name+Integer.toString(i)));
613 }
614 return base_name+Integer.toString(i);
615
616 }
617
618 /** takes the params from the request (in the HashMap) and extracts any
619 * that need to be passed to the constructor and puts them into a
620 * paramList element */
621 protected Element extractOtherParams(HashMap params, int type) {
622
623 Element param_list = doc_.createElement(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
624 if (type == CollectionConstructor.NEW) {
625 Element param = doc_.createElement(GSXML.PARAM_ELEM);
626 param.setAttribute(GSXML.NAME_ATT, "creator");
627 param.setAttribute(GSXML.VALUE_ATT, (String)params.get(CREATOR_PARAM));
628
629 param_list.appendChild(param);
630 param = doc_.createElement(GSXML.PARAM_ELEM);
631 param.setAttribute(GSXML.NAME_ATT, "about");
632 param.setAttribute(GSXML.VALUE_ATT, (String)params.get(NEW_COL_ABOUT_PARAM));
633 param_list.appendChild(param);
634 param = doc_.createElement(GSXML.PARAM_ELEM);
635 param.setAttribute(GSXML.NAME_ATT, "title");
636 param.setAttribute(GSXML.VALUE_ATT, (String)params.get(NEW_COL_TITLE_PARAM));
637 param_list.appendChild(param);
638 param = doc_.createElement(GSXML.PARAM_ELEM);
639 param.setAttribute(GSXML.NAME_ATT, "buildtype");
640 param.setAttribute(GSXML.VALUE_ATT, (String)params.get(BUILDTYPE_PARAM));
641 param_list.appendChild(param);
642 return param_list;
643 }
644
645 // other ones dont have params yet
646 return null;
647 }
648
649}
650
651
Note: See TracBrowser for help on using the repository browser.