[New] Implemented RemoveActivityArchival policy
This commit is contained in:
parent
01815ffc8e
commit
2ed8c8dc5f
|
@ -165,13 +165,44 @@ public class EventBasedExecutionHandler extends ExecutionHandler {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void archiveActivity(String realm, Locator activityLoc) {
|
||||||
|
this.executorService.execute(() -> {
|
||||||
|
try {
|
||||||
|
runAsAgent(ctx -> {
|
||||||
|
try (StrolchTransaction tx = openTx(realm, ctx.getCertificate(), ActivityArchivalPolicy.class)) {
|
||||||
|
tx.lock(activityLoc);
|
||||||
|
|
||||||
|
Activity activity = tx.findElement(activityLoc);
|
||||||
|
|
||||||
|
logger.info("Activity " + activity.getLocator() + " is in state " + activity.getState());
|
||||||
|
|
||||||
|
PolicyDef policyDef;
|
||||||
|
if (activity.hasPolicyDef(ActivityArchivalPolicy.class.getSimpleName())) {
|
||||||
|
policyDef = activity.getPolicyDef(ActivityArchivalPolicy.class.getSimpleName());
|
||||||
|
} else {
|
||||||
|
policyDef = PolicyDef.valueOf(ActivityArchivalPolicy.class.getSimpleName(),
|
||||||
|
KEY_DEFAULT_ACTIVITY_ARCHIVAL);
|
||||||
|
}
|
||||||
|
|
||||||
|
ActivityArchivalPolicy archivalPolicy = getComponent(PolicyHandler.class).getPolicy(policyDef,
|
||||||
|
tx);
|
||||||
|
archivalPolicy.archive(activity);
|
||||||
|
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} catch (Exception e) {
|
||||||
|
logger.error("Failed to archive " + activityLoc + " due to " + e.getMessage(), e);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
private void toExecution(String realm, Locator elementLoc, PrivilegeContext ctx) {
|
private void toExecution(String realm, Locator elementLoc, PrivilegeContext ctx) {
|
||||||
try (StrolchTransaction tx = openTx(realm, ctx.getCertificate(), ExecuteActivityCommand.class)) {
|
try (StrolchTransaction tx = openTx(realm, ctx.getCertificate(), ExecuteActivityCommand.class)) {
|
||||||
|
|
||||||
Locator activityLoc = elementLoc.trim(3);
|
Locator activityLoc = elementLoc.trim(3);
|
||||||
tx.lock(activityLoc);
|
tx.lock(activityLoc);
|
||||||
|
|
||||||
Activity activity = tx.findElement(activityLoc);
|
Activity activity = tx.findElement(activityLoc, true);
|
||||||
if (activity == null) {
|
if (activity == null) {
|
||||||
logger.error("Element for locator " + elementLoc + " does not exist!");
|
logger.error("Element for locator " + elementLoc + " does not exist!");
|
||||||
synchronized (this.registeredActivities) {
|
synchronized (this.registeredActivities) {
|
||||||
|
@ -211,12 +242,11 @@ public class EventBasedExecutionHandler extends ExecutionHandler {
|
||||||
if (activity.getState().isExecuted()) {
|
if (activity.getState().isExecuted()) {
|
||||||
|
|
||||||
synchronized (this.registeredActivities) {
|
synchronized (this.registeredActivities) {
|
||||||
this.registeredActivities.removeElement(realm, activityLoc);
|
if (!this.registeredActivities.removeElement(realm, activityLoc))
|
||||||
|
logger.warn("Activity " + actionLoc + " already removed from registered activities!");
|
||||||
}
|
}
|
||||||
|
|
||||||
archiveActivity(tx, activity);
|
archiveActivity(realm, activity.getLocator());
|
||||||
|
|
||||||
logger.info("Activity " + activityLoc + " is in state " + activity.getState());
|
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
|
@ -237,15 +267,6 @@ public class EventBasedExecutionHandler extends ExecutionHandler {
|
||||||
triggerExecution(realm);
|
triggerExecution(realm);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void archiveActivity(StrolchTransaction tx, Activity activity) {
|
|
||||||
|
|
||||||
PolicyDef policyDef = PolicyDef.valueOf(ActivityArchivalPolicy.class.getSimpleName(),
|
|
||||||
KEY_DEFAULT_ACTIVITY_ARCHIVAL);
|
|
||||||
|
|
||||||
ActivityArchivalPolicy archivalPolicy = getComponent(PolicyHandler.class).getPolicy(policyDef, tx);
|
|
||||||
archivalPolicy.archive(activity);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Triggers a to execution for all registered activities in the given realm
|
* Triggers a to execution for all registered activities in the given realm
|
||||||
*
|
*
|
||||||
|
|
|
@ -0,0 +1,26 @@
|
||||||
|
package li.strolch.execution.policy;
|
||||||
|
|
||||||
|
import li.strolch.agent.api.ComponentContainer;
|
||||||
|
import li.strolch.command.RemoveActivityCommand;
|
||||||
|
import li.strolch.model.activity.Activity;
|
||||||
|
import li.strolch.persistence.api.StrolchTransaction;
|
||||||
|
|
||||||
|
public class RemoveActivityArchival extends ActivityArchivalPolicy {
|
||||||
|
|
||||||
|
public RemoveActivityArchival(ComponentContainer container, StrolchTransaction tx) {
|
||||||
|
super(container, tx);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void archive(Activity activity) {
|
||||||
|
|
||||||
|
if (!activity.getState().isExecuted())
|
||||||
|
throw new IllegalStateException("Can not archive non-executed " + activity.getLocator());
|
||||||
|
|
||||||
|
RemoveActivityCommand cmd = new RemoveActivityCommand(getContainer(), tx());
|
||||||
|
cmd.setActivity(activity);
|
||||||
|
cmd.doCommand();
|
||||||
|
|
||||||
|
logger.info("Removed " + activity.getLocator());
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue