source: main/trunk/greenstone3/src/java/org/greenstone/gsdl3/util/SimpleCollectionDatabase.java@ 27178

Last change on this file since 27178 was 27178, checked in by davidb, 11 years ago

Monitor for 'server' as suffix on DBtype (used in parallel building), and remove so Java *runtime* code loads up correctly the corresponding Wrapper DB type

File size: 7.7 KB
Line 
1/*
2 * SimpleCollectionDatabase.java
3 * Copyright (C) 2008 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.util;
20
21import java.util.Iterator;
22import java.util.Set;
23import java.util.Vector;
24
25import org.apache.log4j.*;
26
27import org.apache.commons.lang3.StringUtils;
28
29public class SimpleCollectionDatabase implements OID.OIDTranslatable
30{
31
32 static Logger logger = Logger.getLogger(org.greenstone.gsdl3.util.SimpleCollectionDatabase.class.getName());
33
34 /* just read access, many readers can share a database */
35 public final static int READ = FlatDatabaseWrapper.READ;
36 /* read/write, exclusive access */
37 public final static int WRITE = FlatDatabaseWrapper.WRITE;
38
39 protected FlatDatabaseWrapper coll_db = null;
40
41 public SimpleCollectionDatabase(String db_type)
42 {
43
44 if (db_type.toLowerCase().endsWith("server")) {
45 db_type = db_type.substring(0, db_type.length() - 6);
46 }
47
48 // Access databaseWrapper through reflection (forName) so code
49 // can be more dynamic as to the database backends that are
50 // supported for this installation of Greenstone
51
52 String dbwrap_name = db_type.toUpperCase() + "Wrapper";
53 Class dbwrap_class = null;
54
55 try
56 {
57 String full_dbwrap_name = "org.greenstone.gsdl3.util." + dbwrap_name;
58 dbwrap_class = Class.forName(full_dbwrap_name);
59 }
60 catch (ClassNotFoundException e)
61 {
62 try
63 {
64 //try the dbwrap_name alone in case the package name is
65 //already specified
66 dbwrap_class = Class.forName(dbwrap_name);
67 }
68 catch (ClassNotFoundException ae)
69 {
70 logger.error("Couldn't create SimpleCollectionDatabase of type " + db_type);
71 logger.info(ae.getMessage());
72 }
73 }
74
75 try
76 {
77 this.coll_db = (FlatDatabaseWrapper) dbwrap_class.newInstance();
78 }
79 catch (Exception e)
80 {
81 logger.error("Failed to call the constructor " + dbwrap_name + "()");
82 }
83
84 }
85
86 public boolean databaseOK()
87 {
88 // Previously failed to open database
89 // Most likely cause is that this installation of Greenstone3 has not
90 // been compiled with support for this database type
91 return coll_db != null;
92 }
93
94 /**
95 * open the database filename, with mode mode - uses the FlatDatabaseWrapper
96 * modes
97 */
98 public boolean openDatabase(String filename, int mode)
99 {
100 return this.coll_db.openDatabase(filename, mode);
101 }
102
103 /** close the database */
104 public void closeDatabase()
105 {
106 this.coll_db.closeDatabase();
107 }
108
109 /**
110 * Returns a DBInfo structure of the key-value pairs associated with a
111 * particular main key in the database
112 */
113 public DBInfo getInfo(String main_key)
114 {
115 // logger.warn("All the entries of the db are:");
116 // this.coll_db.displayAllEntries();
117
118 if (this.coll_db == null)
119 {
120 // Most likely cause is that this installation of Greenstone3 has not
121 // been compiled with support for this database type
122 return null;
123 }
124
125 String key_info = this.coll_db.getValue(main_key);
126 if (key_info == null || key_info.equals(""))
127 {
128 return null;
129 }
130
131 DBInfo info = new DBInfo();
132
133 String[] lines = StringUtils.split(key_info, "\n");
134 String key;
135 String value;
136 for (int i = 0; i < lines.length; i++)
137 {
138 logger.debug("line:" + lines[i]);
139 int a = lines[i].indexOf('<');
140 int b = lines[i].indexOf('>');
141 if (a == -1 || b == -1)
142 {
143 logger.error("bad format in db");
144 }
145 else
146 {
147 key = lines[i].substring(a + 1, b);
148 value = lines[i].substring(b + 1);
149 logger.debug("key=" + key + ", val=" + value);
150 info.addInfo(key, value);
151 }
152 }
153
154 return info;
155 }
156
157 public boolean setInfo(String mainKey, DBInfo info)
158 {
159 StringBuilder valueToAdd = new StringBuilder();
160 Iterator<String> i = info.getKeys().iterator();
161 while (i.hasNext())
162 {
163 String currentKey = i.next();
164 Vector<String> currentValues = (Vector<String>)info.getMultiInfo(currentKey);
165
166 if(currentValues.size() == 0)
167 {
168 valueToAdd.append("<" + currentKey + ">\n");
169 continue;
170 }
171
172 for(int j = 0; j < currentValues.size(); j++)
173 {
174 valueToAdd.append("<" + currentKey + ">" + currentValues.get(j) + "\n");
175 }
176 }
177
178 //Remove the final \n
179 if (valueToAdd.length() > 0)
180 {
181 valueToAdd.delete(valueToAdd.length() - 1, valueToAdd.length());
182 }
183
184 return this.coll_db.setValue(mainKey, valueToAdd.toString());
185 }
186
187 public String getValue(String key)
188 {
189 return this.coll_db.getValue(key);
190 }
191
192 public boolean setValue(String key, String value)
193 {
194 return this.coll_db.setValue(key, value);
195 }
196
197 public boolean deleteKey(String key)
198 {
199 return this.coll_db.deleteKey(key);
200 }
201
202 /** converts a greenstone OID to internal docnum */
203 public String OID2Docnum(String OID)
204 {
205 DBInfo info = getInfo(OID);
206 if (info != null)
207 {
208 return info.getInfo("docnum");
209 }
210 return null;
211 }
212
213 /**
214 * converts a greenstone OID to an internal docnum, returning a Long -
215 * convenience method
216 */
217 public long OID2DocnumLong(String OID)
218 {
219 DBInfo info = getInfo(OID);
220 if (info != null)
221 {
222 long real_num = Long.parseLong(info.getInfo("docnum"));
223 return real_num;
224 }
225 return -1;
226 }
227
228 /** converts a docnum to greenstone OID */
229 public String docnum2OID(String docnum)
230 {
231 DBInfo info = getInfo(docnum);
232 if (info != null)
233 {
234 String oid = info.getInfo("section");
235 return oid;
236 }
237 else
238 {
239 return null;
240 }
241 }
242
243 /**
244 * converts a docnum to greenstone OID - convenience method
245 */
246 public String docnum2OID(long docnum)
247 {
248 return docnum2OID(Long.toString(docnum));
249 }
250
251 /** converts an external id to greenstone OID */
252 public String externalId2OID(String extid)
253 {
254 DBInfo info = getInfo(extid);
255 if (info != null)
256 {
257 String oid = info.getInfo("section");
258 return oid;
259 }
260 return null;
261 }
262
263 /**
264 * After OID.translateOID() is through, this method processes OID further to
265 * translate relative oids into proper oids: .pr (parent), .rt (root) .fc
266 * (first child), .lc (last child), .ns (next sibling), .ps (previous
267 * sibling) .np (next page), .pp (previous page) : links sections in the
268 * order that you'd read the document a suffix is expected to be present so
269 * test before using
270 */
271 public String processOID(String doc_id, String top, String suff, int sibling_num)
272 {
273 DBInfo info = getInfo(doc_id);
274 if (info == null)
275 {
276 logger.info("info is null!!");
277 return top;
278 }
279
280 String contains = info.getInfo("contains");
281 if (contains.equals(""))
282 {
283 // something is wrong
284 return top;
285 }
286 contains = StringUtils.replace(contains, "\"", doc_id);
287 String[] children = StringUtils.split(contains, ";");
288 if (suff.equals("fc"))
289 {
290 return children[0];
291 }
292 else if (suff.equals("lc"))
293 {
294 return children[children.length - 1];
295 }
296 else
297 {
298 if (suff.equals("ss"))
299 {
300 return children[sibling_num - 1];
301 }
302 // find the position that we are at.
303 int i = 0;
304 while (i < children.length)
305 {
306 if (children[i].equals(top))
307 {
308 break;
309 }
310 i++;
311 }
312
313 if (suff.equals("ns"))
314 {
315 if (i == children.length - 1)
316 {
317 return children[i];
318 }
319 return children[i + 1];
320 }
321 else if (suff.equals("ps"))
322 {
323 if (i == 0)
324 {
325 return children[i];
326 }
327 return children[i - 1];
328 }
329 }
330
331 return top;
332 }
333}
Note: See TracBrowser for help on using the repository browser.