Skip to content
This repository
Browse code

Merge pull request #136 from jesselong/core-1.5.x

Core 1.5.x
  • Loading branch information...
commit 371d571775ccf273df97a86ad89842f3269fdc8a 2 parents a9f7a81 + c8f87cc
Martin Grigorov martin-g authored
77 ...arent/minis-parent/minis/src/main/java/org/wicketstuff/minis/model/ReferenceLoadableDetachableModel.java
... ... @@ -0,0 +1,77 @@
  1 +package org.wicketstuff.minis.model;
  2 +
  3 +import java.io.Serializable;
  4 +import org.apache.wicket.model.LoadableDetachableModel;
  5 +
  6 +/**
  7 + * A {@link LoadableDetachableModel} that retains a reference to the loadable object. This is useful
  8 + * for storing database primary keys. This model is capable of modeling {@code null}.
  9 + * @author Jesse Long
  10 + * @param <E> The type of the object that is managed by this model.
  11 + * @param <K> The type of the reference that uniquely references the object. This should be small,
  12 + * serializable type, because it is not detached from the model. Integers, Longs, Strings etc.
  13 + */
  14 +public abstract class ReferenceLoadableDetachableModel<E, K extends Serializable>
  15 + extends LoadableDetachableModel<E>
  16 +{
  17 + /**
  18 + * The reference to the object. This is used to retrieve the object from a database for instance.
  19 + */
  20 + private K reference;
  21 +
  22 + public ReferenceLoadableDetachableModel(E object)
  23 + {
  24 + setObject(object);
  25 + }
  26 +
  27 + /**
  28 + * Loads the model object using the provided reference. This method is not for {@code null} objects.
  29 + * @param reference The reference to the model object.
  30 + * @return The model object.
  31 + * @see #load()
  32 + */
  33 + protected abstract E load(K reference);
  34 +
  35 + /**
  36 + * Returns a reference to the object being modeled. This reference is later passed to the {@link #load(java.io.Serializable)}
  37 + * method to load the object after {@link #detach()} has been called.
  38 + * @param object The object from which to obtain a reference.
  39 + * @return a reference to the object being modeled.
  40 + */
  41 + protected abstract K reference(E object);
  42 +
  43 + /**
  44 + * Loads and returns the modeled object. This method is once after each {@link #detach()} call.
  45 + * <p>
  46 + * If the reference is {@code null}, then {@code null} is returned. Otherwise, {@link #load(java.io.Serializable)} is called
  47 + * passing it the reference previously returned by {@link #reference(java.lang.Object)}.
  48 + * @return the modeled object, or {@code null} if the reference is {@code null}
  49 + */
  50 + @Override
  51 + protected final E load()
  52 + {
  53 + if (reference == null){
  54 + return null;
  55 + }else{
  56 + return load(reference);
  57 + }
  58 + }
  59 +
  60 + /**
  61 + * Sets the object being managed. This method calls the {@link #reference(java.lang.Object) } method to get
  62 + * a reference to the object so it can be loaded again with the {@link #load(java.io.Serializable) } after
  63 + * {@link #detach() } is called on this object. If the object is {@code null}, then {@link #reference(java.lang.Object)}
  64 + * is not called - a {@code null} reference is used instead.
  65 + * @param object The object to model.
  66 + */
  67 + @Override
  68 + public final void setObject(E object)
  69 + {
  70 + super.setObject(object);
  71 + if (object == null){
  72 + reference = null;
  73 + }else{
  74 + reference = reference(object);
  75 + }
  76 + }
  77 +}

0 comments on commit 371d571

Please sign in to comment.
Something went wrong with that request. Please try again.