@Override public Object loadObject(Class<?> entityClass, Serializable id, Optional<FilterExpression> filterExpression, RequestScope scope) { Class idType = dictionary.getIdType(entityClass); String idField = dictionary.getIdFieldName(entityClass); FilterExpression idFilter = new InPredicate( new Path.PathElement(entityClass, idType, idField), id ); FilterExpression joinedFilterExpression = filterExpression .map(fe -> (FilterExpression) new AndFilterExpression(idFilter, fe)) .orElse(idFilter); Iterable<Object> results = loadObjects(entityClass, Optional.of(joinedFilterExpression), Optional.empty(), Optional.empty(), scope); Iterator<Object> it = results == null ? null : results.iterator(); if (it != null && it.hasNext()) { return it.next(); } return null; }
private FilterExpression equalityExpression(String argument, Path path, List<Object> values) { boolean startsWith = argument.startsWith("*"); boolean endsWith = argument.endsWith("*"); if (startsWith && endsWith && argument.length() > 2) { String value = argument.substring(1, argument.length() - 1); return new FilterPredicate(path, caseSensitivityStrategy.mapOperator(Operator.INFIX), Collections.singletonList(value)); } if (startsWith && argument.length() > 1) { String value = argument.substring(1, argument.length()); return new FilterPredicate(path, caseSensitivityStrategy.mapOperator(Operator.POSTFIX), Collections.singletonList(value)); } if (endsWith && argument.length() > 1) { String value = argument.substring(0, argument.length() - 1); return new FilterPredicate(path, caseSensitivityStrategy.mapOperator(Operator.PREFIX), Collections.singletonList(value)); } Boolean isStringLike = path.lastElement() .map(e -> e.getFieldType().isAssignableFrom(String.class)) .orElse(false); if (isStringLike) { return new FilterPredicate(path, caseSensitivityStrategy.mapOperator(Operator.IN), values); } return new InPredicate(path, values); }
private FilterExpression equalityExpression(String argument, Path path, List<Object> values) { boolean startsWith = argument.startsWith("*"); boolean endsWith = argument.endsWith("*"); if (startsWith && endsWith && argument.length() > 2) { String value = argument.substring(1, argument.length() - 1); return new FilterPredicate(path, caseSensitivityStrategy.mapOperator(Operator.INFIX), Collections.singletonList(value)); } if (startsWith && argument.length() > 1) { String value = argument.substring(1, argument.length()); return new FilterPredicate(path, caseSensitivityStrategy.mapOperator(Operator.POSTFIX), Collections.singletonList(value)); } if (endsWith && argument.length() > 1) { String value = argument.substring(0, argument.length() - 1); return new FilterPredicate(path, caseSensitivityStrategy.mapOperator(Operator.PREFIX), Collections.singletonList(value)); } Boolean isStringLike = path.lastElement() .map(e -> e.getFieldType().isAssignableFrom(String.class)) .orElse(false); if (isStringLike) { return new FilterPredicate(path, caseSensitivityStrategy.mapOperator(Operator.IN), values); } return new InPredicate(path, values); }
@Override public Object loadObject(Class<?> entityClass, Serializable id, Optional<FilterExpression> filterExpression, RequestScope scope) { Class idType = dictionary.getIdType(entityClass); String idField = dictionary.getIdFieldName(entityClass); FilterExpression idFilter = new InPredicate( new Path.PathElement(entityClass, idType, idField), id ); FilterExpression joinedFilterExpression = filterExpression .map(fe -> (FilterExpression) new AndFilterExpression(idFilter, fe)) .orElse(idFilter); Iterable<Object> results = loadObjects(entityClass, Optional.of(joinedFilterExpression), Optional.empty(), Optional.empty(), scope); Iterator<Object> it = results == null ? null : results.iterator(); if (it != null && it.hasNext()) { return it.next(); } return null; }
/** * Build an id filter expression for a particular entity type. * * @param ids Ids to include in the filter expression * @param entityType Type of entity * @return Filter expression for given ids and type. */ private static FilterExpression buildIdFilterExpression(List<String> ids, Class entityType, EntityDictionary dictionary, RequestScope scope) { Class<?> idType = dictionary.getIdType(entityType); String idField = dictionary.getIdFieldName(entityType); String typeAlias = dictionary.getJsonAliasFor(entityType); List<Object> coercedIds = ids.stream() .filter(id -> scope.getObjectById(typeAlias, id) == null) // these don't exist yet .map(id -> CoerceUtil.coerce(id, idType)) .collect(Collectors.toList()); /* construct a new SQL like filter expression, eg: book.id IN [1,2] */ FilterExpression idFilter = new InPredicate( new Path.PathElement( entityType, idType, idField), coercedIds); return idFilter; }
@Test public void testSettingQueryParams() { Path.PathElement idPath = new Path.PathElement(Book.class, Chapter.class, "id"); Query query = mock(Query.class); FilterPredicate predicate = new InPredicate(idPath, ABC, DEF); supplyFilterQueryParameters(query, Arrays.asList(predicate)); verify(query, times(2)).setParameter(anyString(), any()); query = mock(Query.class); predicate = new InfixPredicate(idPath, ABC); supplyFilterQueryParameters(query, Arrays.asList(predicate)); verify(query, times(1)).setParameter(anyString(), any()); }
/** * Build an id filter expression for a particular entity type. * * @param ids Ids to include in the filter expression * @param entityType Type of entity * @return Filter expression for given ids and type. */ private static FilterExpression buildIdFilterExpression(List<String> ids, Class entityType, EntityDictionary dictionary, RequestScope scope) { Class<?> idType = dictionary.getIdType(entityType); String idField = dictionary.getIdFieldName(entityType); String typeAlias = dictionary.getJsonAliasFor(entityType); List<Object> coercedIds = ids.stream() .filter(id -> scope.getObjectById(typeAlias, id) == null) // these don't exist yet .map(id -> CoerceUtil.coerce(id, idType)) .collect(Collectors.toList()); /* construct a new SQL like filter expression, eg: book.id IN [1,2] */ FilterExpression idFilter = new InPredicate( new Path.PathElement( entityType, idType, idField), coercedIds); return idFilter; }
Path.PathElement idPath = new Path.PathElement(entityClass, idType, idField); if (id != null) { idExpression = new InPredicate(idPath, id); } else { idExpression = new FalsePredicate(idPath);
Path.PathElement idPath = new Path.PathElement(entityClass, idType, idField); if (id != null) { idExpression = new InPredicate(idPath, id); } else { idExpression = new FalsePredicate(idPath);
Path.PathElement idPath = new Path.PathElement(entityClass, idType, idField); if (id != null) { idExpression = new InPredicate(idPath, id); } else { idExpression = new FalsePredicate(idPath);
@Test public void testRootFetchWithSortingAndFilters() { RootCollectionFetchQueryBuilder builder = new RootCollectionFetchQueryBuilder( Book.class, dictionary, new TestSessionWrapper()); Map<String, Sorting.SortOrder> sorting = new HashMap<>(); sorting.put(TITLE, Sorting.SortOrder.asc); Path.PathElement idPath = new Path.PathElement(Book.class, Chapter.class, "id"); FilterPredicate idPredicate = new InPredicate(idPath, 1); TestQueryWrapper query = (TestQueryWrapper) builder .withPossibleSorting(Optional.of(new Sorting(sorting))) .withPossibleFilterExpression(Optional.of(idPredicate)) .build(); String expected = "SELECT example_Book FROM example.Book AS example_Book " + "WHERE example_Book.id IN (:id_XXX) order by example_Book.title asc"; String actual = query.getQueryText(); actual = actual.replaceFirst(":id_\\w+", ":id_XXX"); Assert.assertEquals(actual, expected); } }
@Test public void testHQLQueryVisitor() throws Exception { List<Path.PathElement> p1Path = Arrays.asList( new Path.PathElement(Book.class, Author.class, "authors"), new Path.PathElement(Author.class, String.class, "name") ); FilterPredicate p1 = new InPredicate(new Path(p1Path), "foo", "bar"); List<Path.PathElement> p2Path = Arrays.asList( new Path.PathElement(Book.class, String.class, "name") ); FilterPredicate p2 = new InPredicate(new Path(p2Path), "blah"); List<Path.PathElement> p3Path = Arrays.asList( new Path.PathElement(Book.class, String.class, "genre") ); FilterPredicate p3 = new InPredicate(new Path(p3Path), "scifi"); OrFilterExpression or = new OrFilterExpression(p2, p3); AndFilterExpression and = new AndFilterExpression(or, p1); NotFilterExpression not = new NotFilterExpression(and); HQLFilterOperation filterOp = new HQLFilterOperation(); String query = filterOp.apply(not, false); String p1Params = p1.getParameters().stream() .map(FilterPredicate.FilterParameter::getPlaceholder).collect(Collectors.joining(", ")); String p2Params = p2.getParameters().stream() .map(FilterPredicate.FilterParameter::getPlaceholder).collect(Collectors.joining(", ")); String p3Params = p3.getParameters().stream() .map(FilterPredicate.FilterParameter::getPlaceholder).collect(Collectors.joining(", ")); String expected = "WHERE NOT (((name IN (" + p2Params + ") OR genre IN (" + p3Params + ")) " + "AND authors.name IN (" + p1Params + ")))"; Assert.assertEquals(query, expected); }
FilterPredicate idExpression = new InPredicate(new PathElement(parentType, idType, idField), idVal);
@Test public void testFilterJoinClause() { List<Path.PathElement> chapterTitlePath = Arrays.asList( new Path.PathElement(Author.class, Book.class, BOOKS), new Path.PathElement(Book.class, Chapter.class, "chapters"), new Path.PathElement(Chapter.class, String.class, TITLE) ); FilterPredicate titlePredicate = new InPredicate( new Path(chapterTitlePath), ABC, DEF); FilterPredicate titlePredicateDuplicate = new InPredicate( new Path(chapterTitlePath), ABC, DEF); List<Path.PathElement> publisherNamePath = Arrays.asList( new Path.PathElement(Author.class, Book.class, BOOKS), new Path.PathElement(Book.class, Publisher.class, PUBLISHER), new Path.PathElement(Publisher.class, String.class, NAME) ); FilterPredicate publisherNamePredicate = new InPredicate( new Path(publisherNamePath), "Pub1"); OrFilterExpression orExpression = new OrFilterExpression(titlePredicate, publisherNamePredicate); AndFilterExpression andExpression = new AndFilterExpression(orExpression, titlePredicateDuplicate); String actual = getJoinClauseFromFilters(andExpression); String expected = " LEFT JOIN example_Author.books example_Author_books " + "LEFT JOIN example_Author_books.chapters example_Book_chapters " + "LEFT JOIN example_Author_books.publisher example_Book_publisher "; Assert.assertEquals(actual, expected); }
); FilterPredicate titlePredicate = new InPredicate( new Path(chapterTitlePath), "ABC", "DEF"); ); FilterPredicate publisherNamePredicate = new InPredicate( new Path(publisherNamePath), "Pub1");
); FilterPredicate titlePredicate = new InPredicate( new Path(chapterTitlePath), "ABC", "DEF"); ); FilterPredicate publisherNamePredicate = new InPredicate( new Path(publisherNamePath), "Pub1");
); FilterPredicate publisherNamePredicate = new InPredicate( new Path(publisherNamePath), PUB1);
); FilterPredicate publisherNamePredicate = new InPredicate( new Path(publisherNamePath), "Pub1");
); FilterPredicate publisherNamePredicate = new InPredicate( new Path(publisherNamePath), PUB1);