/** * Retrieve the parameterized type for the given field. * * @param entityClass the entity class * @param identifier the identifier * @return Entity type for field otherwise null. */ public Class<?> getParameterizedType(Class<?> entityClass, String identifier) { return getParameterizedType(entityClass, identifier, 0); }
/** * Retrieve the parameterized type for the given field. * * @param entityClass the entity class * @param identifier the identifier * @return Entity type for field otherwise null. */ public Class<?> getParameterizedType(Class<?> entityClass, String identifier) { return getParameterizedType(entityClass, identifier, 0); }
/** * Retrieve the parameterized type for the given field. * * @param entity Entity instance * @param identifier Field to lookup * @param paramIndex the index of the parameterization * @return Entity type for field otherwise null. */ public Class<?> getParameterizedType(Object entity, String identifier, int paramIndex) { return getParameterizedType(entity.getClass(), identifier, paramIndex); }
/** * Retrieve the parameterized type for the given field. * * @param entity Entity instance * @param identifier Field to lookup * @param paramIndex the index of the parameterization * @return Entity type for field otherwise null. */ public Class<?> getParameterizedType(Object entity, String identifier, int paramIndex) { return getParameterizedType(entity.getClass(), identifier, paramIndex); }
/** * Retrieve the parameterized type for the given field. * * @param entity Entity instance * @param identifier Field to lookup * @return Entity type for field otherwise null. */ public Class<?> getParameterizedType(Object entity, String identifier) { return getParameterizedType(entity.getClass(), identifier); }
/** * Retrieve the parameterized type for the given field. * * @param entity Entity instance * @param identifier Field to lookup * @return Entity type for field otherwise null. */ public Class<?> getParameterizedType(Object entity, String identifier) { return getParameterizedType(entity.getClass(), identifier); }
/** * Get a list of elide-bound relationships. * * @param entityClass Entity class to find relationships for * @return List of elide-bound relationship names. */ public List<String> getElideBoundRelationships(Class<?> entityClass) { return getRelationships(entityClass).stream() .filter(relationName -> getBindings().contains(getParameterizedType(entityClass, relationName))) .collect(Collectors.toList()); }
/** * Get a list of elide-bound relationships. * * @param entityClass Entity class to find relationships for * @return List of elide-bound relationship names. */ public List<String> getElideBoundRelationships(Class<?> entityClass) { return getRelationships(entityClass).stream() .filter(relationName -> getBindings().contains(getParameterizedType(entityClass, relationName))) .collect(Collectors.toList()); }
private Map coerceMap(Map<?, ?> values, String fieldName, Class<?> fieldClass) { Class<?> keyType = dictionary.getParameterizedType(obj, fieldName, 0); Class<?> valueType = dictionary.getParameterizedType(obj, fieldName, 1); // Verify the existing Map if (isValidParameterizedMap(values, keyType, valueType)) { return values; } LinkedHashMap<Object, Object> result = new LinkedHashMap<>(values.size()); for (Map.Entry<?, ?> entry : values.entrySet()) { result.put(CoerceUtil.coerce(entry.getKey(), keyType), CoerceUtil.coerce(entry.getValue(), valueType)); } return result; }
private Map coerceMap(Map<?, ?> values, String fieldName, Class<?> fieldClass) { Class<?> keyType = dictionary.getParameterizedType(obj, fieldName, 0); Class<?> valueType = dictionary.getParameterizedType(obj, fieldName, 1); // Verify the existing Map if (isValidParameterizedMap(values, keyType, valueType)) { return values; } LinkedHashMap<Object, Object> result = new LinkedHashMap<>(values.size()); for (Map.Entry<?, ?> entry : values.entrySet()) { result.put(CoerceUtil.coerce(entry.getKey(), keyType), CoerceUtil.coerce(entry.getValue(), valueType)); } return result; }
/** * Retrieve fields from an object containing a particular type. * * @param targetClass Class to search for fields * @param targetType Type of fields to find * @return Set containing field names */ public Set<String> getFieldsOfType(Class<?> targetClass, Class<?> targetType) { HashSet<String> fields = new HashSet<>(); for (String field : getAllFields(targetClass)) { if (getParameterizedType(targetClass, field).equals(targetType)) { fields.add(field); } } return fields; }
/** * Retrieve fields from an object containing a particular type. * * @param targetClass Class to search for fields * @param targetType Type of fields to find * @return Set containing field names */ public Set<String> getFieldsOfType(Class<?> targetClass, Class<?> targetType) { HashSet<String> fields = new HashSet<>(); for (String field : getAllFields(targetClass)) { if (getParameterizedType(targetClass, field).equals(targetType)) { fields.add(field); } } return fields; }
protected DataStoreTransaction getRelationTransaction(Object object, String relationName) { EntityDictionary dictionary = multiplexManager.getDictionary(); Class<?> relationClass = dictionary.getParameterizedType(object, relationName); return getTransaction(relationClass); }
private Path buildPath(Class rootEntityType, String selector) { String[] associationNames = selector.split("\\."); List<Path.PathElement> path = new ArrayList<>(); Class entityType = rootEntityType; for (String associationName : associationNames) { String typeName = dictionary.getJsonAliasFor(entityType); Class fieldType = dictionary.getParameterizedType(entityType, associationName); if (fieldType == null) { throw new RSQLParseException( String.format("No such association %s for type %s", associationName, typeName)); } path.add(new Path.PathElement(entityType, fieldType, associationName)); entityType = fieldType; } return new Path(path); }
private Path buildPath(Class rootEntityType, String selector) { String[] associationNames = selector.split("\\."); List<Path.PathElement> path = new ArrayList<>(); Class entityType = rootEntityType; for (String associationName : associationNames) { String typeName = dictionary.getJsonAliasFor(entityType); Class fieldType = dictionary.getParameterizedType(entityType, associationName); if (fieldType == null) { throw new RSQLParseException( String.format("No such association %s for type %s", associationName, typeName)); } path.add(new Path.PathElement(entityType, fieldType, associationName)); entityType = fieldType; } return new Path(path); }
private Set<PersistentResource> getRelation(String relationName, Optional<FilterExpression> filterExpression, Optional<Sorting> sorting, Optional<Pagination> pagination, boolean checked) { if (checked && !checkRelation(relationName)) { return Collections.emptySet(); } final Class<?> relationClass = dictionary.getParameterizedType(obj, relationName); if (pagination.isPresent() && !pagination.get().isDefaultInstance() && !CanPaginateVisitor.canPaginate(relationClass, dictionary, requestScope)) { throw new InvalidPredicateException(String.format("Cannot paginate %s", dictionary.getJsonAliasFor(relationClass))); } return getRelationUnchecked(relationName, filterExpression, sorting, pagination); }
private Set<PersistentResource> getRelation(String relationName, Optional<FilterExpression> filterExpression, Optional<Sorting> sorting, Optional<Pagination> pagination, boolean checked) { if (checked && !checkRelation(relationName)) { return Collections.emptySet(); } final Class<?> relationClass = dictionary.getParameterizedType(obj, relationName); if (pagination.isPresent() && !pagination.get().isDefaultInstance() && !CanPaginateVisitor.canPaginate(relationClass, dictionary, requestScope)) { throw new InvalidPredicateException(String.format("Cannot paginate %s", dictionary.getJsonAliasFor(relationClass))); } return getRelationUnchecked(relationName, filterExpression, sorting, pagination); }
/** * Check the permissions of the relationship, and return true or false. * @param relationName The relationship to the entity * @return True if the relationship to the entity has valid permissions for the user */ protected boolean checkRelation(String relationName) { List<String> relations = dictionary.getRelationships(obj); String realName = dictionary.getNameFromAlias(obj, relationName); relationName = (realName == null) ? relationName : realName; if (relationName == null || relations == null || !relations.contains(relationName)) { throw new InvalidAttributeException(relationName, type); } checkFieldAwareDeferPermissions(ReadPermission.class, relationName, null, null); return !shouldSkipCollection( dictionary.getParameterizedType(obj, relationName), ReadPermission.class, requestScope); }
/** * Check the permissions of the relationship, and return true or false. * @param relationName The relationship to the entity * @return True if the relationship to the entity has valid permissions for the user */ protected boolean checkRelation(String relationName) { List<String> relations = dictionary.getRelationships(obj); String realName = dictionary.getNameFromAlias(obj, relationName); relationName = (realName == null) ? relationName : realName; if (relationName == null || relations == null || !relations.contains(relationName)) { throw new InvalidAttributeException(relationName, type); } checkFieldAwareDeferPermissions(ReadPermission.class, relationName, null, null); return !shouldSkipCollection( dictionary.getParameterizedType(obj, relationName), ReadPermission.class, requestScope); }
/** * Get the filter expression for a particular relationship * @param parent The object which has the relationship * @param relationName The relationship name * @return A type specific filter expression for the given relationship */ public Optional<FilterExpression> getExpressionForRelation(PersistentResource parent, String relationName) { final Class<?> entityClass = dictionary.getParameterizedType(parent.getObject(), relationName); if (entityClass == null) { throw new InvalidAttributeException(relationName, parent.getType()); } if (dictionary.isMappedInterface(entityClass) && interfaceHasFilterExpression(entityClass)) { throw new InvalidOperationException( "Cannot apply filters to polymorphic relations mapped with MappedInterface"); } final String valType = dictionary.getJsonAliasFor(entityClass); return getFilterExpressionByType(valType); }