1 package org.splat.dal.bo.kernel;
3 import org.splat.kernel.InvalidPropertyException;
4 import org.splat.kernel.MissedPropertyException;
5 import org.splat.kernel.MultiplyDefinedException;
6 import org.splat.kernel.ObjectProperties;
9 * Base implementation of the API Design Pattern of objects which persistence is based on Hibernate.<br/> This Design Pattern supports the
12 * <li>Flexible API for constructing objects from many variable arguments</li>
13 * <li>Impossible to leave persistent objects in inconsistent state, even temporarily, during their construction</li>
14 * <li>Same Object Oriented API for constructing and selecting objects from the database</li>
15 * <li>Centralized validity check of arguments</li>
17 * The API is based on intermediate properties objects used for collecting arguments and checking their validity. These properties objects
18 * are passed to persistent object constructors and database select functions for execution. For example, as based on this Design Pattern, a
19 * User object could be created that way:
22 * User.Properties args = new User.Properties();
23 * User user = new User(args.setUsername("mypseudo").setMailAddress(
24 * "me@provider.domain"));
27 * Classes implementing this Design Pattern must inherit from Persistent and implement their Properties class as a nested subclass of
28 * Persistent.Properties.<br/> <br/> Naturally, as usual with Hibernate, any class can be persistent (you don't need to inherit from
29 * Persistent for being persistent). Inheriting from Persistent is only a matter of enabling the API supported by the above Design Pattern.
31 * @see ObjectProperties
32 * @see Persistent.Properties
33 * @author Daniel Brunier-Coulin
34 * @copyright OPEN CASCADE 2012
37 public abstract class Persistent {
39 private long rid; // Primary key of persistent objects
41 protected abstract static class Properties implements ObjectProperties {
42 private long rid; // Primary key of persistent objects
44 private boolean tobechecked = true; // Property validity check flag
46 public void disableCheck() {
50 public boolean mustBeChecked() {
63 public long getIndex() {
73 public void setIndex(long rid) {
78 // ==============================================================================================================================
80 // ==============================================================================================================================
82 * Database fetch constructor.
84 protected Persistent() {
85 // -----------------------
86 rid = 0; // Set when loading the object
90 * Checks the mutual compatibility of arguments previously set in the given properties object, if the validity check is enabled. As this
91 * validity depends on concrete classes, the check is delegated to subclasses of the given Persistent.Properties.
93 protected Persistent(ObjectProperties oprop)
94 throws MissedPropertyException, InvalidPropertyException,
95 MultiplyDefinedException {
96 // ---------------------------------------------
97 if (oprop.mustBeChecked())
98 oprop.checkValidity(); // Throws one of the above exception if not valid
99 rid = 0; // Set when saving the object
102 // ==============================================================================================================================
103 // Public member functions
104 // ==============================================================================================================================
106 public boolean equals(Object entity) {
107 // ------------------------------------
110 if (entity.getClass().equals(this.getClass())) {
111 Persistent object = (Persistent) entity;
112 long he = object.getIndex(); // getIndex() is supposed fetching the index if not yet done
113 long me = this.getIndex(); // getIndex() is supposed fetching the index if not yet done
117 return (this == object);
123 * Returns the Persistent ID of this object. The PID is set when saving this object. It is unique in the scope of the class of this
126 * @return the PID of this, or 0 if this is not saved.
129 public long getIndex() {
130 // ----------------------
135 * Set an id for the object.
140 public void setIndex(long anId) {
144 public int hashCode() {
145 // ----------------------
146 return toString().hashCode();
150 * Returns true if this object is saved.
152 * @return true if this is saved.
155 public boolean isSaved() {
156 // -------------------------
157 return (getIndex() != 0); // getIndex() is supposed fetching the index if not yet done
161 * Return a string representing uniquely this object.
163 * @return the unique string representation of this object.
165 public String toString() {
166 // -------------------------
167 long oid = getIndex(); // getIndex() is supposed fetching the index if not yet done
169 oid = super.hashCode(); // WARNING: Must not call super.toString() as it goes back here (this.toString())
170 return new StringBuffer("object ").append(getClass().getName()).append(
171 "@").append(oid).toString();
179 public long getRid() {
189 public void setRid(long rid) {