Class DataSetHelper<T extends Data>

  • Type Parameters:
    T - the type of Data being managed in the Set

    public class DataSetHelper<T extends Data>
    extends java.lang.Object
    This Helper class can be used inside DBData, to manage collection of Data (whether they contains JStore or JcmsDB Data).

    Example :

      public class DemoDBData extends Publication implements DBData {
        
        protected TreeSet<Member> myDataSet;
        private transient DataSetHelper<Member> myDataSetHelper = DataSetHelper.build(this, Member.class, this::setMyDataSet);
    
        public Set<Member> getMyDataSet() {
          if (isDBData()) {
            myDataSetHelper.refreshDataCollectionIfNeeded();
          }
          return myDataSet;
        }
    
        public void setMyDataSet(TreeSet<Member> groupSet) {
          this.myDataSet = groupSet;
    
          if (isDBData()) {
            myDataSetHelper.updateIdSet(groupSet);
          }
        }
    
        public Set<String> getMyDataIdSet() {
          return myDataSetHelper.getIdSet();
        }
    
        public void setMyDataIdSet(Set<String> idSet) {
          myDataSetHelper.setIdSet(idSet);
        }
        
        @Override
        public void initializeDBDataCollections() {
          super.initializeDBDataCollections();
          if (myDataSetHelper != null) { myDataSetHelper.initializeHibernateCollection(); }
        }
    
        @Override
        public void cloneDBDataCollections() {
          super.cloneDBDataCollections();
          if (myDataSetHelper != null) { myDataSetHelper = myDataSetHelper.copy(this::setMyDataSet); }
        }
        
      }
     
    Since:
    jcms-10.0.5 / JCMS-8445
    • Constructor Summary

      Constructors 
      Constructor Description
      DataSetHelper​(java.lang.Class<T> collectionDataClass, java.util.function.Consumer<java.util.TreeSet<T>> dataCollectionSetter)
      Create a new DataSetHelper to manage a collection of the specified Data type.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      static <T extends Data>
      DataSetHelper<T>
      build​(Data data, java.lang.Class<T> collectionDataClass, java.util.function.Consumer<java.util.TreeSet<T>> dataCollectionSetter)
      Build a new instance of DataSetHelper, IF NEEDED.
      DataSetHelper<T> copy​(java.util.function.Consumer<java.util.TreeSet<T>> dataCollectionSetter)
      Create a copy of the underlying id set, and invoke the specified setter to update the Data Collection.
      java.util.Set<java.lang.String> getIdSet()
      Retrieve the underlying id Set collection.
      void initializeHibernateCollection()
      Force initialization of the underlying Hibernate proxy/collections.
      void refreshDataCollectionIfNeeded()
      Invoke the specified data collection setter if needed.
      void setIdSet​(java.util.Set<java.lang.String> idSet)
      Replace the underlying id Set with the specified one.
      void updateIdSet​(java.util.Set<T> dataSet)
      Update the underlying id Set with values from the specified Set of Data.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • DataSetHelper

        public DataSetHelper​(java.lang.Class<T> collectionDataClass,
                             java.util.function.Consumer<java.util.TreeSet<T>> dataCollectionSetter)
        Create a new DataSetHelper to manage a collection of the specified Data type.
        Parameters:
        collectionDataClass - required, must not be null
        dataCollectionSetter - the setter to invoke in refreshDataCollectionIfNeeded()
        It can be null if you do not have any Collection of Data to update, though it is only under really really rare circumstances
    • Method Detail

      • getIdSet

        public java.util.Set<java.lang.String> getIdSet()
        Retrieve the underlying id Set collection.

        Known Limit : this method return the internal set, which is not thread safe : an underlying modification of the idSet could happen while you retrieve it. Life is hard, so is computer science, deal with it.

        For use in the getter of your Hibernate id collection.

        Returns:
        a Set of Data id, may return null if set was not initialized
      • refreshDataCollectionIfNeeded

        public void refreshDataCollectionIfNeeded()
        Invoke the specified data collection setter if needed.

        that is if idSet was set/modified, and data collection setter was never invoked since the modification.

        For use, as lazy loading, in the getter of your Data collection.

      • setIdSet

        public void setIdSet​(java.util.Set<java.lang.String> idSet)
        Replace the underlying id Set with the specified one.

        For use in the setter of your Hibernate id collection.

        Parameters:
        idSet - the Set of Data id to use
      • updateIdSet

        public void updateIdSet​(java.util.Set<T> dataSet)
        Update the underlying id Set with values from the specified Set of Data.

        For use in the setter of your Data collection.

        Parameters:
        dataSet - the Set of Data id to use
      • initializeHibernateCollection

        public void initializeHibernateCollection()
        Force initialization of the underlying Hibernate proxy/collections.

        For use in Publication.initializeDBDataCollections() implementation.

      • copy

        public DataSetHelper<T> copy​(java.util.function.Consumer<java.util.TreeSet<T>> dataCollectionSetter)
        Create a copy of the underlying id set, and invoke the specified setter to update the Data Collection.

        For use in Publication.cloneDBDataCollections() implementation.

        Parameters:
        dataCollectionSetter - the reference to the setter of the cloned object
        Returns:
        the copy
      • build

        public static <T extends DataDataSetHelper<T> build​(Data data,
                                                              java.lang.Class<T> collectionDataClass,
                                                              java.util.function.Consumer<java.util.TreeSet<T>> dataCollectionSetter)
        Build a new instance of DataSetHelper, IF NEEDED.
        Type Parameters:
        T -
        Parameters:
        data - the instance of Data for which DataSetHelper is built, return null if the specified Data is a JStore object
        collectionDataClass - the type of Data being manipulated in the collection
        dataCollectionSetter - the setter of the Data collection on the Data instance
        Returns:
        a new instance if a valid DBData was specified, or null for JStore object