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

Last change on this file since 24393 was 24393, checked in by sjm84, 13 years ago

Adding in the server-side code for the Document Maker as well as several other enhancements

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