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

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

renaming these classes to get rid of Simple from the names - Simple has specific meaning for document type

File size: 4.6 KB
Line 
1/*
2 * SimpleDocumentDatabase.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.Iterator;
39import java.io.File;
40
41import org.apache.log4j.*;
42
43public class SimpleDocumentDatabase extends AbstractSimpleDocument
44{
45 // collection database
46 protected SimpleCollectionDatabase coll_db = null;
47
48 static Logger logger = Logger.getLogger(org.greenstone.gsdl3.util.SimpleDocumentDatabase.class.getName());
49
50
51 /** constructor */
52 public SimpleDocumentDatabase(Document doc,
53 String database_type, String site_home,
54 String cluster_name, String index_stem)
55 {
56 super(doc);
57
58 coll_db = new SimpleCollectionDatabase(database_type);
59 if (!coll_db.databaseOK()) {
60 logger.error("Couldn't create the collection database of type "+database_type);
61 }
62
63 // Open database for querying
64 String coll_db_file = GSFile.collectionDatabaseFile(site_home, cluster_name, index_stem, database_type);
65 if (!this.coll_db.openDatabase(coll_db_file, SimpleCollectionDatabase.READ)) {
66 logger.error("Could not open collection database!");
67 coll_db = null;
68 }
69 }
70
71 public boolean isValid()
72 {
73 return (coll_db != null);
74 }
75
76 public void cleanUp() {
77 this.coll_db.closeDatabase();
78 }
79
80
81 public DBInfo getInfo(String main_key) {
82 if (this.coll_db==null) {
83 // Most likely cause is that this installation of Greenstone3 has not
84 // been compiled with support for this database type
85 return null;
86 }
87
88 return coll_db.getInfo(main_key);
89 }
90
91
92 /** returns the document type of the doc that the specified node
93 belongs to. should be one of
94 GSXML.DOC_TYPE_SIMPLE,
95 GSXML.DOC_TYPE_PAGED,
96 GSXML.DOC_TYPE_HIERARCHY
97 */
98 public String getDocType(String node_id){
99 DBInfo info = this.coll_db.getInfo(node_id);
100 if (info == null) {
101 return GSXML.DOC_TYPE_SIMPLE;
102 }
103 String doc_type = info.getInfo("doctype");
104 if (!doc_type.equals("")&&!doc_type.equals("doc")) {
105 return doc_type;
106 }
107
108 String top_id = OID.getTop(node_id);
109 boolean is_top = (top_id.equals(node_id) ? true : false);
110
111 String children = info.getInfo("contains");
112 boolean is_leaf = (children.equals("") ? true : false);
113
114 if (is_top && is_leaf) { // a single section document
115 return GSXML.DOC_TYPE_SIMPLE;
116 }
117
118 // now we just check the top node
119 if (!is_top) { // we need to look at the top info
120 info = this.coll_db.getInfo(top_id);
121 }
122 if (info == null) {
123 return GSXML.DOC_TYPE_HIERARCHY;
124 }
125
126 String childtype = info.getInfo("childtype");
127 if (childtype.equals("Paged")) {
128 return GSXML.DOC_TYPE_PAGED;
129 }
130 if (childtype.equals("PagedHierarchy"))
131 {
132 return GSXML.DOC_TYPE_PAGED_HIERARCHY;
133 }
134 return GSXML.DOC_TYPE_HIERARCHY;
135
136 }
137
138 /** returns true if the node has child nodes */
139 public boolean hasChildren(String node_id){
140 DBInfo info = this.coll_db.getInfo(node_id);
141 if (info == null) {
142 return false;
143 }
144 String contains = info.getInfo("contains");
145 if (contains.equals("")) {
146 return false;
147 }
148 return true;
149 }
150
151 /** returns true if the node has a parent */
152 public boolean hasParent(String node_id){
153 String parent = OID.getParent(node_id);
154 if (parent.equals(node_id)) {
155 return false;
156 }
157 return true;
158 }
159
160 /** convert indexer internal id to Greenstone oid */
161 public String internalNum2OID(long docnum)
162 {
163 return this.coll_db.docnum2OID(docnum);
164 }
165
166 public String internalNum2OID(String docnum)
167 {
168 return this.coll_db.docnum2OID(docnum);
169 }
170
171}
172
173
Note: See TracBrowser for help on using the repository browser.