[3492] | 1 | /*
|
---|
| 2 | * GSDL2ClassifierServices.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 |
|
---|
| 21 | import org.greenstone.gdbm.*;
|
---|
| 22 | import org.greenstone.gsdl3.util.*;
|
---|
| 23 |
|
---|
| 24 | import org.w3c.dom.Document;
|
---|
| 25 | import org.w3c.dom.Node;
|
---|
| 26 | import org.w3c.dom.Text;
|
---|
| 27 | import org.w3c.dom.Element;
|
---|
| 28 | import org.w3c.dom.NodeList;
|
---|
| 29 | /**
|
---|
| 30 | * A ServicesImpl class for GSDL2-style Classifiers
|
---|
| 31 | *
|
---|
| 32 | * @author <a href="mailto:[email protected]">Katherine Don</a>
|
---|
| 33 | * @version $Revision: 3567 $
|
---|
| 34 | */
|
---|
| 35 | public class GSDL2ClassifierServices
|
---|
| 36 | extends ServicesImpl {
|
---|
| 37 |
|
---|
[3512] | 38 | // the services on offer
|
---|
| 39 | private static final String CLASSIFIER_SERVICE = "ClassifierBrowse";
|
---|
| 40 |
|
---|
[3492] | 41 | /** gdbm wrapper */
|
---|
| 42 | private GDBMWrapper gdbm_src_=null;
|
---|
| 43 |
|
---|
| 44 | public GSDL2ClassifierServices() {
|
---|
| 45 | gdbm_src_ = new GDBMWrapper();
|
---|
| 46 |
|
---|
| 47 | }
|
---|
| 48 |
|
---|
[3567] | 49 | /** creates a display element containing all the text strings needed to display the service page, in the language specified */
|
---|
| 50 | protected Element createServiceDisplay(String service, String lang) {
|
---|
| 51 | Element display = doc_.createElement(GSXML.DISPLAY_ELEM);
|
---|
| 52 | display.appendChild(GSXML.createTextElement(doc_, GSXML.DISPLAY_NAME_ELEM, getTextString(service+".name", lang)));
|
---|
| 53 | display.appendChild(GSXML.createTextElement(doc_, GSXML.DISPLAY_SUBMIT_ELEM, getTextString(service+".submit", lang)));
|
---|
| 54 |
|
---|
| 55 | Element param;
|
---|
| 56 |
|
---|
| 57 | return display;
|
---|
| 58 |
|
---|
| 59 | }
|
---|
| 60 |
|
---|
[3492] | 61 | /** processes the request */
|
---|
| 62 | protected Element processService(String name, Element request) {
|
---|
| 63 |
|
---|
[3512] | 64 | if (!name.equals(CLASSIFIER_SERVICE)) {
|
---|
[3492] | 65 | System.err.println("GSDL2ClassifierServices:should never get here. service type wrong:"+name);
|
---|
| 66 | return null;
|
---|
| 67 | }
|
---|
| 68 | // the response element
|
---|
[3512] | 69 | Element result = doc_.createElement(GSXML.RESPONSE_ELEM);
|
---|
| 70 | String from = GSPath.appendLink(cluster_name_, CLASSIFIER_SERVICE);
|
---|
| 71 | result.setAttribute(GSXML.FROM_ATT, from);
|
---|
| 72 | result.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_QUERY);
|
---|
| 73 | Element result_content = doc_.createElement(GSXML.CONTENT_ELEM);
|
---|
[3492] | 74 | result.appendChild(result_content);
|
---|
| 75 |
|
---|
| 76 | // get the classifier name out of request content - assume only one for now
|
---|
[3512] | 77 | String path = GSXML.CONTENT_ELEM;
|
---|
| 78 | path = GSPath.appendLink(path, GSXML.RESOURCE_ELEM+GSXML.LIST_MODIFIER);
|
---|
| 79 | path = GSPath.appendLink(path, GSXML.RESOURCE_ELEM);
|
---|
| 80 |
|
---|
| 81 | Element elem = (Element)GSXML.getNodeByPath(request, path);
|
---|
| 82 | String elem_name = elem.getAttribute(GSXML.NAME_ATT);
|
---|
[3492] | 83 | System.out.println("elem name="+elem_name);
|
---|
| 84 |
|
---|
| 85 | if (OID.needsTranslating(elem_name)) {
|
---|
| 86 | System.out.println("translating oid");
|
---|
| 87 | elem_name = gdbm_src_.translateOID(elem_name);
|
---|
| 88 | }
|
---|
| 89 | // get the info from gdbm db
|
---|
| 90 | DBInfo info = gdbm_src_.getInfo(elem_name);
|
---|
| 91 |
|
---|
| 92 | // build up the XML
|
---|
[3512] | 93 | Element classifier = doc_.createElement(GSXML.CLASSIFIER_ELEM);
|
---|
[3492] | 94 |
|
---|
| 95 | String classifier_name = OID.getTop(elem_name);
|
---|
[3512] | 96 | classifier.setAttribute(GSXML.NAME_ATT, classifier_name);
|
---|
[3492] | 97 | result_content.appendChild(classifier);
|
---|
| 98 | DBInfo top_info;
|
---|
| 99 |
|
---|
| 100 | if (elem_name.equals(classifier_name)) {
|
---|
| 101 | top_info = info;
|
---|
| 102 | } else {
|
---|
| 103 | top_info = gdbm_src_.getInfo(classifier_name);
|
---|
| 104 | }
|
---|
| 105 |
|
---|
| 106 | // a substitute template - should be read from config file
|
---|
| 107 | //String extra = "<stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform'><xsl:template match='resource' priority='3'><xsl:param name='collName'>coll-name</xsl:param><xsl:variable name='library' select='ancestor::page/config/library_name'/><td><a href='{$library}?a=r&c={$collName}&r={@name}'><img src='interfaces/default/images/book.gif' width='18' height='11' border='0'/></a></td><td>***</td><td><xsl:value-of select='metadataList/metadata[@name=\"Title\"]'/></td></xsl:template></stylesheet>";
|
---|
| 108 | //Element extra_doc = converter_.getDOM(extra).getDocumentElement();
|
---|
| 109 | //result.appendChild(doc_.importNode(extra_doc, true));
|
---|
| 110 |
|
---|
| 111 | // get the classifier attributes
|
---|
| 112 | String type = top_info.getInfo("childtype");
|
---|
| 113 | if (type.equals("VList")) {
|
---|
| 114 |
|
---|
| 115 | classifier.setAttribute("type", "vertical");
|
---|
| 116 | classifier.setAttribute("interleave", "true");
|
---|
| 117 | classifier.setAttribute("link", "icon");
|
---|
| 118 | }
|
---|
| 119 | else if (type.equals("HList")){
|
---|
| 120 | classifier.setAttribute("type", "horizontal");
|
---|
| 121 | classifier.setAttribute("interleave", "false");
|
---|
| 122 | classifier.setAttribute("link", "name");
|
---|
| 123 | } else {
|
---|
| 124 | System.out.println("unknown type!!! check this");
|
---|
| 125 | classifier.setAttribute("type", "vertical");
|
---|
| 126 | classifier.setAttribute("interleave", "true");
|
---|
| 127 | classifier.setAttribute("link", "icon");
|
---|
| 128 | }
|
---|
| 129 |
|
---|
| 130 | Element this_node;
|
---|
| 131 | if (elem_name.equals(classifier_name)) {
|
---|
| 132 | this_node = classifier;
|
---|
| 133 | } else { // have to build up the parents
|
---|
| 134 | // the current node
|
---|
| 135 | this_node = doc_.createElement("node");
|
---|
| 136 | this_node.setAttribute("name", elem_name);
|
---|
| 137 | type = info.getInfo("childtype");
|
---|
| 138 | if (type.equals("VList")) {
|
---|
| 139 | this_node.setAttribute("type", "vertical");
|
---|
| 140 | } else {
|
---|
| 141 | this_node.setAttribute("type", "horzizontal");
|
---|
| 142 | }
|
---|
| 143 | String title = info.getInfo("Title");
|
---|
[3512] | 144 | Element meta_list = doc_.createElement(GSXML.METADATA_ELEM+GSXML.LIST_MODIFIER);
|
---|
[3492] | 145 | GSXML.addMetadata(doc_, meta_list, "Title", title);
|
---|
| 146 | this_node.appendChild(meta_list);
|
---|
| 147 |
|
---|
| 148 | System.out.println("classifier_name="+classifier_name);
|
---|
| 149 | System.out.println("this name="+elem_name);
|
---|
| 150 | // the parents
|
---|
| 151 | String middle_name = OID.getParent(elem_name);
|
---|
| 152 | Element middle_node = this_node;
|
---|
| 153 | while (!middle_name.equals(classifier_name)) {
|
---|
| 154 | System.out.println("middle_name="+middle_name);
|
---|
| 155 | // add in a new node
|
---|
| 156 | Element temp_node = doc_.createElement("node");
|
---|
| 157 | temp_node.setAttribute("name", middle_name);
|
---|
| 158 |
|
---|
| 159 | DBInfo i = gdbm_src_.getInfo(middle_name);
|
---|
| 160 | type = i.getInfo("childtype");
|
---|
| 161 | if (type.equals("VList")) {
|
---|
| 162 | temp_node.setAttribute("type", "vertical");
|
---|
| 163 | } else {
|
---|
| 164 | temp_node.setAttribute("type", "horzizontal");
|
---|
| 165 | }
|
---|
| 166 | title = i.getInfo("Title");
|
---|
[3512] | 167 | meta_list = doc_.createElement(GSXML.METADATA_ELEM+GSXML.LIST_MODIFIER);
|
---|
[3492] | 168 | GSXML.addMetadata(doc_, meta_list, "Title", title);
|
---|
| 169 | temp_node.appendChild(meta_list);
|
---|
| 170 |
|
---|
| 171 | // link into the tree
|
---|
| 172 | temp_node.appendChild(middle_node);
|
---|
| 173 | middle_node = temp_node;
|
---|
| 174 | middle_name = OID.getParent(middle_name);
|
---|
| 175 | }
|
---|
| 176 | classifier.appendChild(middle_node);
|
---|
| 177 | }
|
---|
| 178 | // get the child nodes
|
---|
| 179 | String contains = info.getInfo("contains");
|
---|
| 180 | // replace the " with parent name
|
---|
| 181 | contains = OID.translateParent(contains, elem_name);
|
---|
| 182 | String [] children = contains.split(";");
|
---|
| 183 | for (int i=0; i<children.length; i++) {
|
---|
| 184 | String child = children[i];
|
---|
| 185 | DBInfo child_info = gdbm_src_.getInfo(child);
|
---|
| 186 | String title = child_info.getInfo("Title");
|
---|
| 187 | Element node;
|
---|
| 188 | if (child.startsWith("HASH")) {
|
---|
| 189 | // a resource
|
---|
[3512] | 190 | node = doc_.createElement(GSXML.RESOURCE_ELEM);
|
---|
[3492] | 191 | } else { // a node in the classification
|
---|
| 192 | node = doc_.createElement("node");
|
---|
| 193 |
|
---|
| 194 | }
|
---|
| 195 |
|
---|
[3512] | 196 | node.setAttribute(GSXML.NAME_ATT, child);
|
---|
| 197 | Element meta_list = doc_.createElement(GSXML.METADATA_ELEM+GSXML.LIST_MODIFIER);
|
---|
[3492] | 198 | GSXML.addMetadata(doc_, meta_list, "Title", title);
|
---|
| 199 | node.appendChild(meta_list);
|
---|
| 200 | this_node.appendChild(node);
|
---|
| 201 |
|
---|
| 202 | }
|
---|
| 203 |
|
---|
| 204 | return result;
|
---|
| 205 |
|
---|
| 206 | }
|
---|
| 207 |
|
---|
| 208 |
|
---|
| 209 | /** configure this service */
|
---|
| 210 | public boolean configure(Element info) {
|
---|
| 211 |
|
---|
| 212 | System.out.println("configuring GSDL2ClassifierServices");
|
---|
| 213 |
|
---|
| 214 | Element e = null;
|
---|
| 215 | // these entries should reflect the build config file - some services may not be available depending on how the colleciton was built.
|
---|
| 216 | // set up short_service_info_ - for now just has name and type
|
---|
[3512] | 217 | e = doc_.createElement(GSXML.SERVICE_ELEM);
|
---|
| 218 | e.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_QUERY);
|
---|
| 219 | e.setAttribute(GSXML.NAME_ATT, CLASSIFIER_SERVICE);
|
---|
[3492] | 220 | short_service_info_.appendChild(e);
|
---|
| 221 |
|
---|
| 222 | // set up service_info_map_ - for now, just has the same elements as above
|
---|
| 223 | // should have full details about each service incl params lists etc.
|
---|
[3512] | 224 | e = doc_.createElement(GSXML.SERVICE_ELEM);
|
---|
| 225 | e.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_QUERY);
|
---|
| 226 | e.setAttribute(GSXML.NAME_ATT, CLASSIFIER_SERVICE);
|
---|
[3492] | 227 |
|
---|
| 228 | // get the classifier list out of the service info
|
---|
[3512] | 229 | Node classList = GSXML.getChildByTagName(info, GSXML.CLASSIFIER_ELEM+GSXML.LIST_MODIFIER);
|
---|
[3492] | 230 | e.appendChild(doc_.importNode(classList, true));
|
---|
| 231 |
|
---|
[3512] | 232 | service_info_map_.put(CLASSIFIER_SERVICE, e);
|
---|
[3492] | 233 |
|
---|
| 234 | if (gdbm_src_.openDatabase(GSFile.GDBMDatabaseFile(site_home_, cluster_name_), GDBMWrapper.READER)) {
|
---|
| 235 | return true;
|
---|
| 236 | }
|
---|
| 237 | else {
|
---|
| 238 | System.err.println("couldn't open gdbm database!");
|
---|
| 239 | return false;
|
---|
| 240 | }
|
---|
| 241 |
|
---|
| 242 | }
|
---|
| 243 |
|
---|
| 244 |
|
---|
| 245 | }
|
---|
| 246 |
|
---|
| 247 |
|
---|