/** * Get an attribute from an object. * * @param entity - The object which owns the attribute. * @param attributeName - name of the attribute. * @param scope - contains request level metadata. * @return the value of the attribute */ default Object getAttribute(Object entity, String attributeName, RequestScope scope) { RequestScope requestScope; try { requestScope = scope; } catch (ClassCastException e) { throw new ClassCastException("Fail trying to cast requestscope"); } Object val = PersistentResource.getValue(entity, attributeName, requestScope); return val; }
/** * Get an attribute from an object. * * @param entity - The object which owns the attribute. * @param attributeName - name of the attribute. * @param scope - contains request level metadata. * @return the value of the attribute */ default Object getAttribute(Object entity, String attributeName, RequestScope scope) { RequestScope requestScope; try { requestScope = scope; } catch (ClassCastException e) { throw new ClassCastException("Fail trying to cast requestscope"); } Object val = PersistentResource.getValue(entity, attributeName, requestScope); return val; }
/** * Return value of field/path for given entity. For example this.book.author * * @param <T> the type of entity to retrieve a value from * @param entity Entity bean * @param fieldPath field value/path * @param requestScope Request scope * @return the value of the field */ private static <T> Object getFieldValue(T entity, String fieldPath, RequestScope requestScope) { Object val = entity; for (String field : fieldPath.split("\\.")) { if ("this".equals(field)) { continue; } if (val == null) { break; } val = PersistentResource.getValue(val, field, requestScope); } return val; }
/** * Return value of field/path for given entity. For example this.book.author * * @param <T> the type of entity to retrieve a value from * @param entity Entity bean * @param fieldPath field value/path * @param requestScope Request scope * @return the value of the field */ private static <T> Object getFieldValue(T entity, String fieldPath, RequestScope requestScope) { Object val = entity; for (String field : fieldPath.split("\\.")) { if ("this".equals(field)) { continue; } if (val == null) { break; } val = PersistentResource.getValue(val, field, requestScope); } return val; }
/** * Retrieve a relation from an object. * * @param relationTx - The datastore that governs objects of the relationhip's type. * @param entity - The object which owns the relationship. * @param relationName - name of the relationship. * @param filterExpression - filtering which can be pushed down to the data store. * It is optional for the data store to attempt evaluation. * @param sorting - sorting which can be pushed down to the data store. * @param pagination - pagination which can be pushed down to the data store. * @param scope - contains request level metadata. * @return the object in the relation */ default Object getRelation( DataStoreTransaction relationTx, Object entity, String relationName, Optional<FilterExpression> filterExpression, Optional<Sorting> sorting, Optional<Pagination> pagination, RequestScope scope) { RequestScope requestScope; try { requestScope = scope; } catch (ClassCastException e) { throw new ClassCastException("Fail trying to cast requestscope"); } return PersistentResource.getValue(entity, relationName, requestScope); }
/** * Retrieve a relation from an object. * * @param relationTx - The datastore that governs objects of the relationhip's type. * @param entity - The object which owns the relationship. * @param relationName - name of the relationship. * @param filterExpression - filtering which can be pushed down to the data store. * It is optional for the data store to attempt evaluation. * @param sorting - sorting which can be pushed down to the data store. * @param pagination - pagination which can be pushed down to the data store. * @param scope - contains request level metadata. * @return the object in the relation */ default Object getRelation( DataStoreTransaction relationTx, Object entity, String relationName, Optional<FilterExpression> filterExpression, Optional<Sorting> sorting, Optional<Pagination> pagination, RequestScope scope) { RequestScope requestScope; try { requestScope = scope; } catch (ClassCastException e) { throw new ClassCastException("Fail trying to cast requestscope"); } return PersistentResource.getValue(entity, relationName, requestScope); }
/** * 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!"); }; }
/** * Retrieve an object without checking read permissions (i.e. value is used internally and not sent to others) * * @param fieldName the field name * @return Value */ protected Object getValueUnchecked(String fieldName) { requestScope.publishLifecycleEvent(this, CRUDEvent.CRUDAction.READ); requestScope.publishLifecycleEvent(this, fieldName, CRUDEvent.CRUDAction.READ, Optional.empty()); return getValue(getObject(), fieldName, requestScope); }
/** * 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!"); }; }
/** * Retrieve an object without checking read permissions (i.e. value is used internally and not sent to others) * * @param fieldName the field name * @return Value */ protected Object getValueUnchecked(String fieldName) { requestScope.publishLifecycleEvent(this, CRUDEvent.CRUDAction.READ); requestScope.publishLifecycleEvent(this, fieldName, CRUDEvent.CRUDAction.READ, Optional.empty()); return getValue(getObject(), fieldName, requestScope); }
@Override public Object getRelation(DataStoreTransaction relationTx, Object entity, String relationName, Optional<FilterExpression> filterExpression, Optional<Sorting> sorting, Optional<Pagination> pagination, RequestScope scope) { Object values = PersistentResource.getValue(entity, relationName, scope); // Gather list of valid id's from this parent Map<String, Object> idToChildResource = new HashMap<>(); if (dictionary.getRelationshipType(entity, relationName).isToOne()) { if (values == null) { return null; } idToChildResource.put(dictionary.getId(values), values); } else if (values instanceof Collection) { idToChildResource.putAll((Map) ((Collection) values).stream() .collect(Collectors.toMap(dictionary::getId, Function.identity()))); } else { throw new IllegalStateException("An unexpected error occurred querying a relationship"); } Class entityClass = dictionary.getParameterizedType(entity, relationName); return processData(entityClass, idToChildResource, filterExpression, sorting, pagination, scope); }
@Override public Object getRelation(DataStoreTransaction relationTx, Object entity, String relationName, Optional<FilterExpression> filterExpression, Optional<Sorting> sorting, Optional<Pagination> pagination, RequestScope scope) { Object values = PersistentResource.getValue(entity, relationName, scope); // Gather list of valid id's from this parent Map<String, Object> idToChildResource = new HashMap<>(); if (dictionary.getRelationshipType(entity, relationName).isToOne()) { if (values == null) { return null; } idToChildResource.put(dictionary.getId(values), values); } else if (values instanceof Collection) { idToChildResource.putAll((Map) ((Collection) values).stream() .collect(Collectors.toMap(dictionary::getId, Function.identity()))); } else { throw new IllegalStateException("An unexpected error occurred querying a relationship"); } Class entityClass = dictionary.getParameterizedType(entity, relationName); return processData(entityClass, idToChildResource, filterExpression, sorting, pagination, scope); }
/** * Gets a value from an entity and checks read permissions. * @param fieldName the field name * @return value value */ protected Object getValueChecked(String fieldName) { requestScope.publishLifecycleEvent(this, CRUDEvent.CRUDAction.READ); requestScope.publishLifecycleEvent(this, fieldName, CRUDEvent.CRUDAction.READ, Optional.empty()); checkFieldAwareDeferPermissions(ReadPermission.class, fieldName, (Object) null, (Object) null); return getValue(getObject(), fieldName, requestScope); }
/** * Gets a value from an entity and checks read permissions. * @param fieldName the field name * @return value value */ protected Object getValueChecked(String fieldName) { requestScope.publishLifecycleEvent(this, CRUDEvent.CRUDAction.READ); requestScope.publishLifecycleEvent(this, fieldName, CRUDEvent.CRUDAction.READ, Optional.empty()); checkFieldAwareDeferPermissions(ReadPermission.class, fieldName, (Object) null, (Object) null); return getValue(getObject(), fieldName, requestScope); }
Object val = com.yahoo.elide.core.PersistentResource.getValue(entity, relationName, scope); if (val instanceof Collection) { Collection filteredVal = (Collection) val;
Object val = com.yahoo.elide.core.PersistentResource.getValue(entity, relationName, scope); if (val instanceof Collection) { Collection filteredVal = (Collection) val;
Object val = com.yahoo.elide.core.PersistentResource.getValue(entity, relationName, scope); if (val instanceof Collection) { Collection filteredVal = (Collection) val;