Activiti模板相關功能詳解

聲明版本:activiti 5.22.0 一、流程設計器保存模板json

public void saveModel(String modelId, String name, String description, String json_xml, String svg_xml) throws IOException {
		Model model = repositoryService.getModel(modelId);
		ObjectNode modelJson = (ObjectNode) new ObjectMapper().readTree(model.getMetaInfo());
		modelJson.put(MODEL_NAME, name);
		modelJson.put(MODEL_DESCRIPTION, description);
		model.setMetaInfo(modelJson.toString());
		model.setName(name);
		repositoryService.saveModel(model);
		repositoryService.addModelEditorSource(model.getId(), json_xml.getBytes("utf-8"));
		InputStream svgStream = new ByteArrayInputStream(svg_xml.getBytes("utf-8"));
		final byte[] result = IOUtils.toByteArray(svgStream);
		repositoryService.addModelEditorSourceExtra(model.getId(), result);
	}

二、直接從bpmn文件導入轉換成模板app

public String convertInputStreamToModel(InputStream is, String name,
			String key, String description) {
		String modelId = null;
		
		XMLInputFactory xif = XMLInputFactory.newInstance();
		InputStreamReader isr = null;
		XMLStreamReader xtr = null;
		try {
			isr = new InputStreamReader(is, "utf-8");
			xtr = xif.createXMLStreamReader(isr);
			BpmnModel bpmnModel = new BpmnXMLConverter().convertToBpmnModel(xtr);
			// 處理異常
			if (bpmnModel.getMainProcess() == null
					|| bpmnModel.getMainProcess().getId() == null) {
				throw new BizException("模板文件可能存在問題,請檢查後重試!");
			}
			
			ObjectNode modelNode = new BpmnJsonConverter().convertToJson(bpmnModel);
			Model modelData = repositoryService.newModel();
			modelData.setKey(key);
			modelData.setName(name);

			ObjectNode modelObjectNode = new ObjectMapper().createObjectNode();
			modelObjectNode.put(ModelDataJsonConstants.MODEL_NAME, name);
			modelObjectNode.put(ModelDataJsonConstants.MODEL_REVISION, 1);
			modelObjectNode.put(ModelDataJsonConstants.MODEL_DESCRIPTION, description);
			modelData.setMetaInfo(modelObjectNode.toString());
			
			//校驗流程類型key,並自動校訂
			updateProcessKey(modelNode);
			
			repositoryService.saveModel(modelData);
			repositoryService.addModelEditorSource(modelData.getId(), modelNode.toString().getBytes("utf-8"));
			
			modelId = modelData.getId();
		} catch (Exception e) {
			e.printStackTrace();
			throw new BizException("模板文件存在問題,操做失敗!");
		} finally {
			try {
				xtr.close();
			} catch (XMLStreamException e) {
				e.printStackTrace();
			}
			try {
				isr.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			try {
				is.close();
			} catch (IOException e) {
				e.printStackTrace();
			}			
		}
		return modelId;
	}

三、模板導出bpmn文件svg

public void exportModel(HttpServletResponse response, String modelId) {
		BufferedOutputStream bos = null;  
		try {  	  	     
	        Model modelData = repositoryService.getModel(modelId);  	  
	        byte[] modelEditorSource = repositoryService  
	                .getModelEditorSource(modelData.getId()); 
	        JsonNode editorNode = new ObjectMapper().readTree(modelEditorSource);
	        BpmnJsonConverter jsonConverter = new BpmnJsonConverter();
	        BpmnModel model = jsonConverter.convertToBpmnModel(editorNode);
	        byte[] bpmnBytes;	        
	        try {
				bpmnBytes = new BpmnXMLConverter().convertToXML(model);
			} catch (Exception e) {
				bpmnBytes = new byte[10];
			}
	  
	        // 封裝輸出流  
	        bos = new BufferedOutputStream(response.getOutputStream());  
	        bos.write(bpmnBytes);// 寫入流  
	        String filename = modelData.getName();
	        if(StringUtils.isNoneBlank(filename) && filename.length() > 20) {
	        	filename = filename.substring(0, 20);
	        }
	        filename +=".bpmn20.xml";  
	        response.setContentType("application/x-msdownload;");  
	        response.setHeader("Content-Disposition",  
	                "attachment; filename=" + URLEncoder.encode(filename, "UTF-8"));  
	        response.flushBuffer();  
		} catch (Exception e) {    
			LogUtils.error("下載模板失敗:"+modelId+"erro:"+e);
		}finally {  
	        try {
				bos.flush();
			} catch (IOException e) {
				e.printStackTrace();
			}  
	        try {
				bos.close();
			} catch (IOException e) {
				e.printStackTrace();
			}  
	    }
	}

四、模板發佈爲流程設計

public Deployment convertModelToProcess(String modelId) throws IOException, UnsupportedEncodingException {
		Deployment deployment = null;
		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";
			deployment = repositoryService.createDeployment().name(modelData.getName())
					.addString(processName, new String(bpmnBytes, "UTF-8")).deploy();
		} catch (Exception e1) {
			throw new BizException("流程定義文件存在錯誤,請修改後從新部署!");
		} 
		return deployment;
	}
相關文章
相關標籤/搜索