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.GS2MacroResolver;
|
---|
31 | import org.greenstone.gsdl3.util.GSFile;
|
---|
32 | import org.greenstone.gsdl3.util.GSXML;
|
---|
33 | import org.greenstone.gsdl3.util.OID;
|
---|
34 | import org.greenstone.gsdl3.util.SimpleCollectionDatabase;
|
---|
35 | import org.w3c.dom.Element;
|
---|
36 |
|
---|
37 | /**
|
---|
38 | * Greenstone 2 collection classifier service
|
---|
39 | *
|
---|
40 | */
|
---|
41 |
|
---|
42 | public class GS2Browse extends AbstractBrowse
|
---|
43 | {
|
---|
44 |
|
---|
45 | static Logger logger = Logger.getLogger(org.greenstone.gsdl3.service.GS2Browse.class.getName());
|
---|
46 |
|
---|
47 | protected SimpleCollectionDatabase coll_db = null;
|
---|
48 | BasicDocumentDatabase gs_doc_db = null;
|
---|
49 | public GS2Browse()
|
---|
50 | {
|
---|
51 | }
|
---|
52 |
|
---|
53 | public void cleanUp()
|
---|
54 | {
|
---|
55 | super.cleanUp();
|
---|
56 | this.coll_db.closeDatabase();
|
---|
57 | this.gs_doc_db.cleanUp();
|
---|
58 | }
|
---|
59 |
|
---|
60 | public boolean configure(Element info, Element extra_info)
|
---|
61 | {
|
---|
62 | if (!super.configure(info, extra_info))
|
---|
63 | {
|
---|
64 | return false;
|
---|
65 | }
|
---|
66 |
|
---|
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 | }
|
---|
79 |
|
---|
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 | }
|
---|
87 |
|
---|
88 | if (database_type == null || database_type.equals(""))
|
---|
89 | {
|
---|
90 | database_type = "gdbm"; // the default
|
---|
91 | }
|
---|
92 |
|
---|
93 | // do we still need this????
|
---|
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 | }
|
---|
100 |
|
---|
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 | }
|
---|
108 | this.macro_resolver = new GS2MacroResolver(this.coll_db);
|
---|
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 |
|
---|
119 | return true;
|
---|
120 | }
|
---|
121 |
|
---|
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);
|
---|
126 | }
|
---|
127 |
|
---|
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 |
|
---|
138 |
|
---|
139 |
|
---|
140 |
|
---|
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 |
|
---|
163 | }
|
---|
164 |
|
---|
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
|
---|
172 | protected Element getMetadataList(String node_id, boolean all_metadata, ArrayList<String> metadata_names)
|
---|
173 | {
|
---|
174 | String lang = "en";
|
---|
175 | Element metadata_list = this.doc.createElement(GSXML.METADATA_ELEM + GSXML.LIST_MODIFIER);
|
---|
176 | DBInfo info = this.coll_db.getInfo(node_id);
|
---|
177 | if (info == null)
|
---|
178 | {
|
---|
179 | return null;
|
---|
180 | }
|
---|
181 | if (all_metadata)
|
---|
182 | {
|
---|
183 | // return everything out of the database
|
---|
184 | Set<String> keys = info.getKeys();
|
---|
185 | Iterator<String> it = keys.iterator();
|
---|
186 | while (it.hasNext())
|
---|
187 | {
|
---|
188 | String key = it.next();
|
---|
189 | String value = info.getInfo(key);
|
---|
190 | GSXML.addMetadata(this.doc, metadata_list, key, this.macro_resolver.resolve(value, lang, GS2MacroResolver.SCOPE_META, node_id));
|
---|
191 | }
|
---|
192 |
|
---|
193 | }
|
---|
194 | else
|
---|
195 | {
|
---|
196 | for (int i = 0; i < metadata_names.size(); i++)
|
---|
197 | {
|
---|
198 | String meta_name = metadata_names.get(i);
|
---|
199 | String value = (String) info.getInfo(meta_name);
|
---|
200 | GSXML.addMetadata(this.doc, metadata_list, meta_name, value);
|
---|
201 | }
|
---|
202 | }
|
---|
203 | return metadata_list;
|
---|
204 | }
|
---|
205 |
|
---|
206 |
|
---|
207 | protected int getNumChildren(String node_id)
|
---|
208 | {
|
---|
209 | return this.gs_doc.getNumChildren(node_id);
|
---|
210 | }
|
---|
211 |
|
---|
212 | /**
|
---|
213 | * returns true if the id refers to a document (rather than a classifier
|
---|
214 | * node)
|
---|
215 | */
|
---|
216 | protected boolean isDocumentId(String node_id)
|
---|
217 | {
|
---|
218 | if (node_id.startsWith("CL"))
|
---|
219 | {
|
---|
220 | return false;
|
---|
221 | }
|
---|
222 | return true;
|
---|
223 | }
|
---|
224 |
|
---|
225 | }
|
---|