[Fix] Ensure removed elements are read-only before notifying observers

This commit is contained in:
Robert von Burg 2023-07-20 16:32:50 +02:00
parent 911e9af7e1
commit e35436b38d
Signed by: eitch
GPG Key ID: 75DB9C85C74331F7
1 changed files with 32 additions and 31 deletions

View File

@ -309,10 +309,10 @@ public abstract class AbstractTransaction implements StrolchTransaction {
this.lockedElements.add(locator);
switch (element.getObjectType()) {
case RESOURCE -> this.resourceCache.addElement(element.getType(), element.getId(), (Resource) element);
case ORDER -> this.orderCache.addElement(element.getType(), element.getId(), (Order) element);
case ACTIVITY -> this.activityCache.addElement(element.getType(), element.getId(), (Activity) element);
default -> throw new IllegalStateException("Unexpected object type " + element.getObjectType());
case RESOURCE -> this.resourceCache.addElement(element.getType(), element.getId(), (Resource) element);
case ORDER -> this.orderCache.addElement(element.getType(), element.getId(), (Order) element);
case ACTIVITY -> this.activityCache.addElement(element.getType(), element.getId(), (Activity) element);
default -> throw new IllegalStateException("Unexpected object type " + element.getObjectType());
}
}
@ -333,8 +333,7 @@ public abstract class AbstractTransaction implements StrolchTransaction {
default -> throw new IllegalStateException("Unexpected object type " + locator.get(0));
};
@SuppressWarnings("unchecked")
T t = (T) freshCopy;
@SuppressWarnings("unchecked") T t = (T) freshCopy;
return t;
}
@ -505,7 +504,8 @@ public abstract class AbstractTransaction implements StrolchTransaction {
// Order/<type>/<id>/Bag/<id>/<param_id>
if (locator.getSize() < 3) {
String msg = "The locator is invalid as it does not have at least three path elements (e.g. Resource/MyType/@id): {0}";
String msg
= "The locator is invalid as it does not have at least three path elements (e.g. Resource/MyType/@id): {0}";
msg = MessageFormat.format(msg, locator.toString());
throw new StrolchModelException(msg);
}
@ -1107,19 +1107,19 @@ public abstract class AbstractTransaction implements StrolchTransaction {
if (locator.getSize() != 3)
throw new IllegalStateException("Locator is invalid for cache removal: " + locator);
switch (locator.get(0)) {
case RESOURCE -> {
if (this.resourceCache != null)
this.resourceCache.removeElement(locator.get(1), locator.get(2));
}
case ORDER -> {
if (this.orderCache != null)
this.orderCache.removeElement(locator.get(1), locator.get(2));
}
case ACTIVITY -> {
if (this.activityCache != null)
this.activityCache.removeElement(locator.get(1), locator.get(2));
}
default -> throw new IllegalStateException("Unexpected object type " + locator.get(0));
case RESOURCE -> {
if (this.resourceCache != null)
this.resourceCache.removeElement(locator.get(1), locator.get(2));
}
case ORDER -> {
if (this.orderCache != null)
this.orderCache.removeElement(locator.get(1), locator.get(2));
}
case ACTIVITY -> {
if (this.activityCache != null)
this.activityCache.removeElement(locator.get(1), locator.get(2));
}
default -> throw new IllegalStateException("Unexpected object type " + locator.get(0));
}
this.objectFilter.removeObjectCache(locator.get(0), locator);
@ -1537,13 +1537,11 @@ public abstract class AbstractTransaction implements StrolchTransaction {
}
writeChanges();
long auditTrailDuration = writeAuditTrail();
commit();
long updateObserversDuration = updateObservers();
handleCommit(start, auditTrailDuration, updateObserversDuration);
this.txResult.setState(TransactionState.COMMITTED);
} catch (Exception e) {
@ -1603,13 +1601,15 @@ public abstract class AbstractTransaction implements StrolchTransaction {
if (this.closeStrategy != TransactionCloseStrategy.READ_ONLY) {
if (!this.commands.isEmpty()) {
autoCloseableRollback();
String msg = "There are commands registered on a read-only transaction. Changing to rollback! Did you forget to commit?";
String msg
= "There are commands registered on a read-only transaction. Changing to rollback! Did you forget to commit?";
throw new IllegalStateException(msg);
}
if (!this.objectFilter.isEmpty()) {
autoCloseableRollback();
String msg = "There are modified objects registered on a read-only transaction. Changing to rollback! Did you forget to commit?";
String msg
= "There are modified objects registered on a read-only transaction. Changing to rollback! Did you forget to commit?";
throw new IllegalStateException(msg);
}
}
@ -1828,7 +1828,8 @@ public abstract class AbstractTransaction implements StrolchTransaction {
event.updated.addList(Tags.RESOURCE, new ArrayList<>(updated));
Set<Resource> deleted = this.resourceMap.getDeleted();
if (!deleted.isEmpty())
event.removed.addList(Tags.RESOURCE, new ArrayList<>(deleted));
event.removed.addList(Tags.RESOURCE,
deleted.stream().map(e -> (StrolchRootElement) e.ensureReadOnly()).toList());
}
if (this.orderMap != null) {
@ -1840,7 +1841,8 @@ public abstract class AbstractTransaction implements StrolchTransaction {
event.updated.addList(Tags.ORDER, new ArrayList<>(updated));
Set<Order> deleted = this.orderMap.getDeleted();
if (!deleted.isEmpty())
event.removed.addList(Tags.ORDER, new ArrayList<>(deleted));
event.removed.addList(Tags.ORDER,
deleted.stream().map(e -> (StrolchRootElement) e.ensureReadOnly()).toList());
}
if (this.activityMap != null) {
@ -1852,7 +1854,8 @@ public abstract class AbstractTransaction implements StrolchTransaction {
event.updated.addList(Tags.ACTIVITY, new ArrayList<>(updated));
Set<Activity> deleted = this.activityMap.getDeleted();
if (!deleted.isEmpty())
event.removed.addList(Tags.ACTIVITY, new ArrayList<>(deleted));
event.removed.addList(Tags.ACTIVITY,
deleted.stream().map(e -> (StrolchRootElement) e.ensureReadOnly()).toList());
}
if (!(event.added.isEmpty() && event.updated.isEmpty() && event.removed.isEmpty())) {
@ -1941,11 +1944,9 @@ public abstract class AbstractTransaction implements StrolchTransaction {
audit.setId(StrolchAgent.getUniqueIdLong());
audit.setUsername(this.certificate.getUsername());
audit.setFirstname(this.certificate.getFirstname() == null ?
this.certificate.getUsername() :
audit.setFirstname(this.certificate.getFirstname() == null ? this.certificate.getUsername() :
this.certificate.getFirstname());
audit.setLastname(this.certificate.getLastname() == null ?
this.certificate.getUsername() :
audit.setLastname(this.certificate.getLastname() == null ? this.certificate.getUsername() :
this.certificate.getLastname());
audit.setDate(new Date());