Search in sources :

Example 1 with FacilityType

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

the class LocationImporter method parseFacilities.

private void parseFacilities(XSSFSheet sheet) {
    XSSFRow row;
    XSSFCell cell;
    Iterator rows = sheet.rowIterator();
    HashSet<Facility> newFacilitySet = new HashSet<>();
    DataFormatter fmt = new DataFormatter();
    while (rows.hasNext()) {
        row = (XSSFRow) rows.next();
        cell = row.getCell(FACILITY_COL_NUMBER);
        if (cell == null) {
            continue;
        }
        String cellText = cell.getStringCellValue();
        if (cellText.equals(FACILITY_HEADER) || StringUtils.isEmpty(cellText)) {
            continue;
        }
        String facilityId = fmt.formatCellValue(row.getCell(ID_FACILITY_COL_NUMBER));
        FacilityType facilityType = FacilityType.getByDisplayName(row.getCell(NAME_FACILITY_COL_NUMBER).getStringCellValue());
        Facility facility = new Facility(cellText, facilityType, facilityId);
        String parentChiefdomName = row.getCell(CHIEFDOM_COL_NUMBER).getStringCellValue();
        String parentDistrictName = row.getCell(DISTRICT_COL_NUMBER).getStringCellValue();
        Chiefdom parent = currentChiefdomList.stream().filter(chiefdom -> chiefdom.getName().equals(parentChiefdomName) && chiefdom.getDistrict().getName().equals(parentDistrictName)).findFirst().orElseThrow(() -> new RuntimeException(String.format("'%s' Facility parent " + "is not defined properly in spreadsheet", facility.getName())));
        facility.setChiefdom(parent);
        newFacilitySet.add(facility);
    }
    newFacilitySet.forEach(newFacility -> {
        if (!currentFacilityList.contains(newFacility)) {
            locationService.createImportedFacility(newFacility);
        }
    });
}
Also used : XSSFRow(org.apache.poi.xssf.usermodel.XSSFRow) Iterator(java.util.Iterator) XSSFCell(org.apache.poi.xssf.usermodel.XSSFCell) Facility(org.motechproject.mots.domain.Facility) FacilityType(org.motechproject.mots.domain.enums.FacilityType) HashSet(java.util.HashSet) DataFormatter(org.apache.poi.ss.usermodel.DataFormatter) Chiefdom(org.motechproject.mots.domain.Chiefdom)

Example 2 with FacilityType

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

the class FacilityRepositoryImpl method prepareQuery.

private <T> CriteriaQuery<T> prepareQuery(CriteriaQuery<T> query, String facilityId, String facilityName, String facilityType, String inchargeFullName, String parentChiefdom, String districtName, boolean count, Pageable pageable) throws IllegalArgumentException {
    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    Root<Facility> root = query.from(Facility.class);
    if (count) {
        CriteriaQuery<Long> countQuery = (CriteriaQuery<Long>) query;
        query = (CriteriaQuery<T>) countQuery.select(builder.count(root));
    }
    Predicate predicate = builder.conjunction();
    if (facilityId != null) {
        predicate = builder.and(predicate, builder.like(root.get(FACILITY_ID), '%' + facilityId + '%'));
    }
    if (facilityName != null) {
        predicate = builder.and(predicate, builder.like(root.get(NAME), '%' + facilityName + '%'));
    }
    if (facilityType != null) {
        FacilityType validFacilityType = FacilityType.valueOf(facilityType.toUpperCase());
        predicate = builder.and(predicate, builder.equal(root.get(FACILITY_TYPE), validFacilityType));
    }
    if (inchargeFullName != null) {
        predicate = builder.and(predicate, builder.like(root.get(INCHARGE).get(FIRST_NAME), '%' + inchargeFullName + '%'));
        predicate = builder.or(predicate, builder.like(root.get(INCHARGE).get(SECOND_NAME), '%' + inchargeFullName + '%'));
        predicate = builder.or(predicate, builder.like(root.get(INCHARGE).get(OTHER_NAME), '%' + inchargeFullName + '%'));
    }
    if (parentChiefdom != null) {
        predicate = builder.and(predicate, builder.like(root.get(CHIEFDOM).get(NAME), '%' + parentChiefdom + '%'));
    }
    if (districtName != null) {
        predicate = builder.and(predicate, builder.like(root.get(CHIEFDOM).get(DISTRICT).get(NAME), '%' + districtName + '%'));
    }
    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) Facility(org.motechproject.mots.domain.Facility) FacilityType(org.motechproject.mots.domain.enums.FacilityType) Predicate(javax.persistence.criteria.Predicate)

Aggregations

Facility (org.motechproject.mots.domain.Facility)2 FacilityType (org.motechproject.mots.domain.enums.FacilityType)2 HashSet (java.util.HashSet)1 Iterator (java.util.Iterator)1 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)1 CriteriaQuery (javax.persistence.criteria.CriteriaQuery)1 Predicate (javax.persistence.criteria.Predicate)1 DataFormatter (org.apache.poi.ss.usermodel.DataFormatter)1 XSSFCell (org.apache.poi.xssf.usermodel.XSSFCell)1 XSSFRow (org.apache.poi.xssf.usermodel.XSSFRow)1 Chiefdom (org.motechproject.mots.domain.Chiefdom)1