public void add(Auth auth) {
   Tauth t = new Tauth();
   BeanUtils.copyProperties(auth, t);
   t.setCid(UUID.randomUUID().toString());
   if (auth.getPid() != null && !auth.getPid().equals(auth.getCid())) {
     t.setTauth(authDao.get(Tauth.class, auth.getPid()));
   }
   authDao.save(t);
 }
 public void delete(String ids) {
   if (ids != null) {
     for (String id : ids.split(",")) {
       Tuser u = userDao.get(Tuser.class, id.trim());
       if (u != null) {
         userroleDao.executeHql("delete Tusertrole t where t.tuser = ?", new Object[] {u});
         userDao.delete(u);
       }
     }
   }
 }
 public List<Auth> treegrid(Auth auth) {
   List<Tauth> l;
   if (auth != null && auth.getId() != null) {
     l =
         authDao.find(
             "from Tauth t where t.tauth.cid = ? order by t.cseq", new Object[] {auth.getId()});
   } else {
     l = authDao.find("from Tauth t where t.tauth is null order by t.cseq");
   }
   return changeModel(l);
 }
 /**
  * keep relationship between user and role
  *
  * @param user
  * @param u
  */
 private void saveUserRole(User user, Tuser u) {
   userroleDao.executeHql("delete Tusertrole t where t.tuser = ?", new Object[] {u});
   if (user.getRoleIds() != null) {
     for (String id : user.getRoleIds().split(",")) {
       Tusertrole tusertrole = new Tusertrole();
       tusertrole.setCid(UUID.randomUUID().toString());
       tusertrole.setTrole(roleDao.get(Trole.class, id.trim()));
       tusertrole.setTuser(u);
       userroleDao.save(tusertrole);
     }
   }
 }
 private void del(String cid) {
   Tauth t = authDao.get(Tauth.class, cid);
   if (t != null) {
     roleauthDao.executeHql("delete Troletauth t where t.tauth = ?", new Object[] {t});
     Set<Tauth> auths = t.getTauths();
     if (auths != null && !auths.isEmpty()) {
       // there exists child auth under current auth
       for (Tauth tauth : auths) {
         del(tauth.getCid());
       }
     }
     authDao.delete(t);
   }
 }
 public void roleEdit(User user) {
   if (user.getIds() != null) {
     for (String id : user.getIds().split(",")) {
       Tuser u = userDao.get(Tuser.class, id);
       this.saveUserRole(user, u);
     }
   }
 }
 public void update(User user) {
   Tuser u = userDao.get(Tuser.class, user.getCid());
   BeanUtils.copyProperties(user, u, new String[] {"cid", "cpwd"});
   if (user.getCpwd() != null && !user.getCpwd().trim().equals("")) {
     u.setCpwd(Encrypt.e(user.getCpwd()));
   }
   u.setCmodifydatetime(new Date());
   this.saveUserRole(user, u);
 }
 public List<User> combobox(User user) {
   String q = "";
   if (user != null && user.getQ() != null) {
     q = user.getQ().trim();
   }
   return changeModel(
       userDao.find(
           "from Tuser t where t.cname like ?", new Object[] {"%%" + q.trim() + "%%"}, 1, 10));
 }
 public void edit(Auth auth) {
   Tauth t = authDao.get(Tauth.class, auth.getCid()); // authority to be edit
   BeanUtils.copyProperties(auth, t);
   if (auth.getPid() != null && !auth.getPid().equals(auth.getCid())) {
     Tauth pAuth = authDao.get(Tauth.class, auth.getPid()); // parent authority
     if (pAuth != null) {
       if (isDown(t, pAuth)) {
         Set<Tauth> tauths = t.getTauths(); // all child auth of current auth
         if (tauths != null && tauths.size() > 0) {
           for (Tauth tauth : tauths) {
             if (tauth != null) {
               tauth.setTauth(null);
             }
           }
         }
       }
       t.setTauth(pAuth);
     }
   }
 }
  private List<Tuser> find(User user) {
    String hql = "from Tuser t where 1=1 ";

    List<Object> values = new ArrayList<Object>();
    hql = addWhere(user, hql, values);

    if (user.getSort() != null && user.getOrder() != null) {
      hql += " order by " + user.getSort() + " " + user.getOrder();
    }
    return userDao.find(hql, values, user.getPage(), user.getRows());
  }
 public List<TreeNode> tree(Auth auth, boolean b) {
   List<Object> param = new ArrayList<Object>();
   String hql = "from Tauth t where t.tauth is null order by t.cseq";
   if (auth != null && auth.getId() != null && !auth.getId().trim().equals("")) {
     hql = "from Tauth t where t.tauth.cid = ? order by t.cseq";
     param.add(auth.getId());
   }
   List<Tauth> l = authDao.find(hql, param);
   List<TreeNode> tree = new ArrayList<TreeNode>();
   for (Tauth t : l) {
     tree.add(this.tree(t, b));
   }
   return tree;
 }
  public void save(User user) {
    Tuser u = new Tuser();
    BeanUtils.copyProperties(user, u, new String[] {"cpwd"});
    u.setCid(UUID.randomUUID().toString());
    u.setCpwd(Encrypt.e(user.getCpwd()));
    if (user.getCcreatedatetime() == null) {
      u.setCcreatedatetime(new Date());
    }
    if (user.getCmodifydatetime() == null) {
      u.setCmodifydatetime(new Date());
    }
    userDao.save(u);

    this.saveUserRole(user, u);
  }
  @Override
  public List<RoleChart> countRole() {

    List<RoleChart> datalist = new ArrayList<RoleChart>();

    String hql =
        "select r.cname as name,count(u.cid) as count from Qrole r left join Qusertrole u on r.cid=u.croleid group by r.cname";
    List list = roleChartDao.findSQL(hql);
    for (int i = 0; i < list.size(); i++) {
      Object[] object = (Object[]) list.get(i);
      RoleChart rc = new RoleChart();
      rc.setName(object[0].toString());
      rc.setCount(Integer.valueOf((object[1]).toString()));
      datalist.add(rc);
    }
    return datalist;
  }
 /** count the number of child nodes */
 private Long countChildren(String pid) {
   return authDao.count("select count(*) from Tauth t where t.tauth.cid = ?", new Object[] {pid});
 }
 private Long total(User user) {
   String hql = "select count(*) from Tuser t where 1=1 ";
   List<Object> values = new ArrayList<Object>();
   hql = addWhere(user, hql, values);
   return userDao.count(hql, values);
 }
  public User login(User user) {
    Tuser t =
        userDao.get(
            "from Tuser t where t.cname = ? and t.cpwd = ?",
            new Object[] {user.getCname(), Encrypt.e(user.getCpwd())});
    if (t != null) {
      BeanUtils.copyProperties(t, user, new String[] {"cpwd"});

      Map<String, String> authIdsMap = new HashMap<String, String>();
      String authIds = "";
      String authNames = "";
      Map<String, String> authUrlsMap = new HashMap<String, String>();
      String authUrls = "";
      String roleIds = "";
      String roleNames = "";
      boolean b1 = false;
      Set<Tusertrole> tusertroles = t.getTusertroles();
      if (tusertroles != null && tusertroles.size() > 0) {
        for (Tusertrole tusertrole : tusertroles) {
          Trole trole = tusertrole.getTrole();
          if (b1) {
            roleIds += ",";
            roleNames += ",";
          }
          roleIds += trole.getCid();
          roleNames += trole.getCname();
          b1 = true;

          Set<Troletauth> troletauths = trole.getTroletauths();
          if (troletauths != null && troletauths.size() > 0) {
            for (Troletauth troletauth : troletauths) {
              Tauth tauth = troletauth.getTauth();
              authIdsMap.put(tauth.getCid(), tauth.getCname());
              authUrlsMap.put(tauth.getCid(), tauth.getCurl());
            }
          }
        }
      }
      boolean b2 = false;
      for (String id : authIdsMap.keySet()) {
        if (b2) {
          authIds += ",";
          authNames += ",";
        }
        authIds += id;
        authNames += authIdsMap.get(id);
        b2 = true;
      }
      user.setAuthIds(authIds);
      user.setAuthNames(authNames);
      user.setRoleIds(roleIds);
      user.setRoleNames(roleNames);
      boolean b3 = false;
      for (String id : authUrlsMap.keySet()) {
        if (b3) {
          authUrls += ",";
        }
        authUrls += authUrlsMap.get(id);
        b3 = true;
      }
      user.setAuthUrls(authUrls);

      return user;
    }
    return null;
  }
 public void editUserInfo(User user) {
   if (user.getCpwd() != null && !user.getCpwd().trim().equals("")) {
     Tuser t = userDao.get(Tuser.class, user.getCid());
     t.setCpwd(Encrypt.e(user.getCpwd()));
   }
 }