PersonQueryBuilder extends AbstractQueryBuilder<PersonList> { @Override protected BaseQueryBuilder build(final PersonList context, final BaseQueryBuilder builder) { Function<Object, String> docNumFunction = value -> { return "LOWER(e.docNum) LIKE LOWER('%" + value + "%') "; }; return builder .where() .openBracket() .addAndCondition(PERSON_TYPES_CONDITION, context.getPersonTypes()) .addAndCondition(GENDERTYPES_CONDITION, context.getGenderTypes()) .addAndCondition(MARRIEDTYPES_CONDITION, context.getMarriedTypes()) .addAndCondition(PARENTS_CONDITION, context.getParents()) .addAndCondition(CITIZEN_COUNTRIES_CONDITION, context.getCitizenCountries()) .addAndConditionForLoop(value -> MessageFormat.format(NAMES_CONDITIOIN_PLACEHOLDER, value), context.getNames()) .addAndConditionForLoop(value -> MessageFormat.format(FIRSTNAMES_CONDITIOIN_PLACEHOLDER, value), context.getFirstNames()) .addAndConditionForLoop(value -> MessageFormat.format(SURNNAMES_CONDITIOIN_PLACEHOLDER, value), context.getSurnames()) .addAndConditionForLoop(value -> MessageFormat.format(FATHERNAMES_CONDITIOIN_PLACEHOLDER, value), context.getFatherNames()) .addAndConditionForLoop(value -> MessageFormat.format(PHOTOS_CONDITIOIN_PLACEHOLDER, value), context.getPhotos()) .addAndConditionForLoop(value -> MessageFormat.format(DOCSERIES_CONDITIOIN_PLACEHOLDER, value), context.getDocSeries()) .addAndConditionForLoop(docNumFunction, context.getDocNums()) .addAndConditionForLoop(value -> MessageFormat.format(IDENTIFIERS_CONDITIOIN_PLACEHOLDER, value), context.getIdentifiers()) .addAndConditionForLoop(value -> MessageFormat.format(BIRTHPLACES_CONDITIOIN_PLACEHOLDER, value), context.getBirthPlaces()) .addAndCondition(RESIDENT_CONDITION, context.getResident()) .addAndCondition(BEGDATE_CONDITION, context.getBegDate()) .addAndCondition(ENDDATE_CONDITION, context.getEndDate()) .addAndCondition(ISMILITARY_CONDITION, context.getIsMilitary()) .addAndCondition(ISHOSTEL_CONDITION, context.getIsHostel()) .closeBracket(); }    }
@Test public void testBuild() throws Exception { PersonList context = new PersonList(); String expectedQuery = "SELECT e FROM Person e WHERE e.status=:status AND e.crtUserGroup IN (:userGroups) "; MultiplePagedSearch<PersonList> pagedSearch = new MultiplePagedSearch<>(); pagedSearch.setEntity(context); String actualQuery = unit.build(pagedSearch); assertEquals(expectedQuery, actualQuery); }
@Test public void testBuildWithPlaceholder() throws Exception { PersonList context = new PersonList(); context.setNames(Arrays.asList("name1", "name2")); String expectedQuery = "SELECT e FROM Person e WHERE ( (LOWER(e.name) LIKE LOWER('%name1%') OR LOWER(e.name) LIKE LOWER('%name2%') ) ) AND e.status=:status AND e.crtUserGroup IN (:userGroups) "; MultiplePagedSearch<PersonList> pagedSearch = new MultiplePagedSearch<>(); pagedSearch.setEntity(context); String actualQuery = unit.build(pagedSearch); assertEquals(expectedQuery, actualQuery); }
@Test public void testBuildWithPlaceholderWithAnotherFields() throws Exception { PersonList context = new PersonList(); context.setNames(Arrays.asList("name1", "name2")); String expectedQuery = "SELECT e FROM Person e WHERE ( (LOWER(e.name) LIKE LOWER('%name1%') OR LOWER(e.name) LIKE LOWER('%name2%') ) ) AND e.status=:status AND e.crtUserGroup IN (:userGroups) "; MultiplePagedSearch<PersonList> pagedSearch = new MultiplePagedSearch<>(); pagedSearch.setEntity(context); String actualQuery = unit.build(pagedSearch); assertEquals(expectedQuery, actualQuery); }
@Test public void testBuildWithPlaceholderWithAllMultiFields() throws Exception { PersonList context = new PersonList(); context.setNames(Arrays.asList("name1", "name2")); context.setFirstNames(Arrays.asList("firtName1", "firtName2", "firtName3", "firtName4")); context.setSurnames(Arrays.asList("surname1", "surname2", "surname3" )); String expectedQuery = "SELECT e FROM Person e WHERE ( (LOWER(e.name) LIKE LOWER('%name1%') OR LOWER(e.name) LIKE LOWER('%name2%') ) AND (LOWER(e.firstName) LIKE LOWER('%firtName1%') OR LOWER(e.firstName) LIKE LOWER('%firtName2%') OR LOWER(e.firstName) LIKE LOWER('%firtName3%') OR LOWER(e.firstName) LIKE LOWER('%firtName4%') ) AND (LOWER(e.surname) LIKE LOWER('%surname1%') OR LOWER(e.surname) LIKE LOWER('%surname2%') OR LOWER(e.surname) LIKE LOWER('%surname3%') ) ) AND e.status=:status AND e.crtUserGroup IN (:userGroups) "; MultiplePagedSearch<PersonList> pagedSearch = new MultiplePagedSearch<>(); pagedSearch.setEntity(context); String actualQuery = unit.build(pagedSearch); assertEquals(expectedQuery, actualQuery); }
@Test public void testBuildWithPlaceholderWithDocnums() throws Exception { List docnumes = new ArrayList(); docnumes.add(1432432L); PersonList context = new PersonList(); context.setDocNums(docnumes); String expectedQuery = "SELECT e FROM Person e WHERE ( (LOWER(e.docNum) LIKE LOWER('%1432432%') ) ) AND e.status=:status AND e.crtUserGroup IN (:userGroups) "; MultiplePagedSearch<PersonList> pagedSearch = new MultiplePagedSearch<>(); pagedSearch.setEntity(context); String actualQuery = unit.build(pagedSearch); assertEquals(expectedQuery, actualQuery); }
@Test public void testBuildWithPlaceholderWithInvalidDocNum() throws Exception { List docnumes = new ArrayList(); docnumes.add("$fsdf32"); PersonList context = new PersonList(); context.setDocNums(docnumes); String expectedQuery = "SELECT e FROM Person e WHERE ( (LOWER(e.docNum) LIKE LOWER('%$fsdf32%') ) ) AND e.status=:status AND e.crtUserGroup IN (:userGroups) "; MultiplePagedSearch<PersonList> pagedSearch = new MultiplePagedSearch<>(); pagedSearch.setEntity(context); String actualQuery = unit.build(pagedSearch); assertEquals(expectedQuery, actualQuery); }
@Test public void testBuildWithDisabledSecurityConstraint() throws Exception { unit.setSecurity(false); PersonList context = new PersonList(); String expectedQuery = "SELECT e FROM Person e WHERE e.status=:status "; MultiplePagedSearch<PersonList> pagedSearch = new MultiplePagedSearch<>(); pagedSearch.setEntity(context); String actualQuery = unit.build(pagedSearch); assertEquals(expectedQuery, actualQuery); }
@Test public void testBuildWithDisabledStatusConstraint() throws Exception { unit.setActive(false); PersonList context = new PersonList(); String expectedQuery = "SELECT e FROM Person e WHERE e.crtUserGroup IN (:userGroups) "; MultiplePagedSearch<PersonList> pagedSearch = new MultiplePagedSearch<>(); pagedSearch.setEntity(context); String actualQuery = unit.build(pagedSearch); assertEquals(expectedQuery, actualQuery); }
@Test public void testBuildWithDisabledDefaultConstraints() throws Exception { unit.setActive(false); unit.setSecurity(false); PersonList context = new PersonList(); String expectedQuery = "SELECT e FROM Person e "; MultiplePagedSearch<PersonList> pagedSearch = new MultiplePagedSearch<>(); pagedSearch.setEntity(context); String actualQuery = unit.build(pagedSearch); assertEquals(expectedQuery, actualQuery); }