KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opensubsystems > patterns > versioneddata > data > VersionedDataObject


1 /*
2  * Copyright (c) 2003 - 2007 OpenSubsystems s.r.o. Slovak Republic. All rights reserved.
3  *
4  * Project: OpenSubsystems
5  *
6  * $Id: VersionedDataObject.java,v 1.2 2007/01/07 06:15:40 bastafidli Exp $
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; version 2 of the License.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  */

21  
22 package org.opensubsystems.patterns.versioneddata.data;
23
24 import java.sql.Timestamp JavaDoc;
25
26 import org.opensubsystems.core.data.DataObject;
27 import org.opensubsystems.core.data.ModifiableDataObject;
28 import org.opensubsystems.core.util.HashCodeUtils;
29
30 /**
31  * Base class for all data objects, which can be versioned and therefore has to
32  * track the current version, if they are tip and the base version of, which this
33  * is a new version.
34  *
35  * @version $Id: VersionedDataObject.java,v 1.2 2007/01/07 06:15:40 bastafidli Exp $
36  * @author Peter Satury
37  * @code.reviewer Miro Halas
38  * @code.reviewed Initial revision
39  */

40 public abstract class VersionedDataObject extends ModifiableDataObject
41 {
42    // Constants ////////////////////////////////////////////////////////////////
43

44    /**
45     * This is the first version number for any versionable data object.
46     */

47    public static final int FIRST_VERSION_NUMBER = 1;
48    
49    // Attributes ///////////////////////////////////////////////////////////////
50

51    /**
52     * Id of the data object, which is base version of this data object. If this
53     * data object is the base version (it is the first version) then this is
54     * equal to the id.
55     */

56    protected int m_iBaseVersionId;
57
58    /**
59     * Flag if this data object is the latest (newest) version called tip.
60     */

61    protected boolean m_bTip;
62    
63    /**
64     * Data object version number. This is the actual version number of this data
65     * object.
66     */

67    protected int m_iVersion;
68    
69    // Constructors /////////////////////////////////////////////////////////////
70

71    /**
72     * Default constructor.
73     */

74    public VersionedDataObject(
75    )
76    {
77       this(DataObject.NEW_ID);
78    }
79
80    /**
81     * Simple constructor creating new data object in particular domain.
82     *
83     * @param iDomainId - domain this data object belongs to
84     */

85    public VersionedDataObject(
86       int iDomainId
87    )
88    {
89       this(DataObject.NEW_ID, iDomainId, DataObject.NEW_ID, true,
90            FIRST_VERSION_NUMBER, null, null);
91    }
92
93    /**
94     * Full constructor.
95     *
96     * @param iId - id of this data object
97     * @param iDomainId - domain this data object belongs to
98     * @param iBaseVersionId - data object base version ID
99     * @param bTip - flag if this data object is TIP
100     * @param iVersion - version number of this data object
101     * @param creationTimestamp - timestamp when the data object was created.
102     * @param modificationTimestamp - timestamp when the data object was last
103     * time modified.
104     */

105    public VersionedDataObject(
106       int iId,
107       int iDomainId,
108       int iBaseVersionId,
109       boolean bTip,
110       int iVersion,
111       Timestamp JavaDoc creationTimestamp,
112       Timestamp JavaDoc modificationTimestamp
113    )
114    {
115       super(iId, iDomainId, creationTimestamp, modificationTimestamp);
116       
117       m_iBaseVersionId = iBaseVersionId;
118       m_bTip = bTip;
119       m_iVersion = iVersion;
120    }
121    
122    // Accessors ////////////////////////////////////////////////////////////////
123

124    /**
125     * Get id of the data object, which is base version of this data object.
126     *
127     * @return int
128     */

129    public int getBaseVersionId()
130    {
131       return m_iBaseVersionId;
132    }
133
134    /**
135     * Set id of the data object, which is base version of this data object.
136     *
137     * @param baseVersionId - id of the version on which is this object based on
138     */

139    public void setBaseVersionId(
140       int baseVersionId
141    )
142    {
143       m_iBaseVersionId = baseVersionId;
144    }
145    
146    /**
147     * Get flag if this data object is the latest (newest) version called tip.
148     *
149     * @return boolean - true if this is the latest version
150     */

151    public boolean isTip()
152    {
153       return m_bTip;
154    }
155
156    /**
157     * Set flag if this data object is the latest (newest) version called tip.
158     *
159     * @param tip - true if this is the tip - latest (newest) version
160     */

161    public void setTip(
162       boolean tip
163    )
164    {
165       m_bTip = tip;
166    }
167
168    /**
169     * Get the actual version number of this data object.
170     *
171     * @return int
172     */

173    public int getVersion()
174    {
175       return m_iVersion;
176    }
177    
178    /**
179     * Set the actual version number of this data object.
180     *
181     * @param version - version of this object
182     */

183    public void setVersion(
184       int version
185    )
186    {
187       m_iVersion = version;
188    }
189
190    /**
191     * {@inheritDoc}
192     */

193    public boolean equals(
194       Object JavaDoc oObject
195    )
196    {
197       boolean bReturn = false;
198       VersionedDataObject helper;
199
200       if (oObject == this)
201       {
202          bReturn = true;
203       }
204       else if ((oObject != null) && (oObject instanceof VersionedDataObject))
205       {
206          helper = (VersionedDataObject) oObject;
207          bReturn = (m_bTip == helper.m_bTip)
208                    && (m_iBaseVersionId == helper.m_iBaseVersionId)
209                    && (m_iVersion == helper.m_iVersion)
210                    && (super.equals(oObject));
211       }
212       
213       return bReturn;
214    }
215
216    /**
217     * {@inheritDoc}
218     */

219    public int hashCode()
220    {
221       int iResult = HashCodeUtils.SEED;
222       iResult = HashCodeUtils.hash(iResult, m_bTip);
223       iResult = HashCodeUtils.hash(iResult, m_iBaseVersionId);
224       iResult = HashCodeUtils.hash(iResult, m_iVersion);
225       iResult = HashCodeUtils.hash(iResult, super.hashCode());
226       return iResult;
227    }
228
229    // Helper methods ///////////////////////////////////////////////////////////
230

231    /**
232     * Restore all values from specified values. This is here to reinitialize
233     * object in case it needs to be reused or reconstructed (e.g. when rollback
234     * is issued).
235     *
236     * @param iId - id of this data object
237     * @param iDomainId - domain this data object belongs to
238     * @param iBaseVersionId - data object base version ID
239     * @param bTip - flag if this data object is TIP
240     * @param iVersion - version number of this data object
241     * @param creationTimestamp - timestamp when the data object was created.
242     * @param modificationTimestamp - timestamp when the data object was last
243     * time modified.
244     */

245    protected void restore(
246       int iId,
247       int iDomainId,
248       int iBaseVersionId,
249       boolean bTip,
250       int iVersion,
251       Timestamp JavaDoc creationTimestamp,
252       Timestamp JavaDoc modificationTimestamp
253    )
254    {
255       super.restore(iId, iDomainId, creationTimestamp, modificationTimestamp);
256       
257       m_iBaseVersionId = iBaseVersionId;
258       m_bTip = bTip;
259       m_iVersion = iVersion;
260    }
261 }
262
Popular Tags