source: main/trunk/greenstone3/src/java/org/greenstone/gsdl3/service/GS2Browse.java@ 25691

Last change on this file since 25691 was 25691, checked in by sjm84, 12 years ago

Added a call that gets the classifier style from the database

  • Property svn:keywords set to Author Date Id Revision
File size: 9.3 KB
Line 
1/*
2 * GS2Browse.java
3 * Copyright (C) 2005 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.util.OID;
23import org.greenstone.gsdl3.util.GSXML;
24import org.greenstone.gsdl3.util.GSFile;
25import org.greenstone.gsdl3.util.MacroResolver;
26import org.greenstone.gsdl3.util.GS2MacroResolver;
27import org.greenstone.gsdl3.util.SimpleCollectionDatabase;
28import org.greenstone.gsdl3.util.DBInfo;
29// XML classes
30import org.w3c.dom.Document;
31import org.w3c.dom.Element;
32import org.w3c.dom.NodeList;
33
34// General Java classes
35import java.util.ArrayList;
36import java.util.StringTokenizer;
37import java.util.Set;
38import java.util.Iterator;
39
40import org.apache.log4j.*;
41
42/**
43 * Greenstone 2 collection classifier service
44 *
45 * @author <a href="mailto:[email protected]">Katherine Don</a>
46 * @author <a href="mailto:[email protected]">Michael Dewsnip</a>
47 */
48
49public class GS2Browse extends AbstractBrowse
50{
51
52 static Logger logger = Logger.getLogger(org.greenstone.gsdl3.service.GS2Browse.class.getName());
53
54 protected SimpleCollectionDatabase coll_db = null;
55
56 public GS2Browse()
57 {
58 }
59
60 public void cleanUp()
61 {
62 super.cleanUp();
63 this.coll_db.closeDatabase();
64 }
65
66 public boolean configure(Element info, Element extra_info)
67 {
68 if (!super.configure(info, extra_info))
69 {
70 return false;
71 }
72
73 logger.info("Configuring GS2Browse...");
74 // the index stem is either specified in the config file or is the collection name
75 Element index_stem_elem = (Element) GSXML.getChildByTagName(info, GSXML.INDEX_STEM_ELEM);
76 String index_stem = null;
77 if (index_stem_elem != null)
78 {
79 index_stem = index_stem_elem.getAttribute(GSXML.NAME_ATT);
80 }
81 if (index_stem == null || index_stem.equals(""))
82 {
83 index_stem = this.cluster_name;
84 }
85
86 // find out what kind of database we have
87 Element database_type_elem = (Element) GSXML.getChildByTagName(info, GSXML.DATABASE_TYPE_ELEM);
88 String database_type = null;
89 if (database_type_elem != null)
90 {
91 database_type = database_type_elem.getAttribute(GSXML.NAME_ATT);
92 }
93
94 if (database_type == null || database_type.equals(""))
95 {
96 database_type = "gdbm"; // the default
97 }
98 coll_db = new SimpleCollectionDatabase(database_type);
99 if (!coll_db.databaseOK())
100 {
101 logger.error("Couldn't create the collection database of type " + database_type);
102 return false;
103 }
104
105 // Open database for querying
106 String coll_db_file = GSFile.collectionDatabaseFile(this.site_home, this.cluster_name, index_stem, database_type);
107 if (!this.coll_db.openDatabase(coll_db_file, SimpleCollectionDatabase.READ))
108 {
109 logger.error("Could not open collection database!");
110 return false;
111 }
112 this.macro_resolver = new GS2MacroResolver(this.coll_db);
113 return true;
114 }
115
116 /** if id ends in .fc, .pc etc, then translate it to the correct id */
117 protected String translateId(String node_id)
118 {
119 return OID.translateOID(this.coll_db, node_id); //return this.coll_db.translateOID(node_id);
120 }
121
122 protected String getChildType(String node_id)
123 {
124 DBInfo info = this.coll_db.getInfo(node_id);
125 if (info == null)
126 {
127 return null;
128 }
129 return info.getInfo("childtype");
130 }
131
132 /**
133 * returns the document type of the doc that the specified node belongs to.
134 * should be one of GSXML.DOC_TYPE_SIMPLE, GSXML.DOC_TYPE_PAGED,
135 * GSXML.DOC_TYPE_HIERARCHY
136 */
137 protected String getDocType(String node_id)
138 {
139 DBInfo info = this.coll_db.getInfo(node_id);
140 if (info == null)
141 {
142 return GSXML.DOC_TYPE_SIMPLE;
143 }
144 String doc_type = info.getInfo("doctype");
145 if (!doc_type.equals("") && !doc_type.equals("doc"))
146 {
147 return doc_type;
148 }
149
150 String top_id = OID.getTop(node_id);
151 boolean is_top = (top_id.equals(node_id) ? true : false);
152
153 String children = info.getInfo("contains");
154 boolean is_leaf = (children.equals("") ? true : false);
155
156 if (is_top && is_leaf)
157 { // a single section document
158 return GSXML.DOC_TYPE_SIMPLE;
159 }
160
161 // now we just check the top node
162 if (!is_top)
163 { // we need to look at the top info
164 info = this.coll_db.getInfo(top_id);
165 }
166 if (info == null)
167 {
168 return GSXML.DOC_TYPE_HIERARCHY;
169 }
170
171 String childtype = info.getInfo("childtype");
172 if (childtype.equals("Paged"))
173 {
174 return GSXML.DOC_TYPE_PAGED;
175 }
176 return GSXML.DOC_TYPE_HIERARCHY;
177
178 }
179
180 /**
181 * returns the id of the root node of the document containing node node_id.
182 * . may be the same as node_id
183 */
184 protected String getRootId(String node_id)
185 {
186 return OID.getTop(node_id);
187 }
188
189 /** returns a list of the child ids in order, null if no children */
190 protected ArrayList<String> getChildrenIds(String node_id)
191 {
192 DBInfo info = this.coll_db.getInfo(node_id);
193 if (info == null)
194 {
195 return null;
196 }
197
198 ArrayList<String> children = new ArrayList<String>();
199
200 String contains = info.getInfo("contains");
201 StringTokenizer st = new StringTokenizer(contains, ";");
202 while (st.hasMoreTokens())
203 {
204 String child_id = st.nextToken().replaceAll("\"", node_id);
205 children.add(child_id);
206 }
207 return children;
208
209 }
210
211 /** returns the node id of the parent node, null if no parent */
212 protected String getParentId(String node_id)
213 {
214 String parent = OID.getParent(node_id);
215 if (parent.equals(node_id))
216 {
217 return null;
218 }
219 return parent;
220 }
221
222 protected String getMetadata(String node_id, String key)
223 {
224 DBInfo info = this.coll_db.getInfo(node_id);
225 if (info == null)
226 {
227 return "";
228 }
229
230 Set<String> keys = info.getKeys();
231 Iterator<String> it = keys.iterator();
232 while (it.hasNext())
233 {
234 String key_in = it.next();
235 String value = info.getInfo(key);
236 if (key_in.equals(key))
237 {
238 return value;
239 }
240 }
241
242 return "";
243
244 }
245
246 /**
247 * get the metadata for the classifier node node_id returns a metadataList
248 * element: <metadataList><metadata
249 * name="xxx">value</metadata></metadataList> if all_metadata is true,
250 * returns all available metadata, otherwise just returns requested metadata
251 */
252 // assumes only one value per metadata
253 protected Element getMetadataList(String node_id, boolean all_metadata, ArrayList<String> metadata_names)
254 {
255 String lang = "en";
256 Element metadata_list = this.doc.createElement(GSXML.METADATA_ELEM + GSXML.LIST_MODIFIER);
257 DBInfo info = this.coll_db.getInfo(node_id);
258 if (info == null)
259 {
260 return null;
261 }
262 if (all_metadata)
263 {
264 // return everything out of the database
265 Set<String> keys = info.getKeys();
266 Iterator<String> it = keys.iterator();
267 while (it.hasNext())
268 {
269 String key = it.next();
270 String value = info.getInfo(key);
271 GSXML.addMetadata(this.doc, metadata_list, key, this.macro_resolver.resolve(value, lang, GS2MacroResolver.SCOPE_META, node_id));
272 }
273
274 }
275 else
276 {
277 for (int i = 0; i < metadata_names.size(); i++)
278 {
279 String meta_name = metadata_names.get(i);
280 String value = (String) info.getInfo(meta_name);
281 GSXML.addMetadata(this.doc, metadata_list, meta_name, value);
282 }
283 }
284 return metadata_list;
285 }
286
287 /**
288 * returns the structural information asked for. info_type may be one of
289 * INFO_NUM_SIBS, INFO_NUM_CHILDREN, INFO_SIB_POS
290 */
291 protected String getStructureInfo(String doc_id, String info_type)
292 {
293 String value = "";
294 if (info_type.equals(INFO_NUM_SIBS))
295 {
296 String parent_id = OID.getParent(doc_id);
297 if (parent_id.equals(doc_id))
298 {
299 value = "0";
300 }
301 else
302 {
303 value = String.valueOf(getNumChildren(parent_id));
304 }
305 return value;
306 }
307
308 if (info_type.equals(INFO_NUM_CHILDREN))
309 {
310 return String.valueOf(getNumChildren(doc_id));
311 }
312
313 if (info_type.equals(INFO_SIB_POS))
314 {
315 String parent_id = OID.getParent(doc_id);
316 if (parent_id.equals(doc_id))
317 {
318 return "-1";
319 }
320
321 DBInfo info = this.coll_db.getInfo(parent_id);
322 if (info == null)
323 {
324 return "-1";
325 }
326
327 String contains = info.getInfo("contains");
328 contains = contains.replaceAll("\"", parent_id);
329 String[] children = contains.split(";");
330 for (int i = 0; i < children.length; i++)
331 {
332 String child_id = children[i];
333 if (child_id.equals(doc_id))
334 {
335 return String.valueOf(i + 1); // make it from 1 to length
336
337 }
338 }
339
340 return "-1";
341 }
342 else
343 {
344 return null;
345 }
346
347 }
348
349 protected int getNumChildren(String node_id)
350 {
351 DBInfo info = this.coll_db.getInfo(node_id);
352 if (info == null)
353 {
354 return 0;
355 }
356 String contains = info.getInfo("contains");
357 if (contains.equals(""))
358 {
359 return 0;
360 }
361 String[] children = contains.split(";");
362 return children.length;
363 }
364
365 /**
366 * returns true if the id refers to a document (rather than a classifier
367 * node)
368 */
369 protected boolean isDocumentId(String node_id)
370 {
371 if (node_id.startsWith("CL"))
372 {
373 return false;
374 }
375 return true;
376 }
377
378}
Note: See TracBrowser for help on using the repository browser.