Search in sources :

Example 11 with SnapshotEconomicEntity

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

the class SnapshotReportManagerImpl method listSnapshotByFamily.

@Override
public List<FamilySnapshotDTO> listSnapshotByFamily(SnapshotFilterDTO filters) {
    List<FamilySnapshotDTO> toRet = new ArrayList<>();
    Sort sort = new Sort(new Sort.Order(Direction.ASC, "createdAt"));
    if (filters.getDateFrom() != null && filters.getDateTo() != null && filters.getFamilyId() != null) {
        List<SnapshotEconomicEntity> snapshots = snapshotRepository.findAll(where(forFamily(filters.getFamilyId())).and(SnapshotEconomicSpecification.createdAtBetween2Dates(filters.getDateFrom(), filters.getDateTo())), sort);
        Map<SurveyEntity, List<SnapshotEconomicEntity>> groupBySurvey = snapshots.stream().collect(Collectors.groupingBy(s -> s.getSurveyDefinition()));
        groupBySurvey.forEach((k, v) -> {
            FamilySnapshotDTO familySnapshots = new FamilySnapshotDTO(filters.getFamilyId(), k.getTitle());
            familySnapshots.setSnapshots(getSnasphots(v));
            toRet.add(familySnapshots);
        });
    }
    return toRet;
}
Also used : Specifications.where(org.springframework.data.jpa.domain.Specifications.where) Arrays(java.util.Arrays) FamilyRepository(py.org.fundacionparaguaya.pspserver.families.repositories.FamilyRepository) SurveyEntity(py.org.fundacionparaguaya.pspserver.surveys.entities.SurveyEntity) SnapshotFilterDTO(py.org.fundacionparaguaya.pspserver.reports.dtos.SnapshotFilterDTO) FamilySpecification.byOrganization(py.org.fundacionparaguaya.pspserver.families.specifications.FamilySpecification.byOrganization) SurveyData(py.org.fundacionparaguaya.pspserver.surveys.dtos.SurveyData) OrganizationFamilyDTO(py.org.fundacionparaguaya.pspserver.reports.dtos.OrganizationFamilyDTO) FamilyEntity(py.org.fundacionparaguaya.pspserver.families.entities.FamilyEntity) ArrayList(java.util.ArrayList) SnapshotEconomicSpecification(py.org.fundacionparaguaya.pspserver.surveys.specifications.SnapshotEconomicSpecification) Service(org.springframework.stereotype.Service) Map(java.util.Map) Sort(org.springframework.data.domain.Sort) Direction(org.springframework.data.domain.Sort.Direction) SnapshotReportManager(py.org.fundacionparaguaya.pspserver.reports.services.SnapshotReportManager) OrganizationEntity(py.org.fundacionparaguaya.pspserver.network.entities.OrganizationEntity) ReportDTO(py.org.fundacionparaguaya.pspserver.reports.dtos.ReportDTO) StringConverter(py.org.fundacionparaguaya.pspserver.common.utils.StringConverter) FamilySnapshotDTO(py.org.fundacionparaguaya.pspserver.reports.dtos.FamilySnapshotDTO) Collectors(java.util.stream.Collectors) FamilyDTOMapper(py.org.fundacionparaguaya.pspserver.reports.mapper.FamilyDTOMapper) SnapshotEconomicRepository(py.org.fundacionparaguaya.pspserver.surveys.repositories.SnapshotEconomicRepository) List(java.util.List) SnapshotEconomicEntity(py.org.fundacionparaguaya.pspserver.surveys.entities.SnapshotEconomicEntity) FamilySpecification(py.org.fundacionparaguaya.pspserver.families.specifications.FamilySpecification) Specification(org.springframework.data.jpa.domain.Specification) FamilySpecification.byApplication(py.org.fundacionparaguaya.pspserver.families.specifications.FamilySpecification.byApplication) SnapshotIndicatorMapper(py.org.fundacionparaguaya.pspserver.surveys.mapper.SnapshotIndicatorMapper) SnapshotEconomicSpecification.forFamily(py.org.fundacionparaguaya.pspserver.surveys.specifications.SnapshotEconomicSpecification.forFamily) FamilySnapshotDTO(py.org.fundacionparaguaya.pspserver.reports.dtos.FamilySnapshotDTO) SnapshotEconomicEntity(py.org.fundacionparaguaya.pspserver.surveys.entities.SnapshotEconomicEntity) SurveyEntity(py.org.fundacionparaguaya.pspserver.surveys.entities.SurveyEntity) ArrayList(java.util.ArrayList) Sort(org.springframework.data.domain.Sort) ArrayList(java.util.ArrayList) List(java.util.List)

Example 12 with SnapshotEconomicEntity

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

the class FamilySpecification method createdAtBetween2Dates.

public static Specification<FamilyEntity> createdAtBetween2Dates(String dateFrom, String dateTo) {
    return new Specification<FamilyEntity>() {

        @Override
        public Predicate toPredicate(Root<FamilyEntity> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
            List<Predicate> predicates = new ArrayList<>();
            if (dateFrom != null && dateTo != null) {
                DateTimeFormatter formatter = DateTimeFormatter.ofPattern(SHORT_DATE_FORMAT);
                Subquery<SnapshotEconomicEntity> subquery = query.subquery(SnapshotEconomicEntity.class);
                Root<SnapshotEconomicEntity> fromSnapshot = subquery.from(SnapshotEconomicEntity.class);
                subquery.select(fromSnapshot.get(SnapshotEconomicEntity_.getFamily()).get(ID_FAMILY));
                predicates.add(cb.greaterThanOrEqualTo(fromSnapshot.get(SnapshotEconomicEntity_.getCreatedAt()), LocalDate.parse(dateFrom, formatter).atStartOfDay()));
                predicates.add(cb.lessThan(fromSnapshot.get(SnapshotEconomicEntity_.getCreatedAt()), LocalDate.parse(dateTo, formatter).plusDays(1).atStartOfDay()));
                subquery.where(cb.and(predicates.toArray(new Predicate[predicates.size()])));
                return cb.in(root.get(ID_FAMILY)).value(subquery);
            }
            return cb.and(predicates.toArray(new Predicate[predicates.size()]));
        }
    };
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) Root(javax.persistence.criteria.Root) SnapshotEconomicEntity(py.org.fundacionparaguaya.pspserver.surveys.entities.SnapshotEconomicEntity) CriteriaQuery(javax.persistence.criteria.CriteriaQuery) ArrayList(java.util.ArrayList) Specification(org.springframework.data.jpa.domain.Specification) DateTimeFormatter(java.time.format.DateTimeFormatter) Predicate(javax.persistence.criteria.Predicate)

Example 13 with SnapshotEconomicEntity

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

the class SnapshotServiceImpl method deleteSnapshotById.

@Override
public void deleteSnapshotById(Long snapshotEconomicId) {
    SnapshotEconomicEntity snapshotEconomicEntity = economicRepository.findOne(snapshotEconomicId);
    if (snapshotEconomicEntity == null) {
        return;
    }
    Long familyId = null;
    if (snapshotEconomicEntity.getFamily() != null) {
        familyId = snapshotEconomicEntity.getFamily().getFamilyId();
    }
    priorityService.deletePrioritiesByIndicator(snapshotEconomicEntity.getSnapshotIndicator().getId());
    economicRepository.delete(snapshotEconomicEntity);
    if (familyId != null && economicRepository.findByFamilyFamilyId(familyId).size() == 0) {
        familyService.deleteFamily(familyId);
    }
}
Also used : SnapshotEconomicEntity(py.org.fundacionparaguaya.pspserver.surveys.entities.SnapshotEconomicEntity)

Example 14 with SnapshotEconomicEntity

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

the class OrganizationServiceImpl method countSnapshotIndicators.

private SnapshotIndicators countSnapshotIndicators(Long organizationId) {
    List<FamilyEntity> families = familyService.findByOrganizationId(organizationId);
    List<SnapshotEconomicEntity> snapshotEconomics = snapshotEconomicRepo.findByFamilyIn(families);
    List<SnapshotIndicatorEntity> entityList = new ArrayList<SnapshotIndicatorEntity>();
    for (SnapshotEconomicEntity economics : snapshotEconomics) {
        entityList.add(economics.getSnapshotIndicator());
    }
    SnapshotIndicators indicators = new SnapshotIndicators();
    List<SurveyData> listProperties = indicatorMapper.entityListToDtoList(entityList);
    for (SurveyData properties : listProperties) {
        properties.forEach((k, v) -> {
            countIndicators(indicators, v);
        });
    }
    return indicators;
}
Also used : FamilyEntity(py.org.fundacionparaguaya.pspserver.families.entities.FamilyEntity) SnapshotIndicators(py.org.fundacionparaguaya.pspserver.surveys.dtos.SnapshotIndicators) SnapshotEconomicEntity(py.org.fundacionparaguaya.pspserver.surveys.entities.SnapshotEconomicEntity) ArrayList(java.util.ArrayList) SnapshotIndicatorEntity(py.org.fundacionparaguaya.pspserver.surveys.entities.SnapshotIndicatorEntity) SurveyData(py.org.fundacionparaguaya.pspserver.surveys.dtos.SurveyData)

Example 15 with SnapshotEconomicEntity

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

the class SnapshotReportManagerImpl method getOrganizationAndFamilyData.

private ReportDTO getOrganizationAndFamilyData(List<SnapshotEconomicEntity> snapshots) {
    ReportDTO report = new ReportDTO();
    report.getHeaders().addAll(DEFAULT_HEADRES);
    List<SurveyData> rows = new ArrayList<>();
    report.getHeaders().addAll(snapshotMapper.getStaticPropertiesNames());
    for (SnapshotEconomicEntity s : snapshots) {
        s.getSnapshotIndicator().getAdditionalProperties().forEach((k, v) -> {
            if (!report.getHeaders().contains(k)) {
                report.getHeaders().add(StringConverter.getNameFromCamelCase(k));
            }
        });
        SurveyData data = snapshotMapper.entityToDto(s.getSnapshotIndicator());
        data.put("organizationCode", s.getFamily().getOrganization().getCode());
        data.put("organizationName", s.getFamily().getOrganization().getName());
        data.put("organizationStatus", s.getFamily().getOrganization().getStatus().toString());
        data.put("familyCode", s.getFamily().getCode());
        data.put("familyName", s.getFamily().getName());
        data.put("familyStatus", s.getFamily().getStatus().toString());
        data.put("snapshotCreatedAt", s.getCreatedAtLocalDateString());
        rows.add(data);
    }
    report.setRows(generateRows(rows, report.getHeaders()));
    return report;
}
Also used : SnapshotEconomicEntity(py.org.fundacionparaguaya.pspserver.surveys.entities.SnapshotEconomicEntity) ArrayList(java.util.ArrayList) ReportDTO(py.org.fundacionparaguaya.pspserver.reports.dtos.ReportDTO) SurveyData(py.org.fundacionparaguaya.pspserver.surveys.dtos.SurveyData)

Aggregations

SnapshotEconomicEntity (py.org.fundacionparaguaya.pspserver.surveys.entities.SnapshotEconomicEntity)17 ArrayList (java.util.ArrayList)9 SnapshotIndicators (py.org.fundacionparaguaya.pspserver.surveys.dtos.SnapshotIndicators)6 SurveyData (py.org.fundacionparaguaya.pspserver.surveys.dtos.SurveyData)6 FamilyEntity (py.org.fundacionparaguaya.pspserver.families.entities.FamilyEntity)5 SnapshotIndicatorEntity (py.org.fundacionparaguaya.pspserver.surveys.entities.SnapshotIndicatorEntity)5 ReportDTO (py.org.fundacionparaguaya.pspserver.reports.dtos.ReportDTO)4 SnapshotIndicatorPriority (py.org.fundacionparaguaya.pspserver.surveys.dtos.SnapshotIndicatorPriority)4 DateTimeFormatter (java.time.format.DateTimeFormatter)3 List (java.util.List)3 Map (java.util.Map)3 Collectors (java.util.stream.Collectors)3 Specifications.where (org.springframework.data.jpa.domain.Specifications.where)3 Service (org.springframework.stereotype.Service)3 Transactional (org.springframework.transaction.annotation.Transactional)3 CustomParameterizedException (py.org.fundacionparaguaya.pspserver.common.exceptions.CustomParameterizedException)3 FamilyDTO (py.org.fundacionparaguaya.pspserver.families.dtos.FamilyDTO)3 PersonEntity (py.org.fundacionparaguaya.pspserver.families.entities.PersonEntity)3 SnapshotIndicatorMapper (py.org.fundacionparaguaya.pspserver.surveys.mapper.SnapshotIndicatorMapper)3 SnapshotEconomicRepository (py.org.fundacionparaguaya.pspserver.surveys.repositories.SnapshotEconomicRepository)3