Project

General

Profile

« Previous | Next » 

Revision 4500

Added by daigle over 15 years ago

Move the code to write metadata to disk into documentImpl

View differences:

DocumentImpl.java
30 30
import java.io.BufferedOutputStream;
31 31
import java.io.File;
32 32
import java.io.FileOutputStream;
33
import java.io.FileWriter;
33 34
import java.io.IOException;
34 35
import java.io.InputStream;
35 36
import java.io.PrintWriter;
......
927 928
        try {
928 929
            this.toXml(docwriter, userName, groupNames, withInlineData);
929 930
        } catch (McdbException mcdbe) {
931
        	logMetacat.warn("Could not convert documentImpl to xml: " + mcdbe.getMessage());
930 932
            return null;
931 933
        }
932 934
        String document = docwriter.toString();
......
971 973
    }
972 974

  
973 975
    /**
974
     * Print a text representation of the XML document to a Writer
975
     *
976
     * @param pw
977
     *            the Writer to which we print the document Now we decide no
978
     *            matter withinInlineData's value, the document will
979
     *
980
     */
981
    public void toXml(Writer pw, String user, String[] groups,
976
	 * Print a text representation of the XML document to a Writer
977
	 * 
978
	 * @param pw
979
	 *            the Writer to which we print the document Now we decide no
980
	 *            matter withinInlineData's value, the document will
981
	 * 
982
	 */
983
	public void toXml(Writer pw, String user, String[] groups, boolean withInLineData)
984
			throws McdbException {
985
		String documentDir = null;
986
		String documentPath = null;
987
		try {
988
			String separator = PropertyService.getProperty("document.accNumSeparator");
989
			documentDir = PropertyService.getProperty("application.documentfilepath");
990
			documentPath = documentDir + FileUtil.getFS() + docid + separator + rev;
991

  
992
			if (FileUtil.getFileStatus(documentPath) == FileUtil.DOES_NOT_EXIST) {
993
				FileWriter fileWriter = new FileWriter(documentPath);
994
				toXmlFromDb(fileWriter, user, groups, true);
995
			}
996
		} catch (PropertyNotFoundException pnfe) {
997
			throw new McdbException("Could not write file: " + documentPath + " : "
998
					+ pnfe.getMessage());
999
		} catch (IOException ioe) {
1000
			throw new McdbException("Could not write file: " + documentPath + " : "
1001
					+ ioe.getMessage());
1002
		}
1003

  
1004
		readFromFileSystem(pw, user, groups, documentPath);
1005
	}
1006
    
1007
    /**
1008
	 * Print a text representation of the XML document to a Writer
1009
	 * 
1010
	 * @param pw
1011
	 *            the Writer to which we print the document Now we decide no
1012
	 *            matter withinInlineData's value, the document will
1013
	 * 
1014
	 */
1015
    public void toXmlFromDb(Writer pw, String user, String[] groups,
982 1016
            boolean withInLineData) throws McdbException
983 1017
    {
984 1018
        // flag for process eml2
......
1294 1328
    public void readFromFileSystem(Writer pw, String user, String[] groups,
1295 1329
            String documentPath) throws McdbException {
1296 1330
        
1297
//        // create a printwriter
1298
//        PrintWriter out = null;
1299
//        if (pw instanceof PrintWriter) {
1300
//            out = (PrintWriter) pw;
1301
//        } else {
1302
//            out = new PrintWriter(pw);
1303
//        }
1304
        
1305 1331
		String xmlFileContents = "";
1306 1332
		
1307 1333
		try {			
......
1351 1377
    }
1352 1378
    
1353 1379
    /**
1380
	 * Write an XML document to the file system.
1381
	 * 
1382
	 * @param xml
1383
	 *            the document we want to write out
1384
	 * @param accNumber
1385
	 *            the document id which is used to name the output file
1386
	 */
1387
    private static void writeToFileSystem(String xml, String accNumber) throws McdbException {
1388

  
1389
		// write the document to disk
1390
		FileWriter fileWriter = null;
1391
		String documentDir = null;
1392
		String documentPath = null;
1393

  
1394
		try {		
1395
			
1396
			documentDir = PropertyService.getProperty("application.documentfilepath");
1397
			documentPath = documentDir + FileUtil.getFS() + accNumber;
1398

  
1399
			if (accNumber == null) {
1400
				throw new McdbException("Could not write document file.  Account number is null" );
1401
			}
1402
			
1403
			if (FileUtil.getFileStatus(documentPath) >= FileUtil.EXISTS_ONLY) {
1404
				throw new McdbException("The file you are trying to write already exists "
1405
                        + " in metacat.  Please update your version number.");
1406
			}
1407
			
1408
			if (accNumber != null
1409
					&& FileUtil.getFileStatus(documentPath) == FileUtil.DOES_NOT_EXIST) {
1410

  
1411
				fileWriter = new FileWriter(documentPath);
1412
				fileWriter.write(xml);
1413
				fileWriter.close();
1414
			}			
1415

  
1416
		} catch (PropertyNotFoundException pnfe) {
1417
			throw new McdbException("Could not write file: " + documentPath + " : "
1418
					+ pnfe.getMessage());
1419
		} catch (IOException ioe) {
1420
			throw new McdbException("Could not write file: " + documentPath + " : "
1421
					+ ioe.getMessage());
1422
		}
1423
	}	
1424
    
1425
    /**
1354 1426
	 * Strip out an inline data section from a 2.0.X version document. This assumes 
1355 1427
	 * that the inline element is within a distribution element and the id for the
1356 1428
	 * distribution is the same as the subtreeid in the xml_access table.
......
1452 1524
    	
1453 1525
    	return changedString;
1454 1526
    }
1527
    
1528
    private static String readerToString(Reader reader) throws IOException {
1529
		String xmlString = "";
1530
		int tmp = reader.read();
1531
		while (tmp != -1) {
1532
			xmlString += (char) tmp;
1533
			tmp = reader.read();
1534
		}
1535
		
1536
		reader.reset();
1537
		return xmlString;
1538
	}
1455 1539

  
1456 1540
    /**
1457
     * Build the index records for this document.  For each node, all absolute
1458
     * and relative paths to the root of the document are created and inserted
1459
     * into the xml_index table.  This requires that the DocumentImpl instance
1460
     * exists, so first call the constructor that reads the document from the
1461
     * database.
1462
     *
1463
     * @throws McdbException on error getting the node records for the document
1464
     */
1541
	 * Build the index records for this document. For each node, all absolute
1542
	 * and relative paths to the root of the document are created and inserted
1543
	 * into the xml_index table. This requires that the DocumentImpl instance
1544
	 * exists, so first call the constructor that reads the document from the
1545
	 * database.
1546
	 * 
1547
	 * @throws McdbException
1548
	 *             on error getting the node records for the document
1549
	 */
1465 1550
    public void buildIndex() throws McdbException
1466 1551
    {
1467 1552
    	logMetacat.warn("buildIndex called on docid " + docid);
......
2422 2507
            boolean needValidation) throws Exception
2423 2508
    {
2424 2509
        // NEW - WHEN CLIENT ALWAYS PROVIDE ACCESSION NUMBER INCLUDING REV IN IT
2425

  
2510
    	
2511
    	// Get the xml as a string so we can write to file later
2512
    	String xmlString = readerToString(xml);
2513
    	
2426 2514
        logMetacat.info("conn usage count before writting: "
2427 2515
                + conn.getUsageCount());
2428 2516
        AccessionNumber ac = new AccessionNumber(accnum, action);
......
2482 2570
                    parser.parse(new InputSource(xml));
2483 2571
                    conn.commit();
2484 2572
                    conn.setAutoCommit(true);
2573
                    
2574
                    //write the file to disk
2575
                	writeToFileSystem(xmlString, accnum);
2485 2576

  
2486 2577
                    // write to xml_node complete. start the indexing thread.
2487 2578
                    addDocidToIndexingQueue(docid, rev);
......
2556 2647
            //System.out.println("commit!!!!!!!!!!!!!!!!!111");
2557 2648
            conn.commit();
2558 2649
            conn.setAutoCommit(true);
2650
            
2651
            //write the file to disk
2652
        	writeToFileSystem(xmlString, accnum);
2559 2653

  
2560 2654
            addDocidToIndexingQueue(docid, rev);
2561 2655
        } catch (Exception e) {
......
2598 2692
           }
2599 2693

  
2600 2694
        }
2601

  
2695
        
2602 2696
        // Force replicate out the new document to each server in our server
2603 2697
        // list. Start the thread to replicate this new document out to the
2604 2698
        // other servers true mean it is xml document null is because no
......
2660 2754
            String ruleBase, boolean needValidation, String tableName, 
2661 2755
            boolean timedReplication, String createDate, String updateDate) throws Exception
2662 2756
    {
2757
    	// Get the xml as a string so we can write to file later
2758
    	String xmlString = readerToString(xml);
2759
    	
2663 2760
        long rootId;
2664 2761
        String docType = null;
2665 2762
        String docName = null;
......
2721 2818
            conn.commit();
2722 2819
            conn.setAutoCommit(true);
2723 2820
            
2821
            // Write the file to disk
2822
        	writeToFileSystem(xmlString, accnum);
2724 2823
            
2725 2824
            // write to xml_node complete. start the indexing thread.
2726 2825
            // this only for xml_documents
......
3886 3985
            DBConnectionPool.returnDBConnection(dbconn, serialNumber);
3887 3986
        }
3888 3987
    }
3988
    
3889 3989
    /*
3890 3990
     * This method will write a record to revision table base on given
3891 3991
     * info. The create date and update will be current time.

Also available in: Unified diff