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);
  }