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;
}
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);
}
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);
}
});
}
Aggregations