1 | /*
|
---|
2 | * GS2MGPPRetrieve.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 | // greenstone classes
|
---|
22 | import org.greenstone.mgpp.*;
|
---|
23 | import org.greenstone.gdbm.*;
|
---|
24 | import org.greenstone.gsdl3.util.*;
|
---|
25 |
|
---|
26 | // xml classes
|
---|
27 | import org.w3c.dom.Document;
|
---|
28 | import org.w3c.dom.Node;
|
---|
29 | import org.w3c.dom.Text;
|
---|
30 | import org.w3c.dom.Element;
|
---|
31 | import org.w3c.dom.NodeList;
|
---|
32 |
|
---|
33 | // general java classes
|
---|
34 | import java.util.HashMap;
|
---|
35 | import java.util.Vector;
|
---|
36 | import java.util.Set;
|
---|
37 | import java.util.Map;
|
---|
38 | import java.util.Iterator;
|
---|
39 | import java.util.Locale;
|
---|
40 |
|
---|
41 | /**
|
---|
42 | * A ServiceRack class for retrieval in greenstone 2 MGPP collections
|
---|
43 | *
|
---|
44 | * @author <a href="mailto:[email protected]">Katherine Don</a>
|
---|
45 | * @version $Revision: 3649 $
|
---|
46 | * @see ServiceRack
|
---|
47 | */
|
---|
48 | public class GS2MGPPRetrieve
|
---|
49 | extends ServiceRack {
|
---|
50 |
|
---|
51 | // these strings must match what is found in the properties file
|
---|
52 | // the services on offer
|
---|
53 | private static final String DOCUMENT_RETRIEVE_SERVICE = "DocumentRetrieve";
|
---|
54 | private static final String METADATA_RETRIEVE_SERVICE = "MetadataRetrieve";
|
---|
55 |
|
---|
56 | // params used
|
---|
57 | private static final String LEVEL_PARAM = "level";
|
---|
58 |
|
---|
59 | // elements used in the config file that are specific to this class
|
---|
60 | private static final String DEFAULT_LEVEL_ELEM = "defaultLevel";
|
---|
61 |
|
---|
62 | private MGPPWrapper mgpp_src_=null;
|
---|
63 | private GDBMWrapper gdbm_src_=null;
|
---|
64 |
|
---|
65 | private String default_level_=null;
|
---|
66 |
|
---|
67 | private Element config_info_ = null;
|
---|
68 |
|
---|
69 | public GS2MGPPRetrieve() {
|
---|
70 | mgpp_src_ = new MGPPWrapper();
|
---|
71 | gdbm_src_ = new GDBMWrapper();
|
---|
72 |
|
---|
73 | }
|
---|
74 |
|
---|
75 | /** configure this service */
|
---|
76 | public boolean configure(Element info) {
|
---|
77 |
|
---|
78 | System.out.println("configuring GS2MGPPRetrieve");
|
---|
79 | config_info_ = info;
|
---|
80 |
|
---|
81 | // get the default level out of <defaultLevel>
|
---|
82 | Element def = (Element)GSXML.getChildByTagName(info, DEFAULT_LEVEL_ELEM);
|
---|
83 | if (def !=null) {
|
---|
84 | default_level_ = def.getAttribute(GSXML.NAME_ATT);
|
---|
85 | }
|
---|
86 | if (default_level_==null||default_level_.equals("")) {
|
---|
87 | System.err.println("Error: default level not specified!");
|
---|
88 | return false;
|
---|
89 | }
|
---|
90 |
|
---|
91 | // set up which services are available for this collection
|
---|
92 | Element e = null;
|
---|
93 | // these entries should reflect the build config file - some services may not be available depending on how the collection was built.
|
---|
94 |
|
---|
95 | // set up short_service_info_ - for now just has name and type
|
---|
96 | e = doc_.createElement(GSXML.SERVICE_ELEM);
|
---|
97 | e.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_QUERY);
|
---|
98 | e.setAttribute(GSXML.NAME_ATT, DOCUMENT_RETRIEVE_SERVICE);
|
---|
99 | short_service_info_.appendChild(e);
|
---|
100 |
|
---|
101 | e = doc_.createElement(GSXML.SERVICE_ELEM);
|
---|
102 | e.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_QUERY);
|
---|
103 | e.setAttribute(GSXML.NAME_ATT, METADATA_RETRIEVE_SERVICE);
|
---|
104 | short_service_info_.appendChild(e);
|
---|
105 |
|
---|
106 | // set up service_info_map_ - for now, just has the same elements as above
|
---|
107 | // should have full details about each service incl params lists etc.
|
---|
108 | // do the text query one - for now a static list. later use buildcfg.xml values to dynamically change this
|
---|
109 | e = doc_.createElement(GSXML.SERVICE_ELEM);
|
---|
110 | e.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_QUERY);
|
---|
111 | e.setAttribute(GSXML.NAME_ATT, DOCUMENT_RETRIEVE_SERVICE);
|
---|
112 | service_info_map_.put(DOCUMENT_RETRIEVE_SERVICE, e);
|
---|
113 |
|
---|
114 | e = doc_.createElement(GSXML.SERVICE_ELEM);
|
---|
115 | e.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_QUERY);
|
---|
116 | e.setAttribute(GSXML.NAME_ATT, METADATA_RETRIEVE_SERVICE);
|
---|
117 | service_info_map_.put(METADATA_RETRIEVE_SERVICE, e);
|
---|
118 |
|
---|
119 | if (gdbm_src_.openDatabase(GSFile.GDBMDatabaseFile(site_home_, cluster_name_), GDBMWrapper.READER)) {
|
---|
120 | return true;
|
---|
121 | }
|
---|
122 | else {
|
---|
123 | System.err.println("couldn't open gdbm database!");
|
---|
124 | return false;
|
---|
125 | }
|
---|
126 | }
|
---|
127 |
|
---|
128 |
|
---|
129 | /** creates a display element containing all the text strings needed to display the service page, in the language specified
|
---|
130 | * these retrieval services dont get displayed to the users - they are only used internally by the actions. so this returns an empty display element*/
|
---|
131 | protected Element createServiceDisplay(String service, String lang) {
|
---|
132 | return doc_.createElement(GSXML.DISPLAY_ELEM);
|
---|
133 | }
|
---|
134 |
|
---|
135 |
|
---|
136 | /** retrieve a document */
|
---|
137 | protected Element processDocumentRetrieve(Element request) {
|
---|
138 |
|
---|
139 | // where the mgpp text files are
|
---|
140 | String basedir = GSFile.collectionBaseDir(site_home_,
|
---|
141 | cluster_name_);
|
---|
142 | String textdir = GSFile.collectionTextPath(cluster_name_);
|
---|
143 |
|
---|
144 | // an empty result
|
---|
145 | Element result = doc_.createElement(GSXML.RESPONSE_ELEM);
|
---|
146 | String from = GSPath.appendLink(cluster_name_, DOCUMENT_RETRIEVE_SERVICE);
|
---|
147 | result.setAttribute(GSXML.FROM_ATT, from);
|
---|
148 | result.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_QUERY);
|
---|
149 | Element result_doc = doc_.createElement(GSXML.CONTENT_ELEM);
|
---|
150 | result.appendChild(result_doc);
|
---|
151 |
|
---|
152 | // get param list and content
|
---|
153 | Element param_elem=null;
|
---|
154 | Element content_elem=null;
|
---|
155 | Node n = request.getFirstChild();
|
---|
156 | while (n!=null) {
|
---|
157 | String node_name = n.getNodeName();
|
---|
158 | if (node_name.equals(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER)) {
|
---|
159 | param_elem = (Element)n;
|
---|
160 | } else if (node_name.equals(GSXML.CONTENT_ELEM)) {
|
---|
161 | content_elem = (Element)n;
|
---|
162 | }
|
---|
163 | n = n.getNextSibling();
|
---|
164 | }
|
---|
165 |
|
---|
166 | if (param_elem==null || content_elem==null) {
|
---|
167 | System.err.println("GS2MGPPRetrieve: malformed request sent to DocumentRetrieve service");
|
---|
168 | return result; // empty result
|
---|
169 | }
|
---|
170 |
|
---|
171 | HashMap params = GSXML.extractParams(param_elem);
|
---|
172 |
|
---|
173 | // get docs from mgpp
|
---|
174 | String level = (String)params.get(LEVEL_PARAM); // level at which to retrieve a doc
|
---|
175 | if (level==null) {
|
---|
176 | level=default_level_;
|
---|
177 | }
|
---|
178 | // get the text for each doc in the list
|
---|
179 | String []ids = GSXML.getDocumentNameList(content_elem);
|
---|
180 | for (int j=0; j<ids.length; j++) {
|
---|
181 | long real_num = gdbm_src_.oid2Docnum(ids[j]);
|
---|
182 | String document = mgpp_src_.getDocument(basedir, textdir, level, real_num);
|
---|
183 | // for now, stick it in a text node - eventually should be parsed as xml??
|
---|
184 | // something funny with the doc -
|
---|
185 | Element new_doc = GSXML.createDocumentElement(doc_, ids[j]);
|
---|
186 | GSXML.addDocText(doc_, new_doc, document);
|
---|
187 | result_doc.appendChild(new_doc);
|
---|
188 | }
|
---|
189 | return result;
|
---|
190 | }
|
---|
191 |
|
---|
192 | /** retrieve metadata */
|
---|
193 | protected Element processMetadataRetrieve(Element request) {
|
---|
194 | Element result = doc_.createElement(GSXML.RESPONSE_ELEM);
|
---|
195 | String from = GSPath.appendLink(cluster_name_, METADATA_RETRIEVE_SERVICE);
|
---|
196 | result.setAttribute(GSXML.FROM_ATT, from);
|
---|
197 | result.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_QUERY);
|
---|
198 | Element result_content = doc_.createElement(GSXML.CONTENT_ELEM);
|
---|
199 | result.appendChild(result_content);
|
---|
200 | Element document_list = doc_.createElement(GSXML.DOCUMENT_ELEM+GSXML.LIST_MODIFIER);
|
---|
201 | result_content.appendChild(document_list);
|
---|
202 | // get the metadata
|
---|
203 |
|
---|
204 | Element content = (Element)request.getElementsByTagName(GSXML.CONTENT_ELEM).item(0);
|
---|
205 | if (content==null) {
|
---|
206 | // error: query had no content!! - should make an error message
|
---|
207 | return result;
|
---|
208 | }
|
---|
209 | String []metas = GSXML.getMetaNameList(content);
|
---|
210 | String []ids = GSXML.getDocumentNameList(content);
|
---|
211 | for (int j=0; j<ids.length; j++) { // for each document
|
---|
212 | Element doc = GSXML.createDocumentElement(doc_, ids[j]);
|
---|
213 | Element list = GSXML.addMetaList(doc_, doc);
|
---|
214 | DBInfo info = gdbm_src_.getInfo(ids[j]);
|
---|
215 | for (int m=0; m<metas.length; m++) {
|
---|
216 | String value = info.getInfo(metas[m]);
|
---|
217 | GSXML.addMetadata(doc_, list, metas[m], value);
|
---|
218 | }
|
---|
219 | document_list.appendChild(doc);
|
---|
220 | }
|
---|
221 | return result;
|
---|
222 | }
|
---|
223 |
|
---|
224 |
|
---|
225 |
|
---|
226 |
|
---|
227 | }
|
---|
228 |
|
---|
229 |
|
---|