Class DataListHelper<T extends Data>

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

    public class DataListHelper<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 List<Member> myDataList;
        private transient DataListHelper<Member> myDataListHelper = DataListHelper.build(this, Member.class, this::setMyDataList);
    
        public List<Member> getMyDataList() {
          if (isDBData()) {
            myDataListHelper.refreshDataCollectionIfNeeded();
          }
          return myDataList;
        }
    
        public void setMyDataList(List<Member> groupList) {
          this.myDataList = groupList;
    
          if (isDBData()) {
            myDataListHelper.updateIdList(groupList);
          }
        }
    
        public List<String> getMyDataIdList() {
          return myDataListHelper.getIdList();
        }
    
        public void setMyDataIdList(List<String> idList) {
          myDataListHelper.setIdList(idList);
        }
        
        @Override
        public void initializeDBDataCollections() {
          super.initializeDBDataCollections();
          if (myDataListHelper != null) { myDataListHelper.initializeHibernateCollection(); }
        }
    
        @Override
        public void cloneDBDataCollections() {
          super.cloneDBDataCollections();
          if (myDataListHelper != null) { myDataListHelper = myDataListHelper.copy(this::setMyDataList); }
        }
        
      }
     
    Since:
    jcms-10.0.5 / JCMS-8445
    • Constructor Summary

      Constructors 
      Constructor Description
      DataListHelper​(java.lang.Class<T> collectionDataClass, java.util.function.Consumer<java.util.List<T>> dataCollectionSetter)
      Create a new DataListHelper 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>
      DataListHelper<T>
      build​(Data data, java.lang.Class<T> collectionDataClass, java.util.function.Consumer<java.util.List<T>> dataCollectionSetter)
      Build a new instance of DataListHelper, IF NEEDED.
      DataListHelper<T> copy​(java.util.function.Consumer<java.util.List<T>> dataCollectionSetter)
      Create a copy of the underlying id set, and invoke the specified setter to update the Data Collection.
      java.util.List<java.lang.String> getIdList()
      Retrieve the underlying id List collection.
      void initializeHibernateCollection()
      Force initialization of the underlying Hibernate proxy/collections.
      void refreshDataCollectionIfNeeded()
      Invoke the specified data collection setter if needed.
      void setIdList​(java.util.List<java.lang.String> idList)
      Replace the underlying id List with the specified one.
      void updateIdList​(java.util.List<T> dataList)
      Update the underlying id List with values from the specified List of Data.
      • Methods inherited from class java.lang.Object

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

      • DataListHelper

        public DataListHelper​(java.lang.Class<T> collectionDataClass,
                              java.util.function.Consumer<java.util.List<T>> dataCollectionSetter)
        Create a new DataListHelper 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

      • getIdList

        public java.util.List<java.lang.String> getIdList()
        Retrieve the underlying id List collection.

        Known Limit : this method return the internal set, which is not thread safe : an underlying modification of the idList 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 List 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 idList 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.

      • setIdList

        public void setIdList​(java.util.List<java.lang.String> idList)
        Replace the underlying id List with the specified one.

        For use in the setter of your Hibernate id collection.

        Parameters:
        idList - the List of Data id to use
      • updateIdList

        public void updateIdList​(java.util.List<T> dataList)
        Update the underlying id List with values from the specified List of Data.

        For use in the setter of your Data collection.

        Parameters:
        dataList - the List 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 DataListHelper<T> copy​(java.util.function.Consumer<java.util.List<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 DataDataListHelper<T> build​(Data data,
                                                               java.lang.Class<T> collectionDataClass,
                                                               java.util.function.Consumer<java.util.List<T>> dataCollectionSetter)
        Build a new instance of DataListHelper, IF NEEDED.
        Type Parameters:
        T -
        Parameters:
        data - the instance of Data for which DataListHelper 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