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