Class DataListHelper<T extends Data>

java.lang.Object
com.jalios.jcms.db.DataListHelper<T>
Type Parameters:
T - the type of Data being managed in the List

public class DataListHelper<T extends Data> extends 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 Details

    • DataListHelper

      public DataListHelper(Class<T> collectionDataClass, Consumer<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 Details

    • getIdList

      public List<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(List<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(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(Consumer<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 Data> DataListHelper<T> build(Data data, Class<T> collectionDataClass, Consumer<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