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
|
---|
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;
|
---|
28 | import org.greenstone.gsdl3.util.BasicDocumentDatabase;
|
---|
29 | import org.greenstone.gsdl3.util.DBInfo;
|
---|
30 | import org.greenstone.gsdl3.util.MacroResolver;
|
---|
31 | import org.greenstone.gsdl3.util.GS2MacroResolver;
|
---|
32 | import org.greenstone.gsdl3.util.GSFile;
|
---|
33 | import org.greenstone.gsdl3.util.GSXML;
|
---|
34 | import org.greenstone.gsdl3.util.OID;
|
---|
35 | import org.greenstone.gsdl3.util.SimpleCollectionDatabase;
|
---|
36 | import org.w3c.dom.Document;
|
---|
37 | import org.w3c.dom.Element;
|
---|
38 |
|
---|
39 | /**
|
---|
40 | * Greenstone 2 collection classifier service
|
---|
41 | *
|
---|
42 | */
|
---|
43 |
|
---|
44 | public 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(this.coll_db, this.class_loader);
|
---|
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 |
|
---|
122 | return true;
|
---|
123 | }
|
---|
124 |
|
---|
125 | /** if id ends in .fc, .pc etc, then translate it to the correct id */
|
---|
126 | protected String translateId(String node_id)
|
---|
127 | {
|
---|
128 | return OID.translateOID(this.coll_db, node_id); //return this.coll_db.translateOID(node_id);
|
---|
129 | }
|
---|
130 |
|
---|
131 | protected String getChildType(String node_id)
|
---|
132 | {
|
---|
133 | DBInfo info = this.coll_db.getInfo(node_id);
|
---|
134 | if (info == null)
|
---|
135 | {
|
---|
136 | return null;
|
---|
137 | }
|
---|
138 | return info.getInfo("childtype");
|
---|
139 | }
|
---|
140 |
|
---|
141 |
|
---|
142 |
|
---|
143 |
|
---|
144 | protected String getMetadata(String node_id, String key)
|
---|
145 | {
|
---|
146 | DBInfo info = this.coll_db.getInfo(node_id);
|
---|
147 | if (info == null)
|
---|
148 | {
|
---|
149 | return "";
|
---|
150 | }
|
---|
151 |
|
---|
152 | Set<String> keys = info.getKeys();
|
---|
153 | Iterator<String> it = keys.iterator();
|
---|
154 | while (it.hasNext())
|
---|
155 | {
|
---|
156 | String key_in = it.next();
|
---|
157 | String value = info.getInfo(key);
|
---|
158 | if (key_in.equals(key))
|
---|
159 | {
|
---|
160 | return value;
|
---|
161 | }
|
---|
162 | }
|
---|
163 |
|
---|
164 | return "";
|
---|
165 |
|
---|
166 | }
|
---|
167 |
|
---|
168 | /**
|
---|
169 | * get the metadata for the classifier node node_id returns a metadataList
|
---|
170 | * element: <metadataList><metadata
|
---|
171 | * name="xxx">value</metadata></metadataList> if all_metadata is true,
|
---|
172 | * returns all available metadata, otherwise just returns requested metadata
|
---|
173 | */
|
---|
174 | // assumes only one value per metadata
|
---|
175 | // does no macro resolving. assumes classifier metadata will not have macros.
|
---|
176 | protected Element getMetadataList(Document doc, String node_id, boolean all_metadata, ArrayList<String> metadata_names)
|
---|
177 | {
|
---|
178 | String lang = "en";
|
---|
179 | Element metadata_list = doc.createElement(GSXML.METADATA_ELEM + GSXML.LIST_MODIFIER);
|
---|
180 | DBInfo info = this.coll_db.getInfo(node_id);
|
---|
181 | if (info == null)
|
---|
182 | {
|
---|
183 | return null;
|
---|
184 | }
|
---|
185 | if (all_metadata)
|
---|
186 | {
|
---|
187 | // return everything out of the database
|
---|
188 | Set<String> keys = info.getKeys();
|
---|
189 | Iterator<String> it = keys.iterator();
|
---|
190 | while (it.hasNext())
|
---|
191 | {
|
---|
192 | String key = it.next();
|
---|
193 | String value = info.getInfo(key);
|
---|
194 | GSXML.addMetadata(metadata_list, key, this.macro_resolver.resolve(value, lang, MacroResolver.SCOPE_META, node_id));
|
---|
195 | }
|
---|
196 |
|
---|
197 | }
|
---|
198 | else
|
---|
199 | {
|
---|
200 | for (int i = 0; i < metadata_names.size(); i++)
|
---|
201 | {
|
---|
202 | String meta_name = metadata_names.get(i);
|
---|
203 | String value = (String) info.getInfo(meta_name);
|
---|
204 | GSXML.addMetadata(metadata_list, meta_name, this.macro_resolver.resolve(value, lang, MacroResolver.SCOPE_META, node_id));
|
---|
205 | }
|
---|
206 | }
|
---|
207 | return metadata_list;
|
---|
208 | }
|
---|
209 |
|
---|
210 |
|
---|
211 | protected int getNumChildren(String node_id)
|
---|
212 | {
|
---|
213 | return this.gs_doc.getNumChildren(node_id);
|
---|
214 | }
|
---|
215 |
|
---|
216 | /**
|
---|
217 | * returns true if the id refers to a document (rather than a classifier
|
---|
218 | * node)
|
---|
219 | */
|
---|
220 | protected boolean isDocumentId(String node_id)
|
---|
221 | {
|
---|
222 | if (node_id.startsWith("CL"))
|
---|
223 | {
|
---|
224 | return false;
|
---|
225 | }
|
---|
226 | return true;
|
---|
227 | }
|
---|
228 |
|
---|
229 | }
|
---|