KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > oracle > toplink > essentials > sessions > ObjectCopyingPolicy


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the "License"). You may not use this file except
5  * in compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * glassfish/bootstrap/legal/CDDLv1.0.txt or
9  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * HEADER in each file and include the License file at
15  * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
16  * add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your
18  * own identifying information: Portions Copyright [yyyy]
19  * [name of copyright owner]
20  */

21 // Copyright (c) 1998, 2005, Oracle. All rights reserved.
22
package oracle.toplink.essentials.sessions;
23
24 import oracle.toplink.essentials.internal.helper.*;
25 import oracle.toplink.essentials.internal.localization.*;
26
27 /**
28  * <b>Purpose</b>: Define how an object is to be copied.<p>
29  * <b>Description</b>: This is for usage with the object copying feature, not the unit of work.
30  * This is useful for copying an entire object graph as part of the
31  * host application's logic.<p>
32  * <b>Responsibilities</b>:<ul>
33  * <li> Inidcate through CASCADE levels the depth relationships will copied.
34  * <li> Indicate if PK attributes should be copied with existing value or should be reset.
35  * </ul>
36  * @since TOPLink/Java 3.0
37  * @see Session#copyObject(Object, ObjectCopyingPolicy)
38  */

39 public class ObjectCopyingPolicy {
40     protected boolean shouldResetPrimaryKey;
41     protected oracle.toplink.essentials.internal.sessions.AbstractSession session;
42     protected IdentityHashtable copies;
43
44     /** Policy depth that determines how the copy will cascade to the object's
45         related parts */

46     protected int depth;
47
48     /** Depth level indicating that NO relationships should be included in the copy.
49         Relationships that are not copied will include the default value of the object's
50         instantiation policy */

51     public static final int NO_CASCADE = 1;
52
53     /** Depth level indicating that only relationships with mapping indicated privately-
54         owned should be copied */

55     public static final int CASCADE_PRIVATE_PARTS = 2;
56
57     /** Depth level indicating that all relationships with mappings should be used when
58         building the copied object graph */

59     public static final int CASCADE_ALL_PARTS = 3;
60
61     /**
62      * PUBLIC:
63      * Return a new copying policy.
64      * By default the policy cascades privately owned parts and nulls primary keys.
65      */

66     public ObjectCopyingPolicy() {
67         this.shouldResetPrimaryKey = true;
68         // 2612538 - the default size of IdentityHashtable (32) is appropriate
69
this.copies = new IdentityHashtable();
70         this.depth = CASCADE_PRIVATE_PARTS;
71     }
72
73     /**
74      * PUBLIC:
75      * Set if the copy should cascade all relationships when copying the object(s).
76      */

77     public void cascadeAllParts() {
78         setDepth(CASCADE_ALL_PARTS);
79     }
80
81     /**
82      * PUBLIC:
83      * Set if the copy should cascade only those relationships that are configured
84      * as privately-owned.
85      */

86     public void cascadePrivateParts() {
87         setDepth(CASCADE_PRIVATE_PARTS);
88     }
89
90     /**
91      * PUBLIC:
92      * Set if the copy should not cascade relationships when copying the object(s)
93      */

94     public void dontCascade() {
95         setDepth(NO_CASCADE);
96     }
97
98     /**
99      * INTERNAL: Get the session.
100      */

101     public IdentityHashtable getCopies() {
102         return copies;
103     }
104
105     /**
106      * INTERNAL: Return the cascade depth.
107      */

108     public int getDepth() {
109         return depth;
110     }
111
112     /**
113      * INTERNAL: Return the session.
114      */

115     public oracle.toplink.essentials.internal.sessions.AbstractSession getSession() {
116         return session;
117     }
118
119     /**
120      * INTERNAL: Set the copies.
121      */

122     public void setCopies(IdentityHashtable newCopies) {
123         copies = newCopies;
124     }
125
126     /**
127      * INTERNAL: Set the cascade depth.
128      */

129     public void setDepth(int newDepth) {
130         depth = newDepth;
131     }
132
133     /**
134      * INTERNAL: Set the session.
135      */

136     public void setSession(oracle.toplink.essentials.internal.sessions.AbstractSession newSession) {
137         session = newSession;
138     }
139
140     /**
141      * PUBLIC:
142      * Set if the primary key should be reset to null.
143      */

144     public void setShouldResetPrimaryKey(boolean newShouldResetPrimaryKey) {
145         shouldResetPrimaryKey = newShouldResetPrimaryKey;
146     }
147
148     /**
149      * PUBLIC:
150      * Return true if the policy has been configured to CASCADE_ALL_PARTS or CASCADE_PRIVATE_PARTS.
151      */

152     public boolean shouldCascade() {
153         return getDepth() != NO_CASCADE;
154     }
155
156     /**
157      * PUBLIC:
158      * Return true if the policy should CASCADE_ALL_PARTS
159      */

160     public boolean shouldCascadeAllParts() {
161         return getDepth() == CASCADE_ALL_PARTS;
162     }
163
164     /**
165      * PUBLIC:
166      * Return true if the policy should CASCADE_PRIVATE_PARTS
167      */

168     public boolean shouldCascadePrivateParts() {
169         return getDepth() == CASCADE_PRIVATE_PARTS;
170     }
171
172     /**
173      * PUBLIC:
174      * Return if the primary key should be reset to null.
175      */

176     public boolean shouldResetPrimaryKey() {
177         return shouldResetPrimaryKey;
178     }
179
180     /**
181      * INTERNAL:
182      */

183     public String JavaDoc toString() {
184         String JavaDoc depthString = "";
185         if (shouldCascadeAllParts()) {
186             depthString = "CASCADE_ALL_PARTS";
187         } else if (shouldCascadePrivateParts()) {
188             depthString = "CASCADE_PRIVATE_PARTS";
189         } else {
190             depthString = "NO_CASCADING";
191         }
192         Object JavaDoc[] args = { depthString, new Boolean JavaDoc(shouldResetPrimaryKey()) };
193         return Helper.getShortClassName(this) + ToStringLocalization.buildMessage("depth_reset_key", args);
194     }
195 }
196
Popular Tags