[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 | */
|
---|
| 19 | package org.greenstone.gsdl3.service;
|
---|
| 20 |
|
---|
[25540] | 21 | import java.io.BufferedWriter;
|
---|
[3649] | 22 | import java.io.File;
|
---|
[25540] | 23 | import java.io.FileWriter;
|
---|
[25635] | 24 | import java.io.Serializable;
|
---|
[27717] | 25 | import java.util.Collections;
|
---|
[29869] | 26 | import java.util.Iterator;
|
---|
| 27 | import java.util.Map.Entry;
|
---|
[27717] | 28 | import java.util.HashMap;
|
---|
| 29 | import java.util.Map;
|
---|
[29869] | 30 | import java.util.Set;
|
---|
[3649] | 31 |
|
---|
[27717] | 32 | import org.apache.log4j.Logger;
|
---|
| 33 | import org.greenstone.gsdl3.build.GS2PerlConstructor;
|
---|
| 34 | import org.greenstone.gsdl3.build.GS2PerlListener;
|
---|
| 35 | import org.greenstone.gsdl3.util.GSFile;
|
---|
| 36 | import org.greenstone.gsdl3.util.GSParams;
|
---|
| 37 | import org.greenstone.gsdl3.util.GSPath;
|
---|
| 38 | import org.greenstone.gsdl3.util.GSStatus;
|
---|
| 39 | import org.greenstone.gsdl3.util.GSXML;
|
---|
[30704] | 40 | import org.greenstone.gsdl3.util.OID;
|
---|
[30516] | 41 | import org.greenstone.gsdl3.util.SimpleCollectionDatabase;
|
---|
[27717] | 42 | import org.greenstone.gsdl3.util.UserContext;
|
---|
[28966] | 43 | import org.greenstone.gsdl3.util.XMLConverter;
|
---|
| 44 |
|
---|
| 45 | import org.w3c.dom.Document;
|
---|
[27717] | 46 | import org.w3c.dom.Element;
|
---|
| 47 | import org.w3c.dom.Node;
|
---|
| 48 | import org.w3c.dom.Text;
|
---|
[25540] | 49 |
|
---|
[3649] | 50 | /**
|
---|
[24855] | 51 | * A Services class for building collections provides a wrapper around the old
|
---|
| 52 | * perl scripts
|
---|
| 53 | *
|
---|
[25727] | 54 | * @author Katherine Don
|
---|
[3649] | 55 | */
|
---|
[24855] | 56 | public class GS2Construct extends ServiceRack
|
---|
| 57 | {
|
---|
[13124] | 58 |
|
---|
[24855] | 59 | static Logger logger = Logger.getLogger(org.greenstone.gsdl3.service.GS2Construct.class.getName());
|
---|
[3649] | 60 |
|
---|
[24855] | 61 | // services offered
|
---|
| 62 | private static final String NEW_SERVICE = "NewCollection";
|
---|
| 63 | private static final String ADD_DOC_SERVICE = "AddDocument";
|
---|
| 64 | private static final String IMPORT_SERVICE = "ImportCollection";
|
---|
| 65 | private static final String BUILD_SERVICE = "BuildCollection";
|
---|
| 66 | private static final String ACTIVATE_SERVICE = "ActivateCollection";
|
---|
[25540] | 67 | private static final String BUILD_AND_ACTIVATE_SERVICE = "BuildAndActivateCollection";
|
---|
[24855] | 68 | private static final String DELETE_SERVICE = "DeleteCollection";
|
---|
| 69 | private static final String RELOAD_SERVICE = "ReloadCollection";
|
---|
[30617] | 70 | private static final String MODIFY_METADATA_SERVICE = "ModifyMetadata"; // set or remove metadata
|
---|
[3649] | 71 |
|
---|
[30617] | 72 |
|
---|
[24855] | 73 | // params used
|
---|
| 74 | private static final String COL_PARAM = "collection";
|
---|
| 75 | private static final String NEW_COL_TITLE_PARAM = "collTitle";
|
---|
| 76 | private static final String NEW_COL_ABOUT_PARAM = "collAbout";
|
---|
| 77 | private static final String CREATOR_PARAM = "creator";
|
---|
| 78 | private static final String NEW_FILE_PARAM = "newfile";
|
---|
| 79 | private static final String PROCESS_ID_PARAM = GSParams.PROCESS_ID;
|
---|
| 80 | private static final String BUILDTYPE_PARAM = "buildType";
|
---|
| 81 | private static final String BUILDTYPE_MG = "mg";
|
---|
| 82 | private static final String BUILDTYPE_MGPP = "mgpp";
|
---|
[3649] | 83 |
|
---|
[30516] | 84 | protected static String DATABASE_TYPE = null;
|
---|
| 85 | protected SimpleCollectionDatabase coll_db = null;
|
---|
| 86 |
|
---|
[24855] | 87 | // the list of the collections - store between some method calls
|
---|
| 88 | private String[] collection_list = null;
|
---|
[3649] | 89 |
|
---|
[24855] | 90 | // set of listeners for any construction commands
|
---|
[25635] | 91 | protected Map<String, GS2PerlListener> listeners = null;
|
---|
[27717] | 92 | protected HashMap<String, Boolean> collectionOperationMap = new HashMap<String, Boolean>();
|
---|
[24855] | 93 |
|
---|
| 94 | public GS2Construct()
|
---|
| 95 | {
|
---|
[25635] | 96 | this.listeners = Collections.synchronizedMap(new HashMap<String, GS2PerlListener>());
|
---|
[3681] | 97 | }
|
---|
[3649] | 98 |
|
---|
[24855] | 99 | /** returns a specific service description */
|
---|
[28966] | 100 | protected Element getServiceDescription(Document doc, String service, String lang, String subset)
|
---|
[24855] | 101 | {
|
---|
[3649] | 102 |
|
---|
[28966] | 103 | Element description = doc.createElement(GSXML.SERVICE_ELEM);
|
---|
[24855] | 104 | description.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_PROCESS);
|
---|
| 105 | description.setAttribute(GSXML.NAME_ATT, service);
|
---|
| 106 | if (subset == null || subset.equals(GSXML.DISPLAY_TEXT_ELEM + GSXML.LIST_MODIFIER))
|
---|
| 107 | {
|
---|
[28966] | 108 | description.appendChild(GSXML.createDisplayTextElement(doc, GSXML.DISPLAY_TEXT_NAME, getTextString(service + ".name", lang)));
|
---|
| 109 | description.appendChild(GSXML.createDisplayTextElement(doc, GSXML.DISPLAY_TEXT_DESCRIPTION, getTextString(service + ".description", lang)));
|
---|
| 110 | description.appendChild(GSXML.createDisplayTextElement(doc, GSXML.DISPLAY_TEXT_SUBMIT, getTextString(service + ".submit", lang)));
|
---|
[24855] | 111 | }
|
---|
| 112 | if (subset == null || subset.equals(GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER))
|
---|
| 113 | {
|
---|
[28966] | 114 | Element param_list = doc.createElement(GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
|
---|
[24855] | 115 | description.appendChild(param_list);
|
---|
[3681] | 116 |
|
---|
[24855] | 117 | if (service.equals(NEW_SERVICE))
|
---|
| 118 | {
|
---|
| 119 |
|
---|
[28966] | 120 | Element param = GSXML.createParameterDescription(doc, NEW_COL_TITLE_PARAM, getTextString("param." + NEW_COL_TITLE_PARAM, lang), GSXML.PARAM_TYPE_STRING, null, null, null);
|
---|
[24855] | 121 | param_list.appendChild(param);
|
---|
[28966] | 122 | param = GSXML.createParameterDescription(doc, CREATOR_PARAM, getTextString("param." + CREATOR_PARAM, lang), GSXML.PARAM_TYPE_STRING, null, null, null);
|
---|
[24855] | 123 | param_list.appendChild(param);
|
---|
[28966] | 124 | param = GSXML.createParameterDescription(doc, NEW_COL_ABOUT_PARAM, getTextString("param." + NEW_COL_ABOUT_PARAM, lang), GSXML.PARAM_TYPE_TEXT, null, null, null);
|
---|
[24855] | 125 | param_list.appendChild(param);
|
---|
| 126 | String[] types = { BUILDTYPE_MGPP, BUILDTYPE_MG };
|
---|
| 127 | String[] type_texts = { getTextString("param." + BUILDTYPE_PARAM + "." + BUILDTYPE_MGPP, lang), getTextString("param." + BUILDTYPE_PARAM + "." + BUILDTYPE_MG, lang) };
|
---|
| 128 |
|
---|
[28966] | 129 | param = GSXML.createParameterDescription(doc, BUILDTYPE_PARAM, getTextString("param." + BUILDTYPE_PARAM, lang), GSXML.PARAM_TYPE_ENUM_SINGLE, BUILDTYPE_MGPP, types, type_texts);
|
---|
[24855] | 130 | param_list.appendChild(param);
|
---|
| 131 | }
|
---|
[30617] | 132 | else if (service.equals(ACTIVATE_SERVICE) || service.equals(IMPORT_SERVICE) || service.equals(BUILD_SERVICE) || service.equals(RELOAD_SERVICE) || service.equals(DELETE_SERVICE) || service.equals(MODIFY_METADATA_SERVICE))
|
---|
[24855] | 133 | {
|
---|
| 134 |
|
---|
| 135 | this.collection_list = getCollectionList();
|
---|
[28966] | 136 | Element param = GSXML.createParameterDescription(doc, COL_PARAM, getTextString("param." + COL_PARAM, lang), GSXML.PARAM_TYPE_ENUM_SINGLE, null, this.collection_list, this.collection_list);
|
---|
[24855] | 137 | param_list.appendChild(param);
|
---|
| 138 | }
|
---|
| 139 | else
|
---|
| 140 | {
|
---|
| 141 | // invalid service name
|
---|
| 142 | return null;
|
---|
| 143 | }
|
---|
| 144 | }
|
---|
| 145 | return description;
|
---|
[3681] | 146 | }
|
---|
| 147 |
|
---|
[24855] | 148 | // each service must have a method "process<New service name>"
|
---|
[3681] | 149 |
|
---|
[24855] | 150 | protected Element processNewCollection(Element request)
|
---|
| 151 | {
|
---|
[29869] | 152 | if (!userHasCollectionEditPermissions(request)) {
|
---|
| 153 | Document result_doc = XMLConverter.newDOM();
|
---|
| 154 | Element result = GSXML.createBasicResponse(result_doc, "processNewCollection");
|
---|
| 155 | GSXML.addError(result, "This user does not have the required permissions to perform this action.");
|
---|
| 156 | return result;
|
---|
| 157 | }
|
---|
| 158 | return runCommand(request, GS2PerlConstructor.NEW);
|
---|
[3649] | 159 | }
|
---|
[24855] | 160 |
|
---|
| 161 | /** TODO:implement this */
|
---|
| 162 | protected Element processAddDocument(Element request)
|
---|
| 163 | {
|
---|
[29869] | 164 | if (!userHasCollectionEditPermissions(request)) {
|
---|
| 165 | Document result_doc = XMLConverter.newDOM();
|
---|
| 166 | Element result = GSXML.createBasicResponse(result_doc, "processAddDocument");
|
---|
| 167 | GSXML.addError(result, "This user does not have the required permissions to perform this action.");
|
---|
| 168 | return result;
|
---|
| 169 | }
|
---|
| 170 |
|
---|
[28966] | 171 | Document result_doc = XMLConverter.newDOM();
|
---|
[24855] | 172 | // decode the file name, add it to the import directory
|
---|
| 173 | String name = GSPath.getFirstLink(request.getAttribute(GSXML.TO_ATT));
|
---|
[28966] | 174 | Element response = result_doc.createElement(GSXML.RESPONSE_ELEM);
|
---|
[24855] | 175 | response.setAttribute(GSXML.FROM_ATT, name);
|
---|
[28966] | 176 | Element status = result_doc.createElement(GSXML.STATUS_ELEM);
|
---|
[24855] | 177 | response.appendChild(status);
|
---|
| 178 | //String lang = request.getAttribute(GSXML.LANG_ATT);
|
---|
| 179 | //String request_type = request.getAttribute(GSXML.TYPE_ATT);
|
---|
[28966] | 180 | Text t = result_doc.createTextNode("AddDocument: not implemented yet");
|
---|
[24855] | 181 | status.appendChild(t);
|
---|
| 182 | status.setAttribute(GSXML.STATUS_ERROR_CODE_ATT, Integer.toString(GSStatus.ERROR));
|
---|
| 183 | return response;
|
---|
[3681] | 184 | }
|
---|
[24855] | 185 |
|
---|
[25540] | 186 | protected Element processBuildAndActivateCollection(Element request)
|
---|
| 187 | {
|
---|
[29869] | 188 | // check permissions
|
---|
[30617] | 189 | if (!userHasCollectionEditPermissions(request)) {
|
---|
[29869] | 190 | Document result_doc = XMLConverter.newDOM();
|
---|
| 191 | Element result = GSXML.createBasicResponse(result_doc, "processBuildAndActivateCollection");
|
---|
| 192 | GSXML.addError(result, "This user does not have the required permissions to perform this action.");
|
---|
| 193 | return result;
|
---|
[30617] | 194 | }
|
---|
[29869] | 195 |
|
---|
[30617] | 196 |
|
---|
[25540] | 197 | waitUntilReady(request);
|
---|
| 198 | Element buildResponse = processBuildCollection(request);
|
---|
[25648] | 199 | if (buildResponse.getElementsByTagName(GSXML.ERROR_ELEM).getLength() > 0)
|
---|
[25540] | 200 | {
|
---|
| 201 | return buildResponse;
|
---|
| 202 | }
|
---|
[25648] | 203 |
|
---|
[25540] | 204 | Element statusElem = (Element) buildResponse.getElementsByTagName(GSXML.STATUS_ELEM).item(0);
|
---|
| 205 | String id = statusElem.getAttribute("pid");
|
---|
| 206 |
|
---|
[25635] | 207 | GS2PerlListener currentListener = this.listeners.get(id);
|
---|
[25540] | 208 | int statusCode = currentListener.getStatus();
|
---|
| 209 | while (!GSStatus.isCompleted(statusCode))
|
---|
| 210 | {
|
---|
| 211 | // wait for the process, and keep checking the status code
|
---|
| 212 | // there is probably a better way to do this.
|
---|
| 213 | try
|
---|
| 214 | {
|
---|
| 215 | Thread.currentThread().sleep(100);
|
---|
| 216 | }
|
---|
| 217 | catch (Exception e)
|
---|
| 218 | { // ignore
|
---|
| 219 | }
|
---|
| 220 | statusCode = currentListener.getStatus();
|
---|
| 221 | }
|
---|
| 222 |
|
---|
| 223 | Element activateResponse = processActivateCollection(request);
|
---|
| 224 | signalReady(request);
|
---|
| 225 | return activateResponse;
|
---|
| 226 | }
|
---|
| 227 |
|
---|
[24855] | 228 | protected Element processImportCollection(Element request)
|
---|
| 229 | {
|
---|
[29869] | 230 | if (!userHasCollectionEditPermissions(request)) {
|
---|
| 231 | Document result_doc = XMLConverter.newDOM();
|
---|
| 232 | Element result = GSXML.createBasicResponse(result_doc, "processImportCollection");
|
---|
| 233 | GSXML.addError(result, "This user does not have the required permissions to perform this action.");
|
---|
| 234 | return result;
|
---|
| 235 | }
|
---|
| 236 |
|
---|
[25540] | 237 | Element param_list = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
|
---|
[25635] | 238 | HashMap<String, Serializable> params = GSXML.extractParams(param_list, false);
|
---|
[25540] | 239 |
|
---|
[27717] | 240 | if (params == null)
|
---|
| 241 | {
|
---|
| 242 | return null;
|
---|
| 243 | }
|
---|
| 244 |
|
---|
[25540] | 245 | //If we have been requested to only build certain documents then we need to create a manifest file
|
---|
| 246 | String documentsParam = (String) params.get("documents");
|
---|
| 247 | if (documentsParam != null && !documentsParam.equals(""))
|
---|
| 248 | {
|
---|
| 249 | String s = File.separator;
|
---|
| 250 | String manifestFolderPath = this.site_home + s + "collect" + s + params.get(COL_PARAM) + s + "manifests";
|
---|
| 251 | String manifestFilePath = manifestFolderPath + File.separator + "tempManifest.xml";
|
---|
| 252 |
|
---|
| 253 | File manifestFolderFile = new File(manifestFolderPath);
|
---|
| 254 | if (!manifestFolderFile.exists())
|
---|
| 255 | {
|
---|
| 256 | manifestFolderFile.mkdirs();
|
---|
| 257 | }
|
---|
| 258 |
|
---|
| 259 | File manifestFile = new File(manifestFilePath);
|
---|
| 260 | if (!manifestFile.exists())
|
---|
| 261 | {
|
---|
| 262 | try
|
---|
| 263 | {
|
---|
| 264 | manifestFile.createNewFile();
|
---|
| 265 | }
|
---|
| 266 | catch (Exception ex)
|
---|
| 267 | {
|
---|
| 268 | ex.printStackTrace();
|
---|
| 269 | return null; //Probably should return an actual error
|
---|
| 270 | }
|
---|
| 271 | }
|
---|
| 272 | String[] docList = documentsParam.split(",");
|
---|
| 273 |
|
---|
| 274 | try
|
---|
| 275 | {
|
---|
| 276 | BufferedWriter bw = new BufferedWriter(new FileWriter(manifestFile));
|
---|
| 277 | bw.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
|
---|
| 278 | bw.write("<Manifest>\n");
|
---|
| 279 | bw.write(" <Index>\n");
|
---|
| 280 | for (int j = 0; j < docList.length; j++)
|
---|
| 281 | {
|
---|
| 282 | bw.write(" <Filename>" + docList[j] + "</Filename>\n");
|
---|
| 283 | }
|
---|
| 284 | bw.write(" </Index>\n");
|
---|
| 285 | bw.write("</Manifest>\n");
|
---|
| 286 | bw.close();
|
---|
| 287 | }
|
---|
| 288 | catch (Exception ex)
|
---|
| 289 | {
|
---|
| 290 | ex.printStackTrace();
|
---|
| 291 | return null; //Probably should return an actual error
|
---|
| 292 | }
|
---|
| 293 | }
|
---|
| 294 |
|
---|
[24855] | 295 | return runCommand(request, GS2PerlConstructor.IMPORT);
|
---|
[3681] | 296 | }
|
---|
[24855] | 297 |
|
---|
| 298 | protected Element processBuildCollection(Element request)
|
---|
| 299 | {
|
---|
[29869] | 300 | if (!userHasCollectionEditPermissions(request)) {
|
---|
| 301 | Document result_doc = XMLConverter.newDOM();
|
---|
| 302 | Element result = GSXML.createBasicResponse(result_doc, "processBuildCollection");
|
---|
| 303 | GSXML.addError(result, "This user does not have the required permissions to perform this action.");
|
---|
| 304 | return result;
|
---|
| 305 | }
|
---|
| 306 |
|
---|
[24855] | 307 | return runCommand(request, GS2PerlConstructor.BUILD);
|
---|
[3681] | 308 | }
|
---|
| 309 |
|
---|
[30617] | 310 | protected Element processModifyMetadata(Element request)
|
---|
[29869] | 311 | {
|
---|
| 312 | if (!userHasCollectionEditPermissions(request)) {
|
---|
| 313 | Document result_doc = XMLConverter.newDOM();
|
---|
[30617] | 314 | Element result = GSXML.createBasicResponse(result_doc, "processModifyMetadata");
|
---|
[29869] | 315 | GSXML.addError(result, "This user does not have the required permissions to perform this action.");
|
---|
| 316 | return result;
|
---|
| 317 | }
|
---|
| 318 |
|
---|
[30617] | 319 | return runCommand(request, GS2PerlConstructor.MODIFY_METADATA_SERVER);
|
---|
[29869] | 320 | }
|
---|
| 321 |
|
---|
[24855] | 322 | protected Element processActivateCollection(Element request)
|
---|
| 323 | {
|
---|
[29869] | 324 |
|
---|
| 325 | if (!userHasCollectionEditPermissions(request)) {
|
---|
| 326 | Document result_doc = XMLConverter.newDOM();
|
---|
| 327 | Element result = GSXML.createBasicResponse(result_doc, "processActivateCollection");
|
---|
| 328 | GSXML.addError(result, "This user does not have the required permissions to perform this action.");
|
---|
| 329 | return result;
|
---|
| 330 | }
|
---|
| 331 |
|
---|
[24855] | 332 | // this activates the collection on disk. but now we need to tell
|
---|
| 333 | // the MR about it. but we have to wait until the process is finished.
|
---|
| 334 | Element param_list = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
|
---|
[25635] | 335 | HashMap<String, Serializable> params = GSXML.extractParams(param_list, false);
|
---|
[24855] | 336 | String coll_name = (String) params.get(COL_PARAM);
|
---|
| 337 | String lang = request.getAttribute(GSXML.LANG_ATT);
|
---|
[24888] | 338 |
|
---|
[29947] | 339 | UserContext userContext = new UserContext(request);
|
---|
[24888] | 340 | String request_type = request.getAttribute(GSXML.TYPE_ATT);
|
---|
[29947] | 341 |
|
---|
| 342 | // now we de-activate the collection before running activate.pl, and then re-activate at end
|
---|
| 343 | // So activate.pl only does the moving, no activation. This way will prevent java from launching
|
---|
| 344 | // perl, exiting and then leaving dangling file handles (on index/text/col.gdb) in perl.
|
---|
| 345 | if (!request_type.equals(GSXML.REQUEST_TYPE_STATUS)) {
|
---|
| 346 | systemRequest("delete", coll_name, null, userContext); // deactivate collection
|
---|
| 347 | }
|
---|
[25540] | 348 |
|
---|
[29947] | 349 | Element response = runCommand(request, GS2PerlConstructor.ACTIVATE); // if request is for STATUS, then this won't run activate.pl
|
---|
[24888] | 350 |
|
---|
[29947] | 351 | Element status = (Element) GSXML.getChildByTagName(response, GSXML.STATUS_ELEM);
|
---|
| 352 |
|
---|
[24855] | 353 | // check for finished
|
---|
| 354 | int status_code = Integer.parseInt(status.getAttribute(GSXML.STATUS_ERROR_CODE_ATT));
|
---|
| 355 | if (GSStatus.isCompleted(status_code) && GSStatus.isError(status_code))
|
---|
| 356 | {
|
---|
| 357 | // we shouldn't carry out the next bit, just return the response
|
---|
| 358 | return response;
|
---|
| 359 | }
|
---|
| 360 | String id = status.getAttribute(GSXML.STATUS_PROCESS_ID_ATT);
|
---|
[25635] | 361 | GS2PerlListener listener = this.listeners.get(id);
|
---|
[24855] | 362 | if (listener == null)
|
---|
| 363 | {
|
---|
| 364 | logger.error("somethings gone wrong, couldn't find the listener");
|
---|
| 365 | status.setAttribute(GSXML.STATUS_ERROR_CODE_ATT, Integer.toString(GSStatus.ERROR));
|
---|
| 366 | return response;
|
---|
| 367 | }
|
---|
[25067] | 368 |
|
---|
[24855] | 369 | while (!GSStatus.isCompleted(status_code))
|
---|
| 370 | {
|
---|
| 371 | // wait for the process, and keep checking the status code
|
---|
| 372 | // there is probably a better way to do this.
|
---|
| 373 | try
|
---|
| 374 | {
|
---|
| 375 | Thread.currentThread().sleep(100);
|
---|
| 376 | }
|
---|
| 377 | catch (Exception e)
|
---|
| 378 | { // ignore
|
---|
| 379 | }
|
---|
| 380 | status_code = listener.getStatus();
|
---|
| 381 | }
|
---|
[3681] | 382 |
|
---|
[24855] | 383 | // add the rest of the messages to the status node
|
---|
[28966] | 384 | Text t = status.getOwnerDocument().createTextNode("\n" + listener.getUpdate());
|
---|
[24855] | 385 | status.appendChild(t);
|
---|
| 386 | status.setAttribute(GSXML.STATUS_ERROR_CODE_ATT, Integer.toString(listener.getStatus()));
|
---|
| 387 | if (GSStatus.isError(status_code))
|
---|
| 388 | {
|
---|
| 389 | return response; // without doing the next bit
|
---|
| 390 | }
|
---|
| 391 |
|
---|
[28966] | 392 | t = status.getOwnerDocument().createTextNode("\n");
|
---|
[24855] | 393 | status.appendChild(t);
|
---|
| 394 | // once have got here, we assume
|
---|
[29947] | 395 | // the first bit proceeded successfully, now reload the collection (sends a collection reactivation request)
|
---|
[24993] | 396 | systemRequest("reload", coll_name, status, userContext); // this will append more messages to the status, and overwrite the error code att
|
---|
[24855] | 397 | return response;
|
---|
| 398 |
|
---|
[3681] | 399 | }
|
---|
[24855] | 400 |
|
---|
| 401 | protected Element processDeleteCollection(Element request)
|
---|
| 402 | {
|
---|
[29869] | 403 | if (!userHasCollectionEditPermissions(request)) {
|
---|
| 404 | Document result_doc = XMLConverter.newDOM();
|
---|
| 405 | Element result = GSXML.createBasicResponse(result_doc, "processDeleteCollection");
|
---|
| 406 | GSXML.addError(result, "This user does not have the required permissions to perform this action.");
|
---|
| 407 | return result;
|
---|
| 408 | }
|
---|
| 409 |
|
---|
[28966] | 410 | Document result_doc = XMLConverter.newDOM();
|
---|
[24855] | 411 | // the response to send back
|
---|
| 412 | String name = GSPath.getFirstLink(request.getAttribute(GSXML.TO_ATT));
|
---|
[28966] | 413 | Element response = result_doc.createElement(GSXML.RESPONSE_ELEM);
|
---|
[24855] | 414 | response.setAttribute(GSXML.FROM_ATT, name);
|
---|
[28966] | 415 | Element status = result_doc.createElement(GSXML.STATUS_ELEM);
|
---|
[24855] | 416 | response.appendChild(status);
|
---|
| 417 | Text t = null; // the text node for the error/success message
|
---|
| 418 | String lang = request.getAttribute(GSXML.LANG_ATT);
|
---|
| 419 | String request_type = request.getAttribute(GSXML.TYPE_ATT);
|
---|
| 420 |
|
---|
| 421 | Element param_list = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
|
---|
[25635] | 422 | HashMap<String, Serializable> params = GSXML.extractParams(param_list, false);
|
---|
[24855] | 423 |
|
---|
| 424 | boolean get_status_only = false;
|
---|
| 425 | if (request_type.equals(GSXML.REQUEST_TYPE_STATUS))
|
---|
| 426 | {
|
---|
| 427 | get_status_only = true;
|
---|
| 428 | }
|
---|
| 429 | if (get_status_only)
|
---|
| 430 | {
|
---|
| 431 | // 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
|
---|
| 432 | logger.error("had a status request for delete - this shouldn't happen!!");
|
---|
[28966] | 433 | //t = result_doc.createTextNode("");
|
---|
[24855] | 434 | //status.appendChild(t);
|
---|
| 435 | status.setAttribute(GSXML.STATUS_ERROR_CODE_ATT, Integer.toString(GSStatus.ERROR));
|
---|
| 436 | return response;
|
---|
| 437 | }
|
---|
| 438 | String coll_name = (String) params.get(COL_PARAM);
|
---|
| 439 | String[] args = { coll_name };
|
---|
| 440 | File coll_dir = new File(GSFile.collectionBaseDir(this.site_home, coll_name));
|
---|
| 441 | // check that the coll is there in the first place
|
---|
| 442 | if (!coll_dir.exists())
|
---|
| 443 | {
|
---|
[28966] | 444 | t = result_doc.createTextNode(getTextString("delete.exists_error", lang, args));
|
---|
[24855] | 445 | status.appendChild(t);
|
---|
| 446 | status.setAttribute(GSXML.STATUS_ERROR_CODE_ATT, Integer.toString(GSStatus.ERROR));
|
---|
| 447 | return response;
|
---|
| 448 | }
|
---|
| 449 |
|
---|
| 450 | // try to delete the directory
|
---|
| 451 | if (!GSFile.deleteFile(coll_dir))
|
---|
| 452 | {
|
---|
[28966] | 453 | t = result_doc.createTextNode(getTextString("delete.delete_error", lang, args));
|
---|
[24855] | 454 | status.setAttribute(GSXML.STATUS_ERROR_CODE_ATT, Integer.toString(GSStatus.ERROR));
|
---|
| 455 | status.appendChild(t);
|
---|
| 456 | return response;
|
---|
| 457 | }
|
---|
[25067] | 458 |
|
---|
[24993] | 459 | UserContext userContext = new UserContext(request);
|
---|
| 460 |
|
---|
| 461 | systemRequest("delete", coll_name, status, userContext);
|
---|
[24855] | 462 | return response;
|
---|
[3681] | 463 | }
|
---|
| 464 |
|
---|
[24855] | 465 | protected Element processReloadCollection(Element request)
|
---|
| 466 | {
|
---|
[29869] | 467 | if (!userHasCollectionEditPermissions(request)) {
|
---|
| 468 | Document result_doc = XMLConverter.newDOM();
|
---|
| 469 | Element result = GSXML.createBasicResponse(result_doc, "processReloadCollection");
|
---|
| 470 | GSXML.addError(result, "This user does not have the required permissions to perform this action.");
|
---|
| 471 | return result;
|
---|
| 472 | }
|
---|
| 473 |
|
---|
[28966] | 474 | Document result_doc = XMLConverter.newDOM();
|
---|
[24855] | 475 | // the response to send back
|
---|
| 476 | String name = GSPath.getFirstLink(request.getAttribute(GSXML.TO_ATT));
|
---|
[28966] | 477 | Element response = result_doc.createElement(GSXML.RESPONSE_ELEM);
|
---|
[24855] | 478 | response.setAttribute(GSXML.FROM_ATT, name);
|
---|
[28966] | 479 | Element status = result_doc.createElement(GSXML.STATUS_ELEM);
|
---|
[24855] | 480 | response.appendChild(status);
|
---|
| 481 | Text t = null; // the text node for the error/success message
|
---|
| 482 |
|
---|
| 483 | String lang = request.getAttribute(GSXML.LANG_ATT);
|
---|
| 484 | String request_type = request.getAttribute(GSXML.TYPE_ATT);
|
---|
| 485 |
|
---|
| 486 | Element param_list = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
|
---|
[25635] | 487 | HashMap<String, Serializable> params = GSXML.extractParams(param_list, false);
|
---|
[24855] | 488 |
|
---|
| 489 | boolean get_status_only = false;
|
---|
| 490 | if (request_type.equals(GSXML.REQUEST_TYPE_STATUS))
|
---|
| 491 | {
|
---|
| 492 | get_status_only = true;
|
---|
| 493 | }
|
---|
| 494 | if (get_status_only)
|
---|
| 495 | {
|
---|
| 496 | // reload is synchronous - this makes no sense
|
---|
| 497 | logger.error("had a status request for reload - this shouldn't happen!!");
|
---|
[28966] | 498 | //t = result_doc.createTextNode("");
|
---|
[24855] | 499 | //status.appendChild(t);
|
---|
| 500 | status.setAttribute(GSXML.STATUS_ERROR_CODE_ATT, Integer.toString(GSStatus.ERROR));
|
---|
| 501 | return response;
|
---|
| 502 | }
|
---|
| 503 |
|
---|
| 504 | String coll_name = (String) params.get(COL_PARAM);
|
---|
[25067] | 505 |
|
---|
[24993] | 506 | UserContext userContext = new UserContext(request);
|
---|
| 507 |
|
---|
| 508 | systemRequest("reload", coll_name, status, userContext);
|
---|
[24855] | 509 | return response;
|
---|
| 510 |
|
---|
[3649] | 511 | }
|
---|
| 512 |
|
---|
[24855] | 513 | /**
|
---|
| 514 | * send a configure request to the message router action name should be
|
---|
| 515 | * "delete" or "reload" response will be put into the status element
|
---|
| 516 | */
|
---|
[25550] | 517 | protected void systemRequest(String operation, String coll_name, Element status, UserContext userContext)
|
---|
[24855] | 518 | {
|
---|
| 519 | // send the request to the MR
|
---|
[28966] | 520 | Document doc = XMLConverter.newDOM();
|
---|
| 521 | Element message = doc.createElement(GSXML.MESSAGE_ELEM);
|
---|
| 522 | Element request = GSXML.createBasicRequest(doc, GSXML.REQUEST_TYPE_SYSTEM, "", userContext);
|
---|
[24855] | 523 | message.appendChild(request);
|
---|
[28966] | 524 | Element command = doc.createElement(GSXML.SYSTEM_ELEM);
|
---|
[24855] | 525 | request.appendChild(command);
|
---|
| 526 | command.setAttribute(GSXML.SYSTEM_MODULE_TYPE_ATT, GSXML.COLLECTION_ELEM);
|
---|
| 527 | command.setAttribute(GSXML.SYSTEM_MODULE_NAME_ATT, coll_name);
|
---|
| 528 |
|
---|
[25550] | 529 | if (operation.equals("delete"))
|
---|
[24855] | 530 | {
|
---|
| 531 | command.setAttribute(GSXML.TYPE_ATT, GSXML.SYSTEM_TYPE_DEACTIVATE);
|
---|
| 532 | }
|
---|
[25550] | 533 | else if (operation.equals("reload"))
|
---|
[24855] | 534 | {
|
---|
| 535 | command.setAttribute(GSXML.TYPE_ATT, GSXML.SYSTEM_TYPE_ACTIVATE);
|
---|
| 536 | }
|
---|
| 537 | else
|
---|
| 538 | {
|
---|
[25550] | 539 | logger.error("invalid action name passed to systemRequest:" + operation);
|
---|
[24855] | 540 | return;
|
---|
| 541 | }
|
---|
| 542 | request.appendChild(command);
|
---|
| 543 | Node response = this.router.process(message); // at the moment, get no info in response so ignore it
|
---|
| 544 | Text t;
|
---|
| 545 | String[] args = { coll_name };
|
---|
| 546 |
|
---|
[24888] | 547 | if (status != null)
|
---|
[24855] | 548 | {
|
---|
[24888] | 549 | if (response == null)
|
---|
| 550 | {
|
---|
[28966] | 551 | t = status.getOwnerDocument().createTextNode(getTextString(operation + ".configure_error", userContext.getLanguage(), args));
|
---|
[24888] | 552 | status.setAttribute(GSXML.STATUS_ERROR_CODE_ATT, Integer.toString(GSStatus.ERROR));
|
---|
| 553 | status.appendChild(t);
|
---|
| 554 | return;
|
---|
| 555 | }
|
---|
| 556 |
|
---|
| 557 | // if we got here, we have succeeded!
|
---|
[28966] | 558 | t = status.getOwnerDocument().createTextNode(getTextString(operation + ".success", userContext.getLanguage(), args));
|
---|
[24888] | 559 | status.setAttribute(GSXML.STATUS_ERROR_CODE_ATT, Integer.toString(GSStatus.SUCCESS));
|
---|
[24855] | 560 | status.appendChild(t);
|
---|
| 561 | }
|
---|
[10093] | 562 | }
|
---|
| 563 |
|
---|
[24855] | 564 | /**
|
---|
| 565 | * configure the service module for now, all services have type=build - need
|
---|
| 566 | * to think about this
|
---|
| 567 | */
|
---|
| 568 | public boolean configure(Element info, Element extra_info)
|
---|
| 569 | {
|
---|
| 570 | if (!super.configure(info, extra_info))
|
---|
| 571 | {
|
---|
| 572 | return false;
|
---|
| 573 | }
|
---|
[3649] | 574 |
|
---|
[24855] | 575 | logger.info("configuring GS2Construct");
|
---|
[3649] | 576 |
|
---|
[24855] | 577 | Element e = null;
|
---|
| 578 | // hard code in the services for now
|
---|
[3649] | 579 |
|
---|
[24855] | 580 | // set up short_service_info_ - for now just has name and type
|
---|
[3649] | 581 |
|
---|
[28966] | 582 | e = this.desc_doc.createElement(GSXML.SERVICE_ELEM);
|
---|
[24855] | 583 | e.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_PROCESS);
|
---|
| 584 | e.setAttribute(GSXML.NAME_ATT, NEW_SERVICE);
|
---|
| 585 | this.short_service_info.appendChild(e);
|
---|
[3649] | 586 |
|
---|
[28966] | 587 | e = this.desc_doc.createElement(GSXML.SERVICE_ELEM);
|
---|
[24855] | 588 | e.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_PROCESS);
|
---|
| 589 | e.setAttribute(GSXML.NAME_ATT, IMPORT_SERVICE);
|
---|
| 590 | this.short_service_info.appendChild(e);
|
---|
[3649] | 591 |
|
---|
[28966] | 592 | e = this.desc_doc.createElement(GSXML.SERVICE_ELEM);
|
---|
[24855] | 593 | e.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_PROCESS);
|
---|
| 594 | e.setAttribute(GSXML.NAME_ATT, BUILD_SERVICE);
|
---|
| 595 | this.short_service_info.appendChild(e);
|
---|
[3649] | 596 |
|
---|
[28966] | 597 | e = this.desc_doc.createElement(GSXML.SERVICE_ELEM);
|
---|
[24855] | 598 | e.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_PROCESS);
|
---|
| 599 | e.setAttribute(GSXML.NAME_ATT, ACTIVATE_SERVICE);
|
---|
| 600 | this.short_service_info.appendChild(e);
|
---|
[3649] | 601 |
|
---|
[28966] | 602 | e = this.desc_doc.createElement(GSXML.SERVICE_ELEM);
|
---|
[24855] | 603 | e.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_PROCESS);
|
---|
[25540] | 604 | e.setAttribute(GSXML.NAME_ATT, BUILD_AND_ACTIVATE_SERVICE);
|
---|
| 605 | this.short_service_info.appendChild(e);
|
---|
| 606 |
|
---|
[28966] | 607 | e = this.desc_doc.createElement(GSXML.SERVICE_ELEM);
|
---|
[25540] | 608 | e.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_PROCESS);
|
---|
[24855] | 609 | e.setAttribute(GSXML.NAME_ATT, DELETE_SERVICE);
|
---|
| 610 | this.short_service_info.appendChild(e);
|
---|
[3649] | 611 |
|
---|
[28966] | 612 | e = this.desc_doc.createElement(GSXML.SERVICE_ELEM);
|
---|
[24855] | 613 | e.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_PROCESS);
|
---|
| 614 | e.setAttribute(GSXML.NAME_ATT, RELOAD_SERVICE);
|
---|
| 615 | this.short_service_info.appendChild(e);
|
---|
[3649] | 616 |
|
---|
[28966] | 617 | //e = this.desc_doc.createElement(GSXML.SERVICE_ELEM);
|
---|
[24855] | 618 | //e.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_PROCESS);
|
---|
| 619 | //e.setAttribute(GSXML.NAME_ATT, ADD_DOC_SERVICE);
|
---|
| 620 | //this.short_service_info.appendChild(e);
|
---|
[3649] | 621 |
|
---|
[29869] | 622 | e = this.desc_doc.createElement(GSXML.SERVICE_ELEM);
|
---|
| 623 | e.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_PROCESS);
|
---|
[30617] | 624 | e.setAttribute(GSXML.NAME_ATT, MODIFY_METADATA_SERVICE);
|
---|
[29869] | 625 | this.short_service_info.appendChild(e);
|
---|
| 626 |
|
---|
[24855] | 627 | return true;
|
---|
| 628 | }
|
---|
[3649] | 629 |
|
---|
[24855] | 630 | /** returns a response element */
|
---|
| 631 | protected Element runCommand(Element request, int type)
|
---|
| 632 | {
|
---|
[28966] | 633 | Document result_doc = XMLConverter.newDOM();
|
---|
[24855] | 634 | // the response to send back
|
---|
| 635 | String name = GSPath.getFirstLink(request.getAttribute(GSXML.TO_ATT));
|
---|
[28966] | 636 | Element response = result_doc.createElement(GSXML.RESPONSE_ELEM);
|
---|
[24855] | 637 | response.setAttribute(GSXML.FROM_ATT, name);
|
---|
[28966] | 638 | Element status = result_doc.createElement(GSXML.STATUS_ELEM);
|
---|
[24855] | 639 | response.appendChild(status);
|
---|
[3681] | 640 |
|
---|
[24855] | 641 | String lang = request.getAttribute(GSXML.LANG_ATT);
|
---|
| 642 | String request_type = request.getAttribute(GSXML.TYPE_ATT);
|
---|
[3681] | 643 |
|
---|
[24855] | 644 | Element param_list = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
|
---|
[25635] | 645 | HashMap<String, Serializable> params = GSXML.extractParams(param_list, false);
|
---|
[24855] | 646 |
|
---|
| 647 | boolean get_status_only = false;
|
---|
| 648 | if (request_type.equals(GSXML.REQUEST_TYPE_STATUS))
|
---|
| 649 | {
|
---|
| 650 | get_status_only = true;
|
---|
[3649] | 651 | }
|
---|
[3681] | 652 |
|
---|
[24855] | 653 | // just check for status messages if that's all that's required
|
---|
| 654 | if (get_status_only)
|
---|
| 655 | {
|
---|
| 656 | String id = (String) params.get(PROCESS_ID_PARAM);
|
---|
| 657 | status.setAttribute(GSXML.STATUS_PROCESS_ID_ATT, id);
|
---|
[25635] | 658 | GS2PerlListener listener = this.listeners.get(id);
|
---|
[24855] | 659 | if (listener == null)
|
---|
| 660 | {
|
---|
[28966] | 661 | Text t = result_doc.createTextNode(getTextString("general.process_id_error", lang));
|
---|
[24855] | 662 | status.appendChild(t);
|
---|
| 663 | status.setAttribute(GSXML.STATUS_ERROR_CODE_ATT, Integer.toString(GSStatus.ERROR));
|
---|
| 664 | }
|
---|
| 665 | else
|
---|
| 666 | {
|
---|
[28966] | 667 | Text t = result_doc.createTextNode(listener.getUpdate());
|
---|
[24855] | 668 | status.appendChild(t);
|
---|
| 669 | status.setAttribute(GSXML.STATUS_ERROR_CODE_ATT, Integer.toString(listener.getStatus()));
|
---|
| 670 | // check that we actually should be removing the listener here
|
---|
| 671 | if (listener.isFinished())
|
---|
| 672 | { // remove this listener - its job is done
|
---|
| 673 | this.listeners.remove(id); // not working
|
---|
| 674 | }
|
---|
| 675 | }
|
---|
| 676 | return response;
|
---|
[3649] | 677 |
|
---|
[24855] | 678 | }
|
---|
[3649] | 679 |
|
---|
[29869] | 680 | // do the actual command
|
---|
[24855] | 681 | String coll_name = null;
|
---|
| 682 | if (type == GS2PerlConstructor.NEW)
|
---|
| 683 | {
|
---|
| 684 | String coll_title = (String) params.get(NEW_COL_TITLE_PARAM);
|
---|
| 685 | coll_name = createNewCollName(coll_title);
|
---|
| 686 | }
|
---|
| 687 | else
|
---|
| 688 | {
|
---|
| 689 | coll_name = (String) params.get(COL_PARAM);
|
---|
| 690 | }
|
---|
[25540] | 691 |
|
---|
[24855] | 692 | // makes a paramList of the relevant params
|
---|
| 693 | Element other_params = extractOtherParams(params, type);
|
---|
[3649] | 694 |
|
---|
[24855] | 695 | //create the constructor to do the work
|
---|
| 696 | GS2PerlConstructor constructor = new GS2PerlConstructor("perl_build");
|
---|
| 697 | if (!constructor.configure())
|
---|
| 698 | {
|
---|
[28966] | 699 | Text t = result_doc.createTextNode(getTextString("general.configure_constructor_error", lang));
|
---|
[24855] | 700 | status.appendChild(t);
|
---|
| 701 | status.setAttribute(GSXML.STATUS_ERROR_CODE_ATT, Integer.toString(GSStatus.ERROR));
|
---|
| 702 | return response;
|
---|
| 703 | }
|
---|
| 704 |
|
---|
| 705 | constructor.setSiteHome(this.site_home);
|
---|
| 706 | constructor.setCollectionName(coll_name);
|
---|
| 707 | constructor.setActionType(type);
|
---|
| 708 | constructor.setProcessParams(other_params);
|
---|
[25540] | 709 | if (type == GS2PerlConstructor.IMPORT)
|
---|
| 710 | {
|
---|
| 711 | constructor.setManifestFile(this.site_home + File.separator + "collect" + File.separator + params.get(COL_PARAM) + File.separator + "manifests" + File.separator + "tempManifest.xml");
|
---|
| 712 | }
|
---|
[30617] | 713 | else if (type == GS2PerlConstructor.MODIFY_METADATA_SERVER) {
|
---|
[29869] | 714 | StringBuffer querystring = new StringBuffer();
|
---|
| 715 |
|
---|
| 716 | // convert params into a single string again?
|
---|
| 717 | Set<Map.Entry<String, Serializable>> entries = params.entrySet();
|
---|
| 718 | Iterator<Map.Entry<String, Serializable>> i = entries.iterator();
|
---|
| 719 |
|
---|
[30516] | 720 | String oid = null;
|
---|
| 721 |
|
---|
| 722 | while (i.hasNext()) {
|
---|
[24888] | 723 |
|
---|
[30516] | 724 | Map.Entry<String, Serializable> entry = i.next();
|
---|
| 725 | String paramname = entry.getKey();
|
---|
| 726 | paramname = paramname.replace("s1.", ""); // replaces all
|
---|
| 727 | // occurrences
|
---|
| 728 | if (paramname.equals("collection")) {
|
---|
| 729 | paramname = "c";
|
---|
| 730 | }
|
---|
| 731 | if (paramname.equals("d")){
|
---|
| 732 | oid = (String) entry.getValue();
|
---|
| 733 | }
|
---|
| 734 | String paramvalue = (String) entry.getValue();
|
---|
| 735 |
|
---|
| 736 | querystring.append(paramname + "=" + paramvalue);
|
---|
| 737 | if (i.hasNext()) {
|
---|
| 738 | querystring.append("&");
|
---|
| 739 | }
|
---|
[29869] | 740 | }
|
---|
[30516] | 741 |
|
---|
[30704] | 742 | markDocumentInFlatDatabase("R", coll_name, OID.getTop(oid));
|
---|
[30516] | 743 |
|
---|
[29869] | 744 | constructor.setQueryString(querystring.toString());
|
---|
| 745 | }
|
---|
| 746 |
|
---|
[25540] | 747 | GS2PerlListener listener = new GS2PerlListener();
|
---|
[24855] | 748 | constructor.addListener(listener);
|
---|
| 749 | constructor.start();
|
---|
| 750 |
|
---|
| 751 | String id = newID();
|
---|
| 752 | this.listeners.put(id, listener);
|
---|
| 753 |
|
---|
| 754 | status.setAttribute(GSXML.STATUS_PROCESS_ID_ATT, id);
|
---|
| 755 | status.setAttribute(GSXML.STATUS_ERROR_CODE_ATT, Integer.toString(GSStatus.ACCEPTED));
|
---|
[28966] | 756 | Text t = result_doc.createTextNode(getTextString("general.process_start", lang));
|
---|
[24855] | 757 | status.appendChild(t);
|
---|
| 758 | return response;
|
---|
[3649] | 759 | }
|
---|
| 760 |
|
---|
[24855] | 761 | //************************
|
---|
| 762 | // some helper functions
|
---|
| 763 | //************************
|
---|
[3649] | 764 |
|
---|
[24855] | 765 | /** parse the collect directory and return a list of collection names */
|
---|
| 766 | protected String[] getCollectionList()
|
---|
| 767 | {
|
---|
[3649] | 768 |
|
---|
[24855] | 769 | File collectDir = new File(GSFile.collectDir(this.site_home));
|
---|
| 770 | if (!collectDir.exists())
|
---|
| 771 | {
|
---|
| 772 | logger.error("couldn't find collect dir: " + collectDir.toString());
|
---|
| 773 | return null;
|
---|
| 774 | }
|
---|
| 775 | logger.info("GS2Construct: reading thru directory " + collectDir.getPath() + " to find collections.");
|
---|
| 776 | File[] contents = collectDir.listFiles();
|
---|
| 777 | int num_colls = 0;
|
---|
| 778 | for (int i = 0; i < contents.length; i++)
|
---|
| 779 | {
|
---|
| 780 | if (contents[i].isDirectory() && !contents[i].getName().startsWith("CVS"))
|
---|
| 781 | {
|
---|
| 782 | num_colls++;
|
---|
| 783 | }
|
---|
| 784 | }
|
---|
[3649] | 785 |
|
---|
[24855] | 786 | String[] names = new String[num_colls];
|
---|
[3649] | 787 |
|
---|
[24855] | 788 | for (int i = 0, j = 0; i < contents.length; i++)
|
---|
| 789 | {
|
---|
| 790 | if (contents[i].isDirectory())
|
---|
| 791 | {
|
---|
| 792 | String colName = contents[i].getName();
|
---|
| 793 | if (!colName.startsWith("CVS"))
|
---|
| 794 | {
|
---|
| 795 | names[j] = colName;
|
---|
| 796 | j++;
|
---|
| 797 | }
|
---|
[3649] | 798 |
|
---|
[24855] | 799 | }
|
---|
| 800 | }
|
---|
| 801 |
|
---|
| 802 | return names;
|
---|
| 803 |
|
---|
[3649] | 804 | }
|
---|
[24855] | 805 |
|
---|
| 806 | /** ids used for process id */
|
---|
| 807 | private int current_id = 0;
|
---|
| 808 |
|
---|
| 809 | private String newID()
|
---|
| 810 | {
|
---|
| 811 | current_id++;
|
---|
| 812 | return Integer.toString(current_id);
|
---|
[3649] | 813 | }
|
---|
[24855] | 814 |
|
---|
| 815 | /** creates a new short name from the collection title */
|
---|
| 816 | protected String createNewCollName(String coll_title)
|
---|
| 817 | {
|
---|
| 818 |
|
---|
| 819 | String base_name = null;
|
---|
| 820 | // take the first 6 letters
|
---|
| 821 | if (coll_title.length() < 6)
|
---|
| 822 | {
|
---|
| 823 | base_name = coll_title;
|
---|
[3649] | 824 | }
|
---|
[24855] | 825 | else
|
---|
| 826 | {
|
---|
| 827 | base_name = coll_title.substring(0, 6);
|
---|
| 828 | }
|
---|
| 829 | File coll_dir = new File(GSFile.collectionBaseDir(this.site_home, base_name));
|
---|
| 830 | if (!coll_dir.exists())
|
---|
| 831 | { // this name is ok - not used yet
|
---|
| 832 | return base_name;
|
---|
| 833 | }
|
---|
[3681] | 834 |
|
---|
[24855] | 835 | // now we have to make a new name until we get a good one
|
---|
| 836 | // try name1, name2 name3 etc
|
---|
| 837 | int i = 0;
|
---|
| 838 | while (coll_dir.exists())
|
---|
| 839 | {
|
---|
| 840 | i++;
|
---|
| 841 | coll_dir = new File(GSFile.collectionBaseDir(this.site_home, base_name + Integer.toString(i)));
|
---|
| 842 | }
|
---|
| 843 | return base_name + Integer.toString(i);
|
---|
[3681] | 844 |
|
---|
| 845 | }
|
---|
| 846 |
|
---|
[24855] | 847 | /**
|
---|
| 848 | * takes the params from the request (in the HashMap) and extracts any that
|
---|
| 849 | * need to be passed to the constructor and puts them into a paramList
|
---|
| 850 | * element
|
---|
| 851 | */
|
---|
[25635] | 852 | protected Element extractOtherParams(HashMap<String, Serializable> params, int type)
|
---|
[24855] | 853 | {
|
---|
[28966] | 854 | Document doc = XMLConverter.newDOM();
|
---|
| 855 | Element param_list = doc.createElement(GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
|
---|
[24855] | 856 | if (type == GS2PerlConstructor.NEW)
|
---|
| 857 | {
|
---|
[28966] | 858 | Element param = doc.createElement(GSXML.PARAM_ELEM);
|
---|
[24855] | 859 | param.setAttribute(GSXML.NAME_ATT, "creator");
|
---|
| 860 | param.setAttribute(GSXML.VALUE_ATT, (String) params.get(CREATOR_PARAM));
|
---|
| 861 |
|
---|
| 862 | param_list.appendChild(param);
|
---|
[28966] | 863 | param = doc.createElement(GSXML.PARAM_ELEM);
|
---|
[24855] | 864 | param.setAttribute(GSXML.NAME_ATT, "about");
|
---|
| 865 | param.setAttribute(GSXML.VALUE_ATT, (String) params.get(NEW_COL_ABOUT_PARAM));
|
---|
| 866 | param_list.appendChild(param);
|
---|
[28966] | 867 | param = doc.createElement(GSXML.PARAM_ELEM);
|
---|
[24855] | 868 | param.setAttribute(GSXML.NAME_ATT, "title");
|
---|
| 869 | param.setAttribute(GSXML.VALUE_ATT, (String) params.get(NEW_COL_TITLE_PARAM));
|
---|
| 870 | param_list.appendChild(param);
|
---|
[28966] | 871 | param = doc.createElement(GSXML.PARAM_ELEM);
|
---|
[24855] | 872 | param.setAttribute(GSXML.NAME_ATT, "buildtype");
|
---|
| 873 | param.setAttribute(GSXML.VALUE_ATT, (String) params.get(BUILDTYPE_PARAM));
|
---|
| 874 | param_list.appendChild(param);
|
---|
| 875 | return param_list;
|
---|
| 876 | }
|
---|
| 877 |
|
---|
| 878 | // other ones dont have params yet
|
---|
| 879 | return null;
|
---|
[3681] | 880 | }
|
---|
[25648] | 881 |
|
---|
[25540] | 882 | protected void waitUntilReady(Element request)
|
---|
| 883 | {
|
---|
| 884 | Element param_list = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
|
---|
[25635] | 885 | HashMap<String, Serializable> params = GSXML.extractParams(param_list, false);
|
---|
[3681] | 886 |
|
---|
[27717] | 887 | String collection = (String) params.get(COL_PARAM);
|
---|
| 888 |
|
---|
[25648] | 889 | if (checkCollectionIsNotBusy(collection))
|
---|
[25540] | 890 | {
|
---|
| 891 | return;
|
---|
| 892 | }
|
---|
| 893 |
|
---|
[25648] | 894 | while (collectionOperationMap.get(collection) != null)
|
---|
[25540] | 895 | {
|
---|
| 896 | try
|
---|
| 897 | {
|
---|
| 898 | Thread.currentThread().sleep(1000);
|
---|
| 899 | }
|
---|
[25648] | 900 | catch (Exception ex)
|
---|
[25540] | 901 | {
|
---|
| 902 | ex.printStackTrace();
|
---|
| 903 | }
|
---|
| 904 | }
|
---|
| 905 | }
|
---|
[25648] | 906 |
|
---|
[25540] | 907 | protected void signalReady(Element request)
|
---|
| 908 | {
|
---|
| 909 | Element param_list = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
|
---|
[25635] | 910 | HashMap<String, Serializable> params = GSXML.extractParams(param_list, false);
|
---|
[25540] | 911 |
|
---|
[27717] | 912 | String collection = (String) params.get(COL_PARAM);
|
---|
| 913 |
|
---|
[25540] | 914 | collectionOperationMap.remove(collection);
|
---|
| 915 | }
|
---|
[25648] | 916 |
|
---|
[25540] | 917 | protected synchronized boolean checkCollectionIsNotBusy(String collection)
|
---|
| 918 | {
|
---|
[25648] | 919 | if (collectionOperationMap.get(collection) == null)
|
---|
[25540] | 920 | {
|
---|
| 921 | collectionOperationMap.put(collection, true);
|
---|
| 922 | return true;
|
---|
| 923 | }
|
---|
| 924 | return false;
|
---|
| 925 | }
|
---|
[29869] | 926 |
|
---|
| 927 |
|
---|
| 928 | /** Copy from DebugService.userHasEditPermissions
|
---|
| 929 | This function checks that the user is logged in and that the user
|
---|
| 930 | is in the right group to edit the collection */
|
---|
| 931 | protected boolean userHasCollectionEditPermissions(Element request) {
|
---|
| 932 | Element param_list = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
|
---|
| 933 | HashMap<String, Serializable> params = GSXML.extractParams(param_list, false);
|
---|
| 934 | String collection = (String) params.get(COL_PARAM); // could be null on newcoll operation
|
---|
| 935 |
|
---|
| 936 | UserContext context = new UserContext(request);
|
---|
| 937 | if(collection == null) {
|
---|
| 938 | return !context.getUsername().equals("");
|
---|
| 939 | }
|
---|
| 940 | for (String group : context.getGroups()) {
|
---|
| 941 | // administrator always has permission
|
---|
| 942 | if (group.equals("administrator")) {
|
---|
| 943 | return true;
|
---|
| 944 | }
|
---|
| 945 | // all-collections-editor can edit any collection
|
---|
| 946 | if (!collection.equals("")) {
|
---|
| 947 | if (group.equals("all-collections-editor")) {
|
---|
| 948 | return true;
|
---|
| 949 | }
|
---|
| 950 | if (group.equals(collection+"-collection-editor")) {
|
---|
| 951 | return true;
|
---|
| 952 | }
|
---|
| 953 | }
|
---|
| 954 | }
|
---|
| 955 | // haven't found a group with edit permissions
|
---|
| 956 | return false;
|
---|
| 957 |
|
---|
| 958 | }
|
---|
[30516] | 959 | protected void markDocumentInFlatDatabase(String mark, String collection, String oid) {
|
---|
| 960 |
|
---|
| 961 | Document msg_doc = XMLConverter.newDOM();
|
---|
| 962 | Element message = msg_doc.createElement(GSXML.MESSAGE_ELEM);
|
---|
| 963 | UserContext userContext = new UserContext();
|
---|
| 964 | Element query_request = GSXML.createBasicRequest(msg_doc, GSXML.REQUEST_TYPE_DESCRIBE , collection, userContext);
|
---|
| 965 | message.appendChild(query_request);
|
---|
| 966 | Element result = (Element) this.router.process(message);
|
---|
| 967 | Element resp_elem = (Element) GSXML.getChildByTagName(result, GSXML.RESPONSE_ELEM);
|
---|
| 968 | Element coll_elem = (Element) GSXML.getChildByTagName(resp_elem, GSXML.COLLECTION_ELEM);
|
---|
| 969 | String dbtype = coll_elem.getAttribute(GSXML.DB_TYPE_ATT);
|
---|
| 970 |
|
---|
| 971 | SimpleCollectionDatabase coll_db = new SimpleCollectionDatabase(dbtype);
|
---|
| 972 | if (!coll_db.databaseOK())
|
---|
| 973 | {
|
---|
| 974 | logger.error("Couldn't create the collection database of type " + dbtype);
|
---|
| 975 | return;
|
---|
| 976 | }
|
---|
| 977 |
|
---|
[30616] | 978 | // Open database for reading. It may not exist if collection is pre-built without archives (such as demo collections)
|
---|
[30516] | 979 | String coll_db_file = GSFile.archivesDatabaseFile(this.site_home, collection, dbtype);
|
---|
| 980 | if (!coll_db.openDatabase(coll_db_file, SimpleCollectionDatabase.READ))
|
---|
| 981 | {
|
---|
[30617] | 982 | logger.error("Could not open collection archives database. Database doesn't exist or else somebody's already using it?");
|
---|
[30616] | 983 | return;
|
---|
[30516] | 984 | }
|
---|
[30616] | 985 | // now we know we have an archives folder
|
---|
[30516] | 986 | String old_value = coll_db.getValue(oid);
|
---|
| 987 | String new_value = old_value.replace("<index-status>B", "<index-status>" + mark);
|
---|
| 988 | // Close database for reading
|
---|
| 989 | coll_db.closeDatabase();
|
---|
| 990 | if (!coll_db.openDatabase(coll_db_file, SimpleCollectionDatabase.WRITE))
|
---|
| 991 | {
|
---|
| 992 | logger.error("Could not open collection archives database. Somebody already using this database!");
|
---|
[30617] | 993 | return;
|
---|
[30516] | 994 | }
|
---|
| 995 | coll_db.setValue(oid, new_value);
|
---|
| 996 | coll_db.closeDatabase();
|
---|
| 997 |
|
---|
| 998 | }
|
---|
[3649] | 999 | }
|
---|