CommunityService extends AbstractInfrastructureAdoService<Community> { public List<Community> getByName(String name, District district, boolean includeArchivedEntities) { CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Community> cq = cb.createQuery(getElementClass()); Root<Community> from = cq.from(getElementClass()); Predicate filter = cb.or( cb.equal(cb.trim(from.get(Community.NAME)), name.trim()), cb.equal(cb.lower(cb.trim(from.get(Community.NAME))), name.trim().toLowerCase())); if (!includeArchivedEntities) { filter = cb.and(filter, createBasicFilter(cb, from)); } if (district != null) { filter = cb.and(filter, cb.equal(from.get(Community.DISTRICT), district)); } cq.where(filter); return em.createQuery(cq).getResultList(); } CommunityService(); List<Community> getByName(String name, District district, boolean includeArchivedEntities); @SuppressWarnings("rawtypes") @Override Predicate createUserFilter(CriteriaBuilder cb, CriteriaQuery cq, From<?, Community> from); Predicate buildCriteriaFilter(CommunityCriteria criteria, CriteriaBuilder cb, Root<Community> from);  }
@Test public void testGetByName() { Region region = creator.createRegion("Region"); District district = creator.createDistrict("District", region); District otherDistrict = creator.createDistrict("Other District", region); creator.createCommunity("Community", district); assertThat(getCommunityService().getByName("Community", district, true), hasSize(1)); assertThat(getCommunityService().getByName(" Community ", district, true), hasSize(1)); assertThat(getCommunityService().getByName("community", district, true), hasSize(1)); assertThat(getCommunityService().getByName("COMMUNITY", district, true), hasSize(1)); assertThat(getCommunityService().getByName("Community", otherDistrict, true), empty()); assertThat(getCommunityService().getByName("Redcliffe Village", district, true), empty()); }