Project

General

Profile

« Previous | Next » 

Revision 2607

Added by Jing Tao about 19 years ago

Add code to handle replicate the deleted document.

View differences:

src/edu/ucsb/nceas/metacat/DocumentImpl.java
74 74
    public static final String EML200 = "eml200";
75 75
    public static final String EML210 = "eml210";
76 76
    public static final String EXTERNALSCHEMALOCATIONPROPERTY = "http://apache.org/xml/properties/schema/external-schemaLocation";
77

  
77
    public static final String REVISIONTABLE = "xml_revisions";
78
    public static final String DOCUMENTTABLE = "xml_documents";
78 79
    /*
79 80
     * public static final String EXTERNALSCHEMALOCATION =
80 81
     * "eml://ecoinformatics.org/eml-2.0.0
......
289 290
            throws SQLException, AccessionNumberException, Exception
290 291
    {
291 292

  
292
        DBConnection dbconn = null;
293
        int serialNumber = -1;
294
        PreparedStatement pstmt = null;
295
        //MetaCatUtil util = new MetaCatUtil();
296
        AccessionNumber ac;
297 293
        String action = null;
298
        try {
299
            //dbconn = util.openDBConnection();
300
            //check out DBConnection
301
            dbconn = DBConnectionPool
302
                    .getDBConnection("DocumentImpl.registerDocument");
303
            serialNumber = dbconn.getCheckOutSerialNumber();
304
            String docIdWithoutRev = MetaCatUtil.getDocIdFromString(accnum);
305
            int userSpecifyRev = MetaCatUtil.getVersionFromString(accnum);
306
            int revInDataBase = getLatestRevisionNumber(docIdWithoutRev);
307
            //revIndataBase=-1, there is no record in xml_documents table
308
            //the data file is a new one, inert it into table
309
            //user specified rev should be great than 0
310
            if (revInDataBase == -1 && userSpecifyRev > 0) {
311
                ac = new AccessionNumber(accnum, "insert");
312
                action = "insert";
313
            }
314
            //rev is greater the last revsion number and revInDataBase isn't -1
315
            // it is a updated data file
316
            else if (userSpecifyRev > revInDataBase && revInDataBase > 0) {
317

  
318
                if (!hasWritePermission(user, groups, accnum)) { throw new Exception(
319
                   "User " + user
320
                   + " does not have permission to update the document"
321
                   + accnum); }
322

  
323
                //archive the old entry
324
                archiveDocRevision(docIdWithoutRev, user);
325
                //delete the old entry in xml_documents
326
                //deleteXMLDocuments(docIdWithoutRev);
327
                ac = new AccessionNumber(accnum, "update");
328
                action = "update";
329
            }
330
            //other situation
331
            else {
332

  
333
                throw new Exception("Revision number couldn't be "
334
                        + userSpecifyRev);
335
            }
336
            String docid = ac.getDocid();
337
            String rev = ac.getRev();
338
            /*
339
             * SimpleDateFormat formatter = new SimpleDateFormat ("MM/dd/yy
340
             * HH:mm:ss"); Date localtime = new Date(); String dateString =
341
             * formatter.format(localtime); String
342
             * sqlDateString=dbAdapter.toDate(dateString, "MM/DD/YY
343
             * HH24:MI:SS");
344
             */
345
            String sqlDateString = dbAdapter.getDateTimeFunction();
346

  
347
            StringBuffer sql = new StringBuffer();
348
            if (action != null && action.equals("insert")) {
349
                sql.append("insert into xml_documents (docid, docname, " +
350
                           "doctype, ");
351
                sql.append("user_owner, user_updated, server_location, " +
352
                           "rev,date_created");
353
                sql.append(", date_updated, public_access) values ('");
354
                sql.append(docid).append("','");
355
                sql.append(docname).append("','");
356
                sql.append(doctype).append("','");
357
                sql.append(user).append("','");
358
                sql.append(user).append("','");
359
                sql.append(serverCode).append("','");
360
                sql.append(rev).append("',");
361
                sql.append(sqlDateString).append(",");
362
                sql.append(sqlDateString).append(",");
363
                sql.append("'0')");
364
            } else if (action != null && action.equals("update")) {
365
                sql.append("update xml_documents set docname ='");
366
                sql.append(docname).append("', ");
367
                sql.append("user_updated='");
368
                sql.append(user).append("', ");
369
                sql.append("server_location='");
370
                sql.append(serverCode).append("',");
371
                sql.append("rev='");
372
                sql.append(rev).append("',");
373
                sql.append("date_updated=");
374
                sql.append(sqlDateString);
375
                sql.append(" where docid='");
376
                sql.append(docid).append("'");
377
            }
378
            pstmt = dbconn.prepareStatement(sql.toString());
379
            pstmt.execute();
380
            pstmt.close();
381
            //dbconn.close();
382
        } finally {
383
            try {
384
                if(pstmt != null){
385
                    pstmt.close();
386
                }
387
            } finally {
388
                DBConnectionPool.returnDBConnection(dbconn, serialNumber);
389
            }
294
        String docIdWithoutRev = MetaCatUtil.getDocIdFromAccessionNumber(accnum);
295
        int userSpecifyRev = MetaCatUtil.getRevisionFromAccessionNumber(accnum);
296
        action = checkRevInXMLDocuments(accnum, userSpecifyRev);
297
        if (action.equals("UPDATE"))
298
        {
299
            //archive the old entry
300
            if (!hasWritePermission(user, groups, accnum)) { throw new Exception(
301
                    "User " + user
302
                    + " does not have permission to update the document"
303
                    + accnum); }
304
            archiveDocRevision(docIdWithoutRev, user);  
390 305
        }
306
        
307
        String rev = Integer.toString(userSpecifyRev);
308
        modifyRecordsInGivenTable(DOCUMENTTABLE, action,docIdWithoutRev, doctype, docname,
309
                    user, rev, serverCode);
391 310
    }
392 311

  
393 312
    /**
......
420 339
     *            resides.
421 340
     */
422 341
    public static void registerDocumentInReplication(String docname,
423
            String doctype, String accnum, String user, int serverCode)
342
            String doctype, String accnum, String user, int serverCode, String tableName)
424 343
            throws SQLException, AccessionNumberException, Exception
425 344
    {
426
        DBConnection dbconn = null;
427
        int serialNumber = -1;
428
        //MetaCatUtil util = new MetaCatUtil();
429
        AccessionNumber ac;
430
        PreparedStatement pstmt = null;
345
       
431 346
        String action = null;
347
        String docIdWithoutRev = MetaCatUtil.getDocIdFromAccessionNumber(accnum);
348
        int userSpecifyRev = MetaCatUtil.getRevisionFromAccessionNumber(accnum);
349
        if (tableName.equals(DOCUMENTTABLE))
350
        {
351
            action = checkRevInXMLDocuments(accnum, userSpecifyRev);
352
            if (action.equals("UPDATE"))
353
            {
354
                    //archive the old entry
355
                    archiveDocRevision(docIdWithoutRev, user);  
356
            }
357
        }
358
        else if (tableName.equals(REVISIONTABLE))
359
        {
360
            action = checkXMLRevisionTable(accnum, userSpecifyRev);
361
        }
362
        else
363
        {
364
            throw new Exception("Couldn't handle this table name "+tableName);
365
        }
366
       
367
        String rev = Integer.toString(userSpecifyRev);
368
        modifyRecordsInGivenTable(tableName, action,docIdWithoutRev, doctype, docname,
369
                    user, rev, serverCode);
370
       
371
    }
372
    
373
   /*
374
    * This method will insert or update xml-documents or xml_revision table
375
    */
376
   private static void modifyRecordsInGivenTable(String tableName, String action,
377
                       String docid, String doctype, String docname, String user,
378
                       String rev, int serverCode) throws Exception
379
   {
380
      DBConnection dbconn = null;
381
      PreparedStatement pstmt = null;
382
      int serialNumber = -1;
383
      String sqlDateString = dbAdapter.getDateTimeFunction();
384
      try
385
      {
386
        dbconn = DBConnectionPool.getDBConnection(
387
          "DocumentImpl.registerDocumentInReplication");
388
        serialNumber = dbconn.getCheckOutSerialNumber();
389
        StringBuffer sql = new StringBuffer();
390
        if (action != null && action.equals("INSERT")) {
391
            sql.append("insert into ");
392
            sql.append(tableName);
393
            sql.append(" (docid, docname, " +
394
                       "doctype, ");
395
            sql.append("user_owner, user_updated, server_location, " +
396
                       "rev,date_created");
397
            sql.append(", date_updated, public_access) values ('");
398
            sql.append(docid).append("','");
399
            sql.append(docname).append("','");
400
            sql.append(doctype).append("','");
401
            sql.append(user).append("','");
402
            sql.append(user).append("','");
403
            sql.append(serverCode).append("','");
404
            sql.append(rev).append("',");
405
            sql.append(sqlDateString).append(",");
406
            sql.append(sqlDateString).append(",");
407
            sql.append("'0')");
408
        } else if (action != null && action.equals("UPDATE")) {
409
            sql.append("update xml_documents set docname ='");
410
            sql.append(docname).append("', ");
411
            sql.append("user_updated='");
412
            sql.append(user).append("', ");
413
            sql.append("server_location='");
414
            sql.append(serverCode).append("',");
415
            sql.append("rev='");
416
            sql.append(rev).append("',");
417
            sql.append("date_updated=");
418
            sql.append(sqlDateString);
419
            sql.append(" where docid='");
420
            sql.append(docid).append("'");
421
        }
422
        pstmt = dbconn.prepareStatement(sql.toString());
423
        pstmt.execute();
424
        pstmt.close();
425
       
426
    } finally {
432 427
        try {
433
            //dbconn = util.openDBConnection();
434
            dbconn = DBConnectionPool.getDBConnection(
435
                    "DocumentImpl.registerDocumentInReplication");
436
            serialNumber = dbconn.getCheckOutSerialNumber();
437
            String docIdWithoutRev = MetaCatUtil
438
                    .getDocIdFromAccessionNumber(accnum);
439
            int userSpecifyRev = MetaCatUtil
440
                    .getRevisionFromAccessionNumber(accnum);
441
            int revInDataBase = getLatestRevisionNumber(docIdWithoutRev);
442
            //revIndataBase=-1, there is no record in xml_documents table
443
            //the data file is a new one, inert it into table
444
            //user specified rev should be great than 0
445
            if (revInDataBase == -1 && userSpecifyRev >= 0) {
446
                ac = new AccessionNumber(accnum, "insert");
447
                action = "insert";
428
            if(pstmt != null){
429
                pstmt.close();
448 430
            }
449
            //rev is greater the last revsion number and revInDataBase isn't -1
450
            // it is a updated data file
451
            else if (userSpecifyRev > revInDataBase && revInDataBase >= 0) {
452

  
453
                //archive the old entry
454
                archiveDocRevision(docIdWithoutRev, user);
455
                //delete the old entry in xml_documents
456
                //deleteXMLDocuments(docIdWithoutRev);
457
                ac = new AccessionNumber(accnum, "update");
458
                action = "update";
459
            }
460
            // local server has newer version, then notify the remote server
461
            else if (userSpecifyRev < revInDataBase && revInDataBase > 0) {
462
                throw new Exception("Local server: "
463
                        + MetaCatUtil.getOption("server")
464
                        + " has newer revision of doc: " + docIdWithoutRev
465
                        + "." + revInDataBase + ". Please notify it.");
466
            }
467
            //other situation
468
            else {
469

  
470
                throw new Exception("Revision number couldn't be "
471
                        + userSpecifyRev);
472
            }
473
            String docid = ac.getDocid();
474
            String rev = ac.getRev();
475
            /*
476
             * SimpleDateFormat formatter = new SimpleDateFormat ("MM/dd/yy
477
             * HH:mm:ss"); Date localtime = new Date(); String dateString =
478
             * formatter.format(localtime); String
479
             * sqlDateString=dbAdapter.toDate(dateString, "MM/DD/YY
480
             * HH24:MI:SS");
481
             */
482
            String sqlDateString = dbAdapter.getDateTimeFunction();
483

  
484
            StringBuffer sql = new StringBuffer();
485
            if (action != null && action.equals("insert")) {
486
                sql.append("insert into xml_documents (docid, docname, " +
487
                           "doctype, ");
488
                sql.append("user_owner, user_updated, server_location, " +
489
                           "rev,date_created");
490
                sql.append(", date_updated, public_access) values ('");
491
                sql.append(docid).append("','");
492
                sql.append(docname).append("','");
493
                sql.append(doctype).append("','");
494
                sql.append(user).append("','");
495
                sql.append(user).append("','");
496
                sql.append(serverCode).append("','");
497
                sql.append(rev).append("',");
498
                sql.append(sqlDateString).append(",");
499
                sql.append(sqlDateString).append(",");
500
                sql.append("'0')");
501
            } else if (action != null && action.equals("update")) {
502
                sql.append("update xml_documents set docname ='");
503
                sql.append(docname).append("', ");
504
                sql.append("user_updated='");
505
                sql.append(user).append("', ");
506
                sql.append("server_location='");
507
                sql.append(serverCode).append("',");
508
                sql.append("rev='");
509
                sql.append(rev).append("',");
510
                sql.append("date_updated=");
511
                sql.append(sqlDateString);
512
                sql.append(" where docid='");
513
                sql.append(docid).append("'");
514
            }
515
            // Set auto commit fasle
516
            dbconn.setAutoCommit(false);
517
            pstmt = dbconn.prepareStatement(sql.toString());
518

  
519
            pstmt.execute();
520
            // Commit the insert
521
            dbconn.commit();
522
            pstmt.close();
523
            //dbconn.close();
524 431
        } finally {
525
            // Set DBConnection auto commit true
526
            dbconn.setAutoCommit(true);
527
            pstmt.close();
528 432
            DBConnectionPool.returnDBConnection(dbconn, serialNumber);
529 433
        }
530
    }
531

  
434
    }     
435
   }
532 436
    /**
533 437
     * This method will register a data file entry in xml_documents and save a
534 438
     * data file input Stream into file system.. It is only used in replication
......
554 458
     */
555 459
    public static void writeDataFileInReplication(InputStream input,
556 460
            String filePath, String docname, String doctype, String accnum,
557
            String user, String docHomeServer, String notificationServer)
461
            String user, String docHomeServer, String notificationServer, String tableName, boolean timedReplication)
558 462
            throws SQLException, AccessionNumberException, Exception
559 463
    {
560 464
        int serverCode = -2;
......
574 478

  
575 479
        //register data file into xml_documents table
576 480
        registerDocumentInReplication(docname, doctype, accnum, user,
577
                serverCode);
481
                serverCode, tableName);
578 482
        //write inputstream into file system.
579 483
        File dataDirectory = new File(filePath);
580 484
        File newFile = new File(dataDirectory, accnum);
......
596 500
        bis.close();
597 501
        outPut.close();
598 502
        fos.close();
599

  
503
        
600 504
        // Force replicate data file
601
        ForceReplicationHandler forceReplication = new ForceReplicationHandler(
505
        if (!timedReplication)
506
        {
507
          ForceReplicationHandler forceReplication = new ForceReplicationHandler(
602 508
                accnum, false, notificationServer);
509
        }
603 510
    }
511
    
512
    
513
    /*
514
     * This method will determine if we need to insert or update xml_document base
515
     * on given docid, rev and rev in xml_documents table
516
     */
517
     private static String checkRevInXMLDocuments(String docid, int userSpecifyRev) throws Exception
518
     {
519
        String action = null;
520
        MetaCatUtil.debugMessage("The user specifyRev: " + userSpecifyRev, 30);
521
        // Revision for this docid in current database
522
        int revInDataBase = getLatestRevisionNumber(docid);
523
        MetaCatUtil.debugMessage("The rev in data base: " + revInDataBase, 30);
524
        // String to store the revision
525
        String rev = null;
604 526

  
527
        //revIndataBase=-1, there is no record in xml_documents table
528
        //the document is a new one for local server, inert it into table
529
        //user specified rev should be great than 0
530
        if (revInDataBase == -1 && userSpecifyRev >= 0) {
531
            // rev equals user specified
532
            rev = (new Integer(userSpecifyRev)).toString();
533
            // action should be INSERT
534
            action = "INSERT";
535
        }
536
        //rev is greater the last revsion number and revInDataBase isn't -1
537
        // it is a updated file
538
        else if (userSpecifyRev > revInDataBase && revInDataBase >= 0) {
539
            // rev equals user specified
540
            rev = (new Integer(userSpecifyRev)).toString();
541
            // action should be update
542
            action = "UPDATE";
543
        }
544
        // local server has newer version, then notify the remote server
545
        else if (userSpecifyRev < revInDataBase && revInDataBase > 0) {
546
            throw new Exception("Local server: "
547
                    + MetaCatUtil.getOption("server")
548
                    + " has newer revision of doc: " + docid + "."
549
                    + revInDataBase + ". Please notify it.");
550
        }
551
        //other situation
552
        else {
553

  
554
            throw new Exception("The docid" + docid
555
                    + "'s revision number couldn't be " + userSpecifyRev);
556
        }
557
        return action;
558
     }
559
     
560
     /*
561
      * This method will check if the xml_revision table already has the
562
      * document or not
563
      */
564
     private static String checkXMLRevisionTable(String docid, int rev) throws Exception
565
     {
566
         String action = "INSERT";
567
         Vector localrev = null;
568
           
569
         try
570
         {
571
           localrev = DBUtil.getRevListFromRevisionTable(docid);
572
         }
573
         catch (SQLException e)
574
         {
575
           MetaCatUtil.debugMessage("Local rev for docid "+ docid + " could not "+
576
                                  " be found because " + e.getMessage(), 45);
577
           MetacatReplication.replErrorLog("Docid "+ docid + " could not be "+
578
                   "written because error happend to find it's local revision");
579
           throw new Exception (e.getMessage());
580
         }
581
         MetaCatUtil.debugMessage("rev list in xml_revision table for docid "+ docid + " is "+
582
                                 localrev.toString(), 45);
583
         
584
         // if the rev is in the xml_revision, it throws a exception
585
         if (localrev.contains(new Integer(rev)))
586
         {
587
            throw new Exception("The docid and rev is already in xml_revision table");       
588
         }
589

  
590
         return action;
591
     }
592
     
593
     /*
594
      * 
595
      */
596

  
605 597
    /**
606 598
     * Get a lock for a given document.
607 599
     */
......
1848 1840
            if (action.equals("INSERT")) {
1849 1841
                //AccessionNumber ac = new AccessionNumber();
1850 1842
                //this.docid = ac.generate(docid, "INSERT");
1851

  
1843
                
1852 1844
                pstmt = connection
1853 1845
                        .prepareStatement("INSERT INTO xml_documents "
1854 1846
                        + "(docid, rootnodeid, docname, doctype, user_owner, "
......
1858 1850
                        + sysdate + ", ?, ?, ?, ?)");
1859 1851
                // Increase dbconnection usage count
1860 1852
                connection.increaseUsageCount(1);
1861

  
1853
                
1862 1854
                //note that the server_location is set to 1.
1863 1855
                //this means that "localhost" in the xml_replication table must
1864 1856
                //always be the first entry!!!!!
......
1871 1863
                pstmt.setString(5, user);
1872 1864
                pstmt.setString(6, user);
1873 1865
                //public access is usefulless, so set it to null
1874
                pstmt.setString(7, null);
1866
                pstmt.setInt(7, 0);
1875 1867
                /*
1876 1868
                 * if ( pub == null ) { pstmt.setString(7, null); } else if (
1877 1869
                 * pub.toUpperCase().equals("YES") || pub.equals("1") ) {
......
1879 1871
                 * pub.toUpperCase().equals("NO") || pub.equals("0") ) {
1880 1872
                 * pstmt.setInt(7, 0); }
1881 1873
                 */
1882
                pstmt.setString(8, catalogid);
1874
                pstmt.setInt(8, (new Integer(catalogid)).intValue());
1883 1875
                pstmt.setInt(9, serverCode);
1884 1876
                pstmt.setInt(10, Integer.parseInt(updatedVersion));
1877
                
1885 1878
            } else if (action.equals("UPDATE")) {
1886

  
1879
               
1887 1880
                // Save the old document publicaccessentry in a backup table
1888 1881
                thisdoc = new DocumentImpl(docid, false);
1889 1882
                DocumentImpl.archiveDocRevision(connection, docid, user, thisdoc);
1890
                MetaCatUtil.debugMessage("after archiveDoc", 40);
1883
                MetaCatUtil.debugMessage("after archiveDoc", 30);
1891 1884

  
1892 1885
                int thisrev = thisdoc.getRev();
1893 1886
                MetaCatUtil.debugMessage("this revsion is: " + thisrev, 40);
......
1913 1906
                    MetaCatUtil.debugMessage("after delete", 40);
1914 1907
                    // Increase dbconnection usage count
1915 1908
                    connection.increaseUsageCount(1);
1916

  
1909
                  
1917 1910
                    pstmt.execute();
1911
                  
1918 1912
                    pstmt.close();
1919 1913
                }
1920 1914

  
......
1937 1931
                pstmt.setString(4, user);
1938 1932
                pstmt.setInt(5, serverCode);
1939 1933
                pstmt.setInt(6, thisrev);
1940
                pstmt.setString(7, null);
1934
                pstmt.setInt(7, 0);
1941 1935
                /*
1942 1936
                 * if ( pub == null ) { pstmt.setString(7, null); } else if (
1943 1937
                 * pub.toUpperCase().equals("YES") || pub.equals("1") ) { pstmt
1944 1938
                 * .setInt(7, 1); } else if ( pub.toUpperCase().equals("NO") ||
1945 1939
                 * pub.equals("0") ) { pstmt.setInt(7, 0); }
1946 1940
                 */
1947
                pstmt.setString(8, catalogid);
1941
                pstmt.setInt(8, (new Integer(catalogid)).intValue());
1948 1942
                pstmt.setString(9, this.docid);
1949 1943

  
1950 1944
            } else {
......
1952 1946
            }
1953 1947

  
1954 1948
            // Do the insertion
1949
           
1955 1950
            pstmt.execute();
1951
            
1956 1952
            pstmt.close();
1957

  
1958 1953
            if(action.equals("UPDATE")){
1959 1954
                // Delete the old nodes in xml_nodes table...
1960 1955
                pstmt = connection.prepareStatement("DELETE FROM xml_nodes "
......
1968 1963
            }
1969 1964

  
1970 1965
        } catch (SQLException sqle) {
1966
            sqle.printStackTrace();
1971 1967
            throw sqle;
1972 1968
        } catch (Exception e) {
1969
            e.printStackTrace();
1973 1970
            throw e;
1974 1971
        }
1975 1972
    }
......
2183 2180
            
2184 2181
            // write to xml_node complete. start the indexing thread.
2185 2182
            DBSAXHandler dbx = (DBSAXHandler) parser.getContentHandler();
2183
            System.out.println("here!!!!!!!adf");
2186 2184
            dbx.runIndexingThread();
2185
            System.out.println("here!!!!!!!adfadfa");
2187 2186
        } catch (Exception e) {
2187
            e.printStackTrace();
2188 2188
            conn.rollback();
2189 2189
            conn.setAutoCommit(true);
2190 2190
            //if it is a eml2 document, we need delete online data
......
2265 2265
    public static String writeReplication(DBConnection conn, Reader xml,
2266 2266
            String pub, Reader dtd, String action, String accnum, String user,
2267 2267
            String[] groups, String homeServer, String notifyServer,
2268
            String ruleBase, boolean needValidation) throws Exception
2268
            String ruleBase, boolean needValidation, String tableName, boolean timedReplication) throws Exception
2269 2269
    {
2270
        long rootId;
2270 2271
        MetaCatUtil.debugMessage("user in replication" + user, 30);
2271 2272
        // Docid without revision
2272 2273
        String docid = MetaCatUtil.getDocIdFromAccessionNumber(accnum);
......
2277 2278
        int revInDataBase = getLatestRevisionNumber(docid);
2278 2279
        MetaCatUtil.debugMessage("The rev in data base: " + revInDataBase, 30);
2279 2280
        // String to store the revision
2280
        String rev = null;
2281
        String rev = (new Integer(userSpecifyRev)).toString();
2281 2282

  
2282
        //revIndataBase=-1, there is no record in xml_documents table
2283
        //the document is a new one for local server, inert it into table
2284
        //user specified rev should be great than 0
2285
        if (revInDataBase == -1 && userSpecifyRev >= 0) {
2286
            // rev equals user specified
2287
            rev = (new Integer(userSpecifyRev)).toString();
2288
            // action should be INSERT
2289
            action = "INSERT";
2283
        if (tableName.equals(DOCUMENTTABLE))
2284
        {
2285
            action = checkRevInXMLDocuments(docid, userSpecifyRev);
2290 2286
        }
2291
        //rev is greater the last revsion number and revInDataBase isn't -1
2292
        // it is a updated file
2293
        else if (userSpecifyRev > revInDataBase && revInDataBase >= 0) {
2294
            // rev equals user specified
2295
            rev = (new Integer(userSpecifyRev)).toString();
2296
            // action should be update
2297
            action = "UPDATE";
2287
        else if (tableName.equals(REVISIONTABLE))
2288
        {
2289
            action = checkXMLRevisionTable(docid, userSpecifyRev);
2298 2290
        }
2299
        // local server has newer version, then notify the remote server
2300
        else if (userSpecifyRev < revInDataBase && revInDataBase > 0) {
2301
            throw new Exception("Local server: "
2302
                    + MetaCatUtil.getOption("server")
2303
                    + " has newer revision of doc: " + docid + "."
2304
                    + revInDataBase + ". Please notify it.");
2291
        else
2292
        {
2293
            throw new Exception("The table name is not support "+tableName);
2305 2294
        }
2306
        //other situation
2307
        else {
2308

  
2309
            throw new Exception("The docid" + docid
2310
                    + "'s revision number couldn't be " + userSpecifyRev);
2311
        }
2312 2295
        // Variable to store homeserver code
2313 2296
        int serverCode = -2;
2314 2297

  
......
2330 2313

  
2331 2314
            parser = initializeParser(conn, action, docid, rev, user, groups,
2332 2315
                    pub, serverCode, dtd, ruleBase, needValidation);
2316
            DBSAXHandler dbx = (DBSAXHandler) parser.getContentHandler();
2317
            if (tableName.equals(REVISIONTABLE))
2318
            {
2319
                dbx.setIsRevisionDoc(true);
2320
            }
2333 2321
            conn.setAutoCommit(false);
2334 2322
            parser.parse(new InputSource(xml));
2335 2323
            conn.commit();
2336 2324
            conn.setAutoCommit(true);
2337 2325
            
2338 2326
            // write to xml_node complete. start the indexing thread.
2339
            DBSAXHandler dbx = (DBSAXHandler) parser.getContentHandler();
2340
            dbx.runIndexingThread();
2327
            // this only for xml_documents
2328
            if (!tableName.equals(REVISIONTABLE))
2329
            {
2330
                dbx.runIndexingThread();
2331
            }
2332
            rootId = dbx.getRootNodeId();
2341 2333

  
2342 2334
        } catch (Exception e) {
2343 2335
            conn.rollback();
......
2354 2346

  
2355 2347
        // run write into access db base on relation table and access rule
2356 2348
        try {
2357
            runRelationAndAccessHandler(accnum, user, groups, serverCode);
2349
            if (!tableName.equals(REVISIONTABLE))
2350
            {
2351
               runRelationAndAccessHandler(accnum, user, groups, serverCode);
2352
            }
2353
            else
2354
            {
2355
               moveNodesToNodesRevision(conn, rootId);
2356
            }
2358 2357
        } catch (Exception ee) {
2359 2358
            MetacatReplication.replErrorLog("Failed to " + "create access "
2360 2359
                    + "rule for package: " + accnum + " because "
......
2364 2363
                    + ee.getMessage(), 30);
2365 2364
        }
2366 2365
        //Force replication to other server
2367
        ForceReplicationHandler forceReplication = new ForceReplicationHandler(
2366
        if (!timedReplication)
2367
        {
2368
          ForceReplicationHandler forceReplication = new ForceReplicationHandler(
2368 2369
                accnum, action, true, notifyServer);
2369

  
2370
        }
2370 2371
        return (accnum);
2371 2372
    }
2372 2373

  
......
2858 2859
    private static void archiveDocRevision(DBConnection dbconn, String docid,
2859 2860
            String user, DocumentImpl doc)
2860 2861
    {
2861
        String sysdate = dbAdapter.getDateTimeFunction();
2862
        //DBConnection conn = null;
2863
        //int serialNumber = -1;
2864
        PreparedStatement pstmt = null;
2862
        
2865 2863

  
2866 2864
        // create a record in xml_revisions table
2867 2865
        // for that document as selected from xml_documents
......
2870 2868
            if (doc == null) {
2871 2869
                    doc = new DocumentImpl(docid);
2872 2870
            }
2871
            
2872
            long rootNodeId = doc.getRootNodeID();
2873 2873

  
2874
            //check out DBConnection
2875
            /*
2876
             * conn=DBConnectionPool.
2877
             * getDBConnection("DocumentImpl.archiveDocRevision");
2878
             * serialNumber=conn.getCheckOutSerialNumber();
2879
             */
2874
            archiveDocRevison(dbconn, docid, user, rootNodeId);
2880 2875

  
2881
            // Move the nodes from xml_nodes to xml_revisions table...
2882
            pstmt = dbconn.prepareStatement("INSERT INTO xml_nodes_revisions "
2883
                    + "(nodeid, nodeindex, nodetype, nodename, nodeprefix, "
2884
                    + "nodedata, parentnodeid, rootnodeid, docid, date_created,"
2885
                    + " date_updated, nodedatanumerical) "
2886
                    + "SELECT * FROM xml_nodes " + "WHERE rootnodeid = ?");
2876
        }catch (Exception e) {
2877
            MetaCatUtil.debugMessage(
2878
                    "Error in DocumentImpl.archiveDocRevision : "
2879
                            + e.getMessage(), 30);
2880
        }
2881
       
2882
        
2883
    }
2884
    
2885
    
2886
    private static void archiveDocRevison(DBConnection dbconn, String docid, 
2887
                                     String user, long rootNodeId) throws Exception
2888
    {
2889
        String sysdate = dbAdapter.getDateTimeFunction();
2890
        //DBConnection conn = null;
2891
        //int serialNumber = -1;
2892
        PreparedStatement pstmt = null;
2893
        try
2894
        {
2895
        // Move the nodes from xml_nodes to xml_nodes_revisions table...
2896
        moveNodesToNodesRevision(dbconn, rootNodeId);
2887 2897

  
2888
            // Increase dbconnection usage count
2889
            dbconn.increaseUsageCount(1);
2890
            // Bind the values to the query and execute it
2891
            pstmt.setLong(1, doc.getRootNodeID());
2892
            pstmt.execute();
2893
            pstmt.close();
2898
        // Move the document information to xml_revisions table...
2899
        pstmt = dbconn.prepareStatement("INSERT INTO xml_revisions "
2900
                + "(docid, rootnodeid, docname, doctype, "
2901
                + "user_owner, user_updated, date_created, date_updated, "
2902
                + "server_location, rev, public_access, catalog_id) "
2903
                + "SELECT ?, rootnodeid, docname, doctype, "
2904
                + "user_owner, ?, " + sysdate + ", " + sysdate + ", "
2905
                + "server_location, rev, public_access, catalog_id "
2906
                + "FROM xml_documents " + "WHERE docid = ?");
2894 2907

  
2895
            // Move the document information to xml_revisions table...
2896
            pstmt = dbconn.prepareStatement("INSERT INTO xml_revisions "
2897
                    + "(docid, rootnodeid, docname, doctype, "
2898
                    + "user_owner, user_updated, date_created, date_updated, "
2899
                    + "server_location, rev, public_access, catalog_id) "
2900
                    + "SELECT ?, rootnodeid, docname, doctype, "
2901
                    + "user_owner, ?, " + sysdate + ", " + sysdate + ", "
2902
                    + "server_location, rev, public_access, catalog_id "
2903
                    + "FROM xml_documents " + "WHERE docid = ?");
2908
        // Increase dbconnection usage count
2909
        dbconn.increaseUsageCount(1);
2910
        // Bind the values to the query and execute it
2911
        pstmt.setString(1, docid);
2912
        pstmt.setString(2, user);
2913
        pstmt.setString(3, docid);
2914
        pstmt.execute();
2915
        pstmt.close();
2904 2916

  
2905
            // Increase dbconnection usage count
2906
            dbconn.increaseUsageCount(1);
2907
            // Bind the values to the query and execute it
2908
            pstmt.setString(1, docid);
2909
            pstmt.setString(2, user);
2910
            pstmt.setString(3, docid);
2911
            pstmt.execute();
2917
      } catch (SQLException e) {
2918
        MetaCatUtil.debugMessage(
2919
                "Error in DocumentImpl.archiveDocRevision : "
2920
                        + e.getMessage(), 30);
2921
        throw e;
2922
      } catch (Exception e) {
2923
        MetaCatUtil.debugMessage(
2924
                "Error in DocumentImpl.archiveDocRevision : "
2925
                        + e.getMessage(), 30);
2926
        throw e;
2927
      }
2928
      finally {
2929
        try {
2912 2930
            pstmt.close();
2913

  
2914
        } catch (SQLException e) {
2931
        } catch (SQLException ee) {
2915 2932
            MetaCatUtil.debugMessage(
2916
                    "Error in DocumentImpl.archiveDocRevision : "
2917
                            + e.getMessage(), 30);
2918
        } catch (Exception e) {
2919
            MetaCatUtil.debugMessage(
2920
                    "Error in DocumentImpl.archiveDocRevision : "
2921
                            + e.getMessage(), 30);
2933
                    "Error in DocumnetImpl.archiveDocRevision: "
2934
                            + ee.getMessage(), 50);
2922 2935
        }
2923
        finally {
2924
            try {
2925
                pstmt.close();
2926
            } catch (SQLException ee) {
2927
                MetaCatUtil.debugMessage(
2928
                        "Error in DocumnetImpl.archiveDocRevision: "
2929
                                + ee.getMessage(), 50);
2930
            }
2931
        }
2936
      }
2932 2937
    }
2938
    
2939
    private static void moveNodesToNodesRevision(DBConnection dbconn,
2940
                                       long rootNodeId) throws Exception
2941
    {
2942
        PreparedStatement pstmt = null;
2943
        // Move the nodes from xml_nodes to xml_revisions table...
2944
        pstmt = dbconn.prepareStatement("INSERT INTO xml_nodes_revisions "
2945
                + "(nodeid, nodeindex, nodetype, nodename, nodeprefix, "
2946
                + "nodedata, parentnodeid, rootnodeid, docid, date_created,"
2947
                + " date_updated, nodedatanumerical) "
2948
                + "SELECT * FROM xml_nodes " + "WHERE rootnodeid = ?");
2933 2949

  
2950
        // Increase dbconnection usage count
2951
        dbconn.increaseUsageCount(1);
2952
        // Bind the values to the query and execute it
2953
        pstmt.setLong(1, rootNodeId);
2954
        pstmt.execute();
2955
        pstmt.close();
2956

  
2957
    }
2958

  
2934 2959
    /** Save a document entry in the xml_revisions table */
2935 2960
    private static void archiveDocRevision(String docid, String user)
2936 2961
    {

Also available in: Unified diff