1
|
/**
|
2
|
* Name: AccessionNumber.java
|
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
|
8
|
*
|
9
|
* Version: '$Id: AccessionNumber.java 165 2000-06-16 01:53:55Z jones $'
|
10
|
*/
|
11
|
|
12
|
package edu.ucsb.nceas.metacat;
|
13
|
|
14
|
import java.net.*;
|
15
|
import java.sql.*;
|
16
|
import java.util.PropertyResourceBundle;
|
17
|
|
18
|
/**
|
19
|
* A class that gets Accession Number, check for uniqueness and register it
|
20
|
* into db connection
|
21
|
*/
|
22
|
public class AccessionNumber {
|
23
|
|
24
|
public static String generate (String accNumber, String action)
|
25
|
throws ClassNotFoundException, StringIndexOutOfBoundsException, SQLException
|
26
|
{
|
27
|
|
28
|
String globalName = null;
|
29
|
String localId = null;
|
30
|
PropertyResourceBundle options = null;
|
31
|
|
32
|
options = (PropertyResourceBundle)
|
33
|
PropertyResourceBundle.getBundle("edu.ucsb.nceas.metacat.metacat");
|
34
|
String dbDriver = (String)options.handleGetObject("dbDriver");
|
35
|
String defaultDB = (String)options.handleGetObject("defaultDB");
|
36
|
String user = (String)options.handleGetObject("user");
|
37
|
String password = (String)options.handleGetObject("password");
|
38
|
String defaultGlobalName = (String)options.handleGetObject("defaultGlobalName");
|
39
|
String sep = (String)options.handleGetObject("accNumberSeparator");
|
40
|
|
41
|
|
42
|
try {
|
43
|
// Open a new connection to the database
|
44
|
Connection conn = MetaCatUtil.openDBConnection(dbDriver, defaultDB, user, password);
|
45
|
conn.setAutoCommit(true);
|
46
|
|
47
|
// split the acc # in 2 parts - global name & local id
|
48
|
if ( accNumber != null ) {
|
49
|
globalName = getGlobalName(accNumber, sep);
|
50
|
localId = getLocalId(accNumber, sep);
|
51
|
}
|
52
|
|
53
|
// register unique acc #
|
54
|
if ( action == "INSERT" )
|
55
|
if ( accNumber == null )
|
56
|
return put(conn, defaultGlobalName, null, sep);
|
57
|
else
|
58
|
return put(conn, globalName, localId, sep);
|
59
|
|
60
|
} catch (SQLException e) {
|
61
|
System.out.println("Error on AccessionNumber.genAccessionNumber(): " + e.getMessage());
|
62
|
throw e;
|
63
|
}
|
64
|
|
65
|
return null;
|
66
|
}
|
67
|
|
68
|
private static String put (Connection conn, String globalName, String localId, String sep)
|
69
|
throws SQLException
|
70
|
{
|
71
|
|
72
|
Integer l = null;
|
73
|
try {
|
74
|
if ( localId == null )
|
75
|
l = new Integer(get(conn, globalName) + 1);
|
76
|
else if ( !unique(conn, globalName, localId) )
|
77
|
l = new Integer(get(conn, globalName) + 1);
|
78
|
else
|
79
|
l = new Integer(localId);
|
80
|
|
81
|
// insert globalName & l
|
82
|
PreparedStatement pstmt;
|
83
|
pstmt = conn.prepareStatement("INSERT INTO xml_acc_numbers (global_name, local_id) " +
|
84
|
"VALUES (?, ?)");
|
85
|
pstmt.setString(1,globalName);
|
86
|
pstmt.setString(2,l.toString());
|
87
|
pstmt.execute();
|
88
|
|
89
|
} catch (SQLException e) {
|
90
|
System.out.println("Error on AccessionNumber.put(conn, globalName, localId): " + e.getMessage());
|
91
|
throw e;
|
92
|
}
|
93
|
return globalName + sep + l;
|
94
|
}
|
95
|
|
96
|
/** check for existance of Accesssion Number */
|
97
|
private static boolean unique (Connection conn, String globalName, String localId)
|
98
|
throws SQLException
|
99
|
{
|
100
|
|
101
|
boolean hasAccNumber = false;
|
102
|
|
103
|
try {
|
104
|
PreparedStatement pstmt;
|
105
|
pstmt = conn.prepareStatement("SELECT 'x' FROM xml_acc_numbers " +
|
106
|
"WHERE global_name LIKE ? AND local_id = ?");
|
107
|
pstmt.setString(1,globalName);
|
108
|
pstmt.setString(2,localId);
|
109
|
pstmt.execute();
|
110
|
ResultSet rs = pstmt.getResultSet();
|
111
|
hasAccNumber = rs.next();
|
112
|
pstmt.close();
|
113
|
|
114
|
} catch (SQLException e) {
|
115
|
System.out.println("Error on AccessionNumber.unique(globalName, localId): " + e.getMessage());
|
116
|
throw e;
|
117
|
}
|
118
|
|
119
|
return hasAccNumber;
|
120
|
}
|
121
|
|
122
|
//** get the last in order local ID by a given global name */
|
123
|
private static int get (Connection conn, String globalName)
|
124
|
throws SQLException
|
125
|
{
|
126
|
try {
|
127
|
PreparedStatement pstmt;
|
128
|
pstmt = conn.prepareStatement("SELECT max(local_id) FROM xml_acc_numbers " +
|
129
|
"WHERE global_name LIKE ?");
|
130
|
pstmt.setString(1,globalName);
|
131
|
pstmt.execute();
|
132
|
ResultSet rs = pstmt.getResultSet();
|
133
|
boolean hasLocalId = rs.next();
|
134
|
|
135
|
if (hasLocalId)
|
136
|
return rs.getInt(1);
|
137
|
|
138
|
pstmt.close();
|
139
|
} catch (SQLException e) {
|
140
|
System.out.println("Error on AccessionNumber.get(): " + e.getMessage());
|
141
|
throw e;
|
142
|
}
|
143
|
|
144
|
return 0;
|
145
|
}
|
146
|
|
147
|
|
148
|
// get the global part of the accession number
|
149
|
private static String getGlobalName (String accNumber, String sep)
|
150
|
throws StringIndexOutOfBoundsException {
|
151
|
|
152
|
return accNumber.substring(0, accNumber.lastIndexOf(sep));
|
153
|
}
|
154
|
|
155
|
// get the local part of the accession number
|
156
|
private static String getLocalId (String accNumber, String sep)
|
157
|
throws StringIndexOutOfBoundsException {
|
158
|
|
159
|
return accNumber.substring(accNumber.lastIndexOf(sep)+1);
|
160
|
}
|
161
|
|
162
|
}
|