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