Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Core 1.5.x #136

Merged
merged 1 commit into from

2 participants

@jesselong

Handy class I use all the time, might save someone some boilerplate code.

@jesselong jesselong Add ReferenceLoadableDetachableModel - a LoadableDetachableModel that…
… uses a reference (think PK) to the object to load it.
c8f87cc
@martin-g
Owner

There are some differences in the indentation characters.
Other than that I think it is OK to merge it.

OK, for reference sake, what is the preferred wicket/wicketstuff indent scheme. I recently switched to using tabs for wicket stuff because that seems to be what is happening in wicket.

Is that right? tabs to indent?

Do you actually read it at 8 characters indented or do you configure your ide to display tabs at 4 char width?

Owner

I care just to be consistent - either spaces or tabs.
I think your file have them mixed. At least this is how GitHub shows them. It is not a big deal though. I'd merge it anyway.

My personal preference is tab. For Java my IDE shows them as 4 char width, for Scala - 2.

@martin-g martin-g merged commit 371d571 into wicketstuff:core-1.5.x
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jul 10, 2012
  1. @jesselong

    Add ReferenceLoadableDetachableModel - a LoadableDetachableModel that…

    jesselong authored
    … uses a reference (think PK) to the object to load it.
This page is out of date. Refresh to see the latest.
View
77 ...ent/minis/src/main/java/org/wicketstuff/minis/model/ReferenceLoadableDetachableModel.java
@@ -0,0 +1,77 @@
+package org.wicketstuff.minis.model;
+
+import java.io.Serializable;
+import org.apache.wicket.model.LoadableDetachableModel;
+
+/**
+ * A {@link LoadableDetachableModel} that retains a reference to the loadable object. This is useful
+ * for storing database primary keys. This model is capable of modeling {@code null}.
+ * @author Jesse Long
+ * @param <E> The type of the object that is managed by this model.
+ * @param <K> The type of the reference that uniquely references the object. This should be small,
+ * serializable type, because it is not detached from the model. Integers, Longs, Strings etc.
+ */
+public abstract class ReferenceLoadableDetachableModel<E, K extends Serializable>
+ extends LoadableDetachableModel<E>
+{
+ /**
+ * The reference to the object. This is used to retrieve the object from a database for instance.
+ */
+ private K reference;
+
+ public ReferenceLoadableDetachableModel(E object)
+ {
+ setObject(object);
+ }
+
+ /**
+ * Loads the model object using the provided reference. This method is not for {@code null} objects.
+ * @param reference The reference to the model object.
+ * @return The model object.
+ * @see #load()
+ */
+ protected abstract E load(K reference);
+
+ /**
+ * Returns a reference to the object being modeled. This reference is later passed to the {@link #load(java.io.Serializable)}
+ * method to load the object after {@link #detach()} has been called.
+ * @param object The object from which to obtain a reference.
+ * @return a reference to the object being modeled.
+ */
+ protected abstract K reference(E object);
+
+ /**
+ * Loads and returns the modeled object. This method is once after each {@link #detach()} call.
+ * <p>
+ * If the reference is {@code null}, then {@code null} is returned. Otherwise, {@link #load(java.io.Serializable)} is called
+ * passing it the reference previously returned by {@link #reference(java.lang.Object)}.
+ * @return the modeled object, or {@code null} if the reference is {@code null}
+ */
+ @Override
+ protected final E load()
+ {
+ if (reference == null){
+ return null;
+ }else{
+ return load(reference);
+ }
+ }
+
+ /**
+ * Sets the object being managed. This method calls the {@link #reference(java.lang.Object) } method to get
+ * a reference to the object so it can be loaded again with the {@link #load(java.io.Serializable) } after
+ * {@link #detach() } is called on this object. If the object is {@code null}, then {@link #reference(java.lang.Object)}
+ * is not called - a {@code null} reference is used instead.
+ * @param object The object to model.
+ */
+ @Override
+ public final void setObject(E object)
+ {
+ super.setObject(object);
+ if (object == null){
+ reference = null;
+ }else{
+ reference = reference(object);
+ }
+ }
+}
Something went wrong with that request. Please try again.