Search in sources :

Example 6 with CustomParameterizedException

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

the class SnapshotIndicatorPriorityServiceImpl method addSnapshotIndicatorPriority.

@Override
@Transactional
public SnapshotIndicatorPriority addSnapshotIndicatorPriority(SnapshotIndicatorPriority priority) {
    checkArgument(priority != null, i18n.translate("argument.notNull", priority));
    checkArgument(priority.getSnapshotIndicatorId() > 0, "Argument was %s but expected nonnegative", priority.getSnapshotIndicatorId());
    if (!priority.getIsAttainment() && snapshotPriorityRepository.countAllBySnapshotIndicatorIdAndIsAttainmentFalse(priority.getSnapshotIndicatorId()) >= 5) {
        throw new CustomParameterizedException(i18n.translate("snapshotPriority.onlyFivePriorities"));
    }
    SnapshotIndicatorPriorityEntity entity = new SnapshotIndicatorPriorityEntity();
    entity.setReason(priority.getReason());
    entity.setAction(priority.getAction());
    entity.setIndicator(priority.getIndicator());
    entity.setIsAttainment(priority.getIsAttainment());
    entity.setEstimatedDateAsISOString(priority.getEstimatedDate());
    SnapshotIndicatorEntity indicator = snapshotIndicatorRepository.getOne(priority.getSnapshotIndicatorId());
    entity.setSnapshotIndicator(indicator);
    SnapshotIndicatorPriorityEntity newSnapshotIndicatorPriority = snapshotPriorityRepository.save(entity);
    // We publish this event so that other components can
    // execute some operations on other entities, like an update
    // on the familiy#lastmModifiedAt property:
    // https://github.com/FundacionParaguaya/FP-PSP-SERVER/issues/134
    // In this way we only need one extra dependency in this service.
    publisher.publishEvent(PriorityCreatedEvent.of(indicator));
    return snapshotPriorityMapper.entityToDto(newSnapshotIndicatorPriority);
}
Also used : SnapshotIndicatorPriorityEntity(py.org.fundacionparaguaya.pspserver.surveys.entities.SnapshotIndicatorPriorityEntity) CustomParameterizedException(py.org.fundacionparaguaya.pspserver.common.exceptions.CustomParameterizedException) SnapshotIndicatorEntity(py.org.fundacionparaguaya.pspserver.surveys.entities.SnapshotIndicatorEntity) Transactional(org.springframework.transaction.annotation.Transactional)

Example 7 with CustomParameterizedException

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

the class OrganizationServiceImpl method addOrganization.

@Override
public OrganizationDTO addOrganization(OrganizationDTO organizationDTO) {
    organizationRepository.findOneByName(organizationDTO.getName()).ifPresent(organization -> {
        throw new CustomParameterizedException("Organisation already exists", new ImmutableMultimap.Builder<String, String>().put("name", organization.getName()).build().asMap());
    });
    OrganizationEntity organization = new OrganizationEntity();
    BeanUtils.copyProperties(organizationDTO, organization);
    ApplicationEntity application = applicationRepository.findById(organizationDTO.getApplication().getId());
    organization.setApplication(application);
    organization.setActive(true);
    if (organizationDTO.getFile() != null) {
        ImageDTO imageDTO = ImageParser.parse(organizationDTO.getFile(), applicationProperties.getAws().getOrgsImageDirectory());
        String generatedURL = imageUploadService.uploadImage(imageDTO);
        organization.setLogoUrl(generatedURL);
    }
    return organizationMapper.entityToDto(organizationRepository.save(organization));
}
Also used : CustomParameterizedException(py.org.fundacionparaguaya.pspserver.common.exceptions.CustomParameterizedException) OrganizationEntity(py.org.fundacionparaguaya.pspserver.network.entities.OrganizationEntity) ApplicationEntity(py.org.fundacionparaguaya.pspserver.network.entities.ApplicationEntity) ImageDTO(py.org.fundacionparaguaya.pspserver.system.dtos.ImageDTO)

Example 8 with CustomParameterizedException

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

the class SurveyServiceImpl method addSurveyDefinition.

@Override
public SurveyDefinition addSurveyDefinition(NewSurveyDefinition surveyDefinition) {
    ValidationResults results = validateSchemas(surveyDefinition);
    if (!results.isValid()) {
        throw new CustomParameterizedException("Invalid Survey Schema", results.asMap());
    }
    SurveyEntity entity = this.repo.save(SurveyEntity.of(surveyDefinition.getTitle(), surveyDefinition.getDescription(), new SurveyDefinition().surveySchema(surveyDefinition.getSurveySchema()).surveyUISchema(surveyDefinition.getSurveyUISchema())));
    if (surveyDefinition.getOrganizations() != null && surveyDefinition.getOrganizations().size() > 0) {
        for (OrganizationDTO organization : surveyDefinition.getOrganizations()) {
            if (surveyOrganizationRepo.findBySurveyIdAndApplicationIdAndOrganizationId(entity.getId(), organization.getApplication().getId(), organization.getId()) == null) {
                SurveyOrganizationEntity surveyOrganization = new SurveyOrganizationEntity();
                surveyOrganization.setSurvey(entity);
                surveyOrganization.setApplication(applicationRepo.findById(organization.getApplication().getId()));
                surveyOrganization.setOrganization(organizationRepo.findById(organization.getId()));
                surveyOrganizationRepo.save(surveyOrganization);
            }
        }
    }
    if (surveyDefinition.getApplications() != null) {
        for (ApplicationDTO application : surveyDefinition.getApplications()) {
            SurveyOrganizationEntity surveyOrganization = new SurveyOrganizationEntity();
            surveyOrganization.setSurvey(entity);
            surveyOrganization.setApplication(applicationRepo.findById(application.getId()));
            surveyOrganizationRepo.save(surveyOrganization);
        }
    }
    return new SurveyDefinition().id(entity.getId()).title(entity.getTitle()).description(entity.getDescription()).surveySchema(entity.getSurveyDefinition().getSurveySchema()).surveyUISchema(entity.getSurveyDefinition().getSurveyUISchema()).organizations(surveyDefinition.getOrganizations());
}
Also used : ApplicationDTO(py.org.fundacionparaguaya.pspserver.network.dtos.ApplicationDTO) ValidationResults(py.org.fundacionparaguaya.pspserver.surveys.validation.ValidationResults) SurveyEntity(py.org.fundacionparaguaya.pspserver.surveys.entities.SurveyEntity) CustomParameterizedException(py.org.fundacionparaguaya.pspserver.common.exceptions.CustomParameterizedException) NewSurveyDefinition(py.org.fundacionparaguaya.pspserver.surveys.dtos.NewSurveyDefinition) SurveyDefinition(py.org.fundacionparaguaya.pspserver.surveys.dtos.SurveyDefinition) OrganizationDTO(py.org.fundacionparaguaya.pspserver.network.dtos.OrganizationDTO) SurveyOrganizationEntity(py.org.fundacionparaguaya.pspserver.network.entities.SurveyOrganizationEntity)

Example 9 with CustomParameterizedException

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

the class UserControllerTest method requestingPostUserShouldFailIfUserAlreadyExists.

@Test
public void requestingPostUserShouldFailIfUserAlreadyExists() throws Exception {
    UserDTO dto = UserDTO.builder().username("admin").email("foo@bar").pass("123").build();
    when(userService.addUser(anyObject())).thenThrow(new CustomParameterizedException("User already exists.", new ImmutableMultimap.Builder<String, String>().put("username", dto.getUsername()).build().asMap()));
    String json = TestHelper.mapToJson(dto);
    mockMvc.perform(post("/api/v1/users").content(json).contentType(MediaType.APPLICATION_JSON)).andDo(print()).andExpect(status().isBadRequest()).andExpect(jsonPath("$.message", containsString("User already exists."))).andExpect(jsonPath("$.fieldErrors.[0].field", is("username"))).andExpect(jsonPath("$.fieldErrors.[0].messages[0]").value("admin"));
}
Also used : CustomParameterizedException(py.org.fundacionparaguaya.pspserver.common.exceptions.CustomParameterizedException) UserDTO(py.org.fundacionparaguaya.pspserver.security.dtos.UserDTO) ImmutableMultimap(com.google.common.collect.ImmutableMultimap) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.Test) WebMvcTest(org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest)

Example 10 with CustomParameterizedException

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

the class ApplicationServiceImpl method addApplication.

@Override
public ApplicationDTO addApplication(ApplicationDTO applicationDTO) {
    applicationRepository.findOneByName(applicationDTO.getName()).ifPresent(application -> {
        throw new CustomParameterizedException("Application already exists", new ImmutableMultimap.Builder<String, String>().put("name", application.getName()).build().asMap());
    });
    ApplicationEntity application = new ApplicationEntity();
    BeanUtils.copyProperties(applicationDTO, application);
    application.setHub(true);
    application.setActive(true);
    if (applicationDTO.getFile() != null) {
        ImageDTO imageDTO = ImageParser.parse(applicationDTO.getFile(), applicationProperties.getAws().getHubsImageDirectory());
        String generatedURL = imageUploadService.uploadImage(imageDTO);
        application.setLogoUrl(generatedURL);
    }
    return applicationMapper.entityToDto(applicationRepository.save(application));
}
Also used : CustomParameterizedException(py.org.fundacionparaguaya.pspserver.common.exceptions.CustomParameterizedException) ApplicationEntity(py.org.fundacionparaguaya.pspserver.network.entities.ApplicationEntity) ImageDTO(py.org.fundacionparaguaya.pspserver.system.dtos.ImageDTO)

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