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
|
* Release: @release@
|
9
|
*
|
10
|
* '$Author: bojilova $'
|
11
|
* '$Date: 2000-08-30 11:19:41 -0700 (Wed, 30 Aug 2000) $'
|
12
|
* '$Revision: 421 $'
|
13
|
*/
|
14
|
|
15
|
package edu.ucsb.nceas.metacat;
|
16
|
|
17
|
import java.net.*;
|
18
|
import java.sql.*;
|
19
|
|
20
|
/**
|
21
|
* A class that generates an Accession Number and will check a submitted
|
22
|
* accession number for uniqueness and register it into the db connection
|
23
|
*/
|
24
|
public class AccessionNumber {
|
25
|
|
26
|
/**
|
27
|
* Get an accession number from the user, check it for uniqueness
|
28
|
* and register it into new db connection. If no accession number is
|
29
|
* provided by the user, generate one from the database and return it.
|
30
|
*
|
31
|
* @param accNumber - accession # if provided or null if not
|
32
|
* @param action - INSERT, UPDATE or DELETE.
|
33
|
* When "INSERT" and accession # provided is not unique, get next one.
|
34
|
* If it is unique, use it.
|
35
|
* When "INSERT" and accession # is null, get a new one.
|
36
|
* When "UPDATE", accession # is required.
|
37
|
* When "DELETE", accession # is required.
|
38
|
*/
|
39
|
public String generate (String accNumber, String action)
|
40
|
throws AccessionNumberException, SQLException, ClassNotFoundException
|
41
|
{
|
42
|
|
43
|
String globalName = null;
|
44
|
String localId = null;
|
45
|
|
46
|
// Open a connection to the database
|
47
|
MetaCatUtil util = new MetaCatUtil();
|
48
|
|
49
|
String defaultGlobalName = util.getOption("defaultGlobalName");
|
50
|
String sep = util.getOption("accNumberSeparator");
|
51
|
|
52
|
try {
|
53
|
// Open a new connection to the database
|
54
|
Connection conn = util.openDBConnection();
|
55
|
conn.setAutoCommit(true);
|
56
|
|
57
|
// split the acc # in 2 parts - global name & local id
|
58
|
if ( accNumber != null ) {
|
59
|
globalName = getGlobalName(accNumber, sep);
|
60
|
localId = getLocalId(accNumber, sep);
|
61
|
}
|
62
|
|
63
|
// register unique acc #
|
64
|
if ( action.equals("INSERT")) {
|
65
|
if ( accNumber == null )
|
66
|
return put(conn, defaultGlobalName, null, sep);
|
67
|
else
|
68
|
return put(conn, globalName, localId, sep);
|
69
|
} else if ( action.equals("UPDATE") || action.equals("DELETE")) {
|
70
|
if ( accNumber == null ) {
|
71
|
throw (new AccessionNumberException("Accession number is " +
|
72
|
"required."));
|
73
|
} else if (!accNumberIsCurrent(conn, accNumber)) {
|
74
|
throw (new AccessionNumberException("Document " +
|
75
|
"not found for accession #: " + accNumber));
|
76
|
} else {
|
77
|
return (globalName + sep + localId);
|
78
|
}
|
79
|
}
|
80
|
|
81
|
conn.close();
|
82
|
|
83
|
} catch (StringIndexOutOfBoundsException siobe) {
|
84
|
MetaCatUtil.debugMessage(
|
85
|
"Error on AccessionNumber.generate(): " +
|
86
|
siobe.getMessage());
|
87
|
throw (new AccessionNumberException("Accession number invalid, " +
|
88
|
"expecting character \'" + sep + "'."));
|
89
|
} catch (SQLException e) {
|
90
|
System.err.println(
|
91
|
"Error on AccessionNumber.genAccessionNumber(): " +
|
92
|
e.getMessage());
|
93
|
throw e;
|
94
|
}
|
95
|
|
96
|
throw (new AccessionNumberException("Fatal Error in " +
|
97
|
"accession number generation: "));
|
98
|
}
|
99
|
|
100
|
/** put unique accession # into db connection */
|
101
|
private String put (Connection conn, String globalName,
|
102
|
String localId, String sep)
|
103
|
throws SQLException
|
104
|
{
|
105
|
|
106
|
Integer l = null;
|
107
|
try {
|
108
|
if ( localId == null )
|
109
|
l = new Integer(get(conn, globalName) + 1);
|
110
|
else if ( accNumberUsed(conn, globalName, localId) )
|
111
|
l = new Integer(get(conn, globalName) + 1);
|
112
|
else
|
113
|
l = new Integer(localId);
|
114
|
|
115
|
// insert globalName & l
|
116
|
PreparedStatement pstmt;
|
117
|
pstmt = conn.prepareStatement(
|
118
|
"INSERT INTO xml_acc_numbers (global_name, local_id) " +
|
119
|
"VALUES (?, ?)");
|
120
|
pstmt.setString(1,globalName);
|
121
|
pstmt.setString(2,l.toString());
|
122
|
pstmt.execute();
|
123
|
|
124
|
pstmt.close();
|
125
|
|
126
|
} catch (SQLException e) {
|
127
|
System.err.println(
|
128
|
"Error on AccessionNumber.put(conn, globalName, localId): "
|
129
|
+ e.getMessage());
|
130
|
throw e;
|
131
|
}
|
132
|
return globalName + sep + l;
|
133
|
}
|
134
|
|
135
|
/** check for existence of Accesssion Number xml_acc_numbers table */
|
136
|
private boolean accNumberUsed(Connection conn, String globalName,
|
137
|
String localId) throws SQLException {
|
138
|
|
139
|
boolean hasAccNumber = false;
|
140
|
|
141
|
try {
|
142
|
PreparedStatement pstmt;
|
143
|
pstmt = conn.prepareStatement(
|
144
|
"SELECT 'x' FROM xml_acc_numbers " +
|
145
|
"WHERE global_name LIKE ? AND local_id = ?");
|
146
|
pstmt.setString(1,globalName);
|
147
|
pstmt.setString(2,localId);
|
148
|
pstmt.execute();
|
149
|
ResultSet rs = pstmt.getResultSet();
|
150
|
hasAccNumber = rs.next();
|
151
|
pstmt.close();
|
152
|
|
153
|
} catch (SQLException e) {
|
154
|
System.err.println("Error on AccessionNumber.unique(globalName, " +
|
155
|
"localId): " + e.getMessage());
|
156
|
throw e;
|
157
|
}
|
158
|
|
159
|
return hasAccNumber;
|
160
|
}
|
161
|
|
162
|
/** check for existence of Accesssion Number in xml_documents table */
|
163
|
private boolean accNumberIsCurrent(
|
164
|
Connection conn, String accNumber) throws SQLException {
|
165
|
|
166
|
boolean hasCurrentAccNumber = false;
|
167
|
|
168
|
try {
|
169
|
PreparedStatement pstmt;
|
170
|
pstmt = conn.prepareStatement(
|
171
|
"SELECT 'x' FROM xml_documents " +
|
172
|
"WHERE docid LIKE ?");
|
173
|
pstmt.setString(1, accNumber);
|
174
|
pstmt.execute();
|
175
|
ResultSet rs = pstmt.getResultSet();
|
176
|
hasCurrentAccNumber = rs.next();
|
177
|
pstmt.close();
|
178
|
|
179
|
} catch (SQLException e) {
|
180
|
System.err.println(
|
181
|
"Error on AccessionNumber.accNumberIsCurrent(globalName, " +
|
182
|
"localId): " + e.getMessage());
|
183
|
throw e;
|
184
|
}
|
185
|
return hasCurrentAccNumber;
|
186
|
}
|
187
|
|
188
|
/** get the last in order local ID by a given global name */
|
189
|
private int get (Connection conn, String globalName)
|
190
|
throws SQLException
|
191
|
{
|
192
|
try {
|
193
|
PreparedStatement pstmt;
|
194
|
pstmt = conn.prepareStatement(
|
195
|
"SELECT max(local_id) FROM xml_acc_numbers " +
|
196
|
"WHERE global_name LIKE ?");
|
197
|
pstmt.setString(1,globalName);
|
198
|
pstmt.execute();
|
199
|
ResultSet rs = pstmt.getResultSet();
|
200
|
boolean hasLocalId = rs.next();
|
201
|
|
202
|
if (hasLocalId)
|
203
|
return rs.getInt(1);
|
204
|
|
205
|
pstmt.close();
|
206
|
} catch (SQLException e) {
|
207
|
System.err.println(
|
208
|
"Error on AccessionNumber.get(): " + e.getMessage());
|
209
|
throw e;
|
210
|
}
|
211
|
|
212
|
return 0;
|
213
|
}
|
214
|
|
215
|
|
216
|
// get the global part of the accession number
|
217
|
private String getGlobalName (String accNumber, String sep)
|
218
|
throws StringIndexOutOfBoundsException {
|
219
|
|
220
|
return accNumber.substring(0, accNumber.lastIndexOf(sep));
|
221
|
}
|
222
|
|
223
|
// get the local part of the accession number
|
224
|
private String getLocalId (String accNumber, String sep)
|
225
|
throws StringIndexOutOfBoundsException {
|
226
|
|
227
|
return accNumber.substring(accNumber.lastIndexOf(sep)+1);
|
228
|
}
|
229
|
}
|
230
|
|
231
|
/**
|
232
|
* '$Log$
|
233
|
* 'Revision 1.11 2000/08/14 20:53:33 jones
|
234
|
* 'Added "release" keyword to all metacat source files so that the release
|
235
|
* 'number will be evident in software distributions.
|
236
|
* '
|
237
|
* 'Revision 1.10 2000/06/27 04:31:07 jones
|
238
|
* 'Fixed bugs associated with the new UPDATE and DELETE functions of
|
239
|
* 'DBWriter. There were problematic interactions between some static
|
240
|
* 'variables used in DBEntityResolver and the way in which the
|
241
|
* 'Servlet objects are re-used across multiple client invocations.
|
242
|
* '
|
243
|
* 'Generally cleaned up error reporting. Now all errors and success
|
244
|
* 'results are reported as XML documents from MetaCatServlet. Need
|
245
|
* 'to make the command line tools do the same.
|
246
|
* '
|
247
|
* 'Revision 1.9 2000/06/26 10:35:04 jones
|
248
|
* 'Merged in substantial changes to DBWriter and associated classes and to
|
249
|
* 'the MetaCatServlet in order to accomodate the new UPDATE and DELETE
|
250
|
* 'functions. The command line tools and the parameters for the
|
251
|
* 'servlet have changed substantially.
|
252
|
* '
|
253
|
* 'Revision 1.8.2.5 2000/06/26 08:38:01 jones
|
254
|
* 'Added DELETE feature to DBWriter. Now takes an action "DELETE" and a
|
255
|
* 'docid and will move the record from the xml_documents table to the
|
256
|
* 'xml_revisions table.
|
257
|
* 'Modified option parsing to support option symbols on command line.
|
258
|
* '
|
259
|
* 'Revision 1.8.2.4 2000/06/25 23:11:40 jones
|
260
|
* 'Documentation update
|
261
|
* '
|
262
|
* 'Revision 1.8.2.3 2000/06/25 23:08:31 jones
|
263
|
* 'Minor change to excpetion handling
|
264
|
* ''
|
265
|
*/
|