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

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

getting rid of my email address

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