/** * Get a type for a field on an entity. * * @param entity Entity instance * @param identifier Field to lookup type * @return Type of entity */ public Class<?> getType(Object entity, String identifier) { return getType(entity.getClass(), identifier); }
/** * Get a type for a field on an entity. * * @param entity Entity instance * @param identifier Field to lookup type * @return Type of entity */ public Class<?> getType(Object entity, String identifier) { return getType(entity.getClass(), identifier); }
private static <T> Predicate<T> in(String field, List<Object> values, RequestScope requestScope, Function<String, String> transform) { return (T entity) -> { Class type = requestScope.getDictionary().getType(entity, field); if (!type.isAssignableFrom(String.class)) { throw new IllegalStateException("Cannot case insensitive compare non-string values"); } String val = transform.apply((String) getFieldValue(entity, field, requestScope)); return val != null && values.stream() .map(v -> transform.apply(CoerceUtil.coerce(v, String.class))) .anyMatch(val::equals); }; }
private static <T> Predicate<T> in(String field, List<Object> values, RequestScope requestScope, Function<String, String> transform) { return (T entity) -> { Class type = requestScope.getDictionary().getType(entity, field); if (!type.isAssignableFrom(String.class)) { throw new IllegalStateException("Cannot case insensitive compare non-string values"); } String val = transform.apply((String) getFieldValue(entity, field, requestScope)); return val != null && values.stream() .map(v -> transform.apply(CoerceUtil.coerce(v, String.class))) .anyMatch(val::equals); }; }
/** * Retrieve the parameterized type for the given field. * * @param entityClass the entity class * @param identifier the identifier * @param paramIndex the index of the parameterization * @return Entity type for field otherwise null. */ public Class<?> getParameterizedType(Class<?> entityClass, String identifier, int paramIndex) { ConcurrentHashMap<String, AccessibleObject> fieldOrMethods = getEntityBinding(entityClass).fieldsToValues; if (fieldOrMethods == null) { return null; } AccessibleObject fieldOrMethod = fieldOrMethods.get(identifier); if (fieldOrMethod == null) { return null; } Type type; if (fieldOrMethod instanceof Method) { type = ((Method) fieldOrMethod).getGenericReturnType(); } else { type = ((Field) fieldOrMethod).getGenericType(); } if (type instanceof ParameterizedType) { return (Class<?>) ((ParameterizedType) type).getActualTypeArguments()[paramIndex]; } return getType(entityClass, identifier); }
/** * Retrieve the parameterized type for the given field. * * @param entityClass the entity class * @param identifier the identifier * @param paramIndex the index of the parameterization * @return Entity type for field otherwise null. */ public Class<?> getParameterizedType(Class<?> entityClass, String identifier, int paramIndex) { ConcurrentHashMap<String, AccessibleObject> fieldOrMethods = getEntityBinding(entityClass).fieldsToValues; if (fieldOrMethods == null) { return null; } AccessibleObject fieldOrMethod = fieldOrMethods.get(identifier); if (fieldOrMethod == null) { return null; } Type type; if (fieldOrMethod instanceof Method) { type = ((Method) fieldOrMethod).getGenericReturnType(); } else { type = ((Field) fieldOrMethod).getGenericType(); } if (type instanceof ParameterizedType) { return (Class<?>) ((ParameterizedType) type).getActualTypeArguments()[paramIndex]; } return getType(entityClass, identifier); }
/** * @return the JSON-API 'sort' query parameter for some GET operations. */ private Parameter getSortParameter() { List<String> filterAttributes = dictionary.getAttributes(type).stream() .filter((name) -> { Class<?> attributeClass = dictionary.getType(type, name); return (attributeClass.isPrimitive() || String.class.isAssignableFrom(attributeClass)); }) .map((name) -> Arrays.asList(name, "-" + name)) .flatMap(Collection::stream) .collect(Collectors.toList()); return new QueryParameter() .name("sort") .type("array") .description("Sorts the collection on the selected attributes. A prefix of '-' sorts descending") .items(new StringProperty()._enum(filterAttributes)) .collectionFormat("csv"); }
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); }
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); }
final Object original = getValueUnchecked(fieldName); try { Class<?> fieldClass = dictionary.getType(targetClass, fieldName); String realName = dictionary.getNameFromAlias(obj, fieldName); fieldName = (realName != null) ? realName : fieldName;
/** * Update attribute in existing resource. * * @param fieldName the field name * @param newVal the new val * @return true if object updated, false otherwise */ public boolean updateAttribute(String fieldName, Object newVal) { Class<?> fieldClass = dictionary.getType(getResourceClass(), fieldName); newVal = coerce(newVal, fieldName, fieldClass); Object val = getValueUnchecked(fieldName); checkFieldAwareDeferPermissions(UpdatePermission.class, fieldName, newVal, val); if (!Objects.equals(val, newVal)) { this.setValueChecked(fieldName, newVal); this.markDirty(); //Hooks for customize logic for setAttribute/Relation if (dictionary.isAttribute(obj.getClass(), fieldName)) { transaction.setAttribute(obj, fieldName, newVal, requestScope); } return true; } return false; }
Class<?> attributeClass = dictionary.getType(type, name);
/** * Update attribute in existing resource. * * @param fieldName the field name * @param newVal the new val * @return true if object updated, false otherwise */ public boolean updateAttribute(String fieldName, Object newVal) { Class<?> fieldClass = dictionary.getType(getResourceClass(), fieldName); newVal = coerce(newVal, fieldName, fieldClass); Object val = getValueUnchecked(fieldName); checkFieldAwareDeferPermissions(UpdatePermission.class, fieldName, newVal, val); if (!Objects.equals(val, newVal)) { this.setValueChecked(fieldName, newVal); this.markDirty(); //Hooks for customize logic for setAttribute/Relation if (dictionary.isAttribute(obj.getClass(), fieldName)) { transaction.setAttribute(obj, fieldName, newVal, requestScope); } return true; } return false; }
final Object original = getValueUnchecked(fieldName); try { Class<?> fieldClass = dictionary.getType(targetClass, fieldName); String realName = dictionary.getNameFromAlias(obj, fieldName); fieldName = (realName != null) ? realName : fieldName;
Class<?> inverseType = dictionary.getType(inverseObj.getClass(), inverseName);
Class<?> inverseType = dictionary.getType(inverseObj.getClass(), inverseName);
Class<?> inverseType = dictionary.getType(inverseEntity.getClass(), inverseField);
Class<?> attributeClass = dictionary.getType(clazz, attribute);
Class<?> inverseType = dictionary.getType(inverseEntity.getClass(), inverseField);
Class<?> attributeClazz = dictionary.getType(clazz, attributeName);