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

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

Fixing Greenstone 3's use (or lack thereof) of generics, this was done automatically so we may want to change it over time. This change will also auto-format any files that have not already been formatted.

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