/** * Get bean's property value and maps to String * * @see io.katharsis.core.internal.utils.PropertyUtils#getProperty(Object, String) * * @param bean bean to be accessed * @param field bean's field * @return bean's property value */ public static String getProperty(Object bean, String field) { Object property = PropertyUtils.getProperty(bean, field); if (property == null) { return "null"; } return property.toString(); } }
@SuppressWarnings({ "rawtypes", "unchecked" }) private Collection<D> getOrCreateCollection(Object source, String fieldName) { Object property = PropertyUtils.getProperty(source, fieldName); if (property == null) { Class<?> propertyClass = PropertyUtils.getPropertyClass(source.getClass(), fieldName); boolean isList = List.class.isAssignableFrom(propertyClass); property = isList ? new ArrayList() : new HashSet(); PropertyUtils.setProperty(source, fieldName, property); } return (Collection<D>) property; }
/** * Similar to {@link PropertyUtils#getProperty(Object,String)} but returns the property value for the tail of the given property path. * * @param bean bean to be accessed * @param propertyPath property path * @return value */ public static Object getProperty(Object bean, List<String> propertyPath) { Object current = bean; for(String propertyName : propertyPath){ if(current == null) return null; if(current instanceof Iterable){ // follow multi-valued property List<Object> result = new ArrayList<>(); Iterable<?> iterable = (Iterable<?>)current; for(Object currentElem : iterable){ result.add(getProperty(currentElem, propertyName)); } current = result; }else{ // follow single-valued property current = getProperty(current, propertyName); } } return current; }
/** * @param resource * @return id of the resource */ public Object getId(Object resource) { return PropertyUtils.getProperty(resource, idField.getUnderlyingName()); }
@Override public MetaElement findOneTarget(String sourceId, String fieldName, QuerySpec querySpec) { MetaElement source = lookup.getMetaById().get(sourceId); if (source == null) { throw new ResourceNotFoundException(sourceId); } Object value = PropertyUtils.getProperty(source, fieldName); if (!(value instanceof MetaElement) && value != null) { throw new IllegalStateException("relation " + fieldName + " is not of type MetaElement, got " + value); } return (MetaElement) value; }
private Serializable getSourceId(T source) { ResourceField idField = sourceResourceInformation.getIdField(); return (Serializable) PropertyUtils.getProperty(source, idField.getUnderlyingName()); }
@SuppressWarnings("unchecked") @Override public ResourceList<MetaElement> findManyTargets(String sourceId, String fieldName, QuerySpec querySpec) { MetaElement source = lookup.getMetaById().get(sourceId); if (source == null) { throw new ResourceNotFoundException(sourceId); } Object value = PropertyUtils.getProperty(source, fieldName); if (!(value instanceof Collection)) { throw new IllegalStateException("relation " + fieldName + " is not a collection, got " + value); } return querySpec.apply((Collection<MetaElement>) value); }
@Override @SuppressWarnings("unchecked") public int compare(T o1, T o2) { for (SortSpec orderSpec : sortSpecs) { Comparable<Object> value1 = (Comparable<Object>) PropertyUtils.getProperty(o1, orderSpec.getAttributePath()); Comparable<Object> value2 = (Comparable<Object>) PropertyUtils.getProperty(o2, orderSpec.getAttributePath()); int d = compare(value1, value2); if (orderSpec.getDirection() == Direction.DESC) { d = -d; } if (d != 0) return d; } return 0; }
public String getIdString(Object entity, ResourceInformation resourceInformation) { ResourceField idField = resourceInformation.getIdField(); Object sourceId = PropertyUtils.getProperty(entity, idField.getUnderlyingName()); return resourceInformation.toIdString(sourceId); }
private <S extends T> Object getId(S entity, boolean create) { if (client.getPushAlways()) { return null; } if (create) { return null; } else { ResourceField idField = resourceInformation.getIdField(); return PropertyUtils.getProperty(entity, idField.getUnderlyingName()); } }
private MetaInformation getResourceMeta(Object entity, ResourceInformation resourceInformation) { if (resourceInformation.getMetaField() != null) { return (MetaInformation) PropertyUtils.getProperty(entity, resourceInformation.getMetaField().getUnderlyingName()); } return null; }
private static boolean matchesPrimitiveOperator(Object object, FilterSpec filterSpec) { Object value = PropertyUtils.getProperty(object, filterSpec.getAttributePath()); FilterOperator operator = filterSpec.getOperator(); Object filterValue = filterSpec.getValue(); boolean result = false; // the ability to operate on a null value should be handled polymorphically by the operator itself if (filterValue != null || ((operator.equals(FilterOperator.EQ) || operator.equals(FilterOperator.NEQ)))) { result = (value instanceof Collection ? ((Collection<?>)value).stream() : Stream.of(value)).anyMatch(v -> operator.matches(v, filterValue)); } return result; }
/** * No lookup specified for the field. Attempt to load relationship from * original POJOs. Throw an InternalServerErrorException if the field is an * Iterable and null. */ private Set<Resource> extractRelationshipField(List<Resource> sourceResources, ResourceField relationshipField, QueryAdapter queryAdapter, Map<ResourceIdentifier, Resource> resourceMap, Map<ResourceIdentifier, Object> entityMap, boolean lookUp) { Set<Resource> loadedEntities = new HashSet<>(); for (Resource sourceResource : sourceResources) { ResourceIdentifier id = sourceResource.toIdentifier(); Object source = entityMap.get(id); if (source != null && !(source instanceof Resource)) { Object targetEntity = PropertyUtils.getProperty(source, relationshipField.getUnderlyingName()); if (!lookUp && Iterable.class.isAssignableFrom(relationshipField.getType()) && targetEntity == null) { throw new InternalServerErrorException(id + " relationship field collection '" + relationshipField.getJsonName() + "' can not be null. Either set the relationship as an empty " + Iterable.class.getCanonicalName() + " or add annotation @" + JsonApiLookupIncludeAutomatically.class.getCanonicalName()); } if (targetEntity == null) { continue; } List<Resource> targetIds = setupRelation(sourceResource, relationshipField, targetEntity, queryAdapter, resourceMap, entityMap); loadedEntities.addAll(targetIds); } } return loadedEntities; }
@SuppressWarnings("unchecked") private void handleTarget(MultivaluedMap<I, D> bulkResult, D result, Set<I> sourceIdSet, String oppositeName, ResourceInformation sourceInformation) { Object property = PropertyUtils.getProperty(result, oppositeName); if (property == null) { throw new IllegalStateException("field " + oppositeName + " is null for " + result + ", make sure to properly implement relationship inclusions"); } if (property instanceof Iterable) { for (T potentialSource : (Iterable<T>) property) { I sourceId = (I) sourceInformation.getId(potentialSource); if (sourceId == null) { throw new IllegalStateException("id is null for " + potentialSource); } // for to-many relations we have to assigned the found resource // to all matching sources if (sourceIdSet.contains(sourceId)) { bulkResult.add(sourceId, result); } } } else { T source = (T) property; I sourceId = (I) sourceInformation.getId(source); PreconditionUtil.assertTrue("filtering not properly implemented in resource repository", sourceIdSet.contains(sourceId)); if (sourceId == null) { throw new IllegalStateException("id is null for " + source); } bulkResult.add(sourceId, result); } }
protected void setAttributes(Resource resource, Object entity, ResourceInformation resourceInformation, QueryAdapter queryAdapter) { // fields parameter may further limit the number of fields List<ResourceField> fields = DocumentMapperUtil.getRequestedFields(resourceInformation, queryAdapter, resourceInformation.getAttributeFields().getFields(), false); // serialize the individual attributes for (ResourceField field : fields) { Object value = PropertyUtils.getProperty(entity, field.getUnderlyingName()); JsonNode valueNode = objectMapper.valueToTree(value); resource.getAttributes().put(field.getJsonName(), valueNode); } }
public LinksInformation getResourceLinks(Object entity, ResourceInformation resourceInformation) { LinksInformation info; if (resourceInformation.getLinksField() != null) { info = (LinksInformation) PropertyUtils.getProperty(entity, resourceInformation.getLinksField().getUnderlyingName()); } else { info = new DefaultSelfRelatedLinksInformation(); } if (info instanceof SelfLinksInformation) { SelfLinksInformation self = (SelfLinksInformation) info; if (self.getSelf() == null) { self.setSelf(util.getSelfUrl(resourceInformation, entity)); } } return info; }
@Override protected void setRelationship(Resource resource, ResourceField field, Object entity, ResourceInformation resourceInformation, QueryAdapter queryAdapter) { // we also include relationship data if it is not null and not a // unloaded proxy boolean includeRelation = true; Object relationshipValue = PropertyUtils.getProperty(entity, field.getUnderlyingName()); if (relationshipValue instanceof ObjectProxy) { includeRelation = ((ObjectProxy) relationshipValue).isLoaded(); } else { // TODO for fieldSets handling in the future the lazy // handling must be different includeRelation = relationshipValue != null || !field.isLazy() && !field.isCollection(); } if (includeRelation) { Relationship relationship = new Relationship(); if (relationshipValue instanceof Collection) { relationship.setData(Nullable.of((Object) util.toResourceIds((Collection<?>) relationshipValue))); } else { relationship.setData(Nullable.of((Object) util.toResourceId(relationshipValue))); } resource.getRelationships().put(field.getJsonName(), relationship); } } };