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 | import org.apache.commons.lang3.StringUtils;
|
---|
24 |
|
---|
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 | }
|
---|
191 |
|
---|
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 | }
|
---|
228 |
|
---|
229 | return top;
|
---|
230 | }
|
---|
231 | }
|
---|