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);
}
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));
}
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());
}
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"));
}
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));
}
Aggregations