@Override
  protected String getUserID(Credentials credentials) {
    if (!(credentials instanceof SimpleCredentials)) {
      return super.getUserID(credentials);
    }

    SimpleCredentials cred = (SimpleCredentials) credentials;
    String id = cred.getUserID();
    if (id == null) {
      return super.getUserID(credentials);
    }

    String key = id + String.valueOf(cred.getPassword());
    String userId = ids.get(key);
    if (userId != null) {
      return userId;
    }

    boolean realId = false;
    Object realIdAttr = cred.getAttribute(JackrabbitConstants.REAL_USER_ID_USED);
    if (realIdAttr instanceof String) {
      realId = Boolean.valueOf((String) realIdAttr);
    }

    if (!realId) {
      try {
        LoginTable loginTable = LoginDBHandler.getUserLoginByUserName(id);
        userId = loginTable == null ? null : String.valueOf(loginTable.getUserId());
      } catch (Exception e) {
        e.printStackTrace();
      }
    }

    if (userId == null) {
      userId = super.getUserID(credentials);
    }

    if (userId != null) {
      ids.put(key, userId);
    }

    return userId;
  }
  private void updatePreferences(IWContext iwc) throws Exception {
    LoginTable loginTable =
        LoginDBHandler.getUserLogin(((Integer) user.getPrimaryKey()).intValue());
    String login = loginTable.getUserLogin();
    String currentPassword = iwc.getParameter(PARAMETER_CURRENT_PASSWORD);
    String newPassword1 = iwc.getParameter(PARAMETER_NEW_PASSWORD);
    String newPassword2 = iwc.getParameter(PARAMETER_NEW_PASSWORD_REPEATED);

    String errorMessage = null;
    boolean updatePassword = false;

    try {

      // if authorized by bank id we allow the user change his preferences
      if (authorizedByBankID(iwc)) {

      } else if (requirePasswordVerification
          && !LoginDBHandler.verifyPassword(login, currentPassword)) {
        throw new Exception(localize(KEY_PASSWORD_INVALID, DEFAULT_PASSWORD_INVALID));
      }

      // Validate new password
      if (!newPassword1.equals("") || !newPassword2.equals("")) {
        if (newPassword1.equals("")) {
          throw new Exception(localize(KEY_PASSWORD_EMPTY, DEFAULT_PASSWORD_EMPTY));
        }
        if (newPassword2.equals("")) {
          throw new Exception(
              localize(KEY_PASSWORD_REPEATED_EMPTY, DEFAULT_PASSWORD_REPEATED_EMPTY));
        }
        if (!newPassword1.equals(newPassword2)) {
          throw new Exception(localize(KEY_PASSWORDS_NOT_SAME, DEFAULT_PASSWORDS_NOT_SAME));
        }
        if (newPassword1.length() < MIN_PASSWORD_LENGTH) {
          throw new Exception(localize(KEY_PASSWORD_TOO_SHORT, DEFAULT_PASSWORD_TOO_SHORT));
        }
        for (int i = 0; i < newPassword1.length(); i++) {
          char c = newPassword1.charAt(i);
          boolean isPasswordCharOK = false;
          if ((c >= 'a') && (c <= 'z')) {
            isPasswordCharOK = true;
          } else if ((c >= 'A') && (c <= 'Z')) {
            isPasswordCharOK = true;
          } else if ((c >= '0') && (c <= '9')) {
            isPasswordCharOK = true;
          } else if ((c == 'Œ') || (c == 'Š') || (c == 'š')) {
            isPasswordCharOK = true;
          } else if ((c == '?') || (c == '€') || (c == '…')) {
            isPasswordCharOK = true;
          }
          if (!isPasswordCharOK) {
            throw new Exception(localize(KEY_PASSWORD_CHAR_ILLEGAL, DEFAULT_PASSWORD_CHAR_ILLEGAL));
          }
        }
        updatePassword = true;
      }
    } catch (Exception e) {
      errorMessage = e.getMessage();
    }

    if (errorMessage != null) {
      add(getErrorText(" " + errorMessage));
    } else {
      // Ok to update preferences
      // UserBusiness ub = (UserBusiness) IBOLookup.getServiceInstance(iwc, UserBusiness.class);

      if (updatePassword) {
        LoginDBHandler.updateLogin(
            ((Integer) user.getPrimaryKey()).intValue(), login, newPassword1);
      }
    }
    drawForm(iwc);
    if (errorMessage == null) {
      add(new Break());
      add(getLocalizedText(KEY_PREFERENCES_SAVED, DEFAULT_PREFERENCES_SAVED));
    }
  }
  private void drawForm(IWContext iwc) {
    Form form = new Form();
    Table T = new Table();
    T.setCellpadding(2);
    T.setCellspacing(2);
    T.setBorder(0);
    form.add(T);

    Table table = new Table();
    //		table.setWidth(getWidth());
    table.setCellpadding(2);
    table.setCellspacing(2);
    table.setBorder(0);
    T.add(table, 1, 1);
    T.setWidth(2, 1, "20");

    T.setVerticalAlignment(1, 1, Table.VERTICAL_ALIGN_BOTTOM);
    T.setVerticalAlignment(3, 1, Table.VERTICAL_ALIGN_BOTTOM);

    int row = 1;

    String personalID =
        PersonalIDFormatter.format(
            user.getPersonalID(), iwc.getIWMainApplication().getSettings().getApplicationLocale());

    table.add(new Break(2), 1, row);
    table.add(getSmallHeader(localize(KEY_PID, DEFAULT_PID)), 1, row);

    if (user.getPersonalID() != null) {
      table.add(getSmallText(personalID), 2, row);
      table.setVerticalAlignment(1, row, Table.VERTICAL_ALIGN_BOTTOM);
      table.setVerticalAlignment(2, row, Table.VERTICAL_ALIGN_BOTTOM);
    }
    row++;
    table.add(getSmallHeader(localize(KEY_LOGIN, DEFAULT_LOGIN)), 1, row);
    LoginTable loginTable =
        LoginDBHandler.getUserLogin(((Integer) user.getPrimaryKey()).intValue());
    if (loginTable != null) {
      table.add(new HiddenInput(PARAMETER_OLD_LOGIN, loginTable.getUserLogin()), 2, row);
      table.add(getSmallText(loginTable.getUserLogin()), 2, row);
      table.setVerticalAlignment(1, row, Table.VERTICAL_ALIGN_BOTTOM);
      table.setVerticalAlignment(2, row, Table.VERTICAL_ALIGN_BOTTOM);
    }

    String valueCurrentPassword =
        iwc.getParameter(PARAMETER_CURRENT_PASSWORD) != null
            ? iwc.getParameter(PARAMETER_CURRENT_PASSWORD)
            : "";
    String valueNewPassword =
        iwc.getParameter(PARAMETER_NEW_PASSWORD) != null
            ? iwc.getParameter(PARAMETER_NEW_PASSWORD)
            : "";
    String valueNewPasswordRepeated =
        iwc.getParameter(PARAMETER_NEW_PASSWORD_REPEATED) != null
            ? iwc.getParameter(PARAMETER_NEW_PASSWORD_REPEATED)
            : "";

    // Text tLogin = getSmallHeader(localize(KEY_LOGIN, DEFAULT_LOGIN));
    Text tCurrentPassword =
        getSmallHeader(localize(KEY_CURRENT_PASSWORD, DEFAULT_CURRENT_PASSWORD));
    Text tNewPassword = getSmallHeader(localize(KEY_NEW_PASSWORD, DEFAULT_NEW_PASSWORD));
    Text tNewPasswordRepeated =
        getSmallHeader(localize(KEY_NEW_PASSWORD_REPEATED, DEFAULT_NEW_PASSWORD_REPEATED));

    PasswordInput tiCurrentPassword =
        (PasswordInput) getStyledInterface(new PasswordInput(PARAMETER_CURRENT_PASSWORD));
    if (valueCurrentPassword != null) {
      tiCurrentPassword.setValue(valueCurrentPassword);
    }
    PasswordInput tiNewPassword =
        (PasswordInput) getStyledInterface(new PasswordInput(PARAMETER_NEW_PASSWORD));
    if (valueNewPassword != null) {
      tiNewPassword.setValue(valueNewPassword);
    }
    PasswordInput tiNewPasswordRepeated =
        (PasswordInput) getStyledInterface(new PasswordInput(PARAMETER_NEW_PASSWORD_REPEATED));
    if (valueNewPasswordRepeated != null) {
      tiNewPasswordRepeated.setValue(valueNewPasswordRepeated);
    }

    SubmitButton sbUpdate =
        (SubmitButton)
            getStyledInterface(
                new SubmitButton(
                    localize(KEY_UPDATE, DEFAULT_UPDATE), PARAMETER_FORM_SUBMIT, "true"));

    row++;
    table.setHeight(row, 12);

    if (requirePasswordVerification) {
      row++;
      table.add(tCurrentPassword, 1, row);
      table.add(tiCurrentPassword, 2, row);
    }

    row++;
    table.add(tNewPassword, 1, row);
    table.add(tiNewPassword, 2, row);

    row++;
    table.add(tNewPasswordRepeated, 1, row);
    table.add(tiNewPasswordRepeated, 2, row);

    row++;
    table.setHeight(row, 12);

    row++;
    table.mergeCells(1, row, 2, row);
    table.setAlignment(1, row, Table.HORIZONTAL_ALIGN_RIGHT);

    table.add(Text.NON_BREAKING_SPACE, 1, row);
    table.add(sbUpdate, 1, row);

    add(form);
  }
  protected String getAttributeValue(IWContext iwc, User user, String alias, String type) {
    if (OpenIDConstants.ATTRIBUTE_ALIAS_EMAIL.equals(alias)
        || OpenIDConstants.ATTRIBUTE_TYPE_EMAIL.equals(type)) {
      Email email = null;
      try {
        email = getUserBusiness(iwc).getUsersMainEmail(user);
      } catch (NoEmailFoundException e) {
        /*No action...*/
      } catch (RemoteException e) {
        e.printStackTrace();
      }

      return (email != null ? email.getEmailAddress() : "");
    } else if (OpenIDConstants.ATTRIBUTE_ALIAS_PERSONAL_ID.equals(alias)
        || OpenIDConstants.ATTRIBUTE_TYPE_PERSONAL_ID.equals(type)) {
      return (user.getPersonalID() != null ? user.getPersonalID() : "");
    } else if (OpenIDConstants.ATTRIBUTE_ALIAS_FULL_NAME.equals(alias)
        || OpenIDConstants.ATTRIBUTE_TYPE_FULL_NAME.equals(type)) {
      return user.getName();
    } else if (OpenIDConstants.ATTRIBUTE_ALIAS_DATE_OF_BIRTH.equals(alias)
        || OpenIDConstants.ATTRIBUTE_TYPE_DATE_OF_BIRTH.equals(type)) {
      return (user.getDateOfBirth() != null
          ? new IWTimestamp(user.getDateOfBirth()).toSQLDateString()
          : "");
    } else if (OpenIDConstants.ATTRIBUTE_ALIAS_GENDER.equals(alias)
        || OpenIDConstants.ATTRIBUTE_TYPE_GENDER.equals(type)) {
      return (user.getGender() != null ? (user.getGender().isMaleGender() ? "M" : "F") : "");
    } else if (OpenIDConstants.ATTRIBUTE_ALIAS_NICKNAME.equals(alias)
        || OpenIDConstants.ATTRIBUTE_TYPE_FRIENDLY_NAME.equals(type)) {
      return LoginDBHandler.getUserLogin(user).getUserLogin();
    } else if (OpenIDConstants.ATTRIBUTE_ALIAS_POSTCODE.equals(alias)
        || OpenIDConstants.ATTRIBUTE_TYPE_POSTAL_CODE.equals(type)) {
      try {
        Address address = getUserBusiness(iwc).getUsersMainAddress(user);
        if (address != null) {
          PostalCode postal = address.getPostalCode();
          return postal != null ? postal.getPostalCode() : "";
        }
      } catch (RemoteException re) {
        re.printStackTrace();
      }
    } else if (OpenIDConstants.ATTRIBUTE_ALIAS_COUNTRY.equals(alias)
        || OpenIDConstants.ATTRIBUTE_TYPE_COUNTRY.equals(type)) {
      try {
        Address address = getUserBusiness(iwc).getUsersMainAddress(user);
        if (address != null) {
          return address.getCountry().getIsoAbbreviation();
        }
      } catch (RemoteException re) {
        re.printStackTrace();
      }
    } else if (OpenIDConstants.ATTRIBUTE_ALIAS_LANGUAGE.equals(alias)
        || OpenIDConstants.ATTRIBUTE_TYPE_LANGUAGE.equals(type)) {
      if (user.getPreferredLocale() != null) {
        Locale locale = LocaleUtil.getLocale(user.getPreferredLocale());
        return locale.getLanguage() + "-" + locale.getCountry();
      } else {
        try {
          Address address = getUserBusiness(iwc).getUsersMainAddress(user);
          if (address != null) {
            Country country = address.getCountry();
            return country.getIsoAbbreviation().toLowerCase() + "-" + country.getIsoAbbreviation();
          }
        } catch (RemoteException re) {
          re.printStackTrace();
        }
      }
    } else if (OpenIDConstants.ATTRIBUTE_ALIAS_TIMEZONE.equals(alias)
        || OpenIDConstants.ATTRIBUTE_TYPE_TIMEZONE.equals(type)) {
      try {
        Address address = getUserBusiness(iwc).getUsersMainAddress(user);
        if (address != null) {
          Country country = address.getCountry();
          Locale locale =
              new Locale(country.getIsoAbbreviation().toLowerCase(), country.getIsoAbbreviation());
          Calendar calendar = new GregorianCalendar(locale);

          return calendar.getTimeZone().getDisplayName(Locale.ENGLISH);
        }
      } catch (RemoteException re) {
        re.printStackTrace();
      }
    }

    return "";
  }