From 7b2a516b383fdec815621944eb3ba6afb5d1df51 Mon Sep 17 00:00:00 2001 From: Robert von Burg Date: Wed, 15 Jul 2020 15:51:28 +0200 Subject: [PATCH] [New] Add SimpleExecution.runWithFreshAction() --- .../execution/policy/SimpleExecution.java | 47 ++++++++++++++++++- 1 file changed, 46 insertions(+), 1 deletion(-) diff --git a/li.strolch.service/src/main/java/li/strolch/execution/policy/SimpleExecution.java b/li.strolch.service/src/main/java/li/strolch/execution/policy/SimpleExecution.java index a33632405..a3b67ca0e 100644 --- a/li.strolch.service/src/main/java/li/strolch/execution/policy/SimpleExecution.java +++ b/li.strolch.service/src/main/java/li/strolch/execution/policy/SimpleExecution.java @@ -1,12 +1,17 @@ package li.strolch.execution.policy; -import li.strolch.model.log.LogMessage; +import java.util.function.BiConsumer; +import java.util.function.Consumer; + +import li.strolch.exception.StrolchException; import li.strolch.handler.operationslog.OperationsLog; import li.strolch.model.State; import li.strolch.model.activity.Action; +import li.strolch.model.log.LogMessage; import li.strolch.model.timevalue.impl.FloatValue; import li.strolch.model.timevalue.impl.ValueChange; import li.strolch.persistence.api.StrolchTransaction; +import li.strolch.privilege.model.PrivilegeContext; /** *

@@ -88,4 +93,44 @@ public class SimpleExecution extends ExecutionPolicy { addMessage(message); getController().asyncToWarning(message.getLocator()); } + + protected StrolchTransaction openLocalTx(PrivilegeContext ctx, boolean readOnly) throws StrolchException { + return getContainer().getRealm(ctx.getCertificate()).openTx(ctx.getCertificate(), getClass(), readOnly); + } + + protected void runWithFreshAction(Consumer consumer) { + runWithFreshAction(consumer, true); + } + + protected void runWithFreshAction(Consumer consumer, boolean readOnly) { + try { + runAsAgent(ctx -> { + try (StrolchTransaction tx = openLocalTx(ctx, readOnly)) { + tx.lock(this.actionLoc.trim(3)); + Action action = tx.findElement(this.actionLoc); + consumer.accept(action); + } + }); + } catch (Exception e) { + logger.error("Failed to perform consumer " + consumer.toString(), e); + } + } + + protected void runWithFreshAction(BiConsumer consumer) { + runWithFreshAction(consumer, true); + } + + protected void runWithFreshAction(BiConsumer consumer, boolean readOnly) { + try { + runAsAgent(ctx -> { + try (StrolchTransaction tx = openLocalTx(ctx, readOnly)) { + tx.lock(this.actionLoc.trim(3)); + Action action = tx.findElement(this.actionLoc); + consumer.accept(tx, action); + } + }); + } catch (Exception e) { + logger.error("Failed to perform consumer " + consumer.toString(), e); + } + } }