T
- type of resourcepublic class PersistentResource<T> extends Object implements PersistentResource<T>
Modifier and Type | Field and Description |
---|---|
static Set<String> |
ALL_FIELDS |
static String |
CLASS_NO_FIELD |
protected EntityDictionary |
dictionary
The Dictionary.
|
protected T |
obj |
Constructor and Description |
---|
PersistentResource(T obj,
PersistentResource parent,
String parentRelationship,
String id,
@NonNull RequestScope scope)
Construct a new resource from the ID provided.
|
PersistentResource(T obj,
String id,
@NonNull RequestScope scope)
Construct a new resource from the ID provided.
|
Modifier and Type | Method and Description |
---|---|
protected void |
addInverseRelation(String relationName,
Object inverseObj)
If a bidirectional relationship exists, attempts to add itself to the inverse
relationship.
|
void |
addRelation(String fieldName,
PersistentResource newRelation)
Add relation link from a given parent resource to a child resource.
|
protected boolean |
addToCollection(Collection collection,
String collectionName,
PersistentResource toAdd)
Adds a new element to a collection and tests update permission.
|
protected void |
auditClass(Audit.Action action,
ChangeSpec changeSpec)
Audit an action on an entity.
|
protected void |
auditField(ChangeSpec changeSpec)
Audit an action on field.
|
protected static boolean |
checkIncludeSparseField(Map<String,Set<String>> sparseFields,
String type,
String fieldName) |
protected boolean |
checkRelation(Relationship relationship)
Check the permissions of the relationship, and return true or false.
|
protected void |
checkTransferablePermission(Set<PersistentResource> resourceIdentifiers)
Check if adding or updating a relation is allowed.
|
boolean |
clearRelation(String relationName)
Clear all elements from a relation.
|
static <T> PersistentResource<T> |
createObject(PersistentResource<?> parent,
String parentRelationship,
Type<T> entityClass,
RequestScope requestScope,
Optional<String> uuid)
Create a resource in the database.
|
static <T> PersistentResource<T> |
createObject(Type<T> entityClass,
RequestScope requestScope,
Optional<String> uuid)
Create a resource in the database.
|
protected void |
deleteInverseRelation(String relationName,
Object inverseEntity)
If a bidirectional relationship exists, attempts to delete itself from the inverse
relationship.
|
void |
deleteResource()
Delete an existing entity.
|
protected void |
delFromCollection(Collection collection,
String collectionName,
PersistentResource toDelete,
boolean isInverseCheck)
Deletes an existing element in a collection and tests update and delete permissions.
|
boolean |
equals(Object obj) |
protected static io.reactivex.Observable<PersistentResource> |
filter(Class<? extends Annotation> permission,
Optional<FilterExpression> filter,
Set<String> requestedFields,
io.reactivex.Observable<PersistentResource> resources)
Filter a set of PersistentResources.
|
protected Set<String> |
filterFields(Collection<String> fields)
Filter a set of fields.
|
static <T> T |
firstOrNullIfEmpty(io.reactivex.Observable<T> coll) |
Object |
getAttribute(Attribute attr)
Get the value for a particular attribute (i.e.
|
Object |
getAttribute(String attr)
Deprecated.
|
protected Map<String,Object> |
getAttributes()
Get attributes mapping from entity.
|
EntityDictionary |
getDictionary()
Gets dictionary.
|
String |
getId()
Get resource ID.
|
ResourceLineage |
getLineage()
Gets lineage.
|
T |
getObject()
Wrapped Entity bean.
|
io.reactivex.Observable<PersistentResource> |
getRelation(List<String> ids,
Relationship relationship)
Load a relation from the PersistentResource.
|
PersistentResource |
getRelation(Relationship relationship,
String id)
Get relation looking for a _single_ id.
|
io.reactivex.Observable<PersistentResource> |
getRelationChecked(Relationship relationship)
Get collection of resources from relation field.
|
io.reactivex.Observable<PersistentResource> |
getRelationCheckedFiltered(Relationship relationship)
Get observable of resources from relation field.
|
protected Map<String,Relationship> |
getRelationships()
Get relationship mappings.
|
protected Map<String,Relationship> |
getRelationshipsWithRelationshipFunction(Function<String,io.reactivex.Observable<PersistentResource>> relationshipFunction)
Get relationship mappings.
|
RelationshipType |
getRelationshipType(String relation)
Get a relationship type.
|
RequestScope |
getRequestScope()
Gets request scope.
|
Type<T> |
getResourceType()
Entity type.
|
String |
getTypeName()
Gets type.
|
Optional<String> |
getUUID()
Gets UUID.
|
static Object |
getValue(Object target,
String fieldName,
RequestScope requestScope)
Invoke the get[fieldName] method on the target object OR get the field with the corresponding name.
|
protected Object |
getValueChecked(Attribute attribute)
Gets a value from an entity and checks read permissions.
|
protected Object |
getValueUnchecked(String fieldName)
Retrieve an object without checking read permissions (i.e.
|
int |
hashCode() |
boolean |
isIdGenerated()
Indicates if the ID is generated or not.
|
boolean |
isNewlyCreated()
Returns whether or not this resource was created in this transaction.
|
static <T> @NonNull PersistentResource<T> |
loadRecord(EntityProjection projection,
String id,
RequestScope requestScope)
Load an single entity from the DB.
|
static io.reactivex.Observable<PersistentResource> |
loadRecords(EntityProjection projection,
List<String> ids,
RequestScope requestScope)
Load a collection from the datastore.
|
boolean |
matchesId(String checkId)
Check whether an id matches for this persistent resource.
|
protected void |
nullValue(String fieldName,
PersistentResource oldValue)
Nulls the relationship or attribute and checks update permissions.
|
boolean |
relationshipAlreadyExists(String fieldName,
PersistentResource toAdd)
Checks whether the new entity is already a part of the relationship.
|
void |
removeRelation(String fieldName,
PersistentResource removeResource)
Remove a relationship.
|
void |
setId(String id)
Set resource ID.
|
void |
setObject(T obj)
Sets object.
|
protected void |
setValue(String fieldName,
Object value)
Invoke the set[fieldName] method on the target object OR set the field with the corresponding name.
|
protected void |
setValueChecked(String fieldName,
Object newValue)
Sets value.
|
Resource |
toResource()
Convert a persistent resource to a resource.
|
Resource |
toResource(EntityProjection projection)
Fetch a resource with support for lambda function for getting relationships and attributes.
|
Resource |
toResource(Map<String,Relationship> relationships,
Map<String,Object> attributes)
Convert a persistent resource to a resource.
|
String |
toString() |
boolean |
updateAttribute(String fieldName,
Object newVal)
Update attribute in existing resource.
|
boolean |
updateRelation(String fieldName,
Set<PersistentResource> resourceIdentifiers)
Perform a full replacement on relationships.
|
protected boolean |
updateToManyRelation(String fieldName,
Set<PersistentResource> resourceIdentifiers,
Set<PersistentResource> mine)
Updates a to-many relationship.
|
protected boolean |
updateToOneRelation(String fieldName,
Set<PersistentResource> resourceIdentifiers,
Set<PersistentResource> mine)
Update a 2-one relationship.
|
protected T obj
public static final String CLASS_NO_FIELD
protected final EntityDictionary dictionary
public PersistentResource(@NonNull T obj, PersistentResource parent, String parentRelationship, String id, @NonNull @NonNull RequestScope scope)
obj
- the objparent
- the parentid
- the idparentRelationship
- The parent relationship traversed to this resource.scope
- the request scopepublic PersistentResource(@NonNull T obj, String id, @NonNull @NonNull RequestScope scope)
obj
- the objid
- the idscope
- the request scopepublic static <T> PersistentResource<T> createObject(Type<T> entityClass, RequestScope requestScope, Optional<String> uuid)
T
- object typeentityClass
- the entity classrequestScope
- the request scopeuuid
- the (optional) uuidpublic static <T> PersistentResource<T> createObject(PersistentResource<?> parent, String parentRelationship, Type<T> entityClass, RequestScope requestScope, Optional<String> uuid)
T
- object typeparent
- - The immediate ancestor in the lineage or null if this is a root.parentRelationship
- - The name of the parent relationship traversed to create this object.entityClass
- the entity classrequestScope
- the request scopeuuid
- the (optional) uuidpublic boolean matchesId(String checkId)
matchesId
in interface PersistentResource<T>
checkId
- the check id@NonNull public static <T> @NonNull PersistentResource<T> loadRecord(EntityProjection projection, String id, RequestScope requestScope) throws InvalidObjectIdentifierException
T
- type of resourceprojection
- What to load from the DB.id
- the idrequestScope
- the request scopeInvalidObjectIdentifierException
- the invalid object identifier exceptionpublic static io.reactivex.Observable<PersistentResource> loadRecords(EntityProjection projection, List<String> ids, RequestScope requestScope)
projection
- the projection to loadrequestScope
- the request scopeids
- a list of object identifiers to optionally load. Can be empty.public boolean updateAttribute(String fieldName, Object newVal)
fieldName
- the field namenewVal
- the new valpublic boolean updateRelation(String fieldName, Set<PersistentResource> resourceIdentifiers)
fieldName
- the field nameresourceIdentifiers
- the resource identifiersprotected boolean updateToManyRelation(String fieldName, Set<PersistentResource> resourceIdentifiers, Set<PersistentResource> mine)
fieldName
- the field nameresourceIdentifiers
- the resource identifiersmine
- Existing, filtered relationships for field nameprotected boolean updateToOneRelation(String fieldName, Set<PersistentResource> resourceIdentifiers, Set<PersistentResource> mine)
fieldName
- the field nameresourceIdentifiers
- the resource identifiersmine
- Existing, filtered relationships for field namepublic boolean clearRelation(String relationName)
relationName
- Name of relation to clearpublic void removeRelation(String fieldName, PersistentResource removeResource)
fieldName
- the field nameremoveResource
- the remove resourcepublic boolean relationshipAlreadyExists(String fieldName, PersistentResource toAdd)
fieldName
- which relation linktoAdd
- the new relationpublic void addRelation(String fieldName, PersistentResource newRelation)
fieldName
- which relation linknewRelation
- the new relationprotected void checkTransferablePermission(Set<PersistentResource> resourceIdentifiers)
resourceIdentifiers
- The persistent resources that are being addedpublic void deleteResource() throws ForbiddenAccessException
ForbiddenAccessException
- the forbidden access exceptionpublic String getId()
getId
in interface PersistentResource<T>
public void setId(String id)
id
- resource idpublic boolean isIdGenerated()
public Optional<String> getUUID()
getUUID
in interface PersistentResource<T>
public PersistentResource getRelation(Relationship relationship, String id)
relationship
- The relationshipid
- single id to lookuppublic io.reactivex.Observable<PersistentResource> getRelation(List<String> ids, Relationship relationship)
relationship
- the relationids
- a list of object identifiers to optionally load. Can be empty.public io.reactivex.Observable<PersistentResource> getRelationCheckedFiltered(Relationship relationship)
relationship
- relationshipprotected boolean checkRelation(Relationship relationship)
relationship
- The relationship to the entitypublic io.reactivex.Observable<PersistentResource> getRelationChecked(Relationship relationship)
relationship
- the relationship to fetchpublic RelationshipType getRelationshipType(String relation)
relation
- Name of relationship@Deprecated public Object getAttribute(String attr)
attr
- Attribute namepublic Object getAttribute(Attribute attr)
attr
- the Attributepublic T getObject()
getObject
in interface PersistentResource<T>
public void setObject(T obj)
obj
- the objpublic Type<T> getResourceType()
getResourceType
in interface PersistentResource<T>
public String getTypeName()
getTypeName
in interface PersistentResource<T>
public boolean isNewlyCreated()
public ResourceLineage getLineage()
public EntityDictionary getDictionary()
public RequestScope getRequestScope()
getRequestScope
in interface PersistentResource<T>
public Resource toResource()
public Resource toResource(EntityProjection projection)
public Resource toResource(Map<String,Relationship> relationships, Map<String,Object> attributes)
relationships
- The relationshipsattributes
- The attributesprotected Map<String,Relationship> getRelationships()
protected Map<String,Relationship> getRelationshipsWithRelationshipFunction(Function<String,io.reactivex.Observable<PersistentResource>> relationshipFunction)
relationshipFunction
- a function to load the value of a relationship. Takes a string of the relationship
name and returns the relationship's value.protected Map<String,Object> getAttributes()
protected void setValueChecked(String fieldName, Object newValue)
fieldName
- the field namenewValue
- the new valueprotected void nullValue(String fieldName, PersistentResource oldValue)
fieldName
- the field name to set or invoke equivalent set methodoldValue
- the old valueprotected Object getValueChecked(Attribute attribute)
attribute
- the attribute to fetch.protected Object getValueUnchecked(String fieldName)
fieldName
- the field nameprotected boolean addToCollection(Collection collection, String collectionName, PersistentResource toAdd)
collection
- the collectioncollectionName
- the collection nametoAdd
- the to addprotected void delFromCollection(Collection collection, String collectionName, PersistentResource toDelete, boolean isInverseCheck)
collection
- the collectioncollectionName
- the collection nametoDelete
- the to deleteisInverseCheck
- Whether or not the deletion is already coming from cleaning up an inverse.
Without this parameter, we could find ourselves in a loop of checks.
TODO: This is a band-aid for a quick fix. This should certainly be refactored.protected void setValue(String fieldName, Object value)
fieldName
- the field name to set or invoke equivalent set methodvalue
- the value to setpublic static Object getValue(Object target, String fieldName, RequestScope requestScope)
target
- the object to getfieldName
- the field name to get or invoke equivalent get methodrequestScope
- the request scopeprotected void deleteInverseRelation(String relationName, Object inverseEntity)
relationName
- The name of the relationship on this (A) object.inverseEntity
- The value (B) which has been deleted from this object.protected void addInverseRelation(String relationName, Object inverseObj)
relationName
- The name of the relationship on this (A) object.inverseObj
- The value (B) which has been added to this object.protected static io.reactivex.Observable<PersistentResource> filter(Class<? extends Annotation> permission, Optional<FilterExpression> filter, Set<String> requestedFields, io.reactivex.Observable<PersistentResource> resources)
permission
- the permissionresources
- the resourcesprotected Set<String> filterFields(Collection<String> fields)
fields
- the fieldsprotected static boolean checkIncludeSparseField(Map<String,Set<String>> sparseFields, String type, String fieldName)
protected void auditField(ChangeSpec changeSpec)
changeSpec
- Change spec for auditprotected void auditClass(Audit.Action action, ChangeSpec changeSpec)
action
- the actionchangeSpec
- the change that occurredpublic static <T> T firstOrNullIfEmpty(io.reactivex.Observable<T> coll)
Copyright © 2015–2021 Yahoo! Inc.. All rights reserved.