source: trunk/gsdl3/src/java/org/greenstone/gsdl3/service/GS2Retrieve.java@ 4268

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

changed to use archivedir instead of assocfilepath for image location

  • Property svn:keywords set to Author Date Id Revision
File size: 26.3 KB
Line 
1/*
2 * GS2Retrieve.java
3 * Copyright (C) 2002 New Zealand Digital Library, http://www.nzdl.org
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19package org.greenstone.gsdl3.service;
20
21
22// Greenstone classes
23import org.greenstone.gdbm.*;
24import org.greenstone.gsdl3.util.*;
25
26// XML classes
27import org.w3c.dom.Document;
28import org.w3c.dom.Element;
29import org.w3c.dom.NodeList;
30
31// General Java classes
32import java.io.File;
33import java.util.StringTokenizer;
34import java.util.Vector;
35import java.util.Set;
36import java.util.Iterator;
37
38/** Implements the generic retrieval and classifier services for GS2
39 * collections.
40 *
41 * @author <a href="mailto:[email protected]">Katherine Don</a>
42 * @author <a href="mailto:[email protected]">Michael Dewsnip</a>
43 * @version $Revision: 4268 $
44 */
45
46public abstract class GS2Retrieve
47 extends ServiceRack {
48
49 // the services on offer
50 // these strings must match what is found in the properties file
51 protected static final String DOCUMENT_STRUCTURE_RETRIEVE_SERVICE = "DocumentStructureRetrieve";
52 protected static final String DOCUMENT_METADATA_RETRIEVE_SERVICE = "DocumentMetadataRetrieve";
53 protected static final String DOCUMENT_CONTENT_RETRIEVE_SERVICE = "DocumentContentRetrieve";
54
55
56 // the browsing services - now in here, these will only be advertised if classifiers have been specified in the config file
57 private static final String CLASSIFIER_SERVICE = "ClassifierBrowse";
58 private static final String CLASSIFIER_METADATA_SERVICE = "ClassifierBrowseMetadataRetrieve";
59
60 protected static final String STRUCT_PARAM = "structure";
61 protected static final String INFO_PARAM = "info";
62
63 protected static final String STRUCT_ANCESTORS = "ancestors";
64 protected static final String STRUCT_PARENT = "parent";
65 protected static final String STRUCT_SIBS = "siblings";
66 protected static final String STRUCT_CHILDREN = "children";
67 protected static final String STRUCT_DESCENDS = "descendants";
68
69 protected static final String INFO_NUM_SIBS = "numSiblings";
70 protected static final String INFO_NUM_CHILDREN = "numChildren";
71 protected static final String INFO_SIB_POS = "siblingPosition";
72
73 protected static final int DOCUMENT=1;
74 protected static final int CLASSIFIER=2;
75
76 protected GDBMWrapper gdbm_src_ = null;
77
78 protected Element config_info_ = null; // the xml from the config file
79
80 /** constructor */
81 protected GS2Retrieve()
82 {
83 gdbm_src_ = new GDBMWrapper();
84 }
85
86
87 /** configure this service */
88 public boolean configure(Element info, Element extra_info)
89 {
90 System.out.println("Configuring GS2Retrieve...");
91 config_info_ = info;
92
93 // set up short_service_info_ - for now just has name and type
94 Element dsr_service = doc_.createElement(GSXML.SERVICE_ELEM);
95 dsr_service.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_RETRIEVE);
96 dsr_service.setAttribute(GSXML.NAME_ATT, DOCUMENT_STRUCTURE_RETRIEVE_SERVICE);
97 short_service_info_.appendChild(dsr_service);
98
99 Element dmr_service = doc_.createElement(GSXML.SERVICE_ELEM);
100 dmr_service.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_RETRIEVE);
101 dmr_service.setAttribute(GSXML.NAME_ATT, DOCUMENT_METADATA_RETRIEVE_SERVICE);
102 short_service_info_.appendChild(dmr_service);
103
104 Element dcr_service = doc_.createElement(GSXML.SERVICE_ELEM);
105 dcr_service.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_RETRIEVE);
106 dcr_service.setAttribute(GSXML.NAME_ATT, DOCUMENT_CONTENT_RETRIEVE_SERVICE);
107 short_service_info_.appendChild(dcr_service);
108
109 // set up service_info_map_ - for now, just has the same elements as above
110 // should have full details about each service incl params lists etc.
111 service_info_map_.put(DOCUMENT_STRUCTURE_RETRIEVE_SERVICE, dsr_service);
112 service_info_map_.put(DOCUMENT_METADATA_RETRIEVE_SERVICE, dmr_service);
113 service_info_map_.put(DOCUMENT_CONTENT_RETRIEVE_SERVICE, dcr_service);
114
115 // Open GDBM database for querying
116 String gdbm_db_file = GSFile.GDBMDatabaseFile(site_home_, cluster_name_);
117 if (!gdbm_src_.openDatabase(gdbm_db_file, GDBMWrapper.READER)) {
118 System.err.println("Error: Could not open GDBM database!");
119 return false;
120 }
121
122 // now do the classifier browse service
123
124 // check that there are classifiers specified
125 Element class_list = (Element)GSXML.getChildByTagName(info, GSXML.CLASSIFIER_ELEM+GSXML.LIST_MODIFIER);
126 if (class_list == null) {
127 // no classifiers specified
128 return true;
129 }
130
131 // get the display and format elements from the coll config file for
132 // the classifiers
133 extractExtraClassifierInfo(info, extra_info);
134 config_info_ = info;
135
136 // short_service_info_ - the browse one
137 Element cb_service = doc_.createElement(GSXML.SERVICE_ELEM);
138 cb_service.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_BROWSE);
139 cb_service.setAttribute(GSXML.NAME_ATT, CLASSIFIER_SERVICE);
140 short_service_info_.appendChild(cb_service);
141
142 // metadata retrieval for the browsing
143 Element cbmr_service = doc_.createElement(GSXML.SERVICE_ELEM);
144 cbmr_service.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_RETRIEVE);
145 cbmr_service.setAttribute(GSXML.NAME_ATT, CLASSIFIER_METADATA_SERVICE);
146 short_service_info_.appendChild(cbmr_service);
147
148 // service_info_map_
149
150 // the metadata one has the same info for now - should this advertise what metadata is available??
151 Element cbmr_service_info = (Element) cbmr_service.cloneNode(true);
152 service_info_map_.put(CLASSIFIER_METADATA_SERVICE, cbmr_service_info);
153
154 //the browse one
155 Element cb_service_info = (Element)cb_service.cloneNode(true);
156 Element cl_list = doc_.createElement(GSXML.CLASSIFIER_ELEM+GSXML.LIST_MODIFIER);
157 cb_service_info.appendChild(cl_list);
158
159 // the format info
160 Element cb_format_info = doc_.createElement(GSXML.FORMAT_ELEM);
161 boolean format_found = false;
162 // add in to the description a simplified list of classifiers
163 NodeList classifiers = class_list.getElementsByTagName(GSXML.CLASSIFIER_ELEM);
164 for(int i=0; i<classifiers.getLength(); i++) {
165 Element cl = (Element)classifiers.item(i);
166 Element new_cl = (Element)doc_.importNode(cl, false); // just import this node, not the children
167
168 cl_list.appendChild(new_cl);
169
170 // get the format info out, and put inside a classifier element
171 Element format_cl = (Element)new_cl.cloneNode(false);
172 Element format = (Element)GSXML.getChildByTagName(cl, GSXML.FORMAT_ELEM);
173 if (format != null) {
174
175 //copy all the children
176 NodeList elems = format.getChildNodes();
177 for (int j=0; j<elems.getLength();j++) {
178 format_cl.appendChild(doc_.importNode(elems.item(j), true));
179 }
180 cb_format_info.appendChild(format_cl);
181 format_found = true;
182 }
183
184
185 }
186
187
188 service_info_map_.put(CLASSIFIER_SERVICE, cb_service_info);
189 if (format_found) {
190 format_info_map_.put(CLASSIFIER_SERVICE, cb_format_info);
191 }
192 return true;
193 }
194
195 /** this looks for any classifier specific display or format info from extra_info and adds it in to the correct place in info */
196 protected boolean extractExtraClassifierInfo(Element info, Element extra_info) {
197
198 if (extra_info == null) {
199 return false;
200 }
201
202 Document owner = info.getOwnerDocument();
203 // so far we have display and format elements that we need for classifiers
204 NodeList classifiers = info.getElementsByTagName(GSXML.CLASSIFIER_ELEM);
205 Element config_browse = (Element)GSXML.getChildByTagName(extra_info, GSXML.BROWSE_ELEM);
206
207 for (int i=0; i<classifiers.getLength();i++) {
208 Element cl = (Element)classifiers.item(i);
209 String name = cl.getAttribute(GSXML.NAME_ATT);
210 Element node_extra = GSXML.getNamedElement(config_browse,
211 GSXML.CLASSIFIER_ELEM,
212 GSXML.NAME_ATT,
213 name);
214 if (node_extra == null) {
215 System.err.println("GS2REtrieve: haven't found extra info for classifier named "+name);
216 continue;
217 }
218
219 // get the display elements if any - displayName
220 NodeList display_names = node_extra.getElementsByTagName(GSXML.DISPLAYNAME_ELEM);
221 if (display_names !=null) {
222 Element display = owner.createElement(GSXML.DISPLAY_ELEM);
223 for (int j=0; j<display_names.getLength(); j++) {
224 Element e = (Element)display_names.item(j);
225
226 Element display_name = GSXML.createTextElement(owner, GSXML.DISPLAY_NAME_ELEM, GSXML.getNodeText(e));
227 display_name.setAttribute(GSXML.LANG_ATT, e.getAttribute(GSXML.LANG_ATT));
228 display.appendChild(display_name);
229 }
230 cl.appendChild(display);
231 }
232
233 // get the format element if any
234 Element format = (Element)GSXML.getChildByTagName(node_extra, GSXML.FORMAT_ELEM);
235 if (format==null) { // try a generic one that applies to all classifiers
236 format = (Element)GSXML.getChildByTagName(extra_info,
237 GSXML.FORMAT_ELEM);
238 }
239 if (format!=null) { // append to index info
240 cl.appendChild(owner.importNode(format, true));
241 }
242 } // for each classifier
243 return true;
244 }
245
246
247 /** creates a display element containing all the text strings needed to display the service page, in the language specified
248 * the retrieval services dont get displayed to the users - they are only used internally by the actions. so this returns an empty display element
249 * for those services. CLASSIFIER_BROWSE service returns some info */
250 protected Element createServiceDisplay(String service, String lang)
251 {
252 Element display = doc_.createElement(GSXML.DISPLAY_ELEM);
253 if (!service.equals(CLASSIFIER_SERVICE)) {
254 return display;
255 }
256
257 // CLASSIFIER_SERVICE
258 display.appendChild(GSXML.createTextElement(doc_, GSXML.DISPLAY_NAME_ELEM, getTextString(service+".name", lang)));
259 //display.appendChild(GSXML.createTextElement(doc_, GSXML.DISPLAY_SUBMIT_ELEM, getTextString(service+".submit", lang)));
260
261 // need to add in the classifier name info
262 // add <classifier name="CL1">text name</classifier> to the
263 // display node
264 NodeList classifiers = config_info_.getElementsByTagName(GSXML.CLASSIFIER_ELEM);
265 for (int i=0; i<classifiers.getLength(); i++) {
266 Element cl = (Element)classifiers.item(i);
267 Element disp = (Element)GSXML.getChildByTagName(cl, GSXML.DISPLAY_ELEM);
268 String text = null;
269 if (disp !=null) {
270 text = GSXML.getDisplayText(disp,
271 GSXML.DISPLAY_NAME_ELEM,
272 lang, "en");
273 }
274 if (text == null || text.equals("")) {
275 // no display element was specified, use the metadata name
276 // for now this looks in the class properties file
277 // this needs to use a general metadata thing instead
278 text = getTextString(cl.getAttribute(GSXML.CLASSIFIER_CONTENT_ATT), lang);
279 }
280
281 Element cl_elem = doc_.createElement(GSXML.CLASSIFIER_ELEM);
282 cl_elem.setAttribute(GSXML.NAME_ATT, cl.getAttribute(GSXML.NAME_ATT));
283 Element cl_name = GSXML.createTextElement(doc_, GSXML.DISPLAY_NAME_ELEM, text);
284 cl_elem.appendChild(cl_name);
285
286 display.appendChild(cl_elem);
287
288 }
289
290 return display;
291
292 }
293
294 /** parent is true if this node is definitely the parent of something,
295 * child is true is it definitely is a child of something - just for efficiency purposes */
296 protected Element createDocNode(String doc_id, boolean parent, boolean child) {
297
298 // create this here or pass it in?
299 DBInfo db_info = gdbm_src_.getInfo(doc_id);
300 Element node;
301 if (isClassifier(doc_id)) {
302 node = doc_.createElement(GSXML.CLASS_NODE_ELEM);
303 String childtype = db_info.getInfo("childtype");
304 String orientation="";
305 if (childtype.equals("HList")) {
306 orientation = "horizontal";
307 } else { // assume vertical
308 orientation = "vertical";
309 }
310 node.setAttribute(GSXML.CLASS_NODE_ORIENTATION_ATT, orientation);
311 } else {
312 node = doc_.createElement(GSXML.DOC_NODE_ELEM);
313 if (/*!child &&*/ OID.isTop(doc_id)) {
314 node.setAttribute(GSXML.NODE_TYPE_ATT, GSXML.NODE_TYPE_ROOT);
315 } else if (!parent && isLeafNode(doc_id)) {
316 node.setAttribute(GSXML.NODE_TYPE_ATT, GSXML.NODE_TYPE_LEAF);
317 } else {
318 node.setAttribute(GSXML.NODE_TYPE_ATT, GSXML.NODE_TYPE_INTERIOR);
319 }
320 }
321 node.setAttribute(GSXML.NODE_ID_ATT, doc_id);
322 return node;
323
324 }
325 /** Returns the parent of a specified documentID, or null if none exists */
326 protected Element getParent(String doc_id)
327 {
328 String parent_id = OID.getParent(doc_id);
329 if (parent_id.equals(doc_id))
330 return null;
331
332 return createDocNode(parent_id, true, false);
333 }
334
335
336 /** adds all the children of doc_id the the doc element,
337 * and if recursive=true, adds all their children as well*/
338 protected void addDescendants(Element doc, String doc_id,
339 boolean recursive)
340 {
341 DBInfo info = gdbm_src_.getInfo(doc_id);
342 String contains = info.getInfo("contains");
343
344 StringTokenizer st = new StringTokenizer(contains, ";");
345 while (st.hasMoreTokens()) {
346 String child_id = st.nextToken().replaceAll("\"", doc_id);
347 Element child = createDocNode(child_id, false, true);
348 doc.appendChild(child);
349
350 // Apply recursively, if desired
351 if (recursive) {
352 addDescendants(child, child_id, recursive);
353 }
354
355 }
356 }
357
358 /** adds all the siblings of current_id to the parent element. */
359 protected Element addSiblings(Element parent, String parent_id, String current_id) {
360 Element current_node = (Element)parent.getFirstChild();
361 if (current_node ==null) {
362 // create a sensible error message
363 System.err.println("Error: there should be a first child.");
364 return null;
365 }
366 // remove the current child,- will add it in later in its correct place
367 parent.removeChild(current_node);
368
369 // add in all the siblings,
370 addDescendants(parent, parent_id, false);
371
372 // find the node that is now the current node
373 // this assumes that the new node that was created is the same as
374 // the old one that was removed - we may want to replace the new one
375 // with the old one.
376 Element new_current = GSXML.getNamedElement(parent, current_node.getNodeName(), GSXML.NODE_ID_ATT, current_id);
377 return new_current;
378
379 }
380 /** Returns true if the OID specifies a leaf node, false otherwise
381 Note: this makes a request to the GDBM database so it may not be
382 a particularly cheap operation */
383 protected boolean isLeafNode(String oid)
384 {
385 DBInfo info = gdbm_src_.getInfo(oid);
386 String children = info.getInfo("contains");
387 return (children.equals(""));
388 }
389
390 // for now just use CL for classifiers - should have a type? in teh gdbm
391 // database.
392 protected boolean isClassifier(String oid) {
393 if (oid.startsWith("CL")) {
394 return true;
395 }
396 return false;
397 }
398
399 protected Element processDocumentStructureRetrieve(Element request) {
400 return genericStructureRetrieve(request, DOCUMENT);
401 }
402
403 protected Element processClassifierBrowse(Element request) {
404 return genericStructureRetrieve(request, CLASSIFIER);
405 }
406
407 /** Retrieve the structure of a document */
408 protected Element genericStructureRetrieve(Element request, int type)
409 {
410 // Create a new (empty) result message
411 Element result = doc_.createElement(GSXML.RESPONSE_ELEM);
412
413 String node_name;
414 String service_name;
415 if (type==DOCUMENT) {
416 service_name = DOCUMENT_STRUCTURE_RETRIEVE_SERVICE;
417 node_name = GSXML.DOC_NODE_ELEM;
418 } else {
419 service_name = CLASSIFIER_SERVICE;
420 node_name = GSXML.CLASS_NODE_ELEM;
421 }
422
423 result.setAttribute(GSXML.FROM_ATT, service_name);
424 result.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_PROCESS);
425
426 // Get the parameters of the request
427 Element param_list = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
428 if (param_list == null) {
429 System.err.println("Error: DocumentStructureRetrieve request had no paramList.");
430 return result; // Return the empty result
431 }
432
433 // the type of info required
434 boolean want_structure = false;
435 boolean want_info = false;
436
437 Vector info_types=new Vector();
438 // The document structure information desired
439 boolean want_ancestors = false;
440 boolean want_parent = false;
441 boolean want_siblings = false;
442 boolean want_children = false;
443 boolean want_descendants = false;
444
445 // Process the request parameters
446 NodeList params = param_list.getElementsByTagName(GSXML.PARAM_ELEM);
447 for (int i=0; i<params.getLength();i++) {
448
449 Element param = (Element)params.item(i);
450 String p_name = param.getAttribute(GSXML.NAME_ATT);
451 String p_value = GSXML.getValue(param);
452 // Identify the structure information desired
453 if (p_name.equals(STRUCT_PARAM)) {
454 want_structure = true;
455
456 // This is NOT locale sensitive
457 if (p_value.equals(STRUCT_ANCESTORS))
458 want_ancestors = true;
459 else if (p_value.equals(STRUCT_PARENT))
460 want_parent = true;
461 else if (p_value.equals(STRUCT_SIBS))
462 want_siblings = true;
463 else if (p_value.equals(STRUCT_CHILDREN))
464 want_children = true;
465 else if (p_value.equals(STRUCT_DESCENDS))
466 want_descendants = true;
467 else
468 System.err.println("Warning: Unknown value \"" + p_value + "\".");
469 } else if (p_name.equals(INFO_PARAM)) {
470 want_info = true;
471 info_types.add(p_value);
472 }
473 }
474
475 // Make sure there is no repeated information
476 if (want_ancestors)
477 want_parent = false;
478 if (want_descendants)
479 want_children = false;
480
481
482 Element query_doc_list = (Element) GSXML.getChildByTagName(request, node_name+GSXML.LIST_MODIFIER);
483 if (query_doc_list == null) {
484 System.err.println("Error: DocumentStructureRetrieve request specified no doc nodes.\n");
485 return result;
486 }
487
488 Element doc_list = doc_.createElement(node_name+GSXML.LIST_MODIFIER);
489 result.appendChild(doc_list);
490
491 // Get the documents
492 String[] doc_ids = GSXML.getAttributeValuesFromList(query_doc_list,
493 GSXML.NODE_ID_ATT);
494 for (int i = 0; i < doc_ids.length; i++) {
495 String doc_id = doc_ids[i];
496
497 System.out.println("doc_id = "+doc_id);
498 if (OID.needsTranslating(doc_id)) {
499 doc_id = gdbm_src_.translateOID(doc_id);
500 System.out.println("translated doc_id = "+doc_id);
501 }
502
503 // Add the document to the list
504 Element doc = doc_.createElement(node_name);
505 doc_list.appendChild(doc);
506 doc.setAttribute(GSXML.NODE_ID_ATT, doc_id);
507
508
509 if (want_info) {
510
511 Element info_elem = doc_.createElement("nodeStructureInfo");
512 doc.appendChild(info_elem);
513
514 for (int j=0; j<info_types.size(); j++) {
515 String info_type = (String)info_types.get(j);
516 Element inf = getInfo(doc_id, info_type);
517 if (inf != null) {
518 info_elem.appendChild(inf);
519 }
520 }
521 }
522 if (want_structure) {
523 // all structure info goes into a nodeStructure elem
524 Element structure_elem = doc_.createElement(GSXML.NODE_STRUCTURE_ELEM);
525 doc.appendChild(structure_elem);
526
527 // Add the requested structure information
528 Element current = createDocNode(doc_id, false, false);
529
530 //Ancestors: continually add parent nodes until the root is reached
531 Element top_node = current; // the top node so far
532 if (want_ancestors) {
533 String current_id = doc_id;
534 while (true) {
535 Element parent = getParent(current_id);
536 if (parent == null)
537 break;
538
539 parent.appendChild(top_node);
540 current_id = parent.getAttribute(GSXML.NODE_ID_ATT);
541 top_node = parent;
542 }
543 }
544 // Parent: get the parent of the selected node
545 if (want_parent) {
546 Element parent = getParent(doc_id);
547 if (parent != null) {
548 parent.appendChild(current);
549 top_node = parent;
550 }
551 }
552
553
554 // now the top node is the root of the structure
555 structure_elem.appendChild(top_node);
556
557 //Siblings: get the other descendants of the selected node's parent
558 if (want_siblings) {
559 Element parent = (Element)current.getParentNode(); // this may be the structure element if there has been no request for parents or ancestors
560 String parent_id = OID.getParent(doc_id);
561
562 // add siblings, - returns a pointer to the new current node
563 current = addSiblings(parent, parent_id, doc_id);
564 }
565
566 // Children: get the descendants, but only one level deep
567 if (want_children)
568 addDescendants(current, doc_id, false);
569 // Descendants: recursively get every descendant of the selected node
570 if (want_descendants)
571 addDescendants(current, doc_id, true);
572 } // if want structure
573 } // for each doc
574 return result;
575 }
576
577
578 protected Element processDocumentMetadataRetrieve(Element request) {
579 return genericMetadataRetrieve(request, DOCUMENT);
580 }
581
582 protected Element processClassifierBrowseMetadataRetrieve(Element request) {
583 return genericMetadataRetrieve(request, CLASSIFIER);
584 }
585
586
587 /** Retrieve metadata associated with a document or classifier node*/
588 protected Element genericMetadataRetrieve(Element request, int type)
589 {
590 // Create a new (empty) result message
591 Element result = doc_.createElement(GSXML.RESPONSE_ELEM);
592
593 String node_name;
594
595 String service_name;
596 if (type==DOCUMENT) {
597 service_name = DOCUMENT_METADATA_RETRIEVE_SERVICE;
598 node_name = GSXML.DOC_NODE_ELEM;
599 } else {
600 service_name = CLASSIFIER_METADATA_SERVICE;
601 node_name = GSXML.CLASS_NODE_ELEM;
602 }
603 result.setAttribute(GSXML.FROM_ATT, service_name);
604 result.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_PROCESS);
605
606 // Get the parameters of the request
607 Element param_list = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
608 if (param_list == null) {
609 System.err.println("GS2Retrieve, DocumentMetadataRetrieve Error: missing paramList.\n");
610 return result; // Return the empty result
611 }
612
613 // The metadata information required
614 Vector metadata_list = new Vector();
615 boolean all_metadata = false;
616 // Process the request parameters
617 Element param = (Element) param_list.getFirstChild();
618 while (param != null) {
619 // Identify the metadata information desired
620 if (param.getAttribute(GSXML.NAME_ATT).equals("metadata")) {
621 String metadata = GSXML.getValue(param);
622 if (metadata.equals("all")) {
623 all_metadata = true;
624 break;
625 }
626 metadata_list.add(metadata);
627 }
628 param = (Element) param.getNextSibling();
629 }
630
631 Element node_list = doc_.createElement(node_name+GSXML.LIST_MODIFIER);
632 result.appendChild(node_list);
633
634 // Get the documents
635 Element request_node_list = (Element) GSXML.getChildByTagName(request, node_name+GSXML.LIST_MODIFIER);
636 if (request_node_list == null) {
637 System.err.println("Error: DocumentMetadataRetrieve request had no "+node_name+"List.\n");
638 return result;
639 }
640
641 NodeList request_nodes = request_node_list.getChildNodes();
642 for (int i = 0; i < request_nodes.getLength(); i++) {
643 Element request_node = (Element) request_nodes.item(i);
644 String node_id = request_node.getAttribute(GSXML.NODE_ID_ATT);
645
646 if (OID.needsTranslating(node_id)) {
647 node_id = gdbm_src_.translateOID(node_id);
648 }
649
650 // Add the document to the list
651 Element new_node = (Element)doc_.importNode(request_node, false);
652 node_list.appendChild(new_node);
653
654 // Add the requested metadata information
655 Element node_meta_list = doc_.createElement(GSXML.METADATA_ELEM+GSXML.LIST_MODIFIER);
656 new_node.appendChild(node_meta_list);
657 DBInfo info = gdbm_src_.getInfo(node_id);
658 if (info == null) {// I have had a case where it is null!
659 continue;
660 }
661 System.out.println("found the info");
662 if (all_metadata) {
663 System.out.println("trying to get all the metadata");
664 // return everything out of the database
665 Set keys = info.getKeys();
666 Iterator it = keys.iterator();
667 while(it.hasNext()) {
668 String key = (String)it.next();
669 System.out.println("getting metadata "+key);
670 String value = info.getInfo(key);
671 GSXML.addMetadata(doc_, node_meta_list, key, value);
672 }
673 } else { // just get the selected ones
674
675 for (int m = 0; m < metadata_list.size(); m++) {
676 String metadata = (String) metadata_list.get(m);
677 String value = info.getInfo(metadata);
678 GSXML.addMetadata(doc_, node_meta_list, metadata, value);
679 }
680 }
681 }
682
683 return result;
684 }
685
686
687 /** Retrieve the content of a document - implemented by concrete subclasses */
688 protected abstract Element processDocumentContentRetrieve(Element request);
689
690 /** needs to get info from gdbm database - if the calling code gets it already it may pay to pass it in instead */
691 protected String resolveImages(String doc_content, String doc_id)
692 {
693 String top_doc_id = OID.getTop(doc_id);
694 DBInfo info = gdbm_src_.getInfo(top_doc_id);
695 String archivedir = info.getInfo("archivedir");
696 String image_dir = site_http_address_ + "/collect/"+cluster_name_+"/index/assoc/"+archivedir;
697
698 // Resolve all "_httpdocimg_"s
699 doc_content = doc_content.replaceAll("_httpdocimg_", image_dir);
700 return doc_content;
701 }
702
703 protected Element getInfo(String doc_id, String info_type) {
704
705 String value="";
706 if (info_type.equals(INFO_NUM_SIBS)) {
707 String parent_id = OID.getParent(doc_id);
708 if (parent_id.equals(doc_id)) {
709 value="0";
710 } else {
711 value = String.valueOf(getNumChildren(parent_id));
712 }
713 } else if (info_type.equals(INFO_NUM_CHILDREN)) {
714 value = String.valueOf(getNumChildren(doc_id));
715 } else if (info_type.equals(INFO_SIB_POS)) {
716 String parent_id = OID.getParent(doc_id);
717 if (parent_id.equals(doc_id)) {
718 value="-1";
719 } else {
720 DBInfo info = gdbm_src_.getInfo(parent_id);
721 String contains = info.getInfo("contains");
722 contains = contains.replaceAll("\"", parent_id);
723 String [] children = contains.split(";");
724 for (int i=0;i<children.length;i++) {
725 String child_id = children[i];
726 System.out.println("child="+child_id+" doc="+doc_id);
727 if (child_id.equals(doc_id)) {
728 value = String.valueOf(i+1); // make it from 1 to length
729 break;
730 }
731 }
732 }
733 } else {
734 return null;
735 }
736 Element info_elem = doc_.createElement("info");
737 info_elem.setAttribute(GSXML.NAME_ATT, info_type);
738 info_elem.setAttribute(GSXML.VALUE_ATT, value);
739 return info_elem;
740 }
741
742 protected int getNumChildren(String doc_id) {
743 DBInfo info = gdbm_src_.getInfo(doc_id);
744 String contains = info.getInfo("contains");
745 if (contains.equals("")) {
746 return 0;
747 }
748 String [] children = contains.split(";");
749 return children.length;
750 }
751
752}
Note: See TracBrowser for help on using the repository browser.