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: daigle $'
|
10
|
* '$Date: 2008-08-05 17:33:45 -0700 (Tue, 05 Aug 2008) $'
|
11
|
* '$Revision: 4212 $'
|
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.dbadapter.AbstractDatabase;
|
35
|
import edu.ucsb.nceas.metacat.service.PropertyService;
|
36
|
import edu.ucsb.nceas.metacat.util.MetaCatUtil;
|
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 static final AbstractDatabase dbAdapter = MetaCatUtil.dbAdapter;
|
50
|
|
51
|
private String sitecode = null;
|
52
|
private String sep = null;
|
53
|
private String docid = null;
|
54
|
private String rev = null;
|
55
|
|
56
|
/**
|
57
|
* Construct an AccessionNumber
|
58
|
*/
|
59
|
private AccessionNumber() throws AccessionNumberException {
|
60
|
try {
|
61
|
this.sitecode = PropertyService.getProperty("document.sitecode");
|
62
|
this.sep = PropertyService.getProperty("document.accNumSeparator");
|
63
|
} catch (PropertyNotFoundException pnfe) {
|
64
|
throw new AccessionNumberException("Could not retrieve property "
|
65
|
+ "in constructor: " + pnfe.getMessage());
|
66
|
}
|
67
|
}
|
68
|
|
69
|
/**
|
70
|
* NEW - WHEN CLIENT ALWAYS PROVIDE ACCESSION NUMBER INCLUDING REV IN IT
|
71
|
* Construct an AccessionNumber
|
72
|
*
|
73
|
* @param conn
|
74
|
* the db connection to read Accession number from
|
75
|
* @param accnum
|
76
|
* the accession number to be checked for validness
|
77
|
*/
|
78
|
public AccessionNumber (String accnum, String action)
|
79
|
throws AccessionNumberException, SQLException, NumberFormatException
|
80
|
{
|
81
|
this();
|
82
|
|
83
|
this.rev = null;
|
84
|
this.docid = accnum;
|
85
|
if ( accnum != null ) {
|
86
|
int firstIndex = accnum.indexOf(this.sep);
|
87
|
int lastIndex = accnum.lastIndexOf(this.sep);
|
88
|
if ( firstIndex != lastIndex ) {
|
89
|
//this docid contains a revision number
|
90
|
this.rev = accnum.substring(lastIndex + 1);
|
91
|
this.docid = accnum.substring(0, lastIndex);
|
92
|
}
|
93
|
}
|
94
|
|
95
|
// INSERT
|
96
|
if ( action.equals("INSERT")) {
|
97
|
|
98
|
if(rev != null){
|
99
|
try {
|
100
|
Integer.parseInt(rev);
|
101
|
}
|
102
|
catch (java.lang.NumberFormatException e) {
|
103
|
throw new AccessionNumberException(
|
104
|
"Revision number is required");
|
105
|
}
|
106
|
}
|
107
|
|
108
|
// check accession number for validness
|
109
|
if ( docid == null ) {
|
110
|
throw new AccessionNumberException("Accession number is required");
|
111
|
|
112
|
// rev is not provided; throw an exception to prevent the insertion
|
113
|
} else if ( rev == null ) {
|
114
|
throw new AccessionNumberException
|
115
|
("Revision number is required");
|
116
|
|
117
|
// docid is used; throw an exception to prevent the insertion
|
118
|
} else if ( accNumberUsed(docid) ) {
|
119
|
throw new AccessionNumberException
|
120
|
("Accession number " + docid + " is already in use");
|
121
|
|
122
|
// rev is <> 1; throw an exception to prevent the insertion
|
123
|
} /*else if ( !rev.equals("1")) {
|
124
|
throw new AccessionNumberException("Revision number must be 1");
|
125
|
}*/
|
126
|
|
127
|
// UPDATE or DELETE
|
128
|
} else if ( action.equals("UPDATE") || action.equals("DELETE")) {
|
129
|
String l_rev = "";
|
130
|
|
131
|
int reversionNumber = 1;
|
132
|
|
133
|
if(rev != null){
|
134
|
try{
|
135
|
reversionNumber = Integer.parseInt(rev);
|
136
|
} catch (java.lang.NumberFormatException e){
|
137
|
throw new AccessionNumberException(
|
138
|
"Revision number is required");
|
139
|
}
|
140
|
}
|
141
|
|
142
|
// Accession# is not provided; throw an exception to prevent the action
|
143
|
if ( docid == null ) {
|
144
|
throw new AccessionNumberException("Accession number is required");
|
145
|
|
146
|
// rev is not provided; throw an exception to prevent the action
|
147
|
} else if ( rev == null ) {
|
148
|
throw new AccessionNumberException
|
149
|
("Revision number is required");
|
150
|
|
151
|
// Accession# is not current (not in xml_documents); throw an exception
|
152
|
} else if ( !accNumberIsCurrent(docid) ) {
|
153
|
throw new AccessionNumberException
|
154
|
("Document not found for Accession number " + docid);
|
155
|
|
156
|
//Revision number is less than or equal the recent one; throw a exception
|
157
|
} else if ( action.equals("UPDATE") &&
|
158
|
reversionNumber <= getLastRevisionNumber(docid) ) {
|
159
|
throw new AccessionNumberException
|
160
|
("Next revision number couldn't be less than or equal "
|
161
|
+ getLastRevisionNumber(docid));
|
162
|
|
163
|
// Revision number is not the recent one; throw an exception
|
164
|
} else if ( action.equals("DELETE") &&
|
165
|
!rev.equals(l_rev = getLastRevision(docid)) ) {
|
166
|
throw new AccessionNumberException
|
167
|
("Last revision number is "+ l_rev);
|
168
|
}
|
169
|
}
|
170
|
}
|
171
|
|
172
|
/** check for existence of Accesssion Number xml_acc_numbers table */
|
173
|
public static boolean accNumberUsed ( String accNumber )
|
174
|
throws SQLException {
|
175
|
|
176
|
boolean hasAccNumber = false;
|
177
|
DBConnection conn = null;
|
178
|
int serialNumber = -1;
|
179
|
|
180
|
try {
|
181
|
PreparedStatement pstmt = null;
|
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
|
ResultSet 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
|
DBConnectionPool.returnDBConnection(conn, serialNumber);
|
205
|
}
|
206
|
|
207
|
return hasAccNumber;
|
208
|
}
|
209
|
|
210
|
// check for existence of Accesssion Number in xml_documents table
|
211
|
private boolean accNumberIsCurrent(String accNumber) throws SQLException {
|
212
|
|
213
|
boolean hasCurrentAccNumber = false;
|
214
|
DBConnection conn = null;
|
215
|
int serialNumber = -1;
|
216
|
|
217
|
try {
|
218
|
PreparedStatement pstmt = null;
|
219
|
//check out DBConnection
|
220
|
conn=DBConnectionPool.getDBConnection("AccessionNumber.accNumberIsCurre");
|
221
|
serialNumber=conn.getCheckOutSerialNumber();
|
222
|
pstmt = conn.prepareStatement(
|
223
|
"SELECT 'x' FROM xml_documents " +
|
224
|
"WHERE docid = ?");
|
225
|
pstmt.setString(1, accNumber);
|
226
|
pstmt.execute();
|
227
|
ResultSet rs = pstmt.getResultSet();
|
228
|
hasCurrentAccNumber = rs.next();
|
229
|
pstmt.close();
|
230
|
|
231
|
} catch (SQLException e) {
|
232
|
throw new SQLException(
|
233
|
"Error on AccessionNumber.accNumberIsCurrent(String accNumber): " +
|
234
|
e.getMessage());
|
235
|
}
|
236
|
finally
|
237
|
{
|
238
|
DBConnectionPool.returnDBConnection(conn, serialNumber);
|
239
|
}
|
240
|
|
241
|
return hasCurrentAccNumber;
|
242
|
}
|
243
|
|
244
|
// get the recent revision number for docid
|
245
|
private String getLastRevision(String docid) throws SQLException
|
246
|
{
|
247
|
String rev = "";
|
248
|
DBConnection conn = null;
|
249
|
int serialNumber = -1;
|
250
|
|
251
|
try {
|
252
|
PreparedStatement pstmt = null;
|
253
|
//check out DBConnection
|
254
|
conn=DBConnectionPool.getDBConnection("AccessionNumber.getLastRevision");
|
255
|
serialNumber=conn.getCheckOutSerialNumber();
|
256
|
pstmt = conn.prepareStatement
|
257
|
("SELECT rev FROM xml_documents WHERE docid='" + docid + "'");
|
258
|
pstmt.execute();
|
259
|
|
260
|
ResultSet rs = pstmt.getResultSet();
|
261
|
boolean hasRow = rs.next();
|
262
|
rev = rs.getString(1);
|
263
|
pstmt.close();
|
264
|
|
265
|
} catch (SQLException e) {
|
266
|
throw new SQLException(
|
267
|
"Error on AccessionNumber.getLastRevision(): " + e.getMessage());
|
268
|
}
|
269
|
finally
|
270
|
{
|
271
|
DBConnectionPool.returnDBConnection(conn,serialNumber);
|
272
|
}
|
273
|
|
274
|
return rev;
|
275
|
}
|
276
|
|
277
|
/**
|
278
|
* Get last revision number from database for a docid
|
279
|
* The return value is integer because we want compare it to there new one
|
280
|
* @param docid <sitecode>.<uniqueid> part of Accession Number
|
281
|
*/
|
282
|
private int getLastRevisionNumber(String docId) throws SQLException
|
283
|
{
|
284
|
int rev = 1;
|
285
|
DBConnection conn =null;
|
286
|
int serialNumber = -1;
|
287
|
|
288
|
try {
|
289
|
PreparedStatement pStmt = null;
|
290
|
//check out DBConnection
|
291
|
conn=DBConnectionPool.getDBConnection("AccessionNumber.getLastRevisionN");
|
292
|
serialNumber=conn.getCheckOutSerialNumber();
|
293
|
|
294
|
pStmt = conn.prepareStatement
|
295
|
("SELECT rev FROM xml_documents WHERE docid='" + docId + "'");
|
296
|
pStmt.execute();
|
297
|
|
298
|
ResultSet rs = pStmt.getResultSet();
|
299
|
boolean hasRow = rs.next();
|
300
|
rev = rs.getInt(1);
|
301
|
pStmt.close();
|
302
|
|
303
|
} catch (SQLException e) {
|
304
|
throw new SQLException(
|
305
|
"Error on AccessionNumber.getLastRevision(): " + e.getMessage());
|
306
|
}
|
307
|
finally
|
308
|
{
|
309
|
DBConnectionPool.returnDBConnection(conn,serialNumber);
|
310
|
}
|
311
|
return rev;
|
312
|
}
|
313
|
|
314
|
/**
|
315
|
* returns the docid encoded in this accession number
|
316
|
*/
|
317
|
public String getDocid() {
|
318
|
|
319
|
return this.docid;
|
320
|
}
|
321
|
|
322
|
/**
|
323
|
* returns the revision number encoded in this accession number
|
324
|
*/
|
325
|
public String getRev()
|
326
|
{
|
327
|
return this.rev;
|
328
|
}
|
329
|
|
330
|
}
|