Search in sources :

Example 6 with SurveyDefinition

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

the class SurveyServiceImpl method listSurveys.

@Override
public List<SurveyDefinition> listSurveys(UserDetailsDTO userDetails, String lastModifiedGt) {
    Long organizationId = Optional.ofNullable(userDetails.getOrganization()).orElse(new OrganizationDTO()).getId();
    Long applicationId = Optional.ofNullable(userDetails.getApplication()).orElse(new ApplicationDTO()).getId();
    if (userHasRole(userDetails, Role.ROLE_ROOT)) {
        return mapper.entityListToDtoList(repo.findAll());
    }
    List<SurveyDefinition> lista = mapper.entityListToDtoList(surveyOrganizationRepo.findAll(where(byApplication(applicationId)).and(byOrganization(organizationId)).and(lastModifiedGt(lastModifiedGt))).stream().map(e -> e.getSurvey()).collect(Collectors.toList()));
    List<SurveyDefinition> toRet = new ArrayList<>();
    for (SurveyDefinition survey : lista.stream().distinct().collect(Collectors.toList())) {
        survey.setOrganizations(organizationMapper.entityListToDtoList(surveyOrganizationRepo.findBySurveyId(survey.getId()).stream().map(o -> o.getOrganization()).collect(Collectors.toList())));
        toRet.add(survey);
    }
    return toRet;
}
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) ApplicationDTO(py.org.fundacionparaguaya.pspserver.network.dtos.ApplicationDTO) NewSurveyDefinition(py.org.fundacionparaguaya.pspserver.surveys.dtos.NewSurveyDefinition) SurveyDefinition(py.org.fundacionparaguaya.pspserver.surveys.dtos.SurveyDefinition) ArrayList(java.util.ArrayList) OrganizationDTO(py.org.fundacionparaguaya.pspserver.network.dtos.OrganizationDTO)

Example 7 with SurveyDefinition

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

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

SurveyDefinition (py.org.fundacionparaguaya.pspserver.surveys.dtos.SurveyDefinition)8 NewSurveyDefinition (py.org.fundacionparaguaya.pspserver.surveys.dtos.NewSurveyDefinition)6 ValidationResults (py.org.fundacionparaguaya.pspserver.surveys.validation.ValidationResults)6 CustomParameterizedException (py.org.fundacionparaguaya.pspserver.common.exceptions.CustomParameterizedException)5 Preconditions.checkNotNull (com.google.common.base.Preconditions.checkNotNull)4 ArrayList (java.util.ArrayList)4 List (java.util.List)4 Optional (java.util.Optional)4 Collectors (java.util.stream.Collectors)4 Specifications.where (org.springframework.data.jpa.domain.Specifications.where)4 Service (org.springframework.stereotype.Service)4 Transactional (org.springframework.transaction.annotation.Transactional)4 UnknownResourceException (py.org.fundacionparaguaya.pspserver.common.exceptions.UnknownResourceException)4 ApplicationDTO (py.org.fundacionparaguaya.pspserver.network.dtos.ApplicationDTO)4 OrganizationDTO (py.org.fundacionparaguaya.pspserver.network.dtos.OrganizationDTO)4 SurveyOrganizationEntity (py.org.fundacionparaguaya.pspserver.network.entities.SurveyOrganizationEntity)4 OrganizationMapper (py.org.fundacionparaguaya.pspserver.network.mapper.OrganizationMapper)4 OrganizationRepository (py.org.fundacionparaguaya.pspserver.network.repositories.OrganizationRepository)4 Role (py.org.fundacionparaguaya.pspserver.security.constants.Role)4 UserDetailsDTO (py.org.fundacionparaguaya.pspserver.security.dtos.UserDetailsDTO)4