/** * Extract the attributes of the entity. */ private void setAttributes() { if (this.data != null) { this.attributes = new LinkedHashSet<>(); EntityDictionary dictionary = this.requestScope.getDictionary(); String idFieldName = dictionary.getIdFieldName(this.entityClass); for (Map.Entry<String, Object> entry : this.data.entrySet()) { if (dictionary.isAttribute(this.entityClass, entry.getKey())) { this.attributes.add(new Attribute(entry.getKey(), entry.getValue())); } if (Objects.equals(entry.getKey(), idFieldName)) { this.attributes.add(new Attribute(entry.getKey(), entry.getValue())); } } } }
/** * Extract the attributes of the entity. */ private void setAttributes() { if (this.data != null) { this.attributes = new LinkedHashSet<>(); EntityDictionary dictionary = this.requestScope.getDictionary(); String idFieldName = dictionary.getIdFieldName(this.entityClass); for (Map.Entry<String, Object> entry : this.data.entrySet()) { if (dictionary.isAttribute(this.entityClass, entry.getKey())) { this.attributes.add(new Attribute(entry.getKey(), entry.getValue())); } if (Objects.equals(entry.getKey(), idFieldName)) { this.attributes.add(new Attribute(entry.getKey(), entry.getValue())); } } } }
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); }
@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"); } }
/** * 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; }
/** * 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; }
/** * Updates an object * @param toUpdate Entities to update * @param entity Resource entity * @param attributes Set of entity attributes * @return Persistence Resource object */ private PersistentResource updateAttributes(PersistentResource toUpdate, Entity entity, Set<Entity.Attribute> attributes) { EntityDictionary dictionary = entity.getRequestScope().getDictionary(); Class<?> entityClass = entity.getEntityClass(); String idFieldName = dictionary.getIdFieldName(entityClass); /* iterate through each attribute provided */ for (Entity.Attribute attribute : attributes) { if (dictionary.isAttribute(entityClass, attribute.getName())) { toUpdate.updateAttribute(attribute.getName(), attribute.getValue()); } else if (!Objects.equals(attribute.getName(), idFieldName)) { throw new IllegalStateException("Unrecognized attribute passed to 'data': " + attribute.getName()); } } return toUpdate; }
/** * Updates an object * @param toUpdate Entities to update * @param entity Resource entity * @param attributes Set of entity attributes * @return Persistence Resource object */ private PersistentResource updateAttributes(PersistentResource toUpdate, Entity entity, Set<Entity.Attribute> attributes) { EntityDictionary dictionary = entity.getRequestScope().getDictionary(); Class<?> entityClass = entity.getEntityClass(); String idFieldName = dictionary.getIdFieldName(entityClass); /* iterate through each attribute provided */ for (Entity.Attribute attribute : attributes) { if (dictionary.isAttribute(entityClass, attribute.getName())) { toUpdate.updateAttribute(attribute.getName(), attribute.getValue()); } else if (!Objects.equals(attribute.getName(), idFieldName)) { throw new IllegalStateException("Unrecognized attribute passed to 'data': " + attribute.getName()); } } return toUpdate; }