[Minor] Refactored ReservationExecution to be used elsewhere

This commit is contained in:
Robert von Burg 2020-02-26 09:26:50 +01:00
parent 6b7f118969
commit 937471c0cf
2 changed files with 25 additions and 41 deletions

View File

@ -5,11 +5,8 @@ import static li.strolch.runtime.StrolchConstants.PolicyConstants.*;
import li.strolch.exception.StrolchModelException; import li.strolch.exception.StrolchModelException;
import li.strolch.model.Resource; import li.strolch.model.Resource;
import li.strolch.model.State;
import li.strolch.model.activity.Action; import li.strolch.model.activity.Action;
import li.strolch.model.parameter.BooleanParameter; import li.strolch.model.parameter.BooleanParameter;
import li.strolch.model.timevalue.impl.FloatValue;
import li.strolch.model.timevalue.impl.ValueChange;
import li.strolch.persistence.api.StrolchTransaction; import li.strolch.persistence.api.StrolchTransaction;
import li.strolch.runtime.StrolchConstants; import li.strolch.runtime.StrolchConstants;
@ -36,41 +33,24 @@ public class ReservationExecution extends DurationExecution {
@Override @Override
public boolean isExecutable(Action action) { public boolean isExecutable(Action action) {
switch (action.getType()) {
case TYPE_RESERVE:
case TYPE_RELEASE:
if (action.getType().equals(TYPE_RESERVE))
return !isReserved(tx(), action);
return isReserved(tx(), action);
// only check if reserve default:
if (!action.getType().equals(TYPE_RESERVE) && !action.getType().equals(TYPE_RELEASE)) {
// otherwise delegate to super class
return super.isExecutable(action); return super.isExecutable(action);
} }
if (action.getType().equals(TYPE_RESERVE))
return !isReserved(action);
else
return isReserved(action);
}
protected boolean isReserved(Action action) {
// get resource
Resource resource = tx().getResourceFor(action, true);
if (!resource.hasParameter(BAG_PARAMETERS, PARAM_RESERVED))
throw new StrolchModelException(
"Parameter " + PARAM_RESERVED + " on bag " + BAG_PARAMETERS + " missing on " + resource
.getLocator());
BooleanParameter reservedP = resource.getParameter(BAG_PARAMETERS, PARAM_RESERVED);
return reservedP.getValue();
} }
@Override @Override
public void toExecution(Action action) { public void toExecution(Action action) {
switch (action.getType()) { switch (action.getType()) {
case TYPE_RESERVE: case TYPE_RESERVE:
case TYPE_RELEASE: case TYPE_RELEASE:
// async to executed only causes trouble =))
toExecuted(action); toExecuted(action);
break; break;
@ -81,35 +61,39 @@ public class ReservationExecution extends DurationExecution {
@Override @Override
public void toExecuted(Action action) { public void toExecuted(Action action) {
switch (action.getType()) { switch (action.getType()) {
case TYPE_RESERVE: case TYPE_RESERVE:
case TYPE_RELEASE: case TYPE_RELEASE:
boolean isReserve = action.getType().equals(TYPE_RESERVE); setReservation(tx(), action, action.getType().equals(TYPE_RESERVE));
setReservation(action, isReserve);
FloatValue value = new FloatValue(isReserve ? 1.0D : 0.0D);
action.addChange(new ValueChange<>(System.currentTimeMillis(), value, ""));
setActionState(action, State.EXECUTED);
break;
default: default:
super.toExecuted(action); super.toExecuted(action);
} }
} }
private void setReservation(Action action, boolean isReserve) { public static boolean isReserved(StrolchTransaction tx, Action action) {
Resource resource = tx().getResourceFor(action, true); // get resource
Resource resource = tx.getResourceFor(action, true);
if (!resource.hasParameter(BAG_PARAMETERS, PARAM_RESERVED))
throw new StrolchModelException(
"Parameter " + PARAM_RESERVED + " on bag " + BAG_PARAMETERS + " missing on " + resource
.getLocator());
BooleanParameter reservedP = resource.getParameter(BAG_PARAMETERS, PARAM_RESERVED);
return reservedP.getValue();
}
public static void setReservation(StrolchTransaction tx, Action action, boolean isReserve) {
Resource resource = tx.getResourceFor(action, true);
// release the resource // release the resource
BooleanParameter reservedP = resource.getParameter(BAG_PARAMETERS, PARAM_RESERVED); BooleanParameter reservedP = resource.getParameter(BAG_PARAMETERS, PARAM_RESERVED);
reservedP.setValue(isReserve); reservedP.setValue(isReserve);
// save changes // save changes
tx().update(resource); tx.update(resource);
} }
} }

View File

@ -39,7 +39,7 @@ public class ToErrorReservationExecution extends ReservationExecution {
@Override @Override
public void toExecution(Action action) { public void toExecution(Action action) {
if (action.getType().equals(TYPE_RESERVE) && isReserved(action)) { if (action.getType().equals(TYPE_RESERVE) && isReserved(tx(), action)) {
setActionState(action, State.EXECUTION); setActionState(action, State.EXECUTION);
toError(new LogMessage(tx().getRealmName(), tx().getCertificate().getUsername(), action.getLocator(), toError(new LogMessage(tx().getRealmName(), tx().getCertificate().getUsername(), action.getLocator(),
LogSeverity.Error, ResourceBundle.getBundle("strolch-service"), LogSeverity.Error, ResourceBundle.getBundle("strolch-service"),