|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.apache.torque.betwixt.XMLEventNotifier org.apache.torque.betwixt.Importer
public class Importer
Top level controller for importing XML into Torque OM objects and storing them into the database. Uses the org.apache.common.betwixt package to do the conversion to objects and registered RecordHandler classes to deal with managing the DB transactions.
The input XML can use the dataset attributes, action and onError, to specify what effect these objects should have on the underlying data base. E.g., action can be: add, addUpdate, update, or delete. onError can be: continue, stop, rollback.
This class configures Betwixt using a supplied Resolver to locate the required mapping file and DTD file, to so that Betwixt can convert the XML input stream into Torque (and possibly other) objects contained in a Dataset object.
Then, based on the action specified in the dataset object, it dispatches each of the imported objects to the matching RecordHandler method for record specific processing required to perform this action on the data in the DB. E.g., resolving id and foreign key issues, data validation, and the like.
It also handles top level error handling and transaction grouping to support the specified onError attribute setting.
Event listeners can be registered with this class so that import status (as specified in the ACTION_* static variables) can be reported to users and/or other reporting agents.
TODO: Support for having mix of records from different databases?
Field Summary | |
---|---|
static int |
ADD_ONLY
Only add records action flag value |
static int |
ADD_UPDATE
Add or update record action flag value |
static int |
DELETE
Delete records action flag value |
static int |
ON_ERROR_CONTINUE
Continue on error flag value |
static int |
ON_ERROR_ROLLBACK
Roll back on error flag value |
static int |
ON_ERROR_STOP
Stop importing on error flag value |
static int |
UPDATE_ONLY
Only update records action flag value |
Fields inherited from class org.apache.torque.betwixt.XMLEventNotifier |
---|
ACTION_ADD, ACTION_DELETE, ACTION_ERROR, ACTION_INFO, ACTION_TYPE, ACTION_UPDATE, EXPORT_END, EXPORT_RECORD, EXPORT_START, IMPORT_END, IMPORT_START |
Constructor Summary | |
---|---|
Importer()
Handles parsing XML records into a Dataset object and then dispatching them to the correct record handlers to be processed. |
Method Summary | |
---|---|
org.apache.commons.betwixt.io.BeanReader |
createBeanReader(Resolver resolver)
Create a Betwixt BeanReader to parse XML with. |
void |
deregisterListener(XMLEventListener listener)
Remove a listener. |
void |
error(org.xml.sax.SAXParseException e)
|
void |
fatalError(org.xml.sax.SAXParseException e)
|
protected RecordHandler |
findRecordHandler(java.lang.Object record)
Search the registered record handlers for one that matches the class or superclass of the supplied record. |
int |
getAction()
|
java.sql.Connection |
getConnection()
Get the connection to save information with. |
int |
getDatasetRecordNumber()
Get the (1 based) number of the current record object being processed. |
protected java.util.Map |
getHandlers()
Get the record handlers hash table. |
protected java.util.List |
getListeners()
Gets the registered listeners. |
int |
getOnError()
|
void |
importData(java.io.Reader xmlReader,
Resolver resolver)
Create a BeanReader; parse the supplied XML input stream into a Dataset; and then process the Dataset. |
void |
initHandlers()
Initialize the default set of record handlers when class is created. |
protected void |
initOptions(Dataset records)
Parses the text version of the Dataset action and onError attributes into option flag values. |
protected boolean |
isErrorParsing()
|
void |
notifyListeners(int recordNum,
java.lang.String action,
java.lang.String info)
Calls all registered listeners to let them know an action occured. |
Dataset |
parseXML(java.io.Reader importReader,
org.apache.commons.betwixt.io.BeanReader beanReader)
Populate a Dataset object from the supplied XML input stream. |
void |
processDataset(Dataset records)
The main driver for importing data into a Torque managed database from XML. |
void |
registerHandler(java.lang.Class recordType,
RecordHandler handler)
Register or modify a recordHandler to be used with the specifed class or subclass. |
void |
registerListener(XMLEventListener listener)
Register a listener that will be notified of export actions. |
protected void |
rollbackTransaction()
Attempt to roll back all changes made prior to an error occuring. |
protected void |
setAction(int action)
|
protected void |
setConnection(java.sql.Connection connection)
Set the database connection to use. |
void |
setDatasetRecordNumber(int datasetRecordNumber)
Sets the (1 based) number of the current record object being processed. |
protected void |
setErrorParsing(boolean errorParsing)
|
protected void |
setHandlers(java.util.Hashtable handlers)
Set a new record handler hash table. |
protected void |
setOnError(int onError)
|
java.lang.String |
simpleClassName(java.lang.Class c)
Get the name of a class without it's package. |
protected void |
startTransaction(java.lang.Object record)
Start a transaction grouping. |
void |
warning(org.xml.sax.SAXParseException e)
|
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
public static final int ON_ERROR_CONTINUE
public static final int ON_ERROR_STOP
public static final int ON_ERROR_ROLLBACK
public static final int ADD_UPDATE
public static final int ADD_ONLY
public static final int UPDATE_ONLY
public static final int DELETE
Constructor Detail |
---|
public Importer()
Method Detail |
---|
public void initHandlers()
public void importData(java.io.Reader xmlReader, Resolver resolver) throws java.lang.Exception
xmlReader
- The XML input source.resolver
- A resolver object to locate needed information.
org.xml.sax.SAXException
java.io.IOException
java.lang.Exception
public Dataset parseXML(java.io.Reader importReader, org.apache.commons.betwixt.io.BeanReader beanReader) throws java.beans.IntrospectionException, java.io.IOException, org.xml.sax.SAXException, java.lang.Exception
importReader
- The XML input streambeanReader
- The betwixt BeanReader configured to parse the XML input.
java.beans.IntrospectionException
java.io.IOException
org.xml.sax.SAXException
java.lang.Exception
public org.apache.commons.betwixt.io.BeanReader createBeanReader(Resolver resolver) throws java.beans.IntrospectionException, java.io.IOException, org.xml.sax.SAXException
resolver
- A class used to locate DTD and mapping files needed to
validate and convert the input XML into objects.. Cannot
be null.
java.beans.IntrospectionException
java.io.IOException
org.xml.sax.SAXException
public void processDataset(Dataset records) throws java.lang.Exception
records
- The dataset of records to process.
java.lang.Exception
protected void initOptions(Dataset records) throws java.lang.IllegalArgumentException, java.lang.Exception
records
- The dataset with the options
java.lang.IllegalArgumentException
- If an unknown value is found.
java.lang.Exception
protected void startTransaction(java.lang.Object record) throws org.apache.torque.TorqueException
record
-
org.apache.torque.TorqueException
protected RecordHandler findRecordHandler(java.lang.Object record) throws java.lang.CloneNotSupportedException
record
- The record to process.
java.lang.CloneNotSupportedException
protected void rollbackTransaction() throws java.lang.Exception
java.lang.Exception
public java.sql.Connection getConnection() throws java.lang.IllegalStateException
java.lang.IllegalStateException
- if has not been initializedprotected void setConnection(java.sql.Connection connection)
connection
- The connection to set.public void notifyListeners(int recordNum, java.lang.String action, java.lang.String info) throws java.lang.Exception
notifyListeners
in class XMLEventNotifier
action
- info
- recordNum
- the (1 based) index of the record object being
processed.
java.lang.Exception
public void registerListener(XMLEventListener listener)
registerListener
in class XMLEventNotifier
listener
- public void deregisterListener(XMLEventListener listener)
deregisterListener
in class XMLEventNotifier
listener
- protected java.util.List getListeners()
getListeners
in class XMLEventNotifier
public void registerHandler(java.lang.Class recordType, RecordHandler handler)
recordType
- handler
- protected java.util.Map getHandlers()
protected void setHandlers(java.util.Hashtable handlers)
handlers
- The handlers to set.public int getAction()
protected void setAction(int action)
action
- The action to set.public int getOnError()
protected void setOnError(int onError)
onError
- The onError to set.public void warning(org.xml.sax.SAXParseException e) throws org.xml.sax.SAXException
warning
in interface org.xml.sax.ErrorHandler
org.xml.sax.SAXException
public void error(org.xml.sax.SAXParseException e) throws org.xml.sax.SAXException
error
in interface org.xml.sax.ErrorHandler
org.xml.sax.SAXException
public void fatalError(org.xml.sax.SAXParseException e) throws org.xml.sax.SAXException
fatalError
in interface org.xml.sax.ErrorHandler
org.xml.sax.SAXException
protected boolean isErrorParsing()
protected void setErrorParsing(boolean errorParsing)
errorParsing
- The errorParsing to set.public java.lang.String simpleClassName(java.lang.Class c)
c
- A class object to find the simple name of.
public int getDatasetRecordNumber()
public void setDatasetRecordNumber(int datasetRecordNumber)
datasetRecordNumber
- The datasetRecordNumber to set.
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |