com.ctvit.vdp.services.sysconfiguration.user.UserService.java Source code

Java tutorial

Introduction

Here is the source code for com.ctvit.vdp.services.sysconfiguration.user.UserService.java

Source

package com.ctvit.vdp.services.sysconfiguration.user;

import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;

import com.ctvit.framework.common.Constants;
import com.ctvit.framework.services.BaseServices;
import com.ctvit.vdp.dao.OrganizationMapper;
import com.ctvit.vdp.dao.RoleMapper;
import com.ctvit.vdp.dao.SortTreeMapper;
import com.ctvit.vdp.dao.SystemConfigMapper;
import com.ctvit.vdp.dao.UserMapper;
import com.ctvit.vdp.dao.UserRolesMapper;
import com.ctvit.vdp.entity.Organization;
import com.ctvit.vdp.entity.OrganizationExample;
import com.ctvit.vdp.entity.Role;
import com.ctvit.vdp.entity.SortTree;
import com.ctvit.vdp.entity.SortTreeExample;
import com.ctvit.vdp.entity.SystemConfig;
import com.ctvit.vdp.entity.User;
import com.ctvit.vdp.entity.UserExample;
import com.ctvit.vdp.entity.UserRoles;
import com.ctvit.vdp.entity.UserRolesExample;
import com.ctvit.vdp.services.sysconfiguration.basisconf.OrganizationService;
import com.ctvit.vdp.services.sysconfiguration.basisconf.SystemConfigService;
import com.ctvit.vdp.services.sysconfiguration.role.RoleService;
import com.utils.GUIDUtil;
import com.ctvit.ws.vdp.VDPPort;
import com.ctvit.ws.vdp.parameter.SynUsers;

public class UserService extends BaseServices {
    private UserMapper userDao;
    private UserRolesMapper userRolesDao;
    private RoleMapper roleDao;
    private SystemConfigMapper systemConfigDao;
    private OrganizationMapper organizationDao;
    private SortTreeMapper sortTreeDao;
    private RoleService roleService;
    private Element rightElement;
    private OrganizationService organizationService;
    private SystemConfigService systemConfigService;
    private User currentUser;

    public SystemConfigService getSystemConfigService() {
        return systemConfigService;
    }

    public void setSystemConfigService(SystemConfigService systemConfigService) {
        this.systemConfigService = systemConfigService;
    }

    private static final Logger logger = Logger.getLogger(UserService.class);

    public User loadUser(String userid) {
        return userDao.selectByPrimaryKey(userid);
    }

    public User getCurrentUser() {
        return currentUser;
    }

    /**
     * ?
     * @throws ParseException 
     **/
    public Map<String, Object> getUser(String name, String password) throws DocumentException, ParseException {
        System.out.println("name: " + name + " password: " + password);
        Map<String, Object> userMap = null;
        try {
            User user = null;
            UserExample example = new UserExample();
            if (name != null && !name.equals("") && password != null && !password.equals("")) {
                example.createCriteria().andUserflagEqualTo(1).andUsernameEqualTo(name)
                        .andUserpasswordEqualTo(password);
            }
            if (userDao.selectByExampleWithBLOBs(example).size() > 0) {
                user = userDao.selectByExampleWithBLOBs(example).get(0);
                //???
                UserRolesExample userRolesExample = new UserRolesExample();
                userRolesExample.createCriteria().andUseUseridEqualTo(user.getUserid());
                List<UserRoles> userRoles = userRolesDao.selectByExample(userRolesExample);
                if (userRoles.size() > 0) {
                    Role role1 = roleDao.selectByPrimaryKey(userRoles.get(0).getRolRoleid());
                    user.setRoles(role1.getRolename());
                }
            }
            userMap = new HashMap<String, Object>();
            if (user != null) {
                UserRolesExample userRolesExample = new UserRolesExample();
                userRolesExample.createCriteria().andUseUseridLike(user.getUserid());
                List<UserRoles> userRolesList = userRolesDao.selectByExample(userRolesExample);
                if (userRolesList.size() > 0) {//?
                    Map<String, String> rightsMap = new HashMap<String, String>();
                    for (UserRoles userRoles : userRolesList) {
                        Role role = roleDao.selectByPrimaryKey(userRoles.getRolRoleid());
                        if (role != null && role.getRolerights() != null) {
                            System.out.println("****** role name is " + role.getRolename());
                            for (String rights : role.getRolerights().split(",")) {
                                rightsMap.put(rights, rights);//??IDmap
                            }
                        }
                    }
                    if (rightsMap.size() > 0) {
                        userMap.put("getXML", this.getRightIds(rightsMap));//?topXML?treeXML??rightIds
                    } else {
                        userMap.put("getXML", "");
                    }
                } else {
                    userMap.put("getXML", "");
                }
                userMap.put("userInfo", user);

                userMap.put("organizationInfoBean", this.organizationInfoBean(user));
                userMap.put("organizationInfo", this.organizationInfo());
                userMap.put("sortTreeInfo", this.sortTreeInfo());

            } else {
                userMap.put("userInfo", user);

            }
            //??
            if (user != null) {
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
                String nowDate = format.format(new Date());
                SystemConfig systemConfig = new SystemConfig();
                systemConfig.setId("LastLoginDate");
                systemConfig.setValue(nowDate);
                systemConfigService.updateSystemConfig(systemConfig);
            }

            String needLicense = systemConfigService.getSystemConfig("NeedLicense");
            if (needLicense.equals("0")) {
                String lastDate = systemConfigService.getSystemConfig("LastLoginDate");
                String licenseDate = systemConfigService.getSystemConfig("License");
                if (licenseDate != null && !licenseDate.equals("")) {
                    String[] licenses = licenseDate.split(",");
                    if (!expire(lastDate, licenses[0])) {
                        userMap.put("licenseStatus", "expire");
                    } else {
                        userMap.put("licenseStatus", licenses[1]);
                    }
                } else {
                    userMap.put("license", "needLicense");
                }

            }
            currentUser = user;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return userMap;

    }

    //???
    public boolean expire(String lastDate, String licenseDate) throws ParseException {
        try {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            String nowDate = format.format(new Date());
            if (!nowDate.equals(lastDate)
                    && java.sql.Date.valueOf(nowDate).before(java.sql.Date.valueOf(lastDate))) {
                return false;
            } else {
                if (nowDate.equals(licenseDate)
                        || java.sql.Date.valueOf(nowDate).before(java.sql.Date.valueOf(licenseDate))) {
                    return true;
                } else {
                    return false;
                }
            }
        } catch (Exception e) {
            logger.info("?:" + e.getMessage());
            return false;
        }
    }

    public User getUserById(String userId) {
        UserExample userExample = new UserExample();
        userExample.createCriteria().andUseridEqualTo(userId);
        List<User> list = userDao.selectByExampleWithBLOBs(userExample);
        if (list != null && list.size() > 0) {
            return list.get(0);
        } else {
            return null;
        }
    }

    public User getUser(String username) {
        UserExample example = new UserExample();
        example.createCriteria().andUsernameEqualTo(username);
        List<User> list = userDao.selectByExampleWithBLOBs(example);
        if (list != null && list.size() > 0) {
            return list.get(0);
        } else {
            return null;
        }
    }

    public User getUser1(String username) {
        UserExample example = new UserExample();
        example.createCriteria().andUsernameEqualTo(username).andUserflagEqualTo(1);
        List<User> list = userDao.selectByExampleWithBLOBs(example);
        if (list != null && list.size() > 0) {
            return list.get(0);
        } else {
            return null;
        }
    }

    /**
     * ?
     **/
    public List<User> getUserList(String userId) {
        UserExample example = new UserExample();
        example.createCriteria().andUserflagEqualTo(1);
        List<User> userList = userDao.selectByExampleWithBLOBs(example);

        for (User user : userList) {//
            StringBuffer roleName = new StringBuffer();
            UserRolesExample userRolesExample = new UserRolesExample();
            userRolesExample.createCriteria().andUseUseridEqualTo(user.getUserid());
            List<UserRoles> userRolesList = userRolesDao.selectByExample(userRolesExample);
            for (UserRoles userRoles : userRolesList) {
                Role role = roleDao.selectByPrimaryKey(userRoles.getRolRoleid());
                if (role != null && role.getRolename() != null) {
                    roleName.append(role.getRolename() + "?");
                }
            }
            String roleNameStr = roleName.toString();
            if (roleNameStr.length() > 0) {
                user.setRoles(roleNameStr.substring(0, roleNameStr.length() - 1));
            } else {
                user.setRoles("?");
            }
        }
        //      for(User u:userList){
        //         System.out.println("roles: "+u.getRoles());
        //      }
        return userList;
    }

    /**
     * ?manage
     **/
    public Map<String, Object> getUserList(String userId, String organizationId, int pageSize, int recordIndex) {
        //      System.out.println("userId: "+userId);
        //      System.out.println("organizationId: "+organizationId);
        //      System.out.println("pageSize: "+pageSize);
        //      System.out.println("recordIndex: "+recordIndex);
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
            UserExample example = new UserExample();
            example.createCriteria().andUserflagEqualTo(1);
            long count = userDao.countByExample(example);//?
            resultMap.put("totalRows", count);

            example.setDistinct(true);
            example.setPageSize(pageSize);
            example.setRecordIndex(recordIndex);
            example.setOrderByClause("createtime");
            example.setOrderExample("desc");
            List<User> userList = userDao.selectPageByExample(example);

            for (User user : userList) {//
                StringBuffer roleName = new StringBuffer();
                UserRolesExample userRolesExample = new UserRolesExample();
                userRolesExample.createCriteria().andUseUseridEqualTo(user.getUserid());
                List<UserRoles> userRolesList = userRolesDao.selectByExample(userRolesExample);
                for (UserRoles userRoles : userRolesList) {
                    Role role = roleDao.selectByPrimaryKey(userRoles.getRolRoleid());
                    if (role != null && role.getRolename() != null) {
                        roleName.append(role.getRolename() + "?");
                    }
                }
                String roleNameStr = roleName.toString();
                if (roleNameStr.length() > 0) {
                    user.setRoles(roleNameStr.substring(0, roleNameStr.length() - 1));
                } else {
                    user.setRoles("?");
                }
            }
            resultMap.put("result", userList);
            //         System.out.println("****** count: "+count);
            //         System.out.println("****** returnList: "+userList.size());
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return resultMap;
    }

    /**
     * ?
     **/
    public List<Role> getRoleList(String roleId) {
        return roleService.getRoleList(roleId);
    }

    /**
     * 
     * ??
     **/
    public int delUsers(String userIds) {
        int flag = 0;
        String[] userIdSZ = userIds.split(",");
        for (String userId : userIdSZ) {
            User user = userDao.selectByPrimaryKey(userId);
            if (user != null) {
                logger.info(": " + user.getUsername());
                user.setUserflag(0);
                flag = userDao.updateByPrimaryKeyWithBLOBs(user);
                if (flag == 1) {//????
                    SynUsers synUsers = new SynUsers();
                    System.out.println("synUsers " + synUsers);
                    SynUsers.User sUser = new SynUsers.User();
                    sUser.setOPERATION(2);//0?1?2
                    sUser.setUserid(user.getUserid());
                    synUsers.getUser().add(sUser);
                    //               BeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource("./config/applicationContext.xml"));
                    //               VDPPort qEditServiceClient = (VDPPort) beanFactory.getBean("qEditServiceClient");
                    //               SynUsersResponse synUsersResponse = qEditServiceClient.sysUsers(synUsers);
                    //               if(synUsersResponse.getDOFLAG()==1){//1????
                    //                  logger.info("*********** ?? ***********");
                    //                  flag = userDao.updateByPrimaryKeyWithBLOBs(user);
                    //               }else{
                    //                  logger.info("*********** ? ***********");
                    //               }
                }
            }
        }
        return flag;
    }

    /**
     * 
     **/
    public int userSaveRoles(Object object) {
        int returnFlag = 0;
        try {
            Map<String, Object> map = (HashMap) object;
            String userId = (String) map.get("userId");//ID
            String selectIds = (String) map.get("selectIds");//??
            UserRolesExample example = new UserRolesExample();
            example.createCriteria().andUseUseridEqualTo(userId);

            //
            userRolesDao.deleteByExample(example);
            //??
            String[] roleIds = selectIds.split(",");
            for (String roleId : roleIds) {
                UserRoles userRoles = new UserRoles();
                userRoles.setRelationid(GUIDUtil.get());
                userRoles.setRolRoleid(roleId);
                userRoles.setUseUserid(userId);
                returnFlag = userRolesDao.insert(userRoles);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        //      if(userId!=null && !userId.equals("") && selectIds!=null && !selectIds.equals("")){
        //         String[] roleIds = selectIds.split(",");
        //         for(String roleId:roleIds){
        //            UserRolesExample example = new UserRolesExample();
        //            example.createCriteria().andRolRoleidLike(roleId).andUseUseridLike(userId);
        //            List<UserRoles> userrolesList = userRolesDao.selectByExample(example);
        //            if(userrolesList.size()==0){//?IDID ?,??
        //               logger.info("  ID"+ roleId + " ID"+userId);
        //               UserRoles userRoles = new UserRoles();
        //               userRoles.setRelationid(GUIDUtil.get());
        //               userRoles.setRolRoleid(roleId);
        //               userRoles.setUseUserid(userId);
        //               returnFlag = userRolesDao.insert(userRoles);
        //            }else{
        //               returnFlag = 1;
        //            }
        //         }
        //      }
        return returnFlag;
    }

    /**
     * ???
     **/
    public int saveRole(Object object) {
        int flag = 0;
        Map<String, Object> map = (HashMap) object;
        String creatorId = (String) map.get("userId");//?ID
        String userId = (String) map.get("userId");//ID
        String roldIds = (String) map.get("roldIds");//??
        User user = userDao.selectByPrimaryKey(userId);
        User creator = userDao.selectByPrimaryKey(creatorId);
        UserRoles userRoles = null;
        if (user != null) {//useUserid
            UserRolesExample srExample = new UserRolesExample();
            srExample.createCriteria().andUseUseridLike(user.getUserid());
            if (userRolesDao.selectByExample(srExample).size() > 0) {
                userRoles = userRolesDao.selectByExample(srExample).get(0);
            }
            if (userRoles == null) {//????
                Role role = new Role();
                role.setRoleid(GUIDUtil.get());
                role.setRoleflag(1);//?
                role.setRolerights(roldIds.substring(0, roldIds.length() - 1));//??ID
                role.setCreatetime(new Date());//
                roleDao.insert(role);//???

                user.setRoleflag(1);
                userDao.updateByPrimaryKeyWithBLOBs(user);

                userRoles = new UserRoles();
                userRoles.setRelationid(GUIDUtil.get());//
                userRoles.setUseUserid(user.getUserid());//ID
                userRoles.setRolRoleid(role.getRoleid());//ID
                userRolesDao.insert(userRoles);
            } else {//???
                Role role = null;
                role = roleDao.selectByPrimaryKey(userRoles.getRolRoleid());
                if (role == null) {//?
                    role = new Role();
                    role.setRoleid(GUIDUtil.get());
                    role.setRoleflag(1);//?
                    role.setRolerights(roldIds.substring(0, roldIds.length() - 1));//??ID
                    role.setCreatetime(new Date());//
                    if (creator != null && creator.getUserid() != null) {//id
                        role.setCreator(creator.getUserid());
                    } else {
                        role.setCreator("");
                    }
                    roleDao.insert(role);//???
                    user.setRoleflag(1);
                    userDao.updateByPrimaryKeyWithBLOBs(user);

                } else {//?? 
                    role.setRolerights(roldIds.substring(0, roldIds.length() - 1));//??ID
                    role.setUpdatecreatetime(new Date());//
                    if (creator != null && creator.getUserid() != null) {//Id
                        role.setUpdateuserid(creator.getUserid());
                    } else {
                        role.setCreator("");
                    }
                    roleDao.updateByPrimaryKey(role);//??
                }
            }
            flag = 1;
        }
        return flag;
    }

    //?
    public int saveUser_(User user) {
        //      System.out.println("userId : "+user.getUserid());
        int flag = 0;
        SynUsers synUsers = new SynUsers();
        SynUsers.User sUser = new SynUsers.User();

        BeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource(Constants.APPLICATION_PATH));

        VDPPort qEditServiceClient = (VDPPort) beanFactory.getBean("qEditServiceClient");

        if (user.getUserid() == null || user.getUserid().equals("")) {
            logger.info(": " + user.getUsername());
            user.setUserid(GUIDUtil.get());
            user.setCreatetime(new Date());
            user.setUserflag(new Integer(1));
            flag = userDao.insert(user);//303?(??)

            //?
            if (flag == 1) {//????
                sUser.setOPERATION(0);//0?1?2
                sUser.setUserid(user.getUserid());//id
                sUser.setCreatetime(user.getCreatetime());//
                sUser.setOrganizationid(user.getOrganizationid());//id
                sUser.setUsername(user.getUsername());//??
                sUser.setUserrealname(user.getUserrealname());
                sUser.setUserpassword(user.getUserpassword());//?
                synUsers.getUser().add(sUser);

                //            SynUsersResponse synUsersResponse = qEditServiceClient.sysUsers(synUsers);
                //            if(synUsersResponse.getDOFLAG()==1){//1????
                //               logger.info("*********** ?? ***********");
                //               
                //            }else{
                //               logger.info("*********** ? ***********");
                //            }
            }
            //??
        } else {
            logger.info(": " + user.getUsername());
            User userUp = userDao.selectByPrimaryKey(user.getUserid());
            if (userUp != null) {
                userUp.setUsername(user.getUsername());
                userUp.setUserpassword(user.getUserpassword());
                userUp.setUserrealname(user.getUserrealname());
                userUp.setOrganizationid(user.getOrganizationid());
                userUp.setUseremail(user.getUseremail());
                userUp.setUpdatecreatetime(new Date());
                flag = userDao.updateByPrimaryKeyWithBLOBs(userUp);
                //?
                if (flag == 1) {//????
                    sUser.setOPERATION(1);//0?1?2
                    sUser.setUserid(user.getUserid());//id
                    sUser.setCreatetime(user.getCreatetime());//
                    sUser.setOrganizationid(user.getOrganizationid());//id
                    sUser.setUsername(user.getUsername());//??
                    sUser.setUserpassword(user.getUserpassword());//?
                    sUser.setUseremail(user.getUseremail());//
                    sUser.setUserrealname(user.getUserrealname());
                    synUsers.getUser().add(sUser);
                    //               SynUsersResponse synUsersResponse = qEditServiceClient.sysUsers(synUsers);
                    //               if(synUsersResponse.getDOFLAG()==1){//1????
                    //                  logger.info("*********** ?? ***********");
                    //                  flag = userDao.updateByPrimaryKeyWithBLOBs(userUp);
                    //               }else{
                    //                  logger.info("*********** ? ***********");
                    //               }
                }
                //??
            } else {
                flag = 0;
            }
        }

        return flag;
    }

    public int saveUser(User user) {
        int flag = 0;
        if (user.getUserid() == null || user.getUserid().equals("")) {
            logger.info(": " + user.getUsername());
            user.setUserid(GUIDUtil.get());
            user.setCreatetime(new Date());
            user.setUserflag(new Integer(1));
            flag = userDao.insert(user);//303?(??)
        } else {
            logger.info(": " + user.getUsername());
            User userUp = userDao.selectByPrimaryKey(user.getUserid());
            if (userUp != null) {
                userUp.setUsername(user.getUsername());
                userUp.setUserpassword(user.getUserpassword());
                userUp.setUserrealname(user.getUserrealname());
                userUp.setOrganizationid(user.getOrganizationid());
                userUp.setUseremail(user.getUseremail());
                userUp.setUpdatecreatetime(new Date());
                flag = userDao.updateByPrimaryKeyWithBLOBs(userUp);
            } else {
                flag = 0;
            }
        }
        return flag;
    }

    /**
     * ??
     **/
    public Map<String, Object> organizationInfo() {
        Map<String, Object> organizationInfo = new HashMap<String, Object>();
        //Map<String,Organization> organizationMap = new HashMap<String,Organization>();
        List<Organization> organizationList = new ArrayList<Organization>();
        OrganizationExample organizationExample = new OrganizationExample();
        organizationExample.createCriteria().andOrganizationflagEqualTo(1);
        organizationList = this.organizationDao.selectByExample(organizationExample);
        organizationInfo.put("organizationList", organizationList);
        //v1.3
        //organizationInfo.put("organizationList", organizationService.queryAll(null));
        return organizationInfo;
    }

    /**
     * ??
     **/
    public Organization organizationInfoBean(User user) {
        //v1.3
        return organizationService.queryOrgForUser(user);
    }

    /**
     * ??
     **/
    public Map<String, Object> sortTreeInfo() {
        Map<String, Object> sortTreeInfo = new HashMap<String, Object>();
        Map<String, SortTree> sortTreeMap = new HashMap<String, SortTree>();
        SortTreeExample example = new SortTreeExample();
        example.createCriteria().andTreenodeflagEqualTo(1);
        List<SortTree> sortTreeList = sortTreeDao.selectByExample(example);
        for (SortTree sortTree : sortTreeList) {
            sortTreeMap.put(sortTree.getTreenodeid(), sortTree);
        }
        sortTreeInfo.put("sortTreeList", sortTreeList);
        sortTreeInfo.put("sortTreeMap", sortTreeMap);
        return sortTreeInfo;
    }

    /**
     * ?rightsID?id???XMLid
     * @throws DocumentException 
     **/
    public Map<String, Object> getRightIds(Map<String, String> rightIdsMap) throws DocumentException {
        Map<String, String> xmlRightIds = new HashMap<String, String>();
        Map<String, String> thirdRightIds = new HashMap<String, String>();
        Map<String, String> rightIds = new HashMap<String, String>();
        StringBuffer roleRightIds = new StringBuffer();
        Iterator rightIdsIterator = rightIdsMap.entrySet().iterator();
        while (rightIdsIterator.hasNext()) {
            Map.Entry rightId = (Map.Entry) rightIdsIterator.next();
            String rightIdStr = rightId.getValue().toString();
            rightIds.put(rightIdStr, rightIdStr);
            if (rightIdStr.length() == 4) {//id
                xmlRightIds.put(rightIdStr, rightIdStr);
            } else if (rightIdStr.length() == 6) {//id
                thirdRightIds.put(rightIdStr, rightIdStr);
            } else {//??id
                roleRightIds.append(rightIdStr + ",");
            }
        }
        Map<String, String> xmlMap = this.getXML(xmlRightIds, thirdRightIds);
        String rightXML = this.getRightXML(rightIds);

        Map<String, Object> sortRightIds = new HashMap<String, Object>();
        sortRightIds.put("roleRightIds", roleRightIds);//??????ID
        sortRightIds.put("rightXML", rightXML);//??????ID
        sortRightIds.put("topMenu", xmlMap.get("topMenu"));//??????ID
        sortRightIds.put("treeMenu", xmlMap.get("treeMenu"));//??????ID
        sortRightIds.put("baseXML", xmlMap.get("baseXML"));//??????ID
        return sortRightIds;
    }

    /**
     * ?????XML
     **/
    public Map<String, String> getXML(Map xmlRightIds, Map thirdRightIds) throws DocumentException {
        String baseXML = systemConfigDao.selectByPrimaryKey("Rights").getValue();//??XML
        Document doc = DocumentHelper.parseText(baseXML);
        Document topDoc = DocumentHelper.createDocument();

        Element baseElement = doc.getRootElement();
        Element topEl = topDoc.addElement("Rights");//?XML
        List<Element> secMenuList = new ArrayList<Element>();
        String topIdFlag = "";

        Iterator elementIter = baseElement.elementIterator();
        while (elementIter.hasNext()) {//??
            Element element01 = (Element) elementIter.next();
            Iterator elementIter01 = element01.elementIterator();
            while (elementIter01.hasNext()) {//??
                Element element02 = (Element) elementIter01.next();
                Iterator elementIter02 = element02.elementIterator();
                String idFlag = "";
                if (xmlRightIds.get(element02.attributeValue("id")) != null) {//??ID?ID
                    Element tempEl = element02.getParent();//?
                    if (topEl.nodeCount() > 0 && !topIdFlag.equals(tempEl.attributeValue("id"))) {
                        topEl.addElement(tempEl.getName()).addAttribute("id", element01.attributeValue("id"))
                                .addAttribute("name", element01.attributeValue("name"));
                    }
                    if (topEl.nodeCount() == 0) {
                        topEl.addElement(tempEl.getName()).addAttribute("id", element01.attributeValue("id"))
                                .addAttribute("name", element01.attributeValue("name"));
                    }
                    topIdFlag = tempEl.attributeValue("id");
                    secMenuList.add(element02);
                }
            }
        }

        StringBuffer secXML = new StringBuffer();
        secXML.append("<Rights>");
        Element tempTopEl = topEl.createCopy();
        //      System.out.println("tempTopEl: "+tempTopEl.asXML());
        Iterator secIt = tempTopEl.elementIterator();//????
        String flag = "";
        while (secIt.hasNext()) {
            Element op = (Element) secIt.next();
            for (Element eo : secMenuList) {//eo?? 
                if (eo.attributeValue("id").substring(0, 2).equals(op.attributeValue("id"))
                        && !flag.equals(eo.attributeValue("id"))) {
                    flag = eo.attributeValue("id");
                    Document secDoc = DocumentHelper.createDocument();
                    Element secEle = secDoc.addElement("SecMenu");
                    secEle.addAttribute("id", eo.attributeValue("id"));
                    secEle.addAttribute("name", eo.attributeValue("name"));
                    secEle.addAttribute("source", eo.attributeValue("source"));

                    Iterator eoIter = eo.elementIterator();
                    while (eoIter.hasNext()) {//??
                        Element thirdEl = (Element) eoIter.next();
                        if (thirdRightIds.get(thirdEl.attributeValue("id")) != null) {
                            Document document = DocumentHelper.createDocument();
                            Element tempEle = document.addElement("ThirdMenu");
                            tempEle.addAttribute("id", thirdEl.attributeValue("id"));
                            tempEle.addAttribute("name", thirdEl.attributeValue("name"));
                            tempEle.addAttribute("source", thirdEl.attributeValue("source"));
                            secEle.add(tempEle);//
                        }
                    }
                    op.add(secEle);//
                }
                //System.out.println("************ op: "+op.asXML());
            }
            secXML.append(op.asXML());
        }
        secXML.append("</Rights>");
        Map<String, String> xmlMap = new HashMap<String, String>();

        xmlMap.put("topMenu", topEl.asXML());
        xmlMap.put("treeMenu", secXML.toString());
        xmlMap.put("baseXML", baseElement.asXML());
        //      this.getElementList(baseElement,xmlRightIds);
        return xmlMap;
    }

    public String getRightXML(Map rightIds) throws DocumentException {
        String baseXML = systemConfigDao.selectByPrimaryKey("Rights").getValue();//??XML
        Document doc = DocumentHelper.parseText(baseXML);
        Element baseElement = doc.getRootElement();
        Iterator elementIter = baseElement.elementIterator();
        //?
        while (elementIter.hasNext()) {//??
            Element el01 = (Element) elementIter.next();
            if (rightIds.get(el01.attributeValue("id")) == null) {
                baseElement.remove(el01);
            }
            Iterator elIter01 = el01.elementIterator();
            while (elIter01.hasNext()) {//??
                Element el02 = (Element) elIter01.next();
                if (rightIds.get(el02.attributeValue("id")) == null) {
                    el01.remove(el02);
                }
                Iterator elIter02 = el02.elementIterator();
                while (elIter02.hasNext()) {//??(?)
                    Element el03 = (Element) elIter02.next();
                    if (rightIds.get(el03.attributeValue("id")) == null) {
                        el02.remove(el03);
                    }

                    Iterator elIter03 = el03.elementIterator();
                    while (elIter03.hasNext()) {//??()
                        Element el04 = (Element) elIter03.next();
                        if (rightIds.get(el04.attributeValue("id")) == null) {
                            el03.remove(el04);
                        }
                        Iterator elIter04 = el04.elementIterator();
                        while (elIter04.hasNext()) {//??()
                            Element el05 = (Element) elIter04.next();
                            System.out.println(el05.attributeValue("id"));
                            if (rightIds.get(el05.attributeValue("id")) == null) {
                                el04.remove(el05);
                            }
                        }
                    }
                }
            }
        }
        return baseElement.asXML();
    }

    /**
     * ?manage
     **/
    public Map<String, Object> getSearchUserList(String userId, String organizationId, int pageSize,
            int recordIndex, String userName, String realName) {
        //      System.out.println("userId: "+userId);
        //      System.out.println("organizationId: "+organizationId);
        //      System.out.println("pageSize: "+pageSize);
        //      System.out.println("recordIndex: "+recordIndex);
        //      System.out.println("userName: "+userName);
        //      System.out.println("realName: "+realName);
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
            UserExample example = new UserExample();
            example.createCriteria().andUserflagEqualTo(1);
            long count = userDao.countByExample(example);//?
            resultMap.put("totalRows", count);

            example.setDistinct(true);
            example.setPageSize(pageSize);
            example.setRecordIndex(recordIndex);
            example.setOrderByClause("userid");
            example.setOrderExample("desc");
            if (userName != null && !userName.equals("")) {
                example.setUserName(userName);
            }
            if (realName != null && !realName.equals("")) {
                example.setRealName(realName);
            }
            List<User> userList = userDao.selectPageByExample(example);

            for (User user : userList) {//
                StringBuffer roleName = new StringBuffer();
                UserRolesExample userRolesExample = new UserRolesExample();
                userRolesExample.createCriteria().andUseUseridEqualTo(user.getUserid());
                List<UserRoles> userRolesList = userRolesDao.selectByExample(userRolesExample);
                for (UserRoles userRoles : userRolesList) {
                    Role role = roleDao.selectByPrimaryKey(userRoles.getRolRoleid());
                    if (role != null && role.getRolename() != null) {
                        roleName.append(role.getRolename() + "?");
                    }
                }
                String roleNameStr = roleName.toString();
                if (roleNameStr.length() > 0) {
                    user.setRoles(roleNameStr.substring(0, roleNameStr.length() - 1));
                } else {
                    user.setRoles("?");
                }
            }
            resultMap.put("result", userList);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return resultMap;
    }

    public OrganizationService getOrganizationService() {
        return organizationService;
    }

    public void setOrganizationService(OrganizationService organizationService) {
        this.organizationService = organizationService;
    }

    public SystemConfigMapper getSystemConfigDao() {
        return systemConfigDao;
    }

    public void setSystemConfigDao(SystemConfigMapper systemConfigDao) {
        this.systemConfigDao = systemConfigDao;
    }

    public UserMapper getUserDao() {
        return userDao;
    }

    public void setUserDao(UserMapper userDao) {
        this.userDao = userDao;
    }

    public UserRolesMapper getUserRolesDao() {
        return userRolesDao;
    }

    public void setUserRolesDao(UserRolesMapper userRolesDao) {
        this.userRolesDao = userRolesDao;
    }

    public RoleMapper getRoleDao() {
        return roleDao;
    }

    public void setRoleDao(RoleMapper roleDao) {
        this.roleDao = roleDao;
    }

    public OrganizationMapper getOrganizationDao() {
        return organizationDao;
    }

    public void setOrganizationDao(OrganizationMapper organizationDao) {
        this.organizationDao = organizationDao;
    }

    public RoleService getRoleService() {
        return roleService;
    }

    public void setRoleService(RoleService roleService) {
        this.roleService = roleService;
    }

    public SortTreeMapper getSortTreeDao() {
        return sortTreeDao;
    }

    public void setSortTreeDao(SortTreeMapper sortTreeDao) {
        this.sortTreeDao = sortTreeDao;
    }

    public Element getRightElement() {
        return rightElement;
    }

    public void setRightElement(Element rightElement) {
        this.rightElement = rightElement;
    }

    public static void main(String[] args) throws IOException, DocumentException, ParseException {
        BeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource(Constants.APPLICATION_PATH));

        UserService service = (UserService) beanFactory.getBean("userService");
        //      File file = new File("D:\\?.xml");
        //      FileInputStream is = new FileInputStream(file);
        //      byte []bytes = new byte[(int)file.length()];
        //      is.read(bytes);
        //      String content = new String(bytes,"GBK");
        //      System.out.println(content);
        //
        //      try{
        //         User user = new User();
        //         user.setUsername("maininsert");
        //         user.setUserid("2a6c2828be4c4904b0f59452280e1cf7");
        //         user.setFaviratetree(content);
        //         
        //         System.out.println(service.saveUser(user));
        //      }catch(Exception e){
        //         System.out.println(e);
        //      }
        //      service.getUser("admin","admin");
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        System.out.println(service.expire("2014-02-18", "2014-02-18"));
        //      System.out.println(java.sql.Date.valueOf("2014-02-18").before(java.sql.Date.valueOf("2014-02-17")));
    }
}