Search in sources :

Example 1 with CityEntity

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

the class CityServiceImpl method addCity.

@Override
public CityDTO addCity(CityDTO cityDTO) {
    CityEntity city = new CityEntity();
    BeanUtils.copyProperties(cityDTO, city);
    CityEntity newCity = cityRepository.save(city);
    return cityMapper.entityToDto(newCity);
}
Also used : CityEntity(py.org.fundacionparaguaya.pspserver.system.entities.CityEntity)

Example 2 with CityEntity

use of py.org.fundacionparaguaya.pspserver.system.entities.CityEntity 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 3 with CityEntity

use of py.org.fundacionparaguaya.pspserver.system.entities.CityEntity 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)

Aggregations

CityEntity (py.org.fundacionparaguaya.pspserver.system.entities.CityEntity)3 FamilyEntity (py.org.fundacionparaguaya.pspserver.families.entities.FamilyEntity)2 CountryEntity (py.org.fundacionparaguaya.pspserver.system.entities.CountryEntity)2 LocalDateTime (java.time.LocalDateTime)1 ArrayList (java.util.ArrayList)1 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)1 CriteriaQuery (javax.persistence.criteria.CriteriaQuery)1 Predicate (javax.persistence.criteria.Predicate)1 Root (javax.persistence.criteria.Root)1 Specification (org.springframework.data.jpa.domain.Specification)1 ApplicationEntity (py.org.fundacionparaguaya.pspserver.network.entities.ApplicationEntity)1 OrganizationEntity (py.org.fundacionparaguaya.pspserver.network.entities.OrganizationEntity)1