use of py.org.fundacionparaguaya.pspserver.surveys.validation.ValidationResults 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);
}
use of py.org.fundacionparaguaya.pspserver.surveys.validation.ValidationResults in project FP-PSP-SERVER by FundacionParaguaya.
the class SurveyServiceImpl method validateSchemas.
private ValidationResults validateSchemas(NewSurveyDefinition surveyDefinition) {
ValidationResults results = ValidationSupport.validResults();
MultipleSchemaValidator schemaValidator = all(presentInSchema(), markedAsRequired());
propertyAttributeSupport.getPropertyAttributes().stream().filter(attr -> attr.getStoptLightType() == StopLightType.MANDATORY).forEach(attr -> {
results.addAll(schemaValidator.apply(surveyDefinition.getSurveySchema(), attr.getPropertySchemaName(), null));
});
return results;
}
use of py.org.fundacionparaguaya.pspserver.surveys.validation.ValidationResults in project FP-PSP-SERVER by FundacionParaguaya.
the class SurveyServiceImpl method checkSchemaCompliance.
@Override
public ValidationResults checkSchemaCompliance(NewSnapshot snapshot) {
SurveyDefinition surveyDefinition = this.getSurveyDefinition(snapshot.getSurveyId());
SurveySchema schema = surveyDefinition.getSurveySchema();
ValidationResults results = ValidationSupport.validResults();
schema.getProperties().entrySet().stream().forEach(propertyEntry -> {
Property property = propertyEntry.getValue();
Object propertyValue = snapshot.getAllSurveyData().get(propertyEntry.getKey());
results.add(propertyValue != null ? validType().apply(property, propertyEntry.getKey(), propertyValue) : ValidationResult.valid());
results.add(requiredValue().apply(schema, propertyEntry.getKey(), propertyValue));
});
snapshot.getAllSurveyData().entrySet().stream().forEach(surveyData -> {
results.add(presentInSchema().apply(schema, surveyData.getKey(), surveyData.getValue()));
});
return results;
}
use of py.org.fundacionparaguaya.pspserver.surveys.validation.ValidationResults 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());
}
Aggregations