Class LoadableDetachableModel<T>

  • Type Parameters:
    T - The IModel Object type
    All Implemented Interfaces:
    Attachable, IDetachable, IModel<T>, java.io.Serializable

    public abstract class LoadableDetachableModel<T>
    extends java.lang.Object
    implements IModel<T>
    IModel that makes working with detachable models a breeze. LoadableDetachableModel holds a temporary, transient model object, that is set when getObject() is called by calling abstract method 'load', and that will be reset/ set to null on detach(). A usage example:
     LoadableDetachableModel venueListModel = new LoadableDetachableModel()
     {
            protected Object load()
            {
                    return getVenueDao().findVenues();
            }
     };
     

    Though you can override methods onAttach() and onDetach() for additional attach/ detach behavior, the point of this class is to hide as much of the attaching/ detaching as possible. So you should rarely need to override those methods, if ever.

    See Also:
    Serialized Form
    • Method Summary

      All Methods Instance Methods Abstract Methods Concrete Methods 
      Modifier and Type Method Description
      void attach()
      Attach an object.
      void detach()
      Detach an object.
      T getObject()
      Gets the model object.
      boolean isAttached()
      Checks if the object is attached.
      protected abstract T load()
      Loads and returns the (temporary) model object.
      protected void onAttach()
      Attaches to the current request.
      protected void onDetach()
      Detaches from the current request.
      void setObject​(T object)
      Manually loads the model with the specified object.
      java.lang.String toString()
      • Methods inherited from class java.lang.Object

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

      • LoadableDetachableModel

        public LoadableDetachableModel​(T object)
        This constructor is used if you already have the object retrieved and want to wrap it with a detachable model.
        Parameters:
        object - retrieved instance of the detachable object
      • LoadableDetachableModel

        public LoadableDetachableModel()
    • Method Detail

      • attach

        public void attach()
        Attach an object.
        Specified by:
        attach in interface Attachable
      • detach

        public void detach()
        Detach an object.
        Specified by:
        detach in interface IDetachable
      • getObject

        public final T getObject()
        Gets the model object.
        Specified by:
        getObject in interface IModel<T>
        Returns:
        The model object
      • setObject

        public void setObject​(T object)
        Manually loads the model with the specified object. Subsequent calls to getObject() will return object until detach() is called.
        Specified by:
        setObject in interface IModel<T>
        Parameters:
        object - The object to set into the model
      • isAttached

        public final boolean isAttached()
        Checks if the object is attached.
        Returns:
        true, if the object is attached otherwise false
      • load

        protected abstract T load()
        Loads and returns the (temporary) model object.
        Returns:
        the (temporary) model object
      • onAttach

        protected void onAttach()
        Attaches to the current request. Implement this method with custom behavior, such as loading the model object.
      • onDetach

        protected void onDetach()
        Detaches from the current request. Implement this method with custom behavior, such as setting the model object to null.
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object