@Override public boolean equals(Object obj) { if (obj instanceof PersistentResource) { PersistentResource that = (PersistentResource) obj; if (this.getObject() == that.getObject()) { return true; } String theirId = dictionary.getId(that.getObject()); return this.matchesId(theirId) && Objects.equals(this.type, that.type); } return false; }
@Override public boolean equals(Object obj) { if (obj instanceof PersistentResource) { PersistentResource that = (PersistentResource) obj; if (this.getObject() == that.getObject()) { return true; } String theirId = dictionary.getId(that.getObject()); return this.matchesId(theirId) && Objects.equals(this.type, that.type); } return false; }
public boolean isNewResource(Object entity) { return newPersistentResources.stream().filter(r -> r.getObject() == entity).findAny().isPresent(); }
public boolean isNewResource(Object entity) { return newPersistentResources.stream().filter(r -> r.getObject() == entity).findAny().isPresent(); }
/** * Get resource ID. * * @return ID id */ @Override public String getId() { return dictionary.getId(getObject()); }
/** * Returns annotations applied to the ID field. * * @return Collection of Annotations */ private Collection<Annotation> getIdAnnotations() { return dictionary.getIdAnnotations(getObject()); }
/** * Get resource ID. * * @return ID id */ @Override public String getId() { return dictionary.getId(getObject()); }
/** * Returns annotations applied to the ID field. * * @return Collection of Annotations */ private Collection<Annotation> getIdAnnotations() { return dictionary.getIdAnnotations(getObject()); }
@Override public int hashCode() { if (hashCode == 0) { // NOTE: UUID's are only present in the case of newly created objects. // Consequently, a known ID will never be present during processing (only after commit // assigned by the DB) and so we can assume that any newly created object can be fully // addressed by its UUID. It is possible for UUID and id to be unset upon a POST or PATCH // ext request, but it is safe to ignore these edge cases. // (1) In a POST request, you would not be referencing this newly created object in any way // so this is not an issue. // (2) In a PATCH ext request, this is also acceptable (assuming request is accepted) in the way // that it is acceptable in a POST. If you do not specify a UUID, there is no way to reference // that newly created object within the context of the request. Thus, if any such action was // required, the user would be forced to provide a UUID anyway. String id = dictionary.getId(getObject()); if (uuid.isPresent() && "0".equals(id)) { hashCode = Objects.hashCode(uuid); } else { hashCode = Objects.hashCode(id); } } return hashCode; }
/** * 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); }
/** * 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); }
/** * Nulls the relationship or attribute and checks update permissions. * Invokes the set[fieldName] method on the target object OR set the field with the corresponding name. * @param fieldName the field name to set or invoke equivalent set method * @param oldValue the old value */ protected void nullValue(String fieldName, PersistentResource oldValue) { if (oldValue == null) { return; } String inverseField = getInverseRelationField(fieldName); if (!inverseField.isEmpty()) { oldValue.checkFieldAwareDeferPermissions(UpdatePermission.class, inverseField, null, getObject()); } this.setValueChecked(fieldName, null); }
/** * Nulls the relationship or attribute and checks update permissions. * Invokes the set[fieldName] method on the target object OR set the field with the corresponding name. * @param fieldName the field name to set or invoke equivalent set method * @param oldValue the old value */ protected void nullValue(String fieldName, PersistentResource oldValue) { if (oldValue == null) { return; } String inverseField = getInverseRelationField(fieldName); if (!inverseField.isEmpty()) { oldValue.checkFieldAwareDeferPermissions(UpdatePermission.class, inverseField, null, getObject()); } this.setValueChecked(fieldName, null); }
/** * 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); }
/** * Add relation link from a given parent resource to a child resource. * * @param fieldName which relation link * @param newRelation the new relation */ public void addRelation(String fieldName, PersistentResource newRelation) { checkSharePermission(Collections.singleton(newRelation)); Object relation = this.getValueUnchecked(fieldName); if (relation instanceof Collection) { if (addToCollection((Collection) relation, fieldName, newRelation)) { this.markDirty(); } //Hook for updateToManyRelation transaction.updateToManyRelation(transaction, obj, fieldName, Sets.newHashSet(newRelation.getObject()), new LinkedHashSet<>(), requestScope); addInverseRelation(fieldName, newRelation.getObject()); } else { // Not a collection, but may be trying to create a ToOne relationship. // NOTE: updateRelation marks dirty. updateRelation(fieldName, Collections.singleton(newRelation)); } }
/** * Assign provided id if id field is not generated. * * @param persistentResource resource * @param id resource id */ private static void assignId(PersistentResource persistentResource, String id) { //If id field is not a `@GeneratedValue` persist the provided id if (!persistentResource.isIdGenerated()) { if (id != null && !id.isEmpty()) { persistentResource.setId(id); } else { //If expecting id to persist and id is not present, throw exception throw new InvalidValueException( persistentResource.toResource(), "No id provided, cannot persist " + persistentResource.getObject() ); } } } }
/** * Assign provided id if id field is not generated. * * @param persistentResource resource * @param id resource id */ private static void assignId(PersistentResource persistentResource, String id) { //If id field is not a `@GeneratedValue` persist the provided id if (!persistentResource.isIdGenerated()) { if (id != null && !id.isEmpty()) { persistentResource.setId(id); } else { //If expecting id to persist and id is not present, throw exception throw new InvalidValueException( persistentResource.toResource(), "No id provided, cannot persist " + persistentResource.getObject() ); } } } }
/** * Get the filter expression for a particular relationship * @param parent The object which has the relationship * @param relationName The relationship name * @return A type specific filter expression for the given relationship */ public Optional<FilterExpression> getExpressionForRelation(PersistentResource parent, String relationName) { final Class<?> entityClass = dictionary.getParameterizedType(parent.getObject(), relationName); if (entityClass == null) { throw new InvalidAttributeException(relationName, parent.getType()); } if (dictionary.isMappedInterface(entityClass) && interfaceHasFilterExpression(entityClass)) { throw new InvalidOperationException( "Cannot apply filters to polymorphic relations mapped with MappedInterface"); } final String valType = dictionary.getJsonAliasFor(entityClass); return getFilterExpressionByType(valType); }
/** * Get the filter expression for a particular relationship * @param parent The object which has the relationship * @param relationName The relationship name * @return A type specific filter expression for the given relationship */ public Optional<FilterExpression> getExpressionForRelation(PersistentResource parent, String relationName) { final Class<?> entityClass = dictionary.getParameterizedType(parent.getObject(), relationName); if (entityClass == null) { throw new InvalidAttributeException(relationName, parent.getType()); } if (dictionary.isMappedInterface(entityClass) && interfaceHasFilterExpression(entityClass)) { throw new InvalidOperationException( "Cannot apply filters to polymorphic relations mapped with MappedInterface"); } final String valType = dictionary.getJsonAliasFor(entityClass); return getFilterExpressionByType(valType); }