@Test public void testAddBidirectionalRelation() { Left left = new Left(); Right right = new Right(); PersistentResource<Left> leftResource = new PersistentResource<>(left, null, "3", goodUserScope); leftResource.addInverseRelation("one2one", right); assertEquals(left, right.getOne2one(), "The one-2-one inverse relationship should have been updated."); Child child = new Child(); Parent parent = new Parent(); child.setParents(Sets.newHashSet()); parent.setChildren(Sets.newHashSet()); parent.setSpouses(Sets.newHashSet()); PersistentResource<Child> childResource = new PersistentResource<>(child, null, "4", goodUserScope); childResource.addInverseRelation("parents", parent); assertEquals(1, parent.getChildren().size(), "The many-2-many inverse relationship should have been updated"); assertTrue(parent.getChildren().contains(child), "The many-2-many inverse relationship should have been updated"); }
protected void addInverseRelation(String relationName, Object inverseObj) { String inverseName = dictionary.getRelationInverse(obj.getClass(), relationName); if (!"".equals(inverseName)) { Class<?> inverseType = dictionary.getType(inverseObj.getClass(), inverseName); String uuid = requestScope.getUUIDFor(inverseObj); PersistentResource inverseResource = new PersistentResource(inverseObj, this, uuid, requestScope); Object inverseRelation = inverseResource.getValueUnchecked(inverseName); if (Collection.class.isAssignableFrom(inverseType)) { if (inverseRelation != null) { inverseResource.addToCollection((Collection) inverseRelation, inverseName, this); } else { inverseResource.setValueChecked(inverseName, Collections.singleton(this.getObject())); } } else if (inverseType.isAssignableFrom(this.getResourceClass())) { inverseResource.setValueChecked(inverseName, this.getObject()); } else { throw new InternalServerErrorException("Relationship type mismatch"); } inverseResource.markDirty(); RelationshipType inverseRelationType = inverseResource.getRelationshipType(inverseName); if (inverseRelationType.isToOne()) { transaction.updateToOneRelation(transaction, inverseObj, inverseName, obj, requestScope); } else { assert (inverseRelation == null || inverseRelation instanceof Collection) : inverseName + " not a collection"; transaction.updateToManyRelation(transaction, inverseObj, inverseName, Sets.newHashSet(obj), new LinkedHashSet<>(), requestScope); } } }
PersistentResource implements com.yahoo.elide.security.PersistentResource<T> { protected void addInverseRelation(String relationName, Object inverseObj) { String inverseName = dictionary.getRelationInverse(obj.getClass(), relationName); if (!"".equals(inverseName)) { Class<?> inverseType = dictionary.getType(inverseObj.getClass(), inverseName); String uuid = requestScope.getUUIDFor(inverseObj); PersistentResource inverseResource = new PersistentResource(inverseObj, this, uuid, requestScope); Object inverseRelation = inverseResource.getValueUnchecked(inverseName); if (Collection.class.isAssignableFrom(inverseType)) { if (inverseRelation != null) { inverseResource.addToCollection((Collection) inverseRelation, inverseName, this); } else { inverseResource.setValueChecked(inverseName, Collections.singleton(this.getObject())); } } else if (inverseType.isAssignableFrom(this.getResourceClass())) { inverseResource.setValueChecked(inverseName, this.getObject()); } else { throw new InternalServerErrorException("Relationship type mismatch"); } inverseResource.markDirty(); RelationshipType inverseRelationType = inverseResource.getRelationshipType(inverseName); if (inverseRelationType.isToOne()) { transaction.updateToOneRelation(transaction, inverseObj, inverseName, obj, requestScope); } else { assert (inverseRelation == null || inverseRelation instanceof Collection) : inverseName + " not a collection"; transaction.updateToManyRelation(transaction, inverseObj, inverseName, Sets.newHashSet(obj), new LinkedHashSet<>(), requestScope); } } } }
PersistentResource implements com.yahoo.elide.security.PersistentResource<T> { protected void addInverseRelation(String relationName, Object inverseObj) { String inverseName = dictionary.getRelationInverse(obj.getClass(), relationName); if (!"".equals(inverseName)) { Class<?> inverseType = dictionary.getType(inverseObj.getClass(), inverseName); String uuid = requestScope.getUUIDFor(inverseObj); PersistentResource inverseResource = new PersistentResource(inverseObj, this, uuid, requestScope); Object inverseRelation = inverseResource.getValueUnchecked(inverseName); if (Collection.class.isAssignableFrom(inverseType)) { if (inverseRelation != null) { inverseResource.addToCollection((Collection) inverseRelation, inverseName, this); } else { inverseResource.setValueChecked(inverseName, Collections.singleton(this.getObject())); } } else if (inverseType.isAssignableFrom(this.getResourceClass())) { inverseResource.setValueChecked(inverseName, this.getObject()); } else { throw new InternalServerErrorException("Relationship type mismatch"); } inverseResource.markDirty(); RelationshipType inverseRelationType = inverseResource.getRelationshipType(inverseName); if (inverseRelationType.isToOne()) { transaction.updateToOneRelation(transaction, inverseObj, inverseName, obj, requestScope); } else { assert (inverseRelation == null || inverseRelation instanceof Collection) : inverseName + " not a collection"; transaction.updateToManyRelation(transaction, inverseObj, inverseName, Sets.newHashSet(obj), new LinkedHashSet<>(), requestScope); } } } PersistentResource(@NonNull T obj, PersistentResource parent, String id, @NonNull RequestScope scope); }
PersistentResource implements com.yahoo.elide.security.PersistentResource<T> { protected void addInverseRelation(String relationName, Object inverseObj) { String inverseName = dictionary.getRelationInverse(obj.getClass(), relationName); if (!"".equals(inverseName)) { Class<?> inverseType = dictionary.getType(inverseObj.getClass(), inverseName); String uuid = requestScope.getUUIDFor(inverseObj); PersistentResource inverseResource = new PersistentResource(inverseObj, this, uuid, requestScope); Object inverseRelation = inverseResource.getValueUnchecked(inverseName); if (Collection.class.isAssignableFrom(inverseType)) { if (inverseRelation != null) { inverseResource.addToCollection((Collection) inverseRelation, inverseName, this); } else { inverseResource.setValueChecked(inverseName, Collections.singleton(this.getObject())); } } else if (inverseType.isAssignableFrom(this.getResourceClass())) { inverseResource.setValueChecked(inverseName, this.getObject()); } else { throw new InternalServerErrorException("Relationship type mismatch"); } inverseResource.markDirty(); RelationshipType inverseRelationType = inverseResource.getRelationshipType(inverseName); if (inverseRelationType.isToOne()) { transaction.updateToOneRelation(transaction, inverseObj, inverseName, obj, requestScope); } else { assert (inverseRelation == null || inverseRelation instanceof Collection) : inverseName + " not a collection"; transaction.updateToManyRelation(transaction, inverseObj, inverseName, Sets.newHashSet(obj), new LinkedHashSet<>(), requestScope); } } } PersistentResource(@NonNull T obj, PersistentResource parent, String id, @NonNull RequestScope scope); @Override String toString(); static PersistentResource<T> createObject(
            PersistentResource<?> parent,
            Class<T> entityClass,
            RequestScope requestScope,
            Optional<String> uuid); @Override boolean matchesId(String checkId); @SuppressWarnings("resource") @NonNull static PersistentResource<T> loadRecord(
            Class<T> loadClass, String id, RequestScope requestScope); static Set<PersistentResource> loadRecords(
            Class<?> loadClass,
            List<String> ids,
            Optional<FilterExpression> filter,
            Optional<Sorting> sorting,
            Optional<Pagination> pagination,
            RequestScope requestScope); boolean updateAttribute(String fieldName, Object newVal); boolean updateRelation(String fieldName, Set<PersistentResource> resourceIdentifiers); boolean clearRelation(String relationName); void removeRelation(String fieldName, PersistentResource removeResource); boolean relationshipAlreadyExists(String fieldName, PersistentResource toAdd); void addRelation(String fieldName, PersistentResource newRelation); void deleteResource(); @Override String getId(); void setId(String id); boolean isIdGenerated(); @Override Optional<String> getUUID(); PersistentResource getRelation(String relation, String id); Set<PersistentResource> getRelation(String relation,
            List<String> ids,
            Optional<FilterExpression> filter,
            Optional<Sorting> sorting,
            Optional<Pagination> pagination); Set<PersistentResource> getRelationCheckedFiltered(String relationName,
            Optional<FilterExpression> filterExpression,
            Optional<Sorting> sorting,
            Optional<Pagination> pagination); RelationshipType getRelationshipType(String relation); Object getAttribute(String attr); @Override T getObject(); void setObject(T obj); @Override @JsonIgnore Class<T> getResourceClass(); @Override String getType(); @Override int hashCode(); @Override boolean equals(Object obj); ResourceLineage getLineage(); EntityDictionary getDictionary(); @Override RequestScope getRequestScope(); Resource toResource(); Resource toResourceWithSortingAndPagination(); Resource toResource(final Supplier<Map<String, Relationship>> relationshipSupplier,
                               final Supplier<Map<String, Object>> attributeSupplier); static Object getValue(Object target, String fieldName, RequestScope requestScope); }
PersistentResource implements com.yahoo.elide.security.PersistentResource<T> { protected void addInverseRelation(String relationName, Object inverseObj) { String inverseName = dictionary.getRelationInverse(obj.getClass(), relationName); if (!"".equals(inverseName)) { Class<?> inverseType = dictionary.getType(inverseObj.getClass(), inverseName); String uuid = requestScope.getUUIDFor(inverseObj); PersistentResource inverseResource = new PersistentResource(inverseObj, this, uuid, requestScope); Object inverseRelation = inverseResource.getValueUnchecked(inverseName); if (Collection.class.isAssignableFrom(inverseType)) { if (inverseRelation != null) { inverseResource.addToCollection((Collection) inverseRelation, inverseName, this); } else { inverseResource.setValueChecked(inverseName, Collections.singleton(this.getObject())); } } else if (inverseType.isAssignableFrom(this.getResourceClass())) { inverseResource.setValueChecked(inverseName, this.getObject()); } else { throw new InternalServerErrorException("Relationship type mismatch"); } inverseResource.markDirty(); RelationshipType inverseRelationType = inverseResource.getRelationshipType(inverseName); if (inverseRelationType.isToOne()) { transaction.updateToOneRelation(transaction, inverseObj, inverseName, obj, requestScope); } else { assert (inverseRelation == null || inverseRelation instanceof Collection) : inverseName + " not a collection"; transaction.updateToManyRelation(transaction, inverseObj, inverseName, Sets.newHashSet(obj), new LinkedHashSet<>(), requestScope); } } } PersistentResource(@NonNull T obj, PersistentResource parent, String id, @NonNull RequestScope scope); @Override String toString(); static PersistentResource<T> createObject(
            PersistentResource<?> parent,
            Class<T> entityClass,
            RequestScope requestScope,
            Optional<String> uuid); @Override boolean matchesId(String checkId); @SuppressWarnings("resource") @NonNull static PersistentResource<T> loadRecord(
            Class<T> loadClass, String id, RequestScope requestScope); static Set<PersistentResource> loadRecords(
            Class<?> loadClass,
            List<String> ids,
            Optional<FilterExpression> filter,
            Optional<Sorting> sorting,
            Optional<Pagination> pagination,
            RequestScope requestScope); boolean updateAttribute(String fieldName, Object newVal); boolean updateRelation(String fieldName, Set<PersistentResource> resourceIdentifiers); boolean clearRelation(String relationName); void removeRelation(String fieldName, PersistentResource removeResource); boolean relationshipAlreadyExists(String fieldName, PersistentResource toAdd); void addRelation(String fieldName, PersistentResource newRelation); void deleteResource(); @Override String getId(); void setId(String id); boolean isIdGenerated(); @Override Optional<String> getUUID(); PersistentResource getRelation(String relation, String id); Set<PersistentResource> getRelation(String relation,
            List<String> ids,
            Optional<FilterExpression> filter,
            Optional<Sorting> sorting,
            Optional<Pagination> pagination); Set<PersistentResource> getRelationCheckedFiltered(String relationName,
            Optional<FilterExpression> filterExpression,
            Optional<Sorting> sorting,
            Optional<Pagination> pagination); RelationshipType getRelationshipType(String relation); Object getAttribute(String attr); @Override T getObject(); void setObject(T obj); @Override @JsonIgnore Class<T> getResourceClass(); @Override String getType(); @Override int hashCode(); @Override boolean equals(Object obj); ResourceLineage getLineage(); EntityDictionary getDictionary(); @Override RequestScope getRequestScope(); Resource toResource(); Resource toResourceWithSortingAndPagination(); Resource toResource(final Supplier<Map<String, Relationship>> relationshipSupplier,
                               final Supplier<Map<String, Object>> attributeSupplier); static Object getValue(Object target, String fieldName, RequestScope requestScope);  }