/**
   * This method sends an email to the given user with the link to reset his password
   *
   * @param user the user
   */
  @Override
  public void resetPassword(UserDTO user) {
    ResetPasswordCodeDAS resetCodeDAS = new ResetPasswordCodeDAS();
    // find previous passwordCode

    ResetPasswordCodeDTO resetCode = resetCodeDAS.findByUser(user);
    if (resetCode == null) {
      resetCode = new ResetPasswordCodeDTO();
      resetCode.setUser(user);
      resetCode.setDateCreated(new Date());
      resetCode.setToken(RandomStringUtils.random(32, true, true));
      resetCodeDAS.save(resetCode);
      resetCodeDAS.flush();
    } else {
      DateTime dateResetCode = new DateTime(resetCode.getDateCreated());
      DateTime today = DateTime.now();
      Duration duration = new Duration(dateResetCode, today);
      Long minutesDifference = duration.getStandardMinutes();
      Long expirationMinutes =
          PreferenceBL.getPreferenceValueAsIntegerOrZero(
                      user.getEntity().getId(),
                      CommonConstants.PREFERENCE_FORGOT_PASSWORD_EXPIRATION)
                  .longValue()
              * 60;
      if (minutesDifference > expirationMinutes) {
        resetCodeDAS.delete(resetCode);
        resetCodeDAS.flush();
        resetCode = new ResetPasswordCodeDTO();
        resetCode.setUser(user);
        resetCode.setDateCreated(new Date());
        resetCode.setToken(RandomStringUtils.random(32, true, true));
        resetCodeDAS.save(resetCode);
      }
    }

    try {
      new UserBL()
          .sendLostPassword(
              user.getCompany().getId(), user.getId(), 1, generateLink(resetCode.getToken()));

    } catch (SessionInternalError e) {
      LOG.error("Exception while sending notification : " + e.getMessage());
      throw new SessionInternalError("forgotPassword.notification.not.found");
    } catch (NotificationNotFoundException e) {
      e.printStackTrace();
    }
  }
  /**
   * Here we will have the initial credentials for a user be created
   *
   * @param user
   */
  @Override
  public void createPassword(UserDTO user) {
    ResetPasswordCodeDAS resetCodeDAS = new ResetPasswordCodeDAS();

    ResetPasswordCodeDTO resetCode = new ResetPasswordCodeDTO();
    resetCode.setUser(user);
    resetCode.setDateCreated(new Date());
    resetCode.setToken(RandomStringUtils.random(32, true, true));
    resetCodeDAS.save(resetCode);

    try {
      new UserBL()
          .sendCredentials(
              user.getCompany().getId(), user.getId(), 1, generateLink(resetCode.getToken()));
    } catch (SessionInternalError e) {
      LOG.error(e.getMessage(), e);
      throw new SessionInternalError("Exception while sending notification : " + e.getMessage());
    } catch (NotificationNotFoundException e) {
      LOG.error(e.getMessage(), e);
      throw new SessionInternalError("createCredentials.notification.not.found");
    }
  }