Search in sources :

Example 1 with CustomParameterizedException

use of py.org.fundacionparaguaya.pspserver.common.exceptions.CustomParameterizedException in project FP-PSP-SERVER by FundacionParaguaya.

the class SnapshotDraftServiceImpl method updateSnapshotDraft.

@Override
public SnapshotDraft updateSnapshotDraft(Long id, SnapshotDraft snapshotDraft) {
    checkArgument(id != null && id > 0, i18n.translate("argument.nonNegative", id));
    checkArgument(snapshotDraft != null, i18n.translate("argument.notNull", snapshotDraft));
    SnapshotDraftEntity snapshotEntity = Optional.ofNullable(repository.findOne(id)).orElseThrow(() -> new CustomParameterizedException(i18n.translate("snapshotDraft.notExist", id)));
    if (snapshotEntity == null) {
        return mapper.entityToDto(new SnapshotDraftEntity());
    }
    snapshotEntity.setStateDraft(snapshotDraft.getStateDraft());
    if (snapshotDraft.getUserName() != null) {
        snapshotEntity.setUser(userRepository.findOneByUsername(snapshotDraft.getUserName()).orElseThrow(() -> new CustomParameterizedException(i18n.translate("user.notExist", snapshotDraft.getUserName()))));
    }
    snapshotEntity = repository.save(snapshotEntity);
    return mapper.entityToDto(snapshotEntity);
}
Also used : SnapshotDraftEntity(py.org.fundacionparaguaya.pspserver.surveys.entities.SnapshotDraftEntity) CustomParameterizedException(py.org.fundacionparaguaya.pspserver.common.exceptions.CustomParameterizedException)

Example 2 with CustomParameterizedException

use of py.org.fundacionparaguaya.pspserver.common.exceptions.CustomParameterizedException in project FP-PSP-SERVER by FundacionParaguaya.

the class SnapshotServiceImpl method addSurveySnapshot.

@Override
@Transactional
public Snapshot addSurveySnapshot(UserDetailsDTO details, NewSnapshot snapshot) {
    checkNotNull(snapshot);
    ValidationResults results = surveyService.checkSchemaCompliance(snapshot);
    if (!results.isValid()) {
        throw new CustomParameterizedException(i18n.translate("snapshot.invalid"), results.asMap());
    }
    SnapshotIndicatorEntity indicatorEntity = economicMapper.newSnapshotToIndicatorEntity(snapshot);
    PersonEntity personEntity = personMapper.snapshotPersonalToEntity(snapshot);
    FamilyEntity family = familyService.getOrCreateFamilyFromSnapshot(details, snapshot, personEntity);
    SnapshotEconomicEntity snapshotEconomicEntity = saveEconomic(snapshot, indicatorEntity, family);
    familyService.updateFamily(family.getFamilyId());
    return economicMapper.entityToDto(snapshotEconomicEntity);
}
Also used : FamilyEntity(py.org.fundacionparaguaya.pspserver.families.entities.FamilyEntity) ValidationResults(py.org.fundacionparaguaya.pspserver.surveys.validation.ValidationResults) SnapshotEconomicEntity(py.org.fundacionparaguaya.pspserver.surveys.entities.SnapshotEconomicEntity) CustomParameterizedException(py.org.fundacionparaguaya.pspserver.common.exceptions.CustomParameterizedException) SnapshotIndicatorEntity(py.org.fundacionparaguaya.pspserver.surveys.entities.SnapshotIndicatorEntity) PersonEntity(py.org.fundacionparaguaya.pspserver.families.entities.PersonEntity) Transactional(org.springframework.transaction.annotation.Transactional)

Example 3 with CustomParameterizedException

use of py.org.fundacionparaguaya.pspserver.common.exceptions.CustomParameterizedException in project FP-PSP-SERVER by FundacionParaguaya.

the class PasswordResetTokenServiceImpl method resetPassword.

@Override
public void resetPassword(String userEmail) {
    UserEntity user = null;
    try {
        user = userRepository.findUserByEmail(userEmail).get();
    } catch (NoSuchElementException e) {
        throw new CustomParameterizedException(i18n.translate("email.emailUserNotFound"));
    } catch (Exception ex) {
        throw new CustomParameterizedException(i18n.translate("email.errorResetMail"));
    }
    if (user == null) {
        throw new CustomParameterizedException(i18n.translate("email.emailUserNotFound"));
    }
    String token = UUID.randomUUID().toString();
    createPasswordResetTokenForUser(user, token);
    SimpleMailMessage template = new SimpleMailMessage();
    template.setText(loadTemplate(applicationProps.getTemplates().getResetMail()));
    String[] args = { i18n.translate("email.bodyTitle"), i18n.translate("email.bodyGreeting"), applicationProps.getClient().getLoginUrl() + "?" + MAIL_PARAM_TOKEN + "=" + token + "&" + MAIL_PARAM_ID + "=" + user.getId(), i18n.translate("email.bodyReset", user.getEmail()), i18n.translate("email.bodySign") };
    emailService.sendSimpleMessageUsingTemplate(user.getEmail(), i18n.translate("email.resetPassword"), template, args);
}
Also used : SimpleMailMessage(org.springframework.mail.SimpleMailMessage) CustomParameterizedException(py.org.fundacionparaguaya.pspserver.common.exceptions.CustomParameterizedException) UserEntity(py.org.fundacionparaguaya.pspserver.security.entities.UserEntity) NoSuchElementException(java.util.NoSuchElementException) NoSuchElementException(java.util.NoSuchElementException) CustomParameterizedException(py.org.fundacionparaguaya.pspserver.common.exceptions.CustomParameterizedException) IOException(java.io.IOException) UnknownResourceException(py.org.fundacionparaguaya.pspserver.common.exceptions.UnknownResourceException)

Example 4 with CustomParameterizedException

use of py.org.fundacionparaguaya.pspserver.common.exceptions.CustomParameterizedException in project FP-PSP-SERVER by FundacionParaguaya.

the class UserServiceImpl method addUser.

@Override
public UserDTO addUser(UserDTO userDTO) {
    userRepository.findOneByUsername(userDTO.getUsername()).ifPresent(user -> {
        throw new CustomParameterizedException("User already exists.", new ImmutableMultimap.Builder<String, String>().put("username", user.getUsername()).build().asMap());
    });
    UserEntity user = new UserEntity();
    BeanUtils.copyProperties(userDTO, user);
    UserEntity newUser = userRepository.save(user);
    return userMapper.entityToDto(newUser);
}
Also used : CustomParameterizedException(py.org.fundacionparaguaya.pspserver.common.exceptions.CustomParameterizedException) UserEntity(py.org.fundacionparaguaya.pspserver.security.entities.UserEntity)

Example 5 with CustomParameterizedException

use of py.org.fundacionparaguaya.pspserver.common.exceptions.CustomParameterizedException in project FP-PSP-SERVER by FundacionParaguaya.

the class SurveyServiceImpl method deleteSurvey.

@Override
public void deleteSurvey(Long surveyId) {
    try {
        Optional.ofNullable(repo.findOne(surveyId)).ifPresent(survey -> {
            surveyOrganizationRepo.deleteBySurveyId(survey.getId());
            repo.delete(survey);
        });
    } catch (Exception e) {
        throw new CustomParameterizedException("The survey can not be deleted!");
    }
}
Also used : CustomParameterizedException(py.org.fundacionparaguaya.pspserver.common.exceptions.CustomParameterizedException) CustomParameterizedException(py.org.fundacionparaguaya.pspserver.common.exceptions.CustomParameterizedException) UnknownResourceException(py.org.fundacionparaguaya.pspserver.common.exceptions.UnknownResourceException)

Aggregations

CustomParameterizedException (py.org.fundacionparaguaya.pspserver.common.exceptions.CustomParameterizedException)15 UserEntity (py.org.fundacionparaguaya.pspserver.security.entities.UserEntity)4 UnknownResourceException (py.org.fundacionparaguaya.pspserver.common.exceptions.UnknownResourceException)3 IOException (java.io.IOException)2 MessagingException (javax.mail.MessagingException)2 MimeMessage (javax.mail.internet.MimeMessage)2 MimeMessageHelper (org.springframework.mail.javamail.MimeMessageHelper)2 Transactional (org.springframework.transaction.annotation.Transactional)2 ApplicationEntity (py.org.fundacionparaguaya.pspserver.network.entities.ApplicationEntity)2 SnapshotIndicatorEntity (py.org.fundacionparaguaya.pspserver.surveys.entities.SnapshotIndicatorEntity)2 ValidationResults (py.org.fundacionparaguaya.pspserver.surveys.validation.ValidationResults)2 ImageDTO (py.org.fundacionparaguaya.pspserver.system.dtos.ImageDTO)2 ImmutableMultimap (com.google.common.collect.ImmutableMultimap)1 File (java.io.File)1 URL (java.net.URL)1 Calendar (java.util.Calendar)1 NoSuchElementException (java.util.NoSuchElementException)1 Matchers.containsString (org.hamcrest.Matchers.containsString)1 Test (org.junit.Test)1 WebMvcTest (org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest)1