KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opensubsystems > core > data > DataObject


1 /*
2  * Copyright (c) 2003 - 2007 OpenSubsystems s.r.o. Slovak Republic. All rights reserved.
3  *
4  * Project: OpenSubsystems
5  *
6  * $Id: DataObject.java,v 1.9 2007/01/07 06:14:17 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.core.data;
23
24 import java.io.Serializable JavaDoc;
25
26 /**
27  * Base class for all data objects regardless of how they are persisted.
28  * Data object is here to implement Transfer Object pattern as described
29  * in http://java.sun.com/blueprints/corej2eepatterns/Patterns/TransferObject.html
30  * The main goal is to encapsulate set of related attributes as one object
31  * so they can be easily passed between different modules and tiers.
32  *
33  * @version $Id: DataObject.java,v 1.9 2007/01/07 06:14:17 bastafidli Exp $
34  * @author Miro Halas
35  * @code.reviewer Miro Halas
36  * @code.reviewed 1.6 2005/09/07 16:32:31 alkalimero
37  */

38 public abstract class DataObject implements Serializable JavaDoc
39 {
40    // Constants ////////////////////////////////////////////////////////////////
41

42    /**
43     * ID used when creating new object for which the real ID wasn't generated yet.
44     * HSQLDB at some time autogenerated first entry as 0 and it wasn't possible
45     * to change it. Therefore we used -1 as value which won't be generated by DB.
46     * Then HSQLDB made it configurable but I was afraid that -1 was already used
47     * at some places as hardcoded value and not at constant that it is too risky
48     * to change it to 0 (which is what would it be if the value it not initialized)
49     */

50    public static final int NEW_ID = -1;
51
52    /**
53     * Object representation for easy reuse.
54     */

55    public static final Integer JavaDoc NEW_ID_OBJ = new Integer JavaDoc(NEW_ID);
56    
57    /**
58     * String representation for easy reuse.
59     */

60    public static final String JavaDoc NEW_ID_STR = Integer.toString(NEW_ID);
61    
62    // Attributes ///////////////////////////////////////////////////////////////
63

64    /**
65     * Flag which is set to true if this data was loaded from some persistance
66     * store, e.g. database and false if it was constructed in memory and it is
67     * not persisted.
68     */

69    private boolean m_bFromPersistanceStore = false;
70
71    // Accessors ////////////////////////////////////////////////////////////////
72

73    /**
74     * Check if this record was loaded from some persistence store and therefore
75     * it is existing data object which was already created in the persistence
76     * store or it came from somewhere else (was created in memory).
77     *
78     * @return boolean
79     */

80    public boolean isFromPersistenceStore(
81    )
82    {
83       return m_bFromPersistanceStore;
84    }
85
86    /**
87     * Set flag that this record was loaded from some persistance store.
88     */

89    public void setFromPersistenceStore(
90    )
91    {
92       m_bFromPersistanceStore = true;
93    }
94
95    /**
96     * Get the id uniquely identifying the object.
97     *
98     * @return int
99     */

100    public abstract int getId(
101    );
102
103    /**
104     * Get the id as object uniquely identifying the object. This method allows
105     * to the implementing class to cache this object if desired to improve
106     * performance.
107     *
108     * @return Integer
109     */

110    public abstract Integer JavaDoc getIdAsObject(
111    );
112    
113    /**
114     * Method to compare all data attributes of two objects to figure out, if
115     * their attributes are the same. The objects can for example differ by database
116     * generated id and creation or modification timestamp but if other properties
117     * which represents the real data are the same then the objects are considered
118     * to be the same. This is weaker constraint that equals since not all
119     * attributes has to be the same, only the one which really represents the
120     * business data should match.
121     *
122     * @param oObject - Object to compare
123     * @return boolean - true if same, false otherwise
124     */

125    public abstract boolean isSame(
126       Object JavaDoc oObject
127    );
128 }
129
Popular Tags