Project

General

Profile

« Previous | Next » 

Revision 965

Added by Jing Tao over 22 years ago

In order to increase data file docid, method registerDocument was revised. Now it not only insert a new data file record to xml_documents table, but also can update the data file documents. Moreover, it can archieve the old version to xml_revisions table. Two other private methods were added to handl these futures: deleteXMLDocuments and getLatestRevisionNumber.

View differences:

src/edu/ucsb/nceas/metacat/DocumentImpl.java
36 36
import java.io.StringWriter;
37 37
import java.io.Writer;
38 38
import java.io.InputStreamReader;
39

  
40 39
import java.text.SimpleDateFormat;
41 40

  
42 41
import java.util.Date;
......
249 248
  {
250 249
    Connection dbconn = null;
251 250
    MetaCatUtil util = new MetaCatUtil();
252

  
251
    AccessionNumber ac;
253 252
    try {
254 253
      dbconn = util.openDBConnection();
255

  
256
      AccessionNumber ac = new AccessionNumber(dbconn, accnum, "insert");
254
      String docIdWithoutRev=MetaCatUtil.getDocIdFromString(accnum);
255
      int userSpecifyRev=MetaCatUtil.getVersionFromString(accnum);
256
      int revInDataBase=getLatestRevisionNumber(dbconn, docIdWithoutRev);
257
      //revIndataBase=-1, there is no record in xml_documents table
258
      //the data file is a new one, inert it into table
259
      //user specified rev should be great than 0
260
      if (revInDataBase==-1 && userSpecifyRev>0 )
261
      {
262
        ac = new AccessionNumber(dbconn, accnum, "insert");
263
      }
264
      //rev is greater the last revsion number and revInDataBase isn't -1
265
      // it is a updated data file
266
      else if (userSpecifyRev>revInDataBase && revInDataBase>0)
267
      {
268
        
269
        //archive the old entry 
270
        archiveDocRevision(dbconn, docIdWithoutRev, user);
271
        //delete the old entry in xml_documents
272
        deleteXMLDocuments(dbconn, docIdWithoutRev);
273
        ac = new AccessionNumber(dbconn, accnum, "update");
274
      }
275
      //other situation
276
      else
277
      {
278
        
279
        throw new Exception("Revision number couldn't be "
280
                    +userSpecifyRev);
281
      }
257 282
      String docid = ac.getDocid();
258 283
      String rev = ac.getRev();
259
  
260 284
      SimpleDateFormat formatter = new SimpleDateFormat ("yy-MM-dd HH:mm:ss");
261 285
      Date localtime = new Date();
262 286
      String dateString = formatter.format(localtime);
......
278 302
      sql.append(sqlDateString).append(",");
279 303
      sql.append(sqlDateString).append(",");
280 304
      sql.append("'0')");
281
      //System.out.println("sql: " + sql.toString());
282

  
283 305
      PreparedStatement pstmt = dbconn.prepareStatement(sql.toString());
284 306
      pstmt.execute();
285 307
      pstmt.close();
......
1269 1291
    pstmt.close();
1270 1292

  
1271 1293
  }
1294
  
1295
  /**
1296
    * delete a entry in xml_table for given docid
1297
    * @param docId, the id of the document need to be delete
1298
    */
1299
  private static void deleteXMLDocuments(Connection conn, String docId) 
1300
                                         throws SQLException 
1301
  {
1302
    //delete a record 
1303
    PreparedStatement pStmt = 
1304
             conn.prepareStatement("DELETE FROM xml_documents WHERE docid = '" 
1305
                                              + docId + "'");
1306
    pStmt.execute();
1307
    pStmt.close();
1272 1308

  
1309
  }//deleteXMLDocuments
1310
  
1273 1311
  /**
1312
    * Get last revision number from database for a docid
1313
    * If couldn't find an entry,  -1 will return
1314
    * The return value is integer because we want compare it to there new one
1315
    * @param docid <sitecode>.<uniqueid> part of Accession Number
1316
    */
1317
  private static int getLatestRevisionNumber(Connection conn, String docId)
1318
                                      throws SQLException
1319
  {
1320
    int rev = 1;
1321
    PreparedStatement pStmt;
1322
     
1323
    pStmt = conn.prepareStatement
1324
              ("SELECT rev FROM xml_documents WHERE docid='" + docId + "'");
1325
    pStmt.execute();
1326

  
1327
    ResultSet rs = pStmt.getResultSet();
1328
    boolean hasRow = rs.next();
1329
    if (hasRow)
1330
    {
1331
      rev = rs.getInt(1);
1332
      pStmt.close();
1333
    }
1334
    else
1335
    {
1336
      rev=-1;
1337
      pStmt.close();
1338
    }
1339
      
1340
    return rev;
1341
  }//getLatestRevisionNumber
1342
  
1343
  /**
1274 1344
   * the main routine used to test the DBWriter utility.
1275 1345
   * <p>
1276 1346
   * Usage: java DocumentImpl <-f filename -a action -d docid>

Also available in: Unified diff