package foundation.workflow;
|
|
import java.util.ArrayList;
|
import java.util.List;
|
|
import foundation.dao.DataPackage;
|
import foundation.dao.DataReader;
|
import foundation.dao.DataWriter;
|
import foundation.dao.OperatorCode;
|
import foundation.dao.bizlogic.IActionProvider;
|
import foundation.handler.ResultPool;
|
import foundation.json.IJSONWriter;
|
import foundation.util.ContentBuilder;
|
import foundation.util.ID;
|
import foundation.util.Util;
|
|
public class WorkStep {
|
|
private DataReader dataReader;
|
private DataWriter dataWriter;
|
private WorkFlowRuntime workFlow;
|
private WorkStepMeta actionMeta;
|
private String dataName;
|
private DataPackage dataPackage;
|
private IActionProvider provider;
|
private String param;
|
private Event event;
|
private List<WorkStep> children;
|
private int currentStepNo;
|
private String instanceId;
|
|
public WorkStep() {
|
workFlow = new WorkFlowRuntime();
|
instanceId = ID.newValue();
|
}
|
|
public WorkStep(DataReader dataReader, DataWriter dataWriter, IWorkStep step) {
|
//1.
|
this.dataName = step.getDataName();
|
|
this.dataReader = dataReader;
|
this.dataWriter = dataWriter;
|
this.actionMeta = (WorkStepMeta)step;
|
this.param = step.getParam();
|
//2.
|
provider = step.createActionProvider();
|
}
|
|
public WorkStep(WorkFlowRuntime workFlow, IWorkStep step) {
|
//1.
|
this.workFlow = workFlow;
|
this.dataName = step.getDataName();
|
|
this.dataReader = workFlow.getDataReader();
|
this.dataWriter = workFlow.getDataWriter();
|
|
this.actionMeta = (WorkStepMeta)step;
|
this.param = step.getParam();
|
//2.
|
provider = step.createActionProvider();
|
}
|
|
public WorkStep newInstance() {
|
WorkStep result = new WorkStep();
|
|
result.workFlow = workFlow;
|
result.dataReader = dataReader;
|
result.dataWriter = dataWriter;
|
result.dataName = dataName;
|
|
return result;
|
}
|
|
public void setDataPackage(DataPackage dataPackage) {
|
this.dataPackage = dataPackage;
|
}
|
|
public String getInstanceId() {
|
return instanceId;
|
}
|
|
public WorkFlowRuntime getWorkFlow() {
|
return workFlow;
|
}
|
|
public double getTransactionScore() {
|
return actionMeta.getTransactionScore();
|
}
|
|
public void terminateTask() {
|
workFlow.terminate();
|
}
|
|
public boolean isTerminate() {
|
return workFlow.isTerminated();
|
}
|
|
public void setSimulate() {
|
workFlow.setSimulate(true);
|
}
|
|
public boolean isSimulate() {
|
return workFlow.isSimulate();
|
}
|
|
public int getCurrentStepNo() {
|
return currentStepNo;
|
}
|
|
public void goNextStep() {
|
currentStepNo++;
|
}
|
|
public OperatorCode getOperatorCode() {
|
return actionMeta.getOperatorCode();
|
}
|
|
public DataReader getDataReader() {
|
return dataReader;
|
}
|
|
public DataWriter getDataWriter() {
|
return dataWriter;
|
}
|
|
public ResultPool getResultPool() {
|
return dataWriter.getResultPool();
|
}
|
|
public String getDataName() throws Exception {
|
if (dataName == null) {
|
return dataReader.getString("dataname");
|
}
|
|
return dataName;
|
}
|
|
public DataPackage getDataPackage() throws Exception {
|
if (dataPackage == null) {
|
return dataReader.getDataPackage();
|
}
|
|
return dataPackage;
|
}
|
|
public IActionProvider getActionProvider() {
|
return provider;
|
}
|
|
public String getStepParam() {
|
if (Util.isEmpty(param)) {
|
return actionMeta.getActionParam();
|
}
|
|
return param;
|
}
|
|
public String getParam() {
|
return param;
|
}
|
|
public String getActionName() {
|
return param;
|
}
|
|
public String getMethodName() {
|
if (actionMeta != null) {
|
return actionMeta.getMethodName();
|
}
|
|
return null;
|
}
|
|
public String getDescription() {
|
String actionName = actionMeta.getActionName();
|
|
if (event != null) {
|
String onEvent = event.getCode();
|
actionName = actionName + "(" + onEvent + ")";
|
}
|
|
return actionName;
|
}
|
|
public void setWorkFlow(WorkFlowRuntime workFlow) {
|
this.workFlow = workFlow;
|
}
|
|
public List<WorkStep> getChildren() {
|
return children;
|
}
|
|
public void addOneChild(WorkStepMeta meta) {
|
if (meta == null) {
|
return;
|
}
|
|
if (children == null) {
|
children = new ArrayList<WorkStep>();
|
}
|
|
WorkStep step = new WorkStep(dataReader, dataWriter, meta);
|
children.add(step);
|
}
|
|
public void addChildren(List<WorkStepMeta> metas) {
|
if (metas == null || metas.isEmpty()) {
|
return;
|
}
|
|
if (children == null) {
|
children = new ArrayList<WorkStep>();
|
}
|
|
for (WorkStepMeta stepMeta: metas) {
|
WorkStep step = new WorkStep(workFlow, stepMeta);
|
children.add(step);
|
}
|
}
|
|
public void setEvent(Event event) {
|
this.event = event;
|
}
|
|
public Event getEvent() {
|
return event;
|
}
|
|
public <T>T getSender(Class<T> clazz, Moment moment) {
|
if (children != null) {
|
for (WorkStep child : children) {
|
T result = child.getSender(clazz, moment);
|
|
if (result != null) {
|
return (T)result;
|
}
|
}
|
}
|
|
if (event == null || event.getMoment() != moment) {
|
return null;
|
}
|
|
return event.getSender(clazz);
|
}
|
|
public <T>T getWorkflowSender(Class<T> clazz, Moment moment, EventScope scope) {
|
if (EventScope.CurrentStep == scope) {
|
return getSender(clazz, moment);
|
}
|
else if (EventScope.CurrentSteps == scope) {
|
return getSender(clazz, moment);
|
}
|
else if (EventScope.Children == scope) {
|
for (WorkStep child : children) {
|
T result = child.getSender(clazz, moment);
|
|
if (result != null) {
|
return (T)result;
|
}
|
}
|
|
return null;
|
}
|
|
return workFlow.getSender(clazz, moment);
|
}
|
|
public void setProvider(IActionProvider provider) {
|
this.provider = provider;
|
}
|
|
public void setParam(String param) {
|
this.param = param;
|
}
|
|
public String getParam(String paramName, String defaultValue) {
|
if(dataReader != null) {
|
return dataReader.getString(paramName, defaultValue);
|
}
|
|
return defaultValue;
|
}
|
|
public void toString(ContentBuilder result) {
|
result.append(actionMeta.toString());
|
}
|
|
public void writeJSON(IJSONWriter writer) {
|
writer.beginObject();
|
|
writer.write("param", param);
|
writer.write("description", getDescription());
|
|
writer.beginArray("children");
|
|
if (children != null) {
|
for (WorkStep child: children) {
|
child.writeJSON(writer);
|
}
|
}
|
|
writer.endArray();
|
|
writer.endObject();
|
}
|
}
|