source: main/trunk/greenstone3/src/java/org/greenstone/gsdl3/util/BasicDocumentDatabase.java@ 36976

Last change on this file since 36976 was 36976, checked in by kjdon, 17 months ago

added a method to use to check whether a doc id is valid or not

File size: 9.5 KB
Line 
1/*
2 * BasicDocumentDatabase.java
3 * Copyright (C) 2002 New Zealand Digital Library, http://www.nzdl.org
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 */
18package org.greenstone.gsdl3.util;
19
20// Greenstone classes
21import org.greenstone.gsdl3.util.OID;
22import org.greenstone.gsdl3.util.DBInfo;
23import org.greenstone.gsdl3.util.GSXML;
24import org.greenstone.gsdl3.util.SimpleCollectionDatabase;
25import org.greenstone.gsdl3.util.GSFile;
26
27// XML classes
28import org.w3c.dom.Document;
29import org.w3c.dom.Element;
30import org.w3c.dom.NodeList;
31
32// java
33import java.util.Vector;
34import java.util.ArrayList;
35import java.util.HashMap;
36import java.util.Map;
37import java.util.Set;
38import java.util.StringTokenizer;
39import java.util.Iterator;
40import java.io.File;
41
42import org.apache.commons.lang3.StringUtils;
43import org.apache.log4j.*;
44
45public class BasicDocumentDatabase extends AbstractBasicDocument
46{
47 // collection database
48 protected SimpleCollectionDatabase coll_db = null;
49
50 static Logger logger = Logger.getLogger(org.greenstone.gsdl3.util.BasicDocumentDatabase.class.getName());
51
52
53 /** constructor */
54 public BasicDocumentDatabase(String database_type, String site_home,
55 String cluster_name, String index_stem)
56 {
57 super();
58
59 coll_db = new SimpleCollectionDatabase(database_type);
60 if (!coll_db.databaseOK()) {
61 logger.error("Couldn't create the collection database of type "+database_type);
62 }
63
64 // Open database for querying
65 String coll_db_file = GSFile.collectionDatabaseFile(site_home, cluster_name, index_stem, database_type);
66 if (!this.coll_db.openDatabase(coll_db_file, SimpleCollectionDatabase.READ)) {
67 logger.error("Could not open collection database!");
68 coll_db = null;
69 }
70 }
71
72 public boolean isValid()
73 {
74 return (coll_db != null);
75 }
76
77 public boolean isValidOID(String doc_id) {
78 DBInfo info = this.coll_db.getInfo(doc_id);
79 if (info == null) {
80 return false;
81 }
82 return true;
83 }
84 public void cleanUp() {
85 this.coll_db.closeDatabase();
86 }
87
88
89 public DBInfo getInfo(String main_key) {
90 if (this.coll_db==null) {
91 // Most likely cause is that this installation of Greenstone3 has not
92 // been compiled with support for this database type
93 return null;
94 }
95
96 return coll_db.getInfo(main_key);
97 }
98
99
100 /** returns the document type of the doc that the specified node
101 belongs to. should be one of
102 GSXML.DOC_TYPE_SIMPLE,
103 GSXML.DOC_TYPE_PAGED,
104 GSXML.DOC_TYPE_HIERARCHY
105 */
106 public String getDocType(String node_id){
107 DBInfo info = this.coll_db.getInfo(node_id);
108 if (info == null) {
109 return GSXML.DOC_TYPE_SIMPLE;
110 }
111 String doc_type = info.getInfo("doctype");
112 if (!doc_type.equals("")&&!doc_type.equals("doc")) {
113 return doc_type;
114 }
115
116 String top_id = OID.getTop(node_id);
117 boolean is_top = (top_id.equals(node_id) ? true : false);
118
119 String children = info.getInfo("contains");
120 boolean is_leaf = (children.equals("") ? true : false);
121
122 if (is_top && is_leaf) { // a single section document
123 return GSXML.DOC_TYPE_SIMPLE;
124 }
125
126 // now we just check the top node
127 if (!is_top) { // we need to look at the top info
128 info = this.coll_db.getInfo(top_id);
129 }
130 if (info == null) {
131 return GSXML.DOC_TYPE_HIERARCHY;
132 }
133
134 String childtype = info.getInfo("childtype");
135 if (childtype.equals("Paged")) {
136 return GSXML.DOC_TYPE_PAGED;
137 }
138 if (childtype.equals("PagedHierarchy")) {
139
140 return GSXML.DOC_TYPE_PAGED_HIERARCHY;
141 }
142 return GSXML.DOC_TYPE_HIERARCHY;
143
144 }
145
146 /**
147 * returns the structural information asked for. info_type may be one of
148 * INFO_NUM_SIBS, INFO_NUM_CHILDREN, INFO_SIB_POS, INFO_DOC_TYPE
149 */
150 public String getStructureInfo(String doc_id, String info_type){
151 String value = "";
152 if (info_type.equals(INFO_NUM_SIBS))
153 {
154 String parent_id = OID.getParent(doc_id);
155 if (parent_id.equals(doc_id))
156 {
157 value = "0";
158 }
159 else
160 {
161 value = String.valueOf(getNumChildren(parent_id));
162 }
163 return value;
164 }
165
166 if (info_type.equals(INFO_NUM_CHILDREN))
167 {
168 return String.valueOf(getNumChildren(doc_id));
169 }
170
171 if (info_type.equals(INFO_SIB_POS))
172 {
173 String parent_id = OID.getParent(doc_id);
174 if (parent_id.equals(doc_id))
175 {
176 return "-1";
177 }
178
179 DBInfo info = this.coll_db.getInfo(parent_id);
180 if (info == null)
181 {
182 return "-1";
183 }
184
185 String contains = info.getInfo("contains");
186 contains = StringUtils.replace(contains, "\"", parent_id);
187 String[] children = contains.split(";");
188 for (int i = 0; i < children.length; i++)
189 {
190 String child_id = children[i];
191 if (child_id.equals(doc_id))
192 {
193 return String.valueOf(i + 1); // make it from 1 to length
194
195 }
196 }
197
198 return "-1";
199 }
200 if (info_type.equals(INFO_DOC_TYPE))
201
202 {
203 return getDocType(doc_id);
204 }
205 return null;
206
207 }
208
209 /** returns true if the node has child nodes */
210 public boolean hasChildren(String node_id){
211 DBInfo info = this.coll_db.getInfo(node_id);
212 if (info == null) {
213 return false;
214 }
215 String contains = info.getInfo("contains");
216 if (contains.equals("")) {
217 return false;
218 }
219 return true;
220 }
221
222 public int getNumChildren(String node_id) {
223 DBInfo info = this.coll_db.getInfo(node_id);
224 if (info == null)
225 {
226 return 0;
227 }
228 String contains = info.getInfo("contains");
229 if (contains.equals(""))
230 {
231 return 0;
232 }
233 String[] children = contains.split(";");
234 return children.length;
235
236
237 }
238 /** returns a list of the child ids in order, null if no children
239 */
240 public ArrayList<String> getChildrenIds(String node_id) {
241 DBInfo info = this.coll_db.getInfo(node_id);
242 if (info == null)
243 {
244 return null;
245 }
246
247 String contains = info.getInfo("contains");
248 if (contains.equals(""))
249 {
250 return null;
251 }
252 ArrayList<String> children = new ArrayList<String>();
253 StringTokenizer st = new StringTokenizer(contains, ";");
254 while (st.hasMoreTokens())
255 {
256 String child_id = StringUtils.replace(st.nextToken(), "\"", node_id);
257 children.add(child_id);
258 }
259 return children;
260
261
262 }
263
264 public String getMetadata(String node_id, String meta_name) {
265 DBInfo info = this.coll_db.getInfo(node_id);
266 if (info == null)
267 {
268 return null;
269 }
270 return info.getInfo(meta_name);
271 }
272
273 /** returns true if the node has a parent */
274 public boolean hasParent(String node_id){
275 String parent = OID.getParent(node_id);
276 if (parent.equals(node_id)) {
277 return false;
278 }
279 return true;
280 }
281 /**
282 * returns the node id of the parent node, null if no parent
283 */
284 public String getParentId(String node_id) {
285 String parent = OID.getParent(node_id);
286 if (parent.equals(node_id))
287 {
288 return null;
289 }
290 return parent;
291
292 }
293
294 /**
295 * returns the node id of the root node of the document containing node_id
296 */
297 public String getRootId(String node_id) {
298 return OID.getTop(node_id);
299 }
300
301 /** convert indexer internal id to Greenstone oid */
302 public String internalNum2OID(long docnum)
303 {
304 return this.coll_db.docnum2OID(docnum);
305 }
306
307 public String internalNum2OID(String docnum)
308 {
309 return this.coll_db.docnum2OID(docnum);
310 }
311
312
313 /**
314 * adds all the children of doc_id to the doc element, and if
315 * recursive=true, adds all their children as well
316 */
317 public void addDescendants(Element doc, String doc_id, boolean recursive)
318 {
319 ArrayList<String> child_ids = getChildrenIds(doc_id);
320 if (child_ids == null)
321 return;
322 for (int i = 0; i < child_ids.size(); i++)
323 {
324 String child_id = child_ids.get(i);
325 Element child_elem = createDocNode(doc.getOwnerDocument(), child_id);
326 doc.appendChild(child_elem);
327 if (recursive && (!child_elem.getAttribute(GSXML.NODE_TYPE_ATT).equals(GSXML.NODE_TYPE_LEAF) || child_elem.getAttribute(GSXML.DOC_TYPE_ATT).equals(GSXML.DOC_TYPE_PAGED)))
328 {
329 addDescendants(child_elem, child_id, recursive);
330 }
331 }
332 }
333
334 /**
335 * adds all the siblings of current_id to the parent element. returns the
336 * new current element
337 */
338 public Element addSiblings(Element parent_node, String parent_id, String current_id)
339 {
340 Element current_node = GSXML.getFirstElementChild(parent_node);//(Element)parent_node.getFirstChild();
341 if (current_node == null)
342 {
343 // create a sensible error message
344 logger.error(" there should be a first child.");
345 return null;
346 }
347 // remove the current child,- will add it in later in its correct place
348 parent_node.removeChild(current_node);
349
350 // add in all the siblings,
351 addDescendants(parent_node, parent_id, false);
352
353 // find the node that is now the current node
354 // this assumes that the new node that was created is the same as
355 // the old one that was removed - we may want to replace the new one
356 // with the old one.
357 Element new_current = GSXML.getNamedElement(parent_node, current_node.getNodeName(), GSXML.NODE_ID_ATT, current_id);
358 return new_current;
359 }
360
361 /** returns the list of sibling ids, including the specified node_id */
362 public ArrayList<String> getSiblingIds(String node_id){
363 String parent_id = getParentId(node_id);
364 if (parent_id == null)
365 {
366 return null;
367 }
368 return getChildrenIds(parent_id);
369
370 }
371
372}
373
374
Note: See TracBrowser for help on using the repository browser.