Search in sources :

Example 6 with Chiefdom

use of org.motechproject.mots.domain.Chiefdom in project mots by motech-implementations.

the class ChiefdomRepositoryImpl method prepareQuery.

private <T> CriteriaQuery<T> prepareQuery(CriteriaQuery<T> query, String chiefdomName, String parentDistrict, boolean count, Pageable pageable) throws IllegalArgumentException {
    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    Root<Chiefdom> root = query.from(Chiefdom.class);
    if (count) {
        CriteriaQuery<Long> countQuery = (CriteriaQuery<Long>) query;
        query = (CriteriaQuery<T>) countQuery.select(builder.count(root));
    }
    Predicate predicate = builder.conjunction();
    if (chiefdomName != null) {
        predicate = builder.and(predicate, builder.like(root.get(NAME), '%' + chiefdomName + '%'));
    }
    if (parentDistrict != null) {
        predicate = builder.and(predicate, builder.like(root.get(DISTRICT).get(NAME), '%' + parentDistrict + '%'));
    }
    query.where(predicate);
    if (!count && pageable != null && pageable.getSort() != null) {
        query = addSortProperties(query, root, pageable);
    }
    return query;
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) CriteriaQuery(javax.persistence.criteria.CriteriaQuery) Chiefdom(org.motechproject.mots.domain.Chiefdom) Predicate(javax.persistence.criteria.Predicate)

Example 7 with Chiefdom

use of org.motechproject.mots.domain.Chiefdom in project mots by motech-implementations.

the class ChiefdomRepositoryImpl method search.

/**
 * Finds Chiefdoms matching all of the provided parameters.
 * If there are no parameters, return all Chiefdoms.
 */
@Override
public Page<Chiefdom> search(String chiefdomName, String parentDistrict, Pageable pageable) throws IllegalArgumentException {
    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Chiefdom> query = builder.createQuery(Chiefdom.class);
    query = prepareQuery(query, chiefdomName, parentDistrict, false, pageable);
    CriteriaQuery<Long> countQuery = builder.createQuery(Long.class);
    countQuery = prepareQuery(countQuery, chiefdomName, parentDistrict, true, pageable);
    Long count = entityManager.createQuery(countQuery).getSingleResult();
    int pageSize = getPageSize(pageable);
    int firstResult = getFirstResult(pageable, pageSize);
    List<Chiefdom> incharges = entityManager.createQuery(query).setMaxResults(pageSize).setFirstResult(firstResult).getResultList();
    return new PageImpl<>(incharges, pageable, count);
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) PageImpl(org.springframework.data.domain.PageImpl) Chiefdom(org.motechproject.mots.domain.Chiefdom)

Example 8 with Chiefdom

use of org.motechproject.mots.domain.Chiefdom in project mots by motech-implementations.

the class LocationImporter method parseChiefdoms.

private void parseChiefdoms(XSSFSheet sheet) {
    XSSFRow row;
    XSSFCell cell;
    Iterator rows = sheet.rowIterator();
    HashSet<Chiefdom> newChiefdomSet = new HashSet<>();
    while (rows.hasNext()) {
        row = (XSSFRow) rows.next();
        cell = row.getCell(CHIEFDOM_COL_NUMBER);
        if (cell == null) {
            continue;
        }
        String cellText = cell.getStringCellValue();
        if (cellText.equals(CHIEFDOM_HEADER) || StringUtils.isEmpty(cellText)) {
            continue;
        }
        Chiefdom chiefdom = new Chiefdom(cellText);
        String parentName = row.getCell(DISTRICT_COL_NUMBER).getStringCellValue();
        District parent = currentDistrictList.stream().filter(district -> district.getName().equals(parentName)).findFirst().orElseThrow(() -> new RuntimeException(String.format("'%s' Chiefdom parent " + "is not defined properly in spreadsheet", chiefdom.getName())));
        chiefdom.setDistrict(parent);
        newChiefdomSet.add(chiefdom);
    }
    newChiefdomSet.forEach(newChiefdom -> {
        if (!currentChiefdomList.contains(newChiefdom)) {
            locationService.createChiefdom(newChiefdom);
        }
    });
}
Also used : XSSFRow(org.apache.poi.xssf.usermodel.XSSFRow) Iterator(java.util.Iterator) XSSFCell(org.apache.poi.xssf.usermodel.XSSFCell) District(org.motechproject.mots.domain.District) Chiefdom(org.motechproject.mots.domain.Chiefdom) HashSet(java.util.HashSet)

Aggregations

Chiefdom (org.motechproject.mots.domain.Chiefdom)8 HashSet (java.util.HashSet)2 Iterator (java.util.Iterator)2 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)2 XSSFCell (org.apache.poi.xssf.usermodel.XSSFCell)2 XSSFRow (org.apache.poi.xssf.usermodel.XSSFRow)2 Facility (org.motechproject.mots.domain.Facility)2 UUID (java.util.UUID)1 CriteriaQuery (javax.persistence.criteria.CriteriaQuery)1 Predicate (javax.persistence.criteria.Predicate)1 DataFormatter (org.apache.poi.ss.usermodel.DataFormatter)1 Test (org.junit.Test)1 District (org.motechproject.mots.domain.District)1 FacilityType (org.motechproject.mots.domain.enums.FacilityType)1 PageImpl (org.springframework.data.domain.PageImpl)1