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 java.util.function.Function<io.jmix.core.SaveContext,java.util.Set<java.lang.Object>> |
commitDelegate |
protected java.util.Map<java.lang.Class<?>,java.util.Map<java.lang.Object,java.lang.Object>> |
content |
protected io.jmix.core.DataManager |
dataManager |
protected boolean |
disableListeners |
protected java.util.Map<java.lang.Object,java.util.Map<java.lang.String,DataContextImpl.EmbeddedPropertyChangeListener>> |
embeddedPropertyListeners |
protected io.jmix.core.EntityReferencesNormalizer |
entityReferencesNormalizer |
protected io.jmix.core.EntityStates |
entityStates |
protected io.jmix.core.EntitySystemStateSupport |
entitySystemStateSupport |
protected io.jmix.core.common.event.EventHub |
events |
protected io.jmix.core.Metadata |
metadata |
protected io.jmix.core.MetadataTools |
metadataTools |
protected java.util.Set<java.lang.Object> |
modifiedInstances |
protected java.util.Map<java.lang.Object,java.lang.Object> |
nullIdEntitiesMap |
protected DataContextImpl |
parentContext |
protected DataContextImpl.PropertyChangeListener |
propertyChangeListener |
protected java.util.Set<java.lang.Object> |
removedInstances |
protected io.jmix.core.impl.StandardSerialization |
standardSerialization |
Constructor and Description |
---|
DataContextImpl() |
Modifier and Type | Method and Description |
---|---|
io.jmix.core.common.event.Subscription |
addChangeListener(java.util.function.Consumer<DataContext.ChangeEvent> listener)
Adds a listener to
DataContext.ChangeEvent . |
io.jmix.core.common.event.Subscription |
addPostCommitListener(java.util.function.Consumer<DataContext.PostCommitEvent> listener)
Adds a listener to
DataContext.PostCommitEvent . |
io.jmix.core.common.event.Subscription |
addPreCommitListener(java.util.function.Consumer<DataContext.PreCommitEvent> listener)
Adds a listener to
DataContext.PreCommitEvent . |
protected void |
cleanupContextAfterRemoveEntity(DataContextImpl context,
java.lang.Object removedEntity) |
void |
clear()
Evicts all tracked entities.
|
io.jmix.core.EntitySet |
commit()
Commits changed and removed instances to the middleware.
|
protected java.util.Set<java.lang.Object> |
commitToDataManager() |
protected java.util.Set<java.lang.Object> |
commitToParentContext() |
boolean |
contains(java.lang.Object entity)
Returns true if the context contains the given entity (distinguished by its class and id).
|
protected java.lang.Object |
copyEntity(java.lang.Object srcEntity) |
protected void |
copySystemState(java.lang.Object srcEntity,
java.lang.Object dstEntity) |
<T> T |
create(java.lang.Class<T> entityClass)
Creates an entity instance and merges it into the context.
|
protected java.util.List<java.lang.Object> |
createObservableList(java.util.List<java.lang.Object> list,
java.lang.Object notifiedEntity) |
protected java.util.List<java.lang.Object> |
createObservableList(java.lang.Object notifiedEntity) |
protected java.util.Set<java.lang.Object> |
createObservableSet(java.lang.Object notifiedEntity) |
protected ObservableSet<java.lang.Object> |
createObservableSet(java.util.Set<java.lang.Object> set,
java.lang.Object notifiedEntity) |
protected boolean |
entityHasReference(java.lang.Object entity,
java.lang.Object refEntity) |
void |
evict(java.lang.Object 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.List |
filterCommittedInstances(java.util.Set<java.lang.Object> instances) |
<T> T |
find(java.lang.Class<T> entityClass,
java.lang.Object entityId)
Returns an entity instance by its class and id.
|
<T> T |
find(T entity)
Returns the instance of entity with the same id if it exists in this context.
|
protected void |
fireChangeListener(java.lang.Object entity) |
java.util.Collection |
getAll() |
java.util.function.Function<io.jmix.core.SaveContext,java.util.Set<java.lang.Object>> |
getCommitDelegate()
Returns a function which will be used to commit data instead of standard implementation.
|
java.util.Set |
getModified()
Returns an immutable set of entities registered as modified.
|
DataContext |
getParent()
Returns a parent context, if any.
|
java.util.Set |
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 java.lang.Object |
internalMerge(java.lang.Object entity,
java.util.Map<java.lang.Object,java.lang.Object> mergedMap,
boolean isRoot,
MergeOptions options) |
boolean |
isModified(java.lang.Object entity)
Returns true if the context has detected changes in the given entity.
|
java.util.Collection<java.lang.Object> |
isolate(java.util.List entities) |
boolean |
isRemoved(java.lang.Object entity)
Returns true if the context has registered removal of the given entity.
|
protected java.lang.Object |
makeKey(java.lang.Object entity) |
io.jmix.core.EntitySet |
merge(java.util.Collection entities)
Merge the given entities into the context.
|
io.jmix.core.EntitySet |
merge(java.util.Collection entities,
MergeOptions options)
Merge the given entities into the context.
|
<T> T |
merge(T entity)
Same as
DataContext.merge(Object, MergeOptions) with default options. |
<T> T |
merge(T entity,
MergeOptions options)
Merge the given entity into the context.
|
protected io.jmix.core.EntitySet |
mergeCommitted(java.util.Set<java.lang.Object> committed) |
protected void |
mergeLazyLoadingState(java.lang.Object srcEntity,
java.lang.Object dstEntity) |
protected void |
mergeList(java.util.List<java.lang.Object> list,
java.lang.Object managedEntity,
io.jmix.core.metamodel.model.MetaProperty property,
boolean replace,
MergeOptions options,
java.util.Map<java.lang.Object,java.lang.Object> mergedMap) |
protected void |
mergeSet(java.util.Set<java.lang.Object> set,
java.lang.Object managedEntity,
io.jmix.core.metamodel.model.MetaProperty property,
boolean replace,
MergeOptions options,
java.util.Map<java.lang.Object,java.lang.Object> mergedMap) |
protected void |
mergeState(java.lang.Object srcEntity,
java.lang.Object dstEntity,
java.util.Map<java.lang.Object,java.lang.Object> mergedMap,
boolean isRoot,
MergeOptions options) |
protected void |
mergeSystemState(java.lang.Object srcEntity,
java.lang.Object dstEntity,
boolean isRoot,
MergeOptions options) |
protected void |
modified(java.lang.Object entity) |
protected java.util.Set<java.lang.Object> |
performCommit() |
java.lang.String |
printContent() |
protected java.lang.String |
printEntity(java.lang.Object entity,
int level,
java.util.Set<java.lang.Object> visited) |
protected java.lang.String |
printObject(java.lang.Object object) |
void |
remove(java.lang.Object entity)
Removes the entity from the context and registers it as deleted.
|
protected void |
removeFromCollections(java.lang.Object entityToRemove) |
protected void |
removeListeners(java.lang.Object entity) |
void |
setCommitDelegate(java.util.function.Function<io.jmix.core.SaveContext,java.util.Set<java.lang.Object>> delegate)
Sets a function which will be used to commit data instead of standard implementation.
|
void |
setModified(java.lang.Object entity,
boolean modified)
Registers or unregisters the given entity as modified.
|
void |
setParent(DataContext parentContext)
Sets the parent context.
|
protected void |
setPropertyValue(java.lang.Object entity,
io.jmix.core.metamodel.model.MetaProperty property,
java.lang.Object value) |
protected void |
setPropertyValue(java.lang.Object entity,
io.jmix.core.metamodel.model.MetaProperty property,
java.lang.Object value,
boolean checkEquals) |
protected java.util.Collection<java.lang.Object> |
wrapLazyValueIntoObservableCollection(java.util.Collection<java.lang.Object> collection,
java.lang.Object notifiedEntity) |
@Autowired protected io.jmix.core.Metadata metadata
@Autowired protected io.jmix.core.MetadataTools metadataTools
@Autowired protected io.jmix.core.EntityStates entityStates
@Autowired protected io.jmix.core.DataManager dataManager
@Autowired protected io.jmix.core.EntitySystemStateSupport entitySystemStateSupport
@Autowired protected io.jmix.core.EntityReferencesNormalizer entityReferencesNormalizer
@Autowired protected io.jmix.core.impl.StandardSerialization standardSerialization
protected io.jmix.core.common.event.EventHub events
protected java.util.Map<java.lang.Class<?>,java.util.Map<java.lang.Object,java.lang.Object>> content
protected java.util.Set<java.lang.Object> modifiedInstances
protected java.util.Set<java.lang.Object> removedInstances
protected DataContextImpl.PropertyChangeListener propertyChangeListener
protected boolean disableListeners
protected DataContextImpl parentContext
protected java.util.function.Function<io.jmix.core.SaveContext,java.util.Set<java.lang.Object>> commitDelegate
protected java.util.Map<java.lang.Object,java.util.Map<java.lang.String,DataContextImpl.EmbeddedPropertyChangeListener>> embeddedPropertyListeners
protected java.util.Map<java.lang.Object,java.lang.Object> nullIdEntitiesMap
@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 io.jmix.core.common.event.Subscription addChangeListener(java.util.function.Consumer<DataContext.ChangeEvent> listener)
DataContext
DataContext.ChangeEvent
.addChangeListener
in interface DataContext
protected void fireChangeListener(java.lang.Object entity)
@Nullable public <T> T find(java.lang.Class<T> entityClass, java.lang.Object entityId)
DataContext
find
in interface DataContext
@Nullable public <T> T find(T entity)
DataContext
find
in interface DataContext
public boolean contains(java.lang.Object entity)
DataContext
contains
in interface DataContext
public <T> 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> T merge(T entity)
DataContext
DataContext.merge(Object, MergeOptions)
with default options.merge
in interface DataContext
public io.jmix.core.EntitySet merge(java.util.Collection entities, MergeOptions options)
DataContext
Same as DataContext.merge(Object)
but for a collection of instances.
merge
in interface DataContext
public io.jmix.core.EntitySet merge(java.util.Collection entities)
DataContext
Same as DataContext.merge(Object, MergeOptions)
but for a collection of instances.
merge
in interface DataContext
protected java.lang.Object internalMerge(java.lang.Object entity, java.util.Map<java.lang.Object,java.lang.Object> mergedMap, boolean isRoot, MergeOptions options)
protected java.lang.Object makeKey(java.lang.Object entity)
protected java.lang.Object copyEntity(java.lang.Object srcEntity)
protected void mergeState(java.lang.Object srcEntity, java.lang.Object dstEntity, java.util.Map<java.lang.Object,java.lang.Object> mergedMap, boolean isRoot, MergeOptions options)
protected void setPropertyValue(java.lang.Object entity, io.jmix.core.metamodel.model.MetaProperty property, @Nullable java.lang.Object value)
protected void setPropertyValue(java.lang.Object entity, io.jmix.core.metamodel.model.MetaProperty property, @Nullable java.lang.Object value, boolean checkEquals)
protected void copySystemState(java.lang.Object srcEntity, java.lang.Object dstEntity)
protected void mergeSystemState(java.lang.Object srcEntity, java.lang.Object dstEntity, boolean isRoot, MergeOptions options)
protected void mergeLazyLoadingState(java.lang.Object srcEntity, java.lang.Object dstEntity)
protected void mergeList(java.util.List<java.lang.Object> list, java.lang.Object managedEntity, io.jmix.core.metamodel.model.MetaProperty property, boolean replace, MergeOptions options, java.util.Map<java.lang.Object,java.lang.Object> mergedMap)
protected void mergeSet(java.util.Set<java.lang.Object> set, java.lang.Object managedEntity, io.jmix.core.metamodel.model.MetaProperty property, boolean replace, MergeOptions options, java.util.Map<java.lang.Object,java.lang.Object> mergedMap)
protected java.util.Collection<java.lang.Object> wrapLazyValueIntoObservableCollection(java.util.Collection<java.lang.Object> collection, java.lang.Object notifiedEntity)
protected java.util.List<java.lang.Object> createObservableList(java.lang.Object notifiedEntity)
protected java.util.List<java.lang.Object> createObservableList(java.util.List<java.lang.Object> list, java.lang.Object notifiedEntity)
protected java.util.Set<java.lang.Object> createObservableSet(java.lang.Object notifiedEntity)
protected ObservableSet<java.lang.Object> createObservableSet(java.util.Set<java.lang.Object> set, java.lang.Object notifiedEntity)
public void remove(java.lang.Object entity)
DataContext
DataContext.commit()
.
If the given entity is not in the context, nothing happens.
remove
in interface DataContext
protected void removeFromCollections(java.lang.Object entityToRemove)
public void evict(java.lang.Object 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(Object)
public <T> 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(java.lang.Object entity)
public boolean hasChanges()
DataContext
hasChanges
in interface DataContext
public boolean isModified(java.lang.Object entity)
DataContext
isModified
in interface DataContext
public void setModified(java.lang.Object 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 getModified()
DataContext
getModified
in interface DataContext
public boolean isRemoved(java.lang.Object entity)
DataContext
isRemoved
in interface DataContext
public java.util.Set getRemoved()
DataContext
getRemoved
in interface DataContext
public io.jmix.core.EntitySet commit()
DataContext
commit
in interface DataContext
DataContext.setParent(DataContext)
public io.jmix.core.common.event.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 io.jmix.core.common.event.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<io.jmix.core.SaveContext,java.util.Set<java.lang.Object>> getCommitDelegate()
DataContext
getCommitDelegate
in interface DataContext
public void setCommitDelegate(java.util.function.Function<io.jmix.core.SaveContext,java.util.Set<java.lang.Object>> delegate)
DataContext
setCommitDelegate
in interface DataContext
protected java.util.Set<java.lang.Object> performCommit()
protected java.util.Set<java.lang.Object> commitToDataManager()
protected java.util.List filterCommittedInstances(java.util.Set<java.lang.Object> instances)
public java.util.Collection<java.lang.Object> isolate(java.util.List entities)
protected java.util.Set<java.lang.Object> commitToParentContext()
protected void cleanupContextAfterRemoveEntity(DataContextImpl context, java.lang.Object removedEntity)
protected boolean entityHasReference(java.lang.Object entity, java.lang.Object refEntity)
protected io.jmix.core.EntitySet mergeCommitted(java.util.Set<java.lang.Object> committed)
public java.util.Collection getAll()
protected void modified(java.lang.Object entity)
public java.lang.String printContent()
protected java.lang.String printEntity(java.lang.Object entity, int level, java.util.Set<java.lang.Object> visited)
protected java.lang.String printObject(java.lang.Object object)