/* (non-Javadoc)
   * @see #updateTaxMap(org.mifosplatform.infrastructure.core.api.JsonCommand, java.lang.Long)
   */
  @Transactional
  @Override
  public CommandProcessingResult updateTaxMap(final JsonCommand command, final Long taxMapId) {
    TaxMap taxMap = null;
    try {
      this.context.authenticatedUser();
      this.apiJsonDeserializer.validateForCreate(command);
      taxMap = retrieveTaxMapById(taxMapId);
      final Map<String, Object> changes = taxMap.update(command);

      if (!changes.isEmpty()) {
        this.taxMapRepository.saveAndFlush(taxMap);
      }

      return new CommandProcessingResultBuilder()
          .withCommandId(command.commandId())
          .withEntityId(taxMap.getId())
          .with(changes)
          .build();
    } catch (final DataIntegrityViolationException dve) {
      if (dve.getCause() instanceof ConstraintViolationException) {
        handleDataIntegrityIssues(command, dve);
      }
      return new CommandProcessingResult(Long.valueOf(-1));
    }
  }
  public void testInterceptorWithFlushFailure() throws Throwable {
    MockControl sfControl = MockControl.createControl(SessionFactory.class);
    SessionFactory sf = (SessionFactory) sfControl.getMock();
    MockControl sessionControl = MockControl.createControl(Session.class);
    Session session = (Session) sessionControl.getMock();
    sf.openSession();
    sfControl.setReturnValue(session, 1);
    session.getSessionFactory();
    sessionControl.setReturnValue(sf, 1);
    SQLException sqlEx = new SQLException("argh", "27");
    session.flush();
    ConstraintViolationException jdbcEx = new ConstraintViolationException("", sqlEx, null);
    sessionControl.setThrowable(jdbcEx, 1);
    session.close();
    sessionControl.setReturnValue(null, 1);
    sfControl.replay();
    sessionControl.replay();

    HibernateInterceptor interceptor = new HibernateInterceptor();
    interceptor.setSessionFactory(sf);
    try {
      interceptor.invoke(new TestInvocation(sf));
      fail("Should have thrown DataIntegrityViolationException");
    } catch (DataIntegrityViolationException ex) {
      // expected
      assertEquals(jdbcEx, ex.getCause());
    }

    sfControl.verify();
    sessionControl.verify();
  }
Exemplo n.º 3
0
  // 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());
    }
  }
  @ExceptionHandler({DataIntegrityViolationException.class})
  public void constraintViolation(HttpServletResponse response, DataIntegrityViolationException ex)
      throws IOException {
    String message;
    Throwable cause = ex.getCause();
    if (cause instanceof ConstraintViolationException) {
      ConstraintViolationException cEx = (ConstraintViolationException) cause;
      message =
          "Application already contain sent data (Constraint "
              + cEx.getConstraintName()
              + " violates).";
    } else {
      message = ex.getMessage();
    }

    response.sendError(CONFLICT.value(), message);
  }
Exemplo n.º 5
0
  public static String getDataIntegrityViolationExceptionCause(
      DataIntegrityViolationException pException) {

    String message = "";

    ConstraintViolationException mConstraintViolationException =
        (ConstraintViolationException) pException.getCause();
    if (mConstraintViolationException == null) return message;

    // La CAUSE de un ConstraintViolationException usualmente será un (PSQLException), pero por
    // motivos de seguridad, hasta no tener
    // claros todos los posibles casos, usaremos Throwable.

    Throwable mCause = mConstraintViolationException.getCause();
    if (mCause == null) return message;

    message = mCause.getMessage();

    return message;
  }