Project

General

Profile

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 171 2000-06-16 22:39:50Z 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
    /**
25
     * This method gets Accession Number (if any), check for uniqueness 
26
     * and register it into new db connection
27
     * @param accNumber - accession # if provided or null if not
28
     * @param action - INSERT, UPDATE or DELETE.
29
     * When "INSERT" and accession # provided is not unique, get next one.
30
     * If it is unique, use it.
31
     * When "INSERT" and accession # is null, get a new one.
32
     * When "UPDATE", accession # is required. 
33
     * When "DELETE", accession # is required. 
34
     */
35
    public static String generate (String accNumber, String action) 
36
        throws ClassNotFoundException, StringIndexOutOfBoundsException, 
37
               SQLException
38
    {
39
        
40
        String globalName = null;
41
        String localId = null;
42
        PropertyResourceBundle options = null;
43
        
44
        options = (PropertyResourceBundle)
45
            PropertyResourceBundle.getBundle("edu.ucsb.nceas.metacat.metacat");
46
        String dbDriver = (String)options.handleGetObject("dbDriver");
47
        String defaultDB = (String)options.handleGetObject("defaultDB");
48
        String user = (String)options.handleGetObject("user");
49
        String password = (String)options.handleGetObject("password");
50
        String defaultGlobalName = 
51
               (String)options.handleGetObject("defaultGlobalName");
52
        String sep = (String)options.handleGetObject("accNumberSeparator");
53

    
54

    
55
        try {
56
            // Open a new connection to the database
57
            Connection conn = MetaCatUtil.openDBConnection(dbDriver, 
58
                              defaultDB, user, password);
59
            conn.setAutoCommit(true);
60

    
61
            // split the acc # in 2 parts - global name & local id
62
            if ( accNumber != null ) {
63
                globalName = getGlobalName(accNumber, sep);
64
                localId = getLocalId(accNumber, sep);
65
            }    
66

    
67
            // register unique acc #
68
            if ( action == "INSERT" )
69
                if ( accNumber == null )
70
                    return put(conn, defaultGlobalName, null, sep);
71
                else
72
                    return put(conn, globalName, localId, sep);
73
                    
74
            conn.close();        
75

    
76
        } catch (SQLException e) {
77
            System.out.println(
78
                       "Error on AccessionNumber.genAccessionNumber(): " + 
79
                       e.getMessage());
80
            throw e;
81
        }    
82
        
83
        return null;        
84
    }    
85

    
86
    /** put unique accession # into db connection */
87
    private static String put (Connection conn, String globalName, 
88
                String localId, String sep) 
89
                throws SQLException
90
    {
91
        
92
        Integer l = null;
93
        try {
94
            if ( localId == null ) 
95
                l = new Integer(get(conn, globalName) + 1); 
96
            else if ( exist(conn, globalName, localId) )
97
                l = new Integer(get(conn, globalName) + 1); 
98
            else
99
                l = new Integer(localId); 
100

    
101
            // insert globalName & l
102
            PreparedStatement pstmt;
103
            pstmt = conn.prepareStatement(
104
                    "INSERT INTO xml_acc_numbers (global_name, local_id) " + 
105
                    "VALUES (?, ?)");
106
            pstmt.setString(1,globalName);
107
            pstmt.setString(2,l.toString());
108
            pstmt.execute();
109
            
110
            pstmt.close();
111
            
112
        } catch (SQLException e) {
113
            System.out.println(
114
                   "Error on AccessionNumber.put(conn, globalName, localId): " 
115
                   + e.getMessage());
116
            throw e;
117
        }    
118
        return globalName + sep + l;
119
    }
120

    
121
    /** check for existance of Accesssion Number */
122
    private static boolean exist (Connection conn, String globalName, 
123
                String localId) throws SQLException
124
    {
125
        
126
        boolean hasAccNumber = false;
127
        
128
        try {
129
            PreparedStatement pstmt;
130
            pstmt = conn.prepareStatement(
131
                    "SELECT 'x' FROM xml_acc_numbers " + 
132
                    "WHERE global_name LIKE ? AND local_id = ?");
133
            pstmt.setString(1,globalName);
134
            pstmt.setString(2,localId);
135
            pstmt.execute();
136
            ResultSet rs = pstmt.getResultSet();
137
            hasAccNumber = rs.next();
138
            pstmt.close();
139
            
140
        } catch (SQLException e) {
141
            System.out.println("Error on AccessionNumber.unique(globalName, " +
142
                               "localId): " + e.getMessage());
143
            throw e;
144
        }    
145
        
146
        return hasAccNumber;
147
    }    
148
    
149
    /** get the last in order local ID by a given global name */
150
    private static int get (Connection conn, String globalName) 
151
                throws SQLException
152
    {
153
        try {
154
            PreparedStatement pstmt;
155
            pstmt = conn.prepareStatement(
156
                    "SELECT max(local_id) FROM xml_acc_numbers " + 
157
                    "WHERE global_name LIKE ?");
158
            pstmt.setString(1,globalName);
159
            pstmt.execute();
160
            ResultSet rs = pstmt.getResultSet();
161
            boolean hasLocalId = rs.next();
162

    
163
            if (hasLocalId)
164
                return rs.getInt(1);
165

    
166
            pstmt.close();
167
        } catch (SQLException e) {
168
            System.out.println(
169
                   "Error on AccessionNumber.get(): " + e.getMessage());
170
            throw e;
171
        }    
172
        
173
        return 0;
174
    }
175

    
176
    
177
    // get the global part of the accession number
178
    private static String getGlobalName (String accNumber, String sep) 
179
        throws StringIndexOutOfBoundsException {
180
        
181
        return accNumber.substring(0, accNumber.lastIndexOf(sep));
182
    }    
183

    
184
    // get the local part of the accession number
185
    private static String getLocalId (String accNumber, String sep)
186
        throws StringIndexOutOfBoundsException {
187

    
188
        return accNumber.substring(accNumber.lastIndexOf(sep)+1);
189
    }    
190

    
191
}
(1-1/20)