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 | */
|
---|
19 | package org.greenstone.gsdl3.util;
|
---|
20 |
|
---|
21 | import org.apache.log4j.*;
|
---|
22 |
|
---|
23 | public 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 | if (db_type.equalsIgnoreCase("gdbm")) {
|
---|
36 | this.coll_db = new GDBMWrapper();
|
---|
37 | }
|
---|
38 | else if (db_type.equalsIgnoreCase("jdbm")) {
|
---|
39 | this.coll_db = new JDBMWrapper();
|
---|
40 | }
|
---|
41 | else {
|
---|
42 | logger.error("Couldn't create SimpleCollectionDatabase of type "+db_type);
|
---|
43 | }
|
---|
44 | }
|
---|
45 |
|
---|
46 | /** open the database filename, with mode mode - uses the FlatDatabaseWrapper modes */
|
---|
47 | public boolean openDatabase(String filename, int mode){
|
---|
48 | return this.coll_db.openDatabase(filename, mode);
|
---|
49 | }
|
---|
50 |
|
---|
51 | /** close the database */
|
---|
52 | public void closeDatabase() {
|
---|
53 | this.coll_db.closeDatabase();
|
---|
54 | }
|
---|
55 |
|
---|
56 | /** Returns a DBInfo structure of the key-value pairs associated with a
|
---|
57 | particular main key in the database */
|
---|
58 | public DBInfo getInfo(String main_key) {
|
---|
59 | // logger.warn("All the entries of the db are:");
|
---|
60 | // this.coll_db.displayAllEntries();
|
---|
61 |
|
---|
62 | String key_info = this.coll_db.getValue(main_key);
|
---|
63 | if (key_info == null || key_info.equals("")) {
|
---|
64 | return null;
|
---|
65 | }
|
---|
66 |
|
---|
67 | DBInfo info = new DBInfo();
|
---|
68 |
|
---|
69 | String [] lines = key_info.split("\n");
|
---|
70 | String key;
|
---|
71 | String value;
|
---|
72 | for (int i=0; i<lines.length; i++) {
|
---|
73 | logger.debug("line:"+lines[i]);
|
---|
74 | int a = lines[i].indexOf('<');
|
---|
75 | int b= lines[i].indexOf('>');
|
---|
76 | if (a==-1 || b==-1) {
|
---|
77 | logger.error("bad format in db");
|
---|
78 | }
|
---|
79 | else {
|
---|
80 | key=lines[i].substring(a+1, b);
|
---|
81 | value=lines[i].substring(b+1);
|
---|
82 | logger.debug("key="+key+", val="+value);
|
---|
83 | info.addInfo(key, value);
|
---|
84 |
|
---|
85 | }
|
---|
86 | }
|
---|
87 | return info;
|
---|
88 |
|
---|
89 | }
|
---|
90 |
|
---|
91 | /** converts a greenstone OID to internal docnum */
|
---|
92 | public String OID2Docnum(String OID) {
|
---|
93 | DBInfo info = getInfo(OID);
|
---|
94 | if (info != null) {
|
---|
95 | return info.getInfo("docnum");
|
---|
96 | }
|
---|
97 | return null;
|
---|
98 | }
|
---|
99 |
|
---|
100 | /** converts a greenstone OID to an internal docnum, returning a Long
|
---|
101 | - convenience method*/
|
---|
102 | public long OID2DocnumLong(String OID) {
|
---|
103 | DBInfo info = getInfo(OID);
|
---|
104 | if (info != null) {
|
---|
105 | long real_num = Long.parseLong(info.getInfo("docnum"));
|
---|
106 | return real_num;
|
---|
107 | }
|
---|
108 | return -1;
|
---|
109 | }
|
---|
110 |
|
---|
111 |
|
---|
112 | /** converts a docnum to greenstone OID */
|
---|
113 | public String docnum2OID(String docnum) {
|
---|
114 | DBInfo info = getInfo(docnum);
|
---|
115 | if (info!=null){
|
---|
116 | String oid = info.getInfo("section");
|
---|
117 | return oid;
|
---|
118 | } else{
|
---|
119 | return null;
|
---|
120 | }
|
---|
121 | }
|
---|
122 |
|
---|
123 | /** converts a docnum to greenstone OID
|
---|
124 | - convenience method */
|
---|
125 | public String docnum2OID(long docnum) {
|
---|
126 | return docnum2OID(Long.toString(docnum));
|
---|
127 | }
|
---|
128 |
|
---|
129 | /** converts an external id to greenstone OID */
|
---|
130 | public String externalId2OID(String extid) {
|
---|
131 | DBInfo info = getInfo(extid);
|
---|
132 | if (info != null) {
|
---|
133 | String oid = info.getInfo("section");
|
---|
134 | return oid;
|
---|
135 | }
|
---|
136 | return null;
|
---|
137 | }
|
---|
138 |
|
---|
139 | /** After OID.translateOID() is through, this method processes OID further
|
---|
140 | * to translate relative oids into proper oids:
|
---|
141 | * .pr (parent), .rt (root) .fc (first child), .lc (last child),
|
---|
142 | * .ns (next sibling), .ps (previous sibling)
|
---|
143 | * .np (next page), .pp (previous page) : links sections in the order that you'd read the document
|
---|
144 | * a suffix is expected to be present so test before using
|
---|
145 | */
|
---|
146 | public String processOID(String doc_id, String top, String suff, int sibling_num) {
|
---|
147 | DBInfo info = getInfo(doc_id);
|
---|
148 | if (info==null) {
|
---|
149 | logger.info("info is null!!");
|
---|
150 | return top;
|
---|
151 | }
|
---|
152 |
|
---|
153 | String contains = info.getInfo("contains");
|
---|
154 | if (contains.equals("")) {
|
---|
155 | // something is wrong
|
---|
156 | return top;
|
---|
157 | }
|
---|
158 | contains = contains.replaceAll("\"", doc_id);
|
---|
159 | String [] children = contains.split(";");
|
---|
160 | if (suff.equals("fc")) {
|
---|
161 | return children[0];
|
---|
162 | } else if (suff.equals("lc")) {
|
---|
163 | return children[children.length-1];
|
---|
164 | } else {
|
---|
165 | if (suff.equals("ss")) {
|
---|
166 | return children[sibling_num-1];
|
---|
167 | }
|
---|
168 | // find the position that we are at.
|
---|
169 | int i=0;
|
---|
170 | while (i<children.length) {
|
---|
171 | if (children[i].equals(top)) {
|
---|
172 | break;
|
---|
173 | }
|
---|
174 | i++;
|
---|
175 | }
|
---|
176 |
|
---|
177 | if (suff.equals("ns")) {
|
---|
178 | if (i==children.length-1) {
|
---|
179 | return children[i];
|
---|
180 | }
|
---|
181 | return children[i+1];
|
---|
182 | } else if (suff.equals("ps")) {
|
---|
183 | if (i==0) {
|
---|
184 | return children[i];
|
---|
185 | }
|
---|
186 | return children[i-1];
|
---|
187 | }
|
---|
188 | }
|
---|
189 |
|
---|
190 | return top;
|
---|
191 | }
|
---|
192 | }
|
---|