public Class getEntityType() { List<PathElement> elements = path.getPathElements(); PathElement first = elements.get(0); return first.getType(); }
public Class getEntityType() { List<PathElement> elements = path.getPathElements(); PathElement first = elements.get(0); return first.getType(); }
public FilterPredicate(Path path, Operator op, List<Object> values) { this.operator = op; this.path = new Path(path); this.values = ImmutableList.copyOf(values); this.field = path.lastElement() .map(PathElement::getFieldName) .orElse(null); this.fieldPath = path.getPathElements().stream() .map(PathElement::getFieldName) .collect(Collectors.joining(PERIOD)); }
public FilterPredicate(Path path, Operator op, List<Object> values) { this.operator = op; this.path = new Path(path); this.values = ImmutableList.copyOf(values); this.field = path.lastElement() .map(PathElement::getFieldName) .orElse(null); this.fieldPath = path.getPathElements().stream() .map(PathElement::getFieldName) .collect(Collectors.joining(PERIOD)); }
@Override public String toString() { List<PathElement> elements = path.getPathElements(); StringBuilder formattedPath = new StringBuilder(); if (!elements.isEmpty()) { formattedPath.append(StringUtils.uncapitalize(EntityDictionary.getSimpleName(elements.get(0).getType()))); } for (PathElement element : elements) { formattedPath.append(PERIOD).append(element.getFieldName()); } return formattedPath.append(' ').append(operator).append(' ').append(values).toString(); }
@Override public String toString() { List<PathElement> elements = path.getPathElements(); StringBuilder formattedPath = new StringBuilder(); if (!elements.isEmpty()) { formattedPath.append(StringUtils.uncapitalize(EntityDictionary.getSimpleName(elements.get(0).getType()))); } for (PathElement element : elements) { formattedPath.append(PERIOD).append(element.getFieldName()); } return formattedPath.append(' ').append(operator).append(' ').append(values).toString(); }
public static boolean toManyInPath(EntityDictionary dictionary, Path path) { return path.getPathElements().stream() .map(element -> dictionary.getRelationshipType(element.getType(), element.getFieldName())) .anyMatch(RelationshipType::isToMany); }
public static boolean toManyInPath(EntityDictionary dictionary, Path path) { return path.getPathElements().stream() .map(element -> dictionary.getRelationshipType(element.getType(), element.getFieldName())) .anyMatch(RelationshipType::isToMany); }
/** * Returns an alias that uniquely identifies the last collection of entities in the path. * @return An alias for the path. */ public String getAlias() { List<PathElement> elements = path.getPathElements(); PathElement last = elements.get(elements.size() - 1); if (elements.size() == 1) { return getTypeAlias(last.getType()); } PathElement previous = elements.get(elements.size() - 2); return getTypeAlias(previous.getType()) + UNDERSCORE + previous.getFieldName(); }
/** * Returns an alias that uniquely identifies the last collection of entities in the path. * @return An alias for the path. */ public String getAlias() { List<PathElement> elements = path.getPathElements(); PathElement last = elements.get(elements.size() - 1); if (elements.size() == 1) { return getTypeAlias(last.getType()); } PathElement previous = elements.get(elements.size() - 2); return getTypeAlias(previous.getType()) + UNDERSCORE + previous.getFieldName(); }
/** * Create a copy of this filter that is scoped by scope. This is used in calculating page totals, we need to * scope this filter in the context of it's parent. * * @param scope the path element to add to the head of the path * @return the scoped filter expression. */ public FilterPredicate scopedBy(PathElement scope) { List<PathElement> pathElements = Lists.asList(scope, path.getPathElements().toArray(ELEMENT_ARRAY)); return new FilterPredicate(new Path(pathElements), operator, values); }
/** * Create a copy of this filter that is scoped by scope. This is used in calculating page totals, we need to * scope this filter in the context of it's parent. * * @param scope the path element to add to the head of the path * @return the scoped filter expression. */ public FilterPredicate scopedBy(PathElement scope) { List<PathElement> pathElements = Lists.asList(scope, path.getPathElements().toArray(ELEMENT_ARRAY)); return new FilterPredicate(new Path(pathElements), operator, values); }
List<Path.PathElement> elements = path.getPathElements(); Path.PathElement last = elements.get(elements.size() - 1);
/** * Get the comparator for sorting. * * @param path Path to field for sorting * @param order Order to sort * @param requestScope Request scope * @return Comparator for sorting */ private Comparator<Object> getComparator(Path path, Sorting.SortOrder order, RequestScope requestScope) { return (left, right) -> { Object leftCompare = left; Object rightCompare = right; // Drill down into path to find value for comparison for (Path.PathElement pathElement : path.getPathElements()) { leftCompare = PersistentResource.getValue(leftCompare, pathElement.getFieldName(), requestScope); rightCompare = PersistentResource.getValue(rightCompare, pathElement.getFieldName(), requestScope); } // Make sure value is comparable and perform comparison if (leftCompare instanceof Comparable) { int result = ((Comparable<Object>) leftCompare).compareTo(rightCompare); if (order == Sorting.SortOrder.asc) { return result; } return -result; } throw new IllegalStateException("Trying to comparing non-comparable types!"); }; }
/** * Get the comparator for sorting. * * @param path Path to field for sorting * @param order Order to sort * @param requestScope Request scope * @return Comparator for sorting */ private Comparator<Object> getComparator(Path path, Sorting.SortOrder order, RequestScope requestScope) { return (left, right) -> { Object leftCompare = left; Object rightCompare = right; // Drill down into path to find value for comparison for (Path.PathElement pathElement : path.getPathElements()) { leftCompare = PersistentResource.getValue(leftCompare, pathElement.getFieldName(), requestScope); rightCompare = PersistentResource.getValue(rightCompare, pathElement.getFieldName(), requestScope); } // Make sure value is comparable and perform comparison if (leftCompare instanceof Comparable) { int result = ((Comparable<Object>) leftCompare).compareTo(rightCompare); if (order == Sorting.SortOrder.asc) { return result; } return -result; } throw new IllegalStateException("Trying to comparing non-comparable types!"); }; }
for (Path.PathElement pathElement : predicate.getPath().getPathElements()) { String fieldName = pathElement.getFieldName(); Class<?> typeClass = dictionary.lookupEntityClass(pathElement.getType());
@Override public FilterExpression parseGlobalExpression(String path, MultivaluedMap<String, String> filterParams) throws ParseException { List<FilterPredicate> filterPredicates; filterPredicates = extractPredicates(filterParams); /* Extract the first collection in the URL */ String normalizedPath = JsonApiParser.normalizePath(path); String[] pathComponents = normalizedPath.split("/"); String firstPathComponent = ""; if (pathComponents.length > 0) { firstPathComponent = pathComponents[0]; } /* Comma separated filter parameters are joined with logical AND. */ FilterExpression joinedExpression = null; for (FilterPredicate filterPredicate : filterPredicates) { Class firstClass = filterPredicate.getPath().getPathElements().get(0).getType(); /* The first type in the predicate must match the first collection in the URL */ if (!dictionary.getJsonAliasFor(firstClass).equals(firstPathComponent)) { throw new ParseException(String.format("Invalid predicate: %s", filterPredicate)); } if (joinedExpression == null) { joinedExpression = filterPredicate; } else { joinedExpression = new AndFilterExpression(joinedExpression, filterPredicate); } } return joinedExpression; }
@Override public FilterExpression parseGlobalExpression(String path, MultivaluedMap<String, String> filterParams) throws ParseException { List<FilterPredicate> filterPredicates; filterPredicates = extractPredicates(filterParams); /* Extract the first collection in the URL */ String normalizedPath = JsonApiParser.normalizePath(path); String[] pathComponents = normalizedPath.split("/"); String firstPathComponent = ""; if (pathComponents.length > 0) { firstPathComponent = pathComponents[0]; } /* Comma separated filter parameters are joined with logical AND. */ FilterExpression joinedExpression = null; for (FilterPredicate filterPredicate : filterPredicates) { Class firstClass = filterPredicate.getPath().getPathElements().get(0).getType(); /* The first type in the predicate must match the first collection in the URL */ if (!dictionary.getJsonAliasFor(firstClass).equals(firstPathComponent)) { throw new ParseException(String.format("Invalid predicate: %s", filterPredicate)); } if (joinedExpression == null) { joinedExpression = filterPredicate; } else { joinedExpression = new AndFilterExpression(joinedExpression, filterPredicate); } } return joinedExpression; }
/** * Validates that none of the provided path's relationships are to-many * @param path The path to validate * @param dictionary The elide entity dictionary * @return True if the path is valid. False otherwise. */ protected static boolean isValidSortRulePath(Path path, EntityDictionary dictionary) { //Validate that none of the relationships are to-many for (Path.PathElement pathElement : path.getPathElements()) { if (! dictionary.isRelation(pathElement.getType(), pathElement.getFieldName())) { continue; } if (dictionary.getRelationshipType(pathElement.getType(), pathElement.getFieldName()).isToMany()) { return false; } } return true; }
/** * Validates that none of the provided path's relationships are to-many * @param path The path to validate * @param dictionary The elide entity dictionary * @return True if the path is valid. False otherwise. */ protected static boolean isValidSortRulePath(Path path, EntityDictionary dictionary) { //Validate that none of the relationships are to-many for (Path.PathElement pathElement : path.getPathElements()) { if (! dictionary.isRelation(pathElement.getType(), pathElement.getFieldName())) { continue; } if (dictionary.getRelationshipType(pathElement.getType(), pathElement.getFieldName()).isToMany()) { return false; } } return true; }