Salome HOME
42629f251affcbab6394d7d14ededace60add091
[tools/siman.git] / Workspace / Siman-Common / src / org / splat / dal / bo / kernel / Persistent.java
1 package org.splat.dal.bo.kernel;
2
3 import org.splat.kernel.InvalidPropertyException;
4 import org.splat.kernel.MissedPropertyException;
5 import org.splat.kernel.MultiplyDefinedException;
6 import org.splat.kernel.ObjectProperties;
7
8 /**
9  * Base implementation of the API Design Pattern of objects which persistence is based on Hibernate.<br/>
10  * This Design Pattern supports the following features:
11  * <ul>
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>
16  * </ul>
17  * The API is based on intermediate properties objects used for collecting arguments and checking their validity.
18  * These properties objects are passed to persistent object constructors and database select functions for execution.
19  * For example, as based on this Design Pattern, a User object could be created that way:
20  * <pre>
21  *   User.Properties  args = new User.Properties();
22  *   User  user = new User( args.setUsername("mypseudo").setMailAddress("me@provider.domain") );
23  * </pre>
24  * Classes implementing this Design Pattern must inherit from Persistent and implement their Properties class as a nested
25  * subclass of Persistent.Properties.<br/>
26  * <br/>
27  * Naturally, as usual with Hibernate, any class can be persistent (you don't need to inherit from Persistent for being
28  * persistent). Inheriting from Persistent is only a matter of enabling the API supported by the above Design Pattern.
29  * 
30  * @see ObjectProperties
31  * @see Persistent.Properties
32  * @author    Daniel Brunier-Coulin
33  * @copyright OPEN CASCADE 2012
34  */
35
36
37 public abstract class Persistent {
38
39     private  long rid;                                     // Primary key of persistent objects
40
41     protected abstract static class Properties implements ObjectProperties {
42 //  ----------------------------------------------------------------------
43       private boolean tobechecked = true;                 // Property validity check flag
44
45           public void disableCheck () {
46         tobechecked = false;
47       }
48           public boolean mustBeChecked () {
49                 return tobechecked;
50           }
51           public void clear () {
52                 tobechecked = true;
53           }
54     }
55
56 //  ==============================================================================================================================
57 //  Constructors
58 //  ==============================================================================================================================
59 /**
60  * Database fetch constructor.
61  */
62     protected Persistent () {
63 //  -----------------------
64       rid = 0;                                            // Set when loading the object
65     }
66
67 /**
68  * Checks the mutual compatibility of arguments previously set in the given properties object, if the validity check is enabled.
69  * As this validity depends on concrete classes, the check is delegated to subclasses of the given Persistent.Properties.
70  */
71     protected Persistent (ObjectProperties oprop) throws MissedPropertyException, InvalidPropertyException, MultiplyDefinedException {
72 //  ---------------------------------------------
73       if (oprop.mustBeChecked()) oprop.checkValidity();   // Throws one of the above exception if not valid
74       rid = 0;                                            // Set when saving the object
75     }
76
77 //  ==============================================================================================================================
78 //  Public member functions
79 //  ==============================================================================================================================
80
81     public boolean equals(Object entity) {
82 //  ------------------------------------
83       if (entity == null) return false;
84       if (entity.getClass().equals(this.getClass())) {
85         Persistent object = (Persistent)entity;
86         long   he = object.getIndex();                     // getIndex() is supposed fetching the index if not yet done
87         long   me = this.getIndex();                       // getIndex() is supposed fetching the index if not yet done
88         if (me*he != 0) return (he == me);
89         if (me+he == 0) return (this == object);
90       }
91       return false;
92     }
93
94 /**
95  * Returns the Persistent ID of this object. The PID is set when saving this object. It is unique in the scope of the class
96  * of this object only.
97  * 
98  * @return the PID of this, or 0 if this is not saved.
99  * @see    isSaved()
100  */
101     public long getIndex () {
102 //  ----------------------
103       return rid;
104     }
105
106         /**
107          * Set an id for the object.
108          * @param anId id to set
109          */
110         public void setIndex(long anId) {
111                 rid = anId;
112         }
113         
114     public int hashCode () {
115 //  ----------------------
116       return toString().hashCode();
117     }
118
119 /**
120  * Returns true if this object is saved.
121  * 
122  * @return true if this is saved.
123  * @see    getIndex()
124  */
125     public boolean isSaved () {
126 //  -------------------------
127       return (getIndex() != 0);                           // getIndex() is supposed fetching the index if not yet done
128     }
129
130 /**
131  * Return a string representing uniquely this object.
132  * 
133  * @return the unique string representation of this object.
134  */
135     public String toString () {
136 //  -------------------------
137       long oid = getIndex();                               // getIndex() is supposed fetching the index if not yet done
138       if (oid == 0) oid = super.hashCode();               //WARNING: Must not call super.toString() as it goes back here (this.toString())
139       return new StringBuffer("object ").append(getClass().getName()).append("@").append(oid).toString();
140     }
141
142 /**
143  * Get the rid.
144  * @return the rid
145  */
146 public long getRid() {
147         return rid;
148 }
149
150 /**
151  * Set the rid.
152  * @param rid the rid to set
153  */
154 public void setRid(long rid) {
155         this.rid = rid;
156 }
157 }