1
|
/**
|
2
|
* '$RCSfile$'
|
3
|
* Purpose: A class that gets Accession Number, check for uniqueness
|
4
|
* and register it into db
|
5
|
* Copyright: 2000 Regents of the University of California and the
|
6
|
* National Center for Ecological Analysis and Synthesis
|
7
|
* Authors: Jivka Bojilova, Matt Jones
|
8
|
*
|
9
|
* '$Author: tao $'
|
10
|
* '$Date: 2016-01-15 11:57:02 -0800 (Fri, 15 Jan 2016) $'
|
11
|
* '$Revision: 9491 $'
|
12
|
*
|
13
|
* This program is free software; you can redistribute it and/or modify
|
14
|
* it under the terms of the GNU General Public License as published by
|
15
|
* the Free Software Foundation; either version 2 of the License, or
|
16
|
* (at your option) any later version.
|
17
|
*
|
18
|
* This program is distributed in the hope that it will be useful,
|
19
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
20
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
21
|
* GNU General Public License for more details.
|
22
|
*
|
23
|
* You should have received a copy of the GNU General Public License
|
24
|
* along with this program; if not, write to the Free Software
|
25
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
26
|
*/
|
27
|
|
28
|
package edu.ucsb.nceas.metacat;
|
29
|
|
30
|
import java.sql.PreparedStatement;
|
31
|
import java.sql.ResultSet;
|
32
|
import java.sql.SQLException;
|
33
|
|
34
|
import edu.ucsb.nceas.metacat.database.DBConnection;
|
35
|
import edu.ucsb.nceas.metacat.database.DBConnectionPool;
|
36
|
import edu.ucsb.nceas.metacat.properties.PropertyService;
|
37
|
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
|
38
|
|
39
|
/**
|
40
|
* (on insert of XML document)
|
41
|
* Generates a unique Accession Number or if provided check it
|
42
|
* for uniqueness and register it into the db connection
|
43
|
* (on update or delete of XML document)
|
44
|
* Check for existance of provided Accession Number
|
45
|
*
|
46
|
*/
|
47
|
public class AccessionNumber {
|
48
|
|
49
|
private String sitecode = null;
|
50
|
private String sep = null;
|
51
|
private String docid = null;
|
52
|
private String rev = null;
|
53
|
|
54
|
/**
|
55
|
* Construct an AccessionNumber
|
56
|
*/
|
57
|
private AccessionNumber() throws AccessionNumberException {
|
58
|
try {
|
59
|
this.sitecode = PropertyService.getProperty("document.sitecode");
|
60
|
this.sep = PropertyService.getProperty("document.accNumSeparator");
|
61
|
} catch (PropertyNotFoundException pnfe) {
|
62
|
throw new AccessionNumberException("Could not retrieve property "
|
63
|
+ "in constructor: " + pnfe.getMessage());
|
64
|
}
|
65
|
}
|
66
|
|
67
|
/**
|
68
|
* NEW - WHEN CLIENT ALWAYS PROVIDE ACCESSION NUMBER INCLUDING REV IN IT
|
69
|
* Construct an AccessionNumber
|
70
|
*
|
71
|
* @param conn
|
72
|
* the db connection to read Accession number from
|
73
|
* @param accnum
|
74
|
* the accession number to be checked for validness
|
75
|
*/
|
76
|
public AccessionNumber (String accnum, String action)
|
77
|
throws AccessionNumberException, SQLException, NumberFormatException
|
78
|
{
|
79
|
this();
|
80
|
|
81
|
this.rev = null;
|
82
|
this.docid = accnum;
|
83
|
if ( accnum != null ) {
|
84
|
int firstIndex = accnum.indexOf(this.sep);
|
85
|
int lastIndex = accnum.lastIndexOf(this.sep);
|
86
|
if ( firstIndex != lastIndex ) {
|
87
|
//this docid contains a revision number
|
88
|
this.rev = accnum.substring(lastIndex + 1);
|
89
|
this.docid = accnum.substring(0, lastIndex);
|
90
|
}
|
91
|
}
|
92
|
|
93
|
// INSERT
|
94
|
if ( action.equals("INSERT")) {
|
95
|
|
96
|
if(rev != null){
|
97
|
try {
|
98
|
Integer.parseInt(rev);
|
99
|
}
|
100
|
catch (java.lang.NumberFormatException e) {
|
101
|
throw new AccessionNumberException(
|
102
|
"Revision number is required");
|
103
|
}
|
104
|
}
|
105
|
|
106
|
// check accession number for validness
|
107
|
if ( docid == null ) {
|
108
|
throw new AccessionNumberException("Accession number is required");
|
109
|
|
110
|
// rev is not provided; throw an exception to prevent the insertion
|
111
|
} else if ( rev == null ) {
|
112
|
throw new AccessionNumberException
|
113
|
("Revision number is required");
|
114
|
|
115
|
// docid is used; throw an exception to prevent the insertion
|
116
|
} else if ( accNumberUsed(docid) ) {
|
117
|
throw new AccessionNumberException
|
118
|
("Accession number " + docid + " is already in use");
|
119
|
|
120
|
// rev is <> 1; throw an exception to prevent the insertion
|
121
|
} /*else if ( !rev.equals("1")) {
|
122
|
throw new AccessionNumberException("Revision number must be 1");
|
123
|
}*/
|
124
|
|
125
|
// UPDATE or DELETE
|
126
|
} else if ( action.equals("UPDATE") || action.equals("DELETE")) {
|
127
|
String l_rev = "";
|
128
|
|
129
|
int reversionNumber = 1;
|
130
|
|
131
|
if(rev != null){
|
132
|
try{
|
133
|
reversionNumber = Integer.parseInt(rev);
|
134
|
} catch (java.lang.NumberFormatException e){
|
135
|
throw new AccessionNumberException(
|
136
|
"Revision number is required");
|
137
|
}
|
138
|
}
|
139
|
|
140
|
// Accession# is not provided; throw an exception to prevent the action
|
141
|
if ( docid == null ) {
|
142
|
throw new AccessionNumberException("Accession number is required");
|
143
|
|
144
|
// rev is not provided; throw an exception to prevent the action
|
145
|
} else if ( rev == null ) {
|
146
|
throw new AccessionNumberException
|
147
|
("Revision number is required");
|
148
|
|
149
|
// Accession# is not current (not in xml_documents or xml_revisions); throw an exception
|
150
|
} else if ( !accNumberIsCurrent(docid) ) {
|
151
|
throw new AccessionNumberException
|
152
|
("Document not found for Accession number " + docid);
|
153
|
|
154
|
//Revision number is less than or equal the recent one; throw a exception
|
155
|
} else if ( action.equals("UPDATE") &&
|
156
|
reversionNumber <= getLastRevisionNumber(docid) ) {
|
157
|
throw new AccessionNumberException
|
158
|
("Next revision number can't be less than or equal to "
|
159
|
+ getLastRevisionNumber(docid));
|
160
|
|
161
|
// Revision number is not the recent one; throw an exception
|
162
|
} else if ( action.equals("DELETE") &&
|
163
|
!rev.equals(l_rev = getLastRevision(docid)) ) {
|
164
|
throw new AccessionNumberException
|
165
|
("Last revision number is "+ l_rev);
|
166
|
}
|
167
|
}
|
168
|
}
|
169
|
|
170
|
/** check for existence of Accesssion Number xml_acc_numbers table */
|
171
|
public static boolean accNumberUsed ( String accNumber )
|
172
|
throws SQLException {
|
173
|
|
174
|
boolean hasAccNumber = false;
|
175
|
DBConnection conn = null;
|
176
|
int serialNumber = -1;
|
177
|
PreparedStatement pstmt = null;
|
178
|
ResultSet rs = null;
|
179
|
|
180
|
try {
|
181
|
|
182
|
//check out DBConnection
|
183
|
conn=DBConnectionPool.getDBConnection("AccessionNumber.accNumberUsed");
|
184
|
serialNumber=conn.getCheckOutSerialNumber();
|
185
|
pstmt = conn.prepareStatement(
|
186
|
"SELECT 'x' FROM xml_documents " +
|
187
|
"WHERE docid = ? " +
|
188
|
"UNION " +
|
189
|
"SELECT 'x' FROM xml_revisions " +
|
190
|
"WHERE docid = ?");
|
191
|
pstmt.setString(1,accNumber);
|
192
|
pstmt.setString(2,accNumber);
|
193
|
pstmt.execute();
|
194
|
rs = pstmt.getResultSet();
|
195
|
hasAccNumber = rs.next();
|
196
|
//pstmt.close();
|
197
|
|
198
|
} catch (SQLException e) {
|
199
|
throw new SQLException
|
200
|
("Error on AccessionNumber.accNumberUsed(accNumber): " + e.getMessage());
|
201
|
}
|
202
|
finally
|
203
|
{
|
204
|
try {
|
205
|
if(rs != null) {
|
206
|
rs.close();
|
207
|
}
|
208
|
if(pstmt != null) {
|
209
|
pstmt.close();
|
210
|
}
|
211
|
} finally {
|
212
|
DBConnectionPool.returnDBConnection(conn, serialNumber);
|
213
|
}
|
214
|
|
215
|
}
|
216
|
|
217
|
return hasAccNumber;
|
218
|
}
|
219
|
|
220
|
// Check for existence of Accesssion Number in xml_documents or xml_revisions
|
221
|
// table. We check xml_revisions because document may have been deleted, which
|
222
|
// will remove it from xml_documents, but a revision still exists.
|
223
|
private boolean accNumberIsCurrent(String accNumber) throws SQLException {
|
224
|
|
225
|
boolean hasCurrentAccNumber = false;
|
226
|
DBConnection conn = null;
|
227
|
int serialNumber = -1;
|
228
|
ResultSet rs = null;
|
229
|
PreparedStatement pstmt = null;
|
230
|
try {
|
231
|
|
232
|
//check out DBConnection
|
233
|
conn=DBConnectionPool.getDBConnection("AccessionNumber.accNumberIsCurre");
|
234
|
serialNumber=conn.getCheckOutSerialNumber();
|
235
|
pstmt = conn.prepareStatement(
|
236
|
"SELECT 'x' FROM xml_documents " +
|
237
|
"WHERE docid = ?");
|
238
|
pstmt.setString(1, accNumber);
|
239
|
pstmt.execute();
|
240
|
rs = pstmt.getResultSet();
|
241
|
hasCurrentAccNumber = rs.next();
|
242
|
if(!hasCurrentAccNumber)
|
243
|
{
|
244
|
//need to look xml_revision table;
|
245
|
pstmt = conn.prepareStatement(
|
246
|
"SELECT 'x' FROM xml_revisions " +
|
247
|
"WHERE docid = ?");
|
248
|
pstmt.setString(1, accNumber);
|
249
|
pstmt.execute();
|
250
|
rs = pstmt.getResultSet();
|
251
|
hasCurrentAccNumber = rs.next();
|
252
|
}
|
253
|
//pstmt.close();
|
254
|
|
255
|
} catch (SQLException e) {
|
256
|
throw new SQLException(
|
257
|
"Error on AccessionNumber.accNumberIsCurrent(String accNumber): " +
|
258
|
e.getMessage());
|
259
|
}
|
260
|
finally
|
261
|
{
|
262
|
|
263
|
try {
|
264
|
if(rs != null) {
|
265
|
rs.close();
|
266
|
}
|
267
|
if(pstmt != null) {
|
268
|
pstmt.close();
|
269
|
}
|
270
|
} finally {
|
271
|
DBConnectionPool.returnDBConnection(conn, serialNumber);
|
272
|
}
|
273
|
|
274
|
}
|
275
|
|
276
|
return hasCurrentAccNumber;
|
277
|
}
|
278
|
|
279
|
// get the recent revision number for docid
|
280
|
private String getLastRevision(String docid) throws SQLException
|
281
|
{
|
282
|
String rev = "";
|
283
|
DBConnection conn = null;
|
284
|
int serialNumber = -1;
|
285
|
PreparedStatement pstmt = null;
|
286
|
ResultSet rs = null;
|
287
|
try {
|
288
|
|
289
|
//check out DBConnection
|
290
|
conn=DBConnectionPool.getDBConnection("AccessionNumber.getLastRevision");
|
291
|
serialNumber=conn.getCheckOutSerialNumber();
|
292
|
pstmt = conn.prepareStatement
|
293
|
("SELECT rev FROM xml_documents WHERE docid = ?");
|
294
|
pstmt.setString(1, docid);
|
295
|
pstmt.execute();
|
296
|
|
297
|
rs = pstmt.getResultSet();
|
298
|
boolean hasRow = rs.next();
|
299
|
rev = rs.getString(1);
|
300
|
//pstmt.close();
|
301
|
|
302
|
} catch (SQLException e) {
|
303
|
throw new SQLException(
|
304
|
"Error on AccessionNumber.getLastRevision(): " + e.getMessage());
|
305
|
}
|
306
|
finally
|
307
|
{
|
308
|
try {
|
309
|
if(rs != null) {
|
310
|
rs.close();
|
311
|
}
|
312
|
if(pstmt != null) {
|
313
|
pstmt.close();
|
314
|
}
|
315
|
} finally {
|
316
|
DBConnectionPool.returnDBConnection(conn,serialNumber);
|
317
|
}
|
318
|
|
319
|
}
|
320
|
|
321
|
return rev;
|
322
|
}
|
323
|
|
324
|
/**
|
325
|
* Get last revision number from database for a docid
|
326
|
* The return value is integer because we want compare it to there new one
|
327
|
* @param docid <sitecode>.<uniqueid> part of Accession Number
|
328
|
*/
|
329
|
private int getLastRevisionNumber(String docId) throws SQLException
|
330
|
{
|
331
|
int rev = 1;
|
332
|
DBConnection conn =null;
|
333
|
int serialNumber = -1;
|
334
|
PreparedStatement pStmt = null;
|
335
|
ResultSet rs = null;
|
336
|
try {
|
337
|
|
338
|
//check out DBConnection
|
339
|
conn=DBConnectionPool.getDBConnection("AccessionNumber.getLastRevisionN");
|
340
|
serialNumber=conn.getCheckOutSerialNumber();
|
341
|
|
342
|
pStmt = conn.prepareStatement
|
343
|
("SELECT rev FROM xml_documents WHERE docid = ? ");
|
344
|
pStmt.setString(1, docId);
|
345
|
pStmt.execute();
|
346
|
|
347
|
rs = pStmt.getResultSet();
|
348
|
boolean hasRow = rs.next();
|
349
|
if(hasRow)
|
350
|
{
|
351
|
rev = rs.getInt(1);
|
352
|
}
|
353
|
//pStmt.close();
|
354
|
|
355
|
} catch (SQLException e) {
|
356
|
throw new SQLException(
|
357
|
"Error on AccessionNumber.getLastRevision(): " + e.getMessage());
|
358
|
}
|
359
|
finally
|
360
|
{
|
361
|
try {
|
362
|
if(rs != null) {
|
363
|
rs.close();
|
364
|
}
|
365
|
if(pStmt != null) {
|
366
|
pStmt.close();
|
367
|
}
|
368
|
} finally {
|
369
|
DBConnectionPool.returnDBConnection(conn,serialNumber);
|
370
|
}
|
371
|
|
372
|
}
|
373
|
return rev;
|
374
|
}
|
375
|
|
376
|
/**
|
377
|
* returns the docid encoded in this accession number
|
378
|
*/
|
379
|
public String getDocid() {
|
380
|
|
381
|
return this.docid;
|
382
|
}
|
383
|
|
384
|
/**
|
385
|
* returns the revision number encoded in this accession number
|
386
|
*/
|
387
|
public String getRev()
|
388
|
{
|
389
|
return this.rev;
|
390
|
}
|
391
|
|
392
|
}
|