public SystemUser update(
      SystemUserVO systemUserVO, String[] roleNames, String[] groupNames, Database db)
      throws ConstraintException, SystemException {
    SystemUser systemUser = getSystemUserWithName(systemUserVO.getUserName(), db);

    systemUserVO.setUserName(systemUser.getUserName());

    if (roleNames != null) {
      systemUser.getRoles().clear();
      for (int i = 0; i < roleNames.length; i++) {
        Role role = RoleController.getController().getRoleWithName(roleNames[i], db);
        systemUser.getRoles().add(role);
        role.getSystemUsers().add(systemUser);
      }
    }

    if (groupNames != null) {
      systemUser.getGroups().clear();
      for (int i = 0; i < groupNames.length; i++) {
        Group group = GroupController.getController().getGroupWithName(groupNames[i], db);
        systemUser.getGroups().add(group);
        group.getSystemUsers().add(systemUser);
      }
    }

    systemUserVO.setPassword(systemUser.getPassword());
    systemUser.setValueObject(systemUserVO);

    return systemUser;
  }
  public SystemUser update(
      SystemUserVO systemUserVO,
      String oldPassword,
      String[] roleNames,
      String[] groupNames,
      Database db)
      throws ConstraintException, SystemException, Exception {
    logger.info("systemUserVO:" + systemUserVO.getUserName());
    logger.info("oldPassword:"******"newPassword:"******"roleNames:" + roleNames);
    logger.info("groupNames:" + groupNames);
    if (CmsPropertyHandler.getUsePasswordEncryption()) {
      String password = systemUserVO.getPassword();
      try {
        byte[] encryptedPassRaw = DigestUtils.sha(password);
        String encryptedPass = new String(new Base64().encode(encryptedPassRaw), "ASCII");
        password = encryptedPass;
        systemUserVO.setPassword(password);

        byte[] encryptedOldPasswordRaw = DigestUtils.sha(oldPassword);
        String encryptedOldPassword =
            new String(new Base64().encode(encryptedOldPasswordRaw), "ASCII");
        oldPassword = encryptedOldPassword;
      } catch (Exception e) {
        logger.error("Error generating password:"******"Wrong user or password.");

    systemUserVO.setUserName(systemUser.getUserName());

    if (roleNames != null) {
      systemUser.getRoles().clear();
      for (int i = 0; i < roleNames.length; i++) {
        Role role = RoleController.getController().getRoleWithName(roleNames[i], db);
        systemUser.getRoles().add(role);
        role.getSystemUsers().add(systemUser);
      }
    }

    if (groupNames != null) {
      systemUser.getGroups().clear();
      for (int i = 0; i < groupNames.length; i++) {
        Group group = GroupController.getController().getGroupWithName(groupNames[i], db);
        systemUser.getGroups().add(group);
        group.getSystemUsers().add(systemUser);
      }
    }

    // systemUserVO.setPassword(systemUser.getPassword());
    systemUser.setValueObject(systemUserVO);

    return systemUser;
  }
  /** This method gets a users roles */
  public List authorizeUser(String userName) throws Exception {
    List roles = new ArrayList();
    List groups = new ArrayList();

    String administratorUserName = CmsPropertyHandler.getAdministratorUserName();

    boolean isAdministrator = userName.equalsIgnoreCase(administratorUserName) ? true : false;
    if (isAdministrator) return roles;

    if (transactionObject == null) {
      List roleVOList = RoleController.getController().getRoleVOList(userName);
      Iterator roleVOListIterator = roleVOList.iterator();
      while (roleVOListIterator.hasNext()) {
        RoleVO roleVO = (RoleVO) roleVOListIterator.next();
        InfoGlueRole infoGlueRole =
            new InfoGlueRole(roleVO.getRoleName(), roleVO.getDescription(), this);
        roles.add(infoGlueRole);
      }

      List groupVOList = GroupController.getController().getGroupVOList(userName);
      Iterator groupVOListIterator = groupVOList.iterator();
      while (groupVOListIterator.hasNext()) {
        GroupVO groupVO = (GroupVO) groupVOListIterator.next();
        InfoGlueGroup infoGlueGroup =
            new InfoGlueGroup(groupVO.getGroupName(), groupVO.getDescription(), this);
        groups.add(infoGlueGroup);
      }
    } else {
      Collection roleList = RoleController.getController().getRoleList(userName, transactionObject);
      Iterator roleListIterator = roleList.iterator();
      while (roleListIterator.hasNext()) {
        Role role = (Role) roleListIterator.next();
        InfoGlueRole infoGlueRole =
            new InfoGlueRole(role.getRoleName(), role.getDescription(), this);
        roles.add(infoGlueRole);
      }

      Collection groupList =
          GroupController.getController().getGroupList(userName, transactionObject);
      Iterator groupListIterator = groupList.iterator();
      while (groupListIterator.hasNext()) {
        Group group = (Group) groupListIterator.next();
        InfoGlueGroup infoGlueGroup =
            new InfoGlueGroup(group.getGroupName(), group.getDescription(), this);
        groups.add(infoGlueGroup);
      }
    }

    return groups;
  }
  public void delete(String userName, Database db)
      throws ConstraintException, SystemException, Exception {
    SystemUser systemUser = getSystemUserWithName(userName, db);

    Collection roles = systemUser.getRoles();
    Iterator rolesIterator = roles.iterator();
    while (rolesIterator.hasNext()) {
      Role role = (Role) rolesIterator.next();
      role.getSystemUsers().remove(systemUser);
    }

    Collection groups = systemUser.getGroups();
    Iterator groupsIterator = groups.iterator();
    while (groupsIterator.hasNext()) {
      Group group = (Group) groupsIterator.next();
      group.getSystemUsers().remove(systemUser);
    }

    db.remove(systemUser);
  }
  public List getFilteredSystemUserList(
      String firstName,
      String lastName,
      String userName,
      String email,
      String[] roleNames,
      Database db)
      throws SystemException, Bug, Exception {
    List filteredList = new ArrayList();

    OQLQuery oql =
        db.getOQLQuery(
            "SELECT u FROM org.infoglue.cms.entities.management.impl.simple.SystemUserImpl u ORDER BY u.userName");

    QueryResults results = oql.execute(Database.ReadOnly);

    while (results.hasMore()) {
      SystemUser extranetUser = (SystemUser) results.next();
      boolean include = true;

      if (firstName != null
          && !firstName.equals("")
          && extranetUser.getFirstName().toLowerCase().indexOf(firstName.toLowerCase()) == -1)
        include = false;

      if (lastName != null
          && !lastName.equals("")
          && extranetUser.getLastName().toLowerCase().indexOf(lastName.toLowerCase()) == -1)
        include = false;

      if (userName != null
          && !userName.equals("")
          && extranetUser.getUserName().toLowerCase().indexOf(userName.toLowerCase()) == -1)
        include = false;

      if (email != null
          && !email.equals("")
          && extranetUser.getEmail().toLowerCase().indexOf(email.toLowerCase()) == -1)
        include = false;

      boolean hasRoles = true;
      if (roleNames != null && roleNames.length > 0) {
        for (int i = 0; i < roleNames.length; i++) {
          String roleName = roleNames[i];
          if (roleName != null && !roleName.equals("")) {
            Collection roles = extranetUser.getRoles();
            Iterator rolesIterator = roles.iterator();
            boolean hasRole = false;
            while (rolesIterator.hasNext()) {
              Role role = (Role) rolesIterator.next();
              if (role.getRoleName().equalsIgnoreCase(roleName)) {
                hasRole = true;
                break;
              }
            }

            if (!hasRole) {
              hasRoles = false;
              break;
            }
          }
        }
      }

      if (include && hasRoles) filteredList.add(extranetUser);
    }

    results.close();
    oql.close();

    return filteredList;
  }