/**
   * Get the filtered list of database users
   *
   * @param duFilter The filter
   * @param bIsSearch True if the user used search filters, false otherwise
   * @param listUsers the initial list to filter
   * @param request HttpServletRequest
   * @param model Map
   * @param url UrlItem
   * @return the filtered list
   */
  public List<DatabaseUser> getFilteredUsersInterface(
      DatabaseUserFilter duFilter,
      boolean bIsSearch,
      List<DatabaseUser> listUsers,
      HttpServletRequest request,
      Map<String, Object> model,
      UrlItem url) {
    Plugin myLutecePlugin = PluginService.getPlugin(MyLutecePlugin.PLUGIN_NAME);
    List<DatabaseUser> filteredUsers = getListFilteredUsers(request, duFilter, listUsers);
    MyLuteceUserFieldFilter mlFieldFilter = new MyLuteceUserFieldFilter();
    mlFieldFilter.setMyLuteceUserFieldFilter(request, request.getLocale());

    List<IAttribute> listAttributes = AttributeHome.findAll(request.getLocale(), myLutecePlugin);

    for (IAttribute attribute : listAttributes) {
      List<AttributeField> listAttributeFields =
          AttributeFieldHome.selectAttributeFieldsByIdAttribute(
              attribute.getIdAttribute(), myLutecePlugin);
      attribute.setListAttributeFields(listAttributeFields);
    }

    String strSortSearchAttribute = StringUtils.EMPTY;

    if (bIsSearch) {
      duFilter.setUrlAttributes(url);

      if (duFilter.getUrlAttributes() != StringUtils.EMPTY) {
        strSortSearchAttribute = AMPERSAND + duFilter.getUrlAttributes();
      }

      mlFieldFilter.setUrlAttributes(url);

      if (mlFieldFilter.getUrlAttributes() != StringUtils.EMPTY) {
        strSortSearchAttribute += (AMPERSAND + mlFieldFilter.getUrlAttributes());
      }
    }

    model.put(MARK_SEARCH_IS_SEARCH, bIsSearch);
    model.put(MARK_SEARCH_USER_FILTER, duFilter);
    model.put(MARK_SORT_SEARCH_ATTRIBUTE, strSortSearchAttribute);
    model.put(MARK_SEARCH_MYLUTECE_USER_FIELD_FILTER, mlFieldFilter);
    model.put(MARK_ATTRIBUTES_LIST, listAttributes);

    return filteredUsers;
  }
  /**
   * Get a XML string describing a given user
   *
   * @param user The user to get the XML of.
   * @param bExportRoles True to export roles of the user, false otherwise.
   * @param bExportGroups True to export groups of the user, false otherwise.
   * @param bExportAttributes True to export attributes of the user, false otherwise.
   * @param listAttributes The list of attributes to export.
   * @param locale The locale
   * @return A string of XML with the information of the user.
   */
  public String getXmlFromUser(
      DatabaseUser user,
      boolean bExportRoles,
      boolean bExportGroups,
      boolean bExportAttributes,
      List<IAttribute> listAttributes,
      Locale locale) {
    Plugin databasePlugin = PluginService.getPlugin(DatabasePlugin.PLUGIN_NAME);
    Plugin mylutecePlugin = PluginService.getPlugin(MyLutecePlugin.PLUGIN_NAME);
    StringBuffer sbXml = new StringBuffer();
    DateFormat dateFormat = new SimpleDateFormat();

    XmlUtil.beginElement(sbXml, CONSTANT_XML_USER);
    XmlUtil.addElement(sbXml, CONSTANT_XML_ACCESS_CODE, user.getLogin());
    XmlUtil.addElement(sbXml, CONSTANT_XML_LAST_NAME, user.getLastName());
    XmlUtil.addElement(sbXml, CONSTANT_XML_FIRST_NAME, user.getFirstName());
    XmlUtil.addElement(sbXml, CONSTANT_XML_EMAIL, user.getEmail());
    XmlUtil.addElement(sbXml, CONSTANT_XML_STATUS, Integer.toString(user.getStatus()));

    String strPasswordMaxValidDate = StringUtils.EMPTY;

    if (user.getPasswordMaxValidDate() != null) {
      strPasswordMaxValidDate = dateFormat.format(user.getPasswordMaxValidDate());
    }

    XmlUtil.addElement(sbXml, CONSTANT_XML_PASSWORD_MAX_VALID_DATE, strPasswordMaxValidDate);

    String strAccountMaxValidDate = StringUtils.EMPTY;

    if (user.getAccountMaxValidDate() != null) {
      strAccountMaxValidDate = dateFormat.format(user.getAccountMaxValidDate());
    }

    XmlUtil.addElement(sbXml, CONSTANT_XML_ACCOUNT_MAX_VALID_DATE, strAccountMaxValidDate);

    if (bExportRoles) {
      List<String> listRoles = DatabaseHome.findUserRolesFromLogin(user.getLogin(), databasePlugin);
      XmlUtil.beginElement(sbXml, CONSTANT_XML_ROLES);

      for (String strRole : listRoles) {
        XmlUtil.addElement(sbXml, CONSTANT_XML_ROLE, strRole);
      }

      XmlUtil.endElement(sbXml, CONSTANT_XML_ROLES);
    }

    if (bExportGroups) {
      List<String> listGroups =
          DatabaseHome.findUserGroupsFromLogin(user.getLogin(), databasePlugin);
      XmlUtil.beginElement(sbXml, CONSTANT_XML_GROUPS);

      for (String strGoup : listGroups) {
        XmlUtil.addElement(sbXml, CONSTANT_XML_GROUP, strGoup);
      }

      XmlUtil.endElement(sbXml, CONSTANT_XML_GROUPS);
    }

    if (bExportAttributes) {
      XmlUtil.beginElement(sbXml, CONSTANT_XML_ATTRIBUTES);

      for (IAttribute attribute : listAttributes) {
        List<MyLuteceUserField> listUserFields =
            MyLuteceUserFieldHome.selectUserFieldsByIdUserIdAttribute(
                user.getUserId(), attribute.getIdAttribute(), mylutecePlugin);

        for (MyLuteceUserField userField : listUserFields) {
          XmlUtil.beginElement(sbXml, CONSTANT_XML_ATTRIBUTE);
          XmlUtil.addElement(
              sbXml, CONSTANT_XML_ATTRIBUTE_ID, Integer.toString(attribute.getIdAttribute()));
          XmlUtil.addElement(
              sbXml, CONSTANT_XML_ATTRIBUTE_FIELD_ID, userField.getAttributeField().getIdField());
          XmlUtil.addElement(sbXml, CONSTANT_XML_ATTRIBUTE_VALUE, userField.getValue());
          XmlUtil.endElement(sbXml, CONSTANT_XML_ATTRIBUTE);
        }
      }

      XmlUtil.endElement(sbXml, CONSTANT_XML_ATTRIBUTES);
    }

    XmlUtil.endElement(sbXml, CONSTANT_XML_USER);

    return sbXml.toString();
  }