KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > PersistentObject


1 /*****************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  ****************************************************************/

19
20 package org.apache.cayenne;
21
22 /**
23  * A convenience base superclass for concrete Persistent objects. Provides implementation
24  * of properties declared in Persistent interface.
25  * <h4>POJO Note</h4>
26  * <p>
27  * If having PersistentObject as a superclass presents a problem in an application, source
28  * code of this class can be copied verbatim to a custom class generation template.
29  * Desired superclass can be set in CayenneModeler.
30  * </p>
31  *
32  * @since 1.2
33  * @author Andrus Adamchik
34  */

35 public abstract class PersistentObject implements Persistent {
36
37     protected ObjectId objectId;
38     protected int persistenceState;
39     protected transient ObjectContext objectContext;
40
41     /**
42      * Creates a new transient object.
43      */

44     public PersistentObject() {
45         this.persistenceState = PersistenceState.TRANSIENT;
46     }
47
48     public int getPersistenceState() {
49         return persistenceState;
50     }
51
52     public void setPersistenceState(int persistenceState) {
53         this.persistenceState = persistenceState;
54
55         if (persistenceState == PersistenceState.TRANSIENT) {
56             setObjectContext(null);
57         }
58     }
59
60     public ObjectContext getObjectContext() {
61         return objectContext;
62     }
63
64     public void setObjectContext(ObjectContext objectContext) {
65         this.objectContext = objectContext;
66     }
67
68     public ObjectId getObjectId() {
69         return objectId;
70     }
71
72     public void setObjectId(ObjectId objectId) {
73         this.objectId = objectId;
74     }
75
76     public String JavaDoc toString() {
77         String JavaDoc state = PersistenceState.persistenceStateName(getPersistenceState());
78
79         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
80         buffer
81                 .append("<")
82                 .append(getClass().getName())
83                 .append("@")
84                 .append(System.identityHashCode(this))
85                 .append(", id=")
86                 .append(objectId)
87                 .append(", state=")
88                 .append(state)
89                 .append(", context=")
90                 .append(objectContext)
91                 .append(">");
92
93         return buffer.toString();
94     }
95 }
96
Popular Tags