⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 abstractcrudaction.java

📁 一个简单的java邮件系统源码
💻 JAVA
字号:
package com.easyjf.web.tools;

import java.lang.reflect.Method;

import com.easyjf.util.CommUtil;
import com.easyjf.web.Globals;
import com.easyjf.web.IWebAction;
import com.easyjf.web.Module;
import com.easyjf.web.Page;
import com.easyjf.web.WebForm;

/**
 * 
 * <p>
 * Title: 通过添删改查(CRUD)处理Action类
 * </p>
 * <p>
 * Description: 处理普通数据表的添删改查(CRUD)处理的抽象类,用户只需继承该Action,并根据自身的情况实现其中的模板方法即可。
 * 该抽象类除了提供了一些固定的系统命令以外,还提供了CmdAction的功能,也即要使用系统外命令的时候,如XX直接使用doXX(WebForm form, Module module)即可。
 * </p>
 * <p>
 * Copyright: Copyright (c) 2006
 * </p>
 * <p>
 * Company: www.easyjf.com
 * </p>
 * 
 * @author 蔡世友
 * @version 1.0
 */
public abstract class AbstractCrudAction implements ICommCRUDAction, IWebAction {
	private String command;

	/**
	 * 系统命令,包括new、edit、add、save、update、del、list、query以及null等都是EasyJWeb
	 * Tools中经常用到的系统命令,执行与命令有关的操作
	 */
	private static final String systemCommand = "new,edit,add,save,update,del,list,query,";
	private static final String manageCommand="new,edit,add,save,update,del";
	private IDAO dao;

	public Page execute(WebForm form, Module module) throws Exception {
		String method = CommUtil.null2String(form.get("easyJWebCommand"));
		this.command = method;
		if (dao == null) {// 自动加载数据操作dao对象
			dao = autoLoadDAO(module);
		}
		Object beforeCheck = doBefore(form, module); // 模版方法
		if (beforeCheck != null) {
			// 如果Action执行前检查,返回Page则直接跳转
			if (beforeCheck.getClass() == Page.class)
				return (Page) beforeCheck;
		}
		IActiveUser user = (IActiveUser) getCurrentUser(form);
		Page forward = null;
		doInit(form, module);
		
		if ("new".equals(method)) {// new命令直接返回
			forward = doNew(form, module, user);
		} else if ("edit".equals(method)) {
			forward = doEdit(form, module, user);
		} else if ("add".equals(method) || "save".equals(method)) {// save或add命令执行同样的功能
			forward = doAdd(form, module, user);
		} else if ("update".equals(method)) {
			forward = doUpdate(form, module, user);
		} else if ("del".equals(method)) {
			forward = doDel(form, module, user);
		} else if ("".equals(method) || "list".equals(method)
				|| "query".equals(method)) {
			forward = doQuery(form, module, user);
		} else// 其它命令直接调用doCommand方法
		{
			Class[] paras = new Class[2];
			paras[0] = WebForm.class;
			paras[1] = Module.class;
			String cmd = "do" + method.substring(0, 1).toUpperCase()
					+ method.substring(1);
			Method m = this.getClass().getMethod(cmd, paras);
			if (m != null) {
				Object[] objs = new Object[2];
				objs[0] = form;
				objs[1] = module;
				Object ret = m.invoke(this, objs);
				if (ret instanceof Page)
					forward = (Page) ret;
			} else
				throw new Exception("方法名称不正确,在" + this.getClass() + "中找不到"
						+ cmd + "方法!请确认您页面中的easyJWebCommand参数值是否正确!");
		}
		doAfter(form, module);
		return forward;
	}

	/**
	 * 执行编辑命令,根据用户主键取得PO,然后返回编辑界面
	 */
	public Page doNew(WebForm form, Module module, IActiveUser user) {
		return crudPage(form,module,"edit");
	}

	/**
	 * 执行add命令,保存到数据库
	 */
	public Page doAdd(WebForm form, Module module, IActiveUser user) {
		Page forward = null;
		Object obj = form2Obj(form);
		if (obj == null) {
			form.addResult("msg", "无法创建要保存的对象,添加失败!");
			return crudPage(form,module,"edit");
		}

		if (dao.save(obj)) {
			form.addResult("msg", "数据添加成功!");
			forward = doQuery(form, module, user);
		} else {
			form.addResult("msg", "数据添加失败");
			forward = crudPage(form,module,"edit");
		}
		return forward;
	}

	public Page doUpdate(WebForm form, Module module, IActiveUser user) {
		Page forward = null;
		Object obj = form2Obj(form);
		if (dao.update(obj)) {
			form.addResult("msg", "数据修改成功!");
			forward = doQuery(form, module, user);
		} else {
			form.addResult("msg", "数据修改失败");
			forward = crudPage(form,module,"edit");
		}
		return forward;
	}

	/**
	 * 执行编辑命令,根据用户主键取得PO,然后返回编辑界面
	 */
	public Page doEdit(WebForm form, Module module, IActiveUser user) {
		Page forward = null;
		Object obj = form2Obj(form);
		if (obj != null) {
			CommUtil.Obj2Map(obj, form.getTextElement());
			forward = crudPage(form,module,"edit");
		} else {
			form.addResult("msg", "找不到数据!");
			forward = doQuery(form, module, user);
		}
		return forward;
	}

	/**
	 * 执行del命令,通过dao对象删除持久层中的数据
	 */
	public Page doDel(WebForm form, Module module, IActiveUser user) {

		Object obj = form2Obj(form);
		if (dao.del(obj)) {
			form.addResult("msg", "数据删除成功!");
		} else {
			form.addResult("msg", "数据修改失败");
		}
		return doQuery(form, module, user);
	}

	/**
	 * 执行list、query或null(空)命令,调用分页引擎实现数据分页查询
	 */
	public Page doQuery(WebForm form, Module module, IActiveUser user) {
		int currentPage = CommUtil.null2Int(form.get("page"));
		int pageSize = CommUtil.null2Int(form.get("pageSize"));
		if (currentPage < 1)
			currentPage = 1;
		if (pageSize < 1)
			pageSize = 15;
		IPageList pList = doQuery(form, currentPage, pageSize);// 调用模板方法执行数据查询
		// 保存查询结果
		if (pList != null) {
			form.addResult("list", pList.getResult());
			form.addResult("pages", new Integer(pList.getPages()));
			form.addResult("rows", new Integer(pList.getRowCount()));
			form.addResult("page", new Integer(pList.getCurrentPage()));
			form.addResult("gotoPageHTML", CommUtil.showPageHtml(pList
					.getCurrentPage(), pList.getPages()));
		}
		return crudPage(form,module,"list");
	}

	public String getCommand() {
		return command;
	}

	public void setCommand(String command) {
		this.command = command;
	}

	public boolean isSystemCommand() {
		boolean ret = false;
		if ("".equals(this.command) || systemCommand.indexOf(this.command) >= 0)
			ret = true;
		return ret;
	}
	public boolean isManageCommand()
	{			
		return  (!"".equals(this.command) && manageCommand.indexOf(this.command) >= 0);			
	}
    protected Page crudPage(WebForm form,Module module,String name)
    {
    	Page ret=module.findPage(name);    	
    	if(ret==null)
    	{
    		Object obj=this.form2Obj(form);
    		if(obj!=null){
    		String className=obj.getClass().getName();
    		String objName=className.substring(className.lastIndexOf('.')+1);
    		if("edit".equals(name))
    		{   			
    			ret=new Page(objName+"Edit","/"+objName+"Edit.html",Globals.PAGE_TEMPLATE_TYPE);
    		}
    		else if ("list".equals(name))
    		{
    			ret=new Page(objName+"List","/"+objName+"List.html",Globals.PAGE_TEMPLATE_TYPE);
    		}}
    	}    	
    	return ret;
    }
	
	/**
	 * 自动加载DAO对象来负责执行数据库操作,此处需要修改根据module的配置或全局配置来决定使用具体的DAO类,当然也要以通过IOC来进行控制
	 * 
	 * @param module
	 * @return
	 */
	protected IDAO autoLoadDAO(Module module) {
		return EasyDBODAO.getInstance();
	}

	public IDAO getDao() {
		return dao;
	}

	/**
	 * 设置模块的数据库操作类dao
	 * 
	 * @param dao
	 */
	public void setDao(IDAO dao) {
		this.dao = dao;
	}

	/**
	 * 根据form中的查询参数,执行具体的查询操作
	 * 
	 * @param form
	 * @param currentPage
	 * @param pageSize
	 * @return
	 */
	public abstract IPageList doQuery(WebForm form, int currentPage,
			int pageSize);

	/**
	 * 执行初始化操作
	 * 
	 * @param form
	 * @param module
	 */
	public abstract void doInit(WebForm form, Module module);// 初始化数据,如表单项及默认值等

	/**
	 * 返回当前操作的用户
	 * 
	 * @param form
	 * @return
	 */
	public abstract IActiveUser getCurrentUser(WebForm form);// 取得用户

	/**
	 * 在doCommand命令之前执行,可用于实现通用简单的拦截
	 * 
	 * @param form
	 * @param module
	 * @return
	 */
	public abstract Object doBefore(WebForm form, Module module);// Action前置校验

	/**
	 * 在doCommand命令之后执行,可用于作一些数据清理
	 * 
	 * @param form
	 * @param module
	 */
	public abstract void doAfter(WebForm form, Module module);// Action后置处理

	/**
	 * 把form中的数据转换成实际的java对象,视图对象View Object(VO)-程序对象Program Object(PO)
	 * 
	 * @param form
	 * @return
	 */
	public abstract Object form2Obj(WebForm form);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -