@Override
  @Transactional(readOnly = false)
  public CommandProcessingResult updatePassword(Long userId, final JsonCommand command) {
    User user = this.userRepository.findOne(userId);

    if (user == null) {
      throw new ResourceNotFoundException(
          "error.entity.user.not.found", "User with id " + userId + " not found", userId);
    }

    if (this.context.authenticatedUser().isPublicUser()
        || command.parameterExists(OldPasswordParamName)) {
      // validate old password
      String oldPassword = command.stringValueOfParameterNamed(OldPasswordParamName);
      if (!user.getPassword().equals(this.applicationPasswordEncoder.encode(oldPassword, user))) {
        throw new GeneralPlatformRuleException(
            "error.old.password.invalid", "Old Password is Incorrect");
      }
    }
    user.updatePasswordFromCommand(command);
    generateKeyUsedForPasswordSalting(user);
    final String encodePassword = this.applicationPasswordEncoder.encode(user);
    user.updatePassword(encodePassword);

    this.userRepository.saveAndFlush(user);

    return new CommandProcessingResultBuilder().withResourceIdAsString(user.getId()).build();
  }
  // public functions
  @Override
  @Transactional
  public CommandProcessingResult createPublicUser(final JsonCommand command) {
    try {
      this.userDataValidator.validateCreate(command.getJsonCommand());

      User user = User.fromJson(command, false, true);

      generateKeyUsedForPasswordSalting(user);
      final String encodePassword = this.applicationPasswordEncoder.encode(user);
      user.updatePassword(encodePassword);

      this.userRepository.save(user);

      final JsonElement element = this.fromJsonHelper.parse(command.getJsonCommand());
      final String returnUrl = this.fromJsonHelper.extractStringNamed(ReturnUrlParamName, element);

      final String email = user.getUsername();
      final SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
      final String nowDate = sf.format(DateTime.now().toDate());
      final String text = nowDate + email + nowDate + Math.random();

      final String otp = new String(Base64.encode(text.getBytes()));
      final UserOtp userOtp =
          UserOtp.createOtp(user, email, otp.substring(3, otp.length() - 5), returnUrl);

      this.userOtpRepository.save(userOtp);

      final String finalOtp = userOtp.getOtp();
      final String verificationLink =
          DefaultAppUrl + "userapi/activate" + "?e=" + email + "&uas=" + finalOtp;
      String toEmails[] = new String[] {email};
      this.emailSenderService.sendEmail(
          toEmails,
          null,
          null,
          EmailTemplates.activateUserEmailSubject(),
          EmailTemplates.activateUserEmailTemplate(user.getName(), verificationLink));
      return new CommandProcessingResultBuilder().withSuccessStatus().build();
    } catch (DataIntegrityViolationException ex) {
      ex.printStackTrace();
      final Throwable realCause = ex.getCause();
      if (realCause.getMessage().toLowerCase().contains("email")) {
        throw new PlatformDataIntegrityException(
            "error.msg.email.already.exist",
            "The email provided already exitst in the system." + realCause.getMessage());
      }
      throw new PlatformDataIntegrityException(
          "error.msg.unknown.data.integrity.issue",
          "Unknown data integrity issue with resource: " + realCause.getMessage());
    }
  }
  @Override
  @Transactional
  public CommandProcessingResult create(final JsonCommand command) {

    this.userDataValidator.validateCreate(command.getJsonCommand());

    User user = User.fromJson(command, true, false);

    generateKeyUsedForPasswordSalting(user);
    final String encodePassword = this.applicationPasswordEncoder.encode(user);
    user.updatePassword(encodePassword);

    this.userRepository.saveAndFlush(user);

    return new CommandProcessingResultBuilder().withResourceIdAsString(user.getId()).build();
  }
 @Override
 public String activateUser(final String email, final String otp) {
   try {
     final UserOtp userOtp = this.userOtpRepository.findUserOtpByUserNameAndOtp(email, otp);
     if (null == userOtp) {
       return null;
       // throw new ResourceNotFoundException(
       //		"error.entity.user.found", "User with given details does not exist");
     }
     final User user = userOtp.getThisUser();
     user.activate();
     this.userRepository.saveAndFlush(user);
     // after successful activation delete otp
     this.userOtpRepository.delete(userOtp);
     return userOtp.getReturnUrl();
   } catch (Exception e) {
     e.printStackTrace();
     return null;
   }
 }