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