public static final class ImportWorker.SqlRecordLoader extends RecordLoader
PropertyMapper
objects, in the order the
mapped data is expected to be encountered in the export file for
each record (one per column, or one per subscript value for extent
columns);
QueryHelper
objects used to resolve foreign
key associations, indexed by the name of the "foreign" class;
Method
objects used to set a "foreign" class
as property of the data model object currently being imported.
An instance of this class is created for each table which needs to be
imported. The above properties are set as necessary, using the various
set, put, and add methods. Once the instance is fully configured,
nextRecord(com.goldencode.p2j.util.Stream, org.hibernate.Session)
is called repeatedly, until there are no more
records to process in the input data file.
Modifier and Type | Field and Description |
---|---|
private java.util.Map<java.lang.String,java.util.Map<java.lang.String,java.lang.Object>> |
fkHqlMap
Reusable map of parameter data for HQL queries
|
private java.util.Map<java.lang.String,java.lang.reflect.Method> |
fkSetters
Map of setter methods used to set foreign key association DMOs
|
private java.util.Map<java.lang.String,BaseDataType> |
notNullProps
Map of not-null properties to their default values.
|
private java.util.Map<java.lang.String,ImportWorker.QueryHelper> |
queries
Query helpers used to resolve foreign key associations
|
private java.lang.String |
table
Name of database table
|
Constructor and Description |
---|
SqlRecordLoader(java.lang.Class<?> dmoClass,
java.lang.String table,
java.io.File dataFile,
boolean checkTypes)
Constructor.
|
Modifier and Type | Method and Description |
---|---|
void |
addNotNullProperty(java.lang.String prop,
BaseDataType def)
Add the not-null property with its default value, if any.
|
private java.lang.Object |
findUnique(ImportWorker.QueryHelper helper,
java.util.Map<java.lang.String,java.lang.Object> parms,
org.hibernate.Session session)
Execute the given query to produce a unique result.
|
(package private) java.util.Set<java.lang.String> |
getDependencies()
Return the set of all DMO classes which represent the tables which
must be loaded before this loader may import records into its
associated table.
|
ImportWorker.QueryHelper |
getQueryHelper(java.lang.String joinClass)
Get the query helper which corresponds with a class on the opposite
end of a foreign association.
|
java.lang.String |
getTable()
Get the name of the database table associated with this loader
instance.
|
(package private) java.lang.Object |
nextRecord(Stream input,
org.hibernate.Session session)
Read a single record from the current location in a Progress export
file and store its data in a bean-like object.
|
protected void |
processNextValue(PropertyMapper mapper,
java.lang.Object record,
java.lang.Object value)
Process the data value most recently read from the data export file.
|
void |
putForeignKeySetter(java.lang.String joinClass,
java.lang.reflect.Method setter)
Register a method to a class name.
|
void |
putQueryHelper(java.lang.String joinClass,
ImportWorker.QueryHelper helper)
Register a query helper to a class name.
|
addProperty, getDataFile, getDMOName, getProperty, nextRecord
private final java.util.Map<java.lang.String,ImportWorker.QueryHelper> queries
private final java.util.Map<java.lang.String,java.util.Map<java.lang.String,java.lang.Object>> fkHqlMap
private final java.util.Map<java.lang.String,java.lang.reflect.Method> fkSetters
private java.lang.String table
private java.util.Map<java.lang.String,BaseDataType> notNullProps
public SqlRecordLoader(java.lang.Class<?> dmoClass, java.lang.String table, java.io.File dataFile, boolean checkTypes)
Note: the checkTypes
flag should be set to
false
for normal operations. It is expensive and should
be enabled for debug purposes only.
dmoClass
- Class which is instantiated for each record imported.table
- Name of the table associated with this loader instance.dataFile
- Data input file.checkTypes
- true
to assert that the data type of each
field read matches the expected type.public void addNotNullProperty(java.lang.String prop, BaseDataType def)
prop
- The property name.def
- The default value.public java.lang.String getTable()
public ImportWorker.QueryHelper getQueryHelper(java.lang.String joinClass)
joinClass
- Name of class on the other end of a foreign association
with the DMO being loaded by this object.joinClass
.public void putQueryHelper(java.lang.String joinClass, ImportWorker.QueryHelper helper)
joinClass
- Name of the class of the object which will be retrieved
by the query.helper
- Helper object which will compose the query.public void putForeignKeySetter(java.lang.String joinClass, java.lang.reflect.Method setter)
joinClass
- Name of the class of the object which represents the
foreign end of a foreign key association.setter
- Method which will be invoked to store the foreign object
in the DMO currently being loaded.protected void processNextValue(PropertyMapper mapper, java.lang.Object record, java.lang.Object value) throws java.lang.IllegalAccessException, java.lang.reflect.InvocationTargetException
nextRecord
method for each data value read from the data export file. If the
current property is not delegated and not dropped,processNextValue
in class RecordLoader
mapper
- Mapping object associated with the current property.record
- Java-bean-like object which holds the contents of the record
currently being loaded.value
- Data value read from the data export file for the current
property. It has not yet been stored in the
record
object.java.lang.IllegalAccessException
- if the setter method which we invoke is not accessible.java.lang.reflect.InvocationTargetException
- if there is any error invoking the appropriate setter method.java.util.Set<java.lang.String> getDependencies()
java.lang.Object nextRecord(Stream input, org.hibernate.Session session) throws java.lang.InstantiationException, java.lang.IllegalAccessException, java.lang.reflect.InvocationTargetException
PropertyMapper
objects stored in this loader
instance.
The checkTypes
functionality is not honored here
although this would be the correct location to add that feature.
This method must consider columns which have been delegated to other tables, as well as foreign key associations to other tables. If a column is encountered which is flagged as a delegated column, its value is read and stored, but is not set into the DMO (no setter method will be available in this case).
If one or more foreign key associations must be resolved for the current record, a predefined HQL query is executed against Hibernate for each such association, using a map of data values read for the current record to bind as parameters.
input
- The export file to be read.session
- Hibernate session used to execute any queries necessary to
resolve foreign key associations.null
if no
more records are available.java.lang.InstantiationException
- if any error occurs creating a new instance of the DMO
class.java.lang.IllegalAccessException
- if the DMO class or its default constructor is not
accessible.java.lang.reflect.InvocationTargetException
- if there is any error invoking a setter method for an
individual record field.findUnique(com.goldencode.p2j.schema.ImportWorker.QueryHelper, java.util.Map<java.lang.String, java.lang.Object>, org.hibernate.Session)
private java.lang.Object findUnique(ImportWorker.QueryHelper helper, java.util.Map<java.lang.String,java.lang.Object> parms, org.hibernate.Session session) throws org.hibernate.HibernateException
Using an uninitialized proxy is sufficient, because we do not actually need the record's data. We only need its primary key, to satisfy the foreign key requirement of the DMO we are currently preparing for insertion into the current table.
helper
- Helper object which manages the Hibernate query to be
executed.parms
- A map of parameter names to parameter value objects. Each
entry is set into the query to replace the named parameter
placeholders defined for the query.session
- Hibernate database session object used to create the
Hibernate query to be executed.null
if no record matched the criteria
specified by the query.org.hibernate.HibernateException
- if any problem occurred executing the query, or if the
query returned a non-unique result.