source: main/trunk/greenstone3/src/java/org/greenstone/gsdl3/service/AbstractGS2DocumentRetrieve.java@ 26046

Last change on this file since 26046 was 26046, checked in by kjdon, 12 years ago

moved a heap of duplicated code out of service racks and into BasicDocument classes

  • Property svn:keywords set to Author Date Id Revision
File size: 12.6 KB
Line 
1/*
2 * AbstractGS2DocumentRetrieve.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// Greenstone classes
22import org.greenstone.gsdl3.core.GSException;
23import org.greenstone.gsdl3.util.BasicDocumentDatabase;
24import org.greenstone.gsdl3.util.GSXML;
25import org.greenstone.gsdl3.util.GSFile;
26import org.greenstone.gsdl3.util.OID;
27import org.greenstone.gsdl3.util.MacroResolver;
28import org.greenstone.gsdl3.util.GS2MacroResolver;
29import org.greenstone.gsdl3.util.GSConstants;
30import org.greenstone.gsdl3.util.SimpleCollectionDatabase;
31import org.greenstone.gsdl3.util.DBInfo;
32// XML classes
33import org.w3c.dom.Document;
34import org.w3c.dom.Element;
35import org.w3c.dom.NodeList;
36
37// General Java classes
38import java.io.File;
39import java.util.StringTokenizer;
40import java.util.Vector;
41import java.util.Set;
42import java.util.Iterator;
43import java.util.ArrayList;
44
45import org.apache.log4j.*;
46
47// Apache Commons
48import org.apache.commons.lang3.*;
49
50/**
51 * Implements the generic retrieval and classifier services for GS2 collections.
52 *
53 * @author Katherine Don
54 * @author Michael Dewsnip
55 */
56
57public abstract class AbstractGS2DocumentRetrieve extends AbstractDocumentRetrieve
58{
59
60 static Logger logger = Logger.getLogger(org.greenstone.gsdl3.service.AbstractGS2DocumentRetrieve.class.getName());
61
62 // protected static final String EXTLINK_PARAM = "ext"; here or in base??
63 protected String index_stem = null;
64
65 protected SimpleCollectionDatabase coll_db = null;
66 BasicDocumentDatabase gs_doc_db = null;
67 /** constructor */
68 protected AbstractGS2DocumentRetrieve()
69 {
70 this.macro_resolver = new GS2MacroResolver();
71 }
72
73 public void cleanUp()
74 {
75 super.cleanUp();
76 this.coll_db.closeDatabase();
77 this.gs_doc_db.cleanUp();
78 }
79
80 /** configure this service */
81 public boolean configure(Element info, Element extra_info)
82 {
83 if (!super.configure(info, extra_info))
84 {
85 return false;
86 }
87
88 logger.info("Configuring AbstractGS2DocumentRetrieve...");
89 //this.config_info = info;
90
91 // the index stem is either specified in the config file or is the collection name
92 Element index_stem_elem = (Element) GSXML.getChildByTagName(info, GSXML.INDEX_STEM_ELEM);
93 if (index_stem_elem != null)
94 {
95 this.index_stem = index_stem_elem.getAttribute(GSXML.NAME_ATT);
96 }
97 if (this.index_stem == null || this.index_stem.equals(""))
98 {
99 logger.error("AbstractGS2DocumentRetrieve.configure(): indexStem element not found, stem will default to collection name");
100 this.index_stem = this.cluster_name;
101 }
102
103 // find out what kind of database we have
104 Element database_type_elem = (Element) GSXML.getChildByTagName(info, GSXML.DATABASE_TYPE_ELEM);
105 String database_type = null;
106 if (database_type_elem != null)
107 {
108 database_type = database_type_elem.getAttribute(GSXML.NAME_ATT);
109 }
110 if (database_type == null || database_type.equals(""))
111 {
112 database_type = "gdbm"; // the default
113 }
114 coll_db = new SimpleCollectionDatabase(database_type);
115 if (!coll_db.databaseOK())
116 {
117 logger.error("Couldn't create the collection database of type " + database_type);
118 return false;
119 }
120
121 // Open database for querying
122 String coll_db_file = GSFile.collectionDatabaseFile(this.site_home, this.cluster_name, this.index_stem, database_type);
123 if (!this.coll_db.openDatabase(coll_db_file, SimpleCollectionDatabase.READ))
124 {
125 logger.error("Could not open collection database!");
126 return false;
127 }
128
129 gs_doc_db = new BasicDocumentDatabase(this.doc, database_type, this.site_home, this.cluster_name, this.index_stem);
130 if (!gs_doc_db.isValid())
131 {
132 logger.error("Failed to open Document Database.");
133 return false;
134 }
135 this.gs_doc = gs_doc_db;
136
137 // we need to set the database for our GS2 macro resolver
138 GS2MacroResolver gs2_macro_resolver = (GS2MacroResolver) this.macro_resolver;
139 gs2_macro_resolver.setDB(this.coll_db);
140
141 return true;
142 }
143
144 /** if id ends in .fc, .pc etc, then translate it to the correct id */
145 protected String translateId(String node_id)
146 {
147 return OID.translateOID(this.coll_db, node_id); //return this.coll_db.translateOID(node_id);
148 }
149
150 /**
151 * if an id is not a greenstone id (an external id) then translate it to a
152 * greenstone one
153 */
154 protected String translateExternalId(String node_id)
155 {
156 return this.coll_db.externalId2OID(node_id);
157 }
158
159 /**
160 * returns the id of the root node of the document containing node node_id.
161 * . may be the same as node_id
162 */
163 protected String getRootId(String node_id)
164 {
165 return this.gs_doc.getRootId(node_id);
166 }
167
168
169
170 /**
171 * get the metadata for the classifier node node_id returns a metadataList
172 * element: <metadataList><metadata
173 * name="xxx">value</metadata></metadataList>
174 */
175 // assumes only one value per metadata
176 protected Element getMetadataList(String node_id, boolean all_metadata, ArrayList<String> metadata_names) throws GSException
177 {
178 Element metadata_list = this.doc.createElement(GSXML.METADATA_ELEM + GSXML.LIST_MODIFIER);
179 DBInfo info = this.coll_db.getInfo(node_id);
180 if (info == null)
181 {
182 return null;
183 }
184 String lang = "en"; // why do we need this??
185 if (all_metadata)
186 {
187 // return everything out of the database
188 Set<String> keys = info.getKeys();
189 Iterator<String> it = keys.iterator();
190 while (it.hasNext())
191 {
192 String key = it.next();
193 //String value = info.getInfo(key);
194 Vector<String> values = info.getMultiInfo(key);
195 for (int i = 0; i < values.size(); i++)
196 {
197 GSXML.addMetadata(this.doc, metadata_list, key, this.macro_resolver.resolve(values.elementAt(i), lang, MacroResolver.SCOPE_META, node_id));
198 }
199 }
200
201 }
202 else
203 {
204 for (int i = 0; i < metadata_names.size(); i++)
205 {
206 String meta_name = metadata_names.get(i);
207 String value = getMetadata(node_id, info, meta_name, lang);
208 GSXML.addMetadata(this.doc, metadata_list, meta_name, value);
209 }
210 }
211 return metadata_list;
212 }
213
214
215 protected int getNumChildren(String node_id)
216 {
217 return this.gs_doc.getNumChildren(node_id);
218 }
219
220
221 /**
222 * returns the content of a node should return a nodeContent element:
223 * <nodeContent>text content or other elements</nodeContent>
224 */
225 abstract protected Element getNodeContent(String doc_id, String lang) throws GSException;
226
227 protected String getMetadata(String node_id, DBInfo info, String metadata, String lang)
228 {
229 String pos = "";
230 String relation = "";
231 String separator = ", ";
232 int index = metadata.indexOf(GSConstants.META_RELATION_SEP);
233 if (index == -1)
234 {
235 Vector<String> values = info.getMultiInfo(metadata);
236 if (values != null)
237 {
238 // just a plain meta entry eg dc.Title
239 StringBuffer result = new StringBuffer();
240 boolean first = true;
241 for (int i = 0; i < values.size(); i++)
242 {
243 if (first)
244 {
245 first = false;
246 }
247 else
248 {
249 result.append(separator);
250 }
251 result.append(this.macro_resolver.resolve(values.elementAt(i), lang, MacroResolver.SCOPE_META, node_id));
252 }
253 return result.toString();
254 }
255 else
256 {
257 String result = info.getInfo(metadata);
258 return this.macro_resolver.resolve(result, lang, MacroResolver.SCOPE_META, node_id);
259 }
260 }
261
262 String temp = metadata.substring(0, index);
263 metadata = metadata.substring(index + 1);
264 // check for pos on the front, indicating which piece of meta the user wants
265 // pos can be "first", "last" or the position value of the requested piece of metadata
266 if (temp.startsWith(GSConstants.META_POS) || temp.equals("all"))
267 {
268 if (temp.startsWith(GSConstants.META_POS)) {
269 temp = temp.substring(GSConstants.META_POS.length());
270 pos = temp;
271 }
272
273 index = metadata.indexOf(GSConstants.META_RELATION_SEP);
274 if (index == -1)
275 {
276 temp = "";
277 }
278 else
279 {
280 temp = metadata.substring(0, index);
281 metadata = metadata.substring(index + 1);
282 }
283 }
284
285 // now check for relational info
286 if (temp.equals("parent") || temp.equals("root") || temp.equals("ancestors")
287 || temp.equals("siblings") || temp.equals("children") || temp.equals("descendants"))
288 { // "current" "siblings" "children" "descendants"
289 // gets all siblings by default
290 relation = temp;
291 index = metadata.indexOf(GSConstants.META_RELATION_SEP);
292 if (index == -1)
293 {
294 temp = "";
295 }
296 else
297 {
298 temp = metadata.substring(0, index);
299 metadata = metadata.substring(index + 1);
300 }
301 }
302
303 // now look for separator info
304 if (temp.startsWith(GSConstants.META_SEPARATOR_SEP) && temp.endsWith(GSConstants.META_SEPARATOR_SEP))
305 {
306 separator = temp.substring(1, temp.length() - 1);
307
308 }
309
310 String relation_id = node_id;
311 if (relation.equals("parent") || relation.equals("ancestors"))
312 {
313 relation_id = OID.getParent(node_id);
314 // parent or ancestor does not include self
315 if (relation_id.equals(node_id))
316 {
317 return "";
318 }
319 }
320 else if (relation.equals("root"))
321 {
322 relation_id = OID.getTop(node_id);
323 }
324
325 // now we either have a single node, or we have ancestors
326 DBInfo relation_info;
327 if (relation_id.equals(node_id))
328 {
329 relation_info = info;
330 }
331 else
332 {
333 relation_info = this.coll_db.getInfo(relation_id);
334 }
335 if (relation_info == null)
336 {
337 return "";
338 }
339
340 StringBuffer result = new StringBuffer();
341
342 Vector<String> values = relation_info.getMultiInfo(metadata);
343
344 if (!pos.equals("")) // if a particular position was specified, so not multiple values for the metadata
345 {
346 String meta = "";
347 if (values != null) {
348 if(pos.equals(GSConstants.META_FIRST)) {
349 meta = values.firstElement();
350 } else if(pos.equals(GSConstants.META_LAST)) {
351 meta = values.lastElement();
352 } else {
353 int position = Integer.parseInt(pos);
354 if(position < values.size()) {
355 meta = values.elementAt(position);
356 }
357 }
358 } // else ""
359
360 result.append(this.macro_resolver.resolve(meta, lang, MacroResolver.SCOPE_META, relation_id));
361 }
362 else
363 {
364 if (values != null)
365 {
366 boolean first = true;
367 for (int i = 0; i < values.size(); i++)
368 {
369 if (first)
370 {
371 first = false;
372 }
373 else
374 {
375 result.append(separator);
376 }
377 result.append(this.macro_resolver.resolve(values.elementAt(i), lang, MacroResolver.SCOPE_META, relation_id));
378 }
379 }
380 logger.info(result);
381 }
382 // if not ancestors, then this is all we do
383 if (!relation.equals("ancestors"))
384 {
385 return result.toString();
386 }
387
388 // now do the ancestors
389 String current_id = relation_id;
390 relation_id = OID.getParent(current_id);
391 while (!relation_id.equals(current_id))
392 {
393 relation_info = this.coll_db.getInfo(relation_id);
394 if (relation_info == null)
395 return result.toString();
396
397 values = relation_info.getMultiInfo(metadata);
398 if (!pos.equals("")) // if a particular position was specified, so not multiple values for the metadata
399 {
400 String meta = "";
401 if (values != null) {
402 if(pos.equals(GSConstants.META_FIRST)) {
403 meta = values.firstElement();
404 } else if(pos.equals(GSConstants.META_LAST)) {
405 meta = values.lastElement();
406 } else {
407 int position = Integer.parseInt(pos);
408 if(position < values.size()) {
409 meta = values.elementAt(position);
410 }
411 }
412 } // else ""
413
414 result.insert(0, separator);
415 result.insert(0, this.macro_resolver.resolve(meta, lang, MacroResolver.SCOPE_META, relation_id));
416 }
417 else
418 {
419 if (values != null)
420 {
421 for (int i = values.size() - 1; i >= 0; i--)
422 {
423 result.insert(0, separator);
424 result.insert(0, this.macro_resolver.resolve(values.elementAt(i), lang, MacroResolver.SCOPE_META, relation_id));
425 }
426 }
427
428 }
429 current_id = relation_id;
430 relation_id = OID.getParent(current_id);
431 }
432 return result.toString();
433 }
434
435 /**
436 * needs to get info from collection database - if the calling code gets it
437 * already it may pay to pass it in instead
438 */
439 protected String resolveTextMacros(String doc_content, String doc_id, String lang)
440 {
441 // resolve any collection specific macros
442 doc_content = macro_resolver.resolve(doc_content, lang, MacroResolver.SCOPE_TEXT, doc_id);
443 return doc_content;
444 }
445
446
447
448}
Note: See TracBrowser for help on using the repository browser.