[24509] | 1 | /*
|
---|
| 2 | * DocumentBasket.java
|
---|
| 3 | * Copyright (C) 2006 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 |
|
---|
| 20 | package org.greenstone.gsdl3.service;
|
---|
| 21 |
|
---|
| 22 | import java.sql.Statement;
|
---|
| 23 | import java.util.Hashtable;
|
---|
| 24 | import java.util.HashMap;
|
---|
| 25 | import java.util.ArrayList;
|
---|
| 26 | import java.util.HashSet;
|
---|
| 27 | import java.util.Iterator;
|
---|
| 28 |
|
---|
[28966] | 29 | import org.w3c.dom.Document;
|
---|
[24509] | 30 | import org.w3c.dom.Element;
|
---|
| 31 | import org.w3c.dom.NodeList;
|
---|
| 32 |
|
---|
| 33 | import org.greenstone.util.GlobalProperties;
|
---|
[25069] | 34 | import org.greenstone.gsdl3.util.GSParams;
|
---|
[24509] | 35 | import org.greenstone.gsdl3.util.GSXML;
|
---|
| 36 | import org.greenstone.gsdl3.util.GSPath;
|
---|
[24993] | 37 | import org.greenstone.gsdl3.util.UserContext;
|
---|
[28966] | 38 | import org.greenstone.gsdl3.util.XMLConverter;
|
---|
[24509] | 39 |
|
---|
| 40 | import java.net.InetAddress;
|
---|
| 41 | import java.util.Properties;
|
---|
| 42 | import java.util.Date;
|
---|
| 43 |
|
---|
| 44 | import javax.mail.*;
|
---|
| 45 | import javax.mail.internet.*;
|
---|
| 46 |
|
---|
| 47 | import java.awt.event.ActionEvent;
|
---|
| 48 | import java.awt.event.ActionListener;
|
---|
| 49 | import javax.swing.Timer;
|
---|
| 50 |
|
---|
| 51 | import java.io.*;
|
---|
| 52 |
|
---|
| 53 | import org.apache.log4j.*;
|
---|
| 54 |
|
---|
[24976] | 55 | public class DocumentBasket extends ServiceRack
|
---|
| 56 | {
|
---|
[24509] | 57 |
|
---|
[24976] | 58 | static Logger logger = Logger.getLogger(org.greenstone.gsdl3.service.DocumentBasket.class.getName());
|
---|
[24509] | 59 |
|
---|
[24976] | 60 | // the services on offer
|
---|
| 61 | // these strings must match what is found in the properties file
|
---|
| 62 | protected static final String ADD_ITEM_SERVICE = "AddDocument";
|
---|
| 63 | protected static final String DISPLAY_ITEMS_SERVICE = "DisplayDocumentList";
|
---|
| 64 | protected static final String ITEM_NUM_SERVICE = "GetDocuments";
|
---|
| 65 | protected static final String DELETE_ITEMS_SERVICE = "DeleteDocuments";
|
---|
| 66 | protected static final String DELETE_ITEM_SERVICE = "DeleteDocument";
|
---|
[25069] | 67 | protected static final String CLEAR_ITEMS_SERVICE = "ClearDocuments";
|
---|
[24509] | 68 | //added
|
---|
| 69 | protected static final String MERGE_ITEM_SERVICE = "MergeDocument";
|
---|
| 70 | protected static final String ITEM_PARAM = "item";
|
---|
[24976] | 71 | protected static final String delimiter = "|";
|
---|
| 72 | protected static final int delay = 1800000;
|
---|
[24509] | 73 |
|
---|
[24976] | 74 | protected static final String BASKET_BOOK = "documentBasketBook";
|
---|
[24509] | 75 |
|
---|
[25635] | 76 | protected Hashtable<String, Hashtable<String, Hashtable<String, Item>>> userMap = null;
|
---|
| 77 | protected Hashtable<String, UserTimer> timerMap = null;
|
---|
[24976] | 78 | protected String username = "";
|
---|
| 79 | protected String password = "";
|
---|
[24509] | 80 |
|
---|
[24976] | 81 | /** constructor */
|
---|
| 82 | public DocumentBasket()
|
---|
[24509] | 83 | {
|
---|
[25635] | 84 | userMap = new Hashtable<String, Hashtable<String, Hashtable<String, Item>>>();
|
---|
| 85 | timerMap = new Hashtable<String, UserTimer>();
|
---|
[24976] | 86 | }
|
---|
| 87 |
|
---|
[25635] | 88 | private Hashtable<String, Hashtable<String, Item>> updateDocMap(Element request)
|
---|
[24976] | 89 | {
|
---|
[24509] | 90 | String id = request.getAttribute("uid");
|
---|
| 91 |
|
---|
[24976] | 92 | if (userMap.containsKey(id))
|
---|
| 93 | {
|
---|
| 94 | if (timerMap.containsKey(id))
|
---|
| 95 | {
|
---|
[25635] | 96 | UserTimer timer = timerMap.get(id);
|
---|
[24509] | 97 | timer.restart();
|
---|
[24976] | 98 | }
|
---|
[25635] | 99 | return userMap.get(id);
|
---|
[24509] | 100 | }
|
---|
| 101 | else
|
---|
| 102 | {
|
---|
[24976] | 103 | UserTimer timer = new UserTimer(delay, id);
|
---|
| 104 | timerMap.put(id, timer);
|
---|
[24509] | 105 | timer.start();
|
---|
[25635] | 106 | Hashtable<String, Hashtable<String, Item>> newDocs = new Hashtable<String, Hashtable<String, Item>>();
|
---|
[24976] | 107 | userMap.put(id, newDocs);
|
---|
[24509] | 108 | return newDocs;
|
---|
[24976] | 109 | }
|
---|
[24509] | 110 | }
|
---|
| 111 |
|
---|
[24976] | 112 | /** configure this service */
|
---|
| 113 | public boolean configure(Element info, Element extra_info)
|
---|
| 114 | {
|
---|
[24509] | 115 | logger.info("Configuring DocumentBasket...");
|
---|
| 116 | this.config_info = info;
|
---|
| 117 |
|
---|
| 118 | // set up short_service_info_ - for now just has name and type
|
---|
[28966] | 119 | Element add_service = this.desc_doc.createElement(GSXML.SERVICE_ELEM);
|
---|
[24509] | 120 | add_service.setAttribute(GSXML.TYPE_ATT, "gather"); // what??
|
---|
| 121 | add_service.setAttribute(GSXML.NAME_ATT, ADD_ITEM_SERVICE);
|
---|
| 122 | this.short_service_info.appendChild(add_service);
|
---|
| 123 |
|
---|
| 124 | // set up short_service_info_ - for now just has name and type
|
---|
[28966] | 125 | Element disp_service = this.desc_doc.createElement(GSXML.SERVICE_ELEM);
|
---|
[24509] | 126 | disp_service.setAttribute(GSXML.TYPE_ATT, "gather"); // what??
|
---|
| 127 | disp_service.setAttribute(GSXML.NAME_ATT, DISPLAY_ITEMS_SERVICE);
|
---|
| 128 | this.short_service_info.appendChild(disp_service);
|
---|
| 129 |
|
---|
| 130 | // set up short_service_info_ - for now just has name and type
|
---|
[28966] | 131 | Element num_service = this.desc_doc.createElement(GSXML.SERVICE_ELEM);
|
---|
[24509] | 132 | num_service.setAttribute(GSXML.TYPE_ATT, "gather"); // what??
|
---|
| 133 | num_service.setAttribute(GSXML.NAME_ATT, ITEM_NUM_SERVICE);
|
---|
| 134 | this.short_service_info.appendChild(num_service);
|
---|
| 135 |
|
---|
[24976] | 136 | // set up short_service_info_ - for now just has name and type
|
---|
[28966] | 137 | Element delete_service = this.desc_doc.createElement(GSXML.SERVICE_ELEM);
|
---|
[24509] | 138 | delete_service.setAttribute(GSXML.TYPE_ATT, "gather"); // what??
|
---|
| 139 | delete_service.setAttribute(GSXML.NAME_ATT, DELETE_ITEMS_SERVICE);
|
---|
| 140 | this.short_service_info.appendChild(delete_service);
|
---|
| 141 |
|
---|
[24976] | 142 | // set up short_service_info_ - for now just has name and type
|
---|
[28966] | 143 | Element deleteone_service = this.desc_doc.createElement(GSXML.SERVICE_ELEM);
|
---|
[24509] | 144 | deleteone_service.setAttribute(GSXML.TYPE_ATT, "gather"); // what??
|
---|
| 145 | deleteone_service.setAttribute(GSXML.NAME_ATT, DELETE_ITEM_SERVICE);
|
---|
| 146 | this.short_service_info.appendChild(deleteone_service);
|
---|
[25069] | 147 |
|
---|
| 148 | // set up short_service_info_ - for now just has name and type
|
---|
[28966] | 149 | Element deleteall_service = this.desc_doc.createElement(GSXML.SERVICE_ELEM);
|
---|
[25069] | 150 | deleteall_service.setAttribute(GSXML.TYPE_ATT, "gather"); // what??
|
---|
| 151 | deleteall_service.setAttribute(GSXML.NAME_ATT, CLEAR_ITEMS_SERVICE);
|
---|
| 152 | this.short_service_info.appendChild(deleteall_service);
|
---|
[24976] | 153 |
|
---|
[24509] | 154 | // set up short_service_info_ - for now just has name and type
|
---|
[28966] | 155 | Element merge_service = this.desc_doc.createElement(GSXML.SERVICE_ELEM);
|
---|
[24509] | 156 | merge_service.setAttribute(GSXML.TYPE_ATT, "gather"); // what??
|
---|
| 157 | merge_service.setAttribute(GSXML.NAME_ATT, MERGE_ITEM_SERVICE);
|
---|
| 158 | this.short_service_info.appendChild(merge_service);
|
---|
| 159 |
|
---|
[24976] | 160 | return true;
|
---|
| 161 | }
|
---|
[24509] | 162 |
|
---|
[24976] | 163 | /** returns a specific service description */
|
---|
[28966] | 164 | protected Element getServiceDescription(Document doc, String service_id, String lang, String subset)
|
---|
[24509] | 165 | {
|
---|
[24976] | 166 | if (service_id.equals(ADD_ITEM_SERVICE))
|
---|
| 167 | {
|
---|
[28966] | 168 | Element add_service = doc.createElement(GSXML.SERVICE_ELEM);
|
---|
[24509] | 169 | add_service.setAttribute(GSXML.TYPE_ATT, "gather"); // what??
|
---|
| 170 | add_service.setAttribute(GSXML.NAME_ATT, ADD_ITEM_SERVICE);
|
---|
| 171 | return add_service;
|
---|
| 172 | }
|
---|
[24976] | 173 | if (service_id.equals(DISPLAY_ITEMS_SERVICE))
|
---|
| 174 | {
|
---|
| 175 |
|
---|
[28966] | 176 | Element disp_service = doc.createElement(GSXML.SERVICE_ELEM);
|
---|
[24509] | 177 | disp_service.setAttribute(GSXML.TYPE_ATT, "gather"); // what??
|
---|
| 178 | disp_service.setAttribute(GSXML.NAME_ATT, DISPLAY_ITEMS_SERVICE);
|
---|
| 179 | return disp_service;
|
---|
| 180 | }
|
---|
| 181 |
|
---|
[24976] | 182 | if (service_id.equals(ITEM_NUM_SERVICE))
|
---|
| 183 | {
|
---|
| 184 |
|
---|
[28966] | 185 | Element num_service = doc.createElement(GSXML.SERVICE_ELEM);
|
---|
[24509] | 186 | num_service.setAttribute(GSXML.TYPE_ATT, "gather"); // what??
|
---|
| 187 | num_service.setAttribute(GSXML.NAME_ATT, ITEM_NUM_SERVICE);
|
---|
| 188 | return num_service;
|
---|
| 189 | }
|
---|
| 190 |
|
---|
[24976] | 191 | if (service_id.equals(DELETE_ITEMS_SERVICE))
|
---|
| 192 | {
|
---|
| 193 |
|
---|
[28966] | 194 | Element del_service = doc.createElement(GSXML.SERVICE_ELEM);
|
---|
[24509] | 195 | del_service.setAttribute(GSXML.TYPE_ATT, "gather"); // what??
|
---|
| 196 | del_service.setAttribute(GSXML.NAME_ATT, DELETE_ITEMS_SERVICE);
|
---|
| 197 | return del_service;
|
---|
| 198 | }
|
---|
[25069] | 199 |
|
---|
| 200 | if (service_id.equals(CLEAR_ITEMS_SERVICE))
|
---|
| 201 | {
|
---|
[24509] | 202 |
|
---|
[28966] | 203 | Element clear_service = doc.createElement(GSXML.SERVICE_ELEM);
|
---|
[25069] | 204 | clear_service.setAttribute(GSXML.TYPE_ATT, "gather"); // what??
|
---|
| 205 | clear_service.setAttribute(GSXML.NAME_ATT, CLEAR_ITEMS_SERVICE);
|
---|
| 206 | return clear_service;
|
---|
| 207 | }
|
---|
| 208 |
|
---|
[24976] | 209 | if (service_id.equals(DELETE_ITEM_SERVICE))
|
---|
| 210 | {
|
---|
| 211 |
|
---|
[28966] | 212 | Element delone_service = doc.createElement(GSXML.SERVICE_ELEM);
|
---|
[24509] | 213 | delone_service.setAttribute(GSXML.TYPE_ATT, "gather"); // what??
|
---|
| 214 | delone_service.setAttribute(GSXML.NAME_ATT, DELETE_ITEM_SERVICE);
|
---|
| 215 | return delone_service;
|
---|
| 216 | }
|
---|
[24976] | 217 | if (service_id.equals(MERGE_ITEM_SERVICE))
|
---|
| 218 | {
|
---|
[28966] | 219 | Element merge_service = doc.createElement(GSXML.SERVICE_ELEM);
|
---|
[24509] | 220 | merge_service.setAttribute(GSXML.TYPE_ATT, "gather"); // what??
|
---|
| 221 | merge_service.setAttribute(GSXML.NAME_ATT, MERGE_ITEM_SERVICE);
|
---|
| 222 | return merge_service;
|
---|
| 223 | }
|
---|
| 224 | return null;
|
---|
[24976] | 225 | }
|
---|
[24509] | 226 |
|
---|
[24976] | 227 | protected Element processAddDocument(Element request)
|
---|
| 228 | {
|
---|
[24509] | 229 | //System.err.println("REQUEST = " + GSXML.xmlNodeToString(request));
|
---|
[25635] | 230 | Hashtable<String, Hashtable<String, Item>> docsMap = updateDocMap(request);
|
---|
[24509] | 231 | //System.err.println("DOCSMAP = " + docsMap);
|
---|
| 232 | // Create a new (empty) result message
|
---|
[28966] | 233 | Document result_doc = XMLConverter.newDOM();
|
---|
| 234 | Element result = result_doc.createElement(GSXML.RESPONSE_ELEM);
|
---|
[24976] | 235 |
|
---|
[24509] | 236 | // Get the parameters of the request
|
---|
[24976] | 237 | Element param_list = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
|
---|
| 238 | if (param_list == null)
|
---|
| 239 | {
|
---|
[24509] | 240 | logger.error("DocumentBasket Error: AddDocument request had no paramList.");
|
---|
[24976] | 241 | return result; // Return the empty result
|
---|
[24509] | 242 | }
|
---|
[24976] | 243 |
|
---|
[25635] | 244 | HashMap<String, Serializable> params = GSXML.extractParams(param_list, false);
|
---|
[24509] | 245 | //System.err.println("PARAMS = " + params);
|
---|
[24976] | 246 | String item = (String) params.get("item");
|
---|
| 247 |
|
---|
[24509] | 248 | int startIndex = item.startsWith(BASKET_BOOK) ? BASKET_BOOK.length() : 0;
|
---|
[24976] | 249 |
|
---|
[24509] | 250 | String collection = "";
|
---|
| 251 | int pos = item.indexOf(":");
|
---|
[24976] | 252 | if (pos != -1)
|
---|
| 253 | {
|
---|
| 254 | collection = item.substring(startIndex, pos);
|
---|
| 255 | item = item.substring(pos + 1);
|
---|
[24509] | 256 | }
|
---|
[25091] | 257 |
|
---|
[24509] | 258 | //logger.error("COLLECTION = " + collection + " *** ITEM = " + item);
|
---|
[24976] | 259 | if (docsMap.containsKey(collection))
|
---|
| 260 | {
|
---|
[25635] | 261 | Hashtable<String, Item> items = docsMap.get(collection);
|
---|
[24976] | 262 | if (!items.containsKey(item))
|
---|
| 263 | {
|
---|
| 264 | Item newItem = generateItem(collection, item);
|
---|
[24509] | 265 | items.put(item, newItem);
|
---|
[28966] | 266 | result.appendChild(newItem.wrapIntoElement(result_doc));
|
---|
[24509] | 267 | }
|
---|
| 268 | }
|
---|
[24976] | 269 | else
|
---|
| 270 | {
|
---|
[25635] | 271 | Hashtable<String, Item> items = new Hashtable<String, Item>();
|
---|
[24976] | 272 | Item newItem = generateItem(collection, item);
|
---|
[24509] | 273 | items.put(item, newItem);
|
---|
[24976] | 274 | docsMap.put(collection, items);
|
---|
[28966] | 275 | result.appendChild(newItem.wrapIntoElement(result_doc));
|
---|
[24509] | 276 | }
|
---|
[24976] | 277 |
|
---|
[24509] | 278 | return result;
|
---|
[24976] | 279 | }
|
---|
| 280 |
|
---|
[24509] | 281 | protected Element processMergeDocument(Element request)
|
---|
| 282 | {
|
---|
| 283 | // Get the parameters of the request
|
---|
[24976] | 284 | Element param_list = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
|
---|
| 285 | if (param_list == null)
|
---|
| 286 | {
|
---|
[24509] | 287 | logger.error("DocumentBasket Error: MergeDocument request had no paramList.");
|
---|
[24976] | 288 | return null; // Return the empty result
|
---|
[24509] | 289 | }
|
---|
[24976] | 290 |
|
---|
[25635] | 291 | HashMap<String, Serializable> params = GSXML.extractParams(param_list, false);
|
---|
[24976] | 292 |
|
---|
| 293 | String docString = (String) params.get("docs");
|
---|
[24509] | 294 | String[] docs = docString.split("-");
|
---|
[24976] | 295 |
|
---|
| 296 | for (String d : docs)
|
---|
[24509] | 297 | {
|
---|
| 298 | logger.error("DOC = " + d);
|
---|
| 299 | }
|
---|
[24976] | 300 |
|
---|
| 301 | /*
|
---|
| 302 | * try{ System.out.println("Concatenate Two PDF"); PdfReader reader1 =
|
---|
| 303 | * new PdfReader("3A01-01_Part_1-001.pdf"); PdfReader reader2 = new
|
---|
| 304 | * PdfReader("3A01-01_Part_1-017.pdf"); PdfCopyFields copy = new
|
---|
| 305 | * PdfCopyFields(new FileOutputStream("concatenatedPDF.pdf"));
|
---|
| 306 | * copy.addDocument(reader1); copy.addDocument(reader2); copy.close(); }
|
---|
| 307 | * catch(Exception ex) { ex.printStackTrace(); }
|
---|
| 308 | */
|
---|
| 309 | // added -->
|
---|
| 310 |
|
---|
| 311 | try
|
---|
[24509] | 312 | {
|
---|
[24976] | 313 | {
|
---|
| 314 | PrintWriter pw = new PrintWriter(new FileOutputStream("G:/output1.xml"));
|
---|
| 315 | File file = new File("G:/greenstone3-svn/web/sites/localsite/collect/peij21/archives/HASH0189.dir/");
|
---|
| 316 | File[] files = file.listFiles();
|
---|
[24509] | 317 |
|
---|
[24976] | 318 | for (int i = 0; i < files.length; i++)
|
---|
| 319 | {
|
---|
[24509] | 320 |
|
---|
[24976] | 321 | //System.out.println(files[i].getName());
|
---|
| 322 | String fileName = files[i].getName();
|
---|
[24509] | 323 |
|
---|
[24976] | 324 | if (fileName.equals("doc.xml"))
|
---|
| 325 | {
|
---|
[24509] | 326 |
|
---|
[24976] | 327 | System.out.println("Processing " + files[i].getPath() + "... ");
|
---|
| 328 | BufferedReader br = new BufferedReader(new FileReader(files[i].getPath()));
|
---|
| 329 | String line = br.readLine();
|
---|
| 330 | while (line != null)
|
---|
| 331 | {
|
---|
| 332 | pw.println(line);
|
---|
| 333 | line = br.readLine();
|
---|
| 334 | }
|
---|
| 335 | br.close();
|
---|
[24509] | 336 |
|
---|
[24976] | 337 | }
|
---|
| 338 |
|
---|
[24509] | 339 | }
|
---|
| 340 |
|
---|
[24976] | 341 | File file1 = new File("G:/greenstone3-svn/web/sites/localsite/collect/peij21/archives/HASHfdc0.dir/");
|
---|
| 342 | File[] files1 = file1.listFiles();
|
---|
[24509] | 343 |
|
---|
[24976] | 344 | for (int i = 0; i < files1.length; i++)
|
---|
| 345 | {
|
---|
[24509] | 346 |
|
---|
[24976] | 347 | //System.out.println(files[i].getName());
|
---|
| 348 | String fileName = files1[i].getName();
|
---|
[24509] | 349 |
|
---|
[24976] | 350 | if (fileName.equals("doc.xml"))
|
---|
| 351 | {
|
---|
[24509] | 352 |
|
---|
[24976] | 353 | System.out.println("Processing " + files1[i].getPath() + "... ");
|
---|
| 354 | BufferedReader br = new BufferedReader(new FileReader(files1[i].getPath()));
|
---|
| 355 | String line = br.readLine();
|
---|
| 356 | while (line != null)
|
---|
| 357 | {
|
---|
| 358 | pw.println(line);
|
---|
| 359 | line = br.readLine();
|
---|
| 360 | }
|
---|
| 361 | br.close();
|
---|
[24509] | 362 |
|
---|
[24976] | 363 | }
|
---|
[24509] | 364 |
|
---|
[24976] | 365 | }
|
---|
[24509] | 366 |
|
---|
[24976] | 367 | pw.close();
|
---|
[24509] | 368 |
|
---|
[24976] | 369 | System.out.println("All doc.xml files have been concatenated into output1.xml");
|
---|
| 370 | }
|
---|
[24509] | 371 | }
|
---|
[24976] | 372 | catch (Exception ex)
|
---|
| 373 | {
|
---|
| 374 | ex.printStackTrace();
|
---|
| 375 | }
|
---|
| 376 | return null;
|
---|
[24509] | 377 | }
|
---|
| 378 |
|
---|
[24976] | 379 | //end
|
---|
| 380 | private Item generateItem(String collection, String id)
|
---|
| 381 | {
|
---|
[24509] | 382 | Item item = new Item(collection, id);
|
---|
| 383 | String to = GSPath.appendLink(collection, "DocumentMetadataRetrieve");
|
---|
[25635] | 384 | ArrayList<String> tmp = new ArrayList<String>();
|
---|
[24976] | 385 | tmp.add(id);
|
---|
[24993] | 386 |
|
---|
| 387 | UserContext userContext = new UserContext();
|
---|
| 388 | userContext.setLanguage("en");
|
---|
| 389 | userContext.setUserID("dumy");
|
---|
| 390 |
|
---|
| 391 | Element response = getDocumentMetadata(to, userContext, tmp.iterator());
|
---|
[24976] | 392 | Element doc_node = (Element) response.getElementsByTagName(GSXML.DOC_NODE_ELEM).item(0);
|
---|
[24509] | 393 |
|
---|
| 394 | String node_id = doc_node.getAttribute(GSXML.NODE_ID_ATT);
|
---|
[24976] | 395 | Element metadata_list = (Element) doc_node.getElementsByTagName(GSXML.METADATA_ELEM + GSXML.LIST_MODIFIER).item(0);
|
---|
[24509] | 396 |
|
---|
| 397 | //assign title metadata if any
|
---|
[24976] | 398 | Element metadata = GSXML.getNamedElement(metadata_list, "metadata", "name", "Title");
|
---|
| 399 | if (metadata != null)
|
---|
| 400 | {
|
---|
[24509] | 401 | item.title = GSXML.getNodeText(metadata).trim();
|
---|
[24976] | 402 | }
|
---|
[24509] | 403 | //assign date metadata if any
|
---|
[24976] | 404 | metadata = GSXML.getNamedElement(metadata_list, "metadata", "name", "Date");
|
---|
| 405 | if (metadata != null)
|
---|
| 406 | {
|
---|
[24509] | 407 | item.date = GSXML.getNodeText(metadata).trim();
|
---|
[24976] | 408 | }
|
---|
| 409 |
|
---|
[24509] | 410 | //assign root title metadata if any
|
---|
[24976] | 411 | metadata = GSXML.getNamedElement(metadata_list, "metadata", "name", "root_Title");
|
---|
| 412 | if (metadata != null)
|
---|
| 413 | {
|
---|
| 414 | String rootTitle = GSXML.getNodeText(metadata).trim();
|
---|
| 415 | if (!rootTitle.equals(item.title))
|
---|
| 416 | {
|
---|
| 417 | item.rootTitle = rootTitle;
|
---|
[24509] | 418 | }
|
---|
[24976] | 419 |
|
---|
[24509] | 420 | }
|
---|
| 421 |
|
---|
| 422 | return item;
|
---|
[24976] | 423 | }
|
---|
[25069] | 424 |
|
---|
| 425 | protected Element processClearDocuments(Element request)
|
---|
| 426 | {
|
---|
[25635] | 427 | Hashtable<String, Hashtable<String, Item>> docsMap = updateDocMap(request);
|
---|
[28966] | 428 | Document result_doc = XMLConverter.newDOM();
|
---|
| 429 | Element result = result_doc.createElement(GSXML.RESPONSE_ELEM);
|
---|
[25069] | 430 |
|
---|
| 431 | // Get the parameters of the request
|
---|
| 432 | Element param_list = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
|
---|
| 433 |
|
---|
| 434 | if (param_list == null)
|
---|
| 435 | {
|
---|
| 436 | logger.error("DocumentBasket Error: DeleteDocument request had no paramList.");
|
---|
| 437 | return result; // Return the empty result
|
---|
| 438 | }
|
---|
[24509] | 439 |
|
---|
[25635] | 440 | HashMap<String, Serializable> params = GSXML.extractParams(param_list, false);
|
---|
[25069] | 441 | String collection = (String) params.get(GSParams.COLLECTION);
|
---|
| 442 |
|
---|
| 443 | if (collection == null)
|
---|
| 444 | return result;
|
---|
| 445 |
|
---|
[25635] | 446 | docsMap.put(collection, new Hashtable<String, Item>());
|
---|
[25069] | 447 |
|
---|
| 448 | return result;
|
---|
| 449 | }
|
---|
| 450 |
|
---|
[24976] | 451 | protected Element processDeleteDocuments(Element request)
|
---|
| 452 | {
|
---|
[25635] | 453 | Hashtable<String, Hashtable<String, Item>> docsMap = updateDocMap(request);
|
---|
[24509] | 454 |
|
---|
| 455 | // Create a new (empty) result message
|
---|
[28966] | 456 | Document result_doc = XMLConverter.newDOM();
|
---|
| 457 | Element result = result_doc.createElement(GSXML.RESPONSE_ELEM);
|
---|
[24976] | 458 |
|
---|
[24509] | 459 | // Get the parameters of the request
|
---|
[24976] | 460 | Element param_list = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
|
---|
[24509] | 461 |
|
---|
[24976] | 462 | //GSXML.printXMLNode(param_list);
|
---|
| 463 |
|
---|
| 464 | if (param_list == null)
|
---|
| 465 | {
|
---|
[24509] | 466 | logger.error("DocumentBasket Error: DeleteDocument request had no paramList.");
|
---|
[24976] | 467 | return result; // Return the empty result
|
---|
[24509] | 468 | }
|
---|
| 469 |
|
---|
[25635] | 470 | HashMap<String, Serializable> params = GSXML.extractParams(param_list, false);
|
---|
[24509] | 471 |
|
---|
[24976] | 472 | String param = (String) params.get("items");
|
---|
| 473 |
|
---|
| 474 | if (param == null)
|
---|
| 475 | return result;
|
---|
| 476 |
|
---|
[24509] | 477 | String[] items = param.split("\\|");
|
---|
[24976] | 478 |
|
---|
| 479 | for (int i = 0; i < items.length; i++)
|
---|
[24509] | 480 | {
|
---|
| 481 | String item = items[i];
|
---|
[24976] | 482 | if (item.trim().length() == 0)
|
---|
| 483 | continue;
|
---|
| 484 |
|
---|
[24509] | 485 | String collection = "";
|
---|
| 486 | int pos = item.indexOf(":");
|
---|
[24976] | 487 | if (pos != -1)
|
---|
| 488 | {
|
---|
| 489 | collection = item.substring(0, pos);
|
---|
| 490 | item = item.substring(pos + 1);
|
---|
[24509] | 491 | }
|
---|
[24976] | 492 |
|
---|
| 493 | if (docsMap.containsKey(collection))
|
---|
| 494 | {
|
---|
[25635] | 495 | Hashtable itemMap = docsMap.get(collection);
|
---|
[24976] | 496 | if (itemMap.containsKey(item))
|
---|
| 497 | {
|
---|
[24509] | 498 | itemMap.remove(item);
|
---|
| 499 | }
|
---|
[24976] | 500 | if (itemMap.size() == 0)
|
---|
| 501 | {
|
---|
[24509] | 502 | docsMap.remove(collection);
|
---|
| 503 | }
|
---|
| 504 | }
|
---|
| 505 | }
|
---|
| 506 |
|
---|
| 507 | return result;
|
---|
[24976] | 508 | }
|
---|
[24509] | 509 |
|
---|
[24976] | 510 | protected Element processDeleteDocument(Element request)
|
---|
| 511 | {
|
---|
[25635] | 512 | Hashtable<String, Hashtable<String, Item>> docsMap = updateDocMap(request);
|
---|
[24976] | 513 |
|
---|
[24509] | 514 | // Create a new (empty) result message
|
---|
[28966] | 515 | Document result_doc = XMLConverter.newDOM();
|
---|
| 516 | Element result = result_doc.createElement(GSXML.RESPONSE_ELEM);
|
---|
[24976] | 517 |
|
---|
[24509] | 518 | // Get the parameters of the request
|
---|
[24976] | 519 | Element param_list = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
|
---|
[24509] | 520 |
|
---|
[24976] | 521 | //GSXML.printXMLNode(param_list);
|
---|
| 522 |
|
---|
| 523 | if (param_list == null)
|
---|
| 524 | {
|
---|
[24509] | 525 | logger.error("DocumentBasket Error: DeleteDocument request had no paramList.");
|
---|
[24976] | 526 | return result; // Return the empty result
|
---|
[24509] | 527 | }
|
---|
| 528 |
|
---|
[25635] | 529 | HashMap<String, Serializable> params = GSXML.extractParams(param_list, false);
|
---|
[24509] | 530 |
|
---|
[25069] | 531 | String item = (String) params.get("item");
|
---|
[24976] | 532 |
|
---|
[25069] | 533 | if (item == null)
|
---|
[24976] | 534 | return result;
|
---|
| 535 |
|
---|
[24509] | 536 | String collection = "";
|
---|
| 537 | int pos = item.indexOf(":");
|
---|
[24976] | 538 |
|
---|
| 539 | if (pos != -1)
|
---|
| 540 | {
|
---|
| 541 | collection = item.substring(0, pos);
|
---|
| 542 | item = item.substring(pos + 1);
|
---|
[24509] | 543 | }
|
---|
[24976] | 544 |
|
---|
| 545 | if (docsMap.containsKey(collection))
|
---|
| 546 | {
|
---|
[25635] | 547 | Hashtable itemMap = docsMap.get(collection);
|
---|
[24976] | 548 | if (itemMap.containsKey(item))
|
---|
| 549 | {
|
---|
[24509] | 550 | itemMap.remove(item);
|
---|
| 551 | }
|
---|
[24976] | 552 | if (itemMap.size() == 0)
|
---|
| 553 | {
|
---|
[24509] | 554 | docsMap.remove(collection);
|
---|
| 555 | }
|
---|
[24976] | 556 | }
|
---|
[24509] | 557 |
|
---|
| 558 | return result;
|
---|
[24976] | 559 | }
|
---|
[24509] | 560 |
|
---|
[24976] | 561 | protected Element processGetDocuments(Element request)
|
---|
| 562 | {
|
---|
| 563 | // GSXML.printXMLNode(request);
|
---|
[25635] | 564 | Hashtable<String, Hashtable<String, Item>> docsMap = updateDocMap(request);
|
---|
[24509] | 565 |
|
---|
| 566 | // Create a new (empty) result message
|
---|
[28966] | 567 | Document result_doc = XMLConverter.newDOM();
|
---|
| 568 | Element result = result_doc.createElement(GSXML.RESPONSE_ELEM);
|
---|
[24976] | 569 |
|
---|
| 570 | int size = 0;
|
---|
| 571 | String ids = "";
|
---|
[25635] | 572 | Iterator<String> keys = docsMap.keySet().iterator();
|
---|
[24976] | 573 |
|
---|
| 574 | while (keys.hasNext())
|
---|
| 575 | {
|
---|
[25635] | 576 | Hashtable items = docsMap.get(keys.next());
|
---|
[24976] | 577 | size += items.size();
|
---|
[24509] | 578 | Iterator values = items.values().iterator();
|
---|
[24976] | 579 | while (values.hasNext())
|
---|
| 580 | {
|
---|
| 581 | Item item = (Item) values.next();
|
---|
[28966] | 582 | result.appendChild(item.wrapIntoElement(result_doc));
|
---|
[24976] | 583 | }
|
---|
[24509] | 584 | }
|
---|
| 585 |
|
---|
[28966] | 586 | Element selement = result_doc.createElement("size");
|
---|
[24976] | 587 | selement.setAttribute("value", size + "");
|
---|
| 588 | result.appendChild(selement);
|
---|
[24509] | 589 |
|
---|
[24976] | 590 | return result;
|
---|
| 591 | }
|
---|
| 592 |
|
---|
[28966] | 593 | private Element getDocumentMetadata(String to, UserContext userContext, Iterator<String> ids)
|
---|
[24976] | 594 | {
|
---|
| 595 |
|
---|
| 596 | // Build a request to obtain some document metadata
|
---|
[28966] | 597 | Document msg_doc = XMLConverter.newDOM();
|
---|
| 598 | Element dm_message = msg_doc.createElement(GSXML.MESSAGE_ELEM);
|
---|
| 599 | Element dm_request = GSXML.createBasicRequest(msg_doc, GSXML.REQUEST_TYPE_PROCESS, to, userContext);
|
---|
[24509] | 600 | dm_message.appendChild(dm_request);
|
---|
| 601 |
|
---|
| 602 | // Create a parameter list to specify the required metadata information
|
---|
[25635] | 603 | HashSet<String> meta_names = new HashSet<String>();
|
---|
[24509] | 604 | meta_names.add("Title"); // the default
|
---|
[24976] | 605 | meta_names.add("root_Title");
|
---|
| 606 | meta_names.add("Date");
|
---|
[24509] | 607 |
|
---|
[28966] | 608 | Element param_list = msg_doc.createElement(GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
|
---|
[24509] | 609 |
|
---|
| 610 | Element param = null;
|
---|
[25635] | 611 | Iterator<String> i = meta_names.iterator();
|
---|
[24976] | 612 | while (i.hasNext())
|
---|
| 613 | {
|
---|
[25635] | 614 | String name = i.next();
|
---|
[28966] | 615 | param = msg_doc.createElement(GSXML.PARAM_ELEM);
|
---|
[24509] | 616 | param_list.appendChild(param);
|
---|
| 617 | param.setAttribute(GSXML.NAME_ATT, "metadata");
|
---|
| 618 | param.setAttribute(GSXML.VALUE_ATT, name);
|
---|
| 619 | }
|
---|
[24976] | 620 |
|
---|
[24509] | 621 | dm_request.appendChild(param_list);
|
---|
[24976] | 622 |
|
---|
[24509] | 623 | // create the doc node list for the metadata request
|
---|
[28966] | 624 | Element dm_doc_list = msg_doc.createElement(GSXML.DOC_NODE_ELEM + GSXML.LIST_MODIFIER);
|
---|
[24509] | 625 | dm_request.appendChild(dm_doc_list);
|
---|
| 626 |
|
---|
[24976] | 627 | while (ids.hasNext())
|
---|
| 628 | {
|
---|
[24509] | 629 | // Add the documentNode to the list
|
---|
[28966] | 630 | Element dm_doc_node = msg_doc.createElement(GSXML.DOC_NODE_ELEM);
|
---|
[24509] | 631 | dm_doc_list.appendChild(dm_doc_node);
|
---|
[25635] | 632 | dm_doc_node.setAttribute(GSXML.NODE_ID_ATT, ids.next());
|
---|
[24509] | 633 | }
|
---|
| 634 |
|
---|
| 635 | return (Element) this.router.process(dm_message);
|
---|
[24976] | 636 | }
|
---|
[24509] | 637 |
|
---|
[24976] | 638 | protected Element processDisplayDocumentList(Element request)
|
---|
| 639 | {
|
---|
[25635] | 640 | Hashtable<String, Hashtable<String, Item>> docsMap = updateDocMap(request);
|
---|
[24509] | 641 |
|
---|
| 642 | // Create a new (empty) result message
|
---|
[28966] | 643 | Document result_doc = XMLConverter.newDOM();
|
---|
| 644 | Element result = result_doc.createElement(GSXML.RESPONSE_ELEM);
|
---|
[24976] | 645 |
|
---|
[25635] | 646 | Iterator<String> keys = docsMap.keySet().iterator();
|
---|
[24976] | 647 |
|
---|
| 648 | while (keys.hasNext())
|
---|
| 649 | {
|
---|
[25635] | 650 | String collection = keys.next();
|
---|
| 651 | Hashtable items = docsMap.get(collection);
|
---|
[24509] | 652 | Iterator itemItr = items.values().iterator();
|
---|
[24976] | 653 |
|
---|
[28966] | 654 | Element collectionNode = result_doc.createElement("documentList");
|
---|
[24976] | 655 | collectionNode.setAttribute("name", collection);
|
---|
[24509] | 656 | result.appendChild(collectionNode);
|
---|
[24976] | 657 |
|
---|
| 658 | while (itemItr.hasNext())
|
---|
| 659 | {
|
---|
| 660 | Item item = (Item) itemItr.next();
|
---|
[28966] | 661 | Element itemElement = result_doc.createElement("item");
|
---|
[24976] | 662 |
|
---|
[24509] | 663 | collectionNode.appendChild(itemElement);
|
---|
| 664 | itemElement.setAttribute("name", item.docid);
|
---|
| 665 | itemElement.setAttribute("collection", item.collection);
|
---|
[24976] | 666 | itemElement.setAttribute("title", item.title);
|
---|
| 667 | itemElement.setAttribute("date", item.date);
|
---|
| 668 | itemElement.setAttribute("root_title", item.rootTitle);
|
---|
| 669 | }
|
---|
[24509] | 670 | }
|
---|
[24976] | 671 |
|
---|
[24509] | 672 | return result;
|
---|
[24976] | 673 | }
|
---|
[24509] | 674 |
|
---|
[24976] | 675 | protected class Item
|
---|
| 676 | {
|
---|
[24509] | 677 | public String collection;
|
---|
| 678 | public String docid;
|
---|
[24976] | 679 | public String title = "";
|
---|
| 680 | public String query = "";
|
---|
| 681 | public String date = "";
|
---|
| 682 | public String rootTitle = "";
|
---|
| 683 |
|
---|
| 684 | public Item(String coll, String id)
|
---|
| 685 | {
|
---|
[24509] | 686 | this.collection = coll;
|
---|
| 687 | this.docid = id;
|
---|
| 688 | }
|
---|
| 689 |
|
---|
[24976] | 690 | public boolean equals(Object o)
|
---|
| 691 | {
|
---|
| 692 | if (!(o instanceof Item))
|
---|
| 693 | {
|
---|
[24509] | 694 | return false;
|
---|
| 695 | }
|
---|
[24976] | 696 |
|
---|
| 697 | Item item = (Item) o;
|
---|
| 698 | String id = collection + ":" + docid;
|
---|
| 699 | String idin = item.collection + ":" + item.docid;
|
---|
[24509] | 700 | return id.equals(idin);
|
---|
[24976] | 701 | }
|
---|
[24509] | 702 |
|
---|
[24976] | 703 | public String toString()
|
---|
| 704 | {
|
---|
| 705 | return collection + ":" + docid + ":" + "[" + ((!rootTitle.equals("")) ? (rootTitle + ":") : "") + title + "]";
|
---|
| 706 | }
|
---|
[24509] | 707 |
|
---|
[28966] | 708 | public Element wrapIntoElement(Document doc)
|
---|
[24976] | 709 | {
|
---|
| 710 | Element itemElement = doc.createElement("item");
|
---|
[24509] | 711 | itemElement.setAttribute("name", docid);
|
---|
| 712 | itemElement.setAttribute("collection", collection);
|
---|
[24976] | 713 | itemElement.setAttribute("title", title);
|
---|
| 714 | itemElement.setAttribute("date", date);
|
---|
| 715 | itemElement.setAttribute("root_title", rootTitle);
|
---|
| 716 | return itemElement;
|
---|
[24509] | 717 | }
|
---|
[24976] | 718 | }
|
---|
| 719 |
|
---|
[24509] | 720 | private class UserTimer extends Timer implements ActionListener
|
---|
| 721 | {
|
---|
[24976] | 722 | String id = "";
|
---|
[24509] | 723 |
|
---|
[24976] | 724 | public UserTimer(int delay, String id)
|
---|
| 725 | {
|
---|
| 726 | super(delay, (ActionListener) null);
|
---|
[24509] | 727 | addActionListener(this);
|
---|
[24976] | 728 | this.id = id;
|
---|
[24509] | 729 | }
|
---|
| 730 |
|
---|
[24976] | 731 | public void actionPerformed(ActionEvent e)
|
---|
| 732 | {
|
---|
| 733 | userMap.remove(id);
|
---|
[24509] | 734 | timerMap.remove(id);
|
---|
| 735 | stop();
|
---|
| 736 | }
|
---|
| 737 | }
|
---|
| 738 | }
|
---|