開源框架eweb4j發佈1.b.7新版本

eweb4j是一個簡單、輕量的JavaWeb開發框架。涵蓋MVC、ORM、IOC。

超輕量、面向接口,擴展靈活。
可先使用本框架快速開發,然後方便的轉換成SSH。只需修改部分代碼,無需修改業務邏輯代碼。
MVC、IOC、ORM自由選擇開閉,靈活與其他框架集成。

特性:
Component組件支持
[list]
[*]DWZ組件
[*]分頁組件
[*]……
[/list]
MVC
[list]
[*]約定由於配置,可零配置。
[*]兩種配置方式(xml,註解)供您選擇
[*]更加靈活方便的url mapping
[*]兩種參數綁定方式(類屬性、方法參數)供您選擇
[*]RESTful
[*]Json渲染
[*]URL攔截器
[*]action驗證器
[*]……
[/list]
IOC
[list]
[*]基本數據類型注入
[*]無限制嵌套Pojo注入
[*]setter方法注入和構造方法注入
[/list]
ORM
[list]
[*]約定由於配置,可零配置。
[*]兩種配置方式(xml、註解),供您選擇
[*]簡單的DAOFactory,提供InsertDAO、DeleteDAO、UpdateDAO、SelectDAO、DivPageDAO、SearchDAO、CascadeDAO接口。
[*]支持表關係、級聯操作
[*]事務模板
[*]JdbcTemplate
[*]支持多個數據源
[*]無緩存,無懶加載,主動級聯操作,純粹的ORM
[*]……
[/list]
各種Util工具類
[list]
[*]BeanXMLUtil,一個pojo序列化和反序列化到xml的工具類,基於註解
[*]FileUtil,一個文件操作工具類
[*]JsonConverter,JSON轉換工具類
[*]ReflectUtil,一個反射工具類,能獲取setter、getter,使反射獲取方法,屬性更簡易,支持父類的反射
[*]……
[/list]

簡單就是美。使用文檔已經編寫齊全,在下載包裏有,PDF格式。
純開源、純分享,不喜勿噴,還望各位大大多多支持。
下載地址:[url]http://code.google.com/p/eweb4j/downloads/list[/url]
啓動配置文件

<?xml version="1.0" encoding="UTF-8"?>

<beans>
<bean xmlBean="com.cfuture08.eweb4j.config.bean.ConfigBean">
<debug>0</debug>
<ioc xmlBean="com.cfuture08.eweb4j.config.bean.ConfigIOC">
<open>1</open>
<debug>true</debug>
<logFile>logs/ioc.log</logFile>
<logMaxSize>5</logMaxSize>
<iocXmlFiles xmlBean="com.cfuture08.eweb4j.config.bean.IOCXmlFiles">
<path>ioc.xml</path>
</iocXmlFiles>
</ioc>
<orm xmlBean="com.cfuture08.eweb4j.config.bean.ConfigORM">
<open>1</open>
<debug>true</debug>
<logFile>logs/orm.log</logFile>
<logMaxSize>5</logMaxSize>
<dataSource>com.mchange.v2.c3p0.ComboPooledDataSource</dataSource>
<scanPojoPackage xmlBean="com.cfuture08.eweb4j.config.bean.ScanPojoPackage">
<path>.</path>
</scanPojoPackage>
<ormXmlFiles xmlBean="com.cfuture08.eweb4j.config.bean.ORMXmlFiles">
<path></path>
</ormXmlFiles>
<dbInfoXmlFiles xmlBean="com.cfuture08.eweb4j.config.bean.DBInfoXmlFiles">
<path>mysql_con_info.xml</path>
</dbInfoXmlFiles>
</orm>
<mvc xmlBean="com.cfuture08.eweb4j.config.bean.ConfigMVC">
<open>true</open>
<debug>true</debug>
<logFile>logs/mvc.log</logFile>
<logMaxSize>5</logMaxSize>
<scanActionPackage xmlBean="com.cfuture08.eweb4j.config.bean.ScanActionPackage">
<path>.</path>
</scanActionPackage>
<actionXmlFiles xmlBean="com.cfuture08.eweb4j.config.bean.ActionXmlFile">
<path></path>
</actionXmlFiles>
<interXmlFiles xmlBean="com.cfuture08.eweb4j.config.bean.InterXmlFile">
<path>interceptor.xml</path>
</interXmlFiles>
</mvc>
</bean>
</beans>



新版本1.b.7更新:
(1) Controller支持以下約定:
action方法不填寫註解RequestMapping的時候,默認以方法名爲url-mapping值。
action 方法返回類型爲void的時候框架不做任何跳轉處理。
缺省返回值忽略大小寫。例如ajax。
類名如果以“Controller”,“Control”,“Action”結尾的話,可以不用添加@Controller註解,框架也能識別爲控制器。
(2)mvc中,爲了改進性能,將Class.forName(ActionBeanName),改爲緩存方式調用。這樣省去了lookup的過程,效能會提升。當系統需要應對高併發的時候,很有效。
(3)廢除了formBean的配置,框架自動識別pojo參數,並且支持無限嵌套pojo屬性。無需任何配置。
(4)將MVC的@Result(location="")屬性改爲@Result(value="")
(5)將ORM的BaseDAO.getOne(Integer id)之類的Integer改爲Number
(6)讓MVC Controller 的屬性的 setter 方法 和 getter 方法 無法映射到url。前提是這些方法沒有使用@RequestMapping註解
(7)修復 MVC Controller 實例屬性與action方法@RequestMapping("xxx/{id}")中的“{}”變量的綁定。
(8)當MVC Controller 實例屬性綁定的UrlParam的值爲null時,不將參數值注入到該屬性。
(9)添加Json支持,MVC的@Result(type="json"),被註解的方法,返回值會被JsonConverter.convert(re),接着被寫入到響應流。
(10)所有的註解都被加上@Inherited,允許類、public方法上的註解被子類繼承。
(11)框架使用的反射工具類範圍調整至涵蓋父類。原先取得某個類的屬性,是不能獲取到它的父類的屬性的(同理,方法也是),現在父類的也會被獲取,主要考慮到MVC的Controller、ORM的
POJO等對象需要使用繼承來複用代碼。
(12)修復MVC的Controller無法實例化的時候框架報致命錯誤導致程序中斷,現在爲跳過該類,將其認爲非控制器,且繼續處理下一個類。
(13)修復MVC的Controller屬性綁定到UrlParam過程中,pojo屬性互相調用導致的循環遞歸異常。
(14)ORM的CascadeDAO @One 關係中的級聯更新操作,對新更新的關聯對象如果在數據庫中找不到記錄就不被更新的規則修改爲當關聯對象爲null或者其主鍵值爲null的時候不級聯更新。例如
更新某個菜單的父菜單爲空這種情況。
(16)重新調整了MVC的Validator有關的註解。去掉了冗餘。舉個實例:

這是修改之前的寫法:
@Validator({"int","required","length"})
@ValField(index={0,1,2},field={"id","name","name"},value={"id必須爲數字","name必填","name長度4-6位"})
@ValParam(index={2,2},name={"minLength","maxLength"},value={"4","6"})
public void doActoin(){}

這是修改之後的寫法:
@Validator({"int","required","length"})
@ValField({"id","name"})
@ValMess(validator={0,1,2},field={0,1,1},value={"id必須爲數字","name必填","name長度4-6位"})
@ValParamName("minLength","maxLength")
@ValParam(valMess={2,2},name={0,1},value={"4","6"})
public void doActoin(){}

可以看到,原來的ValField對要驗證的field會產生冗餘,例如上例中的“name”,修改後是將這些field寫到另外一個@ValField註解中,然後在@ValMess中進行引用,注意那些數字引用表表
示的是數組下標。
同樣道理,對於@ValParam來說也是這樣,爲了去掉冗餘。

備註:說去掉冗餘是不嚴謹的說法,實際上應該是將語言文字的冗餘轉化爲數字的冗餘,後者相對而言是降低了冗餘。思路也更清晰。

(17)IOC增加@Ioc註解,只能用在Action類屬性上,且需要setter方法才能注入。具體用法:
@Controller
class TestAction{
@Ioc("這裏填寫在IOC配置文件裏的BeanID,不填的話默認是簡單類名TestDAO")
private TestService testService;
public void setTestService(TestService testService){
this.testService = testService;
}
}

(18)添加 DAOFactory.getSelectDAO().selectCount(Class<?> clazz,String condition,Object... args);用來查詢表的記錄數。給定where條件,允許有?佔位符。
(19)修復了當被掃描的包裏含有.svn或者其他非.class文件的時候,框架啓動失敗的一個bug
(20)改善了MVC.Action的ParamUtil進行URL參數綁定到Aciton屬性的時候,如果是屬性爲pojo,例如:
private Pet pet;
那麼綁定參數的時候,優先找到 pet.name綁定到pet裏面的name屬性。
如果找不到pet.name 參數,則找name參數綁定。如果還找不到,就不進行任何綁定。
可以看到跟屬性名字“pet”有關,可以理解爲參數的一個scope,這樣解決了不同pojo但相同屬性名字的參數綁定。
url:/testPojoParamAndUrlParam?name=thisName&pet.name=xiaohei&master.name=weiwei
支持深層次。例如:
url:/testPojoParamAndUrlParam?name=thisName&pet.master.name=masterName&pet.name=petName

(21)MVC Action URL變量支持多個。例如:
@RequestMapping("test/{cate}/{name}/edit")
public void testParamArg(){

}
要注意不要無任何間隔的連續寫兩個變量,例如:"test/{cate}{name}/edit"這是不合法的。"test/{cate}at{name}/edit"這是合法的。
(22)ORM 持久化類屬性 添加了@Ignore 註解,表示該屬性不映射到表字段中。


上些實例代碼,其中包括快要發佈的Simportal新版本。

package com.cfuture08.eweb4j.component.dwz.menu.domain.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import com.cfuture08.eweb4j.orm.config.annotation.Column;
import com.cfuture08.eweb4j.orm.config.annotation.Id;
import com.cfuture08.eweb4j.orm.config.annotation.Many;
import com.cfuture08.eweb4j.orm.config.annotation.One;
import com.cfuture08.eweb4j.orm.config.annotation.Table;

/**
* DWZ中左邊樹形結構菜單模型
*
* @author weiwei
*
*/
@Table("t_TreeMenu")
public class TreeMenu implements Serializable {
private static final long serialVersionUID = 5264149396283615258L;
@Id
@Column("id")
private Long treeMenuId = 0L;
private String name;// 菜單名
private String target = "navTab";// navTab或dialog
private String rel;// navTabId
private String reloadFlag = "1";// 是否自動刷新
private String href;// 跳轉路徑
private int rank;// 排序
@One(column = "pid")
private TreeMenu parent;// 父親菜單
@One(column = "navMenuId")
private NavMenu navMenu = new NavMenu();// 所在的導航菜單
@Many(target = TreeMenu.class, column = "pid")
private List<TreeMenu> children = new ArrayList<TreeMenu>();// 孩子菜單們

public Long getTreeMenuId() {
return treeMenuId;
}

public void setTreeMenuId(Long treeMenuId) {
this.treeMenuId = treeMenuId;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public TreeMenu getParent() {
return parent;
}

public void setParent(TreeMenu parent) {
this.parent = parent;
}

public int getRank() {
return rank;
}

public void setRank(int rank) {
this.rank = rank;
}

public String getTarget() {
return target;
}

public void setTarget(String target) {
this.target = target;
}

public String getRel() {
return rel;
}

public void setRel(String rel) {
this.rel = rel;
}

public String getReloadFlag() {
return this.reloadFlag;
}

public void setReloadFlag(String reloadFlag) {
this.reloadFlag = reloadFlag;
}

public String getHref() {
return href;
}

public void setHref(String href) {
this.href = href;
}

public NavMenu getNavMenu() {
return navMenu;
}

public void setNavMenu(NavMenu navMenu) {
this.navMenu = navMenu;
}

public List<TreeMenu> getChildren() {
return children;
}

public void setChildren(List<TreeMenu> children) {
this.children = children;
}

public String toString() {
Long pid = parent == null ? 0L : parent.treeMenuId;
return "{\"treeMenuId\":\"" + treeMenuId + "\",\"navMenuId\":\""
+ navMenu.getNavMenuId() + "\",\"pid\":\"" + pid
+ "\",\"name\":\"" + name + "\",\"href\":\"" + href + "\"}";
}
}



package com.lurencun.ewebdemo;
import java.io.PrintWriter;

import com.cfuture08.eweb4j.mvc.annotation.Param;
import com.cfuture08.eweb4j.mvc.annotation.RequestMapping;
import com.cfuture08.eweb4j.orm.dao.factory.DAOFactory;
public class HelloAction {
//默認方法名爲訪問url
public void helloWorld(PrintWriter out) {
out.print("Hello World !");
}

@RequestMapping("hello")
public void doHello(PrintWriter out){
out.print("doHello");
}

@RequestMapping(value="articles/{id}",method="GET")
public void restful(PrintWriter out,@Param("id")int id){
out.print("id->"+id);
}

public String defaultForward(){
return "test.jsp";
}

public String forward(){
return "forward:test.jsp";
}

public String redirect(){
return "redirect:test.jsp";
}

/**
* url: "/test?name=ooo&pojo.name=xxx&pojo.age=2"
*
*/
public void bindParam(String name,@Param("pojo") Pojo pojo){
Long id = (Long) DAOFactory.getInsertDAO().insert(pojo);
pojo.setId(id);
DAOFactory.getCascadeDAO().update(pojo, "xxx","ooo");
}

public void testDAO(){

}
}



最新的Simportal版本還在開發當中。貼上部分代碼:

package com.cfuture08.eweb4j.component.dwz.menu.action.treeMenu;

import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;

import com.cfuture08.eweb4j.component.dwz.DWZ;
import com.cfuture08.eweb4j.component.dwz.menu.constant.CommonCons;
import com.cfuture08.eweb4j.component.dwz.menu.constant.TreeMenuCons;
import com.cfuture08.eweb4j.component.dwz.menu.domain.model.TreeMenu;
import com.cfuture08.eweb4j.component.dwz.menu.domain.service.TreeMenuService;
import com.cfuture08.eweb4j.ioc.annotation.Ioc;
import com.cfuture08.eweb4j.mvc.annotation.Controller;
import com.cfuture08.eweb4j.mvc.annotation.RequestMapping;
import com.cfuture08.eweb4j.mvc.annotation.Singleton;

/**
* 抽象類BaseAction
*
* @author weiwei
*
*/
@Controller
@Singleton
@RequestMapping(TreeMenuCons.MODULE_PATH)
public abstract class BaseAction {
@Ioc(TreeMenuCons.IOC_SERVICE_BEAN_ID)
protected TreeMenuService service = null;
@Ioc(CommonCons.IOC_DWZ_BEAN_ID)
protected DWZ dwz = null;

protected HttpServletRequest request;
protected PrintWriter out;

protected TreeMenu treeMenu;

protected Long[] ids;
protected Long id = 0L;
protected String keyword = "";
protected Long treeMenuId = 0L;
protected Long navMenuId = 0L;
protected int pageNum = 1;
protected int numPerPage = 20;

//setter and getter
}



package com.cfuture08.eweb4j.component.dwz.menu.action.treeMenu;

import com.cfuture08.eweb4j.component.dwz.menu.constant.TreeMenuCons;
import com.cfuture08.eweb4j.mvc.annotation.Param;
import com.cfuture08.eweb4j.mvc.annotation.RequestMapping;
import com.cfuture08.eweb4j.mvc.annotation.ValField;
import com.cfuture08.eweb4j.mvc.annotation.ValMess;
import com.cfuture08.eweb4j.mvc.annotation.Validator;

public class AddAction extends BaseAction {
@RequestMapping(value = "/new", method = "GET|POST")
public String doNew() {
request.setAttribute("model", TreeMenuCons.MODEL_NAME);
return "add.jsp";
}

@RequestMapping(value = "/", method = "POST", showValErr = "dwzJson")
@Validator({ "int", "required" })
@ValField({ "rank", "navMenus.dwz_navMenu2.navMenuId" })
@ValMess(validator = { 0, 1 }, field = { 0, 1 }, value = { "排序項只能輸入數字",
"導航菜單不能爲空" })
public void doAdd(@Param("navMenus.dwz_navMenu2.navMenuId") Long navMenuId,
@Param(value = TreeMenuCons.MODEL_NAME
+ ".dwz_treeMenu2.treeMenuId", init = "0") Long pid) {
try {
service.addWithCascade(treeMenu, navMenuId, pid);
out.print(TreeMenuCons.DWZ_SUCCESS_JSON_CLOSE_CURRENT);
} catch (Exception e) {
out.print(dwz.getFailedJson(e.getMessage()));
}
}
}




package com.cfuture08.eweb4j.component.dwz.menu.domain.service;

import java.util.ArrayList;
import java.util.List;

import com.cfuture08.eweb4j.component.DivPageComp;
import com.cfuture08.eweb4j.component.dwz.menu.constant.CommonCons;
import com.cfuture08.eweb4j.component.dwz.menu.constant.TreeMenuCons;
import com.cfuture08.eweb4j.component.dwz.menu.domain.dao.NavMenuDAO;
import com.cfuture08.eweb4j.component.dwz.menu.domain.dao.TreeMenuDAO;
import com.cfuture08.eweb4j.component.dwz.menu.domain.model.NavMenu;
import com.cfuture08.eweb4j.component.dwz.menu.domain.model.TreeMenu;
import com.cfuture08.eweb4j.component.dwz.menu.exception.CannotBeParentMenuException;
import com.cfuture08.eweb4j.component.dwz.menu.exception.CannotFindNavMenuException;
import com.cfuture08.eweb4j.component.dwz.menu.exception.CannotFindParentMenuException;
import com.cfuture08.eweb4j.component.dwz.menu.exception.DuplicateMenuNameException;
import com.cfuture08.eweb4j.component.dwz.menu.exception.MenuDataAccessException;
import com.cfuture08.eweb4j.component.dwz.menu.exception.MenuNotFoundException;
import com.cfuture08.eweb4j.component.dwz.view.EditPage;
import com.cfuture08.eweb4j.component.dwz.view.ListPage;
import com.cfuture08.eweb4j.component.dwz.view.SearchForm;

public class TreeMenuServiceImpl implements TreeMenuService {

private SearchForm searchForm = new SearchForm(TreeMenuCons.MODEL_NAME
+ "/search", "");

private NavMenuDAO navMenuDAO = null;
private TreeMenuDAO treeMenuDAO = null;

// IOC注入
public void setNavMenuDAO(NavMenuDAO navMenuDAO) {
if (this.navMenuDAO == null)
this.navMenuDAO = navMenuDAO;
}

// IOC注入
public void setTreeMenuDAO(TreeMenuDAO treeMenuDAO) {
if (this.treeMenuDAO == null)
this.treeMenuDAO = treeMenuDAO;
}

@Override
public ListPage getPageWithCascade(int pageNum, int numPerPage)
throws MenuDataAccessException {
List<TreeMenu> list = treeMenuDAO.getPageWithCascade(pageNum,
numPerPage);
DivPageComp dpc = new DivPageComp(pageNum, numPerPage,
this.treeMenuDAO.countAll(), CommonCons.DIV_PAGE_BTN_MAX_SHOW);

return new ListPage(TreeMenuCons.MODEL_NAME, searchForm, list, dpc);
}

@Override
public ListPage getSearchResult(String keyword, int pageNum, int numPerPage)
throws MenuDataAccessException {

if (keyword == null || keyword.length() == 0) {
searchForm.setKeyword("");
return getPageWithCascade(pageNum, numPerPage);
}

List<TreeMenu> list = treeMenuDAO.getSearchResult(keyword, pageNum,
numPerPage);

DivPageComp dpc = new DivPageComp(pageNum, numPerPage,
this.treeMenuDAO.countSearch(keyword),
CommonCons.DIV_PAGE_BTN_MAX_SHOW);

searchForm.setKeyword(keyword);

return new ListPage(TreeMenuCons.MODEL_NAME, searchForm, list, dpc);
}

@Override
public ListPage getParentsPage(Long navMenuId, Long treeMenuId,
int pageNum, int numPerPage) throws MenuDataAccessException,
CannotFindNavMenuException {
return getParentsSearchResult(navMenuId, treeMenuId, "", pageNum,
numPerPage);
}

@Override
public ListPage getParentsSearchResult(Long navMenuId, Long treeMenuId,
String keyword, int pageNum, int numPerPage)
throws MenuDataAccessException, CannotFindNavMenuException {
if (navMenuId == null || navMenuId <= 0)
throw new CannotFindNavMenuException();

NavMenu navMenu = this.navMenuDAO.getOne(navMenuId);
if (navMenu == null)
throw new CannotFindNavMenuException();

if (keyword == null)
keyword = "";

List<TreeMenu> pojos = this.treeMenuDAO.getParentSearchResult(pageNum,
numPerPage, keyword, navMenuId, treeMenuId);

if (pojos == null)
pojos = new ArrayList<TreeMenu>();
TreeMenu tm = new TreeMenu();
tm.setTreeMenuId(0L);
tm.setName("主菜單-沒有父節點");
tm.setNavMenu(navMenu);
pojos.add(tm);

long allCount = this.treeMenuDAO.countParentSearchResult(keyword,
navMenuId, treeMenuId);
DivPageComp dpc = new DivPageComp(pageNum, numPerPage, allCount,
CommonCons.DIV_PAGE_BTN_MAX_SHOW);

searchForm.setAction(TreeMenuCons.MODEL_NAME + "/" + navMenuId
+ "/lookupSearch");
searchForm.setKeyword(keyword);

return new ListPage(TreeMenuCons.MODEL_NAME, searchForm, pojos, dpc);
}

@Override
public String getParentsFormatJson(Long navMenuId, Long treeMenuId)
throws MenuDataAccessException, CannotFindNavMenuException {
if (navMenuId == null || navMenuId <= 0)
throw new CannotFindNavMenuException();

NavMenu navMenu = this.navMenuDAO.getOne(navMenuId);
if (navMenu == null)
throw new CannotFindNavMenuException();

List<TreeMenu> pojos = this.treeMenuDAO
.getParent(navMenuId, treeMenuId);
if (pojos == null)
pojos = new ArrayList<TreeMenu>();

TreeMenu tm = new TreeMenu();
tm.setTreeMenuId(0L);
tm.setName("主菜單-沒有父節點");
tm.setNavMenu(navMenu);

pojos.add(tm);

return pojos.toString();
}

@Override
public void batchRemove(Long[] ids) throws MenuDataAccessException,
MenuNotFoundException {
TreeMenu treeMenu = null;
for (Long id : ids) {
treeMenu = treeMenuDAO.getOne(id);
if (treeMenu == null)
throw new MenuNotFoundException();

treeMenuDAO.deleteOne(id);
}
}

@Override
public void removeOne(Long id) throws MenuDataAccessException,
MenuNotFoundException {
TreeMenu treeMenu = treeMenuDAO.getOne(id);
if (treeMenu == null)
throw new MenuNotFoundException();

treeMenuDAO.deleteOne(id);
}

@Override
public void addWithCascade(TreeMenu treeMenu, Long navMenuId, Long pid)
throws MenuDataAccessException, CannotFindNavMenuException,
CannotFindParentMenuException, CannotBeParentMenuException,
DuplicateMenuNameException {

TreeMenu tmp_TreeMenu = this.treeMenuDAO.getOneByName(treeMenu
.getName());
if (tmp_TreeMenu != null)
throw new DuplicateMenuNameException();

if (navMenuId == null || navMenuId <= 0)
throw new CannotFindNavMenuException();

NavMenu navMenu = this.navMenuDAO.getOne(navMenuId);
if (navMenu == null)
throw new CannotFindNavMenuException();

if (pid > 0) {
TreeMenu parent = this.treeMenuDAO.getOne(pid);
if (parent == null)
throw new CannotFindParentMenuException();
if (pid.equals(treeMenu.getTreeMenuId()))
throw new CannotBeParentMenuException();
treeMenu.setParent(parent);
} else {
treeMenu.setParent(new TreeMenu());
}

treeMenu.setNavMenu(navMenu);

String reloadFlag = treeMenu.getReloadFlag();
treeMenu.setReloadFlag("on".equals(reloadFlag) ? "1" : "0");

this.treeMenuDAO.create(treeMenu);
}

@Override
public EditPage<TreeMenu> getEditPage(Long id)
throws MenuDataAccessException, MenuNotFoundException {

TreeMenu treeMenu = this.treeMenuDAO.getOne(id);
if (treeMenu == null)
throw new MenuNotFoundException();

String reloadFlag = treeMenu.getReloadFlag();
treeMenu.setReloadFlag("1".equals(reloadFlag) ? "checked" : "");

EditPage<TreeMenu> editPage = new EditPage<TreeMenu>(
TreeMenuCons.MODEL_NAME, TreeMenuCons.MODEL_NAME + "/" + id,
treeMenu);

return editPage;
}

@Override
public void updateWithCascade(TreeMenu treeMenu, Long navMenuId, Long pid)
throws MenuDataAccessException, MenuNotFoundException,
CannotFindNavMenuException, CannotFindParentMenuException,
CannotBeParentMenuException, DuplicateMenuNameException {
Long id = treeMenu.getTreeMenuId();
if (id == null || id <= 0)
throw new MenuNotFoundException();

TreeMenu tmp_TreeMenu = this.treeMenuDAO.getOne(treeMenu
.getTreeMenuId());
if (tmp_TreeMenu == null)
throw new MenuNotFoundException();

tmp_TreeMenu = this.treeMenuDAO.getOneByName(treeMenu.getName());
if (tmp_TreeMenu != null
&& !tmp_TreeMenu.getTreeMenuId().equals(
treeMenu.getTreeMenuId()))
throw new DuplicateMenuNameException();

if (navMenuId == null || navMenuId <= 0)
throw new CannotFindNavMenuException();
NavMenu navMenu = this.navMenuDAO.getOne(navMenuId);
if (navMenu == null)
throw new CannotFindNavMenuException();

if (pid > 0) {
TreeMenu parent = this.treeMenuDAO.getOne(pid);
if (parent == null)
throw new CannotFindParentMenuException();
if (pid.equals(treeMenu.getTreeMenuId()))
throw new CannotBeParentMenuException();
treeMenu.setParent(parent);
}else {
treeMenu.setParent(new TreeMenu());
}

treeMenu.setNavMenu(navMenu);

String reloadFlag = treeMenu.getReloadFlag();
treeMenu.setReloadFlag("on".equals(reloadFlag) ? "1" : "0");

this.treeMenuDAO.update(treeMenu);
}

@Override
public List<TreeMenu> getTopParent(Long navMenuId)
throws CannotFindNavMenuException, MenuDataAccessException {
if (navMenuId == null || navMenuId <= 0)
throw new CannotFindNavMenuException();

NavMenu navMenu = this.navMenuDAO.getOne(navMenuId);
if (navMenu == null)
throw new CannotFindNavMenuException();

return this.treeMenuDAO.getTopParentOrderByRankASC(navMenuId);
}

@Override
public List<TreeMenu> getChildren(Long pid)
throws CannotFindParentMenuException, MenuDataAccessException {
if (pid == null || pid <= 0)
throw new CannotFindParentMenuException();

TreeMenu parent = this.treeMenuDAO.getOne(pid);
if (parent == null)
throw new CannotFindParentMenuException();

return this.treeMenuDAO.getChildrenOrderByRankASC(pid);
}
}




package com.cfuture08.eweb4j.component.dwz.menu.domain.dao;

import java.util.List;

import com.cfuture08.eweb4j.component.dwz.menu.domain.model.TreeMenu;
import com.cfuture08.eweb4j.component.dwz.menu.exception.MenuDataAccessException;
import com.cfuture08.eweb4j.orm.dao.DAOException;
import com.cfuture08.eweb4j.orm.dao.base.BaseDAO;
import com.cfuture08.eweb4j.orm.dao.base.BaseDAOImpl;
import com.cfuture08.eweb4j.orm.dao.factory.DAOFactory;
import com.cfuture08.eweb4j.orm.sql.constant.OrderType;

public class TreeMenuDAOImpl implements TreeMenuDAO {
private BaseDAO<TreeMenu> dao = new BaseDAOImpl<TreeMenu>(TreeMenu.class);

@Override
public List<TreeMenu> getPageWithCascade(int pageNum, int numPerPage)
throws MenuDataAccessException {
List<TreeMenu> list = null;
try {
list = dao.getPage(pageNum, numPerPage);
if (list != null)
DAOFactory.getCascadeDAO().select(
list.toArray(new TreeMenu[] {}), "parent", "navMenu");
} catch (DAOException e) {
throw new MenuDataAccessException(e.getMessage());
}

return list;
}

@Override
public long countAll() throws MenuDataAccessException {
long count = 0;
try {
count = dao.countAll();
} catch (DAOException e) {
throw new MenuDataAccessException(e.getMessage());
}

return count;
}

@Override
public long countSearch(String keyword) throws MenuDataAccessException {
long count = 0;
try {
String condition = "name like '%" + keyword + "%'";
count = DAOFactory.getSelectDAO().selectCount(TreeMenu.class,
condition);
} catch (DAOException e) {
throw new MenuDataAccessException(e.getMessage());
}

return count;
}

@Override
public List<TreeMenu> getSearchResult(String keyword, int pageNum,
int numPerPage) throws MenuDataAccessException {
List<TreeMenu> list = null;
try {
String[] fields = new String[] { "name" };
list = dao.searchByKeywordAndPaging(fields, keyword, pageNum,
numPerPage, 0);
if (list != null)
DAOFactory.getCascadeDAO().select(
list.toArray(new TreeMenu[] {}), "parent", "navMenu");
} catch (DAOException e) {
throw new MenuDataAccessException(e.getMessage());
}

return list;
}

@Override
public TreeMenu getOne(Long id) throws MenuDataAccessException {
TreeMenu treeMenu = null;
try {
treeMenu = dao.getOne(id);
if (treeMenu != null)
DAOFactory.getCascadeDAO()
.select(treeMenu, "navMenu", "parent");
} catch (DAOException e) {
throw new MenuDataAccessException(e.getMessage());
}

return treeMenu;
}

@Override
public void update(TreeMenu treeMenu) throws MenuDataAccessException {
try {
DAOFactory.getUpdateDAO().update(treeMenu);
DAOFactory.getCascadeDAO().update(treeMenu, "parent", "navMenu");
} catch (DAOException e) {
throw new MenuDataAccessException(e.getMessage());
}
}

@Override
public void create(TreeMenu treeMenu) throws MenuDataAccessException {
try {
Long treeMenuId = Long.parseLong(""
+ DAOFactory.getInsertDAO().insert(treeMenu));
treeMenu.setTreeMenuId(treeMenuId);
DAOFactory.getCascadeDAO().update(treeMenu, "parent", "navMenu");
} catch (DAOException e) {
throw new MenuDataAccessException(e.getMessage());
}
}

@Override
public List<TreeMenu> getParent(Long navMenuId, Long treeMenuId)
throws MenuDataAccessException {
List<TreeMenu> pojos = null;
try {
String condition = "id <> ? and navMenuId = ? ";
pojos = DAOFactory.getSelectDAO().selectWhere(TreeMenu.class,
condition, treeMenuId, navMenuId);
if (pojos != null)
DAOFactory.getCascadeDAO().select(
pojos.toArray(new TreeMenu[] {}), "parent", "navMenu");

} catch (DAOException e) {
throw new MenuDataAccessException(e.getMessage());
}

return pojos;
}

@Override
public List<TreeMenu> getParentSearchResult(int pageNum, int numPerPage,
String keyword, Long navMenuId, Long treeMenuId)
throws MenuDataAccessException {
List<TreeMenu> pojos = null;
try {
String condition = "id <> ? and navMenuId = ? and name like ? ";
Object[] args = new Object[] { treeMenuId, navMenuId,
"%" + keyword + "%" };
pojos = DAOFactory.getDivPageDAO().divPageByWhere(TreeMenu.class,
pageNum, numPerPage, condition, args);
if (pojos != null)
DAOFactory.getCascadeDAO().select(
pojos.toArray(new TreeMenu[] {}), "parent", "navMenu");

} catch (DAOException e) {
throw new MenuDataAccessException(e.getMessage());
}

return pojos;
}

@Override
public long countParentSearchResult(String keyword, Long navMenuId,
Long treeMenuId) throws MenuDataAccessException {
long count = 0;
try {
String condition = "id <> '" + treeMenuId + "' and navMenuId = '"
+ navMenuId + "' and name like '%" + keyword + "%'";
count = DAOFactory.getSelectDAO().selectCount(TreeMenu.class,
condition);
} catch (DAOException e) {
throw new MenuDataAccessException(e.getMessage());
}
return count;
}

@Override
public void deleteOne(Long id) throws MenuDataAccessException {
try {
dao.delete(new Long[] { id });
} catch (DAOException e) {
throw new MenuDataAccessException(e.getMessage());
}
}

@Override
public TreeMenu getOneByName(String name) throws MenuDataAccessException {
TreeMenu treeMenu = null;
try {
treeMenu = DAOFactory.getSelectDAO().selectOne(TreeMenu.class,
new String[] { "name" }, new String[] { name });
} catch (DAOException e) {
throw new MenuDataAccessException(e.getMessage());
}

return treeMenu;
}

@Override
public List<TreeMenu> getTopParentOrderByRankASC(Long navMenuId)
throws MenuDataAccessException {
List<TreeMenu> pojos = null;
try {
String[] fields = new String[] { "pid", "navMenuId" };
String[] values = new String[] { "0", String.valueOf(navMenuId) };

pojos = DAOFactory.getSearchDAO().searchByExact(TreeMenu.class,
fields, values, "rank", OrderType.ASC_ORDER, false);

} catch (DAOException e) {
throw new MenuDataAccessException(e.getMessage());
}

return pojos;
}

@Override
public List<TreeMenu> getChildrenOrderByRankASC(Long pid)
throws MenuDataAccessException {
List<TreeMenu> pojos = null;
try {
String[] fields = new String[] { "pid" };
String[] values = new String[] { String.valueOf(pid) };

pojos = DAOFactory.getSearchDAO().searchByExact(TreeMenu.class,
fields, values, "rank", OrderType.ASC_ORDER, false);

} catch (DAOException e) {
throw new MenuDataAccessException(e.getMessage());
}

return pojos;
}
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章