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

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

Reformatting this file

  • 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 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 /**
123 * returns the document type of the doc that the specified node belongs to.
124 * should be one of GSXML.DOC_TYPE_SIMPLE, GSXML.DOC_TYPE_PAGED,
125 * GSXML.DOC_TYPE_HIERARCHY
126 */
127 protected String getDocType(String node_id)
128 {
129 DBInfo info = this.coll_db.getInfo(node_id);
130 if (info == null)
131 {
132 return GSXML.DOC_TYPE_SIMPLE;
133 }
134 String doc_type = info.getInfo("doctype");
135 if (!doc_type.equals("") && !doc_type.equals("doc"))
136 {
137 return doc_type;
138 }
139
140 String top_id = OID.getTop(node_id);
141 boolean is_top = (top_id.equals(node_id) ? true : false);
142
143 String children = info.getInfo("contains");
144 boolean is_leaf = (children.equals("") ? true : false);
145
146 if (is_top && is_leaf)
147 { // a single section document
148 return GSXML.DOC_TYPE_SIMPLE;
149 }
150
151 // now we just check the top node
152 if (!is_top)
153 { // we need to look at the top info
154 info = this.coll_db.getInfo(top_id);
155 }
156 if (info == null)
157 {
158 return GSXML.DOC_TYPE_HIERARCHY;
159 }
160
161 String childtype = info.getInfo("childtype");
162 if (childtype.equals("Paged"))
163 {
164 return GSXML.DOC_TYPE_PAGED;
165 }
166 return GSXML.DOC_TYPE_HIERARCHY;
167
168 }
169
170 /**
171 * returns the id of the root node of the document containing node node_id.
172 * . may be the same as node_id
173 */
174 protected String getRootId(String node_id)
175 {
176 return OID.getTop(node_id);
177 }
178
179 /** returns a list of the child ids in order, null if no children */
180 protected ArrayList<String> getChildrenIds(String node_id)
181 {
182 DBInfo info = this.coll_db.getInfo(node_id);
183 if (info == null)
184 {
185 return null;
186 }
187
188 ArrayList<String> children = new ArrayList<String>();
189
190 String contains = info.getInfo("contains");
191 StringTokenizer st = new StringTokenizer(contains, ";");
192 while (st.hasMoreTokens())
193 {
194 String child_id = st.nextToken().replaceAll("\"", node_id);
195 children.add(child_id);
196 }
197 return children;
198
199 }
200
201 /** returns the node id of the parent node, null if no parent */
202 protected String getParentId(String node_id)
203 {
204 String parent = OID.getParent(node_id);
205 if (parent.equals(node_id))
206 {
207 return null;
208 }
209 return parent;
210 }
211
212 protected String getMetadata(String node_id, String key)
213 {
214 DBInfo info = this.coll_db.getInfo(node_id);
215 if (info == null)
216 {
217 return "";
218 }
219
220 Set<String> keys = info.getKeys();
221 Iterator<String> it = keys.iterator();
222 while (it.hasNext())
223 {
224 String key_in = it.next();
225 String value = info.getInfo(key);
226 if (key_in.equals(key))
227 {
228 return value;
229 }
230 }
231
232 return "";
233
234 }
235
236 /**
237 * get the metadata for the classifier node node_id returns a metadataList
238 * element: <metadataList><metadata
239 * name="xxx">value</metadata></metadataList> if all_metadata is true,
240 * returns all available metadata, otherwise just returns requested metadata
241 */
242 // assumes only one value per metadata
243 protected Element getMetadataList(String node_id, boolean all_metadata, ArrayList<String> metadata_names)
244 {
245 String lang = "en";
246 Element metadata_list = this.doc.createElement(GSXML.METADATA_ELEM + GSXML.LIST_MODIFIER);
247 DBInfo info = this.coll_db.getInfo(node_id);
248 if (info == null)
249 {
250 return null;
251 }
252 if (all_metadata)
253 {
254 // return everything out of the database
255 Set<String> keys = info.getKeys();
256 Iterator<String> it = keys.iterator();
257 while (it.hasNext())
258 {
259 String key = it.next();
260 String value = info.getInfo(key);
261 GSXML.addMetadata(this.doc, metadata_list, key, this.macro_resolver.resolve(value, lang, GS2MacroResolver.SCOPE_META, node_id));
262 }
263
264 }
265 else
266 {
267 for (int i = 0; i < metadata_names.size(); i++)
268 {
269 String meta_name = metadata_names.get(i);
270 String value = (String) info.getInfo(meta_name);
271 GSXML.addMetadata(this.doc, metadata_list, meta_name, value);
272 }
273 }
274 return metadata_list;
275 }
276
277 /**
278 * returns the structural information asked for. info_type may be one of
279 * INFO_NUM_SIBS, INFO_NUM_CHILDREN, INFO_SIB_POS
280 */
281 protected String getStructureInfo(String doc_id, String info_type)
282 {
283 String value = "";
284 if (info_type.equals(INFO_NUM_SIBS))
285 {
286 String parent_id = OID.getParent(doc_id);
287 if (parent_id.equals(doc_id))
288 {
289 value = "0";
290 }
291 else
292 {
293 value = String.valueOf(getNumChildren(parent_id));
294 }
295 return value;
296 }
297
298 if (info_type.equals(INFO_NUM_CHILDREN))
299 {
300 return String.valueOf(getNumChildren(doc_id));
301 }
302
303 if (info_type.equals(INFO_SIB_POS))
304 {
305 String parent_id = OID.getParent(doc_id);
306 if (parent_id.equals(doc_id))
307 {
308 return "-1";
309 }
310
311 DBInfo info = this.coll_db.getInfo(parent_id);
312 if (info == null)
313 {
314 return "-1";
315 }
316
317 String contains = info.getInfo("contains");
318 contains = contains.replaceAll("\"", parent_id);
319 String[] children = contains.split(";");
320 for (int i = 0; i < children.length; i++)
321 {
322 String child_id = children[i];
323 if (child_id.equals(doc_id))
324 {
325 return String.valueOf(i + 1); // make it from 1 to length
326
327 }
328 }
329
330 return "-1";
331 }
332 else
333 {
334 return null;
335 }
336
337 }
338
339 protected int getNumChildren(String node_id)
340 {
341 DBInfo info = this.coll_db.getInfo(node_id);
342 if (info == null)
343 {
344 return 0;
345 }
346 String contains = info.getInfo("contains");
347 if (contains.equals(""))
348 {
349 return 0;
350 }
351 String[] children = contains.split(";");
352 return children.length;
353 }
354
355 /**
356 * returns true if the id refers to a document (rather than a classifier
357 * node)
358 */
359 protected boolean isDocumentId(String node_id)
360 {
361 if (node_id.startsWith("CL"))
362 {
363 return false;
364 }
365 return true;
366 }
367
368}
Note: See TracBrowser for help on using the repository browser.