Project

General

Profile

« Previous | Next » 

Revision 2641

Added by Jing Tao about 19 years ago

Change the code to make sure DocumentImpl class's constructor should have rev attached.

View differences:

src/edu/ucsb/nceas/metacat/ContentTypeProvider.java
173 173
    // create xml document
174 174
    try
175 175
    {
176
      xmlDoc = new DocumentImpl(eml2DocId);
176
      String accNumber = eml2DocId + MetaCatUtil.getOption("accNumSeparator") +
177
                    DBUtil.getLatestRevisionInDocumentTable(eml2DocId);
178
      //System.out.println("the acc number is !!!!!!!!!!!!!!!!!"+accNumber);
179
      xmlDoc = new DocumentImpl(accNumber);
177 180
      xmlString = xmlDoc.toString();
181
      //System.out.println("the xml doc is "+xmlDoc);
178 182
      // create dom tree
179 183
      read = new StringReader(xmlString);
180 184
      in = new InputSource(read);
......
528 532
    String xmlDoc = null;
529 533
    try
530 534
    {
531
      DocumentImpl doc = new DocumentImpl(physicalDocid);
535
      String accNumber = physicalDocid + MetaCatUtil.getOption("accNumSeparator") +
536
        DBUtil.getLatestRevisionInDocumentTable(physicalDocid);
537
      //System.out.println("the accenumber is !!!!!!!!!!!!!!!!!!" + accNumber);
538
      DocumentImpl doc = new DocumentImpl(accNumber);
532 539
      xmlDoc = doc.toString();
540
      //System.out.println("The physical xml is "+xmlDoc);
533 541
    }
534 542
    catch (Exception e)
535 543
    {
src/edu/ucsb/nceas/metacat/DocumentImpl.java
106 106
    static final int READ = 4;
107 107
    protected static final AbstractDatabase dbAdapter = MetaCatUtil.dbAdapter;
108 108
    protected DBConnection connection = null;
109
    protected String docid = null;
110
    protected String updatedVersion = null;
109
    //protected String updatedVersion = null;
111 110
    protected String docname = null;
112 111
    protected String doctype = null;
113 112
    private String validateType = null; //base on dtd or schema
......
116 115
    private String system_id = null;
117 116
    private String userowner = null;
118 117
    private String userupdated = null;
118
    protected String docid = null; // without revision
119 119
    private int rev;
120 120
    private int serverlocation;
121 121
    private String docHomeServer;
......
142 142
     * @param conn
143 143
     *            the database connection from which to read the document
144 144
     * @param docid
145
     *            the identifier of the document to be created
145
     *            the identifier of the document to be created, it should
146
     *            be with revision
146 147
     * @param readNodes
147 148
     *            flag indicating whether the xmlnodes should be read
148 149
     */
149
    public DocumentImpl(String docid, boolean readNodes) throws McdbException
150
    public DocumentImpl(String accNum, boolean readNodes) throws McdbException
150 151
    {
151 152
        try {
152 153
            //this.conn = conn;
153
            this.docid = docid;
154
            this.docid = MetaCatUtil.getDocIdFromAccessionNumber(accNum);
155
            this.rev   = MetaCatUtil.getRevisionFromAccessionNumber(accNum);
154 156

  
155 157
            // Look up the document information
156
            getDocumentInfo(docid);
158
            getDocumentInfo(docid, rev);
157 159

  
158 160
            if (readNodes) {
159 161
                // Download all of the document nodes using a single SQL query
......
235 237
        this.docname = docName;
236 238
        this.doctype = docType;
237 239
        this.docid = docId;
238
        this.updatedVersion = newRevision;
240
        this.rev = (new Integer(newRevision)).intValue();
241
        //this.updatedVersion = newRevision;
239 242
        writeDocumentToDB(action, user, pub, catalogId, serverCode, createDate, updateDate);
240 243
    }
241 244

  
......
384 387
      DBConnection dbconn = null;
385 388
      PreparedStatement pstmt = null;
386 389
      int serialNumber = -1;
390
      int revision = (new Integer(rev)).intValue();
387 391
      String sqlDateString = dbAdapter.getDateTimeFunction();
388 392
      if (createDate == null)
389 393
      {
......
421 425
            sql.append(doctype).append("','");
422 426
            sql.append(user).append("','");
423 427
            sql.append(user).append("','");
424
            sql.append(serverCode).append("','");
425
            sql.append(rev).append("',");
428
            sql.append(serverCode).append("',");
429
            sql.append(revision).append(",");
426 430
            sql.append(createDate).append(",");
427 431
            sql.append(updateDate).append(",");
428 432
            sql.append("'0')");
......
433 437
            sql.append(user).append("', ");
434 438
            sql.append("server_location='");
435 439
            sql.append(serverCode).append("',");
436
            sql.append("rev='");
437
            sql.append(rev).append("',");
440
            sql.append("rev=");
441
            sql.append(revision).append(",");
438 442
            sql.append("date_updated=");
439 443
            sql.append(updateDate);
440 444
            sql.append(" where docid='");
......
541 545
        String action = null;
542 546
        MetaCatUtil.debugMessage("The user specifyRev: " + userSpecifyRev, 30);
543 547
        // Revision for this docid in current database
544
        int revInDataBase = getLatestRevisionNumber(docid);
548
        int revInDataBase =DBUtil.getLatestRevisionInDocumentTable(docid);
545 549
        MetaCatUtil.debugMessage("The rev in data base: " + revInDataBase, 30);
546 550
        // String to store the revision
547 551
        String rev = null;
......
1201 1205
            nodeRecordMap.put(nodeId, currentNode);
1202 1206
        }
1203 1207

  
1204
        DocumentIdentifier doc = null;
1205
        try {
1208
        //DocumentIdentifier doc = null;
1209
        //String doc = MetaCatUtil.getDocIdFromAccessionNumber(docid);
1210
        String doc = docid;
1211
        //System.out.println("The doc is-----!!!!!!!!!!!!!!!!!! "+doc);
1212
        /*try {
1206 1213
            doc = new DocumentIdentifier(docid);
1207 1214
        } catch (AccessionNumberException e){
1208 1215
            MetaCatUtil.debugMessage("AccessionNumber Exception while "
......
1210 1217
                                     + "in DocumentImpl.buildIndex for "
1211 1218
                                     + "document " + docid, 10);
1212 1219
            MetaCatUtil.debugMessage(e.getMessage(), 10);
1213
        }
1220
        }*/
1214 1221

  
1215 1222
        // Opening separate db connection for deleting and writing
1216 1223
        // XML Index -- be sure that it is all in one db transaction
......
1229 1236
            // Delete all the entries in xml_queryresult
1230 1237
            PreparedStatement pstmt = dbConn.prepareStatement(
1231 1238
                    "DELETE FROM xml_queryresult WHERE docid = ?");
1232
            pstmt.setString(1, doc.getIdentifier());
1239
            pstmt.setString(1, doc);
1233 1240
            pstmt.execute();
1234 1241
            pstmt.close();
1235 1242
            dbConn.increaseUsageCount(1);
......
1237 1244
            // Delete all the entries in xml_queryresult
1238 1245
            pstmt = dbConn.prepareStatement(
1239 1246
                    "DELETE FROM xml_path_index WHERE docid = ?");
1240
            pstmt.setString(1, doc.getIdentifier());
1247
            pstmt.setString(1, doc);
1241 1248
            pstmt.execute();
1242 1249
            pstmt.close();
1243 1250
            dbConn.increaseUsageCount(1);
1244

  
1251
            //System.out.println("after dleteing !!!!!!!!!!!1");
1245 1252
            // Step through all of the node records we were given
1246 1253
            // and build the new index and update the database
1247 1254
            it = nodeRecordLists.iterator();
1255
            //System.out.println("before iterator!!!!!!!!!!!1");
1248 1256
            while (it.hasNext()) {
1257
                //System.out.println("here!!!!!!!!!!!!!!!");
1249 1258
                NodeRecord currentNode = (NodeRecord) it.next();
1250 1259
                HashMap pathList = new HashMap();
1251 1260
                if (currentNode.nodetype.equals("ELEMENT") ||
......
1304 1313
                                            + " ) AND n.parentnodeid=i.nodeid AND"
1305 1314
                                            + " n.nodetype LIKE 'TEXT' and n.docid = ?");
1306 1315

  
1307
            pstmt.setString(1, doc.getIdentifier());
1316
            pstmt.setString(1, doc);
1308 1317

  
1309 1318
            pstmt.execute();
1310 1319
            rs = pstmt.getResultSet();
......
1312 1321
            int count = 0;
1313 1322

  
1314 1323
            while (rs.next()) {
1324
                //System.out.println("update xml_path_index!!!!!!!1");
1315 1325
                String docid = rs.getString(1);
1316 1326
                String nodedata = rs.getString(2);
1317 1327
                float nodedatanumerical = rs.getFloat(3);
......
1424 1434
     */
1425 1435
    private void deleteNodeIndex(DBConnection conn) throws SQLException
1426 1436
    {
1427
        String familyId = MetaCatUtil.getDocIdFromString(docid);
1437
        //String familyId = MetaCatUtil.getDocIdFromString(docid);
1438
        String familyId = docid;
1428 1439
        String sql = "DELETE FROM xml_index WHERE docid LIKE ?";
1429 1440
        MetaCatUtil.debugMessage(sql, 55);
1430 1441
        MetaCatUtil.debugMessage("SQL is: " + sql, 60);
......
1460 1471
                "parentnodeid) " + "VALUES (?, ?, ?, ?, ?)");
1461 1472
        // Increase usage count for the connection
1462 1473
        conn.increaseUsageCount(1);
1463
        String familyId = MetaCatUtil.getDocIdFromString(docid);
1474
        String familyId = docid;
1464 1475
        pstmt.setString(3, familyId);
1465 1476
        pstmt.setString(4, doctype);
1466 1477

  
......
1479 1490
        pstmt.close();
1480 1491
    }
1481 1492

  
1482
    private boolean isRevisionOnly(DocumentIdentifier docid) throws Exception
1493
    private boolean isRevisionOnly(String docid, int revision) throws Exception
1483 1494
    {
1484
        //System.out.println("inRevisionOnly");
1495
        //System.out.println("inRevisionOnly given "+ docid + "."+ revision);
1485 1496
        DBConnection dbconn = null;
1486 1497
        int serialNumber = -1;
1487 1498
        PreparedStatement pstmt = null;
1488
        String rev = docid.getRev();
1489
        String newid = docid.getIdentifier();
1499
        String newid = docid;
1490 1500

  
1491 1501
        try {
1492 1502
            dbconn = DBConnectionPool
......
1497 1507
            pstmt.execute();
1498 1508
            ResultSet rs = pstmt.getResultSet();
1499 1509
            boolean tablehasrows = rs.next();
1500
            if (rev.equals("newest") || rev.equals("all")) { return false; }
1501

  
1510
            //if (rev.equals("newest") || rev.equals("all")) { return false; }
1511
            
1502 1512
            if (tablehasrows) {
1503 1513
                int r = rs.getInt(1);
1514
                //System.out.println("the rev in xml_documents table is "+r);
1504 1515
                pstmt.close();
1505
                if (new Integer(rev).intValue() == r) { //the current revision
1516
                if (revision == r) { //the current revision
1506 1517
                                                        // in in xml_documents
1507 1518
                    //System.out.println("returning false");
1508 1519
                    return false;
1509
                } else if (new Integer(rev).intValue() < r) { //the current
1520
                } else if (revision < r) { //the current
1510 1521
                                                              // revision is in
1511 1522
                                                              // xml_revisions.
1512 1523
                    //System.out.println("returning true");
1513 1524
                    return true;
1514
                } else if (new Integer(rev).intValue() > r) { //error, rev
1525
                } else if (revision > r) { //error, rev
1515 1526
                                                              // cannot be
1516 1527
                                                              // greater than r
1517 1528
                throw new Exception(
1518 1529
                        "requested revision cannot be greater than "
1519 1530
                                + "the latest revision number."); }
1520 1531
            }
1532
            else
1533
            {
1534
                //System.out.println("in revision table branch -------");
1535
                // if we couldn't find it in xml_documents we 
1536
                // need to find it in xml_revision table
1537
                Vector revList = DBUtil.getRevListFromRevisionTable(docid);
1538
                for (int i=0; i<revList.size(); i++)
1539
                {
1540
                    Integer k = (Integer) revList.elementAt(i);
1541
                    //System.out.println("The rev in xml_revision talbe "+ k.toString());
1542
                }
1543
                
1544
                if (revList.contains(new Integer(revision)))
1545
                {
1546
                   return true;     
1547
                }
1548
            }
1521 1549
            // Get miss docid and rev, throw to McdDocNotFoundException
1522
            String missDocId = MetaCatUtil.getDocIdFromString(docid.toString());
1523
            String missRevision = MetaCatUtil.getRevisionStringFromString(docid
1524
                    .toString());
1550
            String missDocId = docid;
1551
            String missRevision = (new Integer(revision)).toString();
1525 1552
            throw new McdbDocNotFoundException("the requested docid '"
1526 1553
                    + docid.toString() + "' does not exist", missDocId,
1527 1554
                    missRevision);
......
1532 1559
        }//finally
1533 1560
    }
1534 1561

  
1535
    private void getDocumentInfo(String docid) throws McdbException,
1562
    /*private void getDocumentInfo(String docid) throws McdbException,
1536 1563
            AccessionNumberException, Exception
1537 1564
    {
1538 1565
        getDocumentInfo(new DocumentIdentifier(docid));
1539
    }
1566
    }*/
1540 1567

  
1541 1568
    /**
1542 1569
     * Look up the document type information from the database
......
1544 1571
     * @param docid
1545 1572
     *            the id of the document to look up
1546 1573
     */
1547
    private void getDocumentInfo(DocumentIdentifier docid)
1574
    private void getDocumentInfo(String docid, int revision)
1548 1575
            throws McdbException, Exception
1549 1576
    {
1550 1577
        DBConnection dbconn = null;
......
1553 1580
        String table = "xml_documents";
1554 1581

  
1555 1582
        try {
1556
            if (isRevisionOnly(docid)) { //pull the document from xml_revisions
1583
            if (isRevisionOnly(docid, revision)) { //pull the document from xml_revisions
1557 1584
                                         // instead of from xml_documents;
1558 1585
                table = "xml_revisions";
1559 1586
            }
......
1577 1604
            sql.append("date_created, date_updated, user_owner, user_updated,");
1578 1605
            sql.append(" server_location, public_access, rev");
1579 1606
            sql.append(" FROM ").append(table);
1580
            sql.append(" WHERE docid LIKE '").append(docid.getIdentifier());
1581
            sql.append("' and rev like '").append(docid.getRev()).append("'");
1607
            sql.append(" WHERE docid LIKE '").append(docid);
1608
            sql.append("' and rev like '").append(revision).append("'");
1582 1609

  
1583 1610
            pstmt = dbconn.prepareStatement(sql.toString());
1584 1611

  
......
1641 1668
                pstmt.close();
1642 1669
            }
1643 1670
        } catch (SQLException e) {
1644
            System.out.println("error in DocumentImpl.getDocumentInfo: "
1645
                    + e.getMessage());
1671
            MetaCatUtil.debugMessage("error in DocumentImpl.getDocumentInfo: "
1672
                    + e.getMessage(), 30);
1646 1673
            e.printStackTrace(System.out);
1647 1674
            throw new McdbException("Error accessing database connection in "
1648 1675
                    + "DocumentImpl.getDocumentInfo: ", e);
......
1660 1687

  
1661 1688
        if (this.docname == null) {
1662 1689
            throw new McdbDocNotFoundException(
1663
                "Document not found: " + docid, docid.getIdentifier(), docid
1664
                        .getRev());
1690
                "Document not found: " + docid, docid, (new Integer(revision)).toString());
1665 1691
        }
1666 1692
    }
1667 1693

  
......
1782 1808
        String nodedata = null;
1783 1809
        String quotechar = dbAdapter.getStringDelimiter();
1784 1810
        String table = "xml_nodes";
1785

  
1811
        //System.out.println("in getNodeREcorelist !!!!!!!!!!!for root node id "+rootnodeid);
1786 1812
        try {
1787
            if (isRevisionOnly(new DocumentIdentifier(docid))) { //pull the document from xml_revisions
1813
            if (isRevisionOnly(docid, rev)) { //pull the document from xml_revisions
1788 1814
                // instead of from xml_documents;
1789 1815
                table = "xml_nodes_revisions";
1816
                //System.out.println("in getNodeREcorelist !!!!!!!!!!!2");
1790 1817
            }
1791 1818
        }  catch (McdbDocNotFoundException notFound) {
1792 1819
            throw notFound;
......
1795 1822
            MetaCatUtil.debugMessage("error in DocumentImpl.getDocumentInfo: "
1796 1823
                    + e.getMessage(), 30);
1797 1824
        }
1798

  
1825
        //System.out.println("in getNodeREcorelist !!!!!!!!!!!3");
1799 1826
        try {
1800 1827
            dbconn = DBConnectionPool
1801 1828
                    .getDBConnection("DocumentImpl.getNodeRecordList");
......
1807 1834

  
1808 1835
            // Bind the values to the query
1809 1836
            pstmt.setLong(1, rootnodeid);
1810

  
1837
            //System.out.println("in getNodeREcorelist !!!!!!!!!!!4");
1811 1838
            pstmt.execute();
1812 1839
            ResultSet rs = pstmt.getResultSet();
1840
            //System.out.println("in getNodeREcorelist !!!!!!!!!!!5");
1813 1841
            boolean tableHasRows = rs.next();
1814 1842
            while (tableHasRows) {
1843
                //System.out.println("in getNodeREcorelist !!!!!!!!!!!6");
1815 1844
                nodeid = rs.getLong(1);
1816 1845
                parentnodeid = rs.getLong(2);
1817 1846
                nodeindex = rs.getLong(3);
......
1829 1858
                tableHasRows = rs.next();
1830 1859
            }
1831 1860
            pstmt.close();
1861
            //System.out.println("in getNodeREcorelist !!!!!!!!!!!7");
1832 1862

  
1833 1863
        } catch (SQLException e) {
1834 1864
            throw new McdbException("Error in DocumentImpl.getNodeRecordList "
......
1844 1874
                DBConnectionPool.returnDBConnection(dbconn, serialNumber);
1845 1875
            }
1846 1876
        }
1847

  
1877
        //System.out.println("in getNodeREcorelist !!!!!!!!!!!8");
1848 1878
        return nodeRecordList;
1849 1879

  
1850 1880
    }
......
1853 1883
    private void writeDocumentToDB(String action, String user, String pub,
1854 1884
            String catalogid, int serverCode, String createDate, String updateDate) throws SQLException, Exception
1855 1885
    {
1886
        //System.out.println("!!!!!!!!1write document to db  " +docid +"."+rev);
1856 1887
        String sysdate = dbAdapter.getDateTimeFunction();
1857 1888
        if (createDate == null)
1858 1889
        {
......
1911 1942
                 */
1912 1943
                pstmt.setInt(8, (new Integer(catalogid)).intValue());
1913 1944
                pstmt.setInt(9, serverCode);
1914
                pstmt.setInt(10, Integer.parseInt(updatedVersion));
1945
                pstmt.setInt(10, rev);
1915 1946
                
1916 1947
            } else if (action.equals("UPDATE")) {
1917
               
1948
                int thisrev = DBUtil.getLatestRevisionInDocumentTable(docid);
1949
                MetaCatUtil.debugMessage("this revsion is: " + thisrev, 40);
1918 1950
                // Save the old document publicaccessentry in a backup table
1919
                thisdoc = new DocumentImpl(docid, false);
1951
                String accNumber = docid+MetaCatUtil.getOption("accNumSeparator")+thisrev;
1952
                thisdoc = new DocumentImpl(accNumber, false);
1920 1953
                DocumentImpl.archiveDocRevision(connection, docid, user, thisdoc);
1921 1954
                MetaCatUtil.debugMessage("after archiveDoc", 30);
1922

  
1923
                int thisrev = thisdoc.getRev();
1924
                MetaCatUtil.debugMessage("this revsion is: " + thisrev, 40);
1925 1955
                //if the updated vesion is not greater than current one,
1926 1956
                //throw it into a exception
1927
                if (Integer.parseInt(updatedVersion) <= thisrev) {
1957
                if (rev <= thisrev) {
1928 1958
                    throw new Exception("Next revision number couldn't be less"
1929 1959
                            + " than or equal " + thisrev);
1930 1960
                } else {
1931 1961
                    //set the user specified revision
1932
                    thisrev = Integer.parseInt(updatedVersion);
1962
                    thisrev = rev;
1933 1963
                }
1934 1964
                MetaCatUtil.debugMessage("final revsion is: " + thisrev, 40);
1935 1965
                boolean useXMLIndex = (new Boolean(MetaCatUtil
......
2115 2145
                    + accnum);
2116 2146
            }
2117 2147

  
2118
            DocumentIdentifier id = new DocumentIdentifier(accnum);
2119
            String updaterev = id.getRev();
2148
            //DocumentIdentifier id = new DocumentIdentifier(accnum);
2149
            int revision = MetaCatUtil.getRevisionFromAccessionNumber(accnum);
2150
            String updaterev = (new Integer(revision)).toString();
2120 2151
            String server = MetacatReplication
2121 2152
                    .getServerNameForServerCode(serverCode);
2122 2153
            MetacatReplication.replLog("attempting to lock " + accnum);
......
2218 2249

  
2219 2250
            conn.setAutoCommit(false);
2220 2251
            parser.parse(new InputSource(xml));
2252
            //System.out.println("commit!!!!!!!!!!!!!!!!!111");
2221 2253
            conn.commit();
2222 2254
            conn.setAutoCommit(true);
2223 2255
            
......
2242 2274
        }
2243 2275

  
2244 2276
        // run access db base on relation table and access object
2277
        //System.out.println("the accnum will be write into access table "+accnum);
2245 2278
        runRelationAndAccessHandler(accnum, user, groups, serverCode);
2246 2279

  
2247 2280
        // Delete enteries from xml_queryresult for given docid if
......
2322 2355
        int userSpecifyRev = MetaCatUtil.getRevisionFromAccessionNumber(accnum);
2323 2356
        MetaCatUtil.debugMessage("The user specifyRev: " + userSpecifyRev, 30);
2324 2357
        // Revision for this docid in current database
2325
        int revInDataBase = getLatestRevisionNumber(docid);
2358
        int revInDataBase = DBUtil.getLatestRevisionInDocumentTable(docid);
2326 2359
        MetaCatUtil.debugMessage("The rev in data base: " + revInDataBase, 30);
2327 2360
        // String to store the revision
2328 2361
        String rev = (new Integer(userSpecifyRev)).toString();
......
2371 2404
            conn.commit();
2372 2405
            conn.setAutoCommit(true);
2373 2406
            
2407
            
2374 2408
            // write to xml_node complete. start the indexing thread.
2375 2409
            // this only for xml_documents
2376 2410
            DBSAXHandler dbx = (DBSAXHandler) parser.getContentHandler();
......
2416 2450
        } catch (Exception ee) {
2417 2451
        	conn.rollback();
2418 2452
            conn.setAutoCommit(true);
2453
            if (tableName.equals(REVISIONTABLE))
2454
            {
2455
                // because we couldn't register the table into xml_revsion
2456
                // we need to delete the nodes in xml_ndoes.
2457
                deleteXMLNodes(rootId);
2458
            }
2419 2459
            MetacatReplication.replErrorLog("Failed to " + "create access "
2420 2460
                    + "rule for package: " + accnum + " because "
2421 2461
                    + ee.getMessage());
......
2445 2485
            Vector packagedoctypes = new Vector();
2446 2486
            packagedoctypes = MetaCatUtil.getOptionList(packagedoctype);
2447 2487
            String docIdWithoutRev = MetaCatUtil.getDocIdFromString(accnumber);
2488
            int revision = MetaCatUtil.getRevisionFromAccessionNumber(accnumber);
2448 2489
            if (documenttype != null &&
2449 2490
                    packagedoctypes.contains(documenttype)) {
2450 2491
                dbconn = DBConnectionPool.getDBConnection(
......
2455 2496
                String aclid = RelationHandler.getAccessFileID(docIdWithoutRev);
2456 2497
                // if there are access file, write ACL for that package
2457 2498
                if (aclid != null) {
2458
                    runAccessControlList(dbconn, aclid, userName, group,
2499
                    runAccessControlList(dbconn, aclid, revision, userName, group,
2459 2500
                            servercode);
2460 2501
                }
2461 2502
                dbconn.commit();
......
2471 2512
                serialNumber = dbconn.getCheckOutSerialNumber();
2472 2513
                dbconn.setAutoCommit(false);
2473 2514
                // write ACL for the package
2474
                runAccessControlList(dbconn, docIdWithoutRev, userName, group,
2515
                runAccessControlList(dbconn, docIdWithoutRev, revision, userName, group,
2475 2516
                        servercode);
2476 2517
                dbconn.commit();
2477 2518
                dbconn.setAutoCommit(true);
......
2496 2537

  
2497 2538
    // It runs in xmlIndex thread. It writes ACL for a package.
2498 2539
    private static void runAccessControlList(DBConnection conn, String aclid,
2499
            String users, String[] group, int servercode) throws Exception
2540
            int rev, String users, String[] group, int servercode) throws Exception
2500 2541
    {
2501 2542
        // read the access file from xml_nodes
2502 2543
        // parse the access file and store the access info into xml_access
2503
        AccessControlList aclobj = new AccessControlList(conn, aclid, users,
2544
        AccessControlList aclobj = new AccessControlList(conn, aclid, rev, users,
2504 2545
                group, servercode);
2505 2546

  
2506 2547
    }
......
2931 2972

  
2932 2973
        try {
2933 2974
            if (doc == null) {
2934
                    doc = new DocumentImpl(docid);
2975
                String accNumber = docid + MetaCatUtil.getOption("accNumSeparator") +
2976
                DBUtil.getLatestRevisionInDocumentTable(docid);
2977
                    doc = new DocumentImpl(accNumber);
2935 2978
            }
2936 2979
            
2937 2980
            long rootNodeId = doc.getRootNodeID();
......
3114 3157
        }
3115 3158
    }
3116 3159

  
3117
    /**
3118
     * Get last revision number from database for a docid If couldn't find an
3119
     * entry, -1 will return The return value is integer because we want compare
3120
     * it to there new one
3121
     *
3122
     * @param docid
3123
     *            <sitecode>. <uniqueid>part of Accession Number
3124
     */
3125
    public static int getLatestRevisionNumber(String docId) throws SQLException
3126
    {
3127
        int rev = 1;
3128
        PreparedStatement pStmt = null;
3129
        DBConnection dbConn = null;
3130
        int serialNumber = -1;
3131
        // get rid of rev
3132
        docId = MetaCatUtil.getDocIdFromString(docId);
3133
        try {
3134
            //check out DBConnection
3135
            dbConn = DBConnectionPool
3136
                    .getDBConnection("DocumentImpl.getLatestRevisionNumber");
3137
            serialNumber = dbConn.getCheckOutSerialNumber();
3160
   
3138 3161

  
3139
            pStmt = dbConn
3140
                    .prepareStatement("SELECT rev FROM xml_documents WHERE docid='"
3141
                            + docId + "'");
3142
            pStmt.execute();
3143

  
3144
            ResultSet rs = pStmt.getResultSet();
3145
            boolean hasRow = rs.next();
3146
            if (hasRow) {
3147
                rev = rs.getInt(1);
3148
                pStmt.close();
3149
            } else {
3150
                rev = -1;
3151
                pStmt.close();
3152
            }
3153
        }//try
3154
        finally {
3155
            try {
3156
                pStmt.close();
3157
            } catch (Exception ee) {
3158
                MetaCatUtil.debugMessage("Error in DocumentImpl."
3159
                        + "getLatestRevisionNumber: " + ee.getMessage(), 50);
3160
            } finally {
3161
                DBConnectionPool.returnDBConnection(dbConn, serialNumber);
3162
            }
3163
        }//finally
3164

  
3165
        return rev;
3166
    }//getLatestRevisionNumber
3167

  
3168 3162
    /**
3169 3163
     * Get server location form database for a accNum
3170 3164
     *
......
3471 3465
            if (action.equals("READ")) {
3472 3466
                DocumentImpl xmldoc = new DocumentImpl(docid);
3473 3467
                if (useOldReadAlgorithm) {
3474
                    System.out.println(xmldoc.readUsingSlowAlgorithm());
3468
                    MetaCatUtil.debugMessage(xmldoc.readUsingSlowAlgorithm(), 10);
3475 3469
                } else {
3476 3470
                    xmldoc.toXml(new PrintWriter(System.out), null, null, true);
3477 3471
                }
3478 3472
            } else if (action.equals("DELETE")) {
3479 3473
                DocumentImpl.delete(docid, null, null,null);
3480
                System.out.println("Document deleted: " + docid);
3474
                //System.out.println("Document deleted: " + docid);
3481 3475
            } else {
3482 3476
                /*
3483 3477
                 * String newdocid = DocumentImpl.write(dbconn, filename, null,
......
3498 3492
            double insertTime = (stopTime - connTime) / 1000;
3499 3493
            double executionTime = (stopTime - startTime) / 1000;
3500 3494
            if (showRuntime) {
3501
                System.out.println("\n\nTotal Execution time was: "
3502
                        + executionTime + " seconds.");
3503
                System.out.println("Time to open DB connection was: "
3504
                        + dbOpenTime + " seconds.");
3505
                System.out.println("Time to insert document was: " + insertTime
3506
                        + " seconds.");
3495
                MetaCatUtil.debugMessage("\n\nTotal Execution time was: "
3496
                        + executionTime + " seconds.", 10);
3497
                MetaCatUtil.debugMessage("Time to open DB connection was: "
3498
                        + dbOpenTime + " seconds.", 10);
3499
                MetaCatUtil.debugMessage("Time to insert document was: " + insertTime
3500
                        + " seconds.", 10);
3507 3501
            }
3508 3502
            dbconn.close();
3509 3503
        } catch (McdbException me) {
3510 3504
            me.toXml(new PrintWriter(System.err));
3511 3505
        } catch (AccessionNumberException ane) {
3512
            System.out.println(ane.getMessage());
3506
            System.err.println(ane.getMessage());
3513 3507
        } catch (Exception e) {
3514 3508
            System.err.println("EXCEPTION HANDLING REQUIRED");
3515 3509
            System.err.println(e.getMessage());
......
3640 3634
            DBConnectionPool.returnDBConnection(dbconn, serialNumber);
3641 3635
        }
3642 3636
    }
3637
    
3638
    /*
3639
     * This method will delete the xml_nodes table for a given root id
3640
     * This method will be called in the time_replication for revision table
3641
     * In revision replication, xml first will insert into xml_nodes, then
3642
     * move to xml_nodes_revision and register into xml_revsion table.
3643
     * if in the second step some error happend, we need to delete the
3644
     * node in xml_nodes table as roll back
3645
     */
3646
    static private void deleteXMLNodes(long rootId) throws Exception
3647
    {
3648
        DBConnection dbconn = null;
3649
        int serialNumber = -1;
3650
        //MetaCatUtil util = new MetaCatUtil();
3651
        AccessionNumber ac;
3652
        PreparedStatement pstmt = null;
3653
        String sql = "DELETE FROM xml_documents WHERE docid = "+ rootId;
3654
        try 
3655
        {
3656
            //dbconn = util.openDBConnection();
3657
            dbconn = DBConnectionPool.getDBConnection(
3658
                    "DeletedDocumentImpl.deletXMLNode");
3659
            serialNumber = dbconn.getCheckOutSerialNumber();
3660
            pstmt = dbconn.prepareStatement(sql);
3661
            // Increase dbconnection usage count
3662
            dbconn.increaseUsageCount(1);
3663
            pstmt.close();
3664
            dbconn.close();
3665
        } 
3666
        finally 
3667
        {
3668
            
3669
            DBConnectionPool.returnDBConnection(dbconn, serialNumber);
3670
        }
3671
    }
3643 3672
}
src/edu/ucsb/nceas/metacat/ReplicationHandler.java
269 269

  
270 270
  /* Handle replicate single xml document*/
271 271
  private void handleSingleXMLDocument(String remoteserver, String actions,
272
                                       String docId, String tableName)
272
                                       String accNumber, String tableName)
273 273
               throws Exception
274 274
  {
275 275
    DBConnection dbConn = null;
......
282 282
      serialNumber=dbConn.getCheckOutSerialNumber();
283 283
      //if the document needs to be updated or inserted, this is executed
284 284
      String readDocURLString = "https://" + remoteserver + "?server="+
285
              util.getLocalReplicationServerName()+"&action=read&docid="+docId;
285
              util.getLocalReplicationServerName()+"&action=read&docid="+accNumber;
286 286
      readDocURLString = MetaCatUtil.replaceWhiteSpaceForURL(readDocURLString);
287 287
      URL u = new URL(readDocURLString);
288 288

  
......
301 301
      XMLReader docinfoParser = initParser(dih);
302 302
      String docInfoURLStr = "https://" + remoteserver +
303 303
                       "?server="+util.getLocalReplicationServerName()+
304
                       "&action=getdocumentinfo&docid="+docId;
304
                       "&action=getdocumentinfo&docid="+accNumber;
305 305
      docInfoURLStr = MetaCatUtil.replaceWhiteSpaceForURL(docInfoURLStr);
306 306
      URL docinfoUrl = new URL(docInfoURLStr);
307 307
      MetaCatUtil.debugMessage("Sending message: " +
......
315 315
      String createdDate = (String)docinfoHash.get("date_created");
316 316
      String updatedDate = (String)docinfoHash.get("date_updated");
317 317
      //docid should include rev number too
318
      String accnum=docId+util.getOption("accNumSeparator")+
319
                                              (String)docinfoHash.get("rev");
320
      MetaCatUtil.debugMessage("docid in repl: "+accnum, 45);
318
      /*String accnum=docId+util.getOption("accNumSeparator")+
319
                                              (String)docinfoHash.get("rev");*/
320
      MetaCatUtil.debugMessage("docid in repl: "+accNumber, 45);
321 321
      String docType = (String)docinfoHash.get("doctype");
322 322
      MetaCatUtil.debugMessage("doctype in repl: "+docType, 45);
323 323

  
......
342 342
                              (String)docinfoHash.get("public_access"),
343 343
                              null,  /* the dtd text */
344 344
                              actions,
345
                              accnum,
345
                              accNumber,
346 346
                              (String)docinfoHash.get("user_owner"),
347 347
                              null, /* null for groups[] */
348 348
                              docHomeServer,
349 349
                              remoteserver, tableName, true,// true is for time replication 
350 350
                              createdDate,
351 351
                              updatedDate);
352
      MetaCatUtil.debugMessage("Successfully replicated doc " + accnum, 35);
353
      MetacatReplication.replLog("wrote doc " + accnum + " from " +
352
      MetaCatUtil.debugMessage("Successfully replicated doc " + accNumber, 35);
353
      MetacatReplication.replLog("wrote doc " + accNumber + " from " +
354 354
                                         remoteserver);
355 355

  
356 356
    }//try
357 357
    catch(Exception e)
358 358
    {
359
      MetacatReplication.replErrorLog("Failed to write doc " + docId +
359
      MetacatReplication.replErrorLog("Failed to write doc " + accNumber +
360 360
                                      " into db because " +e.getMessage());
361
      MetaCatUtil.debugMessage("Failed to write doc " + docId +
361
      MetaCatUtil.debugMessage("Failed to write doc " + accNumber +
362 362
                                      " into db because " +e.getMessage(), 30);
363 363
      throw e;
364 364
    }
......
373 373

  
374 374
  /* Handle replicate single xml document*/
375 375
  private void handleSingleDataFile(String remoteserver, String actions,
376
                                    String docId, String tableName)
376
                                    String accNumber, String tableName)
377 377
               throws Exception
378 378
  {
379
    MetaCatUtil.debugMessage("Try to replicate data file: "+docId, 40);
379
    MetaCatUtil.debugMessage("Try to replicate data file: "+accNumber, 40);
380 380
    DBConnection dbConn = null;
381 381
    int serialNumber = -1;
382 382
    try
......
390 390
      XMLReader docinfoParser = initParser(dih);
391 391
      String docInfoURLString = "https://" + remoteserver +
392 392
                  "?server="+util.getLocalReplicationServerName()+
393
                  "&action=getdocumentinfo&docid="+docId;
393
                  "&action=getdocumentinfo&docid="+accNumber;
394 394
      docInfoURLString = MetaCatUtil.replaceWhiteSpaceForURL(docInfoURLString);
395 395
      URL docinfoUrl = new URL(docInfoURLString);
396 396

  
......
409 409
      String createdDate = (String)docinfoHash.get("date_created");
410 410
      String updatedDate = (String)docinfoHash.get("date_updated");
411 411
      //docid should include rev number too
412
      String accnum=docId+util.getOption("accNumSeparator")+
413
                                              (String)docinfoHash.get("rev");
412
      /*String accnum=docId+util.getOption("accNumSeparator")+
413
                                              (String)docinfoHash.get("rev");*/
414 414

  
415 415

  
416 416
      String datafilePath = util.getOption("datafilepath");
417 417
      // Get data file content
418 418
      String readDataURLString = "https://" + remoteserver + "?server="+
419 419
                                        util.getLocalReplicationServerName()+
420
                                            "&action=readdata&docid="+accnum;
420
                                            "&action=readdata&docid="+accNumber;
421 421
      readDataURLString = MetaCatUtil.replaceWhiteSpaceForURL(readDataURLString);
422 422
      URL u = new URL(readDataURLString);
423 423
      InputStream input = u.openStream();
......
428 428
        DocumentImpl.writeDataFileInReplication(input,
429 429
                                                datafilePath,
430 430
                                                docName,docType,
431
                                                accnum, user,
431
                                                accNumber, user,
432 432
                                                docHomeServer,
433 433
                                                remoteserver,
434 434
                                                tableName,
......
436 436
                                                createdDate,
437 437
                                                updatedDate);
438 438
                                         
439
        MetaCatUtil.debugMessage("Successfully to write datafile " + docId, 30);
440
        MetacatReplication.replLog("wrote datafile " + accnum + " from " +
439
        MetaCatUtil.debugMessage("Successfully to write datafile " + accNumber, 30);
440
        MetacatReplication.replLog("wrote datafile " + accNumber + " from " +
441 441
                                    remoteserver);
442 442
      }//if
443 443
      else
444 444
      {
445
         MetaCatUtil.debugMessage("Couldn't open the data file: " + accnum, 30);
446
         throw new Exception("Couldn't open the data file: " + accnum);
445
         MetaCatUtil.debugMessage("Couldn't open the data file: " + accNumber, 30);
446
         throw new Exception("Couldn't open the data file: " + accNumber);
447 447
      }//else
448 448

  
449 449
    }//try
450 450
    catch(Exception e)
451 451
    {
452
      MetacatReplication.replErrorLog("Failed to try wrote datafile " + docId +
452
      MetacatReplication.replErrorLog("Failed to try wrote datafile " + accNumber +
453 453
                                      " because " +e.getMessage());
454
      MetaCatUtil.debugMessage("Failed to try wrote datafile " + docId +
454
      MetaCatUtil.debugMessage("Failed to try wrote datafile " + accNumber +
455 455
                                      " because " +e.getMessage(), 30);
456 456
      throw e;
457 457
    }
......
991 991
       boolean flag = false;
992 992
       try
993 993
       {
994
         localrev = DocumentImpl.getLatestRevisionNumber(docid);
994
         localrev = DBUtil.getLatestRevisionInDocumentTable(docid);
995 995
       }
996 996
       catch (SQLException e)
997 997
       {
......
1027 1027
           flag = true;
1028 1028
         }
1029 1029
       }
1030

  
1030
       
1031
       String accNumber = docid + MetaCatUtil.getOption("accNumSeparator") + rev;
1031 1032
       // this is non-data file
1032 1033
       if(flag && !dataFile)
1033 1034
       {
1034 1035
         try
1035 1036
         {
1036
           handleSingleXMLDocument(remoteServer, action, docid, DocumentImpl.DOCUMENTTABLE);
1037
           handleSingleXMLDocument(remoteServer, action, accNumber, DocumentImpl.DOCUMENTTABLE);
1037 1038
         }
1038 1039
         catch(Exception e)
1039 1040
         {
......
1047 1048
       {
1048 1049
         try
1049 1050
         {
1050
           handleSingleDataFile(remoteServer, action, docid, DocumentImpl.DOCUMENTTABLE);
1051
           handleSingleDataFile(remoteServer, action, accNumber, DocumentImpl.DOCUMENTTABLE);
1051 1052
         }
1052 1053
         catch(Exception e)
1053 1054
         {
......
1065 1066
                                        throws Exception
1066 1067
   {
1067 1068
       // compare the update rev and local rev to see what need happen
1069
       System.out.println("In handle repliation revsion table");
1070
       System.out.println("the docid is "+ docid);
1071
       System.out.println("The rev is "+rev);
1068 1072
       Vector localrev = null;
1069 1073
       String action = "INSERT";
1070 1074
       boolean flag = false;
......
1088 1092
       {
1089 1093
           flag = true;    
1090 1094
       }
1091

  
1095
     
1096
       String accNumber = docid + MetaCatUtil.getOption("accNumSeparator") + rev;
1092 1097
       // this is non-data file
1093 1098
       if(flag && !dataFile)
1094 1099
       {
1095 1100
         try
1096 1101
         {
1097
           handleSingleXMLDocument(remoteServer, action, docid, DocumentImpl.REVISIONTABLE);
1102
           
1103
           handleSingleXMLDocument(remoteServer, action, accNumber, DocumentImpl.REVISIONTABLE);
1098 1104
         }
1099 1105
         catch(Exception e)
1100 1106
         {
......
1108 1114
       {
1109 1115
         try
1110 1116
         {
1111
           handleSingleDataFile(remoteServer, action, docid, DocumentImpl.REVISIONTABLE);
1117
           handleSingleDataFile(remoteServer, action, accNumber, DocumentImpl.REVISIONTABLE);
1112 1118
         }
1113 1119
         catch(Exception e)
1114 1120
         {
src/edu/ucsb/nceas/metacat/DBUtil.java
622 622

  
623 623
      return list;
624 624
  }//getLatestRevisionNumber
625
  
626
  /**
627
   * Get last revision number from database for a docid If couldn't find an
628
   * entry, -1 will return The return value is integer because we want compare
629
   * it to there new one
630
   *
631
   * @param docid
632
   *            <sitecode>. <uniqueid>part of Accession Number
633
   */
634
  public static int getLatestRevisionInDocumentTable(String docIdWithoutRev) throws SQLException
635
  {
636
      int rev = 1;
637
      PreparedStatement pStmt = null;
638
      DBConnection dbConn = null;
639
      int serialNumber = -1;
640
      try {
641
          //check out DBConnection
642
          dbConn = DBConnectionPool
643
                  .getDBConnection("DBUtil.getLatestRevisionInDocumentTable");
644
          serialNumber = dbConn.getCheckOutSerialNumber();
645

  
646
          pStmt = dbConn
647
                  .prepareStatement("SELECT rev FROM xml_documents WHERE docid='"
648
                          + docIdWithoutRev + "'");
649
          pStmt.execute();
650

  
651
          ResultSet rs = pStmt.getResultSet();
652
          boolean hasRow = rs.next();
653
          if (hasRow) {
654
              rev = rs.getInt(1);
655
              pStmt.close();
656
          } else {
657
              rev = -1;
658
              pStmt.close();
659
          }
660
      }//try
661
      finally {
662
          try {
663
              pStmt.close();
664
          } catch (Exception ee) {
665
              MetaCatUtil.debugMessage("Error in DBUtil."
666
                      + "getLatestRevisionInDocumentTable: " + ee.getMessage(), 50);
667
          } finally {
668
              DBConnectionPool.returnDBConnection(dbConn, serialNumber);
669
          }
670
      }//finally
671

  
672
      return rev;
673
  }//getLatestRevisionNumber
625 674
   
626 675
}
src/edu/ucsb/nceas/metacat/DBQuery.java
481 481
                    // xmldoc = new DocumentImpl(dbconn, packageDocid);
482 482
                    //  thus use the following to get the doc info only
483 483
                    //  xmldoc = new DocumentImpl(dbconn);
484
                    xmldoc = new DocumentImpl(packageDocid, false);
484
                    String accNumber = packageDocid + MetaCatUtil.getOption("accNumSeparator") +
485
                    DBUtil.getLatestRevisionInDocumentTable(packageDocid);
486
                    xmldoc = new DocumentImpl(accNumber, false);
485 487
                    if (xmldoc == null)
486 488
                    {
487 489
                       MetaCatUtil.debugMessage("Document was null for: "
......
1621 1623
     *
1622 1624
     * @param dataPackageDocid, the string in docId field of xml_relation table
1623 1625
     */
1624
    private Vector getOldVersionDocidListForDataPackage(String dataPackageDocid)
1626
    private Vector getOldVersionDocidListForDataPackage(String dataPackageDocidWithRev)
1625 1627
    {
1626 1628

  
1627 1629
        Vector docIdList = new Vector();//return value
......
1629 1631
        String xml = null;
1630 1632

  
1631 1633
        // Check the parameter
1632
        if (dataPackageDocid == null || dataPackageDocid.equals("")) { return docIdList; }//if
1634
        if (dataPackageDocidWithRev == null || dataPackageDocidWithRev.equals("")) { return docIdList; }//if
1633 1635

  
1634 1636
        try {
1635 1637
            //initial a documentImpl object
1636
            DocumentImpl packageDocument = new DocumentImpl(dataPackageDocid);
1638
            DocumentImpl packageDocument = new DocumentImpl(dataPackageDocidWithRev);
1637 1639
            //transfer to documentImpl object to string
1638 1640
            xml = packageDocument.toString();
1639 1641

  
......
2045 2047
    /**
2046 2048
     * put a data packadge into a zip output stream
2047 2049
     *
2048
     * @param docId, which the user want to put into zip output stream
2050
     * @param docId, which the user want to put into zip output stream,it has version
2049 2051
     * @param out, a servletoutput stream which the zip output stream will be
2050 2052
     *            put
2051 2053
     * @param user, the username of the user
......
2092 2094
                throw e;
2093 2095
            }
2094 2096

  
2095
            docImpls = new DocumentImpl(docId);
2097
            docImpls = new DocumentImpl(docIdString);
2096 2098
            //checking if the user has the permission to read the documents
2097 2099
            if (DocumentImpl.hasReadPermission(user, groups, docImpls
2098 2100
                    .getDocID())) {
src/edu/ucsb/nceas/metacat/RemoteDocument.java
203 203
      throw new Exception ("Couldn't find a dataset docid for the required id");
204 204
    }
205 205
    // Create a documentImpl object
206
    document = new DocumentImpl(givenDocId, false);
206
    String accNumber = givenDocId + MetaCatUtil.getOption("accNumSeparator") +
207
    DBUtil.getLatestRevisionInDocumentTable(givenDocId);
208
    document = new DocumentImpl(accNumber, false);
207 209
    // get the replication home server (it come from xml_replication table)
208 210
    replicationDocHomeServer = document.getDocHomeServer();
209 211
    
src/edu/ucsb/nceas/metacat/AccessControlList.java
118 118
   * @param serverCode the serverid from xml_replication on which this document
119 119
   *        resides.
120 120
   */
121
  public AccessControlList(DBConnection conn, String aclid, //Reader acl,
121
  public AccessControlList(DBConnection conn, String aclid, int rev,//Reader acl,
122 122
                           String user, String[] groups, int serverCode)
123 123
                  throws SAXException, IOException, McdbException
124 124
  {
......
143 143
    this.serverCode = serverCode;
144 144
    
145 145
    // read the access file from db connection
146
    DocumentImpl acldoc = new DocumentImpl(aclid);
146
    DocumentImpl acldoc = new DocumentImpl(aclid+sep+rev);
147 147
    String acl = acldoc.toString();
148 148
    this.rev = acldoc.getRev();
149 149

  
......
326 326
          for (int i=0; i < aclObjects.size(); i++) {
327 327
            // docid of the current object
328 328
            String docid = (String)aclObjects.elementAt(i); 
329
            DocumentIdentifier docID = new DocumentIdentifier(docid);
330
            docid = docID.getIdentifier();
329
            //DocumentIdentifier docID = new DocumentIdentifier(docid);
330
            //docid = docID.getIdentifier();
331
            // the docid get here has revision number, so we need to 
332
            // remove it.
333
            //docid = MetaCatUtil.getDocIdFromAccessionNumber(docid);
334
            //System.out.println("The docid insert is!!!!!!!!!! "+ docid);
331 335
            insertPermissions(docid,leavingTagName);
332 336
          }
333 337
          
......
429 433
      ResultSet rs = pstmt.getResultSet();
430 434
      boolean hasRows = rs.next();
431 435
      while (hasRows) {
432
        aclObjects.addElement(rs.getString(1));
436
        String object = rs.getString(1);
437
        //System.out.println("add acl object into vector !!!!!!!!!!!!!!!!!"+object);
438
        aclObjects.addElement(object);
433 439
        hasRows = rs.next();
434 440
      }//whil
435 441
    }

Also available in: Unified diff