Class DataContextImpl

java.lang.Object
io.jmix.ui.model.impl.DataContextImpl
All Implemented Interfaces:
DataContext, DataContextChanges, DataContextInternal

public class DataContextImpl extends Object implements DataContextInternal
Standard implementation of DataContext which commits data to DataManager.
  • Field Details

  • Constructor Details

    • DataContextImpl

      public DataContextImpl()
  • Method Details

    • getParent

      @Nullable public DataContext getParent()
      Description copied from interface: DataContext
      Returns a parent context, if any. If the parent context is set, DataContext.commit() method merges the changed instances to it instead of sending them to DataManager or a custom commit delegate.
      Specified by:
      getParent in interface DataContext
    • setParent

      public void setParent(DataContext parentContext)
      Description copied from interface: DataContext
      Sets the parent context. If the parent context is set, DataContext.commit() method merges the changed instances to it instead of sending them to DataManager or a custom commit delegate.
      Specified by:
      setParent in interface DataContext
    • addChangeListener

      public Subscription addChangeListener(Consumer<DataContext.ChangeEvent> listener)
      Description copied from interface: DataContext
      Adds a listener to DataContext.ChangeEvent.
      Specified by:
      addChangeListener in interface DataContext
    • fireChangeListener

      protected void fireChangeListener(Object entity)
    • find

      @Nullable public <T> T find(Class<T> entityClass, Object entityId)
      Description copied from interface: DataContext
      Returns an entity instance by its class and id.
      Specified by:
      find in interface DataContext
      Returns:
      entity instance or null if there is no such entity in the context
    • find

      @Nullable public <T> T find(T entity)
      Description copied from interface: DataContext
      Returns the instance of entity with the same id if it exists in this context.
      Specified by:
      find in interface DataContext
      Returns:
      entity instance or null if there is no such entity in the context
    • contains

      public boolean contains(Object entity)
      Description copied from interface: DataContext
      Returns true if the context contains the given entity (distinguished by its class and id).
      Specified by:
      contains in interface DataContext
    • merge

      public <T> T merge(T entity, MergeOptions options)
      Description copied from interface: DataContext
      Merge the given entity into the context. The whole object graph with all references will be merged.

      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().

      Specified by:
      merge in interface DataContext
      Parameters:
      entity - instance to merge
      options - merge options
      Returns:
      the instance which is tracked by the context
    • merge

      public <T> T merge(T entity)
      Description copied from interface: DataContext
      Same as DataContext.merge(Object, MergeOptions) with default options.
      Specified by:
      merge in interface DataContext
    • merge

      public EntitySet merge(Collection entities, MergeOptions options)
      Description copied from interface: DataContext
      Merge the given entities into the context. The whole object graph for each element of the collection with all references will be merged.

      Same as DataContext.merge(Object) but for a collection of instances.

      Specified by:
      merge in interface DataContext
      Returns:
      set of instances tracked by the context
    • merge

      public EntitySet merge(Collection entities)
      Description copied from interface: DataContext
      Merge the given entities into the context. The whole object graph for each element of the collection with all references will be merged.

      Same as DataContext.merge(Object, MergeOptions) but for a collection of instances.

      Specified by:
      merge in interface DataContext
      Returns:
      set of instances tracked by the context
    • internalMerge

      protected Object internalMerge(Object entity, Map<Object,Object> mergedMap, boolean isRoot, MergeOptions options)
    • makeKey

      protected Object makeKey(Object entity)
    • copyEntity

      protected Object copyEntity(Object srcEntity)
    • mergeState

      protected void mergeState(Object srcEntity, Object dstEntity, Map<Object,Object> mergedMap, boolean isRoot, MergeOptions options)
    • setPropertyValue

      protected void setPropertyValue(Object entity, MetaProperty property, @Nullable Object value)
    • setPropertyValue

      protected void setPropertyValue(Object entity, MetaProperty property, @Nullable Object value, boolean checkEquals)
    • copySystemState

      protected void copySystemState(Object srcEntity, Object dstEntity)
    • mergeSystemState

      protected void mergeSystemState(Object srcEntity, Object dstEntity, boolean isRoot, MergeOptions options)
    • mergeLazyLoadingState

      protected void mergeLazyLoadingState(Object srcEntity, Object dstEntity)
    • mergeList

      protected void mergeList(List<Object> list, Object managedEntity, MetaProperty property, boolean replace, MergeOptions options, Map<Object,Object> mergedMap)
    • mergeSet

      protected void mergeSet(Set<Object> set, Object managedEntity, MetaProperty property, boolean replace, MergeOptions options, Map<Object,Object> mergedMap)
    • wrapLazyValueIntoObservableCollection

      protected Collection<Object> wrapLazyValueIntoObservableCollection(Collection<Object> collection, Object notifiedEntity)
    • createObservableList

      protected List<Object> createObservableList(Object notifiedEntity)
    • createObservableList

      protected List<Object> createObservableList(List<Object> list, Object notifiedEntity)
    • createObservableSet

      protected Set<Object> createObservableSet(Object notifiedEntity)
    • createObservableSet

      protected ObservableSet<Object> createObservableSet(Set<Object> set, Object notifiedEntity)
    • remove

      public void remove(Object entity)
      Description copied from interface: DataContext
      Removes the entity from the context and registers it as deleted. The entity will be removed from the data store upon subsequent call to DataContext.commit().

      If the given entity is not in the context, nothing happens.

      Specified by:
      remove in interface DataContext
    • removeFromCollections

      protected void removeFromCollections(Object entityToRemove)
    • evict

      public void evict(Object entity)
      Description copied from interface: DataContext
      Removes the entity from the context so the context stops tracking it.

      If the given entity is not in the context, nothing happens.

      Specified by:
      evict in interface DataContext
    • evictModified

      public void evictModified()
      Description copied from interface: DataContext
      Clears the lists of created/modified/deleted entities and evicts these entities.
      Specified by:
      evictModified in interface DataContext
    • clear

      public void clear()
      Description copied from interface: DataContext
      Evicts all tracked entities.
      Specified by:
      clear in interface DataContext
      See Also:
    • create

      public <T> T create(Class<T> entityClass)
      Description copied from interface: DataContext
      Creates an entity instance and merges it into the context.

      Same as:

       Foo foo = dataContext.merge(metadata.create(Foo.class));
       
      Specified by:
      create in interface DataContext
      Parameters:
      entityClass - entity class
      Returns:
      a new instance which is tracked by the context
    • removeListeners

      protected void removeListeners(Object entity)
    • hasChanges

      public boolean hasChanges()
      Description copied from interface: DataContext
      Returns true if the context has detected changes in the tracked entities.
      Specified by:
      hasChanges in interface DataContext
    • isModified

      public boolean isModified(Object entity)
      Description copied from interface: DataContext
      Returns true if the context has detected changes in the given entity.
      Specified by:
      isModified in interface DataContext
    • setModified

      public void setModified(Object entity, boolean modified)
      Description copied from interface: DataContext
      Registers or unregisters the given entity as modified.
      Specified by:
      setModified in interface DataContext
      Parameters:
      entity - entity instance which is already merged into the context
      modified - true to register or false to unregister
    • getModified

      public Set getModified()
      Description copied from interface: DataContext
      Returns an immutable set of entities registered as modified.
      Specified by:
      getModified in interface DataContext
    • isRemoved

      public boolean isRemoved(Object entity)
      Description copied from interface: DataContext
      Returns true if the context has registered removal of the given entity.
      Specified by:
      isRemoved in interface DataContext
    • getRemoved

      public Set getRemoved()
      Description copied from interface: DataContext
      Returns an immutable set of entities registered for removal.
      Specified by:
      getRemoved in interface DataContext
    • commit

      public EntitySet commit()
      Description copied from interface: DataContext
      Commits changed and removed instances using DataManager or a custom commit delegate. After successful commit, the context contains updated instances returned from the backend code.
      Specified by:
      commit in interface DataContext
      Returns:
      set of committed and merged back to the context instances. Does not contain removed instances.
      See Also:
    • addPreCommitListener

      public Subscription addPreCommitListener(Consumer<DataContext.PreCommitEvent> listener)
      Description copied from interface: DataContext
      Adds a listener to 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
          }
       
      Specified by:
      addPreCommitListener in interface DataContext
      Parameters:
      listener - listener
      Returns:
      subscription
    • addPostCommitListener

      public Subscription addPostCommitListener(Consumer<DataContext.PostCommitEvent> listener)
      Description copied from interface: DataContext
      Adds a listener to 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
          }
       
      Specified by:
      addPostCommitListener in interface DataContext
      Parameters:
      listener - listener
      Returns:
      subscription
    • getCommitDelegate

      public Function<SaveContext,Set<Object>> getCommitDelegate()
      Description copied from interface: DataContext
      Returns a function which will be used to commit data instead of standard implementation.
      Specified by:
      getCommitDelegate in interface DataContext
    • setCommitDelegate

      public void setCommitDelegate(Function<SaveContext,Set<Object>> delegate)
      Description copied from interface: DataContext
      Sets a function which will be used to commit data instead of standard implementation.
      Specified by:
      setCommitDelegate in interface DataContext
    • performCommit

      protected Set<Object> performCommit()
    • commitToDataManager

      protected Set<Object> commitToDataManager()
    • filterCommittedInstances

      protected List filterCommittedInstances(Set<Object> instances)
    • updateFetchPlans

      protected void updateFetchPlans(SaveContext saveContext)
    • isolate

      public Collection<Object> isolate(List entities)
    • commitToParentContext

      protected Set<Object> commitToParentContext()
    • cleanupContextAfterRemoveEntity

      protected void cleanupContextAfterRemoveEntity(DataContextInternal context, Object removedEntity)
    • entityHasReference

      protected boolean entityHasReference(Object entity, Object refEntity)
    • mergeCommitted

      protected EntitySet mergeCommitted(Set<Object> committed)
    • getAll

      public Collection getAll()
    • modified

      protected void modified(Object entity)
    • printContent

      public String printContent()
    • printEntity

      protected String printEntity(Object entity, int level, Set<Object> visited)
    • printObject

      protected String printObject(Object object)
    • getModifiedInstances

      public Set<Object> getModifiedInstances()
      Specified by:
      getModifiedInstances in interface DataContextChanges
      Returns:
      mutable set of entities registered as modified
    • getRemovedInstances

      public Set<Object> getRemovedInstances()
      Specified by:
      getRemovedInstances in interface DataContextChanges
      Returns:
      mutable set of entities registered as removed
    • getEntityMetaClass

      protected MetaClass getEntityMetaClass(Object entity)
    • getPrimaryKeyPropertyName

      @Nullable protected String getPrimaryKeyPropertyName(Object entity)