package li.strolch.execution.service;

import li.strolch.execution.ExecutionHandler;
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.service.LocatorArgument;
import li.strolch.service.api.AbstractService;
import li.strolch.service.api.ServiceResult;
import li.strolch.service.api.ServiceResultState;

public class ExecuteActionService extends AbstractService<LocatorArgument, ServiceResult> {

	@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;
		Activity activity;
		try (StrolchTransaction tx = openArgOrUserTx(arg)) {
			realm = tx.getRealmName();

			tx.lock(arg.locator.trim(3));
			Action action = tx.findElement(arg.locator);

			// this is so we can re-execute stopped actions
			if (action.getState() == State.STOPPED) {
				action.setState(State.EXECUTABLE);

				tx.update(action.getRootElement());
				tx.commitOnClose();
			}

			activity = action.getRootElement();
		}

		getComponent(ExecutionHandler.class).toExecution(realm, activity);

		return ServiceResult.success();
	}
}

@@ -989,16 +989,112 @@
 im(3));%0A
+%0A%09%09%09if (arg.locator.getSize() == 3) %7B%0A%09%09%09%09activity = tx.findElement(arg.locator);%0A%09%09%09%7D else %7B%0A%0A%09
 %09%09%09Actio
@@ -1134,16 +1134,17 @@
 ator);%0A%0A
+%09
 %09%09%09// th
@@ -1185,24 +1185,25 @@
  actions%0A%09%09%09
+%09
 if (action.g
@@ -1228,24 +1228,25 @@
 .STOPPED) %7B%0A
+%09
 %09%09%09%09action.s
@@ -1277,16 +1277,17 @@
 );%0A%0A%09%09%09%09
+%09
 tx.updat
@@ -1318,16 +1318,17 @@
 ));%0A%09%09%09%09
+%09
 tx.commi
@@ -1339,16 +1339,17 @@
 lose();%0A
+%09
 %09%09%09%7D%0A%0A%09%09
@@ -1349,16 +1349,17 @@
 %09%09%7D%0A%0A%09%09%09
+%09
 activity
@@ -1386,16 +1386,21 @@
 ment();%0A
+%09%09%09%7D%0A
 %09%09%7D%0A%0A%09%09g

Handle locator is for a root element