Search in sources :

Example 1 with ValidationResults

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);
}
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 2 with ValidationResults

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;
}
Also used : OrganizationRepository(py.org.fundacionparaguaya.pspserver.network.repositories.OrganizationRepository) Specifications.where(org.springframework.data.jpa.domain.Specifications.where) UserDetailsDTO(py.org.fundacionparaguaya.pspserver.security.dtos.UserDetailsDTO) SurveyEntity(py.org.fundacionparaguaya.pspserver.surveys.entities.SurveyEntity) SurveyMapper(py.org.fundacionparaguaya.pspserver.surveys.mapper.SurveyMapper) LocalDateTime(java.time.LocalDateTime) SchemaValidator.requiredValue(py.org.fundacionparaguaya.pspserver.surveys.validation.SchemaValidator.requiredValue) ArrayList(java.util.ArrayList) SurveySchema(py.org.fundacionparaguaya.pspserver.surveys.dtos.SurveySchema) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) ApplicationDTO(py.org.fundacionparaguaya.pspserver.network.dtos.ApplicationDTO) SurveyService(py.org.fundacionparaguaya.pspserver.surveys.services.SurveyService) MultipleSchemaValidator(py.org.fundacionparaguaya.pspserver.surveys.validation.MultipleSchemaValidator) ApplicationRepository(py.org.fundacionparaguaya.pspserver.network.repositories.ApplicationRepository) Service(org.springframework.stereotype.Service) SurveyOrganizationEntity(py.org.fundacionparaguaya.pspserver.network.entities.SurveyOrganizationEntity) SurveyOrganizationService(py.org.fundacionparaguaya.pspserver.network.services.SurveyOrganizationService) PropertyValidator.validType(py.org.fundacionparaguaya.pspserver.surveys.validation.PropertyValidator.validType) CustomParameterizedException(py.org.fundacionparaguaya.pspserver.common.exceptions.CustomParameterizedException) NewSnapshot(py.org.fundacionparaguaya.pspserver.surveys.dtos.NewSnapshot) OrganizationDTO(py.org.fundacionparaguaya.pspserver.network.dtos.OrganizationDTO) Property(py.org.fundacionparaguaya.pspserver.surveys.dtos.Property) PropertyAttributeSupport(py.org.fundacionparaguaya.pspserver.surveys.mapper.PropertyAttributeSupport) ValidationResults(py.org.fundacionparaguaya.pspserver.surveys.validation.ValidationResults) SurveyOrganizationSpecification.lastModifiedGt(py.org.fundacionparaguaya.pspserver.network.specifications.SurveyOrganizationSpecification.lastModifiedGt) NewSurveyDefinition(py.org.fundacionparaguaya.pspserver.surveys.dtos.NewSurveyDefinition) MultipleSchemaValidator.all(py.org.fundacionparaguaya.pspserver.surveys.validation.MultipleSchemaValidator.all) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) SurveyRepository(py.org.fundacionparaguaya.pspserver.surveys.repositories.SurveyRepository) ValidationResult(py.org.fundacionparaguaya.pspserver.surveys.validation.ValidationResult) Role(py.org.fundacionparaguaya.pspserver.security.constants.Role) Collectors(java.util.stream.Collectors) OrganizationMapper(py.org.fundacionparaguaya.pspserver.network.mapper.OrganizationMapper) SurveyOrganizationRepository(py.org.fundacionparaguaya.pspserver.network.repositories.SurveyOrganizationRepository) SurveyDefinition(py.org.fundacionparaguaya.pspserver.surveys.dtos.SurveyDefinition) UnknownResourceException(py.org.fundacionparaguaya.pspserver.common.exceptions.UnknownResourceException) ApplicationMapper(py.org.fundacionparaguaya.pspserver.network.mapper.ApplicationMapper) List(java.util.List) StopLightType(py.org.fundacionparaguaya.pspserver.surveys.entities.StopLightType) ValidationSupport(py.org.fundacionparaguaya.pspserver.surveys.validation.ValidationSupport) SurveyOrganizationSpecification.byOrganization(py.org.fundacionparaguaya.pspserver.network.specifications.SurveyOrganizationSpecification.byOrganization) SchemaValidator.presentInSchema(py.org.fundacionparaguaya.pspserver.surveys.validation.SchemaValidator.presentInSchema) Optional(java.util.Optional) SurveyOrganizationSpecification.byApplication(py.org.fundacionparaguaya.pspserver.network.specifications.SurveyOrganizationSpecification.byApplication) SchemaValidator.markedAsRequired(py.org.fundacionparaguaya.pspserver.surveys.validation.SchemaValidator.markedAsRequired) Transactional(org.springframework.transaction.annotation.Transactional) ValidationResults(py.org.fundacionparaguaya.pspserver.surveys.validation.ValidationResults) MultipleSchemaValidator(py.org.fundacionparaguaya.pspserver.surveys.validation.MultipleSchemaValidator)

Example 3 with ValidationResults

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;
}
Also used : ValidationResults(py.org.fundacionparaguaya.pspserver.surveys.validation.ValidationResults) SurveySchema(py.org.fundacionparaguaya.pspserver.surveys.dtos.SurveySchema) NewSurveyDefinition(py.org.fundacionparaguaya.pspserver.surveys.dtos.NewSurveyDefinition) SurveyDefinition(py.org.fundacionparaguaya.pspserver.surveys.dtos.SurveyDefinition) Property(py.org.fundacionparaguaya.pspserver.surveys.dtos.Property)

Example 4 with ValidationResults

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

Aggregations

ValidationResults (py.org.fundacionparaguaya.pspserver.surveys.validation.ValidationResults)4 CustomParameterizedException (py.org.fundacionparaguaya.pspserver.common.exceptions.CustomParameterizedException)3 NewSurveyDefinition (py.org.fundacionparaguaya.pspserver.surveys.dtos.NewSurveyDefinition)3 SurveyDefinition (py.org.fundacionparaguaya.pspserver.surveys.dtos.SurveyDefinition)3 Transactional (org.springframework.transaction.annotation.Transactional)2 ApplicationDTO (py.org.fundacionparaguaya.pspserver.network.dtos.ApplicationDTO)2 OrganizationDTO (py.org.fundacionparaguaya.pspserver.network.dtos.OrganizationDTO)2 SurveyOrganizationEntity (py.org.fundacionparaguaya.pspserver.network.entities.SurveyOrganizationEntity)2 Property (py.org.fundacionparaguaya.pspserver.surveys.dtos.Property)2 SurveySchema (py.org.fundacionparaguaya.pspserver.surveys.dtos.SurveySchema)2 SurveyEntity (py.org.fundacionparaguaya.pspserver.surveys.entities.SurveyEntity)2 Preconditions.checkArgument (com.google.common.base.Preconditions.checkArgument)1 Preconditions.checkNotNull (com.google.common.base.Preconditions.checkNotNull)1 LocalDateTime (java.time.LocalDateTime)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Optional (java.util.Optional)1 Collectors (java.util.stream.Collectors)1 Specifications.where (org.springframework.data.jpa.domain.Specifications.where)1 Service (org.springframework.stereotype.Service)1