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