diff --git a/li.strolch.model/src/main/java/li/strolch/model/State.java b/li.strolch.model/src/main/java/li/strolch/model/State.java index c84171e25..38f6a1fe1 100644 --- a/li.strolch.model/src/main/java/li/strolch/model/State.java +++ b/li.strolch.model/src/main/java/li/strolch/model/State.java @@ -147,31 +147,66 @@ public enum State { } /** - * @return true if {@link #EXECUTION} + * @return true if {@link #CREATED} or {@link #PLANNING} or {@link #PLANNED} or {@link #STOPPED} + */ + public boolean canSetToCreated() { + return this == CREATED || this == PLANNING || this == PLANNED || this == STOPPED; + } + + /** + * @return true if {@link #CREATED} or {@link #PLANNING} + */ + public boolean canSetToPlanning() { + return this == CREATED || this == PLANNING; + } + + /** + * @return true if {@link #CREATED} or {@link #PLANNING} or {@link #PLANNED} + */ + public boolean canSetToPlanned() { + return this == CREATED || this == PLANNING || this == PLANNED; + } + + /** + * @return true if {@link #CREATED} or {@link #PLANNING} or {@link #PLANNED} or {@link #EXECUTION} + */ + public boolean canSetToExecution() { + return this == CREATED || this == PLANNING || this == PLANNED || this == EXECUTION; + } + + /** + * @return true if {@link #EXECUTION} or {@link #WARNING} */ public boolean canSetToWarning() { return this == EXECUTION || this == WARNING; } /** - * @return true if {@link #ERROR} or {@link #STOPPED} + * @return true if {@link #ERROR} or {@link #STOPPED} or {@link #WARNING} */ public boolean canSetToStopped() { return this == ERROR || this == State.STOPPED || this == State.WARNING; } /** - * @return true if {@link #EXECUTION} or {@link #WARNING} + * @return true if {@link #EXECUTION} or {@link #WARNING} or {@link #ERROR} */ public boolean canSetToError() { return this == EXECUTION || this == WARNING || this == ERROR; } /** - * @return true if {@link #EXECUTION} or {@link #WARNING} or {@link #STOPPED} + * @return true if {@link #EXECUTION} or {@link #WARNING} or {@link #STOPPED} or {@link #EXECUTED} */ public boolean canSetToExecuted() { - return this == EXECUTION || this == WARNING || this == STOPPED; + return this == EXECUTION || this == WARNING || this == STOPPED || this == EXECUTED; + } + + /** + * @return true if {@link #EXECUTED} + */ + public boolean canSetToClosed() { + return this == EXECUTED; } public static State parseAllowNull(String s) { diff --git a/li.strolch.rest/src/main/java/li/strolch/rest/StrolchRestfulClasses.java b/li.strolch.rest/src/main/java/li/strolch/rest/StrolchRestfulClasses.java index c0ad2a7e3..e906ec668 100644 --- a/li.strolch.rest/src/main/java/li/strolch/rest/StrolchRestfulClasses.java +++ b/li.strolch.rest/src/main/java/li/strolch/rest/StrolchRestfulClasses.java @@ -39,6 +39,7 @@ public class StrolchRestfulClasses { restfulClasses.add(AuthenticationService.class); restfulClasses.add(StrolchJobsResource.class); restfulClasses.add(ReportResource.class); + restfulClasses.add(ControlResource.class); restfulClasses.add(Inspector.class); restfulClasses.add(VersionQuery.class); restfulClasses.add(ModelQuery.class); diff --git a/li.strolch.rest/src/main/java/li/strolch/rest/endpoint/ControlResource.java b/li.strolch.rest/src/main/java/li/strolch/rest/endpoint/ControlResource.java new file mode 100644 index 000000000..c6c66a5a6 --- /dev/null +++ b/li.strolch.rest/src/main/java/li/strolch/rest/endpoint/ControlResource.java @@ -0,0 +1,265 @@ +package li.strolch.rest.endpoint; + +import javax.servlet.http.HttpServletRequest; +import javax.ws.rs.*; +import javax.ws.rs.core.Context; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; +import java.util.Comparator; +import java.util.List; +import java.util.Objects; +import java.util.stream.Collectors; + +import com.google.gson.JsonElement; +import li.strolch.execution.ExecutionHandler; +import li.strolch.execution.service.*; +import li.strolch.model.Locator; +import li.strolch.model.State; +import li.strolch.model.activity.Activity; +import li.strolch.model.json.StrolchElementToJsonVisitor; +import li.strolch.persistence.api.StrolchTransaction; +import li.strolch.privilege.model.Certificate; +import li.strolch.rest.RestfulStrolchComponent; +import li.strolch.rest.StrolchRestfulConstants; +import li.strolch.rest.helper.ResponseUtil; +import li.strolch.service.LocatorArgument; +import li.strolch.service.api.ServiceArgument; +import li.strolch.service.api.ServiceResult; + +@Path("strolch/control") +public class ControlResource { + + private static String getContext() { + StackTraceElement element = new Throwable().getStackTrace()[2]; + return element.getClassName() + "." + element.getMethodName(); + } + + private StrolchTransaction openTx(Certificate certificate, String realm) { + return RestfulStrolchComponent.getInstance().openTx(certificate, realm, getContext()); + } + + @GET + @Produces(MediaType.APPLICATION_JSON) + public Response getActivities(@Context HttpServletRequest request, @QueryParam("realm") String realm) { + + Certificate cert = (Certificate) request.getAttribute(StrolchRestfulConstants.STROLCH_CERTIFICATE); + + StrolchElementToJsonVisitor visitor = new StrolchElementToJsonVisitor().withVersion().withLocator(); + + List activities; + try (StrolchTransaction tx = openTx(cert, realm)) { + activities = tx.getContainer().getComponent(ExecutionHandler.class).getActiveActivitiesLocator(realm) + .stream().map(locator -> tx.getActivityBy(locator.get(1), locator.get(2))).filter(Objects::nonNull) + .sorted(Comparator.comparing(Activity::getId)).map(activity -> activity.accept(visitor)) + .collect(Collectors.toList()); + } + + return ResponseUtil.toResponse(activities); + } + + @DELETE + @Path("all") + public Response clearAllActivities(@Context HttpServletRequest request, @QueryParam("realm") String realm) { + + Certificate cert = (Certificate) request.getAttribute(StrolchRestfulConstants.STROLCH_CERTIFICATE); + + RestfulStrolchComponent instance = RestfulStrolchComponent.getInstance(); + + ClearAllCurrentExecutionsService svc = new ClearAllCurrentExecutionsService(); + ServiceArgument arg = svc.getArgumentInstance(); + arg.realm = realm; + + ServiceResult svcResult = instance.getServiceHandler().doService(cert, svc, arg); + + return ResponseUtil.toResponse(svcResult); + } + + @POST + @Path("state") + public Response executeActivity(@Context HttpServletRequest request, @QueryParam("realm") String realm, + @QueryParam("locator") String locatorS, @QueryParam("state") String stateS) { + + Certificate cert = (Certificate) request.getAttribute(StrolchRestfulConstants.STROLCH_CERTIFICATE); + + Locator locator = Locator.valueOf(locatorS); + + RestfulStrolchComponent instance = RestfulStrolchComponent.getInstance(); + + StartActivityExecutionService svc = new StartActivityExecutionService(); + LocatorArgument arg = svc.getArgumentInstance(); + arg.locator = locator; + + ServiceResult svcResult = instance.getServiceHandler().doService(cert, svc, arg); + + return ResponseUtil.toResponse(svcResult); + } + + @DELETE + @Path("state") + public Response removeActivityFromExecution(@Context HttpServletRequest request, @QueryParam("realm") String realm, + @QueryParam("locator") String locatorS) { + + Certificate cert = (Certificate) request.getAttribute(StrolchRestfulConstants.STROLCH_CERTIFICATE); + + RestfulStrolchComponent instance = RestfulStrolchComponent.getInstance(); + + Locator locator = Locator.valueOf(locatorS); + + RemoveActivityFromExecutionService svc = new RemoveActivityFromExecutionService(); + LocatorArgument arg = svc.getArgumentInstance(); + arg.realm = realm; + arg.locator = locator; + + ServiceResult svcResult = instance.getServiceHandler().doService(cert, svc, arg); + + return ResponseUtil.toResponse(svcResult); + } + + @PUT + @Path("state") + public Response setElementState(@Context HttpServletRequest request, @QueryParam("realm") String realm, + @QueryParam("locator") String locatorS, @QueryParam("state") String stateS) { + + Certificate cert = (Certificate) request.getAttribute(StrolchRestfulConstants.STROLCH_CERTIFICATE); + + RestfulStrolchComponent instance = RestfulStrolchComponent.getInstance(); + + if (stateS.equals("Trigger")) { + + TriggerExecutionForRealmService svc = new TriggerExecutionForRealmService(); + ServiceArgument arg = svc.getArgumentInstance(); + arg.realm = realm; + + ServiceResult svcResult = instance.getServiceHandler().doService(cert, svc, arg); + return ResponseUtil.toResponse(svcResult); + + } else if (stateS.equals("ReloadActivities")) { + + ReloadActivitiesInExecutionService svc = new ReloadActivitiesInExecutionService(); + ServiceArgument arg = svc.getArgumentInstance(); + arg.realm = realm; + + ServiceResult svcResult = instance.getServiceHandler().doService(cert, svc, arg); + return ResponseUtil.toResponse(svcResult); + } + + State state = State.parse(stateS); + Locator locator = Locator.valueOf(locatorS); + + ServiceResult svcResult; + switch (state) { + case CREATED: { + + SetActionToCreatedService svc = new SetActionToCreatedService(); + LocatorArgument arg = svc.getArgumentInstance(); + arg.realm = realm; + arg.locator = locator; + + svcResult = instance.getServiceHandler().doService(cert, svc, arg); + + break; + } + + case PLANNING: { + + SetActionToPlanningService svc = new SetActionToPlanningService(); + LocatorArgument arg = svc.getArgumentInstance(); + arg.realm = realm; + arg.locator = locator; + + svcResult = instance.getServiceHandler().doService(cert, svc, arg); + + break; + } + + case PLANNED: { + + SetActionToPlannedService svc = new SetActionToPlannedService(); + LocatorArgument arg = svc.getArgumentInstance(); + arg.realm = realm; + arg.locator = locator; + + svcResult = instance.getServiceHandler().doService(cert, svc, arg); + + break; + } + + case EXECUTION: { + + SetToExecutionService svc = new SetToExecutionService(); + LocatorArgument arg = svc.getArgumentInstance(); + arg.realm = realm; + arg.locator = locator; + + svcResult = instance.getServiceHandler().doService(cert, svc, arg); + + break; + } + + case WARNING: { + + SetActionToWarningService svc = new SetActionToWarningService(); + LocatorArgument arg = svc.getArgumentInstance(); + arg.realm = realm; + arg.locator = locator; + + svcResult = instance.getServiceHandler().doService(cert, svc, arg); + + break; + } + + case ERROR: { + + SetActionToErrorService svc = new SetActionToErrorService(); + LocatorArgument arg = svc.getArgumentInstance(); + arg.realm = realm; + arg.locator = locator; + + svcResult = instance.getServiceHandler().doService(cert, svc, arg); + + break; + } + + case STOPPED: { + + SetActionToStoppedService svc = new SetActionToStoppedService(); + LocatorArgument arg = svc.getArgumentInstance(); + arg.realm = realm; + arg.locator = locator; + + svcResult = instance.getServiceHandler().doService(cert, svc, arg); + + break; + } + + case EXECUTED: { + + SetActionToExecutedService svc = new SetActionToExecutedService(); + LocatorArgument arg = svc.getArgumentInstance(); + arg.realm = realm; + arg.locator = locator; + + svcResult = instance.getServiceHandler().doService(cert, svc, arg); + + break; + } + + case CLOSED: { + + SetActionToClosedService svc = new SetActionToClosedService(); + LocatorArgument arg = svc.getArgumentInstance(); + arg.realm = realm; + arg.locator = locator; + + svcResult = instance.getServiceHandler().doService(cert, svc, arg); + + break; + } + + default: + throw new UnsupportedOperationException("Unhandled state " + state); + } + + return ResponseUtil.toResponse(svcResult); + } +} diff --git a/li.strolch.rest/src/main/java/li/strolch/rest/endpoint/Inspector.java b/li.strolch.rest/src/main/java/li/strolch/rest/endpoint/Inspector.java index 4a0c465ad..713445786 100644 --- a/li.strolch.rest/src/main/java/li/strolch/rest/endpoint/Inspector.java +++ b/li.strolch.rest/src/main/java/li/strolch/rest/endpoint/Inspector.java @@ -415,7 +415,7 @@ public class Inspector { // build JSON response ResourceVisitor visitor; if (overview == null || !overview) { - visitor = new StrolchRootElementToJsonVisitor().asResourceVisitor(); + visitor = new StrolchRootElementToJsonVisitor().withLocator().asResourceVisitor(); } else { visitor = e -> { JsonObject jsonObject = new JsonObject(); @@ -423,6 +423,7 @@ public class Inspector { jsonObject.addProperty(Json.ID, e.getId()); jsonObject.addProperty(Json.NAME, e.getName()); jsonObject.addProperty(Json.TYPE, e.getType()); + jsonObject.addProperty(Json.LOCATOR, e.getLocator().toString()); return jsonObject; }; } @@ -459,7 +460,7 @@ public class Inspector { // build JSON response OrderVisitor visitor; if (overview == null || !overview) { - visitor = new StrolchRootElementToJsonVisitor().asOrderVisitor(); + visitor = new StrolchRootElementToJsonVisitor().withLocator().asOrderVisitor(); } else { visitor = e -> { JsonObject jsonObject = new JsonObject(); @@ -467,6 +468,7 @@ public class Inspector { jsonObject.addProperty(Json.ID, e.getId()); jsonObject.addProperty(Json.NAME, e.getName()); jsonObject.addProperty(Json.TYPE, e.getType()); + jsonObject.addProperty(Json.LOCATOR, e.getLocator().toString()); jsonObject.addProperty(Json.STATE, e.getState().name()); jsonObject.addProperty(Json.DATE, ISO8601FormatFactory.getInstance().formatDate(e.getDate())); return jsonObject; @@ -505,7 +507,7 @@ public class Inspector { // build JSON response ActivityVisitor visitor; if (overview == null || !overview) { - visitor = new StrolchRootElementToJsonVisitor().asActivityVisitor(); + visitor = new StrolchRootElementToJsonVisitor().withLocator().asActivityVisitor(); } else { visitor = e -> { JsonObject jsonObject = new JsonObject(); @@ -513,6 +515,7 @@ public class Inspector { jsonObject.addProperty(Json.ID, e.getId()); jsonObject.addProperty(Json.NAME, e.getName()); jsonObject.addProperty(Json.TYPE, e.getType()); + jsonObject.addProperty(Json.LOCATOR, e.getLocator().toString()); jsonObject.addProperty(Json.STATE, e.getState().name()); jsonObject.addProperty(Json.TIME_ORDERING, e.getTimeOrdering().name()); return jsonObject; @@ -639,7 +642,7 @@ public class Inspector { throw new StrolchException(MessageFormat.format("No Resource exists for {0}/{1}", type, id)); //$NON-NLS-1$ } - StrolchElementToJsonVisitor visitor = new StrolchElementToJsonVisitor().withVersion(); + StrolchElementToJsonVisitor visitor = new StrolchElementToJsonVisitor().withLocator().withVersion(); if (Boolean.parseBoolean(flat)) visitor.flat(); @@ -682,7 +685,7 @@ public class Inspector { throw new StrolchException(MessageFormat.format("No Order exists for {0}/{1}", type, id)); //$NON-NLS-1$ } - StrolchElementToJsonVisitor visitor = new StrolchElementToJsonVisitor().withVersion(); + StrolchElementToJsonVisitor visitor = new StrolchElementToJsonVisitor().withLocator().withVersion(); if (Boolean.parseBoolean(flat)) visitor.flat(); return Response.ok().entity(toString(order.accept(visitor))).build(); @@ -724,7 +727,7 @@ public class Inspector { throw new StrolchException(MessageFormat.format("No Activity exists for {0}/{1}", type, id)); //$NON-NLS-1$ } - StrolchElementToJsonVisitor visitor = new StrolchElementToJsonVisitor().withVersion(); + StrolchElementToJsonVisitor visitor = new StrolchElementToJsonVisitor().withLocator().withVersion(); if (Boolean.parseBoolean(flat)) visitor.flat(); return Response.ok().entity(toString(activity.accept(visitor))).build(); @@ -818,7 +821,7 @@ public class Inspector { // do service ServiceResult result = RestfulStrolchComponent.getInstance().getServiceHandler().doService(cert, svc, arg); if (result.isOk()) { - StrolchElementToJsonVisitor toJsonVisitor = new StrolchElementToJsonVisitor().withVersion(); + StrolchElementToJsonVisitor toJsonVisitor = new StrolchElementToJsonVisitor().withLocator().withVersion(); if (flat) toJsonVisitor.flat(); return Response.ok().entity(toString(resource.accept(toJsonVisitor))).build(); @@ -895,7 +898,7 @@ public class Inspector { // do service ServiceResult result = RestfulStrolchComponent.getInstance().getServiceHandler().doService(cert, svc, arg); if (result.isOk()) { - StrolchElementToJsonVisitor toJsonVisitor = new StrolchElementToJsonVisitor().withVersion(); + StrolchElementToJsonVisitor toJsonVisitor = new StrolchElementToJsonVisitor().withLocator().withVersion(); if (flat) toJsonVisitor.flat(); return Response.ok().entity(toString(order.accept(toJsonVisitor))).build(); @@ -972,7 +975,7 @@ public class Inspector { // do service ServiceResult result = RestfulStrolchComponent.getInstance().getServiceHandler().doService(cert, svc, arg); if (result.isOk()) { - StrolchElementToJsonVisitor toJsonVisitor = new StrolchElementToJsonVisitor().withVersion(); + StrolchElementToJsonVisitor toJsonVisitor = new StrolchElementToJsonVisitor().withLocator().withVersion(); if (flat) toJsonVisitor.flat(); return Response.ok().entity(toString(activity.accept(toJsonVisitor))).build(); @@ -1073,7 +1076,7 @@ public class Inspector { ServiceResult result = RestfulStrolchComponent.getInstance().getServiceHandler().doService(cert, svc, arg); if (result.isOk()) { - StrolchElementToJsonVisitor toJsonVisitor = new StrolchElementToJsonVisitor().withVersion(); + StrolchElementToJsonVisitor toJsonVisitor = new StrolchElementToJsonVisitor().withLocator().withVersion(); return Response.ok().entity(toString(resource.accept(toJsonVisitor))).build(); } @@ -1099,7 +1102,7 @@ public class Inspector { ServiceResult result = RestfulStrolchComponent.getInstance().getServiceHandler().doService(cert, svc, arg); if (result.isOk()) { - StrolchElementToJsonVisitor toJsonVisitor = new StrolchElementToJsonVisitor().withVersion(); + StrolchElementToJsonVisitor toJsonVisitor = new StrolchElementToJsonVisitor().withLocator().withVersion(); if (flat) toJsonVisitor.flat(); return Response.ok().entity(toString(resource.accept(toJsonVisitor))).build(); @@ -1152,7 +1155,7 @@ public class Inspector { ServiceResult result = RestfulStrolchComponent.getInstance().getServiceHandler().doService(cert, svc, arg); if (result.isOk()) { - StrolchElementToJsonVisitor toJsonVisitor = new StrolchElementToJsonVisitor().withVersion(); + StrolchElementToJsonVisitor toJsonVisitor = new StrolchElementToJsonVisitor().withLocator().withVersion(); return Response.ok().entity(toString(order.accept(toJsonVisitor))).build(); } @@ -1178,7 +1181,7 @@ public class Inspector { ServiceResult result = RestfulStrolchComponent.getInstance().getServiceHandler().doService(cert, svc, arg); if (result.isOk()) { - StrolchElementToJsonVisitor toJsonVisitor = new StrolchElementToJsonVisitor().withVersion(); + StrolchElementToJsonVisitor toJsonVisitor = new StrolchElementToJsonVisitor().withLocator().withVersion(); if (flat) toJsonVisitor.flat(); return Response.ok().entity(toString(order.accept(toJsonVisitor))).build(); @@ -1233,7 +1236,7 @@ public class Inspector { ServiceResult result = RestfulStrolchComponent.getInstance().getServiceHandler().doService(cert, svc, arg); if (result.isOk()) { - StrolchElementToJsonVisitor toJsonVisitor = new StrolchElementToJsonVisitor().withVersion(); + StrolchElementToJsonVisitor toJsonVisitor = new StrolchElementToJsonVisitor().withLocator().withVersion(); return Response.ok().entity(toString(activity.accept(toJsonVisitor))).build(); } @@ -1259,7 +1262,7 @@ public class Inspector { ServiceResult result = RestfulStrolchComponent.getInstance().getServiceHandler().doService(cert, svc, arg); if (result.isOk()) { - StrolchElementToJsonVisitor toJsonVisitor = new StrolchElementToJsonVisitor().withVersion(); + StrolchElementToJsonVisitor toJsonVisitor = new StrolchElementToJsonVisitor().withLocator().withVersion(); if (flat) toJsonVisitor.flat(); return Response.ok().entity(toString(activity.accept(toJsonVisitor))).build(); diff --git a/li.strolch.rest/src/main/java/li/strolch/rest/helper/ResponseUtil.java b/li.strolch.rest/src/main/java/li/strolch/rest/helper/ResponseUtil.java index b1e3dffd1..2ae6d9865 100644 --- a/li.strolch.rest/src/main/java/li/strolch/rest/helper/ResponseUtil.java +++ b/li.strolch.rest/src/main/java/li/strolch/rest/helper/ResponseUtil.java @@ -86,7 +86,11 @@ public class ResponseUtil { return toResponse(member, list.stream().map(toJson).collect(Collectors.toList())); } - public static Response toResponse(String member, List jsonObjects) { + public static Response toResponse(List jsonObjects) { + return toResponse(DATA, jsonObjects); + } + + public static Response toResponse(String member, List jsonObjects) { JsonObject response = new JsonObject(); response.addProperty(MSG, StringHelper.DASH); diff --git a/li.strolch.service/src/main/java/li/strolch/execution/EventBasedExecutionHandler.java b/li.strolch.service/src/main/java/li/strolch/execution/EventBasedExecutionHandler.java index 6e0523b1e..c0d36affa 100644 --- a/li.strolch.service/src/main/java/li/strolch/execution/EventBasedExecutionHandler.java +++ b/li.strolch.service/src/main/java/li/strolch/execution/EventBasedExecutionHandler.java @@ -2,6 +2,8 @@ package li.strolch.execution; import static li.strolch.runtime.StrolchConstants.SYSTEM_USER_AGENT; +import java.util.Collections; +import java.util.HashSet; import java.util.ResourceBundle; import java.util.Set; import java.util.concurrent.ExecutorService; @@ -22,7 +24,6 @@ import li.strolch.model.policy.PolicyDef; import li.strolch.persistence.api.StrolchTransaction; import li.strolch.policy.PolicyHandler; import li.strolch.privilege.model.PrivilegeContext; -import li.strolch.runtime.StrolchConstants; import li.strolch.runtime.configuration.ComponentConfiguration; import li.strolch.utils.collections.MapOfSets; import li.strolch.utils.dbc.DBC; @@ -81,6 +82,16 @@ public class EventBasedExecutionHandler extends ExecutionHandler { super.stop(); } + @Override + public Set getActiveActivitiesLocator(String realm) { + if (this.registeredActivities == null || !this.registeredActivities.containsSet(realm)) + return Collections.emptySet(); + + synchronized (this.registeredActivities) { + return new HashSet<>(this.registeredActivities.getSet(realm)); + } + } + @Override public void addForExecution(String realm, Locator activityLoc) { Locator rootElemLoc = activityLoc.trim(3); @@ -98,50 +109,58 @@ public class EventBasedExecutionHandler extends ExecutionHandler { } } + @Override + public void clearAllCurrentExecutions(String realm) { + this.registeredActivities.removeSet(realm); + } + private void restartActivityExecution(PrivilegeContext ctx) { // iterate the realms for (String realmName : getContainer().getRealmNames()) { - - // open a TX for each realm - try (StrolchTransaction tx = openTx(realmName, ctx.getCertificate())) { - - // iterate all activities - tx.streamActivities().forEach(activity -> { - - if (activity.isReadOnly()) - activity = activity.getClone(true); - - // we only want to restart activities which were in execution - State state = activity.getState(); - if (!state.inExecutionPhase()) - return; - - logger.info("Starting Execution of " + activity.getLocator() + " on realm " + realmName); - - // Activities need to be in state STOPPED to restart - if (state == State.ERROR) { - activity.getActionsWithState(State.ERROR).forEach(a -> a.setState(State.STOPPED)); - } else if (state == State.WARNING) { - activity.getActionsWithState(State.WARNING).forEach(a -> a.setState(State.STOPPED)); - } else if (state == State.EXECUTION) { - activity.getActionsWithState(State.EXECUTION).forEach(a -> a.setState(State.STOPPED)); - } - tx.update(activity); - - // register for execution - this.registeredActivities.addElement(realmName, activity.getLocator()); - }); - - // commit changes to state - tx.commitOnClose(); - } - - // trigger execution of the registered activities - triggerExecution(realmName); + reloadActivitiesInExecution(ctx, realmName); } } + @Override + public void reloadActivitiesInExecution(PrivilegeContext ctx, String realmName) { + try (StrolchTransaction tx = openTx(realmName, ctx.getCertificate())) { + + // iterate all activities + tx.streamActivities().forEach(activity -> { + + if (activity.isReadOnly()) + activity = activity.getClone(true); + + // we only want to restart activities which were in execution + State state = activity.getState(); + if (!state.inExecutionPhase()) + return; + + logger.info("Starting Execution of " + activity.getLocator() + " on realm " + realmName); + + // Activities need to be in state STOPPED to restart + if (state == State.ERROR) { + activity.getActionsWithState(State.ERROR).forEach(a -> a.setState(State.STOPPED)); + } else if (state == State.WARNING) { + activity.getActionsWithState(State.WARNING).forEach(a -> a.setState(State.STOPPED)); + } else if (state == State.EXECUTION) { + activity.getActionsWithState(State.EXECUTION).forEach(a -> a.setState(State.STOPPED)); + } + tx.update(activity); + + // register for execution + this.registeredActivities.addElement(realmName, activity.getLocator()); + }); + + // commit changes to state + tx.commitOnClose(); + } + + // trigger execution of the registered activities + triggerExecution(realmName); + } + @Override public void triggerExecution(String realm) { synchronized (this.registeredActivities) { diff --git a/li.strolch.service/src/main/java/li/strolch/execution/ExecutionHandler.java b/li.strolch.service/src/main/java/li/strolch/execution/ExecutionHandler.java index 3e1f7c56c..fdbc6a9c4 100644 --- a/li.strolch.service/src/main/java/li/strolch/execution/ExecutionHandler.java +++ b/li.strolch.service/src/main/java/li/strolch/execution/ExecutionHandler.java @@ -1,5 +1,7 @@ package li.strolch.execution; +import java.util.Set; + import li.strolch.agent.api.ComponentContainer; import li.strolch.agent.api.StrolchComponent; import li.strolch.execution.policy.DurationExecution; @@ -9,6 +11,7 @@ import li.strolch.model.State; import li.strolch.model.activity.Action; import li.strolch.model.activity.Activity; import li.strolch.model.activity.TimeOrdering; +import li.strolch.privilege.model.PrivilegeContext; /** *

@@ -55,6 +58,24 @@ public abstract class ExecutionHandler extends StrolchComponent { */ public abstract void removeFromExecution(String realm, Locator activityLoc); + /** + * Restarts all existing Activities which are not yet executed and already in state of execution + * + * @param ctx + * the privilege context + * @param realm + * the realm for which to restart activities + */ + public abstract void reloadActivitiesInExecution(PrivilegeContext ctx, String realm); + + /** + * Removes all currently registered {@link Activity Activities} from execution + * + * @param realm + * the realm for which to restart activities + */ + public abstract void clearAllCurrentExecutions(String realm); + /** * Triggers a to execution for all registered activities in the given realm * @@ -73,6 +94,17 @@ public abstract class ExecutionHandler extends StrolchComponent { */ public abstract void archiveActivity(String realm, Locator activityLoc); + /** + * Returns the {@link Set} of {@link Locator Locators} of {@link Activity Activities} which are registered for + * execution for the given realm + * + * @param realm + * the realm for which to return the registered activities + * + * @return a set of locators + */ + public abstract Set getActiveActivitiesLocator(String realm); + /** *

* Returns the {@link DelayedExecutionTimer} diff --git a/li.strolch.service/src/main/java/li/strolch/execution/command/SetActionToClosedCommand.java b/li.strolch.service/src/main/java/li/strolch/execution/command/SetActionToClosedCommand.java new file mode 100644 index 000000000..76d0a82d1 --- /dev/null +++ b/li.strolch.service/src/main/java/li/strolch/execution/command/SetActionToClosedCommand.java @@ -0,0 +1,63 @@ +package li.strolch.execution.command; + +import java.text.MessageFormat; + +import li.strolch.agent.api.ComponentContainer; +import li.strolch.exception.StrolchException; +import li.strolch.model.State; +import li.strolch.model.activity.Action; +import li.strolch.model.activity.Activity; +import li.strolch.persistence.api.StrolchTransaction; +import li.strolch.utils.dbc.DBC; + +public class SetActionToClosedCommand extends ExecutionCommand { + + private Action action; + + public SetActionToClosedCommand(ComponentContainer container, StrolchTransaction tx) { + super(container, tx); + } + + public void setAction(Action action) { + this.action = action; + } + + @Override + public void validate() { + DBC.PRE.assertNotNull("action can not be null", this.action); + + tx().lock(this.action.getRootElement()); + tx().lock(getResourceLocator(this.action)); + + if (!this.action.getState().canSetToClosed()) { + String msg = "Current state is {0} and can not be changed to {1} for action {2}"; + msg = MessageFormat.format(msg, this.action.getState(), State.CLOSED, this.action.getLocator()); + throw new StrolchException(msg); + } + } + + @Override + public void doCommand() { + Activity rootElement = this.action.getRootElement(); + tx().lock(rootElement); + tx().lock(getResourceLocator(this.action)); + + if (this.action.getState() == State.CLOSED) { + logger.warn("Action " + this.action.getLocator() + " is already in CLOSED! Not changing."); + return; + } + + State currentState = rootElement.getState(); + + this.action.setState(State.CLOSED); + + getConfirmationPolicy(this.action).toClosed(this.action); + + updateOrderState(rootElement, currentState, rootElement.getState()); + } + + @Override + public void undo() { + // can not undo + } +} diff --git a/li.strolch.service/src/main/java/li/strolch/execution/command/SetActionToCreatedCommand.java b/li.strolch.service/src/main/java/li/strolch/execution/command/SetActionToCreatedCommand.java new file mode 100644 index 000000000..7ddb90df4 --- /dev/null +++ b/li.strolch.service/src/main/java/li/strolch/execution/command/SetActionToCreatedCommand.java @@ -0,0 +1,63 @@ +package li.strolch.execution.command; + +import java.text.MessageFormat; + +import li.strolch.agent.api.ComponentContainer; +import li.strolch.exception.StrolchException; +import li.strolch.model.State; +import li.strolch.model.activity.Action; +import li.strolch.model.activity.Activity; +import li.strolch.persistence.api.StrolchTransaction; +import li.strolch.utils.dbc.DBC; + +public class SetActionToCreatedCommand extends ExecutionCommand { + + private Action action; + + public SetActionToCreatedCommand(ComponentContainer container, StrolchTransaction tx) { + super(container, tx); + } + + public void setAction(Action action) { + this.action = action; + } + + @Override + public void validate() { + DBC.PRE.assertNotNull("action can not be null", this.action); + + tx().lock(this.action.getRootElement()); + tx().lock(getResourceLocator(this.action)); + + if (!this.action.getState().canSetToCreated()) { + String msg = "Current state is {0} and can not be changed to {1} for action {2}"; + msg = MessageFormat.format(msg, this.action.getState(), State.CREATED, this.action.getLocator()); + throw new StrolchException(msg); + } + } + + @Override + public void doCommand() { + Activity rootElement = this.action.getRootElement(); + tx().lock(rootElement); + tx().lock(getResourceLocator(this.action)); + + if (this.action.getState() == State.CREATED) { + logger.warn("Action " + this.action.getLocator() + " is already in CREATED! Not changing."); + return; + } + + State currentState = rootElement.getState(); + + this.action.setState(State.CREATED); + + getConfirmationPolicy(this.action).toCreated(this.action); + + updateOrderState(rootElement, currentState, rootElement.getState()); + } + + @Override + public void undo() { + // can not undo + } +} diff --git a/li.strolch.service/src/main/java/li/strolch/execution/command/SetActionToErrorCommand.java b/li.strolch.service/src/main/java/li/strolch/execution/command/SetActionToErrorCommand.java index fc1ab3f76..ac51c730c 100644 --- a/li.strolch.service/src/main/java/li/strolch/execution/command/SetActionToErrorCommand.java +++ b/li.strolch.service/src/main/java/li/strolch/execution/command/SetActionToErrorCommand.java @@ -30,7 +30,7 @@ public class SetActionToErrorCommand extends ExecutionCommand { tx().lock(getResourceLocator(this.action)); if (!this.action.getState().canSetToError()) { - String msg = "Current state is {0} and canot be changed to {1} for action {2}"; + String msg = "Current state is {0} and can not be changed to {1} for action {2}"; msg = MessageFormat.format(msg, this.action.getState(), State.ERROR, this.action.getLocator()); throw new StrolchException(msg); } diff --git a/li.strolch.service/src/main/java/li/strolch/execution/command/SetActionToExecutedCommand.java b/li.strolch.service/src/main/java/li/strolch/execution/command/SetActionToExecutedCommand.java index a835b8108..fdb3f0330 100644 --- a/li.strolch.service/src/main/java/li/strolch/execution/command/SetActionToExecutedCommand.java +++ b/li.strolch.service/src/main/java/li/strolch/execution/command/SetActionToExecutedCommand.java @@ -30,7 +30,7 @@ public class SetActionToExecutedCommand extends ExecutionCommand { tx().lock(getResourceLocator(this.action)); if (!this.action.getState().canSetToExecuted()) { - String msg = "Current state is {0} canot be changed to {1} for action {2}"; + String msg = "Current state is {0} can not be changed to {1} for action {2}"; msg = MessageFormat.format(msg, this.action.getState(), State.EXECUTED, this.action.getLocator()); throw new StrolchException(msg); } @@ -42,6 +42,11 @@ public class SetActionToExecutedCommand extends ExecutionCommand { tx().lock(rootElement); tx().lock(getResourceLocator(this.action)); + if (this.action.getState() == State.EXECUTED) { + logger.warn("Action " + this.action.getLocator() + " is already in EXECUTED! Not changing."); + return; + } + State currentState = rootElement.getState(); getExecutionPolicy(this.action).toExecuted(this.action); diff --git a/li.strolch.service/src/main/java/li/strolch/execution/command/SetActionToPlannedCommand.java b/li.strolch.service/src/main/java/li/strolch/execution/command/SetActionToPlannedCommand.java new file mode 100644 index 000000000..846f0095d --- /dev/null +++ b/li.strolch.service/src/main/java/li/strolch/execution/command/SetActionToPlannedCommand.java @@ -0,0 +1,63 @@ +package li.strolch.execution.command; + +import java.text.MessageFormat; + +import li.strolch.agent.api.ComponentContainer; +import li.strolch.exception.StrolchException; +import li.strolch.model.State; +import li.strolch.model.activity.Action; +import li.strolch.model.activity.Activity; +import li.strolch.persistence.api.StrolchTransaction; +import li.strolch.utils.dbc.DBC; + +public class SetActionToPlannedCommand extends ExecutionCommand { + + private Action action; + + public SetActionToPlannedCommand(ComponentContainer container, StrolchTransaction tx) { + super(container, tx); + } + + public void setAction(Action action) { + this.action = action; + } + + @Override + public void validate() { + DBC.PRE.assertNotNull("action can not be null", this.action); + + tx().lock(this.action.getRootElement()); + tx().lock(getResourceLocator(this.action)); + + if (!this.action.getState().canSetToPlanned()) { + String msg = "Current state is {0} and can not be changed to {1} for action {2}"; + msg = MessageFormat.format(msg, this.action.getState(), State.PLANNED, this.action.getLocator()); + throw new StrolchException(msg); + } + } + + @Override + public void doCommand() { + Activity rootElement = this.action.getRootElement(); + tx().lock(rootElement); + tx().lock(getResourceLocator(this.action)); + + if (this.action.getState() == State.PLANNED) { + logger.warn("Action " + this.action.getLocator() + " is already in PLANNED! Not changing."); + return; + } + + State currentState = rootElement.getState(); + + this.action.setState(State.PLANNED); + + getConfirmationPolicy(this.action).toPlanned(this.action); + + updateOrderState(rootElement, currentState, rootElement.getState()); + } + + @Override + public void undo() { + // can not undo + } +} diff --git a/li.strolch.service/src/main/java/li/strolch/execution/command/SetActionToPlanningCommand.java b/li.strolch.service/src/main/java/li/strolch/execution/command/SetActionToPlanningCommand.java new file mode 100644 index 000000000..32cbfb88e --- /dev/null +++ b/li.strolch.service/src/main/java/li/strolch/execution/command/SetActionToPlanningCommand.java @@ -0,0 +1,63 @@ +package li.strolch.execution.command; + +import java.text.MessageFormat; + +import li.strolch.agent.api.ComponentContainer; +import li.strolch.exception.StrolchException; +import li.strolch.model.State; +import li.strolch.model.activity.Action; +import li.strolch.model.activity.Activity; +import li.strolch.persistence.api.StrolchTransaction; +import li.strolch.utils.dbc.DBC; + +public class SetActionToPlanningCommand extends ExecutionCommand { + + private Action action; + + public SetActionToPlanningCommand(ComponentContainer container, StrolchTransaction tx) { + super(container, tx); + } + + public void setAction(Action action) { + this.action = action; + } + + @Override + public void validate() { + DBC.PRE.assertNotNull("action can not be null", this.action); + + tx().lock(this.action.getRootElement()); + tx().lock(getResourceLocator(this.action)); + + if (!this.action.getState().canSetToPlanning()) { + String msg = "Current state is {0} and can not be changed to {1} for action {2}"; + msg = MessageFormat.format(msg, this.action.getState(), State.PLANNING, this.action.getLocator()); + throw new StrolchException(msg); + } + } + + @Override + public void doCommand() { + Activity rootElement = this.action.getRootElement(); + tx().lock(rootElement); + tx().lock(getResourceLocator(this.action)); + + if (this.action.getState() == State.PLANNING) { + logger.warn("Action " + this.action.getLocator() + " is already in PLANNING! Not changing."); + return; + } + + State currentState = rootElement.getState(); + + this.action.setState(State.PLANNING); + + getConfirmationPolicy(this.action).toPlanning(this.action); + + updateOrderState(rootElement, currentState, rootElement.getState()); + } + + @Override + public void undo() { + // can not undo + } +} diff --git a/li.strolch.service/src/main/java/li/strolch/execution/command/SetActionToStoppedCommand.java b/li.strolch.service/src/main/java/li/strolch/execution/command/SetActionToStoppedCommand.java index 85b3b64bf..f45b26780 100644 --- a/li.strolch.service/src/main/java/li/strolch/execution/command/SetActionToStoppedCommand.java +++ b/li.strolch.service/src/main/java/li/strolch/execution/command/SetActionToStoppedCommand.java @@ -30,7 +30,7 @@ public class SetActionToStoppedCommand extends ExecutionCommand { tx().lock(getResourceLocator(this.action)); if (!this.action.getState().canSetToStopped()) { - String msg = "Current state is {0} and canot be changed to {1} for action {2}"; + String msg = "Current state is {0} and can not be changed to {1} for action {2}"; msg = MessageFormat.format(msg, this.action.getState(), State.STOPPED, this.action.getLocator()); throw new StrolchException(msg); } diff --git a/li.strolch.service/src/main/java/li/strolch/execution/command/SetActionToWarningCommand.java b/li.strolch.service/src/main/java/li/strolch/execution/command/SetActionToWarningCommand.java index c13c77c48..4cf980f82 100644 --- a/li.strolch.service/src/main/java/li/strolch/execution/command/SetActionToWarningCommand.java +++ b/li.strolch.service/src/main/java/li/strolch/execution/command/SetActionToWarningCommand.java @@ -30,7 +30,7 @@ public class SetActionToWarningCommand extends ExecutionCommand { tx().lock(getResourceLocator(this.action)); if (!this.action.getState().canSetToWarning()) { - String msg = "Current state is {0} and canot be changed to {1} for action {2}"; + String msg = "Current state is {0} and can not be changed to {1} for action {2}"; msg = MessageFormat.format(msg, this.action.getState(), State.WARNING, this.action.getLocator()); throw new StrolchException(msg); } diff --git a/li.strolch.service/src/main/java/li/strolch/execution/service/ClearAllCurrentExecutionsService.java b/li.strolch.service/src/main/java/li/strolch/execution/service/ClearAllCurrentExecutionsService.java new file mode 100644 index 000000000..aa4121bbe --- /dev/null +++ b/li.strolch.service/src/main/java/li/strolch/execution/service/ClearAllCurrentExecutionsService.java @@ -0,0 +1,33 @@ +package li.strolch.execution.service; + +import li.strolch.execution.ExecutionHandler; +import li.strolch.runtime.StrolchConstants; +import li.strolch.service.api.AbstractService; +import li.strolch.service.api.ServiceArgument; +import li.strolch.service.api.ServiceResult; +import li.strolch.service.api.ServiceResultState; +import li.strolch.utils.helper.StringHelper; + +public class ClearAllCurrentExecutionsService extends AbstractService { + + @Override + protected ServiceResult getResultInstance() { + return new ServiceResult(ServiceResultState.FAILED); + } + + @Override + public ServiceArgument getArgumentInstance() { + return new ServiceArgument(); + } + + @Override + protected ServiceResult internalDoService(ServiceArgument arg) throws Exception { + + String realm = StringHelper.isEmpty(arg.realm) ? StrolchConstants.DEFAULT_REALM : arg.realm; + + ExecutionHandler executionHandler = getContainer().getComponent(ExecutionHandler.class); + executionHandler.clearAllCurrentExecutions(realm); + + return ServiceResult.success(); + } +} diff --git a/li.strolch.service/src/main/java/li/strolch/execution/service/ReloadActivitiesInExecutionService.java b/li.strolch.service/src/main/java/li/strolch/execution/service/ReloadActivitiesInExecutionService.java new file mode 100644 index 000000000..1649e29c9 --- /dev/null +++ b/li.strolch.service/src/main/java/li/strolch/execution/service/ReloadActivitiesInExecutionService.java @@ -0,0 +1,33 @@ +package li.strolch.execution.service; + +import li.strolch.execution.ExecutionHandler; +import li.strolch.runtime.StrolchConstants; +import li.strolch.service.api.AbstractService; +import li.strolch.service.api.ServiceArgument; +import li.strolch.service.api.ServiceResult; +import li.strolch.service.api.ServiceResultState; +import li.strolch.utils.helper.StringHelper; + +public class ReloadActivitiesInExecutionService extends AbstractService { + + @Override + protected ServiceResult getResultInstance() { + return new ServiceResult(ServiceResultState.FAILED); + } + + @Override + public ServiceArgument getArgumentInstance() { + return new ServiceArgument(); + } + + @Override + protected ServiceResult internalDoService(ServiceArgument arg) throws Exception { + + String realm = StringHelper.isEmpty(arg.realm) ? StrolchConstants.DEFAULT_REALM : arg.realm; + + ExecutionHandler executionHandler = getContainer().getComponent(ExecutionHandler.class); + executionHandler.reloadActivitiesInExecution(getPrivilegeContext(), realm); + + return ServiceResult.success(); + } +} diff --git a/li.strolch.service/src/main/java/li/strolch/execution/service/RemoveActivityFromExecutionService.java b/li.strolch.service/src/main/java/li/strolch/execution/service/RemoveActivityFromExecutionService.java new file mode 100644 index 000000000..2053f813a --- /dev/null +++ b/li.strolch.service/src/main/java/li/strolch/execution/service/RemoveActivityFromExecutionService.java @@ -0,0 +1,33 @@ +package li.strolch.execution.service; + +import li.strolch.execution.ExecutionHandler; +import li.strolch.runtime.StrolchConstants; +import li.strolch.service.LocatorArgument; +import li.strolch.service.api.AbstractService; +import li.strolch.service.api.ServiceResult; +import li.strolch.service.api.ServiceResultState; +import li.strolch.utils.helper.StringHelper; + +public class RemoveActivityFromExecutionService extends AbstractService { + + @Override + protected ServiceResult getResultInstance() { + return new ServiceResult(ServiceResultState.FAILED); + } + + @Override + public LocatorArgument getArgumentInstance() { + return new LocatorArgument(); + } + + @Override + protected ServiceResult internalDoService(LocatorArgument arg) throws Exception { + + String realm = StringHelper.isEmpty(arg.realm) ? StrolchConstants.DEFAULT_REALM : arg.realm; + + ExecutionHandler executionHandler = getContainer().getComponent(ExecutionHandler.class); + executionHandler.removeFromExecution(realm, arg.locator); + + return ServiceResult.success(); + } +} diff --git a/li.strolch.service/src/main/java/li/strolch/execution/service/SetActionToClosedService.java b/li.strolch.service/src/main/java/li/strolch/execution/service/SetActionToClosedService.java new file mode 100644 index 000000000..ab914b0e3 --- /dev/null +++ b/li.strolch.service/src/main/java/li/strolch/execution/service/SetActionToClosedService.java @@ -0,0 +1,39 @@ +package li.strolch.execution.service; + +import li.strolch.execution.command.SetActionToClosedCommand; +import li.strolch.model.activity.Action; +import li.strolch.persistence.api.StrolchTransaction; +import li.strolch.service.LocatorArgument; +import li.strolch.service.api.AbstractService; +import li.strolch.service.api.ServiceResult; +import li.strolch.service.api.ServiceResultState; + +public class SetActionToClosedService extends AbstractService { + + @Override + protected ServiceResult getResultInstance() { + return new ServiceResult(ServiceResultState.FAILED); + } + + @Override + public LocatorArgument getArgumentInstance() { + return new LocatorArgument(); + } + + @Override + protected ServiceResult internalDoService(LocatorArgument arg) throws Exception { + + try (StrolchTransaction tx = openArgOrUserTx(arg)) { + + Action action = tx.findElement(arg.locator); + + SetActionToClosedCommand command = new SetActionToClosedCommand(getContainer(), tx); + command.setAction(action); + tx.addCommand(command); + + tx.commitOnClose(); + } + + return ServiceResult.success(); + } +} diff --git a/li.strolch.service/src/main/java/li/strolch/execution/service/SetActionToCreatedService.java b/li.strolch.service/src/main/java/li/strolch/execution/service/SetActionToCreatedService.java new file mode 100644 index 000000000..74b520236 --- /dev/null +++ b/li.strolch.service/src/main/java/li/strolch/execution/service/SetActionToCreatedService.java @@ -0,0 +1,39 @@ +package li.strolch.execution.service; + +import li.strolch.execution.command.SetActionToCreatedCommand; +import li.strolch.model.activity.Action; +import li.strolch.persistence.api.StrolchTransaction; +import li.strolch.service.LocatorArgument; +import li.strolch.service.api.AbstractService; +import li.strolch.service.api.ServiceResult; +import li.strolch.service.api.ServiceResultState; + +public class SetActionToCreatedService extends AbstractService { + + @Override + protected ServiceResult getResultInstance() { + return new ServiceResult(ServiceResultState.FAILED); + } + + @Override + public LocatorArgument getArgumentInstance() { + return new LocatorArgument(); + } + + @Override + protected ServiceResult internalDoService(LocatorArgument arg) throws Exception { + + try (StrolchTransaction tx = openArgOrUserTx(arg)) { + + Action action = tx.findElement(arg.locator); + + SetActionToCreatedCommand command = new SetActionToCreatedCommand(getContainer(), tx); + command.setAction(action); + tx.addCommand(command); + + tx.commitOnClose(); + } + + return ServiceResult.success(); + } +} diff --git a/li.strolch.service/src/main/java/li/strolch/execution/service/SetActionToErrorService.java b/li.strolch.service/src/main/java/li/strolch/execution/service/SetActionToErrorService.java new file mode 100644 index 000000000..cb0f7f63d --- /dev/null +++ b/li.strolch.service/src/main/java/li/strolch/execution/service/SetActionToErrorService.java @@ -0,0 +1,39 @@ +package li.strolch.execution.service; + +import li.strolch.execution.command.SetActionToErrorCommand; +import li.strolch.model.activity.Action; +import li.strolch.persistence.api.StrolchTransaction; +import li.strolch.service.LocatorArgument; +import li.strolch.service.api.AbstractService; +import li.strolch.service.api.ServiceResult; +import li.strolch.service.api.ServiceResultState; + +public class SetActionToErrorService extends AbstractService { + + @Override + protected ServiceResult getResultInstance() { + return new ServiceResult(ServiceResultState.FAILED); + } + + @Override + public LocatorArgument getArgumentInstance() { + return new LocatorArgument(); + } + + @Override + protected ServiceResult internalDoService(LocatorArgument arg) throws Exception { + + try (StrolchTransaction tx = openArgOrUserTx(arg)) { + + Action action = tx.findElement(arg.locator); + + SetActionToErrorCommand command = new SetActionToErrorCommand(getContainer(), tx); + command.setAction(action); + tx.addCommand(command); + + tx.commitOnClose(); + } + + return ServiceResult.success(); + } +} diff --git a/li.strolch.service/src/main/java/li/strolch/execution/service/SetActionToPlannedService.java b/li.strolch.service/src/main/java/li/strolch/execution/service/SetActionToPlannedService.java new file mode 100644 index 000000000..6929b90c8 --- /dev/null +++ b/li.strolch.service/src/main/java/li/strolch/execution/service/SetActionToPlannedService.java @@ -0,0 +1,39 @@ +package li.strolch.execution.service; + +import li.strolch.execution.command.SetActionToPlannedCommand; +import li.strolch.model.activity.Action; +import li.strolch.persistence.api.StrolchTransaction; +import li.strolch.service.LocatorArgument; +import li.strolch.service.api.AbstractService; +import li.strolch.service.api.ServiceResult; +import li.strolch.service.api.ServiceResultState; + +public class SetActionToPlannedService extends AbstractService { + + @Override + protected ServiceResult getResultInstance() { + return new ServiceResult(ServiceResultState.FAILED); + } + + @Override + public LocatorArgument getArgumentInstance() { + return new LocatorArgument(); + } + + @Override + protected ServiceResult internalDoService(LocatorArgument arg) throws Exception { + + try (StrolchTransaction tx = openArgOrUserTx(arg)) { + + Action action = tx.findElement(arg.locator); + + SetActionToPlannedCommand command = new SetActionToPlannedCommand(getContainer(), tx); + command.setAction(action); + tx.addCommand(command); + + tx.commitOnClose(); + } + + return ServiceResult.success(); + } +} diff --git a/li.strolch.service/src/main/java/li/strolch/execution/service/SetActionToPlanningService.java b/li.strolch.service/src/main/java/li/strolch/execution/service/SetActionToPlanningService.java new file mode 100644 index 000000000..bd0e9a653 --- /dev/null +++ b/li.strolch.service/src/main/java/li/strolch/execution/service/SetActionToPlanningService.java @@ -0,0 +1,39 @@ +package li.strolch.execution.service; + +import li.strolch.execution.command.SetActionToPlanningCommand; +import li.strolch.model.activity.Action; +import li.strolch.persistence.api.StrolchTransaction; +import li.strolch.service.LocatorArgument; +import li.strolch.service.api.AbstractService; +import li.strolch.service.api.ServiceResult; +import li.strolch.service.api.ServiceResultState; + +public class SetActionToPlanningService extends AbstractService { + + @Override + protected ServiceResult getResultInstance() { + return new ServiceResult(ServiceResultState.FAILED); + } + + @Override + public LocatorArgument getArgumentInstance() { + return new LocatorArgument(); + } + + @Override + protected ServiceResult internalDoService(LocatorArgument arg) throws Exception { + + try (StrolchTransaction tx = openArgOrUserTx(arg)) { + + Action action = tx.findElement(arg.locator); + + SetActionToPlanningCommand command = new SetActionToPlanningCommand(getContainer(), tx); + command.setAction(action); + tx.addCommand(command); + + tx.commitOnClose(); + } + + return ServiceResult.success(); + } +} diff --git a/li.strolch.service/src/main/java/li/strolch/execution/service/SetActionToStoppedService.java b/li.strolch.service/src/main/java/li/strolch/execution/service/SetActionToStoppedService.java index 2d0216c1e..6aff09572 100644 --- a/li.strolch.service/src/main/java/li/strolch/execution/service/SetActionToStoppedService.java +++ b/li.strolch.service/src/main/java/li/strolch/execution/service/SetActionToStoppedService.java @@ -1,12 +1,12 @@ package li.strolch.execution.service; -import li.strolch.execution.ExecutionHandler; -import li.strolch.runtime.StrolchConstants; +import li.strolch.execution.command.SetActionToStoppedCommand; +import li.strolch.model.activity.Action; +import li.strolch.persistence.api.StrolchTransaction; import li.strolch.service.LocatorArgument; import li.strolch.service.api.AbstractService; import li.strolch.service.api.ServiceResult; import li.strolch.service.api.ServiceResultState; -import li.strolch.utils.helper.StringHelper; public class SetActionToStoppedService extends AbstractService { @@ -23,10 +23,16 @@ public class SetActionToStoppedService extends AbstractService { + + @Override + protected ServiceResult getResultInstance() { + return new ServiceResult(ServiceResultState.FAILED); + } + + @Override + public LocatorArgument getArgumentInstance() { + return new LocatorArgument(); + } + + @Override + protected ServiceResult internalDoService(LocatorArgument arg) throws Exception { + + try (StrolchTransaction tx = openArgOrUserTx(arg)) { + + Action action = tx.findElement(arg.locator); + + SetActionToWarningCommand command = new SetActionToWarningCommand(getContainer(), tx); + command.setAction(action); + tx.addCommand(command); + + tx.commitOnClose(); + } + + return ServiceResult.success(); + } +} diff --git a/li.strolch.service/src/main/java/li/strolch/execution/service/SetToExecutionService.java b/li.strolch.service/src/main/java/li/strolch/execution/service/SetToExecutionService.java new file mode 100644 index 000000000..0ce43ecee --- /dev/null +++ b/li.strolch.service/src/main/java/li/strolch/execution/service/SetToExecutionService.java @@ -0,0 +1,51 @@ +package li.strolch.execution.service; + +import static li.strolch.utils.helper.StringHelper.isEmpty; + +import java.text.MessageFormat; + +import li.strolch.exception.StrolchException; +import li.strolch.execution.ExecutionHandler; +import li.strolch.model.State; +import li.strolch.model.activity.IActivityElement; +import li.strolch.persistence.api.StrolchTransaction; +import li.strolch.runtime.StrolchConstants; +import li.strolch.service.LocatorArgument; +import li.strolch.service.api.AbstractService; +import li.strolch.service.api.ServiceResult; +import li.strolch.service.api.ServiceResultState; + +public class SetToExecutionService extends AbstractService { + + @Override + protected ServiceResult getResultInstance() { + return new ServiceResult(ServiceResultState.FAILED); + } + + @Override + public LocatorArgument getArgumentInstance() { + return new LocatorArgument(); + } + + @Override + protected ServiceResult internalDoService(LocatorArgument arg) throws Exception { + + String realm = isEmpty(arg.realm) ? StrolchConstants.DEFAULT_REALM : arg.realm; + + try (StrolchTransaction tx = openTx(realm)) { + tx.lock(arg.locator); + + IActivityElement element = tx.findElement(arg.locator); + if (!element.getState().canSetToExecution()) { + String msg = "Current state is {0} and can not be changed to {1} for action {2}"; + msg = MessageFormat.format(msg, element.getState(), State.EXECUTION, element.getLocator()); + throw new StrolchException(msg); + } + + ExecutionHandler executionHandler = getContainer().getComponent(ExecutionHandler.class); + executionHandler.toExecution(realm, arg.locator); + } + + return ServiceResult.success(); + } +} diff --git a/li.strolch.service/src/main/java/li/strolch/execution/service/TriggerExecutionForRealmService.java b/li.strolch.service/src/main/java/li/strolch/execution/service/TriggerExecutionForRealmService.java new file mode 100644 index 000000000..819bf98f3 --- /dev/null +++ b/li.strolch.service/src/main/java/li/strolch/execution/service/TriggerExecutionForRealmService.java @@ -0,0 +1,33 @@ +package li.strolch.execution.service; + +import li.strolch.execution.ExecutionHandler; +import li.strolch.runtime.StrolchConstants; +import li.strolch.service.api.AbstractService; +import li.strolch.service.api.ServiceArgument; +import li.strolch.service.api.ServiceResult; +import li.strolch.service.api.ServiceResultState; +import li.strolch.utils.helper.StringHelper; + +public class TriggerExecutionForRealmService extends AbstractService { + + @Override + protected ServiceResult getResultInstance() { + return new ServiceResult(ServiceResultState.FAILED); + } + + @Override + public ServiceArgument getArgumentInstance() { + return new ServiceArgument(); + } + + @Override + protected ServiceResult internalDoService(ServiceArgument arg) throws Exception { + + String realm = StringHelper.isEmpty(arg.realm) ? StrolchConstants.DEFAULT_REALM : arg.realm; + + ExecutionHandler executionHandler = getContainer().getComponent(ExecutionHandler.class); + executionHandler.triggerExecution(realm); + + return ServiceResult.success(); + } +}