@Service
public class WorkFlowServiceImpl implements WorkFlowService {
@Autowired
private RepositoryService repositoryService;
@Autowired
private RuntimeService runtimeService;
@Autowired
private TaskService taskService;
@Autowired
private HistoryService historyService;
@Autowired
private IdentityService identityService;
@Autowired
private FormService formService;
@Autowired
private ManagementService managementService;
@Autowired
private LeaveBillMapper billMapper;
public DataGridView queryProcessDeploy(WorkFlowVo workFlowVo) {
if (workFlowVo.getDeploymentName() == null) {
workFlowVo.setDeploymentName("");
}
String name = workFlowVo.getDeploymentName();
long count = repositoryService.createDeploymentQuery().deploymentNameLike("%" + name + "%").count();
int firstResult = (workFlowVo.getPage() - 1) * workFlowVo.getLimit();
int maxResults = workFlowVo.getLimit();
List<Deployment> list = repositoryService.createDeploymentQuery().deploymentNameLike("%" + name + "%")
.listPage(firstResult, maxResults);
List<ActDeploymentEntity> data = new ArrayList<ActDeploymentEntity>();
for (Deployment deployment : list) {
ActDeploymentEntity entity = new ActDeploymentEntity();
BeanUtils.copyProperties(deployment, entity);
data.add(entity);
}
return new DataGridView(count, data);
}
@Override
public DataGridView queryAllProcessDefinition(WorkFlowVo workFlowVo) {
if (workFlowVo.getDeploymentName() == null) {
workFlowVo.setDeploymentName("");
}
String name = workFlowVo.getDeploymentName();
List<Deployment> dlist = repositoryService.createDeploymentQuery().deploymentNameLike("%" + name + "%").list();
Set<String> deploymentIds = new HashSet<>();
for (Deployment deployment : dlist) {
deploymentIds.add(deployment.getId());
}
long count = 0;
List<ActProcessDefinitionEntity> data = new ArrayList<>();
if (deploymentIds.size() > 0) {
count = this.repositoryService.createProcessDefinitionQuery().deploymentIds(deploymentIds).count();
int firstResult = (workFlowVo.getPage() - 1) * workFlowVo.getLimit();
int maxResults = workFlowVo.getLimit();
List<ProcessDefinition> list = this.repositoryService.createProcessDefinitionQuery()
.deploymentIds(deploymentIds).listPage(firstResult, maxResults);
for (ProcessDefinition pd : list) {
ActProcessDefinitionEntity entity = new ActProcessDefinitionEntity();
BeanUtils.copyProperties(pd, entity);
data.add(entity);
}
}
return new DataGridView(count, data);
}
@Override
public void addWorkFlow(InputStream inputStream, String deploymentName) {
ZipInputStream zipInputStream = new ZipInputStream(inputStream);
this.repositoryService.createDeployment().name(deploymentName).addZipInputStream(zipInputStream).deploy();
try {
zipInputStream.close();
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void deleteWorkFlow(String deploymentId) {
this.repositoryService.deleteDeployment(deploymentId, true);
}
@Override
public InputStream queryProcessDeploymentImage(String deploymentId) {
ProcessDefinition processDefinition = this.repositoryService.createProcessDefinitionQuery()
.deploymentId(deploymentId).singleResult();
String resourceName = processDefinition.getDiagramResourceName();
InputStream stream = this.repositoryService.getResourceAsStream(deploymentId, resourceName);
return stream;
}
@Override
public void startProcess(Integer leaveBillId) {
String processDefinitionKey = LeaveBill.class.getSimpleName();
String businessKey = processDefinitionKey + ":" + leaveBillId;
Map<String, Object> variables = new HashMap<>();
variables.put("username", SessionUtils.getCurrentUserName());
this.runtimeService.startProcessInstanceByKey(processDefinitionKey, businessKey, variables);
LeaveBill leaveBill = billMapper.selectByPrimaryKey(leaveBillId);
leaveBill.setState(SYSConstast.STATE_LEAVEBILL_ONE);
this.billMapper.updateByPrimaryKeySelective(leaveBill);
}
@Override
public DataGridView queryCurrentUserTask(WorkFlowVo workFlowVo) {
String assignee = SessionUtils.getCurrentUserName();
long count = this.taskService.createTaskQuery().taskAssignee(assignee).count();
int firstResult = (workFlowVo.getPage() - 1) * workFlowVo.getLimit();
int maxResults = workFlowVo.getLimit();
List<Task> list = this.taskService.createTaskQuery().taskAssignee(assignee).listPage(firstResult, maxResults);
List<ActTaskEntity> taskEntities = new ArrayList<>();
for (Task task : list) {
ActTaskEntity entity = new ActTaskEntity();
BeanUtils.copyProperties(task, entity);
taskEntities.add(entity);
}
return new DataGridView(count, taskEntities);
}
@Override
public LeaveBill queryLeaveBillByTaskId(String taskId) {
Task task = this.taskService.createTaskQuery().taskId(taskId).singleResult();
String processInstanceId = task.getProcessInstanceId();
ProcessInstance processInstance = this.runtimeService.createProcessInstanceQuery()
.processInstanceId(processInstanceId).singleResult();
String businessKey = processInstance.getBusinessKey();
String leaveBillId = businessKey.split(":")[1];
return this.billMapper.selectByPrimaryKey(Integer.valueOf(leaveBillId));
}
@Override
public List<String> queryOutComeByTaskId(String taskId) {
List<String> names = new ArrayList<>();
Task task = this.taskService.createTaskQuery().taskId(taskId).singleResult();
String processDefinitionId = task.getProcessDefinitionId();
String processInstanceId = task.getProcessInstanceId();
ProcessInstance processInstance = this.runtimeService.createProcessInstanceQuery()
.processInstanceId(processInstanceId).singleResult();
ProcessDefinitionEntity processDefinition = (ProcessDefinitionEntity) this.repositoryService
.getProcessDefinition(processDefinitionId);
String activityId = processInstance.getActivityId();
ActivityImpl activityImpl = processDefinition.findActivity(activityId);
List<PvmTransition> transitions = activityImpl.getOutgoingTransitions();
if (null != transitions && transitions.size() > 0) {
for (PvmTransition pvmTransition : transitions) {
String name = pvmTransition.getProperty("name").toString();
names.add(name);
}
}
return names;
}
@Override
public DataGridView queryCommentByTaskId(String taskId) {
Task task = this.taskService.createTaskQuery().taskId(taskId).singleResult();
String processInstanceId = task.getProcessInstanceId();
List<Comment> comments = taskService.getProcessInstanceComments(processInstanceId);
List<ActCommentEntity> data = new ArrayList<>();
if (null != comments && comments.size() > 0) {
for (Comment comment : comments) {
ActCommentEntity entity = new ActCommentEntity();
BeanUtils.copyProperties(comment, entity);
data.add(entity);
}
}
return new DataGridView(Long.valueOf(data.size()), data);
}
@Override
public void completeTask(WorkFlowVo workFlowVo) {
String taskId = workFlowVo.getTaskId();
String outcome = workFlowVo.getOutcome();
Integer leaveBillId = workFlowVo.getId();
String comment = workFlowVo.getComment();
Task task = this.taskService.createTaskQuery().taskId(taskId).singleResult();
String processInstanceId = task.getProcessInstanceId();
String userName = SessionUtils.getCurrentUserName();
Authentication.setAuthenticatedUserId(userName);
this.taskService.addComment(taskId, processInstanceId, "[" + outcome + "]" + comment);
Map<String, Object> variables = new HashMap<>();
variables.put("outcome", outcome);
this.taskService.complete(taskId, variables);
ProcessInstance processInstance = this.runtimeService.createProcessInstanceQuery()
.processInstanceId(processInstanceId).singleResult();
if (null == processInstance) {
LeaveBill leaveBill = new LeaveBill();
leaveBill.setId(leaveBillId);
if (outcome.equals("放弃")) {
leaveBill.setState(SYSConstast.STATE_LEAVEBILL_THREE);
} else {
leaveBill.setState(SYSConstast.STATE_LEAVEBILL_TOW);
}
this.billMapper.updateByPrimaryKeySelective(leaveBill);
}
}
@Override
public ProcessDefinition queryProcessDefinitionByTaskId(String taskId) {
Task task = this.taskService.createTaskQuery().taskId(taskId).singleResult();
String processInstanceId = task.getProcessInstanceId();
ProcessInstance processInstance = this.runtimeService.createProcessInstanceQuery()
.processInstanceId(processInstanceId).singleResult();
String processDefinitionId = processInstance.getProcessDefinitionId();
ProcessDefinition processDefinition = this.repositoryService.createProcessDefinitionQuery()
.processDefinitionId(processDefinitionId).singleResult();
return processDefinition;
}
@Override
public Map<String, Object> queryTaskCoordinateByTaskId(String taskId) {
Map<String, Object> coordinate = new HashMap<>();
Task task = this.taskService.createTaskQuery().taskId(taskId).singleResult();
String processDefinitionId = task.getProcessDefinitionId();
String processInstanceId = task.getProcessInstanceId();
ProcessInstance processInstance = this.runtimeService.createProcessInstanceQuery()
.processInstanceId(processInstanceId).singleResult();
ProcessDefinitionEntity processDefinition = (ProcessDefinitionEntity) this.repositoryService
.getProcessDefinition(processDefinitionId);
String activityId = processInstance.getActivityId();
ActivityImpl activityImpl = processDefinition.findActivity(activityId);
coordinate.put("x", activityImpl.getX());
coordinate.put("y", activityImpl.getY());
coordinate.put("width", activityImpl.getWidth());
coordinate.put("height", activityImpl.getHeight());
return coordinate;
}
@Override
public DataGridView querydCommentByLeaveBillId(Integer id) {
String businessKey = LeaveBill.class.getSimpleName() + ":" + id;
HistoricProcessInstance historicProcessInstance = historyService.createHistoricProcessInstanceQuery()
.processInstanceBusinessKey(businessKey).singleResult();
List<Comment> comments = this.taskService.getProcessInstanceComments(historicProcessInstance.getId());
List<ActCommentEntity> data = new ArrayList<>();
if (null != comments && comments.size() > 0) {
for (Comment comment : comments) {
ActCommentEntity entity = new ActCommentEntity();
BeanUtils.copyProperties(comment, entity);
data.add(entity);
}
}
return new DataGridView(Long.valueOf(data.size()), data);
}
public DataGridView queryCurrentUserHistoryTask(WorkFlowVo workFlowVo) {
String assignee=SessionUtils.getCurrentUserName();
int firstResult = (workFlowVo.getPage() - 1) * workFlowVo.getLimit();
int maxResults = workFlowVo.getLimit();
long count =this.historyService.createHistoricTaskInstanceQuery().taskAssignee(assignee).count();
List<HistoricTaskInstance> list = this.historyService.createHistoricTaskInstanceQuery()
.taskAssignee(assignee).listPage(firstResult, maxResults);
return new DataGridView(count, list);
}
}
public void notify(DelegateTask delegateTask) {
WebApplicationContext springContext = WebApplicationContextUtils
.getWebApplicationContext(WebUtils.getServletContext());
UserMapper userMapper = springContext.getBean(UserMapper.class);
User user = WebUtils.getCurrentUser();
User leaderUser = userMapper.selectByPrimaryKey(user.getMgr());
delegateTask.setAssignee(leaderUser.getUsername() + "_" + String.valueOf(user.getMgr()));
}
@Override
public void completeTask(WorkFlowVo workFlowVo) {
String taskId = workFlowVo.getTaskId();
String outcome = workFlowVo.getOutcome();
Integer leaveBillId = workFlowVo.getId();
String comment = workFlowVo.getComment();
Task task = this.taskService.createTaskQuery().taskId(taskId).singleResult();
String processInstanceId = task.getProcessInstanceId();
String userName = SessionUtils.getCurrentUserName();
Authentication.setAuthenticatedUserId(userName);
this.taskService.addComment(taskId, processInstanceId, "[" + outcome + "]" + comment);
Map<String, Object> variables = new HashMap<>();
variables.put("outcome", outcome);
this.taskService.complete(taskId, variables);
ProcessInstance processInstance = this.runtimeService.createProcessInstanceQuery()
.processInstanceId(processInstanceId).singleResult();
if (null == processInstance) {
LeaveBill leaveBill = new LeaveBill();
leaveBill.setId(
);
if (outcome.equals("放弃")) {
leaveBill.setState(SYSConstast.STATE_LEAVEBILL_THREE);
} else {
leaveBill.setState(SYSConstast.STATE_LEAVEBILL_TOW);
}
this.billMapper.updateByPrimaryKeySelective(leaveBill);
}
}
public class WebUtils {
public static ServletRequestAttributes getServletRequestAttributes() {
return (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
}
public static HttpServletRequest getRequest() {
HttpServletRequest request = getServletRequestAttributes().getRequest();
return request;
}
public static HttpServletResponse getResponse() {
HttpServletResponse response = getServletRequestAttributes().getResponse();
return response;
}
public static HttpSession getSession() {
return getRequest().getSession();
}
public static ServletContext getServletContext() {
return getRequest().getServletContext();
}
public static User getCurrentUser() {
return (User) getSession().getAttribute("user");
}
}