use of com.blazebit.persistence.view.spi.TransactionSupport in project blaze-persistence by Blazebit.
the class ResetInitialStateSynchronization method afterCompletion.
@Override
@SuppressWarnings("unchecked")
public void afterCompletion(int status) {
if (status != Status.STATUS_COMMITTED) {
Map<EntityViewProxy, ViewTransition> objects;
if (listenerManager.hasPostRollbackListeners()) {
objects = new IdentityHashMap<>();
} else {
objects = null;
}
if (coalescedInitialStates != null) {
for (int i = 0; i < coalescedInitialStates.size(); i += 2) {
Object[] initialState = coalescedInitialStates.get(i);
Object[] originalInitialState = coalescedInitialStates.get(i + 1);
for (int j = 0; j < initialState.length; j++) {
initialState[j] = originalInitialState[j];
}
}
}
if (coalescedRecordingActions != null) {
for (int i = 0; i < coalescedRecordingActions.size(); i += 4) {
Object collectionReference = coalescedRecordingActions.get(i);
Object actionList = coalescedRecordingActions.get(i + 1);
Map<Object, Object> added = (Map<Object, Object>) coalescedRecordingActions.get(i + 2);
Map<Object, Object> removed = (Map<Object, Object>) coalescedRecordingActions.get(i + 3);
if (collectionReference instanceof RecordingCollection<?, ?>) {
RecordingCollection<Collection<Object>, Object> collection = (RecordingCollection<Collection<Object>, Object>) collectionReference;
collection.setActions((List<CollectionAction<Collection<Object>>>) (List<?>) actionList, added, removed);
collection.$$_markDirty(-1);
} else {
Map<Object, Object> addedElements = (Map<Object, Object>) coalescedRecordingActions.get(i + 4);
Map<Object, Object> removedElements = (Map<Object, Object>) coalescedRecordingActions.get(i + 5);
RecordingMap<Map<Object, Object>, Object, Object> collection = (RecordingMap<Map<Object, Object>, Object, Object>) collectionReference;
collection.setActions((List<MapAction<Map<Object, Object>>>) (List<?>) actionList, added, removed, addedElements, removedElements);
collection.$$_markDirty(-1);
i += 2;
}
}
}
if (persistedViews != null) {
for (int i = 0; i < persistedViews.size(); i += 7) {
MutableStateTrackable view = (MutableStateTrackable) persistedViews.get(i);
Object newObject = persistedViews.get(i + 1);
view.$$_setIsNew(true);
Object id = persistedViews.get(i + 2);
DirtyTracker parent = (DirtyTracker) persistedViews.get(i + 3);
int parentIndex = (int) persistedViews.get(i + 4);
List<Object> readOnlyParents = (List<Object>) persistedViews.get(i + 5);
if (id != NO_ID_MARKER) {
view.$$_setId(id);
}
if (parent != null) {
// This is not needed as this will happen when rolling back collection actions already
if (!(parent instanceof RecordingCollection<?, ?> || parent instanceof RecordingMap<?, ?, ?>)) {
parent.$$_replaceAttribute(newObject, parentIndex, view);
}
for (int j = 0; j < readOnlyParents.size(); j += 2) {
DirtyTracker readOnlyParent = (DirtyTracker) readOnlyParents.get(j);
int readOnlyParentIndex = (int) readOnlyParents.get(j + 1);
readOnlyParent.$$_replaceAttribute(newObject, readOnlyParentIndex, view);
}
}
view.$$_setDirty((long[]) persistedViews.get(i + 6));
if (objects != null) {
objects.put(view, ViewTransition.PERSIST);
}
}
}
if (updatedViews != null) {
for (int i = 0; i < updatedViews.size(); i += 2) {
MutableStateTrackable view = (MutableStateTrackable) updatedViews.get(i);
view.$$_setDirty((long[]) updatedViews.get(i + 1));
if (objects != null) {
objects.put(view, ViewTransition.UPDATE);
}
}
}
if (removedViews != null) {
for (int i = 0; i < removedViews.size(); i += 4) {
EntityViewProxy view = (EntityViewProxy) removedViews.get(i);
if (view instanceof MutableStateTrackable) {
MutableStateTrackable removedView = (MutableStateTrackable) view;
DirtyTracker parent = (DirtyTracker) removedViews.get(i + 1);
if (parent != null) {
removedView.$$_setParent(parent, (Integer) removedViews.get(i + 2));
}
long[] dirtyArray = (long[]) removedViews.get(i + 3);
if (dirtyArray != null) {
removedView.$$_setDirty(dirtyArray);
}
}
if (objects != null) {
objects.put(view, ViewTransition.REMOVE);
}
}
}
if (versionedViews != null) {
for (int i = 0; i < versionedViews.size(); i += 2) {
MutableStateTrackable view = (MutableStateTrackable) versionedViews.get(i);
view.$$_setVersion(versionedViews.get(i + 1));
}
}
if (objects != null) {
PostRollbackInvoker postRollbackInvoker = new PostRollbackInvoker(updateContext, listenerManager, objects);
TransactionSupport txSupport = updateContext.getEntityViewManager().getService(TransactionSupport.class);
if (txSupport == null) {
TransactionAccess transactionAccess = updateContext.getTransactionAccess();
if (transactionAccess instanceof SynchronizationRegistry) {
transactionAccess = ((SynchronizationRegistry) transactionAccess).getTransactionAccess();
}
if (transactionAccess instanceof TransactionSupport) {
((TransactionSupport) transactionAccess).transactional(postRollbackInvoker);
} else {
// Log warning that querying doesn't work because we can't run in a new transaction?
postRollbackInvoker.run();
}
} else {
txSupport.transactional(postRollbackInvoker);
}
}
} else {
if (listenerManager.hasPostCommitListeners()) {
if (persistedViews != null) {
for (int i = 0; i < persistedViews.size(); i += 7) {
listenerManager.invokePostCommit(updateContext, (MutableStateTrackable) persistedViews.get(i), ViewTransition.PERSIST);
}
}
if (updatedViews != null) {
for (int i = 0; i < updatedViews.size(); i += 2) {
listenerManager.invokePostCommit(updateContext, (MutableStateTrackable) updatedViews.get(i), ViewTransition.UPDATE);
}
}
if (removedViews != null) {
for (int i = 0; i < removedViews.size(); i += 4) {
listenerManager.invokePostCommit(updateContext, (EntityViewProxy) removedViews.get(i), ViewTransition.REMOVE);
}
}
}
}
}
Aggregations