Project

General

Profile

« Previous | Next » 

Revision 203

Merged in substantial changes to DBWriter and associated classes and to
the MetaCatServlet in order to accomodate the new UPDATE and DELETE
functions. The command line tools and the parameters for the
servlet have changed substantially.

View differences:

AccessionNumber.java
1 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
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 8
 *
9
 *   Version: '$Id$'
9
 *   '$Author$'
10
 *     '$Date$'
11
 * '$Revision$'
10 12
 */
11 13

  
12 14
package edu.ucsb.nceas.metacat;
......
34 36
     * When "DELETE", accession # is required. 
35 37
     */
36 38
    public static String generate (String accNumber, String action) 
37
        throws ClassNotFoundException, StringIndexOutOfBoundsException, 
38
               SQLException
39
        throws AccessionNumberException, SQLException, ClassNotFoundException
39 40
    {
40 41
        
41 42
        String globalName = null;
......
59 60
            }    
60 61

  
61 62
            // register unique acc #
62
            if ( action == "INSERT" )
63
            if ( action.equals("INSERT")) {
63 64
                if ( accNumber == null )
64
                    return put(conn, defaultGlobalName, null, sep);
65
                  return put(conn, defaultGlobalName, null, sep);
65 66
                else
66
                    return put(conn, globalName, localId, sep);
67
                    
67
                  return put(conn, globalName, localId, sep);
68
            } else if ( action.equals("UPDATE") || action.equals("DELETE")) {
69
                if ( accNumber == null ) {
70
                  throw (new AccessionNumberException("Accession number is " +
71
                         "required."));
72
                } else if (!accNumberIsCurrent(conn, accNumber)) {
73
                  throw (new AccessionNumberException("Document " +
74
                         "not found for accession #: " + accNumber));
75
                } else {
76
                  return (globalName + sep + localId);
77
                }
78
            }
79
 
68 80
            conn.close();        
69 81

  
82
        } catch (StringIndexOutOfBoundsException siobe) {
83
            MetaCatUtil.debugMessage(
84
                       "Error on AccessionNumber.generate(): " + 
85
                       siobe.getMessage());
86
            throw (new AccessionNumberException("Accession number invalid, " +
87
                   "expecting character \'" + sep + "'."));
70 88
        } catch (SQLException e) {
71 89
            System.out.println(
72 90
                       "Error on AccessionNumber.genAccessionNumber(): " + 
......
74 92
            throw e;
75 93
        }    
76 94
        
77
        return null;        
95
        throw (new AccessionNumberException("Fatal Error in " +
96
               "accession number generation: "));
78 97
    }    
79 98

  
80 99
    /** put unique accession # into db connection */
......
87 106
        try {
88 107
            if ( localId == null ) 
89 108
                l = new Integer(get(conn, globalName) + 1); 
90
            else if ( exist(conn, globalName, localId) )
109
            else if ( accNumberUsed(conn, globalName, localId) )
91 110
                l = new Integer(get(conn, globalName) + 1); 
92 111
            else
93 112
                l = new Integer(localId); 
......
112 131
        return globalName + sep + l;
113 132
    }
114 133

  
115
    /** check for existance of Accesssion Number */
116
    private static boolean exist (Connection conn, String globalName, 
117
                String localId) throws SQLException
118
    {
134
    /** check for existence of Accesssion Number xml_acc_numbers table */
135
    private static boolean accNumberUsed(Connection conn, String globalName, 
136
                String localId) throws SQLException {
119 137
        
120
        boolean hasAccNumber = false;
138
      boolean hasAccNumber = false;
121 139
        
122
        try {
123
            PreparedStatement pstmt;
124
            pstmt = conn.prepareStatement(
125
                    "SELECT 'x' FROM xml_acc_numbers " + 
126
                    "WHERE global_name LIKE ? AND local_id = ?");
127
            pstmt.setString(1,globalName);
128
            pstmt.setString(2,localId);
129
            pstmt.execute();
130
            ResultSet rs = pstmt.getResultSet();
131
            hasAccNumber = rs.next();
132
            pstmt.close();
140
      try {
141
        PreparedStatement pstmt;
142
        pstmt = conn.prepareStatement(
143
                "SELECT 'x' FROM xml_acc_numbers " + 
144
                "WHERE global_name LIKE ? AND local_id = ?");
145
        pstmt.setString(1,globalName);
146
        pstmt.setString(2,localId);
147
        pstmt.execute();
148
        ResultSet rs = pstmt.getResultSet();
149
        hasAccNumber = rs.next();
150
        pstmt.close();
133 151
            
134
        } catch (SQLException e) {
135
            System.out.println("Error on AccessionNumber.unique(globalName, " +
136
                               "localId): " + e.getMessage());
137
            throw e;
138
        }    
152
      } catch (SQLException e) {
153
        System.out.println("Error on AccessionNumber.unique(globalName, " +
154
                           "localId): " + e.getMessage());
155
        throw e;
156
      }    
139 157
        
140
        return hasAccNumber;
158
      return hasAccNumber;
141 159
    }    
142 160
    
161
    /** check for existence of Accesssion Number in xml_documents table */
162
    private static boolean accNumberIsCurrent(
163
                Connection conn, String accNumber) throws SQLException {
164
        
165
      boolean hasCurrentAccNumber = false;
166
        
167
      try {
168
        PreparedStatement pstmt;
169
        pstmt = conn.prepareStatement(
170
                "SELECT 'x' FROM xml_documents " + 
171
                "WHERE docid LIKE ?");
172
        pstmt.setString(1, accNumber);
173
        pstmt.execute();
174
        ResultSet rs = pstmt.getResultSet();
175
        hasCurrentAccNumber = rs.next();
176
        pstmt.close();
177
            
178
      } catch (SQLException e) {
179
        System.out.println(
180
          "Error on AccessionNumber.accNumberIsCurrent(globalName, " +
181
          "localId): " + e.getMessage());
182
        throw e;
183
      }    
184
      return hasCurrentAccNumber;
185
    }    
186

  
143 187
    /** get the last in order local ID by a given global name */
144 188
    private static int get (Connection conn, String globalName) 
145 189
                throws SQLException
......
181 225

  
182 226
        return accNumber.substring(accNumber.lastIndexOf(sep)+1);
183 227
    }    
228
}
184 229

  
185
}
230
/**
231
 * '$Log$
232
 * 'Revision 1.8.2.5  2000/06/26 08:38:01  jones
233
 * 'Added DELETE feature to DBWriter.  Now takes an action "DELETE" and a
234
 * 'docid and will move the record from the xml_documents table to the
235
 * 'xml_revisions table.
236
 * 'Modified option parsing to support option symbols on command line.
237
 * '
238
 * 'Revision 1.8.2.4  2000/06/25 23:11:40  jones
239
 * 'Documentation update
240
 * '
241
 * 'Revision 1.8.2.3  2000/06/25 23:08:31  jones
242
 * 'Minor change to excpetion handling
243
 * ''
244
 */

Also available in: Unified diff