Search in sources :

Example 6 with FamilyEntity

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

the class FamilyServiceImpl method createOrReturnFamilyFromSnapshot.

@Override
public FamilyEntity createOrReturnFamilyFromSnapshot(UserDetailsDTO details, NewSnapshot snapshot, String code, PersonEntity person) {
    if (familyRepository.findByCode(code).isPresent()) {
        return familyRepository.findByCode(code).get();
    }
    FamilyEntity newFamily = new FamilyEntity();
    newFamily.setPerson(person);
    newFamily.setCode(code);
    newFamily.setName(person.getFirstName().concat(SPACE).concat(person.getLastName()));
    newFamily.setLocationPositionGps(snapshot.getEconomicSurveyData().getAsString("familyUbication"));
    if (details.getApplication() != null) {
        newFamily.setApplication(applicationMapper.dtoToEntity(details.getApplication()));
    }
    newFamily.setActive(true);
    Optional<CountryEntity> country = countryRepository.findByCountry(snapshot.getEconomicSurveyData().getAsString("familyCountry"));
    newFamily.setCountry(country.orElse(null));
    Optional<CityEntity> city = cityRepository.findByCity(snapshot.getEconomicSurveyData().getAsString("familyCity"));
    newFamily.setCity(city.orElse(null));
    if (snapshot.getOrganizationId() != null) {
        OrganizationEntity organization = organizationRepository.findOne(snapshot.getOrganizationId());
        newFamily.setOrganization(organization);
        newFamily.setApplication(organization.getApplication());
    }
    newFamily = familyRepository.save(newFamily);
    return newFamily;
}
Also used : FamilyEntity(py.org.fundacionparaguaya.pspserver.families.entities.FamilyEntity) OrganizationEntity(py.org.fundacionparaguaya.pspserver.network.entities.OrganizationEntity) CityEntity(py.org.fundacionparaguaya.pspserver.system.entities.CityEntity) CountryEntity(py.org.fundacionparaguaya.pspserver.system.entities.CountryEntity)

Example 7 with FamilyEntity

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

the class FamilyServiceImpl method imageUpload.

@Override
public String imageUpload(Long idFamily, MultipartFile multipartFile) throws IOException {
    FamilyEntity familyEntity = familyRepository.findOne(idFamily);
    if (familyEntity == null) {
        throw new UnknownResourceException(i18n.translate("family.notExist"));
    }
    String familiesImageDirectory = this.applicationProperties.getAws().getFamiliesImageDirectory();
    ImageDTO image = ImageParser.parse(multipartFile, familiesImageDirectory);
    // control if image already exists: if so, deletes the old image
    if (familyEntity.getImageURL() != null) {
        imageUploadService.deleteImage(familyEntity.getImageURL(), familiesImageDirectory);
    }
    // uploads the image and obtains its URL
    String url = imageUploadService.uploadImage(image);
    familyEntity.setImageURL(url);
    LOG.debug("Updating family {} with image {}", familyEntity.getFamilyId(), familyEntity.getImageURL());
    familyRepository.save(familyEntity);
    return url;
}
Also used : FamilyEntity(py.org.fundacionparaguaya.pspserver.families.entities.FamilyEntity) ImageDTO(py.org.fundacionparaguaya.pspserver.system.dtos.ImageDTO) UnknownResourceException(py.org.fundacionparaguaya.pspserver.common.exceptions.UnknownResourceException)

Example 8 with FamilyEntity

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

the class FamilySpecification method byFilter.

public static Specification<FamilyEntity> byFilter(FamilyFilterDTO filter) {
    return new Specification<FamilyEntity>() {

        @Override
        public Predicate toPredicate(Root<FamilyEntity> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
            List<Predicate> predicates = new ArrayList<>();
            if (filter.getApplicationId() != null) {
                Join<FamilyEntity, ApplicationEntity> joinApplication = root.join(FamilyEntity_.getApplication());
                Expression<Long> byApplicationId = joinApplication.<Long>get(ID_ATTRIBUTE);
                predicates.add(cb.equal(byApplicationId, filter.getApplicationId()));
            }
            if (filter.getOrganizationId() != null) {
                Expression<Long> byOrganizationId = root.join(FamilyEntity_.getOrganization()).<Long>get(ID_ATTRIBUTE);
                predicates.add(cb.equal(byOrganizationId, filter.getOrganizationId()));
            }
            if (filter.getCountryId() != null) {
                Join<FamilyEntity, CountryEntity> joinCountry = root.join(FamilyEntity_.getCountry());
                Expression<Long> byCountryId = joinCountry.<Long>get(ID_ATTRIBUTE);
                predicates.add(cb.equal(byCountryId, filter.getCountryId()));
            }
            if (filter.getCityId() != null) {
                Join<FamilyEntity, CityEntity> joinCity = root.join(FamilyEntity_.getCity());
                Expression<Long> byCityId = joinCity.<Long>get(ID_ATTRIBUTE);
                predicates.add(cb.equal(byCityId, filter.getCityId()));
            }
            if (StringUtils.isNotEmpty(filter.getName())) {
                String nameParamQuery = "%" + filter.getName().toLowerCase().replaceAll("\\s", "%") + "%";
                Expression<String> likeName = cb.lower(root.get(FamilyEntity_.getName()));
                predicates.add(cb.like(likeName, nameParamQuery));
            }
            if (filter.getLastModifiedGt() != null) {
                LocalDateTime dateTimeParam = LocalDateTime.parse(filter.getLastModifiedGt());
                Predicate predicate = cb.greaterThan(root.get(FamilyEntity_.getLastModifiedAt()), dateTimeParam);
                predicates.add(predicate);
            }
            predicates.add(cb.isTrue(root.get(FamilyEntity_.getIsActive())));
            return cb.and(predicates.toArray(new Predicate[predicates.size()]));
        }
    };
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) LocalDateTime(java.time.LocalDateTime) FamilyEntity(py.org.fundacionparaguaya.pspserver.families.entities.FamilyEntity) Root(javax.persistence.criteria.Root) CriteriaQuery(javax.persistence.criteria.CriteriaQuery) ArrayList(java.util.ArrayList) CityEntity(py.org.fundacionparaguaya.pspserver.system.entities.CityEntity) Specification(org.springframework.data.jpa.domain.Specification) Predicate(javax.persistence.criteria.Predicate) ApplicationEntity(py.org.fundacionparaguaya.pspserver.network.entities.ApplicationEntity) CountryEntity(py.org.fundacionparaguaya.pspserver.system.entities.CountryEntity)

Example 9 with FamilyEntity

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

the class SnapshotReportManagerImpl method listFamilyByOrganizationAndCreatedDate.

@Override
public List<OrganizationFamilyDTO> listFamilyByOrganizationAndCreatedDate(SnapshotFilterDTO filters) {
    List<FamilyEntity> families = new ArrayList<>();
    Sort sort = new Sort(new Sort.Order(Direction.ASC, "organization.name"), new Sort.Order(Direction.ASC, "name"));
    Specification<FamilyEntity> dateRange = FamilySpecification.createdAtBetween2Dates(filters.getDateFrom(), filters.getDateTo());
    families = familyRepository.findAll(where(byOrganization(filters.getOrganizationId())).and(dateRange).and(byApplication(filters.getApplicationId())).and(dateRange), sort);
    Map<OrganizationEntity, List<FamilyEntity>> groupByOrganization = families.stream().collect(Collectors.groupingBy(f -> f.getOrganization()));
    List<OrganizationFamilyDTO> toRet = new ArrayList<>();
    groupByOrganization.forEach((k, v) -> {
        OrganizationFamilyDTO fa = new OrganizationFamilyDTO(k.getName(), k.getCode(), k.getDescription(), k.isActive());
        fa.setFamilies(familyReportMapper.entityListToDtoList(v));
        toRet.add(fa);
    });
    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) FamilyEntity(py.org.fundacionparaguaya.pspserver.families.entities.FamilyEntity) OrganizationEntity(py.org.fundacionparaguaya.pspserver.network.entities.OrganizationEntity) OrganizationFamilyDTO(py.org.fundacionparaguaya.pspserver.reports.dtos.OrganizationFamilyDTO) ArrayList(java.util.ArrayList) Sort(org.springframework.data.domain.Sort) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

FamilyEntity (py.org.fundacionparaguaya.pspserver.families.entities.FamilyEntity)9 ArrayList (java.util.ArrayList)4 SnapshotEconomicEntity (py.org.fundacionparaguaya.pspserver.surveys.entities.SnapshotEconomicEntity)4 SurveyData (py.org.fundacionparaguaya.pspserver.surveys.dtos.SurveyData)3 SnapshotIndicatorEntity (py.org.fundacionparaguaya.pspserver.surveys.entities.SnapshotIndicatorEntity)3 List (java.util.List)2 Map (java.util.Map)2 Collectors (java.util.stream.Collectors)2 Specification (org.springframework.data.jpa.domain.Specification)2 Specifications.where (org.springframework.data.jpa.domain.Specifications.where)2 Service (org.springframework.stereotype.Service)2 Transactional (org.springframework.transaction.annotation.Transactional)2 CustomParameterizedException (py.org.fundacionparaguaya.pspserver.common.exceptions.CustomParameterizedException)2 UnknownResourceException (py.org.fundacionparaguaya.pspserver.common.exceptions.UnknownResourceException)2 FamilyFilterDTO (py.org.fundacionparaguaya.pspserver.families.dtos.FamilyFilterDTO)2 PersonEntity (py.org.fundacionparaguaya.pspserver.families.entities.PersonEntity)2 OrganizationEntity (py.org.fundacionparaguaya.pspserver.network.entities.OrganizationEntity)2 SnapshotIndicators (py.org.fundacionparaguaya.pspserver.surveys.dtos.SnapshotIndicators)2 ValidationResults (py.org.fundacionparaguaya.pspserver.surveys.validation.ValidationResults)2 CityEntity (py.org.fundacionparaguaya.pspserver.system.entities.CityEntity)2