1
|
/**
|
2
|
* '$RCSfile$'
|
3
|
* Purpose: A Class that implements utility methods like:
|
4
|
* 1/ Reding all doctypes from db connection
|
5
|
* 2/ Reading DTD or Schema file from Metacat catalog system
|
6
|
* 3/ Reading Lore type Data Guide from db connection
|
7
|
* Copyright: 2000 Regents of the University of California and the
|
8
|
* National Center for Ecological Analysis and Synthesis
|
9
|
* Authors: Jivka Bojilova
|
10
|
*
|
11
|
* '$Author: berkley $'
|
12
|
* '$Date: 2007-01-18 10:36:49 -0800 (Thu, 18 Jan 2007) $'
|
13
|
* '$Revision: 3143 $'
|
14
|
*
|
15
|
* This program is free software; you can redistribute it and/or modify
|
16
|
* it under the terms of the GNU General Public License as published by
|
17
|
* the Free Software Foundation; either version 2 of the License, or
|
18
|
* (at your option) any later version.
|
19
|
*
|
20
|
* This program is distributed in the hope that it will be useful,
|
21
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
22
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
23
|
* GNU General Public License for more details.
|
24
|
*
|
25
|
* You should have received a copy of the GNU General Public License
|
26
|
* along with this program; if not, write to the Free Software
|
27
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
28
|
*/
|
29
|
|
30
|
package edu.ucsb.nceas.metacat;
|
31
|
|
32
|
import java.sql.Connection;
|
33
|
import java.sql.SQLException;
|
34
|
import java.sql.PreparedStatement;
|
35
|
import java.sql.ResultSet;
|
36
|
|
37
|
import java.io.BufferedInputStream;
|
38
|
import java.io.InputStream;
|
39
|
import java.io.IOException;
|
40
|
import java.net.URL;
|
41
|
import java.net.URLConnection;
|
42
|
import java.net.MalformedURLException;
|
43
|
|
44
|
import java.util.Enumeration;
|
45
|
import java.util.Vector;
|
46
|
import java.util.Stack;
|
47
|
|
48
|
import org.apache.log4j.Logger;
|
49
|
|
50
|
/**
|
51
|
* A suite of utility classes for quering DB
|
52
|
*/
|
53
|
public class DBUtil {
|
54
|
|
55
|
//private Connection conn = null;
|
56
|
private static Logger logMetacat = Logger.getLogger(DBUtil.class);
|
57
|
|
58
|
/**
|
59
|
* main routine used for testing.
|
60
|
* <p>
|
61
|
* Usage: java DBUtil <-dt|-dg>
|
62
|
*
|
63
|
* @param -dt for selecting all doctypes
|
64
|
* -dg for selecting DataGuide
|
65
|
*/
|
66
|
static public void main(String[] args) {
|
67
|
|
68
|
if (args.length < 1)
|
69
|
{
|
70
|
System.err.println("Wrong number of arguments!!!");
|
71
|
System.err.println(
|
72
|
"USAGE: java DBUtil <-dt | -ds [doctype] | -dl user>");
|
73
|
return;
|
74
|
} else {
|
75
|
try {
|
76
|
|
77
|
// Open a connection to the database
|
78
|
MetaCatUtil util = new MetaCatUtil();
|
79
|
//Connection dbconn = util.openDBConnection();
|
80
|
|
81
|
DBUtil dbutil = new DBUtil();
|
82
|
|
83
|
if ( args[0].equals("-dt") ) {
|
84
|
String doctypes = dbutil.readDoctypes();
|
85
|
System.out.println(doctypes);
|
86
|
} else if ( args[0].equals("-ds") ) {
|
87
|
String doctype = null;
|
88
|
if ( args.length == 2 ) { doctype = args[1]; }
|
89
|
String dtdschema = dbutil.readDTDSchema(doctype);
|
90
|
System.out.println(dtdschema);
|
91
|
} else if ( args[0].equals("-dl") ) {
|
92
|
String scope = "";
|
93
|
if ( args.length == 2 ) { scope = args[1]; }
|
94
|
String docid = dbutil.getMaxDocid(scope);
|
95
|
System.out.println(docid);
|
96
|
} else {
|
97
|
System.err.println(
|
98
|
"USAGE: java DBUtil <-dt | -ds [doctype] | -dg [doctype]>");
|
99
|
}
|
100
|
|
101
|
} catch (Exception e) {
|
102
|
//System.err.println("error in DBUtil.main");
|
103
|
//System.err.println(e.getMessage());
|
104
|
e.printStackTrace(System.err);
|
105
|
}
|
106
|
}
|
107
|
}
|
108
|
|
109
|
/**
|
110
|
* Construct an instance of the utility class
|
111
|
*/
|
112
|
public DBUtil() {
|
113
|
//this.conn = conn;
|
114
|
}
|
115
|
|
116
|
/**
|
117
|
* read all doctypes from db connection in XML format
|
118
|
* select all Public Id from xml_catalog table
|
119
|
*/
|
120
|
public String readDoctypes()
|
121
|
throws SQLException {
|
122
|
|
123
|
Vector doctypeList = new Vector();
|
124
|
DBConnection dbConn = null;
|
125
|
int serialNumber = -1;
|
126
|
PreparedStatement pstmt = null;
|
127
|
try {
|
128
|
|
129
|
dbConn=DBConnectionPool.
|
130
|
getDBConnection("DBUtil.readDoctypes");
|
131
|
serialNumber=dbConn.getCheckOutSerialNumber();
|
132
|
pstmt =
|
133
|
dbConn.prepareStatement("SELECT public_id FROM xml_catalog " +
|
134
|
"WHERE entry_type = 'DTD'");
|
135
|
|
136
|
pstmt.execute();
|
137
|
ResultSet rs = pstmt.getResultSet();
|
138
|
boolean tableHasRows = rs.next();
|
139
|
while (tableHasRows) {
|
140
|
doctypeList.addElement(rs.getString(1));
|
141
|
tableHasRows = rs.next();
|
142
|
}
|
143
|
|
144
|
pstmt.close();
|
145
|
|
146
|
} catch (SQLException e) {
|
147
|
throw new SQLException("DBUtil.readDoctypes(). " + e.getMessage());
|
148
|
}
|
149
|
finally
|
150
|
{
|
151
|
try
|
152
|
{
|
153
|
pstmt.close();
|
154
|
}//try
|
155
|
finally
|
156
|
{
|
157
|
DBConnectionPool.returnDBConnection(dbConn, serialNumber);
|
158
|
}//finally
|
159
|
}//finally
|
160
|
|
161
|
|
162
|
return formatToXML(doctypeList, "doctype");
|
163
|
}
|
164
|
|
165
|
/**
|
166
|
* read DTD or Schema file from Metacat's XML catalog system
|
167
|
*/
|
168
|
public String readDTDSchema(String doctype)
|
169
|
throws SQLException, MalformedURLException, IOException
|
170
|
{
|
171
|
String systemID = null;
|
172
|
PreparedStatement pstmt = null;
|
173
|
StringBuffer cbuff = new StringBuffer();
|
174
|
DBConnection dbConn = null;
|
175
|
int serialNumber = -1;
|
176
|
// get doctype's System ID from db catalog
|
177
|
try {
|
178
|
|
179
|
dbConn=DBConnectionPool.
|
180
|
getDBConnection("DBUtil.readDTDSchema");
|
181
|
serialNumber=dbConn.getCheckOutSerialNumber();
|
182
|
pstmt = dbConn.prepareStatement("SELECT system_id " +
|
183
|
"FROM xml_catalog " +
|
184
|
"WHERE entry_type in ('DTD','Schema') " +
|
185
|
"AND public_id LIKE ?");
|
186
|
pstmt.setString(1, doctype);
|
187
|
pstmt.execute();
|
188
|
ResultSet rs = pstmt.getResultSet();
|
189
|
boolean hasRow = rs.next();
|
190
|
if (hasRow) {
|
191
|
systemID = rs.getString(1);
|
192
|
} else {
|
193
|
throw new SQLException("Non-registered doctype: " + doctype);
|
194
|
}
|
195
|
pstmt.close();
|
196
|
|
197
|
} catch (SQLException e) {
|
198
|
throw new SQLException("DBUtil.readDTD(). " + e.getMessage());
|
199
|
}
|
200
|
finally
|
201
|
{
|
202
|
try
|
203
|
{
|
204
|
pstmt.close();
|
205
|
}//try
|
206
|
finally
|
207
|
{
|
208
|
DBConnectionPool.returnDBConnection(dbConn, serialNumber);
|
209
|
}//finally
|
210
|
}//finally
|
211
|
|
212
|
// read from URL stream as specified by the System ID.
|
213
|
try {
|
214
|
// open a connection to this URL and return an InputStream
|
215
|
// for reading from that connection
|
216
|
InputStream istream = new URL(systemID).openStream();
|
217
|
// create a buffering character-input stream
|
218
|
// that uses a default-sized input buffer
|
219
|
BufferedInputStream in = new BufferedInputStream(istream);
|
220
|
|
221
|
// read the input and write into the string buffer
|
222
|
int inputByte;
|
223
|
while ( (inputByte = in.read()) != -1 ) {
|
224
|
cbuff.append((char)inputByte);
|
225
|
}
|
226
|
|
227
|
// the input stream must be closed
|
228
|
in.close();
|
229
|
|
230
|
} catch (MalformedURLException e) {
|
231
|
throw new MalformedURLException
|
232
|
("DBUtil.readDTD(). " + e.getMessage());
|
233
|
} catch (IOException e) {
|
234
|
throw new IOException
|
235
|
("DBUtil.readDTD(). " + e.getMessage());
|
236
|
} catch (SecurityException e) {
|
237
|
throw new IOException
|
238
|
("DBUtil.readDTD(). " + e.getMessage());
|
239
|
}
|
240
|
|
241
|
return cbuff.toString();
|
242
|
}
|
243
|
|
244
|
/**
|
245
|
* format the DataGuide ResultSet to XML
|
246
|
*/
|
247
|
private String formatToXML(Vector resultset) {
|
248
|
|
249
|
String currPath = null;
|
250
|
String currElement = null;
|
251
|
String prevElement = null;
|
252
|
StringBuffer result = new StringBuffer();
|
253
|
Enumeration rs = resultset.elements();
|
254
|
Stack st = new Stack();
|
255
|
int i = 0;
|
256
|
|
257
|
result.append("<?xml version=\"1.0\"?>\n");
|
258
|
result.append("<resultset>\n");
|
259
|
|
260
|
while (rs.hasMoreElements()) {
|
261
|
currPath = (String)rs.nextElement();
|
262
|
while ( !In(prevElement, currPath) ) {
|
263
|
currElement = (String)st.pop();
|
264
|
result.append(pad(" ",i--) + "</" + currElement + ">\n");
|
265
|
if ( st.empty() )
|
266
|
prevElement = null;
|
267
|
else
|
268
|
prevElement = (String)st.peek();
|
269
|
}
|
270
|
currElement = getElementFromPath(currPath);
|
271
|
st.push(currElement);
|
272
|
result.append(pad(" ",++i) + "<" + currElement + ">\n");
|
273
|
prevElement = currElement;
|
274
|
}
|
275
|
while ( !st.empty() ) {
|
276
|
prevElement = (String)st.pop();
|
277
|
result.append(pad(" ",i--) + "</" + prevElement + ">\n");
|
278
|
}
|
279
|
result.append("</resultset>\n");
|
280
|
|
281
|
return result.toString();
|
282
|
}
|
283
|
|
284
|
/**
|
285
|
* check if element is in path like /elem1/elem2/elemn3
|
286
|
*/
|
287
|
private boolean In(String element, String path) {
|
288
|
|
289
|
if ( element == null ) return true;
|
290
|
return ( path.indexOf(element) != -1 );
|
291
|
}
|
292
|
|
293
|
/**
|
294
|
* get last element from path like /elem1/elem2/elemn3
|
295
|
*/
|
296
|
private String getElementFromPath(String path) {
|
297
|
|
298
|
return ( path.substring(path.lastIndexOf("/")+1) );
|
299
|
}
|
300
|
|
301
|
/**
|
302
|
* repeates the str n-times
|
303
|
*/
|
304
|
private String pad(String str, int n) {
|
305
|
|
306
|
String result = "";
|
307
|
for ( int i = 0; i < n; i++ )
|
308
|
result = result.concat(str);
|
309
|
|
310
|
return result;
|
311
|
}
|
312
|
|
313
|
/**
|
314
|
* format the ResultSet to XML
|
315
|
*/
|
316
|
private String formatToXML(Vector resultset, String tag) {
|
317
|
|
318
|
String val = null;
|
319
|
StringBuffer result = new StringBuffer();
|
320
|
Enumeration rs = resultset.elements();
|
321
|
|
322
|
result.append("<?xml version=\"1.0\"?>\n");
|
323
|
result.append("<resultset>\n");
|
324
|
while (rs.hasMoreElements()) {
|
325
|
val = (String)rs.nextElement();
|
326
|
result.append(" <" + tag + ">" + val + "</" + tag + ">\n");
|
327
|
}
|
328
|
result.append("</resultset>\n");
|
329
|
|
330
|
return result.toString();
|
331
|
}
|
332
|
|
333
|
/**
|
334
|
* get the lastest Accession Number from a particular scope
|
335
|
*/
|
336
|
public String getMaxDocid(String scope)
|
337
|
throws SQLException {
|
338
|
|
339
|
String accnum = null;
|
340
|
String sep = MetaCatUtil.getOption("accNumSeparator");
|
341
|
PreparedStatement pstmt = null;
|
342
|
DBConnection dbConn = null;
|
343
|
int serialNumber = -1;
|
344
|
try {
|
345
|
dbConn=DBConnectionPool.
|
346
|
getDBConnection("DBUtil.getMaxDocid");
|
347
|
serialNumber=dbConn.getCheckOutSerialNumber();
|
348
|
pstmt =
|
349
|
dbConn.prepareStatement(
|
350
|
"SELECT docid, max(rev) FROM " +
|
351
|
"( " +
|
352
|
"SELECT docid, rev " +
|
353
|
"FROM xml_documents " +
|
354
|
"WHERE docid LIKE ? " +
|
355
|
"UNION " +
|
356
|
"SELECT docid, rev " +
|
357
|
"FROM xml_revisions " +
|
358
|
"WHERE docid LIKE ?" +
|
359
|
") subquery GROUP BY docid"
|
360
|
);
|
361
|
|
362
|
pstmt.setString(1,scope + sep + "%");
|
363
|
pstmt.setString(2,scope + sep + "%");
|
364
|
pstmt.execute();
|
365
|
ResultSet rs = pstmt.getResultSet();
|
366
|
|
367
|
long max = 0;
|
368
|
String temp = null;
|
369
|
|
370
|
while(rs.next()){
|
371
|
temp = rs.getString(1);
|
372
|
if(temp != null){
|
373
|
temp = temp.substring(temp.indexOf(scope) + scope.length() + 1);
|
374
|
try {
|
375
|
long localid = Long.parseLong(temp);
|
376
|
if (localid > max) {
|
377
|
max = localid;
|
378
|
accnum = rs.getString(1) + sep + rs.getString(2);
|
379
|
}
|
380
|
} catch (NumberFormatException nfe){
|
381
|
// ignore the exception as it is possible that the
|
382
|
// localid in the identifier is not an integer
|
383
|
}
|
384
|
}
|
385
|
}
|
386
|
|
387
|
pstmt.close();
|
388
|
|
389
|
} catch (SQLException e) {
|
390
|
throw new SQLException("DBUtil.getMaxDocid(). " + e.getMessage());
|
391
|
}
|
392
|
finally
|
393
|
{
|
394
|
try
|
395
|
{
|
396
|
pstmt.close();
|
397
|
}//try
|
398
|
finally
|
399
|
{
|
400
|
DBConnectionPool.returnDBConnection(dbConn, serialNumber);
|
401
|
}//finally
|
402
|
}//finally
|
403
|
|
404
|
return accnum;
|
405
|
}
|
406
|
|
407
|
/**
|
408
|
* return true if the given docid is registered in either the xml_documents
|
409
|
* or xml_revisions table
|
410
|
*/
|
411
|
public boolean idExists(String docid)
|
412
|
throws SQLException
|
413
|
{
|
414
|
Vector v = getAllDocids(null);
|
415
|
for(int i=0; i<v.size(); i++)
|
416
|
{
|
417
|
String id = (String)v.elementAt(i);
|
418
|
if(id.trim().equals(docid.trim()))
|
419
|
{
|
420
|
return true;
|
421
|
}
|
422
|
}
|
423
|
return false;
|
424
|
}
|
425
|
|
426
|
/**
|
427
|
* get the lastest Accession Number from a particular scope
|
428
|
*/
|
429
|
public Vector getAllDocids(String scope)
|
430
|
throws SQLException {
|
431
|
Vector resultVector = new Vector();
|
432
|
String accnum = null;
|
433
|
String sep = MetaCatUtil.getOption("accNumSeparator");
|
434
|
PreparedStatement pstmt = null;
|
435
|
DBConnection dbConn = null;
|
436
|
int serialNumber = -1;
|
437
|
try
|
438
|
{
|
439
|
dbConn=DBConnectionPool.
|
440
|
getDBConnection("DBUtil.getAllDocids");
|
441
|
serialNumber=dbConn.getCheckOutSerialNumber();
|
442
|
StringBuffer sb = new StringBuffer();
|
443
|
|
444
|
sb.append("SELECT docid, rev FROM " +
|
445
|
"( " +
|
446
|
"SELECT docid, rev " +
|
447
|
"FROM xml_documents ");
|
448
|
if(scope != null)
|
449
|
{
|
450
|
sb.append("WHERE docid LIKE ? ");
|
451
|
}
|
452
|
sb.append("UNION " +
|
453
|
"SELECT docid, rev " +
|
454
|
"FROM xml_revisions ");
|
455
|
if(scope != null)
|
456
|
{
|
457
|
sb.append("WHERE docid LIKE ?");
|
458
|
}
|
459
|
sb.append(") subquery GROUP BY docid, rev");
|
460
|
pstmt = dbConn.prepareStatement(sb.toString());
|
461
|
|
462
|
if(scope != null)
|
463
|
{
|
464
|
pstmt.setString(1,scope + sep + "%");
|
465
|
pstmt.setString(2,scope + sep + "%");
|
466
|
}
|
467
|
pstmt.execute();
|
468
|
ResultSet rs = pstmt.getResultSet();
|
469
|
|
470
|
long max = 0;
|
471
|
String id = null;
|
472
|
String rev = null;
|
473
|
while(rs.next()){
|
474
|
id = rs.getString(1);
|
475
|
rev = rs.getString(2);
|
476
|
if(id != null){
|
477
|
//temp = temp.substring(id.indexOf(scope) + scope.length() + 1);
|
478
|
resultVector.addElement(id + sep + rev);
|
479
|
}
|
480
|
}
|
481
|
|
482
|
pstmt.close();
|
483
|
|
484
|
} catch (SQLException e) {
|
485
|
throw new SQLException("DBUtil.getAllDocids(). " + e.getMessage());
|
486
|
}
|
487
|
finally
|
488
|
{
|
489
|
try
|
490
|
{
|
491
|
pstmt.close();
|
492
|
}//try
|
493
|
finally
|
494
|
{
|
495
|
DBConnectionPool.returnDBConnection(dbConn, serialNumber);
|
496
|
}//finally
|
497
|
}//finally
|
498
|
|
499
|
return resultVector;
|
500
|
}
|
501
|
|
502
|
/**
|
503
|
* To a given docid, found a dataset docid which conatains the the given doicd
|
504
|
* This will be done by searching xml_relation table
|
505
|
* If couldn't find, null will be return
|
506
|
* @param givenDocId, the docid which we want to find
|
507
|
*/
|
508
|
public static String findDataSetDocIdForGivenDocument(String givenDocId)
|
509
|
{
|
510
|
// Prepared statement for sql
|
511
|
PreparedStatement pStmt = null;
|
512
|
// Result set
|
513
|
ResultSet resultSet = null;
|
514
|
// String to store the data set docid
|
515
|
String dataSetDocId = null;
|
516
|
// DBConnection will be checkout
|
517
|
DBConnection dbConn = null;
|
518
|
int serialNumber = -1;
|
519
|
// String to store the sql command
|
520
|
String sqlCommand = null;
|
521
|
try
|
522
|
{
|
523
|
// Checkout DBConnection from pool
|
524
|
dbConn=DBConnectionPool.
|
525
|
getDBConnection("DBUtil.findDataSetDocIdForGivenDocument");
|
526
|
serialNumber=dbConn.getCheckOutSerialNumber();
|
527
|
// Sql command to chose a docid from xm_relation table
|
528
|
sqlCommand = "select docid from xml_relation where object like ? or "
|
529
|
+ "subject like ?";
|
530
|
// Prepared statement
|
531
|
pStmt = dbConn.prepareStatement(sqlCommand);
|
532
|
// Bind variable
|
533
|
pStmt.setString(1, givenDocId);
|
534
|
pStmt.setString(2, givenDocId);
|
535
|
// Excute prepared statement
|
536
|
pStmt.execute();
|
537
|
// Get result set
|
538
|
resultSet = pStmt.getResultSet();
|
539
|
|
540
|
// There has record
|
541
|
if (resultSet.next())
|
542
|
{
|
543
|
// Put the docid into dataSetDocid
|
544
|
dataSetDocId = resultSet.getString(1);
|
545
|
return dataSetDocId;
|
546
|
}//if
|
547
|
else
|
548
|
{
|
549
|
// No record in xml_relation table for given doicd, null returned
|
550
|
return dataSetDocId;
|
551
|
}//else
|
552
|
|
553
|
}//try
|
554
|
catch ( SQLException e)
|
555
|
{
|
556
|
// Print out excepition
|
557
|
logMetacat.error("Error in DBUil.findDataSEtDocIdForGivenDocument"
|
558
|
+e.getMessage());
|
559
|
// return null
|
560
|
return dataSetDocId;
|
561
|
|
562
|
}//catch
|
563
|
finally
|
564
|
{
|
565
|
try
|
566
|
{
|
567
|
// Close result set
|
568
|
resultSet.close();
|
569
|
// Close preparedStatement
|
570
|
pStmt.close();
|
571
|
}//try
|
572
|
catch ( SQLException e)
|
573
|
{
|
574
|
// Print out excepition
|
575
|
logMetacat.error("Error in DBUil.findDataSetDocIdForGivenDoc"
|
576
|
+ e.getMessage());
|
577
|
|
578
|
}//catch
|
579
|
finally
|
580
|
{
|
581
|
// Return DBConnection to the pool
|
582
|
DBConnectionPool.returnDBConnection(dbConn, serialNumber);
|
583
|
}//finally
|
584
|
}//finally
|
585
|
|
586
|
}//findDataSetDocIdForGivenDocument
|
587
|
|
588
|
/**
|
589
|
* Method to get current revision and doctype for a given docid
|
590
|
* The output will look like "rev;doctype"
|
591
|
* @param givenDocId, the docid which we want
|
592
|
*/
|
593
|
public String getCurrentRevisionAndDocTypeForGivenDocument(String givenDocId)
|
594
|
throws SQLException
|
595
|
{
|
596
|
// DBConection for JDBC
|
597
|
DBConnection dbConn = null;
|
598
|
int serialNumber = -1;
|
599
|
// Prepared Statement
|
600
|
PreparedStatement pstmt = null;
|
601
|
// String to store a docid without rev
|
602
|
String docIdWithoutRevision = null;
|
603
|
// SQL comand
|
604
|
String sqlCommand = null;
|
605
|
// Resulst set
|
606
|
ResultSet rs = null;
|
607
|
// String to store the revision
|
608
|
String revision = null;
|
609
|
// String to store the doctype
|
610
|
String docType = null;
|
611
|
|
612
|
// Get docid without rev
|
613
|
docIdWithoutRevision = MetaCatUtil.getDocIdFromString(givenDocId);
|
614
|
// SQL comand is:
|
615
|
sqlCommand = "select rev, doctype from xml_documents where docid like ?";
|
616
|
|
617
|
try
|
618
|
{
|
619
|
// Check out the connection
|
620
|
dbConn=DBConnectionPool.
|
621
|
getDBConnection("DBUtil.getCurrentRevisionAndDocTypeForGivenDocument");
|
622
|
serialNumber=dbConn.getCheckOutSerialNumber();
|
623
|
|
624
|
// Prepare the sql command
|
625
|
pstmt = dbConn.prepareStatement(sqlCommand);
|
626
|
// Bin vairable
|
627
|
pstmt.setString(1, docIdWithoutRevision);
|
628
|
// Excute the prepared statement
|
629
|
pstmt.execute();
|
630
|
// Get result set
|
631
|
rs = pstmt.getResultSet();
|
632
|
// If there is some record
|
633
|
if (rs.next())
|
634
|
{
|
635
|
revision = rs.getString(1);
|
636
|
docType = rs.getString(2);
|
637
|
}//if
|
638
|
else
|
639
|
{
|
640
|
// No record, throw a exception
|
641
|
throw new
|
642
|
SQLException("There is not record for given docid:"+givenDocId);
|
643
|
}//else
|
644
|
|
645
|
}
|
646
|
finally
|
647
|
{
|
648
|
try
|
649
|
{
|
650
|
// Close result set
|
651
|
rs.close();
|
652
|
// Close preparedStatement
|
653
|
pstmt.close();
|
654
|
}//try
|
655
|
catch ( SQLException e)
|
656
|
{
|
657
|
// Print out excepition
|
658
|
logMetacat.error("Error in DBUil.getCurrentRevisionAndDocType"
|
659
|
+ e.getMessage());
|
660
|
|
661
|
}//catch
|
662
|
finally
|
663
|
{
|
664
|
// Return DBConnection to the pool
|
665
|
DBConnectionPool.returnDBConnection(dbConn, serialNumber);
|
666
|
}//finally
|
667
|
}
|
668
|
return revision+";"+docType;
|
669
|
}//getCurrentRevisionAndDocTypeForGivenDocument
|
670
|
|
671
|
/**
|
672
|
* Method to return a rev list in xml_revision for given docid.
|
673
|
* @param docId
|
674
|
* @return is a vector which contains Integer object
|
675
|
* @throws SQLException
|
676
|
*/
|
677
|
public static Vector getRevListFromRevisionTable(String docIdWithoutRev) throws SQLException
|
678
|
{
|
679
|
Vector list = new Vector();
|
680
|
int rev = 1;
|
681
|
PreparedStatement pStmt = null;
|
682
|
DBConnection dbConn = null;
|
683
|
int serialNumber = -1;
|
684
|
// get rid of rev
|
685
|
//docId = MetaCatUtil.getDocIdFromString(docId);
|
686
|
try {
|
687
|
//check out DBConnection
|
688
|
dbConn = DBConnectionPool
|
689
|
.getDBConnection("getRevListFromRevisionTable");
|
690
|
serialNumber = dbConn.getCheckOutSerialNumber();
|
691
|
|
692
|
pStmt = dbConn
|
693
|
.prepareStatement("SELECT rev FROM xml_revisions WHERE docid='"
|
694
|
+ docIdWithoutRev + "'");
|
695
|
pStmt.execute();
|
696
|
|
697
|
ResultSet rs = pStmt.getResultSet();
|
698
|
boolean hasRow = rs.next();
|
699
|
while (hasRow) {
|
700
|
rev = rs.getInt(1);
|
701
|
logMetacat.warn("rev "+ rev +" is added to list");
|
702
|
list.add(new Integer(rev));
|
703
|
hasRow = rs.next();
|
704
|
|
705
|
}
|
706
|
pStmt.close();
|
707
|
}//try
|
708
|
finally {
|
709
|
try {
|
710
|
pStmt.close();
|
711
|
} catch (Exception ee) {
|
712
|
logMetacat.error("Error in DocumentImpl."
|
713
|
+ "getLatestRevisionNumber: " + ee.getMessage());
|
714
|
} finally {
|
715
|
DBConnectionPool.returnDBConnection(dbConn, serialNumber);
|
716
|
}
|
717
|
}//finally
|
718
|
|
719
|
return list;
|
720
|
}//getLatestRevisionNumber
|
721
|
|
722
|
/**
|
723
|
* Get last revision number from database for a docid If couldn't find an
|
724
|
* entry, -1 will return The return value is integer because we want compare
|
725
|
* it to there new one
|
726
|
*
|
727
|
* @param docid
|
728
|
* <sitecode>. <uniqueid>part of Accession Number
|
729
|
*/
|
730
|
public static int getLatestRevisionInDocumentTable(String docIdWithoutRev) throws SQLException
|
731
|
{
|
732
|
int rev = 1;
|
733
|
PreparedStatement pStmt = null;
|
734
|
DBConnection dbConn = null;
|
735
|
int serialNumber = -1;
|
736
|
try {
|
737
|
//check out DBConnection
|
738
|
dbConn = DBConnectionPool
|
739
|
.getDBConnection("DBUtil.getLatestRevisionInDocumentTable");
|
740
|
serialNumber = dbConn.getCheckOutSerialNumber();
|
741
|
|
742
|
pStmt = dbConn
|
743
|
.prepareStatement("SELECT rev FROM xml_documents WHERE docid='"
|
744
|
+ docIdWithoutRev + "'");
|
745
|
pStmt.execute();
|
746
|
|
747
|
ResultSet rs = pStmt.getResultSet();
|
748
|
boolean hasRow = rs.next();
|
749
|
if (hasRow) {
|
750
|
rev = rs.getInt(1);
|
751
|
pStmt.close();
|
752
|
} else {
|
753
|
rev = -1;
|
754
|
pStmt.close();
|
755
|
}
|
756
|
}//try
|
757
|
finally {
|
758
|
try {
|
759
|
pStmt.close();
|
760
|
} catch (Exception ee) {
|
761
|
logMetacat.error("Error in DBUtil."
|
762
|
+ "getLatestRevisionInDocumentTable: " + ee.getMessage());
|
763
|
} finally {
|
764
|
DBConnectionPool.returnDBConnection(dbConn, serialNumber);
|
765
|
}
|
766
|
}//finally
|
767
|
|
768
|
return rev;
|
769
|
}//getLatestRevisionNumber
|
770
|
|
771
|
}
|