Project

General

Profile

« Previous | Next » 

Revision 2524

Added by sgarg over 19 years ago

Modified code to enter/remove data from xml_path_index and enter data into xml_nodes_revision when action=insert, update and delete are performed.

View differences:

src/edu/ucsb/nceas/metacat/DocumentImpl.java
1164 1164
     */
1165 1165
    public void buildIndex() throws McdbException
1166 1166
    {
1167
        MetaCatUtil util = new MetaCatUtil();
1168 1167
        TreeSet nodeRecordLists = getNodeRecordList(rootnodeid);
1169
        Stack openElements = new Stack();
1170 1168
        boolean atRootElement = true;
1171 1169
        long rootNodeId = -1;
1172 1170

  
......
1203 1201
            pstmt.close();
1204 1202
            dbConn.increaseUsageCount(1);
1205 1203

  
1206
	    
1204
            // Delete all the entries in xml_queryresult
1205
            pstmt = dbConn.prepareStatement(
1206
                    "DELETE FROM xml_path_index WHERE docid = ?");
1207
            pstmt.setString(1, docid);
1208
            pstmt.execute();
1209
            pstmt.close();
1210
            dbConn.increaseUsageCount(1);
1211

  
1207 1212
            // Step through all of the node records we were given
1208 1213
            // and build the new index and update the database
1209 1214
            it = nodeRecordLists.iterator();
......
1223 1228
                    updateNodeIndex(dbConn, pathList);
1224 1229
                }
1225 1230
            }
1226
            dbConn.commit();
1227
        } catch (SQLException e) {
1228
            MetaCatUtil.debugMessage(
1229
                "SQL Exception while inserting path index in " +
1230
                "DocumentImpl.buildIndex for document " + docid, 10);
1231
      		MetaCatUtil.debugMessage(e.getMessage(), 10);
1232
            e.printStackTrace();
1233
            try {
1234
                dbConn.rollback();
1235
            } catch (SQLException sqle) {
1236
            	MetaCatUtil.debugMessage(
1237
					"Error while rolling back commit in DocumentImpl.buildIndex"
1238
                    + "\n" + sqle.getMessage(), 10);
1239
            }
1240
        } finally {
1241
			DBConnectionPool.returnDBConnection(dbConn, serialNumber);
1242
        }
1231

  
1232
            ResultSet rs = null;
1233
            PreparedStatement pstmt1 = null;
1234

  
1235
            for (int i = 0; i < MetaCatUtil.pathsForIndexing.size(); i++) {
1236
              MetaCatUtil.formattedDebugMessage("Indexing the path '"
1237
                                                + (String) MetaCatUtil
1238
                                                .pathsForIndexing.elementAt(i)
1239
                                                + "' for docid " + docid
1240
                                                + "... ", 30, false, true);
1241

  
1242
              pstmt = dbConn.prepareStatement("SELECT DISTINCT n.docid, "
1243
                                              + "n.nodedata, n.nodedatanumerical, n.parentnodeid"
1244
                                              + " FROM xml_nodes n, xml_index i WHERE"
1245
                                              + " i.path = ? and n.parentnodeid=i.nodeid and"
1246
                                              + " n.nodetype LIKE 'TEXT' and n.docid = ?");
1247
              pstmt.setString(1, (String) MetaCatUtil.
1248
                              pathsForIndexing.elementAt(i));
1249
              pstmt.setString(2, docid);
1250
              pstmt.execute();
1251
              rs = pstmt.getResultSet();
1252

  
1253
              int count = 0;
1254
              MetaCatUtil.debugMessage("Executed the select statement for: "
1255
                                       + (String) MetaCatUtil.pathsForIndexing
1256
                                       .elementAt(i), 60);
1257

  
1258
              try {
1259
                  while (rs.next()) {
1260
                      String docid = rs.getString(1);
1261
                      String nodedata = rs.getString(2);
1262
                      float nodedatanumerical = rs.getFloat(3);
1263
                      int parentnodeid = rs.getInt(4);
1264

  
1265
                      if (!nodedata.trim().equals("")) {
1266
                          pstmt1 = dbConn.prepareStatement("INSERT INTO "
1267
                                      + "xml_path_index (docid, path, nodedata, "
1268
                                      + "nodedatanumerical, parentnodeid)"
1269
                                      + " VALUES (?, ?, ?, ?, ?)");
1270

  
1271
                          pstmt1.setString(1, docid);
1272
                          pstmt1.setString(2, (String) MetaCatUtil.
1273
                                           pathsForIndexing.elementAt(i));
1274
                          pstmt1.setString(3, nodedata);
1275
                          pstmt1.setFloat(4, nodedatanumerical);
1276
                          pstmt1.setFloat(5, parentnodeid);
1277

  
1278
                          pstmt1.execute();
1279
                          pstmt1.close();
1280

  
1281
                          count++;
1282

  
1283
                      }
1284
                  }
1285
              }
1286
              catch (Exception e) {
1287
                  System.out.println("Exception:" + e.getMessage());
1288
                  e.printStackTrace();
1289
              }
1290

  
1291
              rs.close();
1292
              pstmt.close();
1293
              dbConn.increaseUsageCount(1);
1294

  
1295
              MetaCatUtil.formattedDebugMessage("indexed " + count
1296
                                       + " records from xml_nodes",
1297
                                       20, true, false);
1298
          }
1299
          dbConn.commit();
1300
      } catch (SQLException e) {
1301
          MetaCatUtil.debugMessage("SQL Exception while inserting path index "
1302
                                   + "in DocumentImpl.buildIndex for "
1303
                                   + "document " + docid, 10);
1304
          MetaCatUtil.debugMessage(e.getMessage(), 10);
1305
          e.printStackTrace();
1306
          try {
1307
              dbConn.rollback();
1308
          } catch (SQLException sqle) {
1309
              MetaCatUtil.debugMessage("Error while rolling back "
1310
                                       + "commit in DocumentImpl"
1311
                                       + ".buildIndex" + "\n"
1312
                                       + sqle.getMessage(), 10);
1313
          }
1314
      } finally {
1315
          DBConnectionPool.returnDBConnection(dbConn, serialNumber);
1316
      }
1243 1317
    }
1244 1318

  
1245 1319
    /**
......
1365 1439
        PreparedStatement pstmt = null;
1366 1440
        String rev = docid.getRev();
1367 1441
        String newid = docid.getIdentifier();
1442

  
1368 1443
        try {
1369 1444
            dbconn = DBConnectionPool
1370 1445
                    .getDBConnection("DocumentImpl.isRevisionOnly");
......
1658 1733
        String nodeprefix = null;
1659 1734
        String nodedata = null;
1660 1735
        String quotechar = dbAdapter.getStringDelimiter();
1736
        String table = "xml_nodes";
1661 1737

  
1662 1738
        try {
1739
            if (isRevisionOnly(new DocumentIdentifier(docid))) { //pull the document from xml_revisions
1740
                // instead of from xml_documents;
1741
                table = "xml_nodes_revisions";
1742
            }
1743
        }  catch (McdbDocNotFoundException notFound) {
1744
            throw notFound;
1745
        } catch (Exception e) {
1746

  
1747
            MetaCatUtil.debugMessage("error in DocumentImpl.getDocumentInfo: "
1748
                    + e.getMessage(), 30);
1749
        }
1750

  
1751
        try {
1663 1752
            dbconn = DBConnectionPool
1664 1753
                    .getDBConnection("DocumentImpl.getNodeRecordList");
1665 1754
            serialNumber = dbconn.getCheckOutSerialNumber();
1666 1755
            pstmt = dbconn
1667 1756
                    .prepareStatement("SELECT nodeid,parentnodeid,nodeindex, "
1668 1757
                            + "nodetype,nodename,nodeprefix,nodedata "
1669
                            + "FROM xml_nodes WHERE rootnodeid = ?");
1758
                            + "FROM " + table + " WHERE rootnodeid = ?");
1670 1759

  
1671 1760
            // Bind the values to the query
1672 1761
            pstmt.setLong(1, rootnodeid);
......
1717 1806
            String catalogid, int serverCode) throws SQLException, Exception
1718 1807
    {
1719 1808
        String sysdate = dbAdapter.getDateTimeFunction();
1809
        DocumentImpl thisdoc = null;
1720 1810

  
1721 1811
        try {
1722 1812
            PreparedStatement pstmt = null;
......
1761 1851
            } else if (action.equals("UPDATE")) {
1762 1852

  
1763 1853
                // Save the old document publicaccessentry in a backup table
1764
                DocumentImpl.archiveDocRevision(connection, docid, user);
1854
                thisdoc = new DocumentImpl(docid, false);
1855
                DocumentImpl.archiveDocRevision(connection, docid, user, thisdoc);
1765 1856
                MetaCatUtil.debugMessage("after archiveDoc", 40);
1766
                DocumentImpl thisdoc = new DocumentImpl(docid, false);
1857

  
1767 1858
                int thisrev = thisdoc.getRev();
1768 1859
                MetaCatUtil.debugMessage("this revsion is: " + thisrev, 40);
1769 1860
                //if the updated vesion is not greater than current one,
......
1828 1919

  
1829 1920
            // Do the insertion
1830 1921
            pstmt.execute();
1831

  
1832 1922
            pstmt.close();
1833 1923

  
1924
            if(action.equals("UPDATE")){
1925
                // Delete the old nodes in xml_nodes table...
1926
                pstmt = connection.prepareStatement("DELETE FROM xml_nodes "
1927
                                                + "WHERE rootnodeid = ?");
1928
                // Increase dbconnection usage count
1929
                connection.increaseUsageCount(1);
1930
                // Bind the values to the query and execute it
1931
                pstmt.setLong(1, thisdoc.getRootNodeID());
1932
                pstmt.execute();
1933
                pstmt.close();
1934
            }
1935

  
1834 1936
        } catch (SQLException sqle) {
1835 1937
            throw sqle;
1836 1938
        } catch (Exception e) {
......
2373 2475
            ResultSet rs = pstmt.getResultSet();
2374 2476
            if(!rs.next()){
2375 2477
                rs.close();
2478
                pstmt.close();
2479
                conn.increaseUsageCount(1);
2376 2480
                throw new Exception("Docid " + accnum + " does not exsist. "
2377 2481
                                    + "Please check that you have specified the"
2378 2482
                                    + " revision number of the document also.");
......
2417 2521

  
2418 2522
            conn.setAutoCommit(false);
2419 2523
            // Copy the record to the xml_revisions table
2420
            DocumentImpl.archiveDocRevision(conn, docid, user);
2524
            DocumentImpl.archiveDocRevision(conn, docid, user, null);
2421 2525

  
2422 2526
            // Now delete it from the xml_index table
2423 2527
            boolean useXMLIndex = (new Boolean(MetaCatUtil
2424 2528
                    .getOption("usexmlindex"))).booleanValue();
2425 2529

  
2530

  
2531

  
2426 2532
           //if (useXMLIndex) {
2427 2533
            pstmt = conn
2428 2534
                    .prepareStatement("DELETE FROM xml_index WHERE docid = ?");
......
2459 2565
            pstmt = conn.prepareStatement(
2460 2566
                    "DELETE FROM xml_relation WHERE docid = ?");
2461 2567
            //increase usage count
2568
            pstmt.setString(1, docid);
2569
            pstmt.execute();
2570
            pstmt.close();
2462 2571
            conn.increaseUsageCount(1);
2572

  
2573
            // Delete it from xml_path_index table
2574
            pstmt = conn.prepareStatement(
2575
                    "DELETE FROM xml_path_index WHERE docid = ?");
2576
            //increase usage count
2463 2577
            pstmt.setString(1, docid);
2464 2578
            pstmt.execute();
2465 2579
            pstmt.close();
2580
            conn.increaseUsageCount(1);
2466 2581

  
2582

  
2467 2583
            // Delete it from xml_accesssubtree table
2468 2584
            pstmt = conn.prepareStatement(
2469 2585
                    "DELETE FROM xml_accesssubtree WHERE docid = ?");
2470 2586
            //increase usage count
2471
            conn.increaseUsageCount(1);
2472 2587
            pstmt.setString(1, docid);
2473 2588
            pstmt.execute();
2474 2589
            pstmt.close();
2590
            conn.increaseUsageCount(1);
2475 2591

  
2476 2592
            // Delete it from xml_documents table
2477 2593
            pstmt = conn.prepareStatement(
......
2482 2598
            //Usaga count increase 1
2483 2599
            conn.increaseUsageCount(1);
2484 2600

  
2601
            // Delete the old nodes in xml_nodes table...
2602
                   pstmt = conn.prepareStatement("DELETE FROM xml_nodes "
2603
                                                   + "WHERE docid = ?");
2604

  
2605
                   // Increase dbconnection usage count
2606
                   conn.increaseUsageCount(1);
2607
                   // Bind the values to the query and execute it
2608
                   pstmt.setString(1, docid);
2609
                   pstmt.execute();
2610
                   pstmt.close();
2611

  
2612

  
2485 2613
            conn.commit();
2486 2614
            conn.setAutoCommit(true);
2487 2615

  
2488 2616
        } catch (Exception e) {
2489 2617
            MetaCatUtil.debugMessage("error in DocumentImpl.delete: "
2490 2618
                    + e.getMessage(), 30);
2619
    e.printStackTrace();
2491 2620
            throw e;
2492 2621
        } finally {
2493 2622

  
......
2696 2825
     * paramter is in order to rollback feature
2697 2826
     */
2698 2827
    private static void archiveDocRevision(DBConnection dbconn, String docid,
2699
            String user)
2828
            String user, DocumentImpl doc)
2700 2829
    {
2701 2830
        String sysdate = dbAdapter.getDateTimeFunction();
2702 2831
        //DBConnection conn = null;
......
2707 2836
        // for that document as selected from xml_documents
2708 2837

  
2709 2838
        try {
2839
            if (doc == null) {
2840
                    doc = new DocumentImpl(docid);
2841
            }
2842

  
2710 2843
            //check out DBConnection
2711 2844
            /*
2712 2845
             * conn=DBConnectionPool.
2713 2846
             * getDBConnection("DocumentImpl.archiveDocRevision");
2714 2847
             * serialNumber=conn.getCheckOutSerialNumber();
2715 2848
             */
2849

  
2850
            // Move the nodes from xml_nodes to xml_revisions table...
2851
            pstmt = dbconn.prepareStatement("INSERT INTO xml_nodes_revisions "
2852
                    + "(nodeid, nodeindex, nodetype, nodename, nodeprefix, "
2853
                    + "nodedata, parentnodeid, rootnodeid, docid, date_created,"
2854
                    + " date_updated, nodedatanumerical) "
2855
                    + "SELECT * FROM xml_nodes " + "WHERE rootnodeid = ?");
2856

  
2857
            // Increase dbconnection usage count
2858
            dbconn.increaseUsageCount(1);
2859
            // Bind the values to the query and execute it
2860
            pstmt.setLong(1, doc.getRootNodeID());
2861
            pstmt.execute();
2862
            pstmt.close();
2863

  
2864
            // Move the document information to xml_revisions table...
2716 2865
            pstmt = dbconn.prepareStatement("INSERT INTO xml_revisions "
2717 2866
                    + "(docid, rootnodeid, docname, doctype, "
2718 2867
                    + "user_owner, user_updated, date_created, date_updated, "
......
2721 2870
                    + "user_owner, ?, " + sysdate + ", " + sysdate + ", "
2722 2871
                    + "server_location, rev, public_access, catalog_id "
2723 2872
                    + "FROM xml_documents " + "WHERE docid = ?");
2873

  
2724 2874
            // Increase dbconnection usage count
2725 2875
            dbconn.increaseUsageCount(1);
2726 2876
            // Bind the values to the query and execute it
......
2729 2879
            pstmt.setString(3, docid);
2730 2880
            pstmt.execute();
2731 2881
            pstmt.close();
2882

  
2732 2883
        } catch (SQLException e) {
2733 2884
            MetaCatUtil.debugMessage(
2734 2885
                    "Error in DocumentImpl.archiveDocRevision : "
2735 2886
                            + e.getMessage(), 30);
2736
        } finally {
2887
        } catch (Exception e) {
2888
            MetaCatUtil.debugMessage(
2889
                    "Error in DocumentImpl.archiveDocRevision : "
2890
                            + e.getMessage(), 30);
2891
        }
2892
        finally {
2737 2893
            try {
2738 2894
                pstmt.close();
2739 2895
            } catch (SQLException ee) {

Also available in: Unified diff