oa_18

oa_18:

实现WorkflowManager接口,以及流程管理的界面

需注意几个编程技巧:

1、如果页面上显示的图片数据并非磁盘上的一个图片文件,如何显示?
- 定义一个Action来读取图片,并直接写入HttpResponse对象
2、如果不知道XML文件的编码格式,如何正确读取这些XML文件,并转换为字符串?
- 利用DOM4J
3、如何在页面上显示格式良好的XML文件?
- <c:out/>标签的使用
- <pre></pre>标签的使用


package com.bjsxt.oa.manager.impl;

import java.util.List;

import com.bjsxt.oa.manager.JbpmFacade;
import com.bjsxt.oa.manager.WorkflowManager;
import com.bjsxt.oa.model.Workflow;

public class WorkflowManagerImpl extends AbstractManager implements
		WorkflowManager {

	private JbpmFacade jbpmFacade;
	
	public void addOrUpdateWorkflow(byte[] processDef, byte[] processImage) {
		String workflowName = jbpmFacade.deployProcessDefinition(processDef);
		
		//首先根据流程名称,查询是否已有Workflow对象
		Workflow wf = (Workflow)getSession().createQuery("select w from Workflow w where w.name = ?")
			.setParameter(0, workflowName)
			.uniqueResult();
		if(wf == null){
			wf = new Workflow();
			wf.setName(workflowName);
			wf.setProcessDef(processDef);
			wf.setProcessImage(processImage);
			getHibernateTemplate().save(wf);
		}else{
			wf.setProcessDef(processDef);
			wf.setProcessImage(processImage);
			getHibernateTemplate().update(wf);
		}
	}

	public void delWorkflow(int workflowId) {
		Workflow wf = findWorkflow(workflowId);
		jbpmFacade.delProcessDefinition(wf.getName());
		getHibernateTemplate().delete(wf);
	}

	public Workflow findWorkflow(int workflowId) {
		return (Workflow)getHibernateTemplate().load(Workflow.class, workflowId);
	}

	public List searchAllWorkflows() {
		return getHibernateTemplate().find("from Workflow");
	}

	public void setJbpmFacade(JbpmFacade jbpmFacade) {
		this.jbpmFacade = jbpmFacade;
	}



}



package com.bjsxt.oa.web.actions;

import java.io.ByteArrayInputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import org.dom4j.io.SAXReader;

import com.bjsxt.oa.manager.WorkflowManager;
import com.bjsxt.oa.model.Workflow;
import com.bjsxt.oa.web.forms.WorkflowActionForm;

public class WorkflowAction extends BaseAction {

	private WorkflowManager workflowManager;
	
	//打开主界面
	@Override
	protected ActionForward unspecified(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		//获取所有流程信息进行显示
		request.setAttribute("workflows", workflowManager.searchAllWorkflows());
		
		return mapping.findForward("index");
	}
	
	//上传流程定义和流程图片的信息
	public ActionForward add(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		WorkflowActionForm waf = (WorkflowActionForm)form;
		
		if(waf.getProcessDef() != null && waf.getProcessImage() != null){
			workflowManager.addOrUpdateWorkflow(
					waf.getProcessDef().getFileData(), 
					waf.getProcessImage().getFileData());
			
		}
		return mapping.findForward("add_success");
	}
	
	public ActionForward del(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		WorkflowActionForm waf = (WorkflowActionForm)form;
		workflowManager.delWorkflow(waf.getId());
		return mapping.findForward("pub_del_success");
	}
	
	//打开查看流程图片的界面
	public ActionForward viewImage(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		WorkflowActionForm waf = (WorkflowActionForm)form;
		Workflow workflow = workflowManager.findWorkflow(waf.getId());
		request.setAttribute("workflow", workflow);
		return mapping.findForward("view_image");
	}
	
	//获取流程图片信息进行显示
	public ActionForward image(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		WorkflowActionForm waf = (WorkflowActionForm)form;
		Workflow workflow = workflowManager.findWorkflow(waf.getId());
		
		byte[] processImage = workflow.getProcessImage();
		
		response.setContentType("image/jpeg");
		response.getOutputStream().write(processImage);
		
		return null;
	}
	
	//查看流程定义界面
	public ActionForward viewDef(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		WorkflowActionForm waf = (WorkflowActionForm)form;
		Workflow workflow = workflowManager.findWorkflow(waf.getId());
		request.setAttribute("workflow", workflow);
		byte[] processDef = workflow.getProcessDef();
		
		String def = new SAXReader().read(new ByteArrayInputStream(processDef)).asXML();
		
		request.setAttribute("def", def);
		
		return mapping.findForward("view_def");
	}

	public void setWorkflowManager(WorkflowManager workflowManager) {
		this.workflowManager = workflowManager;
	}
	
}

你可能感兴趣的:(apache,编程,workflow,struts,servlet)