protected PlainChangesLogImpl save(PlainChangesLog changesLog) throws InvalidItemStateException,
RepositoryException, IOException
{
// copy state
PlainChangesLogImpl newLog =
new PlainChangesLogImpl(new ArrayList<ItemState>(), changesLog.getSessionId(), changesLog.getEventType(),
changesLog.getPairId());
for (Iterator<ItemState> iter = changesLog.getAllStates().iterator(); iter.hasNext();)
{
ItemState prevState = iter.next();
ItemData newData;
if (prevState.getData() instanceof PersistedItemData)
{
// use existing if persisted
newData = prevState.getData();
}
else
{
// copy transient as persisted
if (prevState.isNode())
{
NodeData prevData = (NodeData)prevState.getData();
newData =
new PersistedNodeData(prevData.getIdentifier(), prevData.getQPath(),
prevData.getParentIdentifier(), prevData.getPersistedVersion() + 1, prevData.getOrderNumber(),
prevData.getPrimaryTypeName(), prevData.getMixinTypeNames(), prevData.getACL());
}
else
{
PropertyData prevData = (PropertyData)prevState.getData();
if (prevData.getValues() != null) // null if it's DELETE state
{
List<ValueData> values = new ArrayList<ValueData>();
for (int i = 0; i < prevData.getValues().size(); i++)
{
ValueData vd = prevData.getValues().get(i);
if (vd instanceof TransientValueData)
{
TransientValueData tvd = (TransientValueData)vd;
ValueData pvd;
if (vd.isByteArray())
{
pvd = new ByteArrayPersistedValueData(i, vd.getAsByteArray());
values.add(pvd);
}
else
{
// TODO ask dest file from VS provider, can be null after
// TODO for JBC case, the storage connection will evict the replicated Value to read it from the DB
File destFile = null;
if (tvd.getSpoolFile() != null)
{
// spooled to temp file
pvd = new StreamPersistedValueData(i, tvd.getSpoolFile(), destFile);
}
else
{
// with original stream
pvd = new StreamPersistedValueData(i, tvd.getOriginalStream(), destFile);
}
values.add(pvd);
}
tvd.delegate(pvd);
}
else
{
values.add(vd);
}
}
newData =
new PersistedPropertyData(prevData.getIdentifier(), prevData.getQPath(),
prevData.getParentIdentifier(), prevData.getPersistedVersion() + 1, prevData.getType(),
prevData.isMultiValued(), values);
}
else
{
newData =
new PersistedPropertyData(prevData.getIdentifier(), prevData.getQPath(),
prevData.getParentIdentifier(), prevData.getPersistedVersion() + 1, prevData.getType(),
prevData.isMultiValued(), null);
}
}
}
ItemState itemState =
new ItemState(newData, prevState.getState(), prevState.isEventFire(), prevState.getAncestorToSave(),
prevState.isInternallyCreated(), prevState.isPersisted());
newLog.add(itemState);
// save state
if (itemState.isPersisted())
{
long start = System.currentTimeMillis();