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

📄 usergroupservice.java

📁 通过面向对象的对象-关系映射持久化技术
💻 JAVA
字号:
package com.faw_qm.ipa.service;

import java.util.*;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.faw_qm.ipa.bo.*;
import com.faw_qm.ipa.frame.util.Util;

import javax.servlet.jsp.*;
import org.hibernate.*;

public class UserGroupService extends HibernateDAO {

	private static Log log = LogFactory.getLog(UserGroupService.class);
	
	/**
	 * 20080514
	 * @param name
	 * @return
	 * @throws Exception
	 */
    public List findUserGroupsByName(String name) throws Exception {
    	if(!Util.NVL(name))
    		return getObjects("from UserGroup i where i.name like '%"+name+"%' order by i.id desc");
    	else
    		//return getObjects("from Item i where  i.parent.id='-1' order by i.id desc");
    		return getObjects("from UserGroup i order by i.id desc");
    }
    
    /**
     * 20080514
     * @param name
     * @return
     */
    public UserGroup getUserGroupByName(String name) throws Exception {
    	UserGroup ug = null;
    	Object o = getObject("from UserGroup i where i.name = '"+name+"'");
    	log.info(o);
    	log.info(o==null);
    	if(o!=null)
    		ug = (UserGroup)o;
    	return ug;
    }
    
    /**
     * 20080516
     * @param root_id
     * @param ug_id
     * @return
     * @throws Exception
     */
    public List findItemsOwnedByParentAndUser_Group(String root_id,String ug_id) throws Exception {
		List result = null;
        Session session = HibernateUtil.getSession();
        Transaction tx = null;
        String sql = "select {i.*} from items i where i.root_id="+root_id;
        sql += " and i.id in (select item_id from ug_item where ug_id="+ug_id+") and i.id<>"+root_id;
        sql += " order by i.parent_id,i.sequence desc";
        log.info(sql);
        try {
            tx = session.beginTransaction();			
    		result = session.createSQLQuery(sql).addEntity("i",Item.class).list();
            tx.commit();
        } catch (Exception e) {
        	log.error(e);
            if (tx != null) {
                // Something went wrong; discard all partial changes
                tx.rollback();
            }
            throw e;
        } finally {
            // No matter what, close the session
            HibernateUtil.closeSession(session);            
        }   
        return result;
    }
    
    /**
     * 20080517
     * 建立用户组与当前节点及其子节点的关联关系
     * @param ug_id
     * @param item_id
     * @throws Exception
     */
    public void associateUserGroupAndItem(String ug_id,String item_id) throws Exception {
        Session session = HibernateUtil.getSession();
        Transaction tx = null;
        try {
            tx = session.beginTransaction();
            UserGroup ug = (UserGroup)session.load(UserGroup.class, Long.parseLong(ug_id));
    		Item item = (Item)session.load(Item.class, Long.parseLong(item_id));
    		log.info(ug.getName()+"--"+item.getName());
    		
    		List childItems = new ArrayList();
    		navigateAllChild(item,childItems);
    		
    		for (Iterator it = childItems.iterator();it.hasNext();) {
    			Item childItem = (Item)it.next();
        	  	ug.getItems().add(childItem);
        	  	childItem.getUsergroups().add(ug);   			
    		}

            tx.commit();
        } catch (Exception e) {
        	log.error(e);
            if (tx != null) {
                // Something went wrong; discard all partial changes
                tx.rollback();
            }
            throw e;
        } finally {
            // No matter what, close the session
            HibernateUtil.closeSession(session);            
        }   
    }

    /**
     * 20090517
     * 解除用户组与当前节点及其子节点的关联关系
     * @param ug_id
     * @param item_id
     * @throws Exception
     */	
    public void removeUserGroupAndItem(String ug_id,String item_id) throws Exception {
        Session session = HibernateUtil.getSession();
        Transaction tx = null;
        try {
            tx = session.beginTransaction();
            UserGroup ug = (UserGroup)session.load(UserGroup.class, Long.parseLong(ug_id));
    		Item item = (Item)session.load(Item.class, Long.parseLong(item_id));
    		log.info(ug.getName()+"--"+item.getName());

    		List childItems = new ArrayList();
    		navigateAllChild(item,childItems);
    		
    		for (Iterator it = childItems.iterator();it.hasNext();) {
    			Item childItem = (Item)it.next();
        	  	ug.getItems().remove(childItem);
        	  	childItem.getUsergroups().remove(ug);   			
    		}    		
    	  	
            tx.commit();
        } catch (Exception e) {
        	log.error(e);
            if (tx != null) {
                // Something went wrong; discard all partial changes
                tx.rollback();
            }
            throw e;
        } finally {
            // No matter what, close the session
            HibernateUtil.closeSession(session);            
        }   
    } 
    
    /**
     * 20080518 
     * @param out
     * @param items
     * @throws Exception
     */
    public void displayItemsOwnedByUserGroupInWeb(JspWriter out,Collection items)throws Exception {
    	StringBuffer sb = new StringBuffer();
    	for(Iterator it = items.iterator();it.hasNext();) {
    		Item item = (Item)it.next();
    		sb.append(item.getName()+",");
    	}
    	//为了防止没有具体菜单的应用出现的String index out of range: -1错误
    	//20080521
    	if (sb.length()>0)
    		out.println(sb.toString().substring(0,sb.toString().length()-1));
    }
    
    /**
     * 20080523
     * 建立用户组和当前用户的关联关系
     * @param ug_id
     * @param ui_id
     * @throws Exception
     */
    public void associateUserGroupAndUserInfo(String ug_id,String ui_name) throws Exception {
        Session session = HibernateUtil.getSession();
        Transaction tx = null;
        try {
            tx = session.beginTransaction();
            UserGroup ug = (UserGroup)session.load(UserGroup.class, Long.parseLong(ug_id));
            UserInfo ui = new UserInfo();
            
            //从一个已经存在用户记录上进行关联
            List list = session.createQuery("from UserInfo ui where ui.name='"+ui_name+"'").list();
            if (list!=null&&list.size()>0) {
            	ui = (UserInfo)list.get(0);
				if(!Hibernate.isInitialized(ui.getUsergroups())) 
					Hibernate.initialize(ui.getUsergroups());   
            }
            //从一个不存在的用户记录上进行关联
            ui.setName(ui_name);
    		log.info(ug.getName()+"--"+ui.getName());
    		
			ug.getUserinfos().add(ui);
			ui.getUsergroups().add(ug);

            tx.commit();
        } catch (Exception e) {
        	log.error(e);
            if (tx != null) {
                // Something went wrong; discard all partial changes
                tx.rollback();
            }
            throw e;
        } finally {
            // No matter what, close the session
            HibernateUtil.closeSession(session);            
        }   
    }

    /**
     * 20090523
     * 解除用户组与当前用户的关联关系
     * @param ug_id
     * @param ui_id
     * @throws Exception
     */	
    public void removeUserGroupAndUserInfo(String ug_id,String ui_id) throws Exception {
        Session session = HibernateUtil.getSession();
        Transaction tx = null;
        try {
            tx = session.beginTransaction();
            log.info("ug_id--"+ug_id);
            log.info("ui_id--"+ui_id);
            UserGroup ug = (UserGroup)session.load(UserGroup.class, Long.parseLong(ug_id));
            UserInfo ui = (UserInfo)session.load(UserInfo.class, Long.parseLong(ui_id));

//            UserInfo ui = (UserInfo)session.createQuery
//            	("from UserInfo ui where ui.name='"+ui_name+"'").
//            	setMaxResults(1).uniqueResult();
//			if(!Hibernate.isInitialized(ui.getUsergroups())) 
//				Hibernate.initialize(ui.getUsergroups());            
    		log.info(ug.getName()+"--"+ui.getName());
    		
    		log.info(ug.getUserinfos().contains(ui));
    		log.info(ui.getUsergroups().contains(ug));
			ug.getUserinfos().remove(ui);
			ui.getUsergroups().remove(ug); 
    	  	
            tx.commit();
        } catch (Exception e) {
        	log.error(e);
            if (tx != null) {
                // Something went wrong; discard all partial changes
                tx.rollback();
            }
            throw e;
        } finally {
            // No matter what, close the session
            HibernateUtil.closeSession(session);            
        }   
    } 
    
//    /**
//     * 2008029
//     * @param name
//     * @return
//     * @throws Exception
//     */
//    public List findUserInfosByName(String name) throws Exception {
//    	if(!Util.NVL(name))
//    		return getObjects("from UserInfo i where i.name = '"+name+"' order by i.id desc");
//    	else
//    		//return getObjects("from Item i where  i.parent.id='-1' order by i.id desc");
//    		return getObjects("from UserInfo i order by i.id desc");
//    }
    
    /**
     * 20080529
     * @param name
     * @return
     * @throws Exception
     */
    public List getItemsFromUser(String username,String item_parameter) throws Exception {
    	List uis_ugs_items = new ArrayList();
    	List current_item_list = new ArrayList();
    	if(Util.NVL(username)||Util.NVL(item_parameter))
    		return null;
    	else {
            Session session = HibernateUtil.getSession();
            Transaction tx = null;
            try {
                tx = session.beginTransaction();
                UserInfo ui = new UserInfo();
                Set uis_ugs = new HashSet();
                
                List ui_list = session.createQuery("from UserInfo ui where ui.name='"+username+"'").list();
                if (ui_list!=null&&ui_list.size()>0) {
                	Iterator it = ui_list.iterator();
                	while(it.hasNext()) {
	                	ui = (UserInfo)it.next();
	    				if(!Hibernate.isInitialized(ui.getUsergroups())) 
	    					Hibernate.initialize(ui.getUsergroups()); 
	    				//
	    				for (Iterator it_ = ui.getUsergroups().iterator();it_.hasNext();) {
	    					uis_ugs.add(it_.next());
	    				}
                	}
                }
                
                for (Iterator it = uis_ugs.iterator();it.hasNext();) {
                	UserGroup ugs = (UserGroup)it.next();
                	
                	for (Iterator it_ = ugs.getItems().iterator();it_.hasNext();) {
                		uis_ugs_items.add(it_.next());
                	}
                } 
                
                //这个集合是按照菜单序号排序的
                current_item_list = session.createQuery(
                		"from Item i where i.root_id=" +
                		"(select id from Item i_ where i_.parameter='"+item_parameter+"') order by i.sequence").list();
                //求交集。下面两种求交集的方式排序不同,所以要慎重采纳。20080530。 
                //uis_ugs_items.retainAll(current_item_list);
                current_item_list.retainAll(uis_ugs_items);
                tx.commit();
            } catch (Exception e) {
            	log.error(e);
                if (tx != null) {
                    // Something went wrong; discard all partial changes
                    tx.rollback();
                }
                throw e;
            } finally {
                // No matter what, close the session
                HibernateUtil.closeSession(session); 
            }  
            return current_item_list;
    	}
    }   
    
    //update ug_item  set ug_item.sequence=items.sequence from items where ug_item.item_id=items.id
    
    public static void main(String args[]) {
    	UserGroupService ugs = new UserGroupService();
    	try {
    		Iterator it = ugs.getItemsFromUser("sa220011","SAPAfterSales").iterator();
    		//from ProductCategory p left join fetch p.parentCategory
			Item root_item = (Item)ugs.getObjects(
					"from Item i left join fetch i.parent where i.parameter='SAPAfterSales'").iterator().next();

//    		for (;it.hasNext();) {
//    			Item i = (Item)it.next();
//    			log.info(i.getName());
//    		} 
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }
     
}

⌨️ 快捷键说明

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