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