首页>代码>java OA开源办公系统源码下载>/myOA/src/com/tiger/oa/action/DocumentAction.java
package com.tiger.oa.action;

import java.io.File;
import java.io.FileInputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

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

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.springframework.beans.BeanUtils;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ModelDriven;
import org.apache.commons.beanutils.ConvertUtils;
import com.tiger.oa.dto.DocumentDTO;
import com.tiger.oa.dto.DownloadFileModel;
import com.tiger.oa.dto.ImagePosition;
import com.tiger.oa.dto.TaskModel;
import com.tiger.oa.manager.DocumentManager;
import com.tiger.oa.manager.FormManager;
import com.tiger.oa.manager.WorkflowManager;
import com.tiger.oa.model.ApproveInfo;
import com.tiger.oa.model.Document;
import com.tiger.oa.model.DocumentProperty;
import com.tiger.oa.model.FieldType;
import com.tiger.oa.model.FlowForm;
import com.tiger.oa.model.FormField;
import com.tiger.oa.util.FileUtil;
import com.tiger.oa.util.SystemException;

@Controller("documentAction")
@Scope("prototype")
public class DocumentAction extends BaseAction implements ModelDriven,
		ServletRequestAware, ServletResponseAware {

	private DocumentManager documentManager;
	private WorkflowManager workflowManager;
	private FormManager formManager;

	private DocumentDTO documentDTO = new DocumentDTO();
	private Map<String,String> propsMap;
	
	private HttpServletRequest request;
	private HttpServletResponse response;

	@Resource
	public void setFormManager(FormManager formManager) {
		this.formManager = formManager;
	}
	@Resource
	public void setDocumentManager(DocumentManager documentManager) {
		this.documentManager = documentManager;
	}

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

	// 公文管理主界面,显示我的公文列表
	@Override
	public String execute() throws Exception {
		request.setAttribute("pm", documentManager
				.searchMyDocuments(currentUser().getId()));
		return "index";
	}

	/**
	 * 已审公文列表,显示由当前登录人员审核的公文列表
	 * 
	 * @return
	 * @throws Exception
	 */
	public String approvedList() throws Exception {
		request.setAttribute("pm", documentManager
				.searchApprovedDocuments(currentUser().getId()));
		return "approved_list";
	}

	/**
	 * 待审公文列表,显示等待当前登录人员审核的公文列表
	 * 
	 * @return
	 * @throws Exception
	 */
	public String approvingList() throws Exception {
		request.setAttribute("documents", documentManager.searchApprovingDocuments(currentUser().getId()));
		return "approving_list";
	}

	/**
	 * 在待审公文列表上,针对某个文档,可以点击打开审批界面,对此文档进行审批
	 * 
	 * @return
	 * @throws Exception
	 */
	public String approveInput() throws Exception {
		return "approve_input";
	}

	/**
	 * 用户输入审批信息之后,点击保存,对文档进行审批操作
	 * 
	 * @return
	 * @throws Exception
	 */
	public String approve() throws Exception {
		String comment = documentDTO.getApproveInfo();
		int documentId = documentDTO.getId();
		int approverId = currentUser().getId();

		ApproveInfo approveInfo = new ApproveInfo();
		approveInfo.setApproveTime(new Date());
		approveInfo.setComment(comment);

		documentManager.addApproveInfo(approveInfo, documentId, approverId);

		return "pub_add_success";
	}

	/**
	 * 在我的公文或待审公文列表上,点击提交,可打开提交的选择界面 在这个界面上,列出下一步所有可选的步骤,用户可以选择其中一个
	 * 步骤进行提交操作。系统将按照用户的选择转移到下一个节点
	 * 
	 * @return
	 * @throws Exception
	 */
	public String submitInput() throws Exception {
		Document doc = documentManager.findDocument(documentDTO.getId());
		TaskModel taskModel = workflowManager.searchNextTransitions(currentUser().getUsername(),doc.getProcessInstanceId());
		documentDTO.setTaskId(taskModel.getTaskId());
		request.setAttribute("steps", taskModel.getTransitions());
		return "submit_input";
	}

	/**
	 * 用户选择了其中一个步骤,点击提交
	 * @return
	 * @throws Exception
	 */
	public String submit() throws Exception {
		documentManager.submitToWorkflow(documentDTO.getId(),documentDTO.getTaskId(), documentDTO.getTransitionName());
		return "pub_add_success";
	}

	/**
	 * 查看公文的审批历史
	 * 
	 * @return
	 * @throws Exception
	 */
	public String approvedHistory() throws Exception {
		request.setAttribute("historys", documentManager.searchApproveInfos(documentDTO.getId()));
		return "approve_history";
	}

	public String del() throws Exception {
		documentManager.delDocument(documentDTO.getId());
		return "pub_del_success";
	}

	/**
	 * 点击添加公文的时候,需要选择相应的流程,此界面列出所有的流程以供选择
	 * 
	 * @return
	 * @throws Exception
	 */
	public String selectFlow() throws Exception {
		request.setAttribute("workflows", workflowManager.searchAllWorkflows());
		return "select_flow";
	}

	/**
	 * 选择了流程之后(即点击流程名称),需要打开公文录入界面
	 * 
	 * @return
	 * @throws Exception
	 */
	public String addInput() throws Exception {
		return "add_input";
	}

	/**
	 * 添加公文的操作
	 * 
	 * @return
	 * @throws Exception
	 */
	public String add() throws Exception {
		Document document = new Document();
		documentDTO.setProps(propsMap);
		BeanUtils.copyProperties(documentDTO,document);
		
		//处理Map的数据
		
		//将Map的数据,按照表单定义对应的类型来进行转换,并设置到document对象即可
		if(documentDTO.getProps().isEmpty()){
			document.setProps(null);
		}else{
			int workflowId = documentDTO.getWorkflowId();
			
			//流程对应的表单定义
			FlowForm flowForm = formManager.findForm(workflowId);
			
			if(flowForm == null){
				document.setProps(null);
			}else{
				
				Map documentProps = new HashMap();
				
				//拿到表单定义对应的所有的域定义
				Set fields = flowForm.getFields();
				
				for (Iterator iter = fields.iterator(); iter.hasNext();) {
					FormField field = (FormField) iter.next();
					
					String propertyName = field.getFieldName();
					FieldType propertyType = field.getFieldType();
					
					//根据表单的属性名称,从Map中将界面上传过来的原始值拿出来
					Object source = documentDTO.getProps().get(propertyName);
					Object target = null;
					
					if(source != null){
						
						//对于输入的字符串
						if(source instanceof String && !propertyType.getType().equals("java.io.File")){
							
							Class targetClass = Class.forName(propertyType.getType());
							
							//利用ConvertUtils工具,将从界面上传过来的字符串
							//转换为FormFiled对应的FieldType所指定类型的对象
							target = ConvertUtils.convert((String)source, targetClass);
						}
						
						//如果表单域是上传文件
						if(propertyType.getType().equals("java.io.File")){
							
							//注意:如果界面上传的是文件,struts可以自动转换为FormFile
							//类型的对象!!!!
							FileInputStream is = new FileInputStream(new File((String)source));
							byte[] fileData = new byte[is.available()];
							target = fileData;
						}
						
						//现在,需要将target的值赋予document对象
						if(target == null){
							throw new SystemException("无法处理输入的值!");
						}
						
						DocumentProperty dp = new DocumentProperty();
						String type = propertyType.getType();
						if(type.equals("java.io.File")){
							dp.setJava_io_File((byte[])target);
						}
						if(type.equals("java.lang.Integer")){
							dp.setJava_lang_Integer((Integer)target);
						}
						if(type.equals("java.lang.String")){
							dp.setJava_lang_String((String)target);
						}
						
						documentProps.put(propertyName, dp);
						System.out.println(documentProps);
					}
				}
				
				//将documentProps对象赋予document对象的props属性
				document.setProps(documentProps);
			}
		}
		
		if(documentDTO.getUpload() != null){
			document.setContent(FileUtil.getFileData(documentDTO.getUpload()));
		}
		
		documentManager.addDocument(document,documentDTO.getWorkflowId(), currentUser().getId());
		
		return "pub_add_success";
	}

	// 下载公文附件
	public String download() throws Exception {
		Document document = documentManager.findDocument(documentDTO.getId());
		response.reset();
		response.setContentType("application/x-download;charset=GBK");
		response.setHeader("Content-Disposition",
				"attachment;filename=temp.doc");
		response.getOutputStream().write(document.getContent());
		response.getOutputStream().flush();
		response.getOutputStream().close();
		// 指示struts,不要对返回值进行处理
		return null;
	}

	/*
	 * 图片页面 
	 */
	public String viewPage() throws Exception{
		ImagePosition image = workflowManager.getPngPosition(documentDTO.getProcessInstanceId());
		request.setAttribute("image", image);
		return "view";
	}
	
	// 查看图片
	public String view() throws Exception {
		DownloadFileModel file = workflowManager.getPngByteByProcessId(documentDTO.getProcessInstanceId());
		response.reset();
		response.setContentType("image/png");
		response.getOutputStream().write(file.getFileData());
		response.getOutputStream().flush();
		response.getOutputStream().close();
		return null;
	}
	
	@Override
	public Object getModel() {
		return documentDTO;
	}

	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}

	@Override
	public void setServletResponse(HttpServletResponse response) {
		this.response = response;
	}

	public Map<String,String> getPropsMap() {
		return propsMap;
	}

	public void setPropsMap(Map<String,String> propsMap) {
		this.propsMap = propsMap;
	}

}
最近下载更多
carloscarlos  LV1 2023年9月13日
15029223037  LV1 2023年7月18日
linmou  LV8 2023年3月19日
微信网友_6376279687794688  LV1 2023年3月5日
快乐的程序员  LV25 2023年1月21日
微信网友_6292700852981760  LV1 2023年1月5日
15342201772  LV7 2022年12月10日
swd287797983  LV1 2022年12月6日
wazp4071  LV1 2022年6月24日
C_mohen 2022年6月8日
暂无贡献等级
最近浏览更多
vluobo  LV1 3月19日
cbddbc  LV1 2023年12月26日
vanweiai2023 2023年11月30日
暂无贡献等级
fesfefe  LV13 2023年11月16日
yuan_bin1990  LV7 2023年11月13日
molu123456 2023年10月16日
暂无贡献等级
2036495585  LV9 2023年9月26日
漫步的海星  LV4 2023年9月21日
carloscarlos  LV1 2023年9月13日
3334004690  LV3 2023年8月19日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友