Class DataSetHelper<T extends Data>

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

public class DataSetHelper<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 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 Details

    • DataSetHelper

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

    • getIdSet

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