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

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

added new doctype pagedhierarchy

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