KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > engine > CascadeStyle


1 //$Id: CascadeStyle.java,v 1.1 2005/06/05 04:31:33 oneovthafew Exp $
2
package org.hibernate.engine;
3
4 import java.io.Serializable JavaDoc;
5 import java.util.HashMap JavaDoc;
6 import java.util.Map JavaDoc;
7
8 import org.hibernate.MappingException;
9 import org.hibernate.util.ArrayHelper;
10
11 /**
12  * A style of cascade that can be specified by the mapping for an association.
13  * The style is specified by the <tt>cascade</tt> attribute in the mapping file.
14  *
15  * @author Gavin King
16  */

17 public abstract class CascadeStyle implements Serializable JavaDoc {
18     
19     /**
20      * Should the given action be cascaded?
21      */

22     public abstract boolean doCascade(CascadingAction action);
23     
24     /**
25      * Should the given action really, really be cascaded?
26      */

27     public boolean reallyDoCascade(CascadingAction action) {
28         return doCascade(action);
29     }
30     
31     /**
32      * Do we need to delete orphaned collection elements?
33      */

34     public boolean hasOrphanDelete() {
35         return false;
36     }
37
38     public static final class MultipleCascadeStyle extends CascadeStyle {
39         private final CascadeStyle[] styles;
40         public MultipleCascadeStyle(CascadeStyle[] styles) {
41             this.styles = styles;
42         }
43         public boolean doCascade(CascadingAction action) {
44             for (int i=0; i<styles.length; i++) {
45                 if ( styles[i].doCascade(action) ) return true;
46             }
47             return false;
48         }
49         public boolean reallyDoCascade(CascadingAction action) {
50             for (int i=0; i<styles.length; i++) {
51                 if ( styles[i].reallyDoCascade(action) ) return true;
52             }
53             return false;
54         }
55         public boolean hasOrphanDelete() {
56             for (int i=0; i<styles.length; i++) {
57                 if ( styles[i].hasOrphanDelete() ) return true;
58             }
59             return false;
60         }
61         public String JavaDoc toString() {
62             return ArrayHelper.toString(styles);
63         }
64     }
65     
66     /**
67      * save / delete / update / evict / lock / replicate / merge / persist + delete orphans
68      */

69     public static final CascadeStyle ALL_DELETE_ORPHAN = new CascadeStyle() {
70         public boolean doCascade(CascadingAction action) {
71             return true;
72         }
73         public boolean hasOrphanDelete() {
74             return true;
75         }
76         public String JavaDoc toString() {
77             return "STYLE_ALL_DELETE_ORPHAN";
78         }
79     };
80     
81     /**
82      * save / delete / update / evict / lock / replicate / merge / persist
83      */

84     public static final CascadeStyle ALL = new CascadeStyle() {
85         public boolean doCascade(CascadingAction action) {
86             return true;
87         }
88         public String JavaDoc toString() {
89             return "STYLE_ALL";
90         }
91     };
92     
93     /**
94      * save / update
95      */

96     public static final CascadeStyle UPDATE = new CascadeStyle() {
97         public boolean doCascade(CascadingAction action) {
98             return action==CascadingAction.SAVE_UPDATE || action==CascadingAction.SAVE_UPDATE_COPY;
99         }
100         public String JavaDoc toString() {
101             return "STYLE_SAVE_UPDATE";
102         }
103     };
104     
105     /**
106      * lock
107      */

108     public static final CascadeStyle LOCK = new CascadeStyle() {
109         public boolean doCascade(CascadingAction action) {
110             return action==CascadingAction.LOCK;
111         }
112         public String JavaDoc toString() {
113             return "STYLE_LOCK";
114         }
115     };
116     
117     /**
118      * refresh
119      */

120     public static final CascadeStyle REFRESH = new CascadeStyle() {
121         public boolean doCascade(CascadingAction action) {
122             return action==CascadingAction.REFRESH;
123         }
124         public String JavaDoc toString() {
125             return "STYLE_REFRESH";
126         }
127     };
128     
129     /**
130      * evict
131      */

132     public static final CascadeStyle EVICT = new CascadeStyle() {
133         public boolean doCascade(CascadingAction action) {
134             return action==CascadingAction.EVICT;
135         }
136         public String JavaDoc toString() {
137             return "STYLE_EVICT";
138         }
139     };
140     
141     /**
142      * replicate
143      */

144     public static final CascadeStyle REPLICATE = new CascadeStyle() {
145         public boolean doCascade(CascadingAction action) {
146             return action==CascadingAction.REPLICATE;
147         }
148         public String JavaDoc toString() {
149             return "STYLE_REPLICATE";
150         }
151     };
152     /**
153      * merge
154      */

155     public static final CascadeStyle MERGE = new CascadeStyle() {
156         public boolean doCascade(CascadingAction action) {
157             return action==CascadingAction.MERGE;
158         }
159         public String JavaDoc toString() {
160             return "STYLE_MERGE";
161         }
162     };
163     
164     /**
165      * create
166      */

167     public static final CascadeStyle PERSIST = new CascadeStyle() {
168         public boolean doCascade(CascadingAction action) {
169             return action==CascadingAction.PERSIST;
170         }
171         public String JavaDoc toString() {
172             return "STYLE_PERSIST";
173         }
174     };
175     
176     /**
177      * delete
178      */

179     public static final CascadeStyle DELETE = new CascadeStyle() {
180         public boolean doCascade(CascadingAction action) {
181             return action==CascadingAction.DELETE;
182         }
183         public String JavaDoc toString() {
184             return "STYLE_DELETE";
185         }
186     };
187     
188     /**
189      * delete + delete orphans
190      */

191     public static final CascadeStyle DELETE_ORPHAN = new CascadeStyle() {
192         public boolean doCascade(CascadingAction action) {
193             return action==CascadingAction.DELETE || action==CascadingAction.SAVE_UPDATE;
194         }
195         public boolean reallyDoCascade(CascadingAction action) {
196             return action==CascadingAction.DELETE;
197         }
198         public boolean hasOrphanDelete() {
199             return true;
200         }
201         public String JavaDoc toString() {
202             return "STYLE_DELETE_ORPHAN";
203         }
204     };
205     
206     /**
207      * no cascades
208      */

209     public static final CascadeStyle NONE = new CascadeStyle() {
210         public boolean doCascade(CascadingAction action) {
211             return false;
212         }
213         public String JavaDoc toString() {
214             return "STYLE_NONE";
215         }
216     };
217     
218     CascadeStyle() {}
219     
220     static final Map JavaDoc STYLES = new HashMap JavaDoc();
221     static {
222         STYLES.put("all", ALL);
223         STYLES.put("all-delete-orphan", ALL_DELETE_ORPHAN);
224         STYLES.put("save-update", UPDATE);
225         STYLES.put("persist", PERSIST);
226         STYLES.put("merge", MERGE);
227         STYLES.put("lock", LOCK);
228         STYLES.put("refresh", REFRESH);
229         STYLES.put("replicate", REPLICATE);
230         STYLES.put("evict", EVICT);
231         STYLES.put("delete", DELETE);
232         STYLES.put("delete-orphan", DELETE_ORPHAN);
233         STYLES.put("none", NONE);
234     }
235     
236     public static CascadeStyle getCascadeStyle(String JavaDoc cascade) {
237         CascadeStyle style = (CascadeStyle) STYLES.get(cascade);
238         if (style==null) {
239             throw new MappingException("Unsupported cascade style: " + cascade);
240         }
241         else {
242             return style;
243         }
244     }
245 }
Popular Tags