KickJava   Java API By Example, From Geeks To Geeks.

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


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 Scott Ferguson
28  */

29
30 package com.caucho.amber.field;
31
32 import com.caucho.amber.expr.AmberExpr;
33 import com.caucho.amber.expr.PathExpr;
34 import com.caucho.amber.query.QueryParser;
35 import com.caucho.amber.table.Column;
36 import com.caucho.amber.table.ForeignColumn;
37 import com.caucho.amber.table.LinkColumns;
38 import com.caucho.amber.type.RelatedType;
39 import com.caucho.amber.type.Type;
40 import com.caucho.config.ConfigException;
41 import com.caucho.java.JavaWriter;
42 import com.caucho.log.Log;
43 import com.caucho.util.L10N;
44
45 import javax.persistence.CascadeType;
46 import java.io.IOException JavaDoc;
47 import java.util.logging.Logger JavaDoc;
48
49 /**
50  * Configuration for a bean's field
51  */

52 public class CollectionField extends CascadableField {
53   private static final L10N L = new L10N(CollectionField.class);
54   protected static final Logger JavaDoc log = Log.open(CollectionField.class);
55
56   private Type _targetType;
57
58   private LinkColumns _linkColumns;
59
60   private String JavaDoc _table;
61
62   public CollectionField(RelatedType relatedType,
63                          String JavaDoc name,
64                          CascadeType[] cascadeTypes)
65     throws ConfigException
66   {
67     super(relatedType, name, cascadeTypes);
68   }
69
70   public CollectionField(RelatedType relatedType)
71   {
72     super(relatedType);
73   }
74
75   /**
76    * Sets the collection table.
77    */

78   public void setTable(String JavaDoc table)
79   {
80     _table = table;
81   }
82
83   /**
84    * Gets the collection table.
85    */

86   public String JavaDoc getTableName()
87   {
88     return _table;
89   }
90
91   /**
92    * Sets the target type.
93    */

94   public void setType(Type targetType)
95   {
96     _targetType = targetType;
97   }
98
99   /**
100    * Returns the target type.
101    */

102   public Type getTargetType()
103   {
104     return _targetType;
105   }
106
107   /**
108    * Sets the key columns.
109    */

110   public void setLinkColumns(LinkColumns linkColumns)
111   {
112     _linkColumns = linkColumns;
113   }
114
115   /**
116    * Gets the key columns.
117    */

118   public LinkColumns getLinkColumns()
119   {
120     return _linkColumns;
121   }
122
123   /**
124    * Generates the (pre) cascade operation from
125    * parent to this child. This field will only
126    * be cascaded first if the operation can be
127    * performed with no risk to break FK constraints.
128    */

129   public void generatePreCascade(JavaWriter out,
130                                  String JavaDoc aConn,
131                                  CascadeType cascadeType)
132     throws IOException JavaDoc
133   {
134     if (isCascade(cascadeType)) {
135
136       String JavaDoc getter = "_caucho_field_" + getGetterName(); // generateSuperGetter();
137

138       out.println("if (" + getter + " != null) {");
139       out.pushDepth();
140
141       out.println("for (Object o : " + getter + ")");
142       out.pushDepth();
143
144       out.print(aConn + ".");
145
146       switch (cascadeType) {
147       case PERSIST:
148         out.print("persistNoChecks");
149         break;
150
151       case MERGE:
152         out.print("merge");
153         break;
154
155       case REMOVE:
156         out.print("remove");
157         break;
158
159       case REFRESH:
160         out.print("refresh");
161         break;
162       }
163
164       out.println("(o);");
165
166       out.popDepth();
167
168       out.popDepth();
169       out.println("}");
170     }
171   }
172
173   /**
174    * Generates the (post) cascade operation from
175    * parent to this child. This field will only
176    * be cascaded first if the operation can be
177    * performed with no risk to break FK constraints.
178    */

179   public void generatePostCascade(JavaWriter out,
180                                   String JavaDoc aConn,
181                                   CascadeType cascadeType)
182     throws IOException JavaDoc
183   {
184   }
185
186   /**
187    * Generates the set clause.
188    */

189   public void generateSet(JavaWriter out, String JavaDoc pstmt,
190                           String JavaDoc obj, String JavaDoc index)
191     throws IOException JavaDoc
192   {
193   }
194
195   /**
196    * Generates loading cache
197    */

198   public void generateUpdate(JavaWriter out, String JavaDoc mask, String JavaDoc pstmt,
199                              String JavaDoc index)
200     throws IOException JavaDoc
201   {
202     String JavaDoc maskVar = mask + "_" + (getIndex() / 64);
203     long maskValue = (1L << (getIndex() % 64));
204
205     out.println();
206     out.println("if ((" + maskVar + " & " + maskValue + "L) != 0) {");
207     out.pushDepth();
208
209     generateSet(out, pstmt, index);
210
211     out.popDepth();
212     out.println("}");
213   }
214
215   /**
216    * Updates the cached copy.
217    */

218   public void generateCopyUpdateObject(JavaWriter out,
219                                        String JavaDoc dst, String JavaDoc src,
220                                        int updateIndex)
221     throws IOException JavaDoc
222   {
223   }
224
225   /**
226    * Generates the select clause.
227    */

228   public String JavaDoc generateLoadSelect(String JavaDoc id)
229   {
230     return null;
231   }
232
233   /**
234    * Generates the target select.
235    */

236   public String JavaDoc generateTargetSelect(String JavaDoc id)
237   {
238     throw new UnsupportedOperationException JavaDoc(getClass().getName());
239   }
240
241   /**
242    * Creates the expression for the field.
243    */

244   public AmberExpr createExpr(QueryParser parser, PathExpr parent)
245   {
246     throw new UnsupportedOperationException JavaDoc(getClass().getName());
247   }
248
249   /**
250    * Generates the linking for a join
251    */

252   public String JavaDoc generateJoin(String JavaDoc sourceTable, String JavaDoc targetTable)
253   {
254     return _linkColumns.generateJoin(sourceTable, targetTable);
255   }
256
257   /**
258    * Returns the source column for a given target key.
259    */

260   public ForeignColumn getSourceColumn(Column key)
261   {
262     return _linkColumns.getSourceColumn(key);
263   }
264 }
265
Popular Tags