public class DataContextImpl extends java.lang.Object implements DataContext
DataContext
which commits data to DataManager
.Modifier and Type | Class and Description |
---|---|
protected class |
DataContextImpl.EmbeddedPropertyChangeListener |
protected class |
DataContextImpl.PropertyChangeListener |
DataContext.ChangeEvent, DataContext.PostCommitEvent, DataContext.PreCommitEvent
Modifier and Type | Field and Description |
---|---|
protected org.springframework.context.ApplicationContext |
applicationContext |
protected java.util.function.Function<CommitContext,java.util.Set<Entity>> |
commitDelegate |
protected java.util.Map<java.lang.Class<?>,java.util.Map<java.lang.Object,Entity>> |
content |
protected boolean |
disableListeners |
protected java.util.Map<Entity,java.util.Map<java.lang.String,DataContextImpl.EmbeddedPropertyChangeListener>> |
embeddedPropertyListeners |
protected EventHub |
events |
protected java.util.Set<Entity> |
modifiedInstances |
protected DataContextImpl |
parentContext |
protected DataContextImpl.PropertyChangeListener |
propertyChangeListener |
protected java.util.Set<Entity> |
removedInstances |
Constructor and Description |
---|
DataContextImpl(org.springframework.context.ApplicationContext applicationContext) |
Modifier and Type | Method and Description |
---|---|
Subscription |
addChangeListener(java.util.function.Consumer<DataContext.ChangeEvent> listener)
Adds a listener to
DataContext.ChangeEvent . |
Subscription |
addPostCommitListener(java.util.function.Consumer<DataContext.PostCommitEvent> listener)
Adds a listener to
DataContext.PostCommitEvent . |
Subscription |
addPreCommitListener(java.util.function.Consumer<DataContext.PreCommitEvent> listener)
Adds a listener to
DataContext.PreCommitEvent . |
protected void |
cleanupContextAfterRemoveEntity(DataContextImpl context,
Entity removedEntity) |
void |
clear()
Evicts all tracked entities.
|
EntitySet |
commit()
Commits changed and removed instances to the middleware.
|
protected java.util.Set<Entity> |
commitToDataManager(CommitContext.ValidationMode validationMode,
java.util.List<java.lang.Class> validationGroups) |
protected java.util.Set<Entity> |
commitToParentContext() |
boolean |
contains(Entity entity)
Returns true if the context contains the given entity (distinguished by its class and id).
|
protected Entity |
copyEntity(Entity srcEntity) |
protected void |
copySystemState(Entity srcEntity,
Entity dstEntity) |
<T extends Entity> |
create(java.lang.Class<T> entityClass)
Creates an entity instance and merge it into the context.
|
protected java.util.List<Entity> |
createObservableList(Entity notifiedEntity) |
protected java.util.List<Entity> |
createObservableList(java.util.List<Entity> list,
Entity notifiedEntity) |
protected java.util.Set<Entity> |
createObservableSet(Entity notifiedEntity) |
protected ObservableSet<Entity> |
createObservableSet(java.util.Set<Entity> set,
Entity notifiedEntity) |
protected boolean |
entityHasReference(Entity entity,
Entity refEntity) |
void |
evict(Entity entity)
Removes the entity from the context so the context stops tracking it.
|
void |
evictModified()
Clears the lists of created/modified/deleted entities and evicts these entities.
|
protected java.util.Collection |
filterCommittedInstances(java.util.Set<Entity> instances) |
<T extends Entity<K>,K> |
find(java.lang.Class<T> entityClass,
K entityId)
Returns an entity instance by its class and id.
|
<T extends Entity> |
find(T entity)
Returns the instance of entity with the same id if it exists in this context.
|
protected void |
fireChangeListener(Entity entity) |
java.util.Collection<Entity> |
getAll() |
java.util.function.Function<CommitContext,java.util.Set<Entity>> |
getCommitDelegate()
Returns a function which will be used to commit data instead of standard implementation.
|
protected DataManager |
getDataManager() |
protected EntityReferencesNormalizer |
getEntityReferencesNormalizer() |
protected EntityStates |
getEntityStates() |
protected Metadata |
getMetadata() |
protected MetadataTools |
getMetadataTools() |
java.util.Set<Entity> |
getModified()
Returns an immutable set of entities registered as modified.
|
DataContext |
getParent()
Returns a parent context, if any.
|
java.util.Set<Entity> |
getRemoved()
Returns an immutable set of entities registered for removal.
|
boolean |
hasChanges()
Returns true if the context has detected changes in the tracked entities.
|
protected Entity |
internalMerge(Entity entity,
java.util.Set<Entity> mergedSet,
boolean isRoot,
MergeOptions options) |
boolean |
isModified(Entity entity)
Returns true if the context has detected changes in the given entity.
|
boolean |
isRemoved(Entity entity)
Returns true if the context has registered removal of the given entity.
|
EntitySet |
merge(java.util.Collection<? extends Entity> entities)
Merge the given entities into the context.
|
EntitySet |
merge(java.util.Collection<? extends Entity> entities,
MergeOptions options)
Merge the given entities into the context.
|
<T extends Entity> |
merge(T entity)
Same as
DataContext.merge(Entity, MergeOptions) with default options. |
<T extends Entity> |
merge(T entity,
MergeOptions options)
Merge the given entity into the context.
|
protected EntitySet |
mergeCommitted(java.util.Set<Entity> committed) |
protected void |
mergeList(java.util.List<Entity> list,
Entity managedEntity,
MetaProperty property,
boolean replace,
MergeOptions options,
java.util.Set<Entity> mergedSet) |
protected void |
mergeSet(java.util.Set<Entity> set,
Entity managedEntity,
MetaProperty property,
boolean replace,
MergeOptions options,
java.util.Set<Entity> mergedSet) |
protected void |
mergeState(Entity srcEntity,
Entity dstEntity,
java.util.Set<Entity> mergedSet,
boolean isRoot,
MergeOptions options) |
protected void |
mergeSystemState(Entity srcEntity,
Entity dstEntity,
boolean isRoot,
MergeOptions options) |
protected void |
modified(Entity entity) |
protected java.util.Set<Entity> |
performCommit(CommitContext.ValidationMode validationMode,
java.util.List<java.lang.Class> validationGroups) |
java.lang.String |
printContent() |
protected java.lang.String |
printEntity(Entity entity,
int level,
java.util.Set<Entity> visited) |
protected java.lang.String |
printObject(java.lang.Object object) |
void |
remove(Entity entity)
Removes the entity from the context and registers it as deleted.
|
protected void |
removeFromCollections(Entity entityToRemove) |
protected void |
removeListeners(Entity entity) |
void |
setCommitDelegate(java.util.function.Function<CommitContext,java.util.Set<Entity>> delegate)
Sets a function which will be used to commit data instead of standard implementation.
|
void |
setModified(Entity entity,
boolean modified)
Registers or unregisters the given entity as modified.
|
void |
setParent(DataContext parentContext)
Sets the parent context.
|
protected void |
setPropertyValue(Entity entity,
MetaProperty property,
java.lang.Object value) |
protected void |
setPropertyValue(Entity entity,
MetaProperty property,
java.lang.Object value,
boolean checkEquals) |
protected org.springframework.context.ApplicationContext applicationContext
protected EventHub events
protected java.util.Map<java.lang.Class<?>,java.util.Map<java.lang.Object,Entity>> content
protected java.util.Set<Entity> modifiedInstances
protected java.util.Set<Entity> removedInstances
protected DataContextImpl.PropertyChangeListener propertyChangeListener
protected boolean disableListeners
protected DataContextImpl parentContext
protected java.util.function.Function<CommitContext,java.util.Set<Entity>> commitDelegate
protected java.util.Map<Entity,java.util.Map<java.lang.String,DataContextImpl.EmbeddedPropertyChangeListener>> embeddedPropertyListeners
public DataContextImpl(org.springframework.context.ApplicationContext applicationContext)
protected Metadata getMetadata()
protected MetadataTools getMetadataTools()
protected EntityStates getEntityStates()
protected DataManager getDataManager()
protected EntityReferencesNormalizer getEntityReferencesNormalizer()
@Nullable public DataContext getParent()
DataContext
DataContext.commit()
method merges the changed instances
to it instead of sending to the middleware.getParent
in interface DataContext
public void setParent(DataContext parentContext)
DataContext
DataContext.commit()
method merges the changed instances
to it instead of sending to the middleware.setParent
in interface DataContext
public Subscription addChangeListener(java.util.function.Consumer<DataContext.ChangeEvent> listener)
DataContext
DataContext.ChangeEvent
.addChangeListener
in interface DataContext
protected void fireChangeListener(Entity entity)
@Nullable public <T extends Entity<K>,K> T find(java.lang.Class<T> entityClass, K entityId)
DataContext
find
in interface DataContext
@Nullable public <T extends Entity> T find(T entity)
DataContext
find
in interface DataContext
public boolean contains(Entity entity)
DataContext
contains
in interface DataContext
public <T extends Entity> T merge(T entity, MergeOptions options)
DataContext
If an entity with the same identifier already exists in the context, the passed entity state is copied into it and the existing instance is returned. Otherwise, a copy of the passed instance is registered in the context and returned.
If the given instance is new and the context doesn't contain an instance with the same identifier, the context
will save the new instance on DataContext.commit()
. Otherwise, even if some attributes of the merged instance are changed
as a result of copying the state of the passed instance, the merged instance will not be committed. Such modifications
are considered as a result of loading more fresh state from the database.
WARNING: use the returned value because it is always a different object instance.
The only case when you get the same instance is if the input was previously returned from the same context as a
result of DataContext.find(Class, Object)
or merge()
.
merge
in interface DataContext
entity
- instance to mergeoptions
- merge optionspublic <T extends Entity> T merge(T entity)
DataContext
DataContext.merge(Entity, MergeOptions)
with default options.merge
in interface DataContext
public EntitySet merge(java.util.Collection<? extends Entity> entities, MergeOptions options)
DataContext
Same as DataContext.merge(Entity)
but for a collection of instances.
merge
in interface DataContext
public EntitySet merge(java.util.Collection<? extends Entity> entities)
DataContext
Same as DataContext.merge(Entity, MergeOptions)
but for a collection of instances.
merge
in interface DataContext
protected Entity internalMerge(Entity entity, java.util.Set<Entity> mergedSet, boolean isRoot, MergeOptions options)
protected void mergeState(Entity srcEntity, Entity dstEntity, java.util.Set<Entity> mergedSet, boolean isRoot, MergeOptions options)
protected void setPropertyValue(Entity entity, MetaProperty property, @Nullable java.lang.Object value)
protected void setPropertyValue(Entity entity, MetaProperty property, @Nullable java.lang.Object value, boolean checkEquals)
protected void mergeSystemState(Entity srcEntity, Entity dstEntity, boolean isRoot, MergeOptions options)
protected void mergeList(java.util.List<Entity> list, Entity managedEntity, MetaProperty property, boolean replace, MergeOptions options, java.util.Set<Entity> mergedSet)
protected void mergeSet(java.util.Set<Entity> set, Entity managedEntity, MetaProperty property, boolean replace, MergeOptions options, java.util.Set<Entity> mergedSet)
protected java.util.List<Entity> createObservableList(java.util.List<Entity> list, Entity notifiedEntity)
protected ObservableSet<Entity> createObservableSet(java.util.Set<Entity> set, Entity notifiedEntity)
public void remove(Entity entity)
DataContext
DataContext.commit()
.
If the given entity is not in the context, nothing happens.
remove
in interface DataContext
protected void removeFromCollections(Entity entityToRemove)
public void evict(Entity entity)
DataContext
If the given entity is not in the context, nothing happens.
evict
in interface DataContext
public void evictModified()
DataContext
evictModified
in interface DataContext
public void clear()
DataContext
clear
in interface DataContext
DataContext.evict(Entity)
public <T extends Entity> T create(java.lang.Class<T> entityClass)
DataContext
Same as:
Foo foo = dataContext.merge(metadata.create(Foo.class));
create
in interface DataContext
entityClass
- entity classprotected void removeListeners(Entity entity)
public boolean hasChanges()
DataContext
hasChanges
in interface DataContext
public boolean isModified(Entity entity)
DataContext
isModified
in interface DataContext
public void setModified(Entity entity, boolean modified)
DataContext
setModified
in interface DataContext
entity
- entity instance which is already merged into the contextmodified
- true to register or false to unregisterpublic java.util.Set<Entity> getModified()
DataContext
getModified
in interface DataContext
public boolean isRemoved(Entity entity)
DataContext
isRemoved
in interface DataContext
public java.util.Set<Entity> getRemoved()
DataContext
getRemoved
in interface DataContext
public EntitySet commit()
DataContext
commit
in interface DataContext
DataContext.setParent(DataContext)
public Subscription addPreCommitListener(java.util.function.Consumer<DataContext.PreCommitEvent> listener)
DataContext
DataContext.PreCommitEvent
.
You can also add an event listener declaratively using a controller method annotated with Subscribe
:
@Subscribe(target = Target.DATA_CONTEXT) protected void onPreCommit(DataContext.PreCommitEvent event) { // handle event here }
addPreCommitListener
in interface DataContext
listener
- listenerpublic Subscription addPostCommitListener(java.util.function.Consumer<DataContext.PostCommitEvent> listener)
DataContext
DataContext.PostCommitEvent
.
You can also add an event listener declaratively using a controller method annotated with Subscribe
:
@Subscribe(target = Target.DATA_CONTEXT) protected void onPostCommit(DataContext.PostCommitEvent event) { // handle event here }
addPostCommitListener
in interface DataContext
listener
- listenerpublic java.util.function.Function<CommitContext,java.util.Set<Entity>> getCommitDelegate()
DataContext
getCommitDelegate
in interface DataContext
public void setCommitDelegate(java.util.function.Function<CommitContext,java.util.Set<Entity>> delegate)
DataContext
setCommitDelegate
in interface DataContext
protected java.util.Set<Entity> performCommit(CommitContext.ValidationMode validationMode, java.util.List<java.lang.Class> validationGroups)
protected java.util.Set<Entity> commitToDataManager(CommitContext.ValidationMode validationMode, java.util.List<java.lang.Class> validationGroups)
protected java.util.Collection filterCommittedInstances(java.util.Set<Entity> instances)
protected java.util.Set<Entity> commitToParentContext()
protected void cleanupContextAfterRemoveEntity(DataContextImpl context, Entity removedEntity)
public java.util.Collection<Entity> getAll()
protected void modified(Entity entity)
public java.lang.String printContent()
protected java.lang.String printEntity(Entity entity, int level, java.util.Set<Entity> visited)
protected java.lang.String printObject(java.lang.Object object)