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