35 |
35 |
|
36 |
36 |
/**
|
37 |
37 |
* (on insert of XML document)
|
38 |
|
* Generates a unique Accession Number or if provided check it
|
39 |
|
* for uniqueness and register it into the db connection
|
|
38 |
* Generates a unique Accession Number or if provided check it
|
|
39 |
* for uniqueness and register it into the db connection
|
40 |
40 |
* (on update or delete of XML document)
|
41 |
41 |
* Check for existance of provided Accession Number
|
42 |
|
*
|
|
42 |
*
|
43 |
43 |
*/
|
44 |
44 |
public class AccessionNumber {
|
45 |
|
|
|
45 |
|
46 |
46 |
private static final AbstractDatabase dbAdapter = MetaCatUtil.dbAdapter;
|
47 |
47 |
|
48 |
48 |
//private Connection conn = null;
|
... | ... | |
50 |
50 |
private String sep = null;
|
51 |
51 |
private String docid = null;
|
52 |
52 |
private String rev = null;
|
53 |
|
|
54 |
|
/**
|
55 |
|
* Construct an AccessionNumber
|
|
53 |
|
|
54 |
/**
|
|
55 |
* Construct an AccessionNumber
|
56 |
56 |
*/
|
57 |
|
//public AccessionNumber ()
|
58 |
|
//throws SQLException, ClassNotFoundException
|
|
57 |
//public AccessionNumber ()
|
|
58 |
//throws SQLException, ClassNotFoundException
|
59 |
59 |
//{
|
60 |
60 |
// Open a connection to the database from here
|
61 |
61 |
//this((new MetaCatUtil()).openDBConnection());
|
62 |
|
//}
|
|
62 |
//}
|
63 |
63 |
|
64 |
|
/**
|
|
64 |
/**
|
65 |
65 |
* Construct an AccessionNumber
|
66 |
66 |
*
|
67 |
67 |
* @param conn the db connection to read from and write Accession# to
|
68 |
68 |
*/
|
69 |
|
public AccessionNumber ()
|
|
69 |
public AccessionNumber ()
|
70 |
70 |
{
|
71 |
71 |
this.sitecode = MetaCatUtil.getOption("sitecode");
|
72 |
72 |
this.sep = MetaCatUtil.getOption("accNumSeparator");
|
... | ... | |
79 |
79 |
* @param conn the db connection to read Accession number from
|
80 |
80 |
* @param accnum the accession number to be checked for validness
|
81 |
81 |
*/
|
82 |
|
public AccessionNumber (String accnum, String action)
|
|
82 |
public AccessionNumber (String accnum, String action)
|
83 |
83 |
throws AccessionNumberException, SQLException, NumberFormatException
|
84 |
84 |
{
|
85 |
85 |
this();
|
... | ... | |
95 |
95 |
this.docid = accnum.substring(0, lastIndex);
|
96 |
96 |
}
|
97 |
97 |
}
|
98 |
|
|
|
98 |
|
99 |
99 |
// INSERT
|
100 |
100 |
if ( action.equals("INSERT")) {
|
101 |
101 |
|
... | ... | |
112 |
112 |
} else if ( accNumberUsed(docid) ) {
|
113 |
113 |
throw new AccessionNumberException
|
114 |
114 |
("Accession number " + docid + " is already in use.");
|
115 |
|
|
|
115 |
|
116 |
116 |
// rev is <> 1; throw an exception to prevent the insertion
|
117 |
117 |
} /*else if ( !rev.equals("1")) {
|
118 |
118 |
throw new AccessionNumberException("Revision number must be 1.");
|
119 |
119 |
}*/
|
120 |
|
|
|
120 |
|
121 |
121 |
// UPDATE or DELETE
|
122 |
122 |
} else if ( action.equals("UPDATE") || action.equals("DELETE")) {
|
123 |
123 |
String l_rev = "";
|
124 |
|
int reversionNumber=Integer.parseInt(rev);
|
125 |
124 |
|
|
125 |
int reversionNumber = 1;
|
|
126 |
|
|
127 |
if(rev != null){
|
|
128 |
reversionNumber = Integer.parseInt(rev);
|
|
129 |
}
|
|
130 |
|
126 |
131 |
// Accession# is not provided; throw an exception to prevent the action
|
127 |
132 |
if ( docid == null ) {
|
128 |
133 |
throw new AccessionNumberException("Accession number is required.");
|
... | ... | |
145 |
150 |
+ getLastRevisionNumber(docid));
|
146 |
151 |
|
147 |
152 |
// Revision number is not the recent one; throw an exception
|
148 |
|
} else if ( action.equals("DELETE") &&
|
|
153 |
} else if ( action.equals("DELETE") &&
|
149 |
154 |
!rev.equals(l_rev = getLastRevision(docid)) ) {
|
150 |
155 |
throw new AccessionNumberException
|
151 |
156 |
("Last revision number is "+ l_rev);
|
... | ... | |
160 |
165 |
* @param docid <sitecode>.<uniqueid> part of Accession Number
|
161 |
166 |
* @param action INSERT, UPDATE or DELETE action
|
162 |
167 |
*/
|
163 |
|
public String generate(String docid, String action)
|
164 |
|
throws AccessionNumberException, SQLException
|
|
168 |
public String generate(String docid, String action)
|
|
169 |
throws AccessionNumberException, SQLException
|
165 |
170 |
{
|
166 |
171 |
return generate(docid, "1", action);
|
167 |
172 |
}
|
... | ... | |
173 |
178 |
* @param rev <revisionid> of Accession Number
|
174 |
179 |
* @param action INSERT, UPDATE or DELETE action
|
175 |
180 |
*/
|
176 |
|
public String generate(String docid, String rev, String action)
|
177 |
|
throws AccessionNumberException, SQLException
|
|
181 |
public String generate(String docid, String rev, String action)
|
|
182 |
throws AccessionNumberException, SQLException
|
178 |
183 |
{
|
179 |
184 |
try {
|
180 |
185 |
// INSERT
|
... | ... | |
222 |
227 |
return docid;
|
223 |
228 |
}
|
224 |
229 |
}
|
225 |
|
|
|
230 |
|
226 |
231 |
} catch (SQLException e) {
|
227 |
232 |
throw new SQLException
|
228 |
233 |
("Error on AccessionNumber.generate(): " + e.getMessage());
|
229 |
|
}
|
230 |
|
|
|
234 |
}
|
|
235 |
|
231 |
236 |
// never comes here
|
232 |
237 |
throw new
|
233 |
238 |
AccessionNumberException("Fatal Error in accession number generation.");
|
234 |
239 |
}
|
235 |
|
|
|
240 |
|
236 |
241 |
// get local sitecode
|
237 |
242 |
private String getSitecode()
|
238 |
243 |
{
|
... | ... | |
248 |
253 |
int serialNumber = -1;
|
249 |
254 |
try {
|
250 |
255 |
PreparedStatement pstmt = null;
|
251 |
|
|
|
256 |
|
252 |
257 |
//check out DBConnection
|
253 |
258 |
conn=DBConnectionPool.getDBConnection("AccessionNumber.getUniqueID");
|
254 |
259 |
serialNumber=conn.getCheckOutSerialNumber();
|
... | ... | |
258 |
263 |
pstmt.setString(1, sitecode);
|
259 |
264 |
pstmt.execute();
|
260 |
265 |
pstmt.close();
|
261 |
|
|
|
266 |
|
262 |
267 |
uniqueid = "" + dbAdapter.getUniqueID(conn.getConnections(),
|
263 |
268 |
"accession_number");
|
264 |
269 |
//because we only count the connection usage one when it check in
|
... | ... | |
266 |
271 |
conn.increaseUsageCount(1);
|
267 |
272 |
|
268 |
273 |
} catch (SQLException e) {
|
269 |
|
throw new
|
|
274 |
throw new
|
270 |
275 |
SQLException("Error on AccessionNumber.getUniqueID(): "+e.getMessage());
|
271 |
276 |
}
|
272 |
277 |
finally
|
... | ... | |
279 |
284 |
/** check for existence of Accesssion Number xml_acc_numbers table */
|
280 |
285 |
public static boolean accNumberUsed ( String accNumber )
|
281 |
286 |
throws SQLException {
|
282 |
|
|
|
287 |
|
283 |
288 |
boolean hasAccNumber = false;
|
284 |
289 |
DBConnection conn = null;
|
285 |
290 |
int serialNumber = -1;
|
286 |
|
|
|
291 |
|
287 |
292 |
try {
|
288 |
293 |
PreparedStatement pstmt = null;
|
289 |
294 |
//check out DBConnection
|
290 |
295 |
conn=DBConnectionPool.getDBConnection("AccessionNumber.accNumberUsed");
|
291 |
296 |
serialNumber=conn.getCheckOutSerialNumber();
|
292 |
297 |
pstmt = conn.prepareStatement(
|
293 |
|
"SELECT 'x' FROM xml_documents " +
|
|
298 |
"SELECT 'x' FROM xml_documents " +
|
294 |
299 |
"WHERE docid = ? " +
|
295 |
300 |
"UNION " +
|
296 |
301 |
"SELECT 'x' FROM xml_revisions " +
|
... | ... | |
301 |
306 |
ResultSet rs = pstmt.getResultSet();
|
302 |
307 |
hasAccNumber = rs.next();
|
303 |
308 |
pstmt.close();
|
304 |
|
|
|
309 |
|
305 |
310 |
} catch (SQLException e) {
|
306 |
311 |
throw new SQLException
|
307 |
312 |
("Error on AccessionNumber.accNumberUsed(accNumber): " + e.getMessage());
|
... | ... | |
310 |
315 |
{
|
311 |
316 |
DBConnectionPool.returnDBConnection(conn, serialNumber);
|
312 |
317 |
}
|
313 |
|
|
|
318 |
|
314 |
319 |
return hasAccNumber;
|
315 |
|
}
|
316 |
|
|
|
320 |
}
|
|
321 |
|
317 |
322 |
// check for existence of Accesssion Number in xml_documents table
|
318 |
323 |
private boolean accNumberIsCurrent(String accNumber) throws SQLException {
|
319 |
|
|
|
324 |
|
320 |
325 |
boolean hasCurrentAccNumber = false;
|
321 |
326 |
DBConnection conn = null;
|
322 |
327 |
int serialNumber = -1;
|
323 |
|
|
|
328 |
|
324 |
329 |
try {
|
325 |
330 |
PreparedStatement pstmt = null;
|
326 |
331 |
//check out DBConnection
|
327 |
332 |
conn=DBConnectionPool.getDBConnection("AccessionNumber.accNumberIsCurre");
|
328 |
333 |
serialNumber=conn.getCheckOutSerialNumber();
|
329 |
334 |
pstmt = conn.prepareStatement(
|
330 |
|
"SELECT 'x' FROM xml_documents " +
|
|
335 |
"SELECT 'x' FROM xml_documents " +
|
331 |
336 |
"WHERE docid = ?");
|
332 |
337 |
pstmt.setString(1, accNumber);
|
333 |
338 |
pstmt.execute();
|
334 |
339 |
ResultSet rs = pstmt.getResultSet();
|
335 |
340 |
hasCurrentAccNumber = rs.next();
|
336 |
341 |
pstmt.close();
|
337 |
|
|
|
342 |
|
338 |
343 |
} catch (SQLException e) {
|
339 |
344 |
throw new SQLException(
|
340 |
345 |
"Error on AccessionNumber.accNumberIsCurrent(String accNumber): " +
|
... | ... | |
344 |
349 |
{
|
345 |
350 |
DBConnectionPool.returnDBConnection(conn, serialNumber);
|
346 |
351 |
}
|
347 |
|
|
|
352 |
|
348 |
353 |
return hasCurrentAccNumber;
|
349 |
|
}
|
|
354 |
}
|
350 |
355 |
|
351 |
356 |
// get the recent revision number for docid
|
352 |
357 |
private String getLastRevision(String docid) throws SQLException
|
... | ... | |
354 |
359 |
String rev = "";
|
355 |
360 |
DBConnection conn = null;
|
356 |
361 |
int serialNumber = -1;
|
357 |
|
|
|
362 |
|
358 |
363 |
try {
|
359 |
364 |
PreparedStatement pstmt = null;
|
360 |
365 |
//check out DBConnection
|
... | ... | |
368 |
373 |
boolean hasRow = rs.next();
|
369 |
374 |
rev = rs.getString(1);
|
370 |
375 |
pstmt.close();
|
371 |
|
|
|
376 |
|
372 |
377 |
} catch (SQLException e) {
|
373 |
378 |
throw new SQLException(
|
374 |
379 |
"Error on AccessionNumber.getLastRevision(): " + e.getMessage());
|
... | ... | |
380 |
385 |
|
381 |
386 |
return rev;
|
382 |
387 |
}
|
383 |
|
|
|
388 |
|
384 |
389 |
/**
|
385 |
390 |
* Get last revision number from database for a docid
|
386 |
391 |
* The return value is integer because we want compare it to there new one
|
... | ... | |
391 |
396 |
int rev = 1;
|
392 |
397 |
DBConnection conn =null;
|
393 |
398 |
int serialNumber = -1;
|
394 |
|
|
|
399 |
|
395 |
400 |
try {
|
396 |
401 |
PreparedStatement pStmt = null;
|
397 |
402 |
//check out DBConnection
|
398 |
403 |
conn=DBConnectionPool.getDBConnection("AccessionNumber.getLastRevisionN");
|
399 |
404 |
serialNumber=conn.getCheckOutSerialNumber();
|
400 |
|
|
|
405 |
|
401 |
406 |
pStmt = conn.prepareStatement
|
402 |
407 |
("SELECT rev FROM xml_documents WHERE docid='" + docId + "'");
|
403 |
408 |
pStmt.execute();
|
... | ... | |
406 |
411 |
boolean hasRow = rs.next();
|
407 |
412 |
rev = rs.getInt(1);
|
408 |
413 |
pStmt.close();
|
409 |
|
|
|
414 |
|
410 |
415 |
} catch (SQLException e) {
|
411 |
416 |
throw new SQLException(
|
412 |
417 |
"Error on AccessionNumber.getLastRevision(): " + e.getMessage());
|
... | ... | |
417 |
422 |
}
|
418 |
423 |
return rev;
|
419 |
424 |
}
|
420 |
|
|
|
425 |
|
421 |
426 |
// get the next revision number for docid
|
422 |
427 |
private String getNextRevision(String docid) throws SQLException
|
423 |
428 |
{
|
... | ... | |
437 |
442 |
boolean hasRow = rs.next();
|
438 |
443 |
rev = rs.getString(1);
|
439 |
444 |
pstmt.close();
|
440 |
|
|
|
445 |
|
441 |
446 |
} catch (SQLException e) {
|
442 |
447 |
throw new SQLException(
|
443 |
448 |
"Error on AccessionNumber.getNextRevision(): " + e.getMessage());
|
... | ... | |
449 |
454 |
|
450 |
455 |
return rev;
|
451 |
456 |
}
|
452 |
|
|
|
457 |
|
453 |
458 |
/**
|
454 |
459 |
* returns the docid encoded in this accession number
|
455 |
460 |
*/
|
... | ... | |
465 |
470 |
{
|
466 |
471 |
return this.rev;
|
467 |
472 |
}
|
468 |
|
|
|
473 |
|
469 |
474 |
}
|
Added eml-2.0.1 tags for eml processing.
Also fixed a bug. The error returned in case of no revision number specified was just null. Now it says that revision number is required.