source: main/trunk/greenstone3/src/java/org/greenstone/gsdl3/service/GS2Construct.java@ 25067

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

Moved the collection unload call so that it is done after a the status check

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