private void pageByPages(int defaultLimit, int maxLimit) { limit = pageData.containsKey(PaginationKey.size) ? pageData.get(PaginationKey.size) : defaultLimit; if (limit > maxLimit) { throw new InvalidValueException("page[size] value must be less than or equal to " + maxLimit); } if (limit < 0) { throw new InvalidValueException("page[size] must contain a positive value."); } int pageNumber = pageData.containsKey(PaginationKey.number) ? pageData.get(PaginationKey.number) : 1; if (pageNumber < 1) { throw new InvalidValueException("page[number] must contain a positive value."); } offset = (pageNumber - 1) * limit; }
private void pageByOffset(int defaultLimit, int maxLimit) { limit = pageData.containsKey(PaginationKey.limit) ? pageData.get(PaginationKey.limit) : defaultLimit; if (limit > maxLimit) { throw new InvalidValueException("page[limit] value must be less than or equal to " + maxLimit); } if (limit < 0) { throw new InvalidValueException("page[limit] value must contain a positive value"); } offset = pageData.containsKey(PaginationKey.offset) ? pageData.get(PaginationKey.offset) : 0; if (offset < 0) { throw new InvalidValueException("page[offset] must contain a positive values."); } }
private void pageByOffset(int defaultLimit, int maxLimit) { limit = pageData.containsKey(PaginationKey.limit) ? pageData.get(PaginationKey.limit) : defaultLimit; if (limit > maxLimit) { throw new InvalidValueException("page[limit] value must be less than or equal to " + maxLimit); } if (limit < 0) { throw new InvalidValueException("page[limit] value must contain a positive value"); } offset = pageData.containsKey(PaginationKey.offset) ? pageData.get(PaginationKey.offset) : 0; if (offset < 0) { throw new InvalidValueException("page[offset] must contain a positive values."); } }
private void pageByPages(int defaultLimit, int maxLimit) { limit = pageData.containsKey(PaginationKey.size) ? pageData.get(PaginationKey.size) : defaultLimit; if (limit > maxLimit) { throw new InvalidValueException("page[size] value must be less than or equal to " + maxLimit); } if (limit < 0) { throw new InvalidValueException("page[size] must contain a positive value."); } int pageNumber = pageData.containsKey(PaginationKey.number) ? pageData.get(PaginationKey.number) : 1; if (pageNumber < 1) { throw new InvalidValueException("page[number] must contain a positive value."); } offset = (pageNumber - 1) * limit; }
private void assertValidValues(String fieldPath, String alias) { if (Strings.isNullOrEmpty(fieldPath)) { throw new InvalidValueException(FILTER_PATH_NOT_NULL); } if (Strings.isNullOrEmpty(alias)) { throw new IllegalStateException(FILTER_ALIAS_NOT_NULL); } }
/** * Convert value to target class. * * @param <T> type * @param value value to convert * @param cls class to convert to * @return coerced value */ public static <T> T coerce(Object value, Class<T> cls) { initializeCurrentClassLoaderIfNecessary(); if (value == null || cls == null || cls.isAssignableFrom(value.getClass())) { return (T) value; } try { return (T) ConvertUtils.convert(value, cls); } catch (ConversionException | InvalidAttributeException | IllegalArgumentException e) { throw new InvalidValueException(value, e.getMessage()); } }
first = Integer.parseInt(firstString); } catch (NumberFormatException e) { throw new InvalidValueException("Offset and first must be numeric values."); throw new InvalidValueException("Offset values must be non-negative."); } else if (first < 1) { throw new InvalidValueException("Limit values must be positive.");
pageData.put(paginationKey, intValue); } catch (NumberFormatException e) { throw new InvalidValueException("page values must be integers"); throw new InvalidValueException("Invalid Pagination Parameter. Accepted values are " + PAGE_KEYS_CSV);
pageData.put(paginationKey, intValue); } catch (NumberFormatException e) { throw new InvalidValueException("page values must be integers"); throw new InvalidValueException("Invalid Pagination Parameter. Accepted values are " + PAGE_KEYS_CSV);
/** * Convert value to target class. * * @param <T> type * @param value value to convert * @param cls class to convert to * @return coerced value */ public static <T> T coerce(Object value, Class<T> cls) { initializeCurrentClassLoaderIfNecessary(); if (value == null || cls == null || cls.isAssignableFrom(value.getClass())) { return (T) value; } try { return (T) ConvertUtils.convert(value, cls); } catch (ConversionException | InvalidAttributeException | IllegalArgumentException e) { throw new InvalidValueException(value, e.getMessage()); } }
first = Integer.parseInt(firstString); } catch (NumberFormatException e) { throw new InvalidValueException("Offset and first must be numeric values."); throw new InvalidValueException("Offset values must be non-negative."); } else if (first < 1) { throw new InvalidValueException("Limit values must be positive.");
/** * Evaluates the pagination variables for default limits. * * @param defaultLimit the default page size * @param maxLimit a hard upper limit on page size * @return the calculated {@link Pagination} */ private Pagination evaluate(int defaultLimit, int maxLimit) { if (hasInvalidCombination(pageData)) { throw new InvalidValueException("Invalid usage of pagination parameters."); } if (pageData.containsKey(PaginationKey.size) || pageData.containsKey(PaginationKey.number)) { pageByPages(defaultLimit, maxLimit); } else if (pageData.containsKey(PaginationKey.limit) || pageData.containsKey(PaginationKey.offset)) { pageByOffset(defaultLimit, maxLimit); } else { limit = defaultLimit; offset = 0; } generateTotals = pageData.containsKey(PaginationKey.totals); return this; }
/** * Evaluates the pagination variables for default limits. * * @param defaultLimit the default page size * @param maxLimit a hard upper limit on page size * @return the calculated {@link Pagination} */ private Pagination evaluate(int defaultLimit, int maxLimit) { if (hasInvalidCombination(pageData)) { throw new InvalidValueException("Invalid usage of pagination parameters."); } if (pageData.containsKey(PaginationKey.size) || pageData.containsKey(PaginationKey.number)) { pageByPages(defaultLimit, maxLimit); } else if (pageData.containsKey(PaginationKey.limit) || pageData.containsKey(PaginationKey.offset)) { pageByOffset(defaultLimit, maxLimit); } else { limit = defaultLimit; offset = 0; } generateTotals = pageData.containsKey(PaginationKey.totals); return this; }
/** * Given the sorting rules validate sorting rules against the entities bound to the entityClass. * @param entityClass The root class for sorting (eg. /book?sort=-title this would be package.Book) * @param dictionary The elide entity dictionary * @param <T> The entityClass * @return The valid sorting rules - validated through the entity dictionary, or empty dictionary * @throws InvalidValueException when sorting values are not valid for the jpa entity */ public <T> Map<Path, SortOrder> getValidSortingRules(final Class<T> entityClass, final EntityDictionary dictionary) throws InvalidValueException { Map<Path, SortOrder> returnMap = new LinkedHashMap<>(); for (Map.Entry<String, SortOrder> entry : replaceIdRule(dictionary.getIdFieldName(entityClass)).entrySet()) { String dotSeparatedPath = entry.getKey(); SortOrder order = entry.getValue(); //Creating a path validates that the dot separated path is valid. Path path = new Path(entityClass, dictionary, dotSeparatedPath); if (! isValidSortRulePath(path, dictionary)) { throw new InvalidValueException("Cannot sort across a to-many relationship: " + path.getFieldPath()); } returnMap.put(path, order); } return returnMap; }
public Path(Class<?> entityClass, EntityDictionary dictionary, String dotSeparatedPath) { List<PathElement> elements = new ArrayList<>(); String[] fieldNames = dotSeparatedPath.split("\\."); Class<?> currentClass = entityClass; for (String fieldName : fieldNames) { if (dictionary.isRelation(currentClass, fieldName)) { Class<?> relationClass = dictionary.getParameterizedType(currentClass, fieldName); elements.add(new PathElement(currentClass, relationClass, fieldName)); currentClass = relationClass; } else if (dictionary.isAttribute(currentClass, fieldName) || fieldName.equals(dictionary.getIdFieldName(entityClass))) { Class<?> attributeClass = dictionary.getType(currentClass, fieldName); elements.add(new PathElement(currentClass, attributeClass, fieldName)); } else if ("this".equals(fieldName)) { elements.add(new PathElement(currentClass, null, fieldName)); } else { String alias = dictionary.getJsonAliasFor(currentClass); throw new InvalidValueException(alias + " doesn't contain the field " + fieldName); } } pathElements = ImmutableList.copyOf(elements); }
/** * Given the sorting rules validate sorting rules against the entities bound to the entityClass. * @param entityClass The root class for sorting (eg. /book?sort=-title this would be package.Book) * @param dictionary The elide entity dictionary * @param <T> The entityClass * @return The valid sorting rules - validated through the entity dictionary, or empty dictionary * @throws InvalidValueException when sorting values are not valid for the jpa entity */ public <T> Map<Path, SortOrder> getValidSortingRules(final Class<T> entityClass, final EntityDictionary dictionary) throws InvalidValueException { Map<Path, SortOrder> returnMap = new LinkedHashMap<>(); for (Map.Entry<String, SortOrder> entry : replaceIdRule(dictionary.getIdFieldName(entityClass)).entrySet()) { String dotSeparatedPath = entry.getKey(); SortOrder order = entry.getValue(); //Creating a path validates that the dot separated path is valid. Path path = new Path(entityClass, dictionary, dotSeparatedPath); if (! isValidSortRulePath(path, dictionary)) { throw new InvalidValueException("Cannot sort across a to-many relationship: " + path.getFieldPath()); } returnMap.put(path, order); } return returnMap; }
/** * Assign provided id if id field is not generated. * * @param persistentResource resource * @param id resource id */ private static void assignId(PersistentResource persistentResource, String id) { //If id field is not a `@GeneratedValue` persist the provided id if (!persistentResource.isIdGenerated()) { if (id != null && !id.isEmpty()) { persistentResource.setId(id); } else { //If expecting id to persist and id is not present, throw exception throw new InvalidValueException( persistentResource.toResource(), "No id provided, cannot persist " + persistentResource.getObject() ); } } } }
public Path(Class<?> entityClass, EntityDictionary dictionary, String dotSeparatedPath) { List<PathElement> elements = new ArrayList<>(); String[] fieldNames = dotSeparatedPath.split("\\."); Class<?> currentClass = entityClass; for (String fieldName : fieldNames) { if (dictionary.isRelation(currentClass, fieldName)) { Class<?> relationClass = dictionary.getParameterizedType(currentClass, fieldName); elements.add(new PathElement(currentClass, relationClass, fieldName)); currentClass = relationClass; } else if (dictionary.isAttribute(currentClass, fieldName) || fieldName.equals(dictionary.getIdFieldName(entityClass))) { Class<?> attributeClass = dictionary.getType(currentClass, fieldName); elements.add(new PathElement(currentClass, attributeClass, fieldName)); } else if ("this".equals(fieldName)) { elements.add(new PathElement(currentClass, null, fieldName)); } else { String alias = dictionary.getJsonAliasFor(currentClass); throw new InvalidValueException(alias + " doesn't contain the field " + fieldName); } } pathElements = ImmutableList.copyOf(elements); }
/** * Assign provided id if id field is not generated. * * @param persistentResource resource * @param id resource id */ private static void assignId(PersistentResource persistentResource, String id) { //If id field is not a `@GeneratedValue` persist the provided id if (!persistentResource.isIdGenerated()) { if (id != null && !id.isEmpty()) { persistentResource.setId(id); } else { //If expecting id to persist and id is not present, throw exception throw new InvalidValueException( persistentResource.toResource(), "No id provided, cannot persist " + persistentResource.getObject() ); } } } }
throw new InvalidValueException("Cannot assign value of type: " + resource.getType() + " to type: " + entityClass);