@Override
  public Element createVCard(String username, Element element) {
    Element vcard = null;
    try {
      defaultProvider.deleteVCard(username);
      vcard = defaultProvider.createVCard(username, element);
    } catch (AlreadyExistsException e) {
      e.printStackTrace();
      logger.error("AlreadyExistsException even afer delete is called!");
      if (username.compareToIgnoreCase(PA_USER) == 0) {
        return defaultProvider.loadVCard(username);
      }
    }

    if (username.compareToIgnoreCase(PA_USER) == 0) {
      return vcard;
    }

    return updateVCard(username, element);
  }
  /** Updates the vCard both in SipX and in the database. */
  @Override
  public Element updateVCard(String username, Element vCardElement) {
    if (username.compareToIgnoreCase(PA_USER) == 0) {
      try {
        return defaultProvider.updateVCard(username, vCardElement);
      } catch (NotFoundException e) {
        e.printStackTrace();
        logger.error("update " + PA_USER + "'s vcard failed!");
        return null;
      }
    }

    try {
      defaultProvider.updateVCard(username, vCardElement);
    } catch (NotFoundException e) {
      try {
        defaultProvider.createVCard(username, vCardElement);
      } catch (AlreadyExistsException e1) {
        logger.error("Failed to create vcard due to existing vcard found");
        e1.printStackTrace();
      }
      e.printStackTrace();
    }

    try {
      String sipUserName = getAORFromJABBERID(username);
      if (sipUserName != null) {

        int attempts = 0;
        boolean tryAgain;
        do {
          tryAgain = false;
          try {
            RestInterface.sendRequest(MODIFY_METHOD, vCardElement);
          } catch (ConnectException e) {
            try {
              Thread.sleep(ATTEMPT_INTERVAL);
            } catch (Exception e1) {
              e1.printStackTrace();
            }
            attempts++;
            tryAgain = true;
          }
        } while (attempts < MAX_ATTEMPTS && tryAgain);

        if (attempts >= MAX_ATTEMPTS)
          logger.error(
              "Failed to update contact info for user " + username + ", sipXconfig might be down");

        Element vcardAfterUpdate = cacheVCard(username);

        // If client doesn't set local avatar, use the avatar from sipx/gravatar.
        if (getAvatar(vCardElement) == null) {
          VCardManager.getInstance().reset();
          Util.updateAvatar(username, vcardAfterUpdate);
        }

        return vcardAfterUpdate;

      } else {
        logger.error("Failed to find a valid SIP account for user " + username);
        return vCardElement;
      }
    } catch (Exception ex) {
      logger.error("updateVCard failed! " + ex.getMessage());
      return vCardElement;
    }
  }