Search in sources :

Example 1 with NewSurveyDefinition

use of py.org.fundacionparaguaya.pspserver.surveys.dtos.NewSurveyDefinition 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 2 with NewSurveyDefinition

use of py.org.fundacionparaguaya.pspserver.surveys.dtos.NewSurveyDefinition 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 3 with NewSurveyDefinition

use of py.org.fundacionparaguaya.pspserver.surveys.dtos.NewSurveyDefinition in project FP-PSP-SERVER by FundacionParaguaya.

the class SurveyController method addSurveyDefinition.

@PostMapping
@io.swagger.annotations.ApiOperation(value = "Create Survey Definition", notes = "Creates a new survey definition", response = SurveyDefinition.class, tags = {})
@io.swagger.annotations.ApiResponses(value = { @io.swagger.annotations.ApiResponse(code = 201, message = "The created survey definition", response = SurveyDefinition.class) })
public ResponseEntity addSurveyDefinition(@RequestBody NewSurveyDefinition surveyDefinition) throws NotFoundException, URISyntaxException {
    SurveyDefinition definition = surveyService.addSurveyDefinition(surveyDefinition);
    URI surveyLocation = new URI("/surveys/" + definition.getId());
    return ResponseEntity.created(surveyLocation).body(definition);
}
Also used : NewSurveyDefinition(py.org.fundacionparaguaya.pspserver.surveys.dtos.NewSurveyDefinition) SurveyDefinition(py.org.fundacionparaguaya.pspserver.surveys.dtos.SurveyDefinition) URI(java.net.URI) PostMapping(org.springframework.web.bind.annotation.PostMapping)

Aggregations

NewSurveyDefinition (py.org.fundacionparaguaya.pspserver.surveys.dtos.NewSurveyDefinition)3 SurveyDefinition (py.org.fundacionparaguaya.pspserver.surveys.dtos.SurveyDefinition)3 CustomParameterizedException (py.org.fundacionparaguaya.pspserver.common.exceptions.CustomParameterizedException)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 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 URI (java.net.URI)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 Transactional (org.springframework.transaction.annotation.Transactional)1 PostMapping (org.springframework.web.bind.annotation.PostMapping)1 UnknownResourceException (py.org.fundacionparaguaya.pspserver.common.exceptions.UnknownResourceException)1