activiti实战五(我的流程)

创建一张请假信息的表(业务表)leave_item:

字段有id、begin_date、create_date、end_date、leave_case、leave_hour、proc_instence_id、status、creater、type_code(请假类型)

新建我的流程

@Override
public void saveMyProcess(LeaveItem leaveItem) {
leaveItem.setCreater(SessionUser.getUsername());
leaveItem.setCreateDate(new Date());
DateTime begin = new DateTime(leaveItem.getBeginDate());
DateTime end = new DateTime(leaveItem.getEndDate());
Duration d = new Duration(begin, end);
leaveItem.setLeaveHour((double) d.getStandardHours());
leaveItem.setStatus(2);// 表示状态为新建
session.save(leaveItem);

// 创建运行服务对象
RuntimeService runtimeService = processEngine.getRuntimeService();
Map<String, Object> map = new HashMap<>();
List<Sysrole> list = getRoleName(SessionUser.getLoginname());//根据登录名获取角色名
if (!list.isEmpty()) {
map.put("roleName", list.get(0).getRolename());//这个就是流程图中的变量roleName,通过它来判断走哪条路
}
//LeaveProcess是流程图的Key,目前固定

Authentication.setAuthenticatedUserId(sessionUser.getUser().getUserid());//这个很重要,因为记录的是流程发起者,对于邮件发送很有意义。当一个审批完成后可以用过历史流程找到流程发起者,然后发送邮件。
// 根据流程定义Key开启流程实例
ProcessInstance pi = runtimeService.startProcessInstanceByKey("LeaveProcess", leaveItem.getId().toString(),
map);//leaveItem.getId().toString()是bussinessKey,是在流程实例中绑定一个与业务相关的id
leaveItem.setProcInstenceId(pi.getProcessInstanceId());//业务表中也可以保存流程实例ID。
session.saveOrUpdate(leaveItem);//只要不是一个新对象,一个方法中保存多遍leaveItem不会报错
}

查询我的流程(这个与流程实例无关,只与业务表有关)

@Override
public AjaxJson getMyProcessInfo(String typeCode, Integer start, Integer limit)  {
String creater = SessionUser()getUsername();
String hql = "FROM LeaveItem WHERE creater = :creater ORDER BY createDate DESC";
if (!StringUtils.isEmpty(typeCode)) {
hql = "FROM LeaveItem WHERE typeCode = :typeCode AND creater = :creater ORDER BY createDate DESC";
}
Query query = session.createQuery(hql);
if (!StringUtils.isEmpty(typeCode)) {
query.setString("typeCode", typeCode);
}
query.setString("creater", creater);
List<LeaveItem> list = query.setFirstResult(start).setMaxResults(limit).list();
AjaxJson aj = new AjaxJson();
aj.setData(list);
aj.setTotalCount(query.list().size());
return aj;
}

查询流程图(包括未完成与已完成的)

@Override
public BufferedImage showImage(String piId) throws IOException {
ProcessInstance processInstance = runtimeService.createProcessInstanceQuery().processInstanceId(piId)
.singleResult();
if (processInstance == null) {//如果流程已经走完
ProcessEngineConfiguration processEngineConfiguration = processEngine.getProcessEngineConfiguration();
Context.setProcessEngineConfiguration((ProcessEngineConfigurationImpl) processEngineConfiguration);
ProcessDiagramGenerator diagramGenerator = processEngineConfiguration.getProcessDiagramGenerator();
HistoricProcessInstance historicProcessInstance = historyService.createHistoricProcessInstanceQuery()
.processInstanceId(piId).singleResult();
List<HistoricActivityInstance> list = historyService.createHistoricActivityInstanceQuery()
.processInstanceId(piId).list();
BpmnModel bpmnModel = repositoryService.getBpmnModel(historicProcessInstance.getProcessDefinitionId());
ProcessDefinitionEntity definitionEntity = (ProcessDefinitionEntity) repositoryService
.getProcessDefinition(historicProcessInstance.getProcessDefinitionId());
List<String> highLightedFlows = getHighLightedFlows(definitionEntity, list);
List<String> activitiIds = new ArrayList<>();
for (HistoricActivityInstance historicActivityInstance : list) {
activitiIds.add(historicActivityInstance.getActivityId());
}
// ProcessDiagramGenerator
InputStream imageStream = diagramGenerator.generateDiagram(bpmnModel, "png", activitiIds, highLightedFlows,
"宋体", "宋体", "宋体", null, 1.0);
BufferedImage image = ImageIO.read(imageStream);
return image;
}

//如果流程没走完,只显示当前任务,可以修改成上面方式
InputStream is = repositoryService.getProcessDiagram(processInstance.getProcessDefinitionId());
BufferedImage image = ImageIO.read(is);
Graphics2D graphics = (Graphics2D) image.getGraphics();
graphics.setColor(Color.RED);
// 设置粗细
graphics.setStroke(new BasicStroke(3.0f));
// 消除锯齿
graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
// 获取当前所有的活动节点id(就是轮到的那个节点)
List<String> activeActivityIds = runtimeService.getActiveActivityIds(piId);
// 流程图模型
BpmnModel bpmnModel = repositoryService.getBpmnModel(processInstance.getProcessDefinitionId());
for (String activeActivityId : activeActivityIds) {
GraphicInfo graphicInfo = bpmnModel.getGraphicInfo(activeActivityId);
// <omgdc:Bounds height="55.0" width="105.0" x="330.0"
// y="150.0"></omgdc:Bounds>
// 绘制圆角矩形
graphics.drawRoundRect((int) graphicInfo.getX(), (int) graphicInfo.getY(), (int) graphicInfo.getWidth(),
(int) graphicInfo.getHeight(), 10, 10);
}
return image;
}

controller中

@RequestMapping(params = "showProcessImage")
public void showProcessImage(String procInstenceId, HttpServletRequest request, HttpServletResponse response)
throws IOException {
BufferedImage image = myProcessService.showImage(procInstenceId);
response.setContentType("image/png");//不是images
ImageIO.write(image, "png", response.getOutputStream());
}

查询历史流程(这里要注意,要查询的其实是历史任务,而非历史流程实例,当历史任务的endtime为null时,就不要显示,否则显示)

@Override
public AjaxJson getHistory(String procInstenceId) {
List<HistoricTaskInstance> list = historyService.createHistoricTaskInstanceQuery()
.processInstanceId(procInstenceId).list();
List<HistoricTaskEntity> tasks = new ArrayList<>();
for (HistoricTaskInstance historicTaskInstance : list) {
HistoricTaskEntity historicTaskEntity = new HistoricTaskEntity();
if (historicTaskInstance.getEndTime() == null) {
continue;
}
historicTaskEntity.setTaskId(historicTaskInstance.getId());
long durationInMillis = historicTaskInstance.getDurationInMillis();

//这个方法是将毫秒转化为几天几小时几分几秒
String dueTime = (durationInMillis / (24 * 60 * 60 * 1000)) + "天"
+ ((durationInMillis % (24 * 60 * 60 * 1000)) / (60 * 60 * 1000)) + "时"
+ +((durationInMillis % (24 * 60 * 60 * 1000) % (60 * 60 * 1000)) / (60 * 1000)) + "分"
+ +((durationInMillis % (24 * 60 * 60 * 1000) % (60 * 60 * 1000) % (60 * 1000)) / 1000) + "秒";
historicTaskEntity.setDueTime(dueTime);
historicTaskEntity.setEndTime(historicTaskInstance.getEndTime());
historicTaskEntity.setStartTime(historicTaskInstance.getStartTime());
Sysuser sysUser = getSysuserByUserId(historicTaskInstance.getAssignee());//这个Assignee会在审批时指定(先登陆先领取)
historicTaskEntity.setTaskAssignee(sysUser.getUsername());
historicTaskEntity.setTaskName(historicTaskInstance.getName());
tasks.add(historicTaskEntity);
}
AjaxJson aj = new AjaxJson();
aj.setData(tasks);
return aj;
}

查询审批意见:

@RequestMapping(params = "showProcessContent")
public ModelAndView showProcessConten(String procInstenceId) {
ModelAndView mav = new ModelAndView();
mav.setViewName(PREFFIX + "DealProcessDetail");
List<HistoricTaskInstance> list = historyService.createHistoricTaskInstanceQuery()
.processInstanceId(procInstenceId).list();//查询历史任务列表(一个流程不管有没有结束,会把下一个没完成的也查询出来)
for (HistoricTaskInstance historicTaskInstance : list) {
List<Comment> taskComments = taskService.getTaskComments(historicTaskInstance.getId());//我们不将审批建议放到自己建的表中,而是放到act_hi_comment中,他可以用taskService.getTaskComments去查
for (Comment comment : taskComments) {
mav.addObject("message", comment.getFullMessage());
mav.addObject("user", myProcessService.getSysuserByUserId(comment.getUserId()).getUsername());//userid会在执行任务时用Authentication.setAuthenticatedUserId()设置
mav.addObject("time", comment.getTime());//审批时间
}
}
LeaveItem leaveItem = myProcessService.getLeaveItemByPiId(procInstenceId);
mav.addObject("leaveItem", leaveItem);// PROCESSSTATUS
Syscode syscode = myProcessService.getSyscodeByCodeAndValue("PROCESSSTATUS", leaveItem.getStatus());
if (syscode != null) {
mav.addObject("status", syscode.getCodeexplain());
} else {
mav.addObject("status", "未知");
}
return mav;
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值