Project

General

Profile

« Previous | Next » 

Revision 5334

Added by berkley over 14 years ago

refactoring to remove duplicate code

View differences:

src/edu/ucsb/nceas/metacat/IdentifierManager.java
45 45
 */
46 46
public class IdentifierManager {
47 47
    
48
    public static final String TYPE_SYSTEM_METADATA = "systemmetadata";
49
    public static final String TYPE_IDENTIFIER = "identifier";
50
  
48 51
    /**
49 52
     * The single instance of the manager that is always returned.
50 53
     */
......
83 86
     */
84 87
    public String getLocalId(String guid) throws McdbDocNotFoundException
85 88
    {
86
        String db_guid = "";
87
        String docid = "";
88
        int rev = 0;
89
        
90
        String query = "select guid, docid, rev from identifier where guid = ?";
91
        
92
        DBConnection dbConn = null;
93
        int serialNumber = -1;
94
        try {
95
            // Get a database connection from the pool
96
            dbConn = DBConnectionPool.getDBConnection("Identifier.getLocalId");
97
            serialNumber = dbConn.getCheckOutSerialNumber();
98
            
99
            // Execute the insert statement
100
            PreparedStatement stmt = dbConn.prepareStatement(query);
101
            stmt.setString(1, guid);
102
            ResultSet rs = stmt.executeQuery();
103
            if (rs.next()) {
104
                db_guid = rs.getString(1);
105
                docid = rs.getString(2);
106
                rev = rs.getInt(3);
107
                assert(db_guid.equals(guid));
108
            } else {
109
                throw new McdbDocNotFoundException("Document not found:" + guid);
110
            }
111
            stmt.close();
112
        } catch (SQLException e) {
113
            logMetacat.error("Error while looking up the local identifier: " 
114
                    + e.getMessage());
115
        } finally {
116
            // Return database connection to the pool
117
            DBConnectionPool.returnDBConnection(dbConn, serialNumber);
118
        }
119
        return docid + "." + rev;
89
        return this.getId(guid, TYPE_IDENTIFIER);
120 90
    }
121 91
    
122 92
    /**
......
150 120
     */
151 121
    public void createMapping(String guid, String localId)
152 122
    {   
153
        int serialNumber = -1;
154
        DBConnection dbConn = null;
155
        try {
156

  
157
            // Parse the localId into scope and rev parts
158
            AccessionNumber acc = new AccessionNumber(localId, "NOACTION");
159
            String docid = acc.getDocid();
160
            int rev = (new Integer(acc.getRev()).intValue());
161

  
162
            // Get a database connection from the pool
163
            dbConn = 
164
                DBConnectionPool.getDBConnection("Identifier.createMapping");
165
            serialNumber = dbConn.getCheckOutSerialNumber();
166

  
167
            // Execute the insert statement
168
            String query = "insert into identifier (guid, docid, rev) values (?, ?, ?)";            
169
            PreparedStatement stmt = dbConn.prepareStatement(query);
170
            stmt.setString(1, guid);
171
            stmt.setString(2, docid);
172
            stmt.setInt(3, rev);
173
            int rows = stmt.executeUpdate();
174

  
175
            stmt.close();
176
        } catch (SQLException e) {
177
            logMetacat.error("SQL error while creating a mapping to the local identifier: " 
178
                    + e.getMessage());
179
        } catch (NumberFormatException e) {
180
            logMetacat.error("NumberFormat error while creating a mapping to the local identifier: " 
181
                    + e.getMessage());
182
        } catch (AccessionNumberException e) {
183
            logMetacat.error("AccessionNumber error while creating a mapping to the local identifier: " 
184
                    + e.getMessage());
185
        } finally {
186
            // Return database connection to the pool
187
            DBConnectionPool.returnDBConnection(dbConn, serialNumber);
188
        }
123
        createGenericMapping(guid, localId, TYPE_IDENTIFIER);
189 124
    }
190 125

  
191 126
    /**
......
286 221
     */
287 222
    public void createSystemMetadataMapping(String guid, String localId)
288 223
    {
224
        createGenericMapping(guid, localId, TYPE_SYSTEM_METADATA);
225
    }
226
    
227
    
228
    /**
229
     * return the localId of a system metadata document based on its guid
230
     */
231
    public String getSystemMetadataId(String guid)
232
      throws McdbDocNotFoundException
233
    {
234
      return this.getId(guid, TYPE_SYSTEM_METADATA);
235
    }
236
    
237
    /**
238
     * return a localId based on a guid.  The type can either be 'identifier' 
239
     * to get an id from the identifier table or 'systemmetadata' to get
240
     * the identifier from the systemidentifier table
241
     */
242
    private String getId(String guid, String type)
243
      throws McdbDocNotFoundException
244
    {
245
      if(!type.equals(TYPE_IDENTIFIER) &&
246
        !type.equals(TYPE_SYSTEM_METADATA))
247
      {
248
        throw new RuntimeException("cannot lookup the identifier for type " + type +
249
          ".  Please choose 'identifier' or 'systemmetadata'.");
250
      }
251
      
252
      String db_guid = "";
253
      String docid = "";
254
      int rev = 0;
255
      
256
      String query = "select guid, docid, rev from " + type + " where guid = ?";
257
      
258
      DBConnection dbConn = null;
289 259
      int serialNumber = -1;
260
      try {
261
          // Get a database connection from the pool
262
          dbConn = DBConnectionPool.getDBConnection("Identifier.getLocalId");
263
          serialNumber = dbConn.getCheckOutSerialNumber();
264
          
265
          // Execute the insert statement
266
          PreparedStatement stmt = dbConn.prepareStatement(query);
267
          stmt.setString(1, guid);
268
          ResultSet rs = stmt.executeQuery();
269
          if (rs.next()) {
270
              db_guid = rs.getString(1);
271
              docid = rs.getString(2);
272
              rev = rs.getInt(3);
273
              assert(db_guid.equals(guid));
274
          } else {
275
              throw new McdbDocNotFoundException("Document not found:" + guid);
276
          }
277
          stmt.close();
278
      } catch (SQLException e) {
279
          logMetacat.error("Error while looking up the local identifier: " 
280
                  + e.getMessage());
281
      } finally {
282
          // Return database connection to the pool
283
          DBConnectionPool.returnDBConnection(dbConn, serialNumber);
284
      }
285
      return docid + "." + rev;
286
    }
287
    
288
    private void createGenericMapping(String guid, String localId, String type)
289
    {
290
        if(!type.equals(TYPE_IDENTIFIER) &&
291
        !type.equals(TYPE_SYSTEM_METADATA))
292
        {
293
          throw new RuntimeException("Cannot create mapping for type " + type +
294
            ".  Please choose 'identifier' or 'systemmetadata'.");
295
        }
296
        
297
        int serialNumber = -1;
290 298
        DBConnection dbConn = null;
291 299
        try {
292 300

  
......
301 309
            serialNumber = dbConn.getCheckOutSerialNumber();
302 310

  
303 311
            // Execute the insert statement
304
            String query = "insert into systemMetadata (guid, docid, rev) values (?, ?, ?)";            
312
            String query = "insert into " + type + " (guid, docid, rev) values (?, ?, ?)";            
305 313
            PreparedStatement stmt = dbConn.prepareStatement(query);
306 314
            stmt.setString(1, guid);
307 315
            stmt.setString(2, docid);

Also available in: Unified diff