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