Project

General

Profile

« Previous | Next » 

Revision 5195

Added by daigle over 14 years ago

Pass the doc xml as a string to docImpl.write and writeRepication. This is so a reader can be create for the parsing and for the write to disk. Also created a db access class for xml query result deletion.

View differences:

DocumentImpl.java
63 63
import edu.ucsb.nceas.metacat.replication.ReplicationService;
64 64
import edu.ucsb.nceas.metacat.service.XMLSchema;
65 65
import edu.ucsb.nceas.metacat.service.XMLSchemaService;
66
import edu.ucsb.nceas.metacat.shared.MetacatUtilException;
66
import edu.ucsb.nceas.metacat.shared.AccessException;
67 67
import edu.ucsb.nceas.metacat.spatial.SpatialHarvester;
68 68
import edu.ucsb.nceas.metacat.util.AuthUtil;
69 69
import edu.ucsb.nceas.metacat.util.DocumentUtil;
......
71 71
import edu.ucsb.nceas.metacat.util.SystemUtil;
72 72
import edu.ucsb.nceas.utilities.FileUtil;
73 73
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
74
import edu.ucsb.nceas.utilities.StringUtil;
75 74
import edu.ucsb.nceas.utilities.UtilException;
76 75

  
77 76
import org.apache.log4j.Logger;
......
166 165
    protected long rootnodeid;
167 166
    private ElementNode rootNode = null;
168 167
    private TreeSet<NodeRecord> nodeRecordList = null;
168
    
169
    private Vector<String> pathsForIndexing = null;
169 170
  
170 171
    private static Logger logMetacat = Logger.getLogger(DocumentImpl.class);
171 172
    private static Logger logReplication = Logger.getLogger("ReplicationLogging");
......
199 200
            //this.conn = conn;
200 201
            this.docid = DocumentUtil.getDocIdFromAccessionNumber(accNum);
201 202
            this.rev   = DocumentUtil.getRevisionFromAccessionNumber(accNum);
203
            
204
            pathsForIndexing = SystemUtil.getPathsForIndexing();
202 205

  
203 206
            // Look up the document information
204 207
            getDocumentInfo(docid, rev);
......
284 287
        this.doctype = docType;
285 288
        this.docid = docId;
286 289
        this.rev = (new Integer(newRevision)).intValue();
290
        
291
        pathsForIndexing = SystemUtil.getPathsForIndexing();
292
        
287 293
        //this.updatedVersion = newRevision;
288 294
        writeDocumentToDB(action, user, pub, catalogId, serverCode, createDate, updateDate);
289 295
    }
......
1404 1410
	 * @param accNumber
1405 1411
	 *            the document id which is used to name the output file
1406 1412
	 */
1407
    private static void writeToFileSystem(String xml, String accNumber) throws McdbException {
1413
    private static void writeToFileSystem(Reader xml, String accNumber) throws McdbException {
1408 1414

  
1409 1415
		// write the document to disk
1410 1416
//		FileWriter fileWriter = null;
......
1748 1754
                      currentName, docid, doctype, parentId));
1749 1755
                  }
1750 1756
                }
1751
                try {
1752
					if (SystemUtil.getPathsForIndexing().contains(currentName)
1753
							&& leafData.trim().length() != 0) {
1754
						logMetacat.debug("paths found for indexing: " + currentName);
1755
						pathsFoundForIndexing.put(currentName, new PathIndexEntry(
1756
								leafNodeId, currentName, docid, leafParentId, leafData,
1757
								leafDataNumerical));
1758
					}
1759
				} catch (MetacatUtilException ue) {
1760
					logMetacat.warn("Could not get index paths: " + ue.getMessage());
1757
				if (pathsForIndexing.contains(currentName)
1758
						&& leafData.trim().length() != 0) {
1759
					logMetacat.debug("paths found for indexing: " + currentName);
1760
					pathsFoundForIndexing.put(currentName, new PathIndexEntry(
1761
							leafNodeId, currentName, docid, leafParentId, leafData,
1762
							leafDataNumerical));
1761 1763
				}
1762
                
1763 1764
            }
1764 1765
            
1765 1766
            // process relative xpaths
......
1777 1778
                logMetacat.debug("B: " + path +"\n");
1778 1779
                pathList.put(path, new PathIndexEntry(leafNodeId, path, docid,
1779 1780
                    doctype, parentId));
1780
            	try {
1781
					if (SystemUtil.getPathsForIndexing().contains(path)
1782
							&& leafData.trim().length() != 0) {
1783
						logMetacat.debug("paths found for indexing: " + currentName);
1784
						pathsFoundForIndexing.put(path, new PathIndexEntry(leafNodeId,
1785
								path, docid, leafParentId, leafData, leafDataNumerical));
1786
					}
1787
				} catch (MetacatUtilException ue) {
1788
					logMetacat.warn("Could not get index paths: " + ue.getMessage());
1781
				if (pathsForIndexing.contains(path)
1782
						&& leafData.trim().length() != 0) {
1783
					logMetacat.debug("paths found for indexing: " + currentName);
1784
					pathsFoundForIndexing.put(path, new PathIndexEntry(leafNodeId,
1785
							path, docid, leafParentId, leafData, leafDataNumerical));
1789 1786
				}
1790 1787
            }
1791 1788
            // process absolute xpaths
......
1797 1794
                logMetacat.debug("C: " + fullPath +"\n");
1798 1795
                pathList.put(fullPath, new PathIndexEntry(leafNodeId, fullPath,
1799 1796
                    docid, doctype, parentId));
1800
                try {
1801
					if (SystemUtil.getPathsForIndexing().contains(fullPath)
1802
							&& leafData.trim().length() != 0) {
1803
						logMetacat.debug("paths found for indexing: " + currentName);
1804
						pathsFoundForIndexing.put(fullPath, new PathIndexEntry(
1805
								leafNodeId, fullPath, docid, leafParentId, leafData,
1806
								leafDataNumerical));
1807
					}
1808
				} catch (MetacatUtilException ue) {
1809
					logMetacat.warn("Could not get index paths: " + ue.getMessage());
1797

  
1798
				if (pathsForIndexing.contains(fullPath)
1799
						&& leafData.trim().length() != 0) {
1800
					logMetacat.debug("paths found for indexing: " + currentName);
1801
					pathsFoundForIndexing.put(fullPath, new PathIndexEntry(
1802
							leafNodeId, fullPath, docid, leafParentId, leafData,
1803
							leafDataNumerical));
1810 1804
				}
1811 1805
            }
1812 1806
        } 
......
1843 1837
        double afterDeleteIndex = System.currentTimeMillis()/1000;
1844 1838
        logMetacat.debug("DocumentImpl.deleteNodeIndex - The delete index time is "+(afterDeleteIndex - start));
1845 1839
        // Delete all the entries in xml_queryresult
1846
        pstmt = conn.prepareStatement(
1847
                "DELETE FROM xml_queryresult WHERE docid = ?");
1848
        pstmt.setString(1, docid);
1849
        rows = pstmt.executeUpdate();
1850
        conn.increaseUsageCount(1);
1851
        pstmt.close();
1840
        
1841
//        pstmt = conn.prepareStatement(
1842
//                "DELETE FROM xml_queryresult WHERE docid = ?");
1843
//        pstmt.setString(1, docid);
1844
//        rows = pstmt.executeUpdate();
1845
//        conn.increaseUsageCount(1);
1846
//        pstmt.close();
1847
        try {
1848
            XMLQueryresultAccess xmlQueryresultAccess = new XMLQueryresultAccess();
1849
            xmlQueryresultAccess.deleteXMLQueryresulForDoc(docid);
1850
        } catch (AccessException ae) {
1851
        	throw new SQLException("Problem deleting xml query result for docid " + 
1852
        			docid + " : " + ae.getMessage());
1853
        }
1852 1854
        logMetacat.debug("DocumentImpl.deleteNodeIndex - Deleted " + rows + " rows from xml_queryresult " +
1853 1855
                "for document " + docid);
1854 1856
        double afterDeleteQueryResult = System.currentTimeMillis()/1000;
......
2496 2498
     * false); }
2497 2499
     */
2498 2500

  
2499
    public static String write(DBConnection conn, Reader xml, String pub,
2501
    public static String write(DBConnection conn, String xmlString, String pub,
2500 2502
            Reader dtd, String action, String docid, String user,
2501 2503
            String[] groups, String ruleBase, boolean needValidation)
2502 2504
            throws Exception
......
2505 2507
        //in MetacatServlet class and now is wrapper into documentImple
2506 2508
        // get server location for this doc
2507 2509
        int serverLocation = getServerLocationNumber(docid);
2508
        return write(conn, xml, pub, dtd, action, docid, user, groups,
2510
        return write(conn, xmlString, pub, dtd, action, docid, user, groups,
2509 2511
                serverLocation, false, ruleBase, needValidation);
2510 2512
    }
2511 2513

  
......
2539 2541
     *            and version checked.
2540 2542
     */
2541 2543

  
2542
    public static String write(DBConnection conn, Reader xml, String pub,
2544
    public static String write(DBConnection conn, String xmlString, String pub,
2543 2545
            Reader dtd, String action, String accnum, String user,
2544 2546
            String[] groups, int serverCode, boolean override, String ruleBase,
2545 2547
            boolean needValidation) throws Exception
......
2547 2549
        // NEW - WHEN CLIENT ALWAYS PROVIDE ACCESSION NUMBER INCLUDING REV IN IT
2548 2550
    	
2549 2551
    	// Get the xml as a string so we can write to file later
2550
    	String xmlString = StringUtil.readerToString(xml);
2552
    	StringReader xmlReader = new StringReader(xmlString);
2551 2553
    	
2552 2554
        logMetacat.debug("DocumentImpl.write - conn usage count before writting: "
2553 2555
                + conn.getUsageCount());
......
2598 2600
                     * updaterev, validate, user, groups, pub, serverCode, dtd);
2599 2601
                     */
2600 2602
                    logMetacat.debug("DocumentImpl.write - initializing parser");
2601
                    parser = initializeParser(conn, action, docid, xml, updaterev,
2603
                    parser = initializeParser(conn, action, docid, xmlReader, updaterev,
2602 2604
                            user, groups, pub, serverCode, dtd, ruleBase,
2603 2605
                            needValidation, false, null, null);// false means it is not a revision doc
2604 2606
                                   //null, null are createdate and updatedate
2605 2607
                                   //null will use current time as create date time
2606 2608
                    conn.setAutoCommit(false);
2607 2609
                    logMetacat.debug("DocumentImpl.write - parsing xml");
2608
                    parser.parse(new InputSource(xml));
2610
                    parser.parse(new InputSource(xmlReader));
2609 2611
                    conn.commit();
2610 2612
                    conn.setAutoCommit(true);
2611 2613
                    
2612 2614
                    //write the file to disk
2613 2615
                    logMetacat.debug("DocumentImpl.write - Writing xml to file system");
2614
                	writeToFileSystem(xmlString, accnum);
2616
                    StringReader xmlReaderForFile = new StringReader(xmlString);
2617
                	writeToFileSystem(xmlReaderForFile, accnum);
2615 2618

  
2616 2619
                    // write to xml_node complete. start the indexing thread.
2617 2620
                    addDocidToIndexingQueue(docid, rev);
......
2676 2679
        }
2677 2680
        XMLReader parser = null;
2678 2681
        try {
2679
            parser = initializeParser(conn, action, docid, xml, rev, user, groups,
2682
            parser = initializeParser(conn, action, docid, xmlReader, rev, user, groups,
2680 2683
                    pub, serverCode, dtd, ruleBase, needValidation, false, null, null);
2681 2684
                    // null and null are createtime and updatetime
2682 2685
                    // null will create current time
2683 2686
                    //false means it is not a revision doc
2684 2687

  
2685 2688
            conn.setAutoCommit(false);
2686
            logMetacat.debug("DocumentImpl.write - XML to be parsed: " + xml);
2687
            parser.parse(new InputSource(xml));
2689
            logMetacat.debug("DocumentImpl.write - XML to be parsed: " + xmlString);
2690
            parser.parse(new InputSource(xmlReader));
2688 2691

  
2689 2692
            conn.commit();
2690 2693
            conn.setAutoCommit(true);
2691 2694
            
2692 2695
            //write the file to disk
2693
        	writeToFileSystem(xmlString, accnum);
2696
            StringReader xmlReaderForFile = new StringReader(xmlString);
2697
        	writeToFileSystem(xmlReaderForFile, accnum);
2694 2698

  
2695 2699
            addDocidToIndexingQueue(docid, rev);
2696 2700
        } catch (Exception e) {
......
2719 2723
        // result next time
2720 2724
        if (action.equals("UPDATE")) {
2721 2725
          try {
2722
              PreparedStatement pstmt = null;
2723
              pstmt = conn.prepareStatement(
2724
                      "DELETE FROM xml_queryresult WHERE docid = ?");
2725
              pstmt.setString(1, docid);
2726
              pstmt.execute();
2727
              pstmt.close();
2728
              conn.increaseUsageCount(1);
2726
//              PreparedStatement pstmt = null;
2727
//              pstmt = conn.prepareStatement(
2728
//                      "DELETE FROM xml_queryresult WHERE docid = ?");
2729
//              pstmt.setString(1, docid);
2730
//              pstmt.execute();
2731
//              pstmt.close();
2732
//              conn.increaseUsageCount(1);
2733
              try {
2734
                  XMLQueryresultAccess xmlQueryresultAccess = new XMLQueryresultAccess();
2735
                  xmlQueryresultAccess.deleteXMLQueryresulForDoc(docid);
2736
              } catch (AccessException ae) {
2737
              	throw new SQLException("Problem deleting xml query result for docid " + 
2738
              			docid + " : " + ae.getMessage());
2739
              }
2729 2740
          } catch (Exception e){
2730 2741
              logMetacat.error("DocumentImpl.write - Error in deleting enteries from "
2731 2742
                                       + "xml_queryresult where docid is "
......
2790 2801
     *            the server which notify local server the force replication
2791 2802
     *            command
2792 2803
     */
2793
    public static String writeReplication(DBConnection conn, Reader xml,
2804
    public static String writeReplication(DBConnection conn, String xmlString,
2794 2805
            String pub, Reader dtd, String action, String accnum, String user,
2795 2806
            String[] groups, String homeServer, String notifyServer,
2796 2807
            String ruleBase, boolean needValidation, String tableName, 
2797 2808
            boolean timedReplication, String createDate, String updateDate) throws Exception
2798 2809
    {
2799 2810
    	// Get the xml as a string so we can write to file later
2800
    	String xmlString = StringUtil.readerToString(xml);
2811
    	StringReader xmlReader = new StringReader(xmlString);
2801 2812
    	
2802 2813
        long rootId;
2803 2814
        String docType = null;
......
2851 2862
            {
2852 2863
                isRevision = true;
2853 2864
            }
2854
            parser = initializeParser(conn, action, docid, xml, rev, user, groups,
2865
            parser = initializeParser(conn, action, docid, xmlReader, rev, user, groups,
2855 2866
                    pub, serverCode, dtd, ruleBase, needValidation, 
2856 2867
                    isRevision, createDate, updateDate);
2857 2868
         
2858 2869
            conn.setAutoCommit(false);
2859
            parser.parse(new InputSource(xml));
2870
            parser.parse(new InputSource(xmlReader));
2860 2871
            conn.commit();
2861 2872
            conn.setAutoCommit(true);
2862 2873
            
2863 2874
            // Write the file to disk
2864
        	writeToFileSystem(xmlString, accnum);
2875
            StringReader xmlReaderForFile = new StringReader(xmlString);
2876
        	writeToFileSystem(xmlReaderForFile, accnum);
2865 2877
            
2866 2878
            // write to xml_node complete. start the indexing thread.
2867 2879
            // this only for xml_documents
......
3173 3185
            double afterDeleteXmlAccess2 = System.currentTimeMillis()/1000;
3174 3186
            logMetacat.info("DocumentImpl.delete - The deleting xml_access base on docid time is "+(afterDeleteXmlAccess2 - afterDeleteXmlAccess1 ));
3175 3187
            // Delete enteries from xml_queryresult
3176
            pstmt = conn.prepareStatement(
3177
                    "DELETE FROM xml_queryresult WHERE docid = ?");
3178
            pstmt.setString(1, docid);
3179
            logMetacat.debug("DocumentImpl.delete - running sql: " + pstmt.toString());
3180
            pstmt.execute();
3181
            pstmt.close();
3182
            conn.increaseUsageCount(1);
3188
//            pstmt = conn.prepareStatement(
3189
//                    "DELETE FROM xml_queryresult WHERE docid = ?");
3190
//            pstmt.setString(1, docid);
3191
//            logMetacat.debug("DocumentImpl.delete - running sql: " + pstmt.toString());
3192
//            pstmt.execute();
3193
//            pstmt.close();
3194
//            conn.increaseUsageCount(1);
3195
            try {
3196
                XMLQueryresultAccess xmlQueryresultAccess = new XMLQueryresultAccess();
3197
                xmlQueryresultAccess.deleteXMLQueryresulForDoc(docid);
3198
            } catch (AccessException ae) {
3199
            	throw new SQLException("Problem deleting xml query result for docid " + 
3200
            			docid + " : " + ae.getMessage());
3201
            }
3183 3202
            double afterDeleteQueryResult = System.currentTimeMillis()/1000;
3184 3203
            logMetacat.info("DocumentImpl.delete - The deleting xml_queryresult time is "+(afterDeleteQueryResult - afterDeleteXmlAccess2));
3185 3204
            // Delete it from relation table

Also available in: Unified diff