diff --git a/src/main/java/com/cm/inspection/controller/apis/activiti/ActivitiController.java b/src/main/java/com/cm/inspection/controller/apis/activiti/ActivitiController.java new file mode 100644 index 0000000..7cd642a --- /dev/null +++ b/src/main/java/com/cm/inspection/controller/apis/activiti/ActivitiController.java @@ -0,0 +1,105 @@ +package com.cm.inspection.controller.apis.activiti; + +import com.cm.common.annotation.CheckRequestBodyAnnotation; +import com.cm.common.base.AbstractController; +import com.cm.common.constants.ISystemConstant; +import com.cm.common.exception.SearchException; +import com.cm.common.pojo.ListPage; +import com.cm.common.result.ErrorResult; +import com.cm.common.result.SuccessResult; +import com.cm.common.result.SuccessResultData; +import com.cm.common.result.SuccessResultList; +import com.cm.inspection.pojo.dtos.check.CheckDTO; +import com.cm.inspection.pojo.vos.activiti.ActivitiVO; +import com.cm.inspection.service.activiti.IActivitiService; +import io.swagger.annotations.*; +import org.activiti.engine.repository.Model; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.*; + +import javax.management.ObjectName; +import java.io.IOException; +import java.io.UnsupportedEncodingException; +import java.util.Arrays; +import java.util.List; +import java.util.Map; + +/** + * When you feel like quitting. Think about why you started + * 当你想要放弃的时候,想想当初你为何开始 + * + * @ClassName: ActivitiController + * @Description: activiti + * @Author: WangGeng + * @Date: 2021/7/11 16:36 + * @Version: 1.0 + **/ +@Api(tags = ISystemConstant.API_TAGS_SYSTEM_PREFIX + "流程引擎接口") +@RestController +@RequestMapping(ISystemConstant.API_PREFIX + "/activiti") +public class ActivitiController extends AbstractController { + + @Autowired + private IActivitiService activitiService; + + @ApiOperation(value = "创建模型", notes = "创建模型接口") + @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)}) + @PostMapping("create") + @CheckRequestBodyAnnotation + public SuccessResultData create(@RequestBody ActivitiVO activitiVO) throws UnsupportedEncodingException { + return new SuccessResultData<>(activitiService.create(activitiVO)); + } + + @ApiOperation(value = "删除模型", notes = "删除模型接口") + @ApiImplicitParams({ + @ApiImplicitParam(name = "modelId", value = "模型ID", paramType = "path") + }) + @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)}) + @DeleteMapping("delete/{modelId}") + public SuccessResult delete(@PathVariable("modelId") String modelId) { + activitiService.delete(modelId); + return new SuccessResult(); + } + + @ApiOperation(value = "发布模型", notes = "发布模型接口") + @ApiImplicitParams({ + @ApiImplicitParam(name = "modelId", value = "模型ID", paramType = "path") + }) + @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)}) + @PutMapping("publish/{modelId}") + public SuccessResult publish(@PathVariable("modelId") String modelId) throws IOException { + activitiService.publish(modelId); + return new SuccessResult(); + } + + @ApiOperation(value = "撤销发布模型", notes = "撤销发布模型接口") + @ApiImplicitParams({ + @ApiImplicitParam(name = "modelId", value = "模型ID", paramType = "path") + }) + @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)}) + @PutMapping("revoke-publish/{modelId}") + public SuccessResult revokePublish(@PathVariable("modelId") String modelId) { + activitiService.revokePublish(modelId); + return new SuccessResult(); + } + + + @ApiOperation(value = "模型列表", notes = "模型列表接口") + @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)}) + @GetMapping("list") + public List list() { + return activitiService.list(); + } + + @ApiOperation(value = "模型分页列表", notes = "模型分页列表接口") + @ApiImplicitParams({ + @ApiImplicitParam(name = "page", value = "当前页码", paramType = "query", dataType = "Integer", defaultValue = "1"), + @ApiImplicitParam(name = "rows", value = "显示数量", paramType = "query", dataType = "Integer", defaultValue = "20"), + }) + @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)}) + @GetMapping("listpage") + public SuccessResultList> listPage(ListPage page) { + return activitiService.listPage(page.getPage(), page.getRows()); + } + +} diff --git a/src/main/java/com/cm/inspection/pojo/vos/activiti/ActivitiVO.java b/src/main/java/com/cm/inspection/pojo/vos/activiti/ActivitiVO.java new file mode 100644 index 0000000..6fefcab --- /dev/null +++ b/src/main/java/com/cm/inspection/pojo/vos/activiti/ActivitiVO.java @@ -0,0 +1,65 @@ +package com.cm.inspection.pojo.vos.activiti; + +import com.cm.common.annotation.CheckEmptyAnnotation; +import com.cm.common.annotation.CheckNumberAnnotation; +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; + +/** + * When you feel like quitting. Think about why you started + * 当你想要放弃的时候,想想当初你为何开始 + * + * @ClassName: ActivitiVO + * @Description: activiti + * @Author: WangGeng + * @Date: 2021/7/11 22:04 + * @Version: 1.0 + **/ +@ApiModel +public class ActivitiVO { + + @ApiModelProperty(name = "modelName", value = "模型名称") + @CheckEmptyAnnotation(name = "模型名称") + private String modelName; + @ApiModelProperty(name = "modelKey", value = "模型key") + @CheckEmptyAnnotation(name = "模型key") + private String modelKey; + @ApiModelProperty(name = "modelDescription", value = "模型描述") + private String modelDescription; + @ApiModelProperty(name = "modelVersion", value = "模型版本") + @CheckNumberAnnotation(name = "模型版本") + private Integer modelVersion; + + public String getModelName() { + return modelName == null ? "" : modelName.trim(); + } + + public void setModelName(String modelName) { + this.modelName = modelName; + } + + public String getModelKey() { + return modelKey == null ? "" : modelKey.trim(); + } + + public void setModelKey(String modelKey) { + this.modelKey = modelKey; + } + + public String getModelDescription() { + return modelDescription == null ? "" : modelDescription.trim(); + } + + public void setModelDescription(String modelDescription) { + this.modelDescription = modelDescription; + } + + public Integer getModelVersion() { + return modelVersion; + } + + public void setModelVersion(Integer modelVersion) { + this.modelVersion = modelVersion; + } +} diff --git a/src/main/java/com/cm/inspection/service/activiti/IActivitiService.java b/src/main/java/com/cm/inspection/service/activiti/IActivitiService.java new file mode 100644 index 0000000..2334a11 --- /dev/null +++ b/src/main/java/com/cm/inspection/service/activiti/IActivitiService.java @@ -0,0 +1,73 @@ +package com.cm.inspection.service.activiti; + +import com.cm.common.pojo.ListPage; +import com.cm.common.result.SuccessResultList; +import com.cm.inspection.pojo.vos.activiti.ActivitiVO; +import org.activiti.engine.repository.Model; + +import java.io.IOException; +import java.io.UnsupportedEncodingException; +import java.util.List; +import java.util.Map; + +/** + * When you feel like quitting. Think about why you started + * 当你想要放弃的时候,想想当初你为何开始 + * + * @ClassName: IActivitiService + * @Description: activiti + * @Author: WangGeng + * @Date: 2021/7/11 16:46 + * @Version: 1.0 + **/ +public interface IActivitiService { + + /** + * 新建模型 + * + * @param activitiVO + * @return + * @throws UnsupportedEncodingException + */ + String create(ActivitiVO activitiVO) throws UnsupportedEncodingException; + + /** + * 删除模型 + * + * @param modelId 模型ID + */ + void delete(String modelId); + + + /** + * 发布模型 + * + * @param modelId + */ + void publish(String modelId) throws IOException; + + /** + * 撤销发布模型 + * + * @param modelId + */ + void revokePublish(String modelId); + + /** + * 模型列表 + * + * @return + */ + List list(); + + /** + * 模型分页列表 + * + * @param page + * @param rows + * @return + */ + SuccessResultList> listPage(int page, int rows); + + +} diff --git a/src/main/java/com/cm/inspection/service/activiti/impl/ActivitiServiceImpl.java b/src/main/java/com/cm/inspection/service/activiti/impl/ActivitiServiceImpl.java new file mode 100644 index 0000000..1fd2b88 --- /dev/null +++ b/src/main/java/com/cm/inspection/service/activiti/impl/ActivitiServiceImpl.java @@ -0,0 +1,158 @@ +package com.cm.inspection.service.activiti.impl; + +import com.alibaba.fastjson.JSONObject; +import com.cm.common.base.AbstractService; +import com.cm.common.constants.ISystemConstant; +import com.cm.common.exception.RemoveException; +import com.cm.common.exception.SearchException; +import com.cm.common.pojo.ListPage; +import com.cm.common.result.SuccessResultList; +import com.cm.inspection.pojo.vos.activiti.ActivitiVO; +import com.cm.inspection.service.activiti.IActivitiService; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ObjectNode; +import org.activiti.bpmn.model.BpmnModel; +import org.activiti.editor.constants.ModelDataJsonConstants; +import org.activiti.editor.language.json.converter.BpmnJsonConverter; +import org.activiti.engine.HistoryService; +import org.activiti.engine.RepositoryService; +import org.activiti.engine.RuntimeService; +import org.activiti.engine.repository.Deployment; +import org.activiti.engine.repository.Model; +import org.activiti.engine.repository.ModelQuery; +import org.activiti.engine.runtime.ProcessInstance; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.io.IOException; +import java.io.UnsupportedEncodingException; +import java.util.List; +import java.util.Map; + +/** + * When you feel like quitting. Think about why you started + * 当你想要放弃的时候,想想当初你为何开始 + * + * @ClassName: ActivitiServiceImpl + * @Description: activiti + * @Author: WangGeng + * @Date: 2021/7/11 16:46 + * @Version: 1.0 + **/ +@Service +public class ActivitiServiceImpl extends AbstractService implements IActivitiService { + + @Autowired + private RepositoryService repositoryService; + @Autowired + private HistoryService historyService; + @Autowired + private RuntimeService runtimeService; + + @Override + public String create(ActivitiVO activitiVO) throws UnsupportedEncodingException { + Model model = repositoryService.newModel(); + model.setName(activitiVO.getModelName()); + model.setKey(activitiVO.getModelKey()); + model.setMetaInfo(createModelMetaInfo(activitiVO)); + LOG.debug("保存模型"); + repositoryService.saveModel(model); + addModelEditorSource(model.getId()); + return model.getId(); + } + + @Override + public void delete(String modelId) { + Model model = repositoryService.getModel(modelId); + if (model == null) { + throw new SearchException("模型不存在"); + } + ProcessInstance processInstance = runtimeService.createProcessInstanceQuery().processDefinitionKey(model.getKey()).singleResult(); + // 删除模型实例 + if (processInstance != null) { + runtimeService.deleteProcessInstance(processInstance.getId(), ""); + historyService.deleteHistoricProcessInstance(processInstance.getId()); + } + //删除模型模型 + repositoryService.deleteModel(modelId); + } + + @Override + public void publish(String modelId) throws IOException { + Model model = repositoryService.getModel(modelId); + if (model == null) { + throw new SearchException("模型不存在"); + } + byte[] modelEditorSource = repositoryService.getModelEditorSource(model.getId()); + if (modelEditorSource == null) { + throw new SearchException("模型数据不存在"); + } + JsonNode modelNode = new ObjectMapper().readTree(modelEditorSource); + BpmnModel bpmnModel = new BpmnJsonConverter().convertToBpmnModel(modelNode); + Deployment deployment = repositoryService.createDeployment() + .name(model.getName()) + .addBpmnModel(model.getKey() + ".bpmn20.xml", bpmnModel) + .deploy(); + model.setDeploymentId(deployment.getId()); + repositoryService.saveModel(model); + } + + @Override + public void revokePublish(String modelId) { + Model modelData = repositoryService.getModel(modelId); + if (modelData == null) { + throw new SearchException("模型不存在"); + } + /** + * 参数不加true:为普通删除,如果当前规则下有正在执行的流程,则抛异常 + * 参数加true:为级联删除,会删除和当前规则相关的所有信息,包括历史 + */ + repositoryService.deleteDeployment(modelData.getDeploymentId(), true); + } + + @Override + public List list() { + return repositoryService.createModelQuery().list(); + } + + @Override + public SuccessResultList> listPage(int page, int rows) { + ModelQuery modelQuery = repositoryService.createModelQuery(); + List models = modelQuery.listPage(page - 1, rows); + return new SuccessResultList<>(models, page, modelQuery.count()); + } + + /** + * 创建模型信息 + * + * @param activitiVO + * @return + */ + private String createModelMetaInfo(ActivitiVO activitiVO) { + JSONObject metaInfo = new JSONObject(); + metaInfo.put(ModelDataJsonConstants.MODEL_NAME, activitiVO.getModelName()); + metaInfo.put(ModelDataJsonConstants.MODEL_DESCRIPTION, activitiVO.getModelDescription()); + metaInfo.put(ModelDataJsonConstants.MODEL_REVISION, activitiVO.getModelVersion()); + return metaInfo.toString(); + } + + /** + * 添加模型时完善ModelEditorSource + * + * @param modelId + */ + private void addModelEditorSource(String modelId) throws UnsupportedEncodingException { + LOG.info("添加模型时完善ModelEditorSource入参模型ID:{}", modelId); + JSONObject editorObject = new JSONObject(); + editorObject.put("id", "canvas"); + editorObject.put("resourceId", "canvas"); + + JSONObject stencilSetObject = new JSONObject(); + stencilSetObject.put("namespace", "http://b3mn.org/stencilset/bpmn2.0#"); + + editorObject.put("stencilset", stencilSetObject); + repositoryService.addModelEditorSource(modelId, editorObject.toString().getBytes(ISystemConstant.CHARSET_UTF8)); + } + +} diff --git a/src/main/resources/static/route/activiti/list.html b/src/main/resources/static/route/activiti/list.html new file mode 100644 index 0000000..1da883b --- /dev/null +++ b/src/main/resources/static/route/activiti/list.html @@ -0,0 +1,214 @@ + + + + + + + + + + + + + +
+
+
+
+
+
+ + +
+
+
+
+
+ + + + + \ No newline at end of file diff --git a/src/main/resources/templates/modeler-list.html b/src/main/resources/templates/modeler-list.html index 00efd88..9b9df80 100644 --- a/src/main/resources/templates/modeler-list.html +++ b/src/main/resources/templates/modeler-list.html @@ -1,48 +1,48 @@ - - - 工作流模型列表 - - - + + + 工作流模型列表 + + + -
-
工作流管理
- - - - - - - - - - - - - - - - - - - - -
模型编号版本模型key模型名称操作
- 部署 - 撤销 - 编辑 - 删除 -
+
+
工作流管理
+ + + + + + + + + + + + + + + + + + + + +
模型编号版本模型key模型名称操作
+ 部署 + 撤销 + 编辑 + 删除 +
\ No newline at end of file