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

Last change on this file since 22973 was 22973, checked in by davidb, 14 years ago

Code changed to use Class.forName to dynamically load required database wrapper class (e.g. GDBMWrapper or JDBMWrapper). This is so the code can be compiled up with some of these database wrapper classes optionally switched out (e.g. name changed to .java.tmp). Additional routine also added to test is a database is open. In JDBMWrapper, the close routine was modified so it only closes the database one. Our code seems to call 'cleanup' more than once, and letting it close a JDBMWrapper class a second time throws an exception deep within JDBM implementation.

File size: 6.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 org.apache.log4j.*;
22
23public class SimpleCollectionDatabase implements OID.OIDTranslatable {
24
25 static Logger logger = Logger.getLogger(org.greenstone.gsdl3.util.SimpleCollectionDatabase.class.getName());
26
27 /* just read access, many readers can share a database */
28 public final static int READ = FlatDatabaseWrapper.READ;
29 /* read/write, exclusive access */
30 public final static int WRITE = FlatDatabaseWrapper.WRITE;
31
32 protected FlatDatabaseWrapper coll_db = null;
33
34 public SimpleCollectionDatabase(String db_type) {
35
36 // Access databaseWrapper through reflection (forName) so code
37 // can be more dynamic as to the database backends that are
38 // supported for this installation of Greenstone
39
40 String dbwrap_name = db_type.toUpperCase() + "Wrapper";
41 Class dbwrap_class = null;
42
43 try {
44 String full_dbwrap_name = "org.greenstone.gsdl3.util."+dbwrap_name;
45 dbwrap_class = Class.forName(full_dbwrap_name);
46 }
47 catch(ClassNotFoundException e) {
48 try {
49 //try the dbwrap_name alone in case the package name is
50 //already specified
51 dbwrap_class = Class.forName(dbwrap_name);
52 }
53 catch(ClassNotFoundException ae) {
54 logger.error("Couldn't create SimpleCollectionDatabase of type "+db_type);
55 logger.info(ae.getMessage());
56 }
57 }
58
59 try {
60 this.coll_db = (FlatDatabaseWrapper)dbwrap_class.newInstance();
61 }
62 catch(Exception e) {
63 logger.error("Failed to call the constructor "+dbwrap_name+"()");
64 }
65
66
67 }
68
69 public boolean databaseOK() {
70 // Previously failed to open database
71 // Most likely cause is that this installation of Greenstone3 has not
72 // been compiled with support for this database type
73 return coll_db != null;
74 }
75
76 /** open the database filename, with mode mode - uses the FlatDatabaseWrapper modes */
77 public boolean openDatabase(String filename, int mode){
78 return this.coll_db.openDatabase(filename, mode);
79 }
80
81 /** close the database */
82 public void closeDatabase() {
83 this.coll_db.closeDatabase();
84 }
85
86 /** Returns a DBInfo structure of the key-value pairs associated with a
87 particular main key in the database */
88 public DBInfo getInfo(String main_key) {
89 // logger.warn("All the entries of the db are:");
90 // this.coll_db.displayAllEntries();
91
92
93 if (this.coll_db==null) {
94 // Most likely cause is that this installation of Greenstone3 has not
95 // been compiled with support for this database type
96 return null;
97 }
98
99 String key_info = this.coll_db.getValue(main_key);
100 if (key_info == null || key_info.equals("")) {
101 return null;
102 }
103
104 DBInfo info = new DBInfo();
105
106 String [] lines = key_info.split("\n");
107 String key;
108 String value;
109 for (int i=0; i<lines.length; i++) {
110 logger.debug("line:"+lines[i]);
111 int a = lines[i].indexOf('<');
112 int b= lines[i].indexOf('>');
113 if (a==-1 || b==-1) {
114 logger.error("bad format in db");
115 }
116 else {
117 key=lines[i].substring(a+1, b);
118 value=lines[i].substring(b+1);
119 logger.debug("key="+key+", val="+value);
120 info.addInfo(key, value);
121
122 }
123 }
124 return info;
125
126 }
127
128 /** converts a greenstone OID to internal docnum */
129 public String OID2Docnum(String OID) {
130 DBInfo info = getInfo(OID);
131 if (info != null) {
132 return info.getInfo("docnum");
133 }
134 return null;
135 }
136
137 /** converts a greenstone OID to an internal docnum, returning a Long
138 - convenience method*/
139 public long OID2DocnumLong(String OID) {
140 DBInfo info = getInfo(OID);
141 if (info != null) {
142 long real_num = Long.parseLong(info.getInfo("docnum"));
143 return real_num;
144 }
145 return -1;
146 }
147
148
149 /** converts a docnum to greenstone OID */
150 public String docnum2OID(String docnum) {
151 DBInfo info = getInfo(docnum);
152 if (info!=null){
153 String oid = info.getInfo("section");
154 return oid;
155 } else{
156 return null;
157 }
158 }
159
160 /** converts a docnum to greenstone OID
161 - convenience method */
162 public String docnum2OID(long docnum) {
163 return docnum2OID(Long.toString(docnum));
164 }
165
166 /** converts an external id to greenstone OID */
167 public String externalId2OID(String extid) {
168 DBInfo info = getInfo(extid);
169 if (info != null) {
170 String oid = info.getInfo("section");
171 return oid;
172 }
173 return null;
174 }
175
176 /** After OID.translateOID() is through, this method processes OID further
177 * to translate relative oids into proper oids:
178 * .pr (parent), .rt (root) .fc (first child), .lc (last child),
179 * .ns (next sibling), .ps (previous sibling)
180 * .np (next page), .pp (previous page) : links sections in the order that you'd read the document
181 * a suffix is expected to be present so test before using
182 */
183 public String processOID(String doc_id, String top, String suff, int sibling_num) {
184 DBInfo info = getInfo(doc_id);
185 if (info==null) {
186 logger.info("info is null!!");
187 return top;
188 }
189
190 String contains = info.getInfo("contains");
191 if (contains.equals("")) {
192 // something is wrong
193 return top;
194 }
195 contains = contains.replaceAll("\"", doc_id);
196 String [] children = contains.split(";");
197 if (suff.equals("fc")) {
198 return children[0];
199 } else if (suff.equals("lc")) {
200 return children[children.length-1];
201 } else {
202 if (suff.equals("ss")) {
203 return children[sibling_num-1];
204 }
205 // find the position that we are at.
206 int i=0;
207 while (i<children.length) {
208 if (children[i].equals(top)) {
209 break;
210 }
211 i++;
212 }
213
214 if (suff.equals("ns")) {
215 if (i==children.length-1) {
216 return children[i];
217 }
218 return children[i+1];
219 } else if (suff.equals("ps")) {
220 if (i==0) {
221 return children[i];
222 }
223 return children[i-1];
224 }
225 }
226
227 return top;
228 }
229}
Note: See TracBrowser for help on using the repository browser.