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

Last change on this file since 33341 was 33341, checked in by kjdon, 5 years ago

tidied up relational metadata retrieval. implemented descendants and entire. make all metadata come from getMetaValuesForOID - that way all get the [xxx] metadata replacement opportunity. now it handles [parent(Top):assocfilepath]

  • Property svn:keywords set to Author Date Id Revision
File size: 12.9 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 */
54
55public abstract class AbstractGS2DocumentRetrieve extends AbstractDocumentRetrieve
56{
57
58 static Logger logger = Logger.getLogger(org.greenstone.gsdl3.service.AbstractGS2DocumentRetrieve.class.getName());
59
60 // protected static final String EXTLINK_PARAM = "ext"; here or in base??
61 protected String index_stem = null;
62
63 protected SimpleCollectionDatabase coll_db = null;
64 BasicDocumentDatabase gs_doc_db = null;
65 /** constructor */
66 protected AbstractGS2DocumentRetrieve()
67 {
68 this.macro_resolver = new GS2MacroResolver();
69 }
70
71 public void cleanUp()
72 {
73 super.cleanUp();
74 this.coll_db.closeDatabase();
75 this.gs_doc_db.cleanUp();
76 }
77
78 /** configure this service */
79 public boolean configure(Element info, Element extra_info)
80 {
81 if (!super.configure(info, extra_info))
82 {
83 return false;
84 }
85
86 logger.info("Configuring AbstractGS2DocumentRetrieve...");
87 //this.config_info = info;
88
89 // the index stem is either specified in the config file or is the collection name
90 Element index_stem_elem = (Element) GSXML.getChildByTagName(info, GSXML.INDEX_STEM_ELEM);
91 if (index_stem_elem != null)
92 {
93 this.index_stem = index_stem_elem.getAttribute(GSXML.NAME_ATT);
94 }
95 if (this.index_stem == null || this.index_stem.equals(""))
96 {
97 logger.error("AbstractGS2DocumentRetrieve.configure(): indexStem element not found, stem will default to collection name");
98 this.index_stem = this.cluster_name;
99 }
100
101 // find out what kind of database we have
102 Element database_type_elem = (Element) GSXML.getChildByTagName(info, GSXML.DATABASE_TYPE_ELEM);
103 String database_type = null;
104 if (database_type_elem != null)
105 {
106 database_type = database_type_elem.getAttribute(GSXML.NAME_ATT);
107 }
108 if (database_type == null || database_type.equals(""))
109 {
110 database_type = "gdbm"; // the default
111 }
112 coll_db = new SimpleCollectionDatabase(database_type);
113 if (!coll_db.databaseOK())
114 {
115 logger.error("Couldn't create the collection database of type " + database_type);
116 return false;
117 }
118
119 // Open database for querying
120 String coll_db_file = GSFile.collectionDatabaseFile(this.site_home, this.cluster_name, this.index_stem, database_type);
121 if (!this.coll_db.openDatabase(coll_db_file, SimpleCollectionDatabase.READ))
122 {
123 logger.error("Could not open collection database!");
124 return false;
125 }
126
127 gs_doc_db = new BasicDocumentDatabase(database_type, this.site_home, this.cluster_name, this.index_stem);
128 if (!gs_doc_db.isValid())
129 {
130 logger.error("Failed to open Document Database.");
131 return false;
132 }
133 this.gs_doc = gs_doc_db;
134
135 // we need to set the database for our GS2 macro resolver
136 GS2MacroResolver gs2_macro_resolver = (GS2MacroResolver) this.macro_resolver;
137 gs2_macro_resolver.setDB(this.coll_db);
138 // set the class loader in case we have collection specific properties files
139 gs2_macro_resolver.setClassLoader(this.class_loader);
140 return true;
141 }
142
143 /** if id ends in .fc, .pc etc, then translate it to the correct id */
144 protected String translateId(String node_id)
145 {
146 return OID.translateOID(this.coll_db, node_id); //return this.coll_db.translateOID(node_id);
147 }
148
149 /**
150 * if an id is not a greenstone id (an external id) then translate it to a
151 * greenstone one
152 */
153 protected String translateExternalId(String node_id)
154 {
155 return this.coll_db.externalId2OID(node_id);
156 }
157
158 /**
159 * returns the id of the root node of the document containing node node_id.
160 * . may be the same as node_id
161 */
162 protected String getRootId(String node_id)
163 {
164 return this.gs_doc.getRootId(node_id);
165 }
166
167
168
169 /**
170 * get the metadata for the classifier node node_id returns a metadataList
171 * element: <metadataList><metadata
172 * name="xxx">value</metadata></metadataList>
173 */
174 protected Element getMetadataList(Document doc, String node_id, boolean all_metadata, ArrayList<String> metadata_names, String lang) throws GSException
175 {
176 Element metadata_list = doc.createElement(GSXML.METADATA_ELEM + GSXML.LIST_MODIFIER);
177 DBInfo info = this.coll_db.getInfo(node_id);
178 if (info == null)
179 {
180 return null;
181 }
182
183 if (all_metadata) // this will get all metadata for current node
184 {
185 // return everything out of the database
186 Set<String> keys = info.getKeys();
187 Iterator<String> it = keys.iterator();
188 while (it.hasNext())
189 {
190 String key = it.next();
191 //String value = info.getInfo(key);
192 Vector<String> values = info.getMultiInfo(key);
193 for (int i = 0; i < values.size(); i++)
194 {
195 GSXML.addMetadata(metadata_list, key, this.macro_resolver.resolve(values.elementAt(i), lang, MacroResolver.SCOPE_META, node_id));
196 }
197 }
198
199 }
200 // now we go through the list of names. If we have specified
201 // all_metadata, then here we only get the ones like
202 // parent_Title, that are not the current node.
203 for (int i = 0; i < metadata_names.size(); i++)
204 {
205 String meta_name = metadata_names.get(i);
206
207 if (!all_metadata || meta_name.indexOf(GSConstants.META_RELATION_SEP)!=-1) {
208 Vector <String> values = getMetadata(node_id, info, meta_name, lang);
209 if (values != null) {
210 for (int j = 0; j < values.size(); j++)
211 {
212 // some of these may be parent/ancestor. does resolve need a different id???
213 GSXML.addMetadata(metadata_list, meta_name, this.macro_resolver.resolve(values.elementAt(j), lang, MacroResolver.SCOPE_META, node_id));
214 }
215 }
216 }
217 }
218
219 return metadata_list;
220 }
221
222 protected Vector<String> getMetadata(String node_id, DBInfo info, String metadata, String lang) {
223
224 DBInfo current_info = info;
225
226 int index = metadata.indexOf(GSConstants.META_RELATION_SEP);
227 if (index == -1) {
228 // metadata is for this node
229 return getMetaValuesForOID(node_id, current_info, metadata);
230 }
231 // we need to get metadata for one or more different nodes
232 // we have a relation root, parent, ancestors, siblings, children, descendants, entire
233 String relation = metadata.substring(0, index);
234 String relation_id="";
235 metadata = metadata.substring(index + 1);
236 if (relation.equals("root")) {
237 relation_id = OID.getTop(node_id);
238 if (!relation_id.equals(node_id)) {
239 // get the dbinfo for the root node
240 current_info = this.coll_db.getInfo(relation_id);
241 }
242 return getMetaValuesForOID(relation_id, current_info, metadata);
243 }
244
245 if (relation.equals("parent")) {
246 relation_id = OID.getParent(node_id);
247 if (relation_id.equals(node_id)) {
248 // no parent
249 return null;
250 }
251 return getMetaValuesForOID(relation_id, null, metadata);
252 }
253
254 if (relation.equals("ancestors")) {
255 if (OID.isTop(node_id)) {
256 return null;
257 }
258 String current_id = node_id;
259 relation_id = OID.getParent(current_id);
260 Vector<String> values = new Vector<String>();
261 while (!relation_id.equals(current_id)) {
262
263 Vector<String> more_values = getMetaValuesForOID(relation_id, null, metadata);
264 if (more_values != null) {
265 values.addAll(0, more_values);
266 }
267 current_id = relation_id;
268 relation_id = OID.getParent(current_id);
269 }
270 return values;
271 }
272 if (relation.equals("siblings")) {
273 String parent_id = OID.getParent(node_id);
274 if (parent_id.equals(node_id)) {
275 // no parent, therefore no siblings
276 return null;
277 }
278 // siblings is the same as asking for children of the parent
279 node_id = parent_id;
280 relation = "children";
281 current_info = this.coll_db.getInfo(parent_id);
282 if (current_info == null) {
283 return null;
284 }
285 // drop through to children part
286 }
287
288 Vector<String> values = new Vector<String>();
289 if (relation.equals("children")) {
290 processChildren(node_id, current_info, metadata, values, false);
291 return values;
292 }
293
294 if (relation.equals("entire")) {
295 // this is the same as doing descendants on root
296 relation_id = OID.getTop(node_id);
297
298 // first of all, add root node info
299
300 if (relation_id.equals(node_id)) {
301 current_info = info;
302 } else {
303 current_info = this.coll_db.getInfo(relation_id);
304 }
305 values = getMetaValuesForOID(relation_id, current_info, metadata);
306
307 node_id = relation_id;
308 relation = "descendants";
309 // drop through to next part
310 }
311
312
313 if (relation.equals("descendants")) {
314 processChildren(node_id, current_info, metadata, values, true);
315 return values;
316 }
317 // unknown relation
318 // we assume that maybe the metadata has an _ in the name, so get the original name
319 return info.getMultiInfo(relation+GSConstants.META_RELATION_SEP+metadata);
320
321 }
322
323 protected void processChildren(String node_id, DBInfo info, String metadata, Vector<String> values,
324 boolean recursive) {
325 String contains = info.getInfo("contains");
326 contains = StringUtils.replace(contains, "\"", node_id);
327 String[] children = contains.split(";");
328 for (int i = 0; i < children.length; i++) {
329
330 String child_id = children[i];
331 Vector<String> more_values = getMetaValuesForOID(child_id, null, metadata);
332 if (more_values != null) {
333 values.addAll(more_values);
334 }
335 if (recursive) {
336 DBInfo child_info = this.coll_db.getInfo(child_id);
337 processChildren(child_id, child_info, metadata, values, recursive);
338 }
339 }
340 }
341
342 protected Vector<String> getMetaValuesForOID(String oid, DBInfo info, String metadata) {
343
344 DBInfo this_info;
345 if (info != null) {
346 this_info = info;
347 } else {
348 this_info = this.coll_db.getInfo(oid);
349 }
350 if (this_info == null) {
351 return null;
352 }
353
354 Vector<String> values = this_info.getMultiInfo(metadata);
355 // lets look through the values and look for [xxx] things. We need to look up metadata for them.
356 if (values == null) { return values; }
357
358 for (int j = 0; j < values.size(); j++) {
359 String val = values.elementAt(j);
360 if (val.contains("[")) {
361 // look for metadata refs
362 String [] metas = StringUtils.substringsBetween(val, "[", "]");
363 for (int i=0; i<metas.length; i++) {
364 String meta = metas[i];
365 String meta_val;
366 // hack for parent(Top). gs2 had lots of other modifiers, but is this the only one that was used in
367 // metadata values??
368 if (meta.startsWith("parent(Top):")) {
369 String nmeta = meta.substring(12); // or 13?
370 String root_id = OID.getTop(oid);
371 DBInfo root_info = this.coll_db.getInfo(root_id);
372 meta_val = root_info.getInfo(nmeta);
373 } else {
374 meta_val = this_info.getInfo(meta);
375 }
376 if (!meta_val.equals("")) {
377 val = StringUtils.replace(val,"["+meta+"]",meta_val);
378 }
379 }
380 values.set(j,val);
381 }
382 }
383 return values;
384 }
385 protected int getNumChildren(String node_id)
386 {
387 return this.gs_doc.getNumChildren(node_id);
388 }
389
390
391 /**
392 * returns the content of a node should return a nodeContent element:
393 * <nodeContent>text content or other elements</nodeContent>
394 */
395 abstract protected Element getNodeContent(Document doc, String doc_id, String lang) throws GSException;
396
397
398 /**
399 * needs to get info from collection database - if the calling code gets it
400 * already it may pay to pass it in instead
401 */
402 protected String resolveTextMacros(String doc_content, String doc_id, String lang)
403 {
404 // resolve any collection specific macros
405 doc_content = macro_resolver.resolve(doc_content, lang, MacroResolver.SCOPE_TEXT, doc_id);
406 return doc_content;
407 }
408
409
410
411}
Note: See TracBrowser for help on using the repository browser.