Activiti Eclipse Designer与Activiti Modeler的相互转换

在Activiti Modeler尚未推出之前,我们使用Activiti进行流程设计的时候,优先会使用官方提供的Eclipse插件来进行设计,然后再通过代码把流程设计文件发布至系统中,然后再调用启动流程,这会导致一个问题,当我们需要进行流程修改的时候,麻烦来了,解决这种问题方案有以下方式:

1.重新在eclipse设计器流程,然后再次通过代码发布至系统中,然后再次执行。

2.通过自身开发的界面,在线重新设计

Activiti Modeler提供了我们一种全新的方式,即允许用户在eclipse中进行设计,然后再上传,上传后可直接转为Modeler的格式,可在Modeler中打开,然后再修改。

如我们上传eclipse设计器中的流程设计文件:


Activiti Eclipse Designer与Activiti Modeler的相互转换
 

通过上传配置,把eclipse设计的文件上传:


Activiti Eclipse Designer与Activiti Modeler的相互转换
 

通过代码可以转化为Activiti Modeler在线设计的文件格式:


Activiti Eclipse Designer与Activiti Modeler的相互转换
 

通过这种方式可以有效实现eclipse设计,在线设计的相互转换.

eclipse转为Modeler的格式的转化代码如下:

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamReader;

import org.activiti.bpmn.converter.BpmnXMLConverter;
import org.activiti.bpmn.model.BpmnModel;
import org.activiti.editor.language.json.converter.BpmnJsonConverter;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.repository.Model;
import org.apache.commons.lang.StringUtils;

...



XMLInputFactory xif = XmlUtil.createSafeXmlInputFactory();
		Map<String, MultipartFile> files = request.getFileMap();
		Iterator<MultipartFile> it = files.values().iterator();
		while(it.hasNext()){
			MultipartFile f = it.next();
			InputStream is = f.getInputStream();
			InputStreamReader in = new InputStreamReader(is, "UTF-8");
			XMLStreamReader xtr = xif.createXMLStreamReader(in);
	        BpmnModel bpmnModel = new BpmnXMLConverter().convertToBpmnModel(xtr);
	        
	        if (bpmnModel.getMainProcess() == null 
	        		|| bpmnModel.getMainProcess().getId() == null) {
	        	msg="上传流程文件解析有问题!";
	        } else {
		      BpmnJsonConverter converter = new BpmnJsonConverter();
	          ObjectNode modelNode = converter.convertToJson(bpmnModel);
	          Model modelData = repositoryService.newModel();
	          
	          ObjectNode modelObjectNode = new ObjectMapper().createObjectNode();
	          modelObjectNode.put("name", subject);
	          modelObjectNode.put("revision", 1);
	          modelObjectNode.put("documentation", bpmDef.getDescp());
	          
	          modelData.setMetaInfo(modelObjectNode.toString());
	          modelData.setName(subject);
	          modelData.setTenantId(ContextUtil.getCurrentTenantId());
	          
	          repositoryService.saveModel(modelData);	          
	          //modelNode.put("resourceId", modelData.getId());
	          
	          logger.debug("json:"+modelNode.toString());
	          repositoryService.addModelEditorSource(modelData.getId(), modelNode.toString().getBytes("utf-8"));
	          
	          if(StringUtils.isEmpty(bpmDef.getDefId())){//创建新的流程定义
	        	  bpmDef.setMainDefId(idGenerator.getSID());
	        	  bpmDef.setModelId(modelData.getId());
		        
	        	  bpmDefManager.createEntity(bpmDef);
	          }else if("true".equals(isDeployNew)){//发布新的版本
	        	  //发布新的
	        	  bpmDefManager.doDeployNew(bpmDef, modelData.getId());
	          }else{//生成一新版本,关联该流程定义,不发布流程定义
	        	  bpmDefManager.doCopyNewVersion(bpmDef,modelData.getId());
	          }
	          
	          msg="成功上传BPMN流程文件!";
	          success=true;
	          
	        }

发布流程定义后,可以通过以下代码取到bpmn文件:

/**
	 * 取得流程定义的XML
	 * 
	 * @param deployId
	 * @return
	 */
	public String getDefXmlByDeployId(String deployId){
		String sql = "select a.* from ACT_GE_BYTEARRAY a where NAME_ LIKE '%bpmn20.xml' and DEPLOYMENT_ID_= ? ";
		final LobHandler lobHandler = new DefaultLobHandler(); // reusable
		final ByteArrayOutputStream contentOs = new ByteArrayOutputStream();
		String defXml = null;
		try{
			jdbcTemplate.query(sql, new Object[]{deployId },new AbstractLobStreamingResultSetExtractor<Object>(){
						public void streamData(ResultSet rs) throws SQLException, IOException{
							FileCopyUtils.copy(lobHandler.getBlobAsBinaryStream(rs, "BYTES_"), contentOs);
						}
				}
			);
			defXml = new String(contentOs.toByteArray(), "UTF-8");
		} catch (Exception ex){
			ex.printStackTrace();
		}
		return defXml;
	}

 具体访问效果如:

http://www.redxun.cn:8020/saweb/login.jsp

访问流程管理—》流程定义设计

相关推荐