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 {
|
---|
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 | /** translates relative oids into proper oids:
|
---|
140 | * .pr (parent), .rt (root) .fc (first child), .lc (last child),
|
---|
141 | * .ns (next sibling), .ps (previous sibling)
|
---|
142 | * .np (next page), .pp (previous page) : links sections in the order that you'd read the document
|
---|
143 | * a suffix is expected to be present so test before using
|
---|
144 | */
|
---|
145 | public String translateOID(String oid) {
|
---|
146 |
|
---|
147 | int p = oid.lastIndexOf('.');
|
---|
148 | if (p != oid.length()-3) {
|
---|
149 | logger.info("translateoid error: '.' is not the third to last char!!");
|
---|
150 | return oid;
|
---|
151 | }
|
---|
152 |
|
---|
153 | String top = oid.substring(0, p);
|
---|
154 | String suff = oid.substring(p+1);
|
---|
155 | // just in case we have multiple extensions, we must translate
|
---|
156 | // we process inner ones first
|
---|
157 | if (OID.needsTranslating(top)) {
|
---|
158 | top = translateOID(top);
|
---|
159 | }
|
---|
160 | if (suff.equals("pr")) {
|
---|
161 | return OID.getParent(top);
|
---|
162 | }
|
---|
163 | if (suff.equals("rt")) {
|
---|
164 | return OID.getTop(top);
|
---|
165 | }
|
---|
166 | if (suff.equals("np")) {
|
---|
167 | // try first child
|
---|
168 | String node_id = translateOID(top+".fc");
|
---|
169 | if (!node_id.equals(top)) {
|
---|
170 | return node_id;
|
---|
171 | }
|
---|
172 | // try next sibling
|
---|
173 | node_id = translateOID(top+".ns");
|
---|
174 | if (!node_id.equals(top)) {
|
---|
175 | return node_id;
|
---|
176 | }
|
---|
177 | // otherwise we keep trying parents sibling
|
---|
178 | String child_id = top;
|
---|
179 | String parent_id = OID.getParent(child_id);
|
---|
180 | while(!parent_id.equals(child_id)) {
|
---|
181 | node_id = translateOID(parent_id+".ns");
|
---|
182 | if (!node_id.equals(parent_id)) {
|
---|
183 | return node_id;
|
---|
184 | }
|
---|
185 | child_id = parent_id;
|
---|
186 | parent_id = OID.getParent(child_id);
|
---|
187 | }
|
---|
188 | return top; // we couldn't get a next page, so just return the original
|
---|
189 | }
|
---|
190 | if (suff.equals("pp")) {
|
---|
191 | String prev_sib = translateOID(top+".ps");
|
---|
192 | if (prev_sib.equals(top)) {
|
---|
193 | // no previous sibling, so return the parent
|
---|
194 | return OID.getParent(top);
|
---|
195 | }
|
---|
196 | // there is a previous sibling, so its either this section, or the last child of the last child..
|
---|
197 | String last_child = translateOID(prev_sib+".lc");
|
---|
198 | while (!last_child.equals(prev_sib)) {
|
---|
199 | prev_sib = last_child;
|
---|
200 | last_child = translateOID(prev_sib+".lc");
|
---|
201 | }
|
---|
202 | return last_child;
|
---|
203 | }
|
---|
204 |
|
---|
205 | int sibling_num = 0;
|
---|
206 | if (suff.equals("ss")) {
|
---|
207 | // we have to remove the sib num before we get top
|
---|
208 | p = top.lastIndexOf('.');
|
---|
209 | sibling_num = Integer.parseInt(top.substring(p+1));
|
---|
210 | top = top.substring(0, p);
|
---|
211 | }
|
---|
212 |
|
---|
213 | // need to get info out of collection db -
|
---|
214 | String doc_id = top;
|
---|
215 | if (suff.endsWith("s")) {
|
---|
216 | doc_id = OID.getParent(top);
|
---|
217 | if (doc_id.equals(top)) {
|
---|
218 | // i.e. we are already at the top
|
---|
219 | return top;
|
---|
220 | }
|
---|
221 | }
|
---|
222 | DBInfo info = getInfo(doc_id);
|
---|
223 | if (info==null) {
|
---|
224 | logger.info("info is null!!");
|
---|
225 | return top;
|
---|
226 | }
|
---|
227 |
|
---|
228 | String contains = info.getInfo("contains");
|
---|
229 | if (contains.equals("")) {
|
---|
230 | // something is wrong
|
---|
231 | return top;
|
---|
232 | }
|
---|
233 | contains = contains.replaceAll("\"", doc_id);
|
---|
234 | String [] children = contains.split(";");
|
---|
235 | if (suff.equals("fc")) {
|
---|
236 | return children[0];
|
---|
237 | } else if (suff.equals("lc")) {
|
---|
238 | return children[children.length-1];
|
---|
239 | } else {
|
---|
240 | if (suff.equals("ss")) {
|
---|
241 | return children[sibling_num-1];
|
---|
242 | }
|
---|
243 | // find the position that we are at.
|
---|
244 | int i=0;
|
---|
245 | while (i<children.length) {
|
---|
246 | if (children[i].equals(top)) {
|
---|
247 | break;
|
---|
248 | }
|
---|
249 | i++;
|
---|
250 | }
|
---|
251 |
|
---|
252 | if (suff.equals("ns")) {
|
---|
253 | if (i==children.length-1) {
|
---|
254 | return children[i];
|
---|
255 | }
|
---|
256 | return children[i+1];
|
---|
257 | } else if (suff.equals("ps")) {
|
---|
258 | if (i==0) {
|
---|
259 | return children[i];
|
---|
260 | }
|
---|
261 | return children[i-1];
|
---|
262 | }
|
---|
263 | }
|
---|
264 |
|
---|
265 | return top;
|
---|
266 | }
|
---|
267 | }
|
---|