protected void setEntity(User user) {
   user.setName(p("userName"));
   user.setDisplayName(p("userDisplayName"));
   user.setEmployee(AdminHelper.toEmployee(p("employee")));
   // 修改时不改变密码
   if (StringUtils.isNotEmpty(p("password"))) {
     user.setPassword(Tools.encodePassword(p("password")));
   }
 }
 public String getUsers() throws Exception {
   List<User> userList = getDefService().getListData().getList();
   JSONArray ja = new JSONArray();
   for (User user : userList) {
     JSONObject record = new JSONObject();
     record.put("id", user.getId());
     record.put("text", user.getEmployee().getName());
     ja.add(record);
   }
   jo.put("data", ja);
   return render(jo);
 }
 public String getLeaveEmp() throws Exception {
   queryMap.put("roleId", p("relativeId"));
   List<User> list = userService.getListData(query, queryMap, 0, 0).getList();
   JSONArray ja = new JSONArray();
   for (User user : list) {
     JSONObject record = new JSONObject();
     Employee e = user.getEmployee();
     record.put("id", e.getId());
     record.put("text", e.getName());
     ja.add(record);
   }
   jo.put("data", ja);
   return render(jo);
 }
 public String updateOrganizations() throws Exception {
   User user = userService.get(id);
   List<Organization> list = new ArrayList<Organization>();
   for (String code : getCheckedId()) {
     list.add(AdminHelper.toOrganization(code));
   }
   user.setOrganizations(list);
   userService.update(user);
   setJsonMessage(
       true,
       user.toString().equals("")
           ? "更新了一条记录"
           : (new StringBuffer("更新了(")).append(user).append(")的记录").toString());
   return render(jo);
 }
 private void doLog(Date createTime, User opUser, String opName, Model model)
     throws ParseException {
   String opUserName = opUser == null ? "系统管理员" : opUser.getDisplayName();
   String contents = getContents(createTime, opUserName, opName, model);
   printLogMsg(contents);
   saveToDb(createTime, opUserName, opName, contents);
 }
 public String getUserIsSysManager() throws Exception {
   Role role = roleService.getRoleByCode(Role.SYSTEM_GENERAL_STAFF);
   queryMap.put("roleId", role.getDbId() + "");
   queryMap.put("type", "sysOperate");
   List<User> list = userService.getListData(query, queryMap, 0, 0).getList();
   //		List<User> list = userService.getSomeUser();
   JSONArray ja = new JSONArray();
   for (User user : list) {
     JSONObject record = new JSONObject();
     //			Employee e = user.getEmployee();
     record.put("id", user.getId());
     record.put("name", user.getDisplayName());
     record.put("userName", user.getName());
     ja.add(record);
   }
   jo.put("data", ja);
   return render(jo);
 }
  @Override
  public String update() throws Exception {
    String userName = p("userName");
    boolean isExits = userService.checkUserName(userName, this.id);
    if (isExits) {
      User user = (User) getDefService().get(this.id);
      setEntity(user);
      String StrRoleArr = p("role");
      String roleArr[] = StrRoleArr.split(",");
      List<Role> list = new ArrayList<Role>();
      if (StrRoleArr != null && StrRoleArr != "") {
        String as[];
        int j = (as = roleArr).length;
        for (int i = 0; i < j; i++) {
          String id = as[i];
          Role role = AdminHelper.toRole(id);
          list.add(role);
        }

        user.setRoles(list);
      } else {
        user.setRoles(list);
      }
      userService.update(user);
      jo.put("entity", toJsonObject(user));
      setJsonMessage(
          true,
          user.toString().equals("")
              ? "更新了一条记录!"
              : (new StringBuilder("更新了(")).append(user).append(")的记录").toString());
      return render(jo);
    } else {
      setJsonMessage(
          false, (new StringBuilder("修改失败,用户")).append(userName).append("已经存在").toString());
      return render(jo);
    }
  }
  @Override
  public String create() throws Exception {
    String userName = p("userName");
    boolean isExist = userService.checkUserName(userName, null);
    if (isExist) {
      User user = (User) getEntityClass().newInstance();
      setEntity(user);
      String StrRoleArr = p("role");
      String roleArr[] = StrRoleArr.split(",");
      List<Role> roleList = new ArrayList<Role>();
      String as[];
      int j = (as = roleArr).length;
      for (int i = 0; i < j; i++) {
        String id = as[i];
        Role role = AdminHelper.toRole(id);
        roleList.add(role);
      }

      user.setRoles(roleList);
      Employee e = employeeService.get(p("employee"));
      List<Organization> organizations = new ArrayList<Organization>();
      organizations.add(e.getOrganization());
      user.setOrganizations(organizations);
      userService.add(user);
      jo.put("entity", toJsonObject(user));
      setJsonMessage(
          true,
          user.toString().equals("")
              ? "新增了一条记录!"
              : (new StringBuilder("新增了(")).append(user).append(")的记录").toString());
      return render(jo);
    } else {
      setJsonMessage(
          false, (new StringBuilder(String.valueOf(userName))).append("已经存在").toString());
      return render(jo);
    }
  }
 protected JSONObject toJsonObject(User user) throws Exception {
   AdminHelper record = new AdminHelper();
   record.put("id", user.getId());
   record.put("userName", user.getName());
   record.put("userDisplayName", user.getDisplayName());
   record.put("password", user.getPassword());
   record.put("password2", user.getPassword());
   record.put("employee", user.getEmployee());
   record.put("locked", user.isLocked());
   record.put("lastLoginTime", user.getLoginSession().getLastLoginTime());
   record.put("logined", user.getLoginSession().isLogined());
   record.put("organization", user.getEmployee().getOrganization());
   record.put("role", toRoleArray(user.getRoles()));
   return record.getJsonObject();
 }