浏览 1518 次
|
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
|---|---|
| 作者 | 正文 |
|
时间:2007-12-03
无意中看到现在还有朋友在使用Struts-menu.真的感觉挺感慨的,想想自己两年前开始使用Struts-menu,刚开始的时候,真的是被搞的有点找不到头绪,但最终,我以struts-menu为基础,构建了基于数据库的树,树访问权限,用户系统等一系列的基础框架功能,这些,也是我在华罗庚软件基地有限公司工作期间所构建的主要框架模板,今天完整的写出来,供后来使用Struts-menu的朋友参考吧.
实现过程看上去会比较难看,您就凑合着点,后面还有Struts-menu到Dtree的转化,也就是说,我们使用Struts-menu的结构,可以直接转化为Dtree的结构,不需要任何额外的工作. 关于如何将Struts-menu增加到项目中,请参考Struts-menu的官方文档,这一块,官方文档有很详细的说明,我们目前主要使用Struts-menu的database menu. 1,关于数据库设计: -- MySQL dump 10.10 DROP TABLE IF EXISTS `functionkey`; CREATE TABLE `functionkey` ( `id` varchar(36) NOT NULL, `name` varchar(255) NOT NULL, `description` varchar(255) default NULL, `location` varchar(255) default NULL, `target` varchar(255) default NULL, `INFORMATIONTYPECODE` varchar(36) default NULL, `MODELID` varchar(36) default NULL, PRIMARY KEY (`id`), KEY `FK8BFB7B876B978C10` (`INFORMATIONTYPECODE`), KEY `FK8BFB7B87A649C0EB` (`MODELID`), CONSTRAINT `FK8BFB7B876B978C10` FOREIGN KEY (`INFORMATIONTYPECODE`) REFERENCES `imformationtypecode` (`ID`), CONSTRAINT `FK8BFB7B87A649C0EB` FOREIGN KEY (`MODELID`) REFERENCES `newscategory` (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; DROP TABLE IF EXISTS `groups`; CREATE TABLE `groups` ( `id` varchar(36) NOT NULL, `name` varchar(45) NOT NULL, `description` varchar(255) default NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; -- -- Table structure for table `imformationtypecode` -- DROP TABLE IF EXISTS `imformationtypecode`; CREATE TABLE `imformationtypecode` ( `ID` varchar(36) NOT NULL, `NAME` varchar(100) default NULL, `DESCRIPTION` varchar(255) default NULL, `CODE` varchar(255) default NULL, `IMFORMATIONGRADEID` varchar(36) default NULL, PRIMARY KEY (`ID`), KEY `FK48915B14310CBA6` (`IMFORMATIONGRADEID`), CONSTRAINT `FK48915B14310CBA6` FOREIGN KEY (`IMFORMATIONGRADEID`) REFERENCES `information_grade` (`ID`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; -- -- Table structure for table `information_grade` -- DROP TABLE IF EXISTS `information_grade`; CREATE TABLE `information_grade` ( `ID` varchar(36) NOT NULL, `NAME` varchar(50) default NULL, `DESCRIPTION` varchar(255) default NULL, `createDate` date default NULL, PRIMARY KEY (`ID`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; -- -- Table structure for table `newscategory` -- DROP TABLE IF EXISTS `newscategory`; CREATE TABLE `newscategory` ( `id` varchar(36) NOT NULL, `name` varchar(45) NOT NULL, `description` varchar(45) default NULL, `isBeDeleted` char(1) default NULL, `target` varchar(255) default NULL, `onclick` varchar(255) default NULL, `onmouseover` varchar(255) default NULL, `onmouseout` varchar(255) default NULL, `image` varchar(255) default NULL, `altImage` varchar(255) default NULL, `tooltip` varchar(255) default NULL, `roles` varchar(255) default NULL, `page` varchar(255) default NULL, `width` varchar(255) default NULL, `height` varchar(255) default NULL, `forward` varchar(255) default NULL, `action` varchar(255) default NULL, `location` varchar(255) default NULL, `orderIndex` bigint(20) default NULL, `PARENTID` varchar(36) default NULL, PRIMARY KEY (`id`), KEY `FK3873C4D1A333722C` (`PARENTID`), CONSTRAINT `FK3873C4D1A333722C` FOREIGN KEY (`PARENTID`) REFERENCES `newscategory` (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; -- -- Table structure for table `privilege` -- DROP TABLE IF EXISTS `privilege`; CREATE TABLE `privilege` ( `id` varchar(36) NOT NULL, `name` varchar(45) NOT NULL, `description` varchar(255) default NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; DROP TABLE IF EXISTS `role_model_privilege`; CREATE TABLE `role_model_privilege` ( `modelid` varchar(36) NOT NULL, `roleid` varchar(36) NOT NULL, `privilegeid` varchar(36) NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8; DROP TABLE IF EXISTS `sys_permis_resc`; CREATE TABLE `sys_permis_resc` ( `PERMIS_ID` varchar(36) NOT NULL, `RESC_ID` varchar(36) NOT NULL, PRIMARY KEY (`PERMIS_ID`,`RESC_ID`), KEY `FK9C399A361D2D5E14` (`RESC_ID`), KEY `FK9C399A366CDBC45E` (`PERMIS_ID`), KEY `FKE3C0E6561D2D5E14` (`RESC_ID`), KEY `FKE3C0E6566CDBC45E` (`PERMIS_ID`), CONSTRAINT `FK9C399A361D2D5E14` FOREIGN KEY (`RESC_ID`) REFERENCES `sys_resources` (`id`), CONSTRAINT `FK9C399A366CDBC45E` FOREIGN KEY (`PERMIS_ID`) REFERENCES `sys_permissions` (`id`), CONSTRAINT `FKE3C0E6561D2D5E14` FOREIGN KEY (`RESC_ID`) REFERENCES `sys_resources` (`id`), CONSTRAINT `FKE3C0E6566CDBC45E` FOREIGN KEY (`PERMIS_ID`) REFERENCES `sys_permissions` (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; DROP TABLE IF EXISTS `sys_permissions`; CREATE TABLE `sys_permissions` ( `id` varchar(36) NOT NULL, `name` varchar(255) default NULL, `DESCN` varchar(255) default NULL, `OPERATION` varchar(255) default NULL, `STATUS` varchar(255) default NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; -- -- Table structure for table `sys_resources` -- DROP TABLE IF EXISTS `sys_resources`; CREATE TABLE `sys_resources` ( `id` varchar(36) NOT NULL, `NAME` varchar(255) NOT NULL, `MODULE_ID` varchar(36) NOT NULL, `RES_TYPE` varchar(255) NOT NULL, `RES_STRING` varchar(255) NOT NULL, `DESCN` varchar(255) default NULL, PRIMARY KEY (`id`), KEY `FK14BE02D3A502B5` (`MODULE_ID`), CONSTRAINT `FK14BE02D3A502B5` FOREIGN KEY (`MODULE_ID`) REFERENCES `newscategory` (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; DROP TABLE IF EXISTS `sys_role_functionkeys`; CREATE TABLE `sys_role_functionkeys` ( `functionkey_id` varchar(36) NOT NULL, `ROLE_ID` varchar(36) NOT NULL, PRIMARY KEY (`functionkey_id`,`ROLE_ID`), KEY `FKB3A691E3CFC0A469` (`ROLE_ID`), KEY `FKB3A691E38C3DC364` (`functionkey_id`), CONSTRAINT `FKB3A691E38C3DC364` FOREIGN KEY (`functionkey_id`) REFERENCES `functionkey` (`id`), CONSTRAINT `FKB3A691E3CFC0A469` FOREIGN KEY (`ROLE_ID`) REFERENCES `sys_roles` (`ID`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; -- -- Table structure for table `sys_role_permis` -- DROP TABLE IF EXISTS `sys_role_permis`; CREATE TABLE `sys_role_permis` ( `PERMIS_ID` varchar(36) NOT NULL, `ROLE_ID` varchar(36) NOT NULL, PRIMARY KEY (`ROLE_ID`,`PERMIS_ID`), KEY `FK1D79B171CFC0A469` (`ROLE_ID`), KEY `FK1D79B1716CDBC45E` (`PERMIS_ID`), CONSTRAINT `FK1D79B1716CDBC45E` FOREIGN KEY (`PERMIS_ID`) REFERENCES `sys_permissions` (`id`), CONSTRAINT `FK1D79B171CFC0A469` FOREIGN KEY (`ROLE_ID`) REFERENCES `sys_roles` (`ID`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; -- -- Table structure for table `sys_roles` -- DROP TABLE IF EXISTS `sys_roles`; CREATE TABLE `sys_roles` ( `ID` varchar(36) NOT NULL, `NAME` varchar(255) NOT NULL, `DESCN` varchar(255) default NULL, `groupid` varchar(36) NOT NULL, PRIMARY KEY (`ID`), KEY `FKFCE2C68B23BA1C44` (`groupid`), CONSTRAINT `FKFCE2C68B23BA1C44` FOREIGN KEY (`groupid`) REFERENCES `groups` (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; -- -- Table structure for table `sys_user_password_recover` -- DROP TABLE IF EXISTS `sys_user_password_recover`; CREATE TABLE `sys_user_password_recover` ( `ID` varchar(36) NOT NULL, `RANDOMPASSWORD` varchar(16) default NULL, `QUESTION` varchar(255) default NULL, `ANSWER` varchar(255) default NULL, `userid` varchar(36) NOT NULL, PRIMARY KEY (`ID`), UNIQUE KEY `userid` (`userid`), KEY `FK161201824BDBAC60` (`userid`), CONSTRAINT `FK161201824BDBAC60` FOREIGN KEY (`userid`) REFERENCES `sys_users` (`ID`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; -- -- Table structure for table `sys_user_role` -- DROP TABLE IF EXISTS `sys_user_role`; CREATE TABLE `sys_user_role` ( `USER_ID` varchar(36) NOT NULL, `ROLE_ID` varchar(36) NOT NULL, PRIMARY KEY (`USER_ID`,`ROLE_ID`), KEY `FKAABB7D5874EB6849` (`USER_ID`), KEY `FKAABB7D58CFC0A469` (`ROLE_ID`), KEY `FK3D86815874EB6849` (`USER_ID`), KEY `FK3D868158CFC0A469` (`ROLE_ID`), CONSTRAINT `FK3D86815874EB6849` FOREIGN KEY (`USER_ID`) REFERENCES `sys_users` (`ID`), CONSTRAINT `FK3D868158CFC0A469` FOREIGN KEY (`ROLE_ID`) REFERENCES `sys_roles` (`ID`), CONSTRAINT `FKAABB7D5874EB6849` FOREIGN KEY (`USER_ID`) REFERENCES `sys_users` (`ID`), CONSTRAINT `FKAABB7D58CFC0A469` FOREIGN KEY (`ROLE_ID`) REFERENCES `sys_roles` (`ID`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; -- -- Table structure for table `sys_users` -- DROP TABLE IF EXISTS `sys_users`; CREATE TABLE `sys_users` ( `ID` varchar(36) NOT NULL, `LOGINID` varchar(255) NOT NULL, `PASSWD` varchar(36) NOT NULL, `NAME` varchar(45) NOT NULL, `EMAIL` varchar(255) default NULL, `REGION` varchar(255) default NULL, `STATUS` varchar(1) default NULL, `DESCN` varchar(255) default NULL, `informationGradeID` varchar(36) default NULL, PRIMARY KEY (`ID`), KEY `FKFD0EC5D653BAA9A7` (`informationGradeID`), CONSTRAINT `FKFD0EC5D653BAA9A7` FOREIGN KEY (`informationGradeID`) REFERENCES `information_grade` (`ID`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; 2.实体类 实体类比较简单,按照上面的数据库结构进行映射就可以了,不再列出. 3.后台对Struts-menu数据的处理:这段处理比较混乱了一些,主要是以前项目中需要各种功能的树形结构,比如单选啊,多选啊,根据其它表结构生成树啊等等,所以增加了很多方法,增加的这些方法,最终可以实现,根据一条HQL或一个HQL名字生成一棵完整的树,在稍后的Action中会看到. package com.huasoft.misdframework.service.controller;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import net.sf.navigator.menu.MenuComponent;
import net.sf.navigator.menu.MenuRepository;
import org.apache.commons.lang.StringUtils;
import com.huasoft.misdframework.dbengine.pojo.Newscategory;
import com.huasoft.misdframework.dbengine.pojo.Privilege;
import com.huasoft.misdframework.service.querySupport.controller.QueryObject;
import com.huasoft.misdframework.service.servicelayerinterface.INewscategoryService;
import com.huasoft.misdframework.util.exception.ServiceException;
import com.huasoft.misdframework.util.helper.CollectionUtil;
import com.huasoft.misdframework.util.helper.StringPool;
public class DataDictionary extends BaseService implements INewscategoryService {
public DataDictionary() {
}
/**
* 构造授权树
*/
public MenuRepository buildMenuRepositoryForPermission(
HttpServletRequest request, List permissionedModules) {
// TODO Auto-generated method stub
MenuRepository repository = new MenuRepository();
MenuRepository defaultRepository = (MenuRepository) request
.getSession().getServletContext().getAttribute(
MenuRepository.MENU_REPOSITORY_KEY);
repository.setDisplayers(defaultRepository.getDisplayers());
List source = transNewscategoryAfterGetFromDataBase();
List arferTransNodesCollection = this
.transNodesCollectionForPermissionRepository(source,
permissionedModules);
Iterator it = arferTransNodesCollection.iterator();
while (it.hasNext()) {
HashMap item = (HashMap) it.next();
MenuComponent mc = new MenuComponent();
mc.setName(item.get("name").toString());
mc.setTitle(item.get("name").toString());
if (item.get("tooltip") != null) {
mc.setToolTip(item.get("tooltip").toString());
} else {
mc.setToolTip(item.get("name").toString());
}
if (this.isHaveParentNode(item.get("id").toString(),
arferTransNodesCollection)) {
MenuComponent parentMenu = repository.getMenu(item.get(
"parentname").toString());
if (parentMenu == null) {
parentMenu = new MenuComponent();
HashMap parentNode = this.getNode(item.get("parentid")
.toString(), arferTransNodesCollection);
parentMenu.setName(parentNode.get("name").toString());
repository.addMenu(parentMenu);
}
mc.setParent(parentMenu);
}
if (!this.isHaveChildNode(item.get("id").toString(),
arferTransNodesCollection)) {
mc.setLocation("#");
}
repository.addMenu(mc);
}
return repository;
}
/**
* 根据一个给定的节点编号,这个编号,可能是一个全局配置变量 拿出一根树 并送到界面上,供用户选择。
*
* @param request
* HttpServletRequest
* @param id
* String
* @return MenuRepository
* @todo Implement this
* com.cbright.cbrightframe.service.servicelayerinterface.INewscategoryService
* method
*/
public MenuRepository buildMenuRepositoryForCheck(
HttpServletRequest request, String id) {
List nodesCollection = this.getTree(id, new ArrayList());
return this.buildMenuRepositoryForCheck(request, nodesCollection);
}
public MenuRepository buildMenuRepositoryForCheck(
HttpServletRequest request, List nodesCollection) {
if(nodesCollection==null){
return null;
}
MenuRepository repository = new MenuRepository();
MenuRepository defaultRepository = (MenuRepository) request
.getSession().getServletContext().getAttribute(
MenuRepository.MENU_REPOSITORY_KEY);
repository.setDisplayers(defaultRepository.getDisplayers());
Iterator it = nodesCollection.iterator();
while (it.hasNext()) {
HashMap item = (HashMap) it.next();
MenuComponent mc = new MenuComponent();
mc.setName(item.get("name").toString());
mc.setTitle(item.get("name").toString());
if (item.get("tooltip") != null) {
mc.setToolTip(item.get("tooltip").toString());
} else {
mc.setToolTip(item.get("name").toString());
}
if (this.isHaveParentNode(item.get("id").toString(),
nodesCollection)) {
MenuComponent parentMenu = repository.getMenu(item.get(
"parentname").toString());
if (parentMenu == null) {
parentMenu = new MenuComponent();
HashMap parentNode = this.getNode(item.get("parentid")
.toString(), nodesCollection);
parentMenu.setName(parentNode.get("name").toString());
repository.addMenu(parentMenu);
}
mc.setParent(parentMenu);
}
if (!this.isHaveChildNode(item.get("id").toString(),
nodesCollection)) {
mc.setLocation("#");
}
repository.addMenu(mc);
}
return repository;
}
public MenuRepository buildMenuRepositoryForCheck(HttpServletRequest request) {
return this.buildMenuRepositoryForCheck(request, this
.transNewscategoryAfterGetFromDataBase());
}
public MenuRepository buildMenuRepositoryDisplay(HttpServletRequest reqest,
String id) {
List nodesCollection = this.getTree(id, new ArrayList());
return this.buildMenuRepositoryDisplay(reqest, nodesCollection);
}
public MenuRepository buildMenuRepositoryDisplay(
HttpServletRequest request, List nodesCollection) {
MenuRepository repository = new MenuRepository();
MenuRepository defaultRepository = (MenuRepository) request
.getSession().getServletContext().getAttribute(
MenuRepository.MENU_REPOSITORY_KEY);
repository.setDisplayers(defaultRepository.getDisplayers());
Iterator it = nodesCollection.iterator();
while (it.hasNext()) {
HashMap item = (HashMap) it.next();
MenuComponent mc = new MenuComponent();
mc.setName(item.get("name").toString());
mc.setTitle(item.get("name").toString());
if (item.get("parentname") != null) {
MenuComponent parentMenu = repository.getMenu(item.get(
"parentname").toString());
if (parentMenu == null) {
parentMenu = new MenuComponent();
parentMenu.setName(item.get("parentname").toString());
repository.addMenu(parentMenu);
}
mc.setParent(parentMenu);
}
if (!this.isHaveChildNode(item.get("id").toString(),
nodesCollection)) {
String currentUrl = request.getRequestURL().toString();
String subCurrentUrl = currentUrl.substring(0, currentUrl
.lastIndexOf("/") + 1);
StringBuffer url = new StringBuffer(subCurrentUrl);
url.append(item.get("location"));
url.append("&nodeID=" + item.get("id"));
String hql="from "+QueryObject.class.getName()+ " q where q.modulid=:modulid";
QueryObject q=null;
try {
List listObjects=this.getAllByHQL(hql, "modulid", item.get("id").toString());
if(listObjects!=null&&listObjects.size()>0){
q=(QueryObject)listObjects.iterator().next();
}
} catch (ServiceException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(q!=null){
url.append("&queryObjectIdentifer="+q.getId());
if(StringUtils.isNotEmpty(q.getFmPrjName())){
url.append("&prjName="+q.getFmPrjName());
}
}
if (item.get("target") != null) {
mc.setTarget(item.get("target").toString());
// url.append("&target="+item.get("target").toString());
}
if (item.get("onclick") != null) {
mc.setOnclick(item.get("onclick").toString());
}
mc.setLocation(url.toString());
}
repository.addMenu(mc);
}
return repository;
}
public MenuRepository buildMenuRepositoryDisplay(HttpServletRequest request) {
return this.buildMenuRepositoryDisplay(request, this
.transNewscategoryAfterGetFromDataBase());
}
/**
* 组织结构节点生成
*
* @param request
* HttpServletRequest
* @param nodesCollection
* List
* @return MenuRepository
*/
public MenuRepository buuilMenuRepositoryForGroupDisplay(
HttpServletRequest request, List nodesCollection) {
MenuRepository repository = new MenuRepository();
MenuRepository defaultRepository = (MenuRepository) request
.getSession().getServletContext().getAttribute(
MenuRepository.MENU_REPOSITORY_KEY);
repository.setDisplayers(defaultRepository.getDisplayers());
Iterator it = nodesCollection.iterator();
while (it.hasNext()) {
HashMap item = (HashMap) it.next();
MenuComponent mc = new MenuComponent();
mc.setName(item.get("name").toString());
mc.setTitle(item.get("name").toString());
if (item.get("parentname") != null) {
MenuComponent parentMenu = repository.getMenu(item.get(
"parentname").toString());
if (parentMenu == null) {
parentMenu = new MenuComponent();
parentMenu.setName(item.get("parentname").toString());
repository.addMenu(parentMenu);
}
mc.setParent(parentMenu);
}
StringBuffer url = null;
if (item.get("location") != null
&& !"".equals(item.get("location"))) {
String currentUrl = request.getRequestURL().toString();
String subCurrentUrl = currentUrl.substring(0, currentUrl
.lastIndexOf("/") + 1);
url = new StringBuffer(subCurrentUrl);
// url
// .append("newscategoryAction.do?method=categoryMaintenance&objectID="
// + item.get("id").toString());
url.append(item.get("location").toString());
url.append("&nodeID=" + item.get("id").toString());
}
if (item.containsKey("identifer")) {
if (item.get("identifer").toString()
.equals("parentNodeWithUrl")) {
url.append("\" target=\"" + item.get("target"));
}
}
if (url != null) {
mc.setLocation(url.toString());
}
if (item.get("target") != null) {
mc.setTarget(item.get("target").toString());
}
repository.addMenu(mc);
}
return repository;
}
/**
* 目录树节点维护
*/
public MenuRepository buuilMenuRepositoryForMaintenance(
HttpServletRequest request, List nodesCollection) {
MenuRepository repository = new MenuRepository();
MenuRepository defaultRepository = (MenuRepository) request
.getSession().getServletContext().getAttribute(
MenuRepository.MENU_REPOSITORY_KEY);
repository.setDisplayers(defaultRepository.getDisplayers());
Iterator it = nodesCollection.iterator();
while (it.hasNext()) {
HashMap item = (HashMap) it.next();
MenuComponent mc = new MenuComponent();
mc.setName(item.get("name").toString());
mc.setTitle(item.get("name").toString());
if (item.get("parentname") != null) {
MenuComponent parentMenu = repository.getMenu(item.get(
"parentname").toString());
if (parentMenu == null) {
parentMenu = new MenuComponent();
parentMenu.setName(item.get("parentname").toString());
repository.addMenu(parentMenu);
}
mc.setParent(parentMenu);
}
String currentUrl = request.getRequestURL().toString();
String subCurrentUrl = currentUrl.substring(0, currentUrl
.lastIndexOf("/") + 1);
StringBuffer url = new StringBuffer(subCurrentUrl);
url
.append("newscategoryAction.do?method=categoryMaintenance&objectID="
+ item.get("id").toString());
url.append("\" target=\"GroupMain");
// url.append(item.get("location").toString());
// url.append("&nodeID="+item.get("id").toString());
//
// if(item.containsKey("identifer")){
// if(item.get("identifer").toString().equals("parentNodeWithUrl")){
// url.append("\" target=\""+item.get("target"));
// }
// }
mc.setLocation(url.toString());
// if(item.get("target")!=null){
// mc.setTarget(item.get("target").toString());
// }
repository.addMenu(mc);
}
return repository;
}
/**
* getNewscategory
*
* @param id
* String
* @return Newscategory
* @todo Implement this
* com.cbright.cbrightframe.service.servicelayerinterface.INewscategoryService
* method
*/
public Newscategory getNewscategory(String id) {
return (Newscategory) DAO.loadObject(Newscategory.class, id);
}
/**
* getNewscategorys
*
* @return List
* @todo Implement this
* com.cbright.cbrightframe.service.servicelayerinterface.INewscategoryService
* method
*/
public List getNewscategorys() {
return DAO.getAll("getNewscategorys");
// return DAO.loadAllObjects(Newscategory.class);
}
/**
*
* @param id
* String
* @param result
* List
* @return List
* @todo Implement this
* com.cbright.cbrightframe.service.servicelayerinterface.INewscategoryService
* method
*/
public List getTree(String id, List result) {
return this.getTree(id, this.transNewscategoryAfterGetFromDataBase(),
result);
}
public List getTree(String id, List nodesCollection, List result) {
List directoryChildrenNodes = this.getDirectoryChildrenNodes(id,
nodesCollection);
Iterator it = directoryChildrenNodes.iterator();
HashMap currentNode = this.getNode(id, nodesCollection);
result.add(currentNode);
while (it.hasNext()) {
HashMap item = (HashMap) it.next();
if (this.isHaveChildNode(id, nodesCollection)) {
this
.getTree(item.get("id").toString(), nodesCollection,
result);
} else {
result.add(item);
}
}
return result;
}
/**
* ---------------------------------------------------------------
* 根据一给定的同级父节点 得到一组树
*
* @param parentNodeCollection
* List
* @param result
* List
* @return List
* ----------------------------------------------------------------
* @todo Implement this
* com.cbright.cbrightframe.service.servicelayerinterface.INewscategoryService
* method
*/
public List getTree(List parentNodeCollection, List result) {
return this.getTree(parentNodeCollection, this
.transNewscategoryAfterGetFromDataBase(), result);
}
public List getTree(List parentNodeCollection, List nodesCollection,
List result) {
Iterator it = parentNodeCollection.iterator();
while (it.hasNext()) {
HashMap item = (HashMap) it.next();
if (this
.isHaveChildNode(item.get("id").toString(), nodesCollection)) {
this
.getTree(item.get("id").toString(), nodesCollection,
result);
} else {
result.add(item);
}
}
return result;
}
/**
* --------------------------------------------------------
* 判断一个给定的节点,在一组给定的节点集合中,是否有子节点
*
* @param id
* String
* @param nodeCollection
* List
* @return boolean --------------------------------------------------------
* @todo Implement this
* com.cbright.cbrightframe.service.servicelayerinterface.INewscategoryService
* method
*/
public boolean isHaveChildNode(String id, List nodeCollection) {
boolean result = false;
Iterator it = nodeCollection.iterator();
while (it.hasNext()) {
HashMap item = (HashMap) it.next();
if (item.get("parentid").toString().equals(id)) {
result = true;
break;
}
}
return result;
}
/**
* -----------------------------------------------------
* 判断一个节点在给定的节点集合中是否有父节点。
*
* @param id
* String
* @param nodeCollection
* List
* @return boolean -------------------------------------------------------
* @todo Implement this
* com.cbright.cbrightframe.service.servicelayerinterface.INewscategoryService
* method
*/
public boolean isHaveParentNode(String id, List nodeCollection) {
boolean result = false;
HashMap node = this.getNode(id, nodeCollection);
Iterator it = nodeCollection.iterator();
while (it.hasNext()) {
HashMap item = (HashMap) it.next();
if (item.get("id").toString().equals(
node.get("parentid").toString())) {
result = true;
break;
}
}
return result;
}
/**
* removeNewscategory
*
* @param id
* String
* @todo Implement this
* com.cbright.cbrightframe.service.servicelayerinterface.INewscategoryService
* method
*/
public void removeNewscategory(String id) {
DAO.removeObject(Newscategory.class, id);
}
/**
* saveOrUpdateNewscategory
*
* @param newscategory
* Newscategory
* @param parentNewscategory
* Newscategory
* @todo Implement this
* com.cbright.cbrightframe.service.servicelayerinterface.INewscategoryService
* method
*/
public void saveOrUpdateNewscategory(Newscategory newscategory,
Newscategory parentNewscategory) {
newscategory.setParentNewscategory(parentNewscategory);
parentNewscategory.getChildNewscategory().add(newscategory);
DAO.saveOrUpdateObject(newscategory);
}
/**
* 基础数据处理
*
* @param newscategory
* Newscategory
* @todo Implement this
* com.cbright.cbrightframe.service.servicelayerinterface.INewscategoryService
* method
*/
public void saveOrUpdateNewscategory(Newscategory newscategory) {
DAO.saveOrUpdateObject(newscategory);
}
/**
*
* @return List
* @todo Implement this
* com.cbright.cbrightframe.service.servicelayerinterface.INewscategoryService
* method
*/
public List transNewscategoryAfterGetFromDataBase() {
List source = this.getNewscategorys();
List result = new ArrayList(source.size());
Iterator it = source.iterator();
while (it.hasNext()) {
Newscategory item = (Newscategory) it.next();
HashMap temp = this.transNewscaetgoryToMap(item);
result.add(temp);
}
return result;
}
public List transNewscategoryAfterGetFromDataBase(String queryName,
String[] keys) {
List items = DAO.getAll(queryName);
CollectionUtil cU = new CollectionUtil();
// List temp = cU.transCollection(items);
List result = cU.transCollection(items, keys);
return result;
}
/**
* -----------------------------------------------------------------------
* 将一组将要显示成树的节点集合 转制为可单击选择的集合。
*
* @param source
* List
* @return List
* -----------------------------------------------------------------------
* @todo Implement this
* com.cbright.cbrightframe.service.servicelayerinterface.INewscategoryService
* method
*/
public List transNodeCollectionForCheckListRepository(List source) {
List result = new ArrayList(source.size());
Iterator it = source.iterator();
while (it.hasNext()) {
HashMap item = (HashMap) it.next();
if (!this.isHaveChildNode(item.get("id").toString(), source)) {
String input = "<input type=\"radio\" name=\"categorywillBeCheck\" onClick=\"SubmitValue(\'"
+ item.get("id").toString()
+ "@#@"
+ item.get("name").toString()
+ "\')\""
+ " value=\""
+ item.get("id").toString() + "\"></input>";
item.put("tooltip", item.get("name").toString());
item.put("name", item.get("name").toString() + " " + input);
}
// }
result.add(item);
}
return result;
}
/**
* 将一组将要显示成树的节点集合,转化为可授权的树节点集合。
*
* @param source
* @return
*/
public List transNodesCollectionForPermissionRepository(List source) {
// TODO Auto-generated method stub
List result = new ArrayList(source.size());
Iterator it = source.iterator();
List privileges = null;
try {
privileges = super.loadAllObjects(Privilege.class);
} catch (ServiceException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
while (it.hasNext()) {
HashMap item = (HashMap) it.next();
if (!this.isHaveChildNode(item.get("id").toString(), source)) {
StringBuffer sb = new StringBuffer();
sb.append("<select id=privilege name=privilegeid>");
sb.append("<option value=0>---请选择将要授于的权限---</option>");
sb.append("<option value="
+ StringPool.REMOVE_ALL_PRIVILEGES_ON_MODEL + "@#@"
+ item.get("id").toString()
+ ">清除本角色在改模块的所有权限</option>");
Iterator pIte = privileges.iterator();
while (pIte.hasNext()) {
Privilege pItem = (Privilege) pIte.next();
sb.append("<option value=" + pItem.getOid() + "@#@"
+ item.get("id").toString() + ">" + pItem.getName()
+ "</option>");
}
sb.append("</select>");
item.put("name", item.get("name").toString() + " "
+ sb.toString());
}
// }
result.add(item);
}
return result;
}
/**
*
* @param source
* @param permissionModules,已授权节点id集合
* @return
*/
private boolean isBePermissioned(List permissionedModules,
String currentModuleid, String currentPrivilegeid) {
boolean result = false;
Iterator pIt = permissionedModules.iterator();
while (pIt.hasNext()) {
// List pItem=(List) pIt.next();
HashMap pItem = (HashMap) pIt.next();
if (currentModuleid.equals(pItem.get("modelid").toString())
&& currentPrivilegeid.equals(pItem.get("privilegeid")
.toString())) {
result = true;
break;
}
}
return result;
}
/**
* 取得一个角色可以访问的模块集合。
*
* @param permissionedModules(已授权节点id集合)
*/
public List transNodesCollectionForPermissionRepository(List source,
List permissionedModules) {
// TODO Auto-generated method stub
List result = new ArrayList(source.size());
Iterator it = source.iterator();
List privileges = null;
try {
privileges = super.loadAllObjects(Privilege.class);
} catch (ServiceException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
while (it.hasNext()) {
HashMap item = (HashMap) it.next();
String currentModuleid = item.get("id").toString();
if (!this.isHaveChildNode(item.get("id").toString(), source)) {
StringBuffer sb = new StringBuffer();
sb.append("<select id=\"privilege\" name=\"privilegeid\">");
sb.append("<option value=\"0\">---请选择将要授于的权限---</option>");
sb.append("<option value=\""
+ StringPool.REMOVE_ALL_PRIVILEGES_ON_MODEL + "@#@"
+ item.get("id").toString()
+ "\">清除本角色在改模块的所有权限</option>");
Iterator pIte = privileges.iterator();
while (pIte.hasNext()) {
Privilege pItem = (Privilege) pIte.next();
if (this.isBePermissioned(permissionedModules,
currentModuleid, pItem.getOid())) {
sb.append("<option value=\"" + pItem.getOid() + "@#@"
+ item.get("id").toString() + "\" selected>"
+ pItem.getName() + "</option>");
} else {
sb.append("<option value=\"" + pItem.getOid() + "@#@"
+ item.get("id").toString() + "\">"
+ pItem.getName() + "</option>");
}
}
sb.append("</select>");
item.put("tooltip", item.get("name"));
item.put("name", item.get("name").toString() + " "
+ sb.toString());
System.out.println(item.get("name").toString());
}
// }
result.add(item);
}
return result;
}
public HashMap transNewscaetgoryToMap(Newscategory n) {
if (n == null) {
return null;
}
HashMap result = new HashMap();
result.put("id", n.getId());
result.put("name", n.getName());
result.put("description", n.getDescription());
result.put("action", n.getAction());
result.put("altImage", n.getAltImage());
result.put("froward", n.getForward());
result.put("height", n.getHeight());
result.put("image", n.getImage());
result.put("location", n.getLocation());
result.put("onclick", n.getOnclick());
result.put("onmouseout", n.getOnmouseout());
result.put("onmouseover", n.getOnmouseover());
result.put("page", n.getPage());
result.put("roles", n.getRoles());
result.put("target", n.getTarget());
result.put("tooltip", n.getTooltip());
result.put("width", n.getWidth());
if (n.getIsBeDeleted()) {
result.put("isBeDeleted", "1");
} else {
result.put("isBeDeleted", "0");
}
if (n.getParentNewscategory() == null) {
result.put("parentid", "0");
result.put("parentname", null);
} else {
result.put("parentid", n.getParentNewscategory().getId());
result.put("parentname", n.getParentNewscategory().getName());
}
return result;
}
public HashMap getNode(String id) {
return this.getNode(id, this.transNewscategoryAfterGetFromDataBase());
}
public HashMap getNode(String id, List nodesCollection) {
Iterator it = nodesCollection.iterator();
HashMap result = new HashMap();
while (it.hasNext()) {
HashMap item = (HashMap) it.next();
if (item.get("id").toString().equals(id)) {
result = item;
break;
}
}
return result;
}
public List getDirectoryChildrenNodes(String id) {
return this.getDirectoryChildrenNodes(id, this
.transNewscategoryAfterGetFromDataBase());
}
public List getDirectoryChildrenNodes(String id, List nodesCollection) {
List result = new ArrayList();
Iterator it = nodesCollection.iterator();
while (it.hasNext()) {
HashMap item = (HashMap) it.next();
if (item.get("parentid").toString().equals(id)) {
result.add(item);
}
}
return result;
}
public List getRoots() {
return getRoots(this.transNewscategoryAfterGetFromDataBase());
}
public List getRoots(List nodesCollection) {
if (nodesCollection == null) {
return null;
}
List result = new ArrayList();
Iterator it = nodesCollection.iterator();
while (it.hasNext()) {
HashMap item = (HashMap) it.next();
if (!this.isHaveParentNode(item.get("id").toString(),
nodesCollection)) {
result.add(item);
}
}
return result;
}
public HashMap buildTreeForDisplay(String queryName, String[] keys,
HttpServletRequest request) {
HashMap result = new HashMap();
List items = this
.transNewscategoryAfterGetFromDataBase(queryName, keys);
List menuItems = this.transItems(items);
List parentItems = this.getRoots(menuItems);
MenuRepository mr = this.buildMenuRepositoryDisplay(request, menuItems);
HashMap lineTree = this.getTree(menuItems);
result.put(StringPool.PARENT_NODES, parentItems);
result.put(StringPool.MENU_REPOSITORY, mr);
result.put(StringPool.LINE_TREE, lineTree);
result.put(StringPool.TREE_ITEMS, menuItems);
return result;
}
public HashMap buildTreeForCheck(String queryName, String[] keys,
HttpServletRequest request) {
// TODO Auto-generated method stub
HashMap result = new HashMap();
List items = this
.transNewscategoryAfterGetFromDataBase(queryName, keys);
List transRootNodesCollection = this.transItems(items);
List transedItems = this
.transNodeCollectionForCheckListRepository(transRootNodesCollection);
List parentItems = this.getRoots(transedItems);
MenuRepository mr = this.buildMenuRepositoryForCheck(request,
transedItems);
result.put(StringPool.DATA_DICTIONARY_TREE_PARENT_NODES, parentItems);
result.put(StringPool.DATA_DICTIONARY_MENU_REPOSITORY, mr);
return result;
}
public HashMap transItem(HashMap source, List nodesCollection) {
// TODO Auto-generated method stub
String id = source.get("id").toString();
if (this.isHaveParentNode(id, nodesCollection)) {
String parentid = source.get("parentid").toString();
HashMap parentNode = this.getNode(parentid, nodesCollection);
source.put("parentname", parentNode.get("name").toString());
} else {
source.put("parentname", null);
}
return source;
}
public List transItems(List nodesCollection) {
// TODO Auto-generated method stub
if (nodesCollection == null || nodesCollection.size() < 1) {
return null;
}
List result = new ArrayList();
List transedRootsNodesCollection = this
.transRootsItems(nodesCollection);
Iterator it = transedRootsNodesCollection.iterator();
while (it.hasNext()) {
HashMap item = (HashMap) it.next();
HashMap temp = this.transItem(item, transedRootsNodesCollection);
result.add(temp);
}
return result;
}
public List transRootsItems(List nodesCollection) {
// TODO Auto-generated method stub
Iterator it = nodesCollection.iterator();
if (nodesCollection == null || nodesCollection.size() < 1) {
return null;
}
List result = new ArrayList();
while (it.hasNext()) {
HashMap item = (HashMap) it.next();
if (item.get("parentid") == null) {
item.put("parentid", new Long(0));
}
result.add(item);
}
return result;
}
public List getLineTree(String id, List nodesCollection, List result) {
Iterator it = nodesCollection.iterator();
HashMap currentNode = this.getNode(id, nodesCollection);
if (currentNode != null && currentNode.size() > 0) {
result.add(currentNode);
}
// while (it.hasNext()) {
// HashMap item = (HashMap) it.next();
if (this.isHaveParentNode(currentNode.get("id").toString(),
nodesCollection)) {
this.getLineTree(currentNode.get("parentid").toString(),
nodesCollection, result);
}
// }
List willReturn = new ArrayList();
if (result != null && result.size() > 0) {
for (int i = result.size(); i > 0; i--) {
willReturn.add(result.get(i - 1));
}
}
return result;
}
public HashMap getTree(List nodesCollection) {
if (nodesCollection == null || nodesCollection.size() < 1) {
return null;
}
HashMap result = new HashMap();
List allLastNodes = this.getAllLastNode(nodesCollection);
Iterator it = allLastNodes.iterator();
while (it.hasNext()) {
HashMap temp = (HashMap) it.next();
List item = this.getLineTree(temp.get("id").toString(),
nodesCollection, new ArrayList());
result.put(temp.get("id").toString(), item);
}
return result;
}
private List getAllLastNode(List nodesCollection) {
if (nodesCollection == null || nodesCollection.size() < 1) {
return null;
}
List result = new ArrayList();
Iterator it = nodesCollection.iterator();
while (it.hasNext()) {
HashMap item = (HashMap) it.next();
if (!this.isHaveChildNode(item.get("id").toString(),
nodesCollection)) {
result.add(item);
}
}
return result;
}
public HashMap buileTreeForMaintenance(String queryName, String[] keys,
HttpServletRequest request) {
List nodesCollection = this.transNewscategoryAfterGetFromDataBase(
queryName, keys);
List menuItems = this.transItems(nodesCollection);
List roots = this.getRoots(menuItems);
MenuRepository mr = this.buuilMenuRepositoryForMaintenance(request,
menuItems);
HashMap result = new HashMap();
result.put(StringPool.CATEGORY_MAINTENANCE_ROOTS, roots);
result.put(StringPool.CATEGORY_MAINTENANCE_MENUREPOSITORY, mr);
return result;
}
public List getParentNodes(List nodesCollecitons, String nodeid, List result) {
// TODO Auto-generated method stub
HashMap currentNode = this.getNode(nodeid, nodesCollecitons);
result.add(currentNode);
Iterator it = nodesCollecitons.iterator();
// while(it.hasNext()){
// HashMap item=(HashMap) it.next();
if (this.isHaveParentNode(currentNode.get("id").toString(),
nodesCollecitons)) {
this.getParentNodes(nodesCollecitons, currentNode.get("parentid")
.toString(), result);
}
// }
return result;
}
/**
* 根据给定的一组节点ID(这组节点为树的最末一级节点,取得这些结点的所有父节点,用户构造个性化用户树使用。
* 在这些节点中,不但包含当前用户节点,同时,也包含当前用户节点的上一级节点。 这些给定的最末一级ID,是根据当前用户信息取得的,所以
* 在本处得到的LIST,都是用户有权访问的节点组合。
*
* @param nodesID
* @param result
* @return
*/
public List getParentNodes(List nodesCollection, String[] nodesID) {
// TODO Auto-generated method stub
int length = nodesID.length;
List result = new ArrayList();
for (int i = 0; i < length; i++) {
List temp = this.getParentNodes(nodesCollection, nodesID[i],
new ArrayList());
Iterator it = temp.iterator();
while (it.hasNext()) {
HashMap item = (HashMap) it.next();
if (!this.isContainsInList(result, item)) {
result.add(item);
}
}
}
return result;
}
/**
* 在存入节点之前做判断,判断节点是否已经被存入结果集中。
*
* @param source
* @param item
* @return
*/
private boolean isContainsInList(List source, HashMap item) {
boolean result = false;
String id = item.get("id").toString();
Iterator it = source.iterator();
while (it.hasNext()) {
HashMap temp = (HashMap) it.next();
if (temp.get("id").toString().equals(id)) {
result = true;
break;
}
}
return result;
}
private boolean isContainsInList(List source, String nodeid) {
boolean result = false;
Iterator it = source.iterator();
while (it.hasNext()) {
HashMap item = (HashMap) it.next();
if (item.get("modelid").toString().equals(nodeid)) {
result = true;
break;
}
}
return result;
}
/**
* 将权限信息组合入每个节点 如果为最后一级节点,则直接赋予其对应权限,权限的编号从1-8
* 如果不是最后一级节点,则认为其具有访问节点的权限,统一定义为0. 同是,权限key为privilegeid
*/
public List prepareTreeNodesWithPermission(List nodesCollection,
List lastModelWithPermission) {
// TODO Auto-generated method stu
List result = new ArrayList();
Iterator it = nodesCollection.iterator();
while (it.hasNext()) {
HashMap item = (HashMap) it.next();
if (this.isContainsInList(lastModelWithPermission, item.get("id")
.toString())) {
HashMap permissionNode = this.getModelWithPermission(
lastModelWithPermission, item.get("id").toString());
item.put("privilegeid", permissionNode.get("privilegeid"));
} else {
item.put("privilegeid", "0");
}
result.add(item);
}
return result;
}
/**
* 根据最后一级模块ID取得模块信息,本信息中已包含授权信息
*
* @param lastModelWithPermission
* @param nodeid
* @return
*/
private HashMap getModelWithPermission(List lastModelWithPermission,
String nodeid) {
HashMap result = new HashMap();
Iterator it = lastModelWithPermission.iterator();
while (it.hasNext()) {
HashMap item = (HashMap) it.next();
if (item.get("modelid").toString().equals(nodeid)) {
result = item;
break;
}
}
return result;
}
public List getAllLastNodes() {
// TODO Auto-generated method stub
List source = this.transNewscategoryAfterGetFromDataBase();
List result = new ArrayList();
Iterator it = source.iterator();
while (it.hasNext()) {
HashMap item = (HashMap) it.next();
if (!this.isHaveChildNode(item.get("id").toString(), source)) {
result.add(item);
}
}
return result;
}
public MenuRepository buildMenuRepositoryForMaintenance(
HttpServletRequest request, List nodesCollection, String editUrl,
String target) {
// TODO Auto-generated method stub
MenuRepository repository = new MenuRepository();
MenuRepository defaultRepository = (MenuRepository) request
.getSession().getServletContext().getAttribute(
MenuRepository.MENU_REPOSITORY_KEY);
repository.setDisplayers(defaultRepository.getDisplayers());
Iterator it = nodesCollection.iterator();
while (it.hasNext()) {
HashMap item = (HashMap) it.next();
MenuComponent mc = new MenuComponent();
mc.setName(item.get("name").toString());
mc.setTitle(item.get("name").toString());
if (item.get("parentname") != null) {
MenuComponent parentMenu = repository.getMenu(item.get(
"parentname").toString());
if (parentMenu == null) {
parentMenu = new MenuComponent();
parentMenu.setName(item.get("parentname").toString());
repository.addMenu(parentMenu);
}
mc.setParent(parentMenu);
}
String currentUrl = request.getRequestURL().toString();
String subCurrentUrl = currentUrl.substring(0, currentUrl
.lastIndexOf("/") + 1);
StringBuffer url = new StringBuffer(subCurrentUrl);
url.append(editUrl + item.get("id").toString());
url.append("\" target=\"" + target);
// url.append(item.get("location").toString());
// url.append("&nodeID="+item.get("id").toString());
//
// if(item.containsKey("identifer")){
// if(item.get("identifer").toString().equals("parentNodeWithUrl")){
// url.append("\" target=\""+item.get("target"));
// }
// }
mc.setLocation(url.toString());
// if(item.get("target")!=null){
// mc.setTarget(item.get("target").toString());
// }
repository.addMenu(mc);
}
return repository;
}
public HashMap buildTreeForMaintenance(String queryName, String[] keys,
HttpServletRequest request, String editUrl, String target) {
// TODO Auto-generated method stub
List nodesCollection = this.transNewscategoryAfterGetFromDataBase(
queryName, keys);
List menuItems = this.transItems(nodesCollection);
List roots = this.getRoots(menuItems);
MenuRepository mr = this.buildMenuRepositoryForMaintenance(request,
menuItems, editUrl, target);
HashMap result = new HashMap();
result.put(StringPool.CATEGORY_MAINTENANCE_ROOTS, roots);
result.put(StringPool.CATEGORY_MAINTENANCE_MENUREPOSITORY, mr);
return result;
}
public MenuRepository buildMenuRepositoryForMultiCheckBox(
HttpServletRequest request, List nodesCollection) {
// TODO Auto-generated method stub
MenuRepository repository = new MenuRepository();
MenuRepository defaultRepository = (MenuRepository) request
.getSession().getServletContext().getAttribute(
MenuRepository.MENU_REPOSITORY_KEY);
repository.setDisplayers(defaultRepository.getDisplayers());
Iterator it = nodesCollection.iterator();
while (it.hasNext()) {
HashMap item = (HashMap) it.next();
MenuComponent mc = new MenuComponent();
mc.setName(item.get("name").toString());
mc.setTitle(item.get("name").toString());
if (item.get("tooltip") != null) {
mc.setToolTip(item.get("tooltip").toString());
} else {
mc.setToolTip(item.get("name").toString());
}
if (this.isHaveParentNode(item.get("id").toString(),
nodesCollection)) {
MenuComponent parentMenu = repository.getMenu(item.get(
"parentname").toString());
if (parentMenu == null) {
parentMenu = new MenuComponent();
HashMap parentNode = this.getNode(item.get("parentid")
.toString(), nodesCollection);
parentMenu.setName(parentNode.get("name").toString());
repository.addMenu(parentMenu);
}
mc.setParent(parentMenu);
}
if (!this.isHaveChildNode(item.get("id").toString(),
nodesCollection)) {
mc.setLocation("#");
}
repository.addMenu(mc);
}
return repository;
}
public List transNodesCollecitonForMultiCheckBox(List nodesCollection) {
// TODO Auto-generated method stub
List result = new ArrayList(nodesCollection.size());
Iterator it = nodesCollection.iterator();
while (it.hasNext()) {
HashMap item = (HashMap) it.next();
if (!this.isHaveChildNode(item.get("id").toString(),
nodesCollection)) {
String input = "<input type=\"checkBox\" name=\"nodesId\""
+ " value=\"" + item.get("id").toString()
+ "\"></input>";
item.put("tooltip", item.get("name").toString());
item.put("name", item.get("name").toString() + " " + input);
}
// }
result.add(item);
}
return result;
}
public List transGroupAndRoleCollectionForMultiCheckBox(
List nodesCollection, String currentUserId) {
List result = new ArrayList(nodesCollection.size());
try {
String sql = "select r.role_id from "
+ StringPool.PRIVILEGE_TABLE_SPACE
+ ".sys_user_role r where r.user_id=\'" + currentUserId
+ "\'";
List models = this.getJdbcQueryUtil().getAll(sql);
Iterator it = nodesCollection.iterator();
boolean flag = false;
while (it.hasNext()) {
HashMap item = (HashMap) it.next();
Iterator iIt = models.iterator();
while (iIt.hasNext()) {
HashMap ite = (HashMap) iIt.next();
String tempId = ite.get("role_id").toString();
if (tempId.equals(item.get("id").toString())) {
flag = true;
break;
}
}
if (!this.isHaveChildNode(item.get("id").toString(),
nodesCollection)) {
if (flag) {
String input = "<input type=\"checkBox\" checked name=\"nodesId\""
+ " value=\""
+ item.get("id").toString()
+ "\"></input>";
item.put("tooltip", item.get("name").toString());
item.put("name", item.get("name").toString() + " "
+ input);
} else {
String input = "<input type=\"checkBox\" name=\"nodesId\""
+ " value=\""
+ item.get("id").toString()
+ "\"></input>";
item.put("tooltip", item.get("name").toString());
item.put("name", item.get("name").toString() + " "
+ input);
}
}
if (flag) {
flag = false;
}
// }
result.add(item);
}
// select t.modelid from role_model_privilege t,sys_user_role u
// where t.roleid=u.role_id and
// u.user_id='402880be118bd18301118c009fa70002'
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// TODO Auto-generated method stub
return result;
}
public List transStruntsMenuNodes2DTree(List source) {
// TODO Auto-generated method stub
List moreKey=this.addKey2Map(source);
List result=new ArrayList();
Iterator it=moreKey.iterator();
while(it.hasNext()){
HashMap item=(HashMap) it.next();
if(this.isHaveParentNode(item.get("id").toString(), source)){
HashMap parentNode=this.getNode(item.get("parentid").toString(), source);
item.put("dTree_parentid", parentNode.get("dTree_id"));
}else{
item.put("dTree_parentid", new Integer(-1));
}
result.add(item);
}
return result;
}
private List addKey2Map(List source){
Iterator it=source.iterator();
List result=new ArrayList();
int temp=0;
while(it.hasNext()){
HashMap item=(HashMap) it.next();
item.put("dTree_id", new Integer(temp));
temp++;
result.add(item);
}
return result;
}
}
4.下面是一个在Aciton中构造单选树形结构的例子,很简单,只用调用上面的业务方法就可以了. public ActionForward radioTree(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response){
String queryName=getHttp().getParameter(request, StringPool.QUERYNAME);
// String nameAndId=getHttp().getParameter(request, StringPool.DATA_TREE_NAME_ID);
Map treeMap=super.getService().getNewscategoryService().buildTreeForCheck(queryName, new String[]{"id","name","parentid"}, request);
super.getHttp().setRequestAttribute(request, "root", treeMap.get(StringPool.DATA_DICTIONARY_TREE_PARENT_NODES));
super.getHttp().setRequestAttribute(request, "trees", treeMap.get(StringPool.DATA_DICTIONARY_MENU_REPOSITORY));
return mapping.findForward("radioTree");
}
5.树形结构在页面上的显示. 在上面的Action方法中,我们已经将生成树所需要的数据放入了request中,那么在页面上,我们只需要按照Struts-menu官方文档中的说明进行操作就可以了. 以上就是使用Struts-menu根据数据库生成各种树形结构的主要代码,个人感觉Struts-menu在速度上面,优秀不是很大,但我们可以做其它种种操作来帮助它,比如我们可以将数据库中的数据一次性全部加载到内存中等.但是Strut-menu的database menu的扩展性,确实很不错,很多关于树的功能扩展,作者早已设计到了数据库中,比如更换节点的图片,更换节点的点击事件等,这些都可以在后台数据库中进行修改,而不需要改变我们的程序及网页. 大家一块试试吧,真的很不错哦 声明:JavaEye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
|
|
| 返回顶楼 | |
|
时间:2008-03-11
很赞!谢谢:)
|
|
| 返回顶楼 | |
|
时间:2008-04-18
你现在在用什么替代 struts menu
|
|
| 返回顶楼 | |
|
时间:2008-04-18
lz很奉献,表示支持;
不过看起来很复杂啊写的,我找到一个相对简单的xytree, 只是扩展性不如strutsMenu,自己修改变更比较麻烦 |
|
| 返回顶楼 | |






