package com.ivo.bpm.core;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.activiti.engine.ProcessEngine;
import org.springframework.beans.factory.annotation.Autowired;
import com.ivo.bpm.Node;
import com.ivo.bpm.ProcessException;
import com.ivo.bpm.Transition;
import com.ivo.bpm.ex.Task;
import com.ivo.bpm.ex.TaskService;
import com.ivo.bpm.ex.TodoTask;
import com.ivo.bpm.record.Execution;
import com.ivo.bpm.record.ExecutionService;
import com.ivo.bpm.record.History;
import com.ivo.idm.Actor;
import com.ivo.idm.IdentityService;
import com.ivo.idm.model.Group;
import com.ivo.idm.model.User;
/**
* @author XC
*
*/
public abstract class AbstractNormalSign extends AbstractSign {
@Autowired
private ProcessEngine processEngine;
@Autowired
private ExecutionService executionService;
@Autowired
protected TaskService taskService;
@Autowired
private ProcessService processService;
@Autowired
private IdentityService is;
@Override
public void singal(Execution execution, Actor handler, List<String> uids,
String comment, Map<String, Object> map) {
singal(execution, handler, comment, assertNotNull(map));
}
public abstract void singal(Execution execution, Actor handler,
String comment, Map<String, Object> map);
private Map<String, Object> assertNotNull(Map<String, Object> map) {
if (map == null)
map = new HashMap<String, Object>();
return map;
}
protected void execute(Execution execution, Actor handler, String comment,
Map<String, Object> map, String transition) {
endProcess(execution, handler, comment, transition);
String nid = execution.getNode().getId();
if (nid.equals(Node.Recosign)) {
if (execution.getParent() == null)
throw new ProcessException("该征询意见环节存在异常,父节点不存在");
if (executionService.isFinished(execution)) {
createProcess(execution, handler, null, execution.getParent()
.getNode());
endExecution(execution.getParent(), false);
}
}else if (nid.equals(Node.CosignOver)) {
if (execution.getParent() == null)
throw new ProcessException("该会办环节存在异常,父节点不存在");
if (executionService.isFinished(execution)) {
createProcess(execution, handler, null, execution.getParent()
.getNode());
endExecution(execution.getParent(), false);
}
} else if (nid.equals(Node.PlusSign)) {
if (execution.getParent() == null)
throw new ProcessException("该加签环节存在异常,父节点不存在");
if (transition.equals(Transition.Submit)) {
createProcessRecord(execution, handler, null, execution.getParent()
.getNode());
endExecution(execution.getParent(), false);
} else if (transition.equals(Transition.Reject)) {
if (!execution.getParent().getNode().getId()
.equals(Node.Cosign))
executeTask(execution, transition, handler
.toString(), new Actor(execution.getHandleGroup(),
execution.getHandler()).toString(), map);
endExecution(execution.getParent(), false);
}
} else if (execution.getNode().getIsMulti()) {
String task = execution.getTask();
if (executionService.isFinished(execution.getTask()))
executeTask(
execution,
transition.equals(Transition.Reject)
|| processEngine.getTaskService()
.getVariableLocal(task, "multiReject") != null ? Transition.Reject
: Transition.Submit, handler.toString(),
new Actor(execution.getHandleGroup(), execution
.getHandler()).toString(), map);
else {
if (transition.equals(Transition.Reject))
if(execution.getNode().isCanMultiReject()){
executeTask(execution, transition, handler
.toString(), new Actor(execution.getHandleGroup(),
execution.getHandler()).toString(), map);
}else{
processEngine.getTaskService().setVariableLocal(task,
"multiReject", true);
}
}
} else if (execution.getNode().isCanMultiDraftSubmit()) {
String task = execution.getTask();
if (executionService.isNotDraftFinished(execution.getTask()))
executeTask(
execution,
transition.equals(Transition.Reject)
|| processEngine.getTaskService()
.getVariableLocal(task, "multiReject") != null ? Transition.Reject
: Transition.Submit, handler.toString(),
new Actor(execution.getHandleGroup(), execution
.getHandler()).toString(), map);
else {
if(handler.getUid().equals(execution.getDrafter().getId())){
executeTask(
execution,
transition.equals(Transition.Reject)
|| processEngine.getTaskService()
.getVariableLocal(task, "multiReject") != null ? Transition.Reject
: Transition.Submit, handler.toString(),
new Actor(execution.getHandleGroup(), execution
.getHandler()).toString(), map);
}
if (transition.equals(Transition.Reject))
if(execution.getNode().isCanMultiReject()){
executeTask(execution, transition, handler
.toString(), new Actor(execution.getHandleGroup(),
execution.getHandler()).toString(), map);
}else{
processEngine.getTaskService().setVariableLocal(task,
"multiReject", true);
}
}
}else {
executeTask(execution, transition, handler.toString(),
new Actor(execution.getHandleGroup(), execution
.getHandler()).toString(), map);
}
}
protected void executeTask(Execution execution, String transition, String handler,
String actualHandler, Map<String, Object> map) {
map.put("transition", transition);
map.put("handler", handler);
map.put("actualHandler", actualHandler);
User user = execution.getSender();
Actor actor = is.getActor(user.getId());
if(actor != null) map.put("sender", actor.toString());
processEngine.getTaskService().complete(execution.getTask(), map);
//重新set一下结束时间
Date now = new Date();
execution.getHistory().setHandleDate(now);
dao.save(execution.getHistory());
}
protected void createProcessRecord(Execution execution, Actor handler,
Actor actor, Node node) {
Execution process = new Execution();
process.setParent(actor == null ? execution.getParent().getParent()
: node == null ? execution.getParent() : execution);
process.setTask(execution.getTask());
process.setOrder(execution.getOrder());
process.setSource(execution.getSource());
process.setDraftGroup(execution.getDraftGroup());
process.setDrafter(execution.getDrafter());
process.setDraftDate(execution.getDraftDate());
process.setSender(handler != null ? new User(handler.getUid())
: execution.getHandler());
process.setHandleGroup(actor == null ? execution.getParent()
.getHandleGroup() : new Group(actor.getGid()));
process.setHandler(actor == null ? execution.getParent().getHandler()
: new User(actor.getUid()));
process.setNode(node == null ? execution.getNode() : node);
History childHistory = new History();
childHistory.setOrder(process.getOrder());
childHistory.setSource(process.getSource());
childHistory.setDraftGroup(process.getDraftGroup());
childHistory.setDrafter(process.getDrafter());
childHistory.setDraftDate(process.getDraftDate());
childHistory.setSendGroup(handler != null ? new Group(handler.getGid())
: execution.getHandleGroup());
childHistory.setSender(process.getSender());
childHistory.setHandleGroup(process.getHandleGroup());
childHistory.setHandler(process.getHandler());
childHistory.setNode(process.getNode());
dao.save(childHistory);
process.setHistory(childHistory);
dao.save(process);
TodoTask task = taskService.getTask(execution.getSource().getId(), process.getNode().getId(),
// new Actor(childHistory.getSendGroup().getId(), childHistory.getSender().getId()),
new Actor(execution.getHandleGroup().getId(), execution.getHandler().getId()),
new Actor(process.getHandleGroup().getId(), process.getHandler().getId()));
if (task != null ) {
if(task.getTask() != Task.PlusSignTask || !task.getHandler().equals(execution.getHandler().getId()) ){//发送人和加签人相同时不加签
processService
.signal(
process.getId(),
task.getHandler(),
task.getTask() == Task.PlusSignTask ? Transition.PlusSign
: Transition.ChangeSign);
}
}else {
String source = execution.getSource().getId();
if (source.equals("TR") || source.equals("LR")|| source.equals("KO")){
String actualHandler = execution.getDraftGroup().getId()+"/"+execution.getDrafter().getId();
TodoTask task2 = taskService.getTask(source, process.getNode().getId(),
new Actor(childHistory.getSendGroup().getId(), childHistory.getSender().getId()),
new Actor(process.getHandleGroup().getId(), process.getHandler().getId()));
if (task2 != null ) {
if(task2.getTask() != Task.PlusSignTask || !task2.getHandler().equals(execution.getHandler().getId()) ){//发送人和加签人相同时不加签
processService
.signal(
process.getId(),
task2.getHandler(),
task2.getTask() == Task.PlusSignTask ? Transition.PlusSign
: Transition.ChangeSign);
}
}
}
}
}
}