source: branches/ant-install-branch/gsdl3/src/java/org/greenstone/gsdl3/service/GS2Browse.java@ 9824

Last change on this file since 9824 was 9824, checked in by kjdon, 19 years ago

when a collection (using gdbm) is opened by tomcat, windows holds a lock on the gdbm file, so you can't rebuild it. modified ModuleInterface to have a cleanUp method, so all modules need to implement this. for mg/mgpp and gdbm modules, they now unload the index data or close the connection to the database. so cleanUp should be called whenever you deactivate a module

  • Property svn:keywords set to Author Date Id Revision
File size: 7.5 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.GDBMWrapper;
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
40/** Greenstone 2 collection classifier service
41 *
42 * @author <a href="mailto:[email protected]">Katherine Don</a>
43 * @author <a href="mailto:[email protected]">Michael Dewsnip</a>
44 */
45
46public class GS2Browse
47 extends AbstractBrowse
48{
49
50 protected GDBMWrapper gdbm_src = null;
51
52 public GS2Browse()
53 {
54 this.gdbm_src = new GDBMWrapper();
55 this.macro_resolver = new GS2MacroResolver(this.gdbm_src);
56 }
57
58 public void cleanUp() {
59 super.cleanUp();
60 this.gdbm_src.closeDatabase();
61 }
62
63 public boolean configure(Element info, Element extra_info)
64 {
65 System.err.println("Configuring GS2Browse...");
66 // Open GDBM database for querying
67 String gdbm_db_file = GSFile.GDBMDatabaseFile(this.site_home, this.cluster_name);
68 if (!this.gdbm_src.openDatabase(gdbm_db_file, GDBMWrapper.READER)) {
69 System.err.println("GS2Browse Error: Could not open GDBM database!");
70 return false;
71 }
72 return super.configure(info, extra_info);
73 }
74
75 /** if id ends in .fc, .pc etc, then translate it to the correct id */
76 protected String translateId(String node_id) {
77 return this.gdbm_src.translateOID(node_id);
78 }
79
80 /** returns the document type of the doc that the specified node
81 belongs to. should be one of
82 GSXML.DOC_TYPE_SIMPLE,
83 GSXML.DOC_TYPE_PAGED,
84 GSXML.DOC_TYPE_HIERARCHY
85 */
86 protected String getDocType(String node_id) {
87 DBInfo info = this.gdbm_src.getInfo(node_id);
88 if (info == null) {
89 return GSXML.DOC_TYPE_SIMPLE;
90 }
91 String doc_type = info.getInfo("doctype");
92 if (!doc_type.equals("")&&!doc_type.equals("doc")) {
93 return doc_type;
94 }
95
96 String top_id = OID.getTop(node_id);
97 boolean is_top = (top_id.equals(node_id) ? true : false);
98
99 String children = info.getInfo("contains");
100 boolean is_leaf = (children.equals("") ? true : false);
101
102 if (is_top && is_leaf) { // a single section document
103 return GSXML.DOC_TYPE_SIMPLE;
104 }
105
106 // now we just check the top node
107 if (!is_top) { // we need to look at the top info
108 info = this.gdbm_src.getInfo(top_id);
109 }
110 if (info == null) {
111 return GSXML.DOC_TYPE_HIERARCHY;
112 }
113
114 String childtype = info.getInfo("childtype");
115 if (childtype.equals("Paged")) {
116 return GSXML.DOC_TYPE_PAGED;
117 }
118 return GSXML.DOC_TYPE_HIERARCHY;
119
120 }
121
122 /** returns the id of the root node of the document containing node node_id. . may be the same as node_id */
123 protected String getRootId(String node_id) {
124 return OID.getTop(node_id);
125 }
126 /** returns a list of the child ids in order, null if no children */
127 protected ArrayList getChildrenIds(String node_id) {
128 DBInfo info = this.gdbm_src.getInfo(node_id);
129 if (info == null) {
130 return null;
131 }
132
133 ArrayList children = new ArrayList();
134
135 String contains = info.getInfo("contains");
136 StringTokenizer st = new StringTokenizer(contains, ";");
137 while (st.hasMoreTokens()) {
138 String child_id = st.nextToken().replaceAll("\"", node_id);
139 children.add(child_id);
140 }
141 return children;
142
143 }
144 /** returns the node id of the parent node, null if no parent */
145 protected String getParentId(String node_id){
146 String parent = OID.getParent(node_id);
147 if (parent.equals(node_id)) {
148 return null;
149 }
150 return parent;
151 }
152
153 /** get the metadata for the classifier node node_id
154 * returns a metadataList element:
155 * <metadataList><metadata name="xxx">value</metadata></metadataList>
156 * if all_metadata is true, returns all available metadata, otherwise just
157 * returns requested metadata
158 */
159 // assumes only one value per metadata
160 protected Element getMetadataList(String node_id, boolean all_metadata,
161 ArrayList metadata_names) {
162 String lang = "en";
163 Element metadata_list = this.doc.createElement(GSXML.METADATA_ELEM+GSXML.LIST_MODIFIER);
164 DBInfo info = this.gdbm_src.getInfo(node_id);
165 if (info == null) {
166 return null;
167 }
168 if (all_metadata) {
169 // return everything out of the database
170 Set keys = info.getKeys();
171 Iterator it = keys.iterator();
172 while(it.hasNext()) {
173 String key = (String)it.next();
174 String value = info.getInfo(key);
175 GSXML.addMetadata(this.doc, metadata_list, key, this.macro_resolver.resolve(value, lang, GS2MacroResolver.SCOPE_META, node_id));
176 }
177
178 } else {
179 for (int i=0; i<metadata_names.size(); i++) {
180 String meta_name = (String) metadata_names.get(i);
181 String value = (String)info.getInfo(meta_name);
182 GSXML.addMetadata(this.doc, metadata_list, meta_name, value);
183 }
184 }
185 return metadata_list;
186 }
187
188 /** returns the structural information asked for.
189 * info_type may be one of
190 * INFO_NUM_SIBS, INFO_NUM_CHILDREN, INFO_SIB_POS
191 */
192 protected String getStructureInfo(String doc_id, String info_type) {
193 String value="";
194 if (info_type.equals(INFO_NUM_SIBS)) {
195 String parent_id = OID.getParent(doc_id);
196 if (parent_id.equals(doc_id)) {
197 value="0";
198 } else {
199 value = String.valueOf(getNumChildren(parent_id));
200 }
201 return value;
202 }
203
204 if (info_type.equals(INFO_NUM_CHILDREN)) {
205 return String.valueOf(getNumChildren(doc_id));
206 }
207
208
209 if (info_type.equals(INFO_SIB_POS)) {
210 String parent_id = OID.getParent(doc_id);
211 if (parent_id.equals(doc_id)) {
212 return "-1";
213 }
214
215 DBInfo info = this.gdbm_src.getInfo(parent_id);
216 if (info==null) {
217 return "-1";
218 }
219
220 String contains = info.getInfo("contains");
221 contains = contains.replaceAll("\"", parent_id);
222 String [] children = contains.split(";");
223 for (int i=0;i<children.length;i++) {
224 String child_id = children[i];
225 if (child_id.equals(doc_id)) {
226 return String.valueOf(i+1); // make it from 1 to length
227
228 }
229 }
230
231 return "-1";
232 } else {
233 return null;
234 }
235
236 }
237
238 protected int getNumChildren(String node_id) {
239 DBInfo info = this.gdbm_src.getInfo(node_id);
240 if (info == null) {
241 return 0;
242 }
243 String contains = info.getInfo("contains");
244 if (contains.equals("")) {
245 return 0;
246 }
247 String [] children = contains.split(";");
248 return children.length;
249 }
250
251 /** returns true if the id refers to a document (rather than
252 * a classifier node)
253 */
254 protected boolean isDocumentId(String node_id){
255 if (node_id.startsWith("CL")) {
256 return false;
257 }
258 return true;
259 }
260
261}
Note: See TracBrowser for help on using the repository browser.