KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > amber > field > CascadableField


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Rodrigo Westrupp
28  */

29
30 package com.caucho.amber.field;
31
32 import com.caucho.amber.type.RelatedType;
33 import com.caucho.config.ConfigException;
34 import com.caucho.java.JavaWriter;
35 import com.caucho.log.Log;
36 import com.caucho.util.L10N;
37
38 import javax.persistence.CascadeType;
39 import java.io.IOException JavaDoc;
40 import java.util.logging.Logger JavaDoc;
41
42 /**
43  * A child field that is cascadable from parent to child
44  * on persist, merge, remove or update operations.
45  */

46 abstract public class CascadableField extends AbstractField {
47   private static final L10N L = new L10N(CascadableField.class);
48   private static final Logger JavaDoc log = Log.open(CascadableField.class);
49
50   private CascadeType[] _cascadeTypes;
51
52   CascadableField(RelatedType sourceType)
53   {
54     super(sourceType);
55   }
56
57   CascadableField(RelatedType sourceType,
58                   String JavaDoc name,
59                   CascadeType[] cascadeTypes)
60     throws ConfigException
61   {
62     super(sourceType, name);
63
64     _cascadeTypes = cascadeTypes;
65   }
66
67   /**
68    * Returns true if this is cascadable
69    * from parent to child.
70    */

71   public boolean isCascade(CascadeType cascade)
72   {
73     if (_cascadeTypes == null)
74       return false;
75
76     for (int i = 0; i < _cascadeTypes.length; i++) {
77       if (_cascadeTypes[i] == CascadeType.ALL)
78         return true;
79
80       if (_cascadeTypes[i] == cascade)
81         return true;
82     }
83
84     return false;
85   }
86
87   /**
88    * Sets the cascade types for this field
89    * from parent to child.
90    */

91   public void setCascadeType(CascadeType[] cascadeTypes)
92   {
93     _cascadeTypes = cascadeTypes;
94   }
95
96   /**
97    * Generates the (pre) cascade operation from
98    * parent to this child. This field will only
99    * be cascaded first if the operation can be
100    * performed with no risk to break FK constraints.
101    *
102    * Default is to pre-cascade the persist() operation only.
103    *
104    * Check subclasses for one-to-one, many-to-one,
105    * one-to-many and many-to-many relationships.
106    */

107   public void generatePreCascade(JavaWriter out,
108                                  String JavaDoc aConn,
109                                  CascadeType cascadeType)
110     throws IOException JavaDoc
111   {
112     if (cascadeType != CascadeType.PERSIST)
113       return;
114
115     if (isCascade(cascadeType)) {
116
117       String JavaDoc getter = generateSuperGetter();
118
119       out.println("if (" + getter + " != null) {");
120       out.pushDepth();
121
122       out.println(aConn + ".persistNoChecks("+ getter + ");");
123
124       out.popDepth();
125       out.println("}");
126     }
127   }
128
129   /**
130    * Generates the (post) cascade operation from
131    * parent to this child. This field will be
132    * (post) cascaded if the operation on the
133    * parent is required to be performed first
134    * to avoid breaking FK constraints.
135    *
136    * Default is to post-cascade all operations,
137    * except the persist() operation.
138    *
139    * Check subclasses for one-to-one, many-to-one,
140    * one-to-many and many-to-many relationships.
141    */

142   public void generatePostCascade(JavaWriter out,
143                                   String JavaDoc aConn,
144                                   CascadeType cascadeType)
145     throws IOException JavaDoc
146   {
147     if (cascadeType == CascadeType.PERSIST)
148       return;
149
150     if (isCascade(cascadeType)) {
151
152       String JavaDoc getter = generateSuperGetter();
153
154       out.println("if (" + getter + " != null) {");
155       out.pushDepth();
156
157       out.print(aConn + ".");
158
159       switch (cascadeType) {
160       case MERGE:
161         out.print("merge");
162         break;
163
164       case REMOVE:
165         out.print("remove");
166         break;
167
168       case REFRESH:
169         out.print("refresh");
170         break;
171       }
172
173       out.println("("+ getter + ");");
174
175       out.popDepth();
176       out.println("}");
177     }
178   }
179
180   /**
181    * Returns true if the field is cascadable.
182    */

183   public boolean isCascadable()
184   {
185     return true;
186   }
187
188   /**
189    * Generates the flush check for this child.
190    * See DependentEntityOneToOneField.
191    */

192   public boolean generateFlushCheck(JavaWriter out)
193     throws IOException JavaDoc
194   {
195     return false;
196   }
197 }
198
Popular Tags