?? abstractcrudaction.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 IDAO dao;
public Page execute(WebForm form, Module module) throws Exception {
String method = CommUtil.null2String(form.get("easyJWebCommand"));
this.command = method;
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 (dao == null) {// 自動加載數據操作dao對象
dao = autoLoadDAO(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;
}
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 + -