*/
public void sort() {
// the list of entity names that indicate the batch number
for ( Iterator actionItr = insertions.iterator(); actionItr.hasNext(); ) {
EntityInsertAction action = ( EntityInsertAction ) actionItr.next();
// remove the current element from insertions. It will be added back later.
String entityName = action.getEntityName();
// the entity associated with the current action.
Object currentEntity = action.getInstance();
Integer batchNumber;
if ( latestBatches.containsKey( entityName ) ) {
// There is already an existing batch for this type of entity.
// Check to see if the latest batch is acceptable.
batchNumber = findBatchNumber( action, entityName );
}
else {
// add an entry for this type of entity.
// we can be assured that all referenced entities have already
// been processed,
// so specify that this entity is with the latest batch.
// doing the batch number before adding the name to the list is
// a faster way to get an accurate number.
batchNumber = new Integer( actionBatches.size() );
latestBatches.put( entityName, batchNumber );
}
entityBatchNumber.put( currentEntity, batchNumber );
addToBatch( batchNumber, action );
}
insertions.clear();
// now rebuild the insertions list. There is a batch for each entry in the name list.
for ( int i = 0; i < actionBatches.size(); i++ ) {
List batch = ( List ) actionBatches.get( new Integer( i ) );
for ( Iterator batchItr = batch.iterator(); batchItr.hasNext(); ) {
EntityInsertAction action = ( EntityInsertAction ) batchItr.next();
insertions.add( action );
}
}
}