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 {
40 * Primary key of persistent objects.
44 * UUID of the object used for identification in equals() and in hashCode().
46 private String _uid = IdGenerator.createId();
48 protected abstract static class Properties implements ObjectProperties {
49 private long rid; // Primary key of persistent objects
51 private boolean tobechecked = true; // Property validity check flag
53 public void disableCheck() {
57 public boolean mustBeChecked() {
70 public long getIndex() {
80 public void setIndex(final long rid) {
85 // ==============================================================================================================================
87 // ==============================================================================================================================
89 * Database fetch constructor.
91 protected Persistent() {
92 rid = 0; // Set when loading the object
96 * Checks the mutual compatibility of arguments previously set in the given properties object, if the validity check is enabled. As this
97 * validity depends on concrete classes, the check is delegated to subclasses of the given Persistent.Properties.
99 protected Persistent(final ObjectProperties oprop)
100 throws MissedPropertyException, InvalidPropertyException,
101 MultiplyDefinedException {
102 if (oprop.mustBeChecked()) {
103 oprop.checkValidity(); // Throws one of the above exception if not valid
105 rid = 0; // Set when saving the object
108 // ==============================================================================================================================
109 // Public member functions
110 // ==============================================================================================================================
113 * Persistent objects are equal if their UIDs are equal.<BR>
116 * @see java.lang.Object#equals(java.lang.Object)
119 public boolean equals(final Object entity) {
120 boolean res = (entity != null);
122 res = Persistent.class.isAssignableFrom(entity.getClass());
124 Persistent object = (Persistent) entity;
125 res = getUid().equals(object.getUid());
132 * 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
135 * @return the PID of this, or 0 if this is not saved.
138 public long getIndex() {
143 * Set an id for the object.
148 public void setIndex(final long anId) {
155 * @see java.lang.Object#hashCode()
158 public int hashCode() {
159 return getUid().hashCode();
163 * Returns true if this object is saved.
165 * @return true if this is saved.
168 public boolean isSaved() {
169 return (getIndex() != 0); // getIndex() is supposed fetching the index if not yet done
173 * Return a string representing uniquely this object.
175 * @return the unique string representation of this object.
178 public String toString() {
179 return new StringBuffer("object ").append(getClass().getName()).append(
180 "@").append(getUid()).append("@").append(getIndex()).toString();
188 public long getRid() {
198 public void setRid(final long rid) {
207 public String getUid() {
217 public void setUid(final String uid) {