source: main/trunk/greenstone3/src/java/org/greenstone/gsdl3/service/GS2Browse.java@ 32453

Last change on this file since 32453 was 31867, checked in by kjdon, 7 years ago

setting classifier style attribute - can't remember why! Also, need to set teh class loader for the macro resolver AFTER the class loader is constructedsvn diff util/GSXML.java

  • Property svn:keywords set to Author Date Id Revision
File size: 6.8 KB
Line 
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 */
19package org.greenstone.gsdl3.service;
20
21// Greenstone classes
22import java.util.ArrayList;
23import java.util.Iterator;
24import java.util.Set;
25import java.util.StringTokenizer;
26
27import org.apache.log4j.Logger;
28import org.greenstone.gsdl3.util.BasicDocumentDatabase;
29import org.greenstone.gsdl3.util.DBInfo;
30import org.greenstone.gsdl3.util.MacroResolver;
31import org.greenstone.gsdl3.util.GS2MacroResolver;
32import org.greenstone.gsdl3.util.GSFile;
33import org.greenstone.gsdl3.util.GSXML;
34import org.greenstone.gsdl3.util.OID;
35import org.greenstone.gsdl3.util.SimpleCollectionDatabase;
36import org.w3c.dom.Document;
37import org.w3c.dom.Element;
38
39/**
40 * Greenstone 2 collection classifier service
41 *
42 */
43
44public class GS2Browse extends AbstractBrowse
45{
46
47 static Logger logger = Logger.getLogger(org.greenstone.gsdl3.service.GS2Browse.class.getName());
48
49 protected SimpleCollectionDatabase coll_db = null;
50 BasicDocumentDatabase gs_doc_db = null;
51 public GS2Browse()
52 {
53 this.macro_resolver = new GS2MacroResolver();
54 }
55
56 public void cleanUp()
57 {
58 super.cleanUp();
59 this.coll_db.closeDatabase();
60 this.gs_doc_db.cleanUp();
61 }
62
63 public boolean configure(Element info, Element extra_info)
64 {
65 if (!super.configure(info, extra_info))
66 {
67 return false;
68 }
69
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 }
82
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 }
90
91 if (database_type == null || database_type.equals(""))
92 {
93 database_type = "gdbm"; // the default
94 }
95
96 // do we still need this????
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 }
103
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 }
111
112
113 gs_doc_db = new BasicDocumentDatabase(database_type, this.site_home, this.cluster_name, index_stem);
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;
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);
126
127
128 return true;
129 }
130
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);
135 }
136
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
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 }
160
161
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
184 }
185
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
193 // does no macro resolving. assumes classifier metadata will not have macros.
194 protected Element getMetadataList(Document doc, String node_id, boolean all_metadata, ArrayList<String> metadata_names)
195 {
196 String lang = "en";
197 Element metadata_list = doc.createElement(GSXML.METADATA_ELEM + GSXML.LIST_MODIFIER);
198 DBInfo info = this.coll_db.getInfo(node_id);
199 if (info == null)
200 {
201 return null;
202 }
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);
212 GSXML.addMetadata(metadata_list, key, this.macro_resolver.resolve(value, lang, MacroResolver.SCOPE_META, node_id));
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);
222 GSXML.addMetadata(metadata_list, meta_name, this.macro_resolver.resolve(value, lang, MacroResolver.SCOPE_META, node_id));
223 }
224 }
225 return metadata_list;
226 }
227
228
229 protected int getNumChildren(String node_id)
230 {
231 return this.gs_doc.getNumChildren(node_id);
232 }
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;
245 }
246
247}
Note: See TracBrowser for help on using the repository browser.