r4500 - trunk/src/edu/ucsb/nceas/metacat
daigle at ecoinformatics.org
daigle at ecoinformatics.org
Mon Nov 3 11:00:33 PST 2008
Author: daigle
Date: 2008-11-03 11:00:33 -0800 (Mon, 03 Nov 2008)
New Revision: 4500
Modified:
trunk/src/edu/ucsb/nceas/metacat/DocumentImpl.java
Log:
Move the code to write metadata to disk into documentImpl
Modified: trunk/src/edu/ucsb/nceas/metacat/DocumentImpl.java
===================================================================
--- trunk/src/edu/ucsb/nceas/metacat/DocumentImpl.java 2008-11-03 18:59:59 UTC (rev 4499)
+++ trunk/src/edu/ucsb/nceas/metacat/DocumentImpl.java 2008-11-03 19:00:33 UTC (rev 4500)
@@ -30,6 +30,7 @@
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
+import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
@@ -927,6 +928,7 @@
try {
this.toXml(docwriter, userName, groupNames, withInlineData);
} catch (McdbException mcdbe) {
+ logMetacat.warn("Could not convert documentImpl to xml: " + mcdbe.getMessage());
return null;
}
String document = docwriter.toString();
@@ -971,14 +973,46 @@
}
/**
- * Print a text representation of the XML document to a Writer
- *
- * @param pw
- * the Writer to which we print the document Now we decide no
- * matter withinInlineData's value, the document will
- *
- */
- public void toXml(Writer pw, String user, String[] groups,
+ * Print a text representation of the XML document to a Writer
+ *
+ * @param pw
+ * the Writer to which we print the document Now we decide no
+ * matter withinInlineData's value, the document will
+ *
+ */
+ public void toXml(Writer pw, String user, String[] groups, boolean withInLineData)
+ throws McdbException {
+ String documentDir = null;
+ String documentPath = null;
+ try {
+ String separator = PropertyService.getProperty("document.accNumSeparator");
+ documentDir = PropertyService.getProperty("application.documentfilepath");
+ documentPath = documentDir + FileUtil.getFS() + docid + separator + rev;
+
+ if (FileUtil.getFileStatus(documentPath) == FileUtil.DOES_NOT_EXIST) {
+ FileWriter fileWriter = new FileWriter(documentPath);
+ toXmlFromDb(fileWriter, user, groups, true);
+ }
+ } catch (PropertyNotFoundException pnfe) {
+ throw new McdbException("Could not write file: " + documentPath + " : "
+ + pnfe.getMessage());
+ } catch (IOException ioe) {
+ throw new McdbException("Could not write file: " + documentPath + " : "
+ + ioe.getMessage());
+ }
+
+ readFromFileSystem(pw, user, groups, documentPath);
+ }
+
+ /**
+ * Print a text representation of the XML document to a Writer
+ *
+ * @param pw
+ * the Writer to which we print the document Now we decide no
+ * matter withinInlineData's value, the document will
+ *
+ */
+ public void toXmlFromDb(Writer pw, String user, String[] groups,
boolean withInLineData) throws McdbException
{
// flag for process eml2
@@ -1294,14 +1328,6 @@
public void readFromFileSystem(Writer pw, String user, String[] groups,
String documentPath) throws McdbException {
-// // create a printwriter
-// PrintWriter out = null;
-// if (pw instanceof PrintWriter) {
-// out = (PrintWriter) pw;
-// } else {
-// out = new PrintWriter(pw);
-// }
-
String xmlFileContents = "";
try {
@@ -1351,6 +1377,52 @@
}
/**
+ * Write an XML document to the file system.
+ *
+ * @param xml
+ * the document we want to write out
+ * @param accNumber
+ * the document id which is used to name the output file
+ */
+ private static void writeToFileSystem(String xml, String accNumber) throws McdbException {
+
+ // write the document to disk
+ FileWriter fileWriter = null;
+ String documentDir = null;
+ String documentPath = null;
+
+ try {
+
+ documentDir = PropertyService.getProperty("application.documentfilepath");
+ documentPath = documentDir + FileUtil.getFS() + accNumber;
+
+ if (accNumber == null) {
+ throw new McdbException("Could not write document file. Account number is null" );
+ }
+
+ if (FileUtil.getFileStatus(documentPath) >= FileUtil.EXISTS_ONLY) {
+ throw new McdbException("The file you are trying to write already exists "
+ + " in metacat. Please update your version number.");
+ }
+
+ if (accNumber != null
+ && FileUtil.getFileStatus(documentPath) == FileUtil.DOES_NOT_EXIST) {
+
+ fileWriter = new FileWriter(documentPath);
+ fileWriter.write(xml);
+ fileWriter.close();
+ }
+
+ } catch (PropertyNotFoundException pnfe) {
+ throw new McdbException("Could not write file: " + documentPath + " : "
+ + pnfe.getMessage());
+ } catch (IOException ioe) {
+ throw new McdbException("Could not write file: " + documentPath + " : "
+ + ioe.getMessage());
+ }
+ }
+
+ /**
* Strip out an inline data section from a 2.0.X version document. This assumes
* that the inline element is within a distribution element and the id for the
* distribution is the same as the subtreeid in the xml_access table.
@@ -1452,16 +1524,29 @@
return changedString;
}
+
+ private static String readerToString(Reader reader) throws IOException {
+ String xmlString = "";
+ int tmp = reader.read();
+ while (tmp != -1) {
+ xmlString += (char) tmp;
+ tmp = reader.read();
+ }
+
+ reader.reset();
+ return xmlString;
+ }
/**
- * Build the index records for this document. For each node, all absolute
- * and relative paths to the root of the document are created and inserted
- * into the xml_index table. This requires that the DocumentImpl instance
- * exists, so first call the constructor that reads the document from the
- * database.
- *
- * @throws McdbException on error getting the node records for the document
- */
+ * Build the index records for this document. For each node, all absolute
+ * and relative paths to the root of the document are created and inserted
+ * into the xml_index table. This requires that the DocumentImpl instance
+ * exists, so first call the constructor that reads the document from the
+ * database.
+ *
+ * @throws McdbException
+ * on error getting the node records for the document
+ */
public void buildIndex() throws McdbException
{
logMetacat.warn("buildIndex called on docid " + docid);
@@ -2422,7 +2507,10 @@
boolean needValidation) throws Exception
{
// NEW - WHEN CLIENT ALWAYS PROVIDE ACCESSION NUMBER INCLUDING REV IN IT
-
+
+ // Get the xml as a string so we can write to file later
+ String xmlString = readerToString(xml);
+
logMetacat.info("conn usage count before writting: "
+ conn.getUsageCount());
AccessionNumber ac = new AccessionNumber(accnum, action);
@@ -2482,6 +2570,9 @@
parser.parse(new InputSource(xml));
conn.commit();
conn.setAutoCommit(true);
+
+ //write the file to disk
+ writeToFileSystem(xmlString, accnum);
// write to xml_node complete. start the indexing thread.
addDocidToIndexingQueue(docid, rev);
@@ -2556,6 +2647,9 @@
//System.out.println("commit!!!!!!!!!!!!!!!!!111");
conn.commit();
conn.setAutoCommit(true);
+
+ //write the file to disk
+ writeToFileSystem(xmlString, accnum);
addDocidToIndexingQueue(docid, rev);
} catch (Exception e) {
@@ -2598,7 +2692,7 @@
}
}
-
+
// Force replicate out the new document to each server in our server
// list. Start the thread to replicate this new document out to the
// other servers true mean it is xml document null is because no
@@ -2660,6 +2754,9 @@
String ruleBase, boolean needValidation, String tableName,
boolean timedReplication, String createDate, String updateDate) throws Exception
{
+ // Get the xml as a string so we can write to file later
+ String xmlString = readerToString(xml);
+
long rootId;
String docType = null;
String docName = null;
@@ -2721,6 +2818,8 @@
conn.commit();
conn.setAutoCommit(true);
+ // Write the file to disk
+ writeToFileSystem(xmlString, accnum);
// write to xml_node complete. start the indexing thread.
// this only for xml_documents
@@ -3886,6 +3985,7 @@
DBConnectionPool.returnDBConnection(dbconn, serialNumber);
}
}
+
/*
* This method will write a record to revision table base on given
* info. The create date and update will be current time.
More information about the Metacat-cvs
mailing list