package foundation.state.approve;
|
|
import java.util.Iterator;
|
import java.util.List;
|
|
import foundation.action.ActionContext;
|
import foundation.data.entity.Entity;
|
import foundation.json.IJSONProvider;
|
import foundation.json.IJSONWriter;
|
import foundation.state.DetailState;
|
import foundation.token.IOnlineUser;
|
import foundation.util.MapList;
|
|
public class ApproveRuntime implements IJSONProvider, Iterable<Step> {
|
|
private String id;
|
private String name;
|
private boolean simpleList;
|
private MapList<String, Step> items;
|
|
public ApproveRuntime() {
|
items = new MapList<String, Step>();
|
}
|
|
public ApproveRuntime newEmptyInstance() {
|
ApproveRuntime result = new ApproveRuntime();
|
result.id = this.id;
|
result.name = this.name;
|
|
return result;
|
}
|
|
public static ApproveRuntime getInstance(String id) {
|
ApproveRuntimeBucket bucket = ApproveRuntimeBucket.getInstance();
|
|
ApproveRuntime result = bucket.get(id);
|
|
return result;
|
}
|
|
public void load(Entity entity) {
|
id = entity.getString("id");
|
name = entity.getString("name");
|
}
|
|
public ApproveRuntime getRuntime(IOnlineUser user, ActionContext context) throws Exception {
|
//1. 如果是简单列表,直接使用
|
if (simpleList) {
|
return this;
|
}
|
|
//2. 如果不是简单列表,需要根据RoleCode进行计算
|
ApproveRuntime result = newEmptyInstance();
|
|
for (Step step: items) {
|
StepType type = step.getType();
|
|
//2.1 如果是固定步骤,直接添加
|
if (StepType.ListStep == type) {
|
result.items.add(step.getId(), step);
|
continue;
|
}
|
|
//2.2 如果动态步骤,实时创建
|
List<Step> stepList = StepsCreator.createSalesHierarchySteps(type, user, context);
|
|
if (stepList == null) {
|
continue;
|
}
|
|
for (Step dynamic: stepList) {
|
result.items.add(dynamic.getId(), dynamic);
|
}
|
}
|
|
return result;
|
}
|
|
public String getId() {
|
return id;
|
}
|
|
public String getName() {
|
return name;
|
}
|
|
public void loadOneStep(Step step) {
|
simpleList = simpleList && (StepType.ListStep == step.getType());
|
items.add(step.getId(), step);
|
}
|
|
public boolean isSimpleList() {
|
return simpleList;
|
}
|
|
public DetailState getFirstDetailState() {
|
if (items.isEmpty()) {
|
return null;
|
}
|
|
Step step = items.get(0);
|
return step.getDetailState();
|
}
|
|
@Override
|
public Iterator<Step> iterator() {
|
return items.iterator();
|
}
|
|
@Override
|
public void writeJSON(IJSONWriter writer) {
|
writer.beginObject();
|
|
writer.write("id", id);
|
writer.write("name", name);
|
|
writer.beginArray("steps");
|
for (Step step: items) {
|
step.writeJSON(writer);
|
}
|
writer.endArray();
|
|
writer.endObject();
|
}
|
}
|