[8959] | 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 | */
|
---|
| 19 | package org.greenstone.gsdl3.service;
|
---|
| 20 |
|
---|
| 21 | // Greenstone classes
|
---|
[25717] | 22 | import java.util.ArrayList;
|
---|
| 23 | import java.util.Iterator;
|
---|
| 24 | import java.util.Set;
|
---|
| 25 | import java.util.StringTokenizer;
|
---|
| 26 |
|
---|
| 27 | import org.apache.log4j.Logger;
|
---|
[26046] | 28 | import org.greenstone.gsdl3.util.BasicDocumentDatabase;
|
---|
[25717] | 29 | import org.greenstone.gsdl3.util.DBInfo;
|
---|
| 30 | import org.greenstone.gsdl3.util.GS2MacroResolver;
|
---|
| 31 | import org.greenstone.gsdl3.util.GSFile;
|
---|
| 32 | import org.greenstone.gsdl3.util.GSXML;
|
---|
[8959] | 33 | import org.greenstone.gsdl3.util.OID;
|
---|
[15326] | 34 | import org.greenstone.gsdl3.util.SimpleCollectionDatabase;
|
---|
[25690] | 35 | import org.w3c.dom.Element;
|
---|
[8959] | 36 |
|
---|
[25690] | 37 | /**
|
---|
| 38 | * Greenstone 2 collection classifier service
|
---|
| 39 | *
|
---|
[8959] | 40 | */
|
---|
| 41 |
|
---|
[25690] | 42 | public class GS2Browse extends AbstractBrowse
|
---|
[8959] | 43 | {
|
---|
[13124] | 44 |
|
---|
[25690] | 45 | static Logger logger = Logger.getLogger(org.greenstone.gsdl3.service.GS2Browse.class.getName());
|
---|
[13124] | 46 |
|
---|
[25690] | 47 | protected SimpleCollectionDatabase coll_db = null;
|
---|
[26046] | 48 | BasicDocumentDatabase gs_doc_db = null;
|
---|
[25690] | 49 | public GS2Browse()
|
---|
| 50 | {
|
---|
[10093] | 51 | }
|
---|
| 52 |
|
---|
[25690] | 53 | public void cleanUp()
|
---|
| 54 | {
|
---|
| 55 | super.cleanUp();
|
---|
| 56 | this.coll_db.closeDatabase();
|
---|
[26046] | 57 | this.gs_doc_db.cleanUp();
|
---|
[10651] | 58 | }
|
---|
[22974] | 59 |
|
---|
[25690] | 60 | public boolean configure(Element info, Element extra_info)
|
---|
| 61 | {
|
---|
| 62 | if (!super.configure(info, extra_info))
|
---|
| 63 | {
|
---|
| 64 | return false;
|
---|
| 65 | }
|
---|
[8959] | 66 |
|
---|
[25690] | 67 | logger.info("Configuring GS2Browse...");
|
---|
| 68 | // the index stem is either specified in the config file or is the collection name
|
---|
| 69 | Element index_stem_elem = (Element) GSXML.getChildByTagName(info, GSXML.INDEX_STEM_ELEM);
|
---|
| 70 | String index_stem = null;
|
---|
| 71 | if (index_stem_elem != null)
|
---|
| 72 | {
|
---|
| 73 | index_stem = index_stem_elem.getAttribute(GSXML.NAME_ATT);
|
---|
| 74 | }
|
---|
| 75 | if (index_stem == null || index_stem.equals(""))
|
---|
| 76 | {
|
---|
| 77 | index_stem = this.cluster_name;
|
---|
| 78 | }
|
---|
[8959] | 79 |
|
---|
[25690] | 80 | // find out what kind of database we have
|
---|
| 81 | Element database_type_elem = (Element) GSXML.getChildByTagName(info, GSXML.DATABASE_TYPE_ELEM);
|
---|
| 82 | String database_type = null;
|
---|
| 83 | if (database_type_elem != null)
|
---|
| 84 | {
|
---|
| 85 | database_type = database_type_elem.getAttribute(GSXML.NAME_ATT);
|
---|
| 86 | }
|
---|
[8959] | 87 |
|
---|
[25690] | 88 | if (database_type == null || database_type.equals(""))
|
---|
| 89 | {
|
---|
| 90 | database_type = "gdbm"; // the default
|
---|
| 91 | }
|
---|
[26046] | 92 |
|
---|
| 93 | // do we still need this????
|
---|
[25690] | 94 | coll_db = new SimpleCollectionDatabase(database_type);
|
---|
| 95 | if (!coll_db.databaseOK())
|
---|
| 96 | {
|
---|
| 97 | logger.error("Couldn't create the collection database of type " + database_type);
|
---|
| 98 | return false;
|
---|
| 99 | }
|
---|
[8959] | 100 |
|
---|
[25690] | 101 | // Open database for querying
|
---|
| 102 | String coll_db_file = GSFile.collectionDatabaseFile(this.site_home, this.cluster_name, index_stem, database_type);
|
---|
| 103 | if (!this.coll_db.openDatabase(coll_db_file, SimpleCollectionDatabase.READ))
|
---|
| 104 | {
|
---|
| 105 | logger.error("Could not open collection database!");
|
---|
| 106 | return false;
|
---|
| 107 | }
|
---|
[26198] | 108 | this.macro_resolver = new GS2MacroResolver(this.coll_db, this.class_loader);
|
---|
[26046] | 109 |
|
---|
| 110 | gs_doc_db = new BasicDocumentDatabase(this.doc, database_type, this.site_home, this.cluster_name, index_stem);
|
---|
| 111 | if (!gs_doc_db.isValid())
|
---|
| 112 | {
|
---|
| 113 | logger.error("Failed to open Document Database.");
|
---|
| 114 | return false;
|
---|
| 115 | }
|
---|
| 116 | this.gs_doc = gs_doc_db;
|
---|
| 117 |
|
---|
| 118 |
|
---|
[25690] | 119 | return true;
|
---|
[8959] | 120 | }
|
---|
| 121 |
|
---|
[25690] | 122 | /** if id ends in .fc, .pc etc, then translate it to the correct id */
|
---|
| 123 | protected String translateId(String node_id)
|
---|
| 124 | {
|
---|
| 125 | return OID.translateOID(this.coll_db, node_id); //return this.coll_db.translateOID(node_id);
|
---|
[8959] | 126 | }
|
---|
| 127 |
|
---|
[25691] | 128 | protected String getChildType(String node_id)
|
---|
| 129 | {
|
---|
| 130 | DBInfo info = this.coll_db.getInfo(node_id);
|
---|
| 131 | if (info == null)
|
---|
| 132 | {
|
---|
| 133 | return null;
|
---|
| 134 | }
|
---|
| 135 | return info.getInfo("childtype");
|
---|
| 136 | }
|
---|
| 137 |
|
---|
[8959] | 138 |
|
---|
| 139 |
|
---|
| 140 |
|
---|
[25690] | 141 | protected String getMetadata(String node_id, String key)
|
---|
| 142 | {
|
---|
| 143 | DBInfo info = this.coll_db.getInfo(node_id);
|
---|
| 144 | if (info == null)
|
---|
| 145 | {
|
---|
| 146 | return "";
|
---|
| 147 | }
|
---|
| 148 |
|
---|
| 149 | Set<String> keys = info.getKeys();
|
---|
| 150 | Iterator<String> it = keys.iterator();
|
---|
| 151 | while (it.hasNext())
|
---|
| 152 | {
|
---|
| 153 | String key_in = it.next();
|
---|
| 154 | String value = info.getInfo(key);
|
---|
| 155 | if (key_in.equals(key))
|
---|
| 156 | {
|
---|
| 157 | return value;
|
---|
| 158 | }
|
---|
| 159 | }
|
---|
| 160 |
|
---|
| 161 | return "";
|
---|
| 162 |
|
---|
[8959] | 163 | }
|
---|
| 164 |
|
---|
[25690] | 165 | /**
|
---|
| 166 | * get the metadata for the classifier node node_id returns a metadataList
|
---|
| 167 | * element: <metadataList><metadata
|
---|
| 168 | * name="xxx">value</metadata></metadataList> if all_metadata is true,
|
---|
| 169 | * returns all available metadata, otherwise just returns requested metadata
|
---|
| 170 | */
|
---|
| 171 | // assumes only one value per metadata
|
---|
[26088] | 172 | // does no macro resolving. assumes classifier metadata will not have macros.
|
---|
[25690] | 173 | protected Element getMetadataList(String node_id, boolean all_metadata, ArrayList<String> metadata_names)
|
---|
| 174 | {
|
---|
| 175 | String lang = "en";
|
---|
| 176 | Element metadata_list = this.doc.createElement(GSXML.METADATA_ELEM + GSXML.LIST_MODIFIER);
|
---|
| 177 | DBInfo info = this.coll_db.getInfo(node_id);
|
---|
| 178 | if (info == null)
|
---|
| 179 | {
|
---|
| 180 | return null;
|
---|
[8959] | 181 | }
|
---|
[25690] | 182 | if (all_metadata)
|
---|
| 183 | {
|
---|
| 184 | // return everything out of the database
|
---|
| 185 | Set<String> keys = info.getKeys();
|
---|
| 186 | Iterator<String> it = keys.iterator();
|
---|
| 187 | while (it.hasNext())
|
---|
| 188 | {
|
---|
| 189 | String key = it.next();
|
---|
| 190 | String value = info.getInfo(key);
|
---|
[26088] | 191 | GSXML.addMetadata(this.doc, metadata_list, key, value);
|
---|
[25690] | 192 | }
|
---|
| 193 |
|
---|
| 194 | }
|
---|
| 195 | else
|
---|
| 196 | {
|
---|
| 197 | for (int i = 0; i < metadata_names.size(); i++)
|
---|
| 198 | {
|
---|
| 199 | String meta_name = metadata_names.get(i);
|
---|
| 200 | String value = (String) info.getInfo(meta_name);
|
---|
| 201 | GSXML.addMetadata(this.doc, metadata_list, meta_name, value);
|
---|
| 202 | }
|
---|
| 203 | }
|
---|
| 204 | return metadata_list;
|
---|
[8959] | 205 | }
|
---|
| 206 |
|
---|
| 207 |
|
---|
[25690] | 208 | protected int getNumChildren(String node_id)
|
---|
| 209 | {
|
---|
[26046] | 210 | return this.gs_doc.getNumChildren(node_id);
|
---|
[8959] | 211 | }
|
---|
[25690] | 212 |
|
---|
| 213 | /**
|
---|
| 214 | * returns true if the id refers to a document (rather than a classifier
|
---|
| 215 | * node)
|
---|
| 216 | */
|
---|
| 217 | protected boolean isDocumentId(String node_id)
|
---|
| 218 | {
|
---|
| 219 | if (node_id.startsWith("CL"))
|
---|
| 220 | {
|
---|
| 221 | return false;
|
---|
| 222 | }
|
---|
| 223 | return true;
|
---|
[8959] | 224 | }
|
---|
| 225 |
|
---|
| 226 | }
|
---|