Set<ObjectProvider> opsToDelete = new HashSet<ObjectProvider>();
Set<ObjectProvider> opsToInsert = new HashSet<ObjectProvider>();
Iterator<ObjectProvider> opIter = opsToFlush.iterator();
while (opIter.hasNext())
{
ObjectProvider op = opIter.next();
if (op.isEmbedded())
{
op.markAsFlushed(); // Embedded have nothing to flush since the owner manages it
opIter.remove();
}
else
{
if (classesToFlush != null)
{
classesToFlush.add(op.getObject().getClass());
}
if (op.getLifecycleState().isNew() && !op.isFlushedToDatastore() && !op.isFlushedNew())
{
// P_NEW and not yet flushed to datastore
opsToInsert.add(op);
opIter.remove();
}
else if (op.getLifecycleState().isDeleted() && !op.isFlushedToDatastore())
{
if (!op.getLifecycleState().isNew())
{
// P_DELETED
opsToDelete.add(op);
opIter.remove();
}
else if (op.getLifecycleState().isNew() && op.isFlushedNew())
{
// P_NEW_DELETED already persisted
opsToDelete.add(op);
opIter.remove();
}
}
}
}
if (NucleusLogger.PERSISTENCE.isDebugEnabled())
{
NucleusLogger.PERSISTENCE.debug(LOCALISER.msg("010046", opsToDelete.size(), opsToInsert.size(), opsToFlush.size()));
}
if (!opsToDelete.isEmpty())
{
// Perform preDelete - deleteAll - postDelete, and mark all ObjectProviders as flushed
// TODO This omits some parts of sm.internalDeletePersistent
for (ObjectProvider op : opsToDelete)
{
op.setFlushing(true);
getCallbackHandler().preDelete(op.getObject());
}
try
{
persistenceHandler.deleteObjects(opsToDelete.toArray(new ObjectProvider[opsToDelete.size()]));
}
catch (NucleusOptimisticException noe)
{
// TODO Could get multiple failures from the deletions
if (optimisticFailures == null)
{
optimisticFailures = new ArrayList();
}
optimisticFailures.add(noe);
}
for (ObjectProvider op : opsToDelete)
{
getCallbackHandler().postDelete(op.getObject());
op.setFlushedNew(false);
op.markAsFlushed();
op.setFlushing(false);
}
}
if (!opsToInsert.isEmpty())
{
// Perform preStore - insertAll - postStore, and mark all ObjectProviders as flushed
// TODO This omits some parts of sm.internalMakePersistent
for (ObjectProvider op : opsToInsert)
{
op.setFlushing(true);
getCallbackHandler().preStore(op.getObject());
// TODO Make sure identity is set since user could have updated fields in preStore
}
persistenceHandler.insertObjects(opsToInsert.toArray(new ObjectProvider[opsToInsert.size()]));
for (ObjectProvider op : opsToInsert)
{
getCallbackHandler().postStore(op.getObject());
op.markAsFlushed();
op.setFlushing(false);
putObjectIntoCache(op); // Update the object in the cache(s) now that version/id are set
}
}
if (!opsToFlush.isEmpty())
{
// Objects to update
for (ObjectProvider op : opsToFlush)
{
try
{
op.flush();
}
catch (NucleusOptimisticException oe)
{
if (optimisticFailures == null)
{