/** * Extract the relationships of the entity. */ private void setRelationships() { if (this.data != null) { this.relationships = new LinkedHashSet<>(); EntityDictionary dictionary = this.requestScope.getDictionary(); for (Map.Entry<String, Object> entry : this.data.entrySet()) { if (dictionary.isRelation(this.entityClass, entry.getKey())) { Set<Entity> entitySet = new LinkedHashSet<>(); Class<?> loadClass = dictionary.getParameterizedType(this.entityClass, entry.getKey()); Boolean isToOne = dictionary.getRelationshipType(this.entityClass, entry.getKey()).isToOne(); if (isToOne) { entitySet.add(new Entity(Optional.of(this), ((Map<String, Object>) entry.getValue()), loadClass, this.requestScope)); } else { for (Map<String, Object> row : (List<Map<String, Object>>) entry.getValue()) { entitySet.add(new Entity(Optional.of(this), row, loadClass, this.requestScope)); } } this.relationships.add(new Relationship(entry.getKey(), entitySet)); } } } }
/** * Extract the relationships of the entity. */ private void setRelationships() { if (this.data != null) { this.relationships = new LinkedHashSet<>(); EntityDictionary dictionary = this.requestScope.getDictionary(); for (Map.Entry<String, Object> entry : this.data.entrySet()) { if (dictionary.isRelation(this.entityClass, entry.getKey())) { Set<Entity> entitySet = new LinkedHashSet<>(); Class<?> loadClass = dictionary.getParameterizedType(this.entityClass, entry.getKey()); Boolean isToOne = dictionary.getRelationshipType(this.entityClass, entry.getKey()).isToOne(); if (isToOne) { entitySet.add(new Entity(Optional.of(this), ((Map<String, Object>) entry.getValue()), loadClass, this.requestScope)); } else { for (Map<String, Object> row : (List<Map<String, Object>>) entry.getValue()) { entitySet.add(new Entity(Optional.of(this), row, loadClass, this.requestScope)); } } this.relationships.add(new Relationship(entry.getKey(), entitySet)); } } } }
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); }
if (! dictionary.isRelation(pathElement.getType(), fieldName)) { return joinClause.toString();
@Override public Object processFetch(Environment context, PersistentResourceFetcher fetcher) { EntityDictionary dictionary = context.requestScope.getDictionary(); Class parentClass = context.parentResource.getResourceClass(); String fieldName = context.field.getName(); String idFieldName = dictionary.getIdFieldName(parentClass); if (dictionary.isAttribute(parentClass, fieldName)) { /* fetch attribute properties */ Object attribute = context.parentResource.getAttribute(fieldName); if (attribute instanceof Map) { return ((Map<Object, Object>) attribute).entrySet().stream() .map(MapEntryContainer::new) .collect(Collectors.toList()); } return attribute; } if (dictionary.isRelation(parentClass, fieldName)) { /* fetch relationship properties */ boolean generateTotals = requestContainsPageInfo(context.field); return fetcher.fetchRelationship(context, context.parentResource, fieldName, context.ids, context.offset, context.first, context.sort, context.filters, generateTotals); } if (Objects.equals(idFieldName, fieldName)) { return new DeferredId(context.parentResource); } throw new BadRequestException("Unrecognized object: " + fieldName + " for: " + parentClass.getName() + " in node"); } }
@Override public Object processFetch(Environment context, PersistentResourceFetcher fetcher) { EntityDictionary dictionary = context.requestScope.getDictionary(); Class parentClass = context.parentResource.getResourceClass(); String fieldName = context.field.getName(); String idFieldName = dictionary.getIdFieldName(parentClass); if (dictionary.isAttribute(parentClass, fieldName)) { /* fetch attribute properties */ Object attribute = context.parentResource.getAttribute(fieldName); if (attribute instanceof Map) { return ((Map<Object, Object>) attribute).entrySet().stream() .map(MapEntryContainer::new) .collect(Collectors.toList()); } return attribute; } if (dictionary.isRelation(parentClass, fieldName)) { /* fetch relationship properties */ boolean generateTotals = requestContainsPageInfo(context.field); return fetcher.fetchRelationship(context, context.parentResource, fieldName, context.ids, context.offset, context.first, context.sort, context.filters, generateTotals); } if (Objects.equals(idFieldName, fieldName)) { return new DeferredId(context.parentResource); } throw new BadRequestException("Unrecognized object: " + fieldName + " for: " + parentClass.getName() + " in node"); } }
/** * Validates that none of the provided path's relationships are to-many * @param path The path to validate * @param dictionary The elide entity dictionary * @return True if the path is valid. False otherwise. */ protected static boolean isValidSortRulePath(Path path, EntityDictionary dictionary) { //Validate that none of the relationships are to-many for (Path.PathElement pathElement : path.getPathElements()) { if (! dictionary.isRelation(pathElement.getType(), pathElement.getFieldName())) { continue; } if (dictionary.getRelationshipType(pathElement.getType(), pathElement.getFieldName()).isToMany()) { return false; } } return true; }
/** * Validates that none of the provided path's relationships are to-many * @param path The path to validate * @param dictionary The elide entity dictionary * @return True if the path is valid. False otherwise. */ protected static boolean isValidSortRulePath(Path path, EntityDictionary dictionary) { //Validate that none of the relationships are to-many for (Path.PathElement pathElement : path.getPathElements()) { if (! dictionary.isRelation(pathElement.getType(), pathElement.getFieldName())) { continue; } if (dictionary.getRelationshipType(pathElement.getType(), pathElement.getFieldName()).isToMany()) { return false; } } return true; }