Project

General

Profile

« Previous | Next » 

Revision 145

Added by bojilova about 24 years ago

new class for registering of Accession numbers into metacat db

View differences:

src/edu/ucsb/nceas/metacat/AccessionNumber.java
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$'
10
 */
11

  
12
package edu.ucsb.nceas.metacat;
13

  
14
import java.net.*;
15
import java.sql.*;
16

  
17
public class AccessionNumber  {
18
    
19
    String globalName;
20
    String localId;
21
    String accNumber;
22
    Connection conn;
23
    
24
    /** Split the accession number in 2 parts by ":" character */
25
    public AccessionNumber (Connection conn, String accNumber) {
26
        
27
        this.conn = conn;
28
        this.globalName = getGlobalName(accNumber);
29
        this.localId = getLocalId(accNumber);
30
        this.accNumber = put();
31
      
32
    }    
33
    
34
    /** check for existance of Accesssion Number */
35
    public boolean check (String globalName, String localId) {
36
        
37
        boolean hasAccNumber = false;
38
        
39
        try {
40
            PreparedStatement pstmt;
41
            pstmt = conn.prepareStatement("SELECT 'x' FROM xml_acc_numbers " + 
42
                                         "WHERE global_name LIKE ? AND local_id = ?");
43
            pstmt.setString(1,globalName);
44
            pstmt.setString(2,localId);
45
            pstmt.execute();
46
            ResultSet rs = pstmt.getResultSet();
47
            hasAccNumber = rs.next();
48
            pstmt.close();
49
            
50
        } catch (SQLException e) {
51
            System.out.println("Error on AccessionNumber.check(globalName, localId): " + e.getMessage());
52
        }    
53
        
54
        return hasAccNumber;
55
    }    
56
    
57
    //** get the last in order local ID by a given global name */
58
    public int get (String globalName) {
59
        
60
        try {
61
            PreparedStatement pstmt;
62
            pstmt = conn.prepareStatement("SELECT max(local_id) FROM xml_acc_numbers " + 
63
                                         "WHERE global_name LIKE ?");
64
            pstmt.setString(1,globalName);
65
            pstmt.execute();
66
            ResultSet rs = pstmt.getResultSet();
67
            boolean hasLocalId = rs.next();
68

  
69
            if (hasLocalId)
70
                return rs.getInt(1);
71

  
72
            pstmt.close();
73
        } catch (SQLException e) {
74
            System.out.println("Error on AccessionNumber.get(): " + e.getMessage());
75
        }    
76
        
77
        return -1;
78
    }
79

  
80
    //** register next unique number for a local ID by a given global name */
81
    public String put () {
82
        
83
        if (!check(globalName, localId)) {
84
            put(globalName, localId);
85
            return globalName + ":" + localId;
86
        } else {
87
            // get max local Id, put next Id
88
            int l = get(globalName);
89
            System.out.println(l);
90
            if ( l == -1 ) l = 1;
91
            else l += 1;
92
            put (globalName, (new Integer(l)).toString());
93
            return globalName + ":" + l;
94
        }    
95
    }
96

  
97
    private void put (String globalName, String localId) {
98
        
99
        try {
100
            PreparedStatement pstmt;
101
            pstmt = conn.prepareStatement("INSERT INTO xml_acc_numbers (global_name, local_id) " + 
102
                                          "VALUES (?, ?)");
103
            pstmt.setString(1,globalName);
104
            pstmt.setString(2,localId);
105
            pstmt.execute();
106
            
107
        } catch (SQLException e) {
108
            System.out.println("Error on AccessionNumber.put(globalName, localId): " + e.getMessage());
109
        }    
110
    }
111
    
112
    
113
    // get the global part of the accession number
114
    private String getGlobalName (String accNumber) 
115
        throws StringIndexOutOfBoundsException {
116
        
117
        return accNumber.substring(0, accNumber.lastIndexOf(":"));
118
    }    
119

  
120
    // get the local part of the accession number
121
    private String getLocalId (String accNumber)
122
        throws StringIndexOutOfBoundsException {
123

  
124
        return accNumber.substring(accNumber.lastIndexOf(":")+1);
125
    }    
126

  
127
    static public void main(String[] args) {
128

  
129
        String 	defaultDB = "jdbc:oracle:thin:@penelope.nceas.ucsb.edu:1526:DEV8";
130
        
131
        if (args.length < 4)
132
        {
133
            System.err.println("Wrong number of arguments!!!");
134
            System.err.println("USAGE: java AccessionNumber " +
135
                               "<userID> <accessionNumber> <user> <password> [dbstring]");
136
            return;
137
        } else {
138
            try {
139
                    
140
                String userID   = args[0];
141
                String accNum   = args[1];
142
                String user     = args[2];
143
                String password = args[3];
144
                String dbstring = null;
145

  
146
                if (args.length <= 4)
147
                    dbstring = defaultDB;
148
                else
149
                    dbstring = args[4];
150

  
151
                // Open a connection to the database
152
                // Load the Oracle JDBC driver
153
                Class.forName ("oracle.jdbc.driver.OracleDriver");
154

  
155
                // Connect to the database
156
                Connection conn = DriverManager.getConnection( dbstring, user, password);
157

  
158
                AccessionNumber an = new AccessionNumber(conn, accNum);
159
                System.out.println("Accession Number registration finished for: " + an.accNumber);
160

  
161
            } catch (Exception e) {
162
                System.err.println(e.getMessage());
163
            }
164
        }
165
    }    
166
       
167
}
0 168

  

Also available in: Unified diff