2015-08-22 20:44:31 +02:00
|
|
|
/*
|
|
|
|
* Copyright 2015 Robert von Burg <eitch@eitchnet.ch>
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
package li.strolch.policy;
|
|
|
|
|
2016-09-23 13:29:02 +02:00
|
|
|
import static li.strolch.utils.helper.StringHelper.DASH;
|
|
|
|
|
2015-08-22 23:00:50 +02:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
2015-08-22 20:44:31 +02:00
|
|
|
import li.strolch.agent.api.ComponentContainer;
|
|
|
|
import li.strolch.agent.api.StrolchComponent;
|
2016-09-23 13:29:02 +02:00
|
|
|
import li.strolch.exception.StrolchException;
|
|
|
|
import li.strolch.model.Resource;
|
|
|
|
import li.strolch.model.activity.Action;
|
2015-08-22 20:44:31 +02:00
|
|
|
import li.strolch.persistence.api.StrolchTransaction;
|
|
|
|
import li.strolch.service.api.Command;
|
2016-09-23 13:29:02 +02:00
|
|
|
import li.strolch.utils.helper.StringHelper;
|
2015-08-22 20:44:31 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Interface for all Strolch policies, which are instantiated by the {@link PolicyHandler}
|
2017-10-18 13:11:39 +02:00
|
|
|
*
|
2015-08-22 20:44:31 +02:00
|
|
|
* @author Robert von Burg <eitch@eitchnet.ch>
|
|
|
|
*/
|
|
|
|
public abstract class StrolchPolicy {
|
|
|
|
|
2015-08-22 23:00:50 +02:00
|
|
|
protected static final Logger logger = LoggerFactory.getLogger(StrolchPolicy.class);
|
|
|
|
|
2015-08-22 20:44:31 +02:00
|
|
|
private final ComponentContainer container;
|
|
|
|
private final StrolchTransaction tx;
|
|
|
|
|
|
|
|
/**
|
2017-10-18 13:11:39 +02:00
|
|
|
* Instantiate a new {@link StrolchPolicy}
|
|
|
|
*
|
2015-08-22 20:44:31 +02:00
|
|
|
* @param container
|
2017-10-18 13:11:39 +02:00
|
|
|
* the {@link ComponentContainer} to access components at runtime
|
2015-08-22 20:44:31 +02:00
|
|
|
* @param tx
|
2017-10-18 13:11:39 +02:00
|
|
|
* the transaction for this policy
|
2015-08-22 20:44:31 +02:00
|
|
|
*/
|
|
|
|
public StrolchPolicy(ComponentContainer container, StrolchTransaction tx) {
|
|
|
|
this.container = container;
|
|
|
|
this.tx = tx;
|
|
|
|
}
|
|
|
|
|
2019-05-30 17:15:19 +02:00
|
|
|
/**
|
|
|
|
* Instantiate a new {@link StrolchPolicy}
|
|
|
|
*
|
|
|
|
* @param tx
|
|
|
|
* the transaction for this policy
|
|
|
|
*/
|
|
|
|
public StrolchPolicy(StrolchTransaction tx) {
|
|
|
|
this.container = tx.getContainer();
|
|
|
|
this.tx = tx;
|
|
|
|
}
|
|
|
|
|
2015-08-22 20:44:31 +02:00
|
|
|
/**
|
|
|
|
* Allows the concrete {@link Command} implementation access to {@link StrolchComponent StrolchComponents} at
|
|
|
|
* runtime
|
2017-10-18 13:11:39 +02:00
|
|
|
*
|
2015-08-22 20:44:31 +02:00
|
|
|
* @param clazz
|
2017-10-18 13:11:39 +02:00
|
|
|
* the type of component to be returned
|
|
|
|
*
|
2015-08-22 20:44:31 +02:00
|
|
|
* @return the component with the given {@link Class} which is registered on the {@link ComponentContainer}
|
2017-10-18 13:11:39 +02:00
|
|
|
*
|
2015-08-22 20:44:31 +02:00
|
|
|
* @throws IllegalArgumentException
|
2017-10-18 13:11:39 +02:00
|
|
|
* if the component with the given class does not exist
|
2015-08-22 20:44:31 +02:00
|
|
|
*/
|
|
|
|
protected <V> V getComponent(Class<V> clazz) throws IllegalArgumentException {
|
|
|
|
return this.container.getComponent(clazz);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return the container
|
|
|
|
*/
|
|
|
|
protected ComponentContainer getContainer() {
|
|
|
|
return this.container;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the {@link StrolchTransaction} bound to this {@link Command}'s runtime
|
2017-10-18 13:11:39 +02:00
|
|
|
*
|
2015-08-22 20:44:31 +02:00
|
|
|
* @return the {@link StrolchTransaction} bound to this {@link Command}'s runtime
|
|
|
|
*/
|
|
|
|
protected StrolchTransaction tx() {
|
|
|
|
return this.tx;
|
|
|
|
}
|
2015-08-22 21:16:43 +02:00
|
|
|
|
2016-09-23 13:29:02 +02:00
|
|
|
/**
|
|
|
|
* Return the defined {@link Resource} on the given {@link Action}
|
2017-10-18 13:11:39 +02:00
|
|
|
*
|
2016-09-23 13:29:02 +02:00
|
|
|
* @param action
|
2017-10-18 13:11:39 +02:00
|
|
|
* the action for which to get the {@link Resource}
|
|
|
|
*
|
2016-09-23 13:29:02 +02:00
|
|
|
* @return the {@link Resource}
|
2017-10-18 13:11:39 +02:00
|
|
|
*
|
2016-09-23 13:29:02 +02:00
|
|
|
* @throws IllegalArgumentException
|
2017-10-18 13:11:39 +02:00
|
|
|
* if the resource is not defined on the action, i.e. fields are empty or a dash
|
2016-09-23 13:29:02 +02:00
|
|
|
* @throws StrolchException
|
2017-10-18 13:11:39 +02:00
|
|
|
* if the resource does not exist, which is referenced by the action
|
2016-09-23 13:29:02 +02:00
|
|
|
*/
|
|
|
|
protected Resource getResource(Action action) throws IllegalArgumentException, StrolchException {
|
|
|
|
String resourceId = action.getResourceId();
|
|
|
|
if (StringHelper.isEmpty(resourceId) || resourceId.equals(DASH))
|
|
|
|
throw new IllegalArgumentException("No resourceId defined on action " + action.getLocator());
|
|
|
|
String resourceType = action.getResourceType();
|
|
|
|
if (StringHelper.isEmpty(resourceType) || resourceType.equals(DASH))
|
|
|
|
throw new IllegalArgumentException("No resourceType defined on action " + action.getLocator());
|
|
|
|
|
2017-10-18 13:11:39 +02:00
|
|
|
return this.tx.getResourceBy(resourceType, resourceId, true);
|
2016-09-23 13:29:02 +02:00
|
|
|
}
|
|
|
|
|
2015-08-22 21:16:43 +02:00
|
|
|
/**
|
|
|
|
* @see Command#undo()
|
|
|
|
*/
|
2015-08-22 23:00:50 +02:00
|
|
|
public abstract void undo();
|
2015-08-22 20:44:31 +02:00
|
|
|
}
|