FacilityService extends AbstractInfrastructureAdoService<Facility> { public List<Facility> getFacilitiesByNameAndType( String name, District district, Community community, FacilityType type, boolean includeArchivedEntities) { CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Facility> cq = cb.createQuery(getElementClass()); Root<Facility> from = cq.from(getElementClass()); Predicate filter = cb.or( cb.equal(cb.trim(from.get(Facility.NAME)), name.trim()), cb.equal(cb.lower(cb.trim(from.get(Facility.NAME))), name.trim().toLowerCase())); if (!includeArchivedEntities) { filter = cb.and(filter, createBasicFilter(cb, from)); } if (!FacilityDto.OTHER_FACILITY.equals(name.trim()) && !FacilityDto.NO_FACILITY.equals(name.trim())) { if (community != null) { filter = cb.and(filter, cb.equal(from.get(Facility.COMMUNITY), community)); } else if (district != null) { filter = cb.and(filter, cb.equal(from.get(Facility.DISTRICT), district)); } if (type != null) { filter = cb.and(filter, cb.equal(from.get(Facility.TYPE), type)); } } cq.where(filter); return em.createQuery(cq).getResultList(); } FacilityService(); List<Facility> getActiveFacilitiesByCommunityAndType(
		Community community,
		FacilityType type,
		boolean includeOtherFacility,
		boolean includeNoneFacility); List<Facility> getActiveFacilitiesByDistrictAndType(
		District district,
		FacilityType type,
		boolean includeOtherFacility,
		boolean includeNoneFacility); List<Facility> getAllActiveLaboratories(boolean includeOtherFacility); List<Facility> getFacilitiesByNameAndType(
		String name,
		District district,
		Community community,
		FacilityType type,
		boolean includeArchivedEntities); @SuppressWarnings("rawtypes") @Override Predicate createUserFilter(CriteriaBuilder cb, CriteriaQuery cq, From<?, Facility> from); Predicate buildCriteriaFilter(FacilityCriteria facilityCriteria, CriteriaBuilder cb, Root<Facility> from); void createConstantFacilities();  }
@Test public void testGetHealthFacilitiesByName() { Region region = creator.createRegion("Region"); District district = creator.createDistrict("District", region); District otherDistrict = creator.createDistrict("Other District", region); Community community = creator.createCommunity("Community", district); Community otherCommunity = creator.createCommunity("Other Community", otherDistrict); creator.createFacility("Facility", region, district, community); assertThat(getFacilityService().getFacilitiesByNameAndType("Facility", district, community, null, true), hasSize(1)); assertThat(getFacilityService().getFacilitiesByNameAndType(" Facility ", district, community, null, true), hasSize(1)); assertThat(getFacilityService().getFacilitiesByNameAndType("facility", district, null, null, true), hasSize(1)); assertThat(getFacilityService().getFacilitiesByNameAndType("FACILITY", district, null, null, true), hasSize(1)); assertThat(getFacilityService().getFacilitiesByNameAndType("Facility", otherDistrict, otherCommunity, null, true), empty()); assertThat(getFacilityService().getFacilitiesByNameAndType("Redcliffe Church", district, community, null, true), empty()); }