001: /**
002: * Copyright (C) 2006, 2007 David Bulmore, Software Sensation Inc.
003: * All Rights Reserved.
004: *
005: * This file is part of JPersist.
006: *
007: * JPersist is free software; you can redistribute it and/or modify it under
008: * the terms of the GNU General Public License (Version 2) as published by
009: * the Free Software Foundation.
010: *
011: * JPersist is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with JPersist; if not, write to the Free Software Foundation,
018: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
019: */package jpersist;
020:
021: import java.util.HashMap;
022: import java.util.HashSet;
023: import java.util.Map;
024: import java.util.Set;
025:
026: /**
027: * This class is extended by classes that wish to be persistent.
028: */
029: @SuppressWarnings("unchecked")
030: // working to complete a Java 1.5 version
031: public class PersistentObject {
032: static final int OBJECT_CAN_PERSIST = 0;
033: static final int MISSING_ROW_ID = 1;
034: static final int NO_TABLE_INFO = 2;
035:
036: private int objectPersistence = OBJECT_CAN_PERSIST;
037: private long objectChecksum;
038: private Set ignoreAssociationClasses = null;
039: private Map objectKeyValues = null;
040: private boolean objectHasChanged, reloadAfterSave = true,
041: ignoreAssociations;
042:
043: long getObjectChecksum() {
044: return objectChecksum;
045: }
046:
047: void setObjectChecksum(long objectChecksum) {
048: this .objectChecksum = objectChecksum;
049: }
050:
051: Map getObjectKeyValues() {
052: if (objectKeyValues == null)
053: objectKeyValues = new HashMap();
054:
055: return objectKeyValues;
056: }
057:
058: void setObjectPersistence(int reason) {
059: objectPersistence = reason;
060: }
061:
062: /**
063: * Returns true is associations are being ignored and not loaded or saved, false otherwise.
064: *
065: * @return true is associations are being ignored and not loaded or saved, false otherwise.
066: */
067: protected boolean getIgnoreAssociations() {
068: return ignoreAssociations;
069: }
070:
071: /**
072: * Set whether loads/saves ignore associations.
073: *
074: * @param ignoreAssociations true to ignore saving/loading associations, false otherwise.
075: */
076: protected void setIgnoreAssociations(boolean ignoreAssociations) {
077: this .ignoreAssociations = ignoreAssociations;
078: }
079:
080: /**
081: * Adds the class to the set of ignored associations.
082: *
083: *
084: * @param objectClass the class to ignore
085: */
086: protected void addIgnoreAssociation(Class objectClass) {
087: if (ignoreAssociationClasses == null)
088: ignoreAssociationClasses = new HashSet();
089:
090: ignoreAssociationClasses.add(objectClass);
091: }
092:
093: /**
094: * Removes the class from the set of ignored associations.
095: *
096: *
097: * @param objectClass the class to remove
098: */
099: protected void removeIgnoreAssociation(Class objectClass) {
100: if (ignoreAssociationClasses != null)
101: ignoreAssociationClasses.remove(objectClass);
102: }
103:
104: boolean classInIgnoreAssociation(Class objectClass) {
105: if (ignoreAssociationClasses != null
106: && ignoreAssociationClasses.contains(objectClass))
107: return true;
108:
109: return false;
110: }
111:
112: /**
113: * Returns true if the object is to be reloaded following a save.
114: *
115: * @return true if the object is to be reloaded following a save.
116: */
117: protected boolean getReloadAfterSave() {
118: return reloadAfterSave;
119: }
120:
121: /**
122: * Set whether objects are reloaded after saves. If the database changes
123: * any of the columns associated with an object, it should be reloaded.
124: *
125: * @param reloadAfterSave true if the object is to be reloaded following a save
126: */
127: protected void setReloadAfterSave(boolean reloadAfterSave) {
128: this .reloadAfterSave = reloadAfterSave;
129: }
130:
131: /**
132: * Returns whether an object can be persistent, and the reason why or why not.
133: * Objects are checked during loading to see if they can be made persistent.
134: * If they can OBJECT_CAN_PERSIST will be returned. If the object can't persist,
135: * then the reason why, usually MISSING_ROW_ID, will be returned.
136: *
137: * @return whether an object can be persistent, and the reason why or why not
138: */
139: protected int getObjectPersistence() {
140: return objectPersistence;
141: }
142:
143: /**
144: * Returns the key value that was originally loaded with the object. Even if the
145: * field in the object changes the original key's value remains for updates
146: * and deletes.
147: *
148: * @param key a key field defined in the object
149: * @return the key value that was originally loaded with the object
150: */
151: protected Object getObjectKeyValue(Object key) {
152: if (objectKeyValues != null)
153: return objectKeyValues.get(key);
154:
155: return null;
156: }
157:
158: /**
159: * Make object transient/new. Will insert instead of update.
160: */
161: protected void makeObjectTransient() {
162: objectKeyValues = null;
163: objectChecksum = 0;
164: objectHasChanged = false;
165: }
166:
167: /**
168: * Returns true if the object has changed in some way and needs to be saved.
169: *
170: *
171: * @return true if the object has changed in some way and needs to be saved.
172: */
173: protected boolean objectHasChanged() {
174: return objectHasChanged;
175: }
176:
177: /**
178: * Set the state of the object to has changed which will cause it to be saved.
179: *
180: *
181: * @param objectHasChanged true if the object has changed and needs to be saved
182: */
183: protected void setObjectHasChanged(boolean objectHasChanged) {
184: this .objectHasChanged = objectHasChanged;
185: }
186:
187: /**
188: * Returns true if object is in the persistent state (has been saved/loaded and will be updated)
189: * @return true if object is in the persistent state (has been saved/loaded and will be updated)
190: */
191: protected boolean isPersistent() {
192: return getObjectChecksum() != 0
193: && getObjectPersistence() == PersistentObject.OBJECT_CAN_PERSIST;
194: }
195:
196: /**
197: * Returns true if the object can be persistent. If an object doesn't have table information
198: * or row id information, it can not be persistent.
199: *
200: * @return true if the object can be persistent.
201: */
202: protected boolean canPersist() {
203: return getObjectPersistence() == PersistentObject.OBJECT_CAN_PERSIST;
204: }
205:
206: /**
207: * Saves the object to a matching database table using the jpersist.Database instance.
208: *
209: * @param database a database instance
210: *
211: * @throws JPersistException
212: */
213: public int save(Database database) throws JPersistException {
214: return database.saveObject(this );
215: }
216:
217: /**
218: * Saves the object to a matching database table using the jpersist.DatabaseManager instance.
219: *
220: * @param databaseManager a database manager instance
221: *
222: * @throws JPersistException
223: */
224: public int save(DatabaseManager databaseManager)
225: throws JPersistException {
226: return databaseManager.saveObject(this );
227: }
228:
229: /**
230: * Saves the object to a matching database table using the jpersist.TransactionManager instance.
231: *
232: * @param transactionManager a jpersist.TransactionManager instance
233: *
234: * @throws JPersistException
235: */
236: public int save(TransactionManager transactionManager)
237: throws JPersistException {
238: return transactionManager.getDatabase().saveObject(this );
239: }
240:
241: /**
242: * Deletes the object from a matching database table using the jpersist.Database instance.
243: *
244: * @param database a database instance
245: *
246: * @throws JPersistException
247: */
248: protected int delete(Database database) throws JPersistException {
249: return database.deleteObject(this );
250: }
251:
252: /**
253: * Deletes the object from a matching database table using the jpersist.DatabaseManager instance.
254: *
255: * @param databaseManager a database manager instance
256: *
257: * @throws JPersistException
258: */
259: protected int delete(DatabaseManager databaseManager)
260: throws JPersistException {
261: return databaseManager.deleteObject(this );
262: }
263:
264: /**
265: * Deletes the object from a matching database table using the jpersist.TransactionManager instance.
266: *
267: * @param transactionManager a jpersist.TransactionManager instance
268: *
269: * @throws JPersistException
270: */
271: protected int delete(TransactionManager transactionManager)
272: throws JPersistException {
273: return transactionManager.getDatabase().deleteObject(this);
274: }
275: }
|