Changeset 24855


Ignore:
Timestamp:
2011-12-06T10:58:22+13:00 (12 years ago)
Author:
sjm84
Message:

Reformatting this file ahead of some changes

File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/trunk/greenstone3/src/java/org/greenstone/gsdl3/service/GS2Construct.java

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