Search in sources :

Example 1 with SurveyOrganizationEntity

use of py.org.fundacionparaguaya.pspserver.network.entities.SurveyOrganizationEntity in project FP-PSP-SERVER by FundacionParaguaya.

the class SurveyOrganizationServiceImpl method crudSurveyOrganization.

@Override
public void crudSurveyOrganization(UserDetailsDTO details, Long surveyId, SurveyDefinition surveyDefinition, SurveyEntity surveyEntity) {
    ApplicationEntity applicationEntity;
    SurveyOrganizationEntity surveyOrganizationEntity;
    List<SurveyOrganizationEntity> surveyOrganizationEntityList = new ArrayList<SurveyOrganizationEntity>();
    List<SurveyOrganizationEntity> tempSurveyOrganizationEntityList;
    ApplicationDTO applicationDTOCheck = details.getApplication();
    // is hub_admin
    if (applicationDTOCheck != null) {
        // deletes former organizations assigned to a survey
        repo.deleteBySurveyIdAndApplicationIdAndOrganizationIsNotNull(surveyId, applicationDTOCheck.getId());
        for (OrganizationDTO organizationDTO : surveyDefinition.getOrganizations()) {
            surveyOrganizationEntity = new SurveyOrganizationEntity();
            surveyOrganizationEntity.setOrganization(organizationRepo.findById(organizationDTO.getId()));
            surveyOrganizationEntity.setApplication(applicationRepo.findById(applicationDTOCheck.getId()));
            surveyOrganizationEntity.setSurvey(surveyEntity);
            surveyOrganizationEntityList.add(surveyOrganizationEntity);
        }
        // saves the new list of organizations assigned to a survey
        repo.save(surveyOrganizationEntityList);
    }
    // is root
    if (applicationDTOCheck == null) {
        for (ApplicationDTO applicationDTO : surveyDefinition.getApplications()) {
            // brings all associated surveys with the corresponding applications and organizations
            tempSurveyOrganizationEntityList = repo.findBySurveyIdAndApplicationId(surveyId, applicationDTO.getId());
            if (tempSurveyOrganizationEntityList != null && tempSurveyOrganizationEntityList.size() > 0) {
                surveyOrganizationEntityList.addAll(tempSurveyOrganizationEntityList);
            } else {
                applicationEntity = applicationRepo.findById(applicationDTO.getId());
                surveyOrganizationEntity = new SurveyOrganizationEntity();
                surveyOrganizationEntity.setApplication(applicationEntity);
                surveyOrganizationEntity.setSurvey(surveyEntity);
                surveyOrganizationEntityList.add(surveyOrganizationEntity);
            }
        }
        // deletes old application list
        repo.deleteBySurveyId(surveyId);
        repo.flush();
        // saves new application list
        repo.save(surveyOrganizationEntityList);
    }
}
Also used : ApplicationDTO(py.org.fundacionparaguaya.pspserver.network.dtos.ApplicationDTO) ApplicationEntity(py.org.fundacionparaguaya.pspserver.network.entities.ApplicationEntity) ArrayList(java.util.ArrayList) OrganizationDTO(py.org.fundacionparaguaya.pspserver.network.dtos.OrganizationDTO) SurveyOrganizationDTO(py.org.fundacionparaguaya.pspserver.network.dtos.SurveyOrganizationDTO) SurveyOrganizationEntity(py.org.fundacionparaguaya.pspserver.network.entities.SurveyOrganizationEntity)

Example 2 with SurveyOrganizationEntity

use of py.org.fundacionparaguaya.pspserver.network.entities.SurveyOrganizationEntity in project FP-PSP-SERVER by FundacionParaguaya.

the class SurveyOrganizationServiceImpl method addSurveyOrganization.

@Override
public SurveyOrganizationDTO addSurveyOrganization(SurveyOrganizationDTO surveyOrganization) {
    SurveyOrganizationEntity entity = new SurveyOrganizationEntity();
    entity.setSurvey(surveyRepo.findOne(surveyOrganization.getSurvey().getId()));
    entity.setOrganization(organizationRepo.findOne(surveyOrganization.getOrganization().getId()));
    return mapper.entityToDto(repo.save(entity));
}
Also used : SurveyOrganizationEntity(py.org.fundacionparaguaya.pspserver.network.entities.SurveyOrganizationEntity)

Example 3 with SurveyOrganizationEntity

use of py.org.fundacionparaguaya.pspserver.network.entities.SurveyOrganizationEntity 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

SurveyOrganizationEntity (py.org.fundacionparaguaya.pspserver.network.entities.SurveyOrganizationEntity)3 ApplicationDTO (py.org.fundacionparaguaya.pspserver.network.dtos.ApplicationDTO)2 OrganizationDTO (py.org.fundacionparaguaya.pspserver.network.dtos.OrganizationDTO)2 ArrayList (java.util.ArrayList)1 CustomParameterizedException (py.org.fundacionparaguaya.pspserver.common.exceptions.CustomParameterizedException)1 SurveyOrganizationDTO (py.org.fundacionparaguaya.pspserver.network.dtos.SurveyOrganizationDTO)1 ApplicationEntity (py.org.fundacionparaguaya.pspserver.network.entities.ApplicationEntity)1 NewSurveyDefinition (py.org.fundacionparaguaya.pspserver.surveys.dtos.NewSurveyDefinition)1 SurveyDefinition (py.org.fundacionparaguaya.pspserver.surveys.dtos.SurveyDefinition)1 SurveyEntity (py.org.fundacionparaguaya.pspserver.surveys.entities.SurveyEntity)1 ValidationResults (py.org.fundacionparaguaya.pspserver.surveys.validation.ValidationResults)1