Project

General

Profile

« Previous | Next » 

Revision 2751

Added by Jing Tao about 19 years ago

Fixed the bug that replicate data file failed that the documents records wouldn't be deleted.

View differences:

src/edu/ucsb/nceas/metacat/DocumentImpl.java
296 296
            String accnum, String user, String[] groups, int serverCode)
297 297
            throws SQLException, AccessionNumberException, Exception
298 298
    {
299

  
300
        String action = null;
301
        String docIdWithoutRev = MetaCatUtil.getDocIdFromAccessionNumber(accnum);
302
        int userSpecifyRev = MetaCatUtil.getRevisionFromAccessionNumber(accnum);
303
        action = checkRevInXMLDocuments(accnum, userSpecifyRev);
304
        if (action.equals("UPDATE"))
299
        DBConnection conn = null;
300
        int serialNumber = -1;
301
        try
305 302
        {
306
            //archive the old entry
307
            if (!hasWritePermission(user, groups, accnum)) { throw new Exception(
308
                    "User " + user
309
                    + " does not have permission to update the document"
310
                    + accnum); }
311
            archiveDocRevision(docIdWithoutRev, user);  
303
            conn = DBConnectionPool
304
                .getDBConnection("DocumentImpl.registerDocumentInreplication");
305
            serialNumber = conn.getCheckOutSerialNumber();
306
            conn.setAutoCommit(false);
307
            String action = null;
308
            String docIdWithoutRev = MetaCatUtil.getDocIdFromAccessionNumber(accnum);
309
            int userSpecifyRev = MetaCatUtil.getRevisionFromAccessionNumber(accnum);
310
            action = checkRevInXMLDocuments(docIdWithoutRev, userSpecifyRev);
311
            logMetacat.warn("after check rev, the action is "+action);
312
            if (action.equals("UPDATE"))
313
            {
314
                //archive the old entry
315
                if (!hasWritePermission(user, groups, accnum)) { throw new Exception(
316
                        "User " + user
317
                        + " does not have permission to update the document"
318
                        + accnum); }
319
                archiveDocRevision(docIdWithoutRev, user, conn);  
320
            }
321
            
322
            String rev = Integer.toString(userSpecifyRev);
323
            modifyRecordsInGivenTable(DOCUMENTTABLE, action,docIdWithoutRev, doctype, docname,
324
                        user, rev, serverCode, null, null, conn);
325
                        // null and null is createdate and updatedate
326
                        // null will create current time
327
            conn.commit();
328
            conn.setAutoCommit(true);
312 329
        }
330
        catch(Exception e)
331
        {
332
            conn.rollback();
333
            conn.setAutoCommit(true);
334
            throw e;
335
        }
336
        finally
337
        {
338
            //check in DBConnection
339
            DBConnectionPool.returnDBConnection(conn, serialNumber);
340
        }
313 341
        
314
        String rev = Integer.toString(userSpecifyRev);
315
        modifyRecordsInGivenTable(DOCUMENTTABLE, action,docIdWithoutRev, doctype, docname,
316
                    user, rev, serverCode, null, null);
317
                    // null and null is createdate and updatedate
318
                    // null will create current time
319 342
    }
320 343

  
321 344
    /**
......
352 375
            String tableName, String createDate, String updateDate)
353 376
            throws SQLException, AccessionNumberException, Exception
354 377
    {
355
       
356
        String action = null;
357
        String docIdWithoutRev = MetaCatUtil.getDocIdFromAccessionNumber(accnum);
358
        int userSpecifyRev = MetaCatUtil.getRevisionFromAccessionNumber(accnum);
359
        if (tableName.equals(DOCUMENTTABLE))
378
        DBConnection conn = null;
379
        int serialNumber = -1;
380
        try
360 381
        {
361
            action = checkRevInXMLDocuments(accnum, userSpecifyRev);
362
            if (action.equals("UPDATE"))
382
            //check out DBConnection
383
            conn = DBConnectionPool
384
                    .getDBConnection("DocumentImpl.registerDocumentInreplication");
385
            serialNumber = conn.getCheckOutSerialNumber();
386
            conn.setAutoCommit(false);
387
            String action = null;
388
            String docIdWithoutRev = MetaCatUtil.getDocIdFromAccessionNumber(accnum);
389
            int userSpecifyRev = MetaCatUtil.getRevisionFromAccessionNumber(accnum);
390
            if (tableName.equals(DOCUMENTTABLE))
363 391
            {
364
                    //archive the old entry
365
                    archiveDocRevision(docIdWithoutRev, user);  
392
                action = checkRevInXMLDocuments(docIdWithoutRev, userSpecifyRev);
393
                if (action.equals("UPDATE"))
394
                {
395
                        //archive the old entry
396
                        archiveDocRevision(docIdWithoutRev, user, conn);  
397
                }
366 398
            }
399
            else if (tableName.equals(REVISIONTABLE))
400
            {
401
                action = checkXMLRevisionTable(docIdWithoutRev, userSpecifyRev);
402
            }
403
            else
404
            {
405
                throw new Exception("Couldn't handle this table name "+tableName);
406
            }
407
           
408
            String rev = Integer.toString(userSpecifyRev);
409
            modifyRecordsInGivenTable(tableName, action,docIdWithoutRev, doctype, docname,
410
                        user, rev, serverCode, createDate, updateDate, conn);
411
            conn.commit();
412
            conn.setAutoCommit(true);
367 413
        }
368
        else if (tableName.equals(REVISIONTABLE))
414
        catch(Exception e)
369 415
        {
370
            action = checkXMLRevisionTable(accnum, userSpecifyRev);
416
            conn.rollback();
417
            conn.setAutoCommit(true);
418
            throw e;
371 419
        }
372
        else
420
        finally
373 421
        {
374
            throw new Exception("Couldn't handle this table name "+tableName);
422
            //check in DBConnection
423
            DBConnectionPool.returnDBConnection(conn, serialNumber);
375 424
        }
376 425
       
377
        String rev = Integer.toString(userSpecifyRev);
378
        modifyRecordsInGivenTable(tableName, action,docIdWithoutRev, doctype, docname,
379
                    user, rev, serverCode, createDate, updateDate);
380
       
381 426
    }
382 427
    
383 428
   /*
......
385 430
    */
386 431
   private static void modifyRecordsInGivenTable(String tableName, String action,
387 432
                       String docid, String doctype, String docname, String user,
388
                       String rev, int serverCode, String createDate, String updateDate) throws Exception
433
                       String rev, int serverCode, String createDate, String updateDate,
434
                       DBConnection dbconn) throws Exception
389 435
   {
390
      DBConnection dbconn = null;
436
      
391 437
      PreparedStatement pstmt = null;
392
      int serialNumber = -1;
393 438
      int revision = (new Integer(rev)).intValue();
394 439
      String sqlDateString = dbAdapter.getDateTimeFunction();
395 440
      if (createDate == null)
......
411 456
      }
412 457
      try
413 458
      {
414
        dbconn = DBConnectionPool.getDBConnection(
415
          "DocumentImpl.registerDocumentInReplication");
416
        serialNumber = dbconn.getCheckOutSerialNumber();
459
        
417 460
        StringBuffer sql = new StringBuffer();
418 461
        if (action != null && action.equals("INSERT")) {
462
            
419 463
            sql.append("insert into ");
420 464
            sql.append(tableName);
421 465
            sql.append(" (docid, docname, " +
......
434 478
            sql.append(updateDate).append(",");
435 479
            sql.append("'0')");
436 480
        } else if (action != null && action.equals("UPDATE")) {
481
            
437 482
            sql.append("update xml_documents set docname ='");
438 483
            sql.append(docname).append("', ");
439 484
            sql.append("user_updated='");
......
451 496
        pstmt.execute();
452 497
        pstmt.close();
453 498
       
454
    } finally {
455
        try {
456
            if(pstmt != null){
457
                pstmt.close();
458
            }
459
        } finally {
460
            DBConnectionPool.returnDBConnection(dbconn, serialNumber);
461
        }
462
    }     
499
    }
500
    catch(Exception e) 
501
    {
502
     
503
        throw e;
504
    }
505
    finally 
506
    {
507
       if(pstmt != null)
508
       {
509
         pstmt.close();
510
       }
511
     }     
463 512
   }
464 513
    /**
465 514
     * This method will register a data file entry in xml_documents and save a
......
505 554
        // Get server code again
506 555
        serverCode = getServerCode(docHomeServer);
507 556

  
508
        //register data file into xml_documents table
509
        registerDocumentInReplication(docname, doctype, accnum, user,
510
                serverCode, tableName, createDate, updateDate);
557
        
511 558
        //write inputstream into file system.
512 559
        File dataDirectory = new File(filePath);
513
        File newFile = new File(dataDirectory, accnum);
514

  
515
        // create a buffered byte output stream
516
        // that uses a default-sized output buffer
517
        FileOutputStream fos = new FileOutputStream(newFile);
518
        BufferedOutputStream outPut = new BufferedOutputStream(fos);
519

  
520
        BufferedInputStream bis = null;
521
        bis = new BufferedInputStream(input);
522
        byte[] buf = new byte[4 * 1024]; // 4K buffer
523
        int b = bis.read(buf);
524

  
525
        while (b != -1) {
526
            outPut.write(buf, 0, b);
527
            b = bis.read(buf);
560
        File newFile = null;
561
        try
562
        {
563
            newFile = new File(dataDirectory, accnum);
564
    
565
            // create a buffered byte output stream
566
            // that uses a default-sized output buffer
567
            FileOutputStream fos = new FileOutputStream(newFile);
568
            BufferedOutputStream outPut = new BufferedOutputStream(fos);
569
    
570
            BufferedInputStream bis = null;
571
            bis = new BufferedInputStream(input);
572
            byte[] buf = new byte[4 * 1024]; // 4K buffer
573
            int b = bis.read(buf);
574
    
575
            while (b != -1) {
576
                outPut.write(buf, 0, b);
577
                b = bis.read(buf);
578
            }
579
            bis.close();
580
            outPut.close();
581
            fos.close();
582
            
583
            //register data file into xml_documents table
584
            registerDocumentInReplication(docname, doctype, accnum, user,
585
                    serverCode, tableName, createDate, updateDate);
528 586
        }
529
        bis.close();
530
        outPut.close();
531
        fos.close();
587
        catch (Exception ee)
588
        {
589
            newFile.delete();
590
            throw ee;
591
        }
532 592
        
533 593
        // Force replicate data file
534 594
        if (!timedReplication)
......
546 606
     private static String checkRevInXMLDocuments(String docid, int userSpecifyRev) throws Exception
547 607
     {
548 608
        String action = null;
549
        logMetacat.info("The user specifyRev: " + userSpecifyRev);
609
        logMetacat.warn("The docid without rev is "+docid);
610
        logMetacat.warn("The user specifyRev: " + userSpecifyRev);
550 611
        // Revision for this docid in current database
551 612
        int revInDataBase =DBUtil.getLatestRevisionInDocumentTable(docid);
552
        logMetacat.info("The rev in data base: " + revInDataBase);
613
        logMetacat.warn("The rev in data base: " + revInDataBase);
553 614
        // String to store the revision
554 615
        String rev = null;
555 616

  
......
2412 2473
        logMetacat.info("user in replication" + user);
2413 2474
        // Docid without revision
2414 2475
        String docid = MetaCatUtil.getDocIdFromAccessionNumber(accnum);
2476
        logMetacat.warn("The docid without rev is " + docid);
2415 2477
        // Revision specified by user (int)
2416 2478
        int userSpecifyRev = MetaCatUtil.getRevisionFromAccessionNumber(accnum);
2417
        logMetacat.info("The user specifyRev: " + userSpecifyRev);
2479
        logMetacat.warn("The user specifyRev: " + userSpecifyRev);
2418 2480
        // Revision for this docid in current database
2419 2481
        int revInDataBase = DBUtil.getLatestRevisionInDocumentTable(docid);
2420
        logMetacat.info("The rev in data base: " + revInDataBase);
2482
        logMetacat.warn("The rev in data base: " + revInDataBase);
2421 2483
        // String to store the revision
2422 2484
        String rev = (new Integer(userSpecifyRev)).toString();
2423 2485

  
......
3147 3209
    }
3148 3210

  
3149 3211
    /** Save a document entry in the xml_revisions table */
3150
    private static void archiveDocRevision(String docid, String user) throws Exception
3212
    private static void archiveDocRevision(String docid, String user, DBConnection conn) throws Exception
3151 3213
    {
3152 3214
        String sysdate = dbAdapter.getDateTimeFunction();
3153
        DBConnection conn = null;
3154
        int serialNumber = -1;
3155 3215
        PreparedStatement pstmt = null;
3156 3216

  
3157 3217
        // create a record in xml_revisions table
......
3159 3219

  
3160 3220
        try {
3161 3221
            //check out DBConnection
3162
            conn = DBConnectionPool
3163
                    .getDBConnection("DocumentImpl.archiveDocRevision");
3164
            serialNumber = conn.getCheckOutSerialNumber();
3165 3222
            pstmt = conn.prepareStatement("INSERT INTO xml_revisions "
3166 3223
                    + "(docid, rootnodeid, docname, doctype, "
3167 3224
                    + "user_owner, user_updated, date_created, date_updated, "
......
3190 3247
                        "Error in DocumnetImpl.archiveDocRevision: "
3191 3248
                                + ee.getMessage());
3192 3249
                throw ee;
3193
            } finally {
3194
                //check in DBConnection
3195
                DBConnectionPool.returnDBConnection(conn, serialNumber);
3196
            }
3250
            } 
3197 3251
        }
3198 3252
    }
3199 3253

  

Also available in: Unified diff