Source Code Cross Referenced for PersistentObject.java in  » Web-Framework » jWebApp » jpersist » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Web Framework » jWebApp » jpersist 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.