Activiti Modeler和業務系統整合
阿新 • • 發佈:2019-01-01
前言
本部落格是將modeler單獨出來和業務系統整合的方案,希望對使用activiti的童鞋有所幫助
檔案整合
在src/main/resource中新增modeler配置檔案
editor.html,plugins.xml,stencilset.json,ui.properties,ui.properties.alfresco
在src/main/webapp中新增modeler資料夾
資料夾中包括modeler中的api,diagram-viewer,editor,explorer,libs資料夾
修改web.xml
<servlet >
<servlet-name > ExplorerRestletServlet</servlet-name >
<servlet-class> org.restlet.ext.servlet.ServerServlet</servlet-class >
<init-param>
<!-- Application class name -->
<param-name> org.restlet.application</param-name >
<param-value > com.isprint.ssf.controller.ExplorerRestApplication</param-value >
</init-param>
</servlet >
<servlet >
<servlet-name> RestletServlet</servlet-name >
<servlet-class> org.restlet.ext.servlet.ServerServlet</servlet-class >
<init-param>
<!-- Application class name -->
<param-name> org.restlet.application</param-name >
<param-value> org.activiti.rest.service.application.ActivitiRestServicesApplication </param-value>
</init-param>
</servlet >
<!-- Catch all service requests -->
<servlet-mapping>
<servlet-name> RestletServlet</servlet-name >
<url-pattern> /rest/*</ url-pattern>
</servlet-mapping >
<servlet-mapping>
<servlet-name> ExplorerRestletServlet</servlet-name >
<url-pattern> /modeler/service/*</url-pattern >
</servlet-mapping>
封裝API
1、新增ExplorerRestApplication 類
public class ExplorerRestApplication extends ActivitiRestApplication {
public ExplorerRestApplication() {
super();
}
/**
* Creates a root Restlet that will receive all incoming calls.
*/
@Override
public synchronized Restlet createInboundRoot() {
Router router = new Router(getContext());
router.attachDefault(DefaultResource.class);
ModelerServicesInit.attachResources(router);
JsonpFilter jsonpFilter = new JsonpFilter(getContext());
jsonpFilter.setNext(router);
return jsonpFilter;
}
}
2、新增ModelController類,完成流程建立、部署、編輯、匯出、刪除操作。
/**
* 流程模型控制器
*
* @author
*/
@Controller
@RequestMapping(value = "/model")
public class ModelController {
protected Logger logger = LoggerFactory.getLogger(getClass());
@Autowired
RepositoryService repositoryService;
@Autowired
ModelService modelService;
@RequestMapping(value = "list")
@ResponseBody
public Map<String, Object> modelList(ModelDto modelDto, HttpServletRequest request) {
return modelService.queryModel(modelDto);
}
/**
* 建立模型
*/
@RequestMapping("create")
public void create(ModelDto modelDto, HttpServletRequest request, HttpServletResponse response) {
try {
ObjectMapper objectMapper = new ObjectMapper();
ObjectNode editorNode = objectMapper.createObjectNode();
editorNode.put("id", "canvas");
editorNode.put("resourceId", "canvas");
ObjectNode stencilSetNode = objectMapper.createObjectNode();
stencilSetNode.put("namespace", "http://b3mn.org/stencilset/bpmn2.0#");
editorNode.put("stencilset", stencilSetNode);
Model modelData = repositoryService.newModel();
ObjectNode modelObjectNode = objectMapper.createObjectNode();
modelObjectNode.put(ModelDataJsonConstants.MODEL_NAME, modelDto.getName());
modelObjectNode.put(ModelDataJsonConstants.MODEL_REVISION, 1);
String description = StringUtils.defaultString(modelDto.getDescription());
modelObjectNode.put(ModelDataJsonConstants.MODEL_DESCRIPTION, description);
modelData.setMetaInfo(modelObjectNode.toString());
modelData.setName(modelDto.getName());
modelData.setKey(StringUtils.defaultString(modelDto.getKey()));
//儲存模型
repositoryService.saveModel(modelData);
repositoryService.addModelEditorSource(modelData.getId(), editorNode.toString().getBytes("utf-8"));
response.sendRedirect(request.getContextPath() + "/modeler/service/editor?id=" + modelData.getId());
} catch (Exception e) {
logger.error("建立模型失敗:", e);
}
}
/**
* 根據Model部署流程
*/
@RequestMapping(value = "deploy/{modelId}")
@ResponseBody
public String deploy(@PathVariable("modelId") String modelId, RedirectAttributes redirectAttributes) {
try {
Model modelData = repositoryService.getModel(modelId);
ObjectNode modelNode = (ObjectNode) new ObjectMapper().readTree(repositoryService.getModelEditorSource(modelData.getId()));
byte[] bpmnBytes = null;
BpmnModel model = new BpmnJsonConverter().convertToBpmnModel(modelNode);
bpmnBytes = new BpmnXMLConverter().convertToXML(model);
String processName = modelData.getName() + ".bpmn20.xml";
repositoryService.createDeployment().name(modelData.getName()).addString(processName, new String(bpmnBytes,"UTF-8")).deploy();
//redirectAttributes.addFlashAttribute("message", "部署成功,部署ID=" + deployment.getId());
} catch (Exception e) {
logger.error("根據模型部署流程失敗:modelId={}", modelId, e);
}
return "deployesuccess";
}
/**
* 匯出model的xml檔案
*/
@RequestMapping(value = "export/{modelId}")
public void export(@PathVariable("modelId") String modelId, HttpServletResponse response) {
try {
Model modelData = repositoryService.getModel(modelId);
BpmnJsonConverter jsonConverter = new BpmnJsonConverter();
JsonNode editorNode = new ObjectMapper().readTree(repositoryService.getModelEditorSource(modelData.getId()));
BpmnModel bpmnModel = jsonConverter.convertToBpmnModel(editorNode);
BpmnXMLConverter xmlConverter = new BpmnXMLConverter();
byte[] bpmnBytes = xmlConverter.convertToXML(bpmnModel);
ByteArrayInputStream in = new ByteArrayInputStream(bpmnBytes);
IOUtils.copy(in, response.getOutputStream());
String filename = bpmnModel.getMainProcess().getId() + ".bpmn20.xml";
response.setHeader("Content-Disposition", "attachment; filename=" + filename);
response.flushBuffer();
} catch (Exception e) {
logger.error("匯出model的xml檔案失敗:modelId={}", modelId, e);
}
}
@RequestMapping(value = "delete/{modelId}")
@ResponseBody
public String delete(@PathVariable("modelId") String modelId) {
repositoryService.deleteModel(modelId);
return "deletesuccess";
}
}
3、新增Service、Dao類
ModelService介面
public interface ModelService {
public Map<String,Object> queryModel(ModelDto modelDto);
}
Service實現
@Service
@Transactional
public class ModelServiceImpl implements ModelService {
public static final Logger logger = Logger
.getLogger(ModelServiceImpl.class);
@Autowired
private IModelDao modelDao;
public Map<String,Object> queryModel(ModelDto modelDto){
Map<String,Object> map = new HashMap<String,Object>();
Map<String,String> paramMap=new HashMap<String,String>();
if(StringUtils.isNotBlank(modelDto.getId())){
paramMap.put("id", modelDto.getId());
}
if(StringUtils.isNotBlank(modelDto.getKey())){
paramMap.put("key", modelDto.getKey());
}
if(StringUtils.isNotBlank(modelDto.getName())){
paramMap.put("name", modelDto.getName());
}
List<ModelDto> modelDtoList=modelDao.getModelByQuery(paramMap, modelDto.getPage(), modelDto.getRows());
map.put("total", modelDao.getModelCountByQuery(paramMap));
map.put("rows", modelDtoList);
return map;
}
}
Dao介面
import java.util.List;
import java.util.Map;
public interface IModelDao {
public List<ModelDto> getModelByQuery(Map<String,String> paramMap, int page, int rows);
public int getModelCountByQuery(Map<String,String> paramMap);
}
Dao實現
@Autowired
DirectlyDBService db;
public static final Logger log = Logger.getLogger(ModelDaoImpl.class);
@Override
public List<ModelDto> getModelByQuery(Map<String, String> paramMap,
int page, int rows) {
int firstRow;
int maxRow;
if (page > 1) {
firstRow = (page - 1) * rows + 1;
maxRow = page * rows;
} else {
firstRow = 1;
maxRow = firstRow * rows;
}
StringBuilder sb = new StringBuilder();
sb.append("select * from ( ");
sb.append(" select t.*,ROWNUM rn from (");
sb.append(" select * from ACT_RE_MODEL where 1=1 ");
if (paramMap.containsKey("id")) {
sb.append(" and id_ ='" + paramMap.get("id") + "'");
}
if (paramMap.containsKey("name")) {
sb.append(" and name_ like '%" + paramMap.get("name") + "%'");
}
if (paramMap.containsKey("key")) {
sb.append(" and key_ like '%" + paramMap.get("key") + "%'");
}
sb.append(" ORDER BY create_time_ DESC) t)");
sb.append(" where rn >=" + firstRow + " and rn<=" + maxRow);
List<ModelDto> modelDtoList = new ArrayList<ModelDto>();
try {
List<Map<String, Object>> list = db.listBySql(sb.toString());
for (Map<String, Object> dbMap : list) {
ModelDto modelDto = new ModelDto();
modelDto.setId(dbMap.get("ID_").toString());
modelDto.setName(dbMap.get("NAME_") == null ? "" : dbMap.get(
"NAME_").toString());
modelDto.setRev(dbMap.get("REV_") == null ? 0 : Integer
.parseInt(dbMap.get("REV_").toString()));
modelDto.setKey(dbMap.get("KEY_") == null ? "" : dbMap.get(
"KEY_").toString());
modelDto.setCategory(dbMap.get("CATEGORY_") == null ? ""
: dbMap.get("CATEGORY_").toString());
modelDto.setVersion(dbMap.get("VERSION_") == null ? 0 : Integer
.parseInt(dbMap.get("VERSION_").toString()));
modelDto.setMetaInfo(dbMap.get("META_INFO_") == null ? ""
: dbMap.get("META_INFO_").toString());
modelDto.setDeploymentId(dbMap.get("DEPLOYMENT_ID_") == null ? ""
: dbMap.get("DEPLOYMENT_ID_").toString());
modelDto.setEditorSourceValueId(dbMap
.get("EDITOR_SOURCE_VALUE_ID_") == null ? "" : dbMap
.get("EDITOR_SOURCE_VALUE_ID_").toString());
modelDto.setEditorSourceExtraValueId(dbMap
.get("EDITOR_SOURCE_EXTRA_VALUE_ID_") == null ? ""
: dbMap.get("EDITOR_SOURCE_EXTRA_VALUE_ID_").toString());
modelDto.setTenantId(dbMap.get("TENANT_ID_") == null ? ""
: dbMap.get("TENANT_ID_").toString());
modelDto.setCreateTime(dbMap.get("CREATE_TIME_") == null ? ""
: dbMap.get("CREATE_TIME_").toString());
modelDto.setLastUpdateTime(dbMap.get("LAST_UPDATE_TIME_") == null ? ""
:dbMap.get("LAST_UPDATE_TIME_").toString());
modelDtoList.add(modelDto);
}
} catch (Exception e) {
log.error("ModelDao-getModelByQuery", e);
}
return modelDtoList;
}
@Override
public int getModelCountByQuery(Map<String, String> paramMap) {
StringBuilder sb = new StringBuilder();
sb.append("select count(*) as MODELCOUNT from ( ");
sb.append(" select t.* from (");
sb.append(" select * from ACT_RE_MODEL where 1=1 ");
if (paramMap.containsKey("id")) {
sb.append(" and id_ ='" + paramMap.get("id") + "'");
}
if (paramMap.containsKey("name")) {
sb.append(" and name_ like '%" + paramMap.get("name") + "%'");
}
if (paramMap.containsKey("key")) {
sb.append(" and key_ like '%" + paramMap.get("key") + "%'");
}
sb.append(" ORDER BY id_ DESC) t)");
System.out.println(sb);
List<Map<String, Object>> list = db.listBySql(sb.toString());
if (list != null && !list.isEmpty()) {
Map<String, Object> listMap = list.get(0);
return Integer.valueOf(listMap.get("MODELCOUNT").toString())
.intValue();
} else {
return 0;
}
}
總結
通過上面的操作之後,就已經準備好Modeler使用的環境了,如果業務系統想整合此流程設計器的話,就需要呼叫提供的方法即可。