/*
* @author : Mark Drake
* @version : 1.0
* Development Environment : Oracle9i JDeveloper
* Name of the File : SimpleBulkLoader.java
* Creation / Modification History :
* Shefali Bansal 25-Jan-2003 Modified
* Modifications Made:
* 1. Provided for getting the Table for Insert from the BaseApplication class
* 2. Renamed the doSomething() method as doBulkLoad() method
* 3. Provided for user friendly messages for the Exceptions thrown
*/
//Package name
package oracle.otnsamples.xmldb.simplebulkloader.examples;
//IO Imports
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
//Java SQL Imports
import java.sql.CallableStatement;
//JDBC imports
import java.sql.Connection;
import java.sql.SQLException;
//Import 'BaseApplication' base class
import oracle.otnsamples.xmldb.simplebulkloader.common.baseApp.BaseApplication;
//Import Alert class for trace purpose
import oracle.otnsamples.xmldb.simplebulkloader.common.trace.Alert;
//Oracle SQL Imports
import oracle.sql.CLOB;
/**
* 'SimpleBulkLoader' class is part of the Simple Bulk Loader application. This
* class demonstrates how to load a directory of XML files into XML DB using
* Java. The important thing here is, it shows how using a single Temporary
* CLOB one can upload multiple XML Files. In this application the XML files
* are inserted into a 'PurchaseOrder' table which is of XMLType. The table
* into which the XML files are to be loaded can be customized as required by
* the user. This class extends the 'BaseApplication' class.
*/
public class SimpleBulkLoader extends BaseApplication {
public CallableStatement m_InsertStatement = null;
CLOB m_TemporaryCLOB = null;
// Empty Constructor
public SimpleBulkLoader( ) {
}
/**
* This method calls all the methods required for loading of XML files into
* the XML DB. It first gets the DB connection information from the
* connection.xml file. Once the connection info is available, it
* establishes the connection. After establishing of the connection , it
* reads through each of the XML files available in the source directory and
* loads them into the database.
*
* @param args String Array
*
* @throws Exception
*/
public void doBulkLoad( String[] args ) throws Exception {
try {
// Establish the database connection
super.getConnectionSettings( );
super.createConnection( );
Connection connection = super.getConnection( );
// Get the source directory from which to load the XML files
File sourceDirectory = new File( super.getTextNode( "sourceDirectory",
"c:\\temp" ) );
// SQL Statement for inserting content into the required database table
String INSERT_STATEMENT_TEXT = "insert into " + super.getTable( ) +
" values (xmltype(?))";
// Get the Insert statement Object
m_InsertStatement = connection.prepareCall( INSERT_STATEMENT_TEXT );
// If the temporary CLOB has not yet been created, create new
m_TemporaryCLOB = CLOB.createTemporary( connection, true,
CLOB.DURATION_SESSION );
// Upload the XML files in the source directory
int uploadCount = uploadDirectory( connection, m_TemporaryCLOB,
sourceDirectory );
// Free the temporary CLOB
m_TemporaryCLOB.freeTemporary( );
// Close the connection
connection.close( );
} catch ( NullPointerException ex ) {
System.out.println( "SimpleBulkLoader.doBulkLoad- The system cannot find the specified config XML file's path or the source directory path. NullPointerException" );
} catch ( IOException ex ) {
Alert.log( "SimpleBulkLoader.doBulkLoad- The system cannot find the path specified. IO:FileNotFound Exception" );
}
}
/**
* Loads all the XML files under the given source directory and its
* subdirectories into the XML DB
*
* @param conn object specifying the DB connection information
* @param clob specifying the temporary CLOB holding the XML content
* @param sourceDirectory specifying the source directory where all the XML
* files are located
*
* @return int The count of the XML files in the source directory
*
* @throws java.sql.SQLException
* @throws java.io.IOException
*/
public int uploadDirectory( Connection conn, CLOB clob, File sourceDirectory )
throws java.sql.SQLException, java.io.IOException {
int fileCounter = 0;
try {
// Get the list of all the XML files and sub directories in the source directory
File[] files = sourceDirectory.listFiles( );
// Loop through this list
for ( int i = 0; i < files.length; i++ ) {
// Handle the case of a sub directory
if ( files[i].isDirectory( ) ) {
fileCounter = fileCounter + uploadDirectory( conn, clob, files[i] );
}
else {
// Handle the case of XML files
// Load the content of the XML files into the XML DB
uploadXMLFile( conn, clob, files[i] );
fileCounter = fileCounter + 1;
}
}
if ( files.length == 0 ) {
Alert.log( "If you see this message it means the directory specified or some sub directory of the directory specified does not contain any XML file" );
}
// Commit the insertion of XML content in the XML DB
conn.commit( );
}
catch ( NullPointerException ex ) {
Alert.log( "SimpleBulkLoader.uploadDirectory:The system cannot find the XML files in the source directory location specified : FileNotFound Exception" );
}
// Return the count of the XML files in the source directory
return fileCounter;
}
/**
* Loads the content of an XML file into the XML DB
*
* @param conn object specifying the DB connection information
* @param clob specifying the temporary CLOB holding the XML content
* @param xmlFile specifying the XML file to be loaded
*
* @throws java.sql.SQLException
* @throws java.io.IOException
*/
public void uploadXMLFile( Connection conn, CLOB clob, File xmlFile )
throws java.sql.SQLException, java.io.IOException {
boolean result;
// Write the XML file content into the CLOB object
writeToClob( clob, new FileInputStream( xmlFile ) );
// Bind this CLOB to the Insert Statement
m_InsertStatement.setClob( 1, clob );
// Execute the Insert Statement
result = m_InsertStatement.execute( );
clob.trim( 0 );
System.out.println( "XMLLoader: Uploaded File " +
xmlFile.getAbsoluteFile( ) );
}
/**
* Main method for this class
*
* @param args Array
*/
public static void main( String[] args ) {
try {
// Instantiate the SimpleBulkLoader class
SimpleBulkLoader app = new SimpleBulkLoader( );
// Call the doBulkLoad() method load all the XML files under the given
// directory and its subdirectories into the XML DB
app.doBulkLoad( args );
}
catch ( Exception e ) {
Alert.log( e );
}
}
}