KickJava   Java API By Example, From Geeks To Geeks.

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


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.manager.AmberPersistenceUnit;
33 import com.caucho.amber.table.Column;
34 import com.caucho.amber.table.LinkColumns;
35 import com.caucho.amber.type.RelatedType;
36 import com.caucho.java.JavaWriter;
37 import com.caucho.log.Log;
38 import com.caucho.util.CharBuffer;
39 import com.caucho.util.L10N;
40
41 import java.io.IOException JavaDoc;
42 import java.util.ArrayList JavaDoc;
43 import java.util.HashSet JavaDoc;
44 import java.util.logging.Logger JavaDoc;
45
46 /**
47  * Configuration for a bean's field
48  */

49 public class SubId extends Id {
50   private static final L10N L = new L10N(SubId.class);
51   protected static final Logger JavaDoc log = Log.open(SubId.class);
52
53   private Id _parentId;
54   private LinkColumns _link;
55
56   public SubId(RelatedType ownerType, RelatedType rootType)
57   {
58     super(ownerType, new ArrayList JavaDoc<IdField>());
59
60     _parentId = rootType.getId();
61   }
62
63   /**
64    * Returns the parent keys.
65    */

66   public ArrayList JavaDoc<IdField> getParentKeys()
67   {
68     return _parentId.getKeys();
69   }
70
71   /**
72    * Returns the keys.
73    */

74   public ArrayList JavaDoc<IdField> getKeys()
75   {
76     return _parentId.getKeys();
77   }
78
79   /**
80    * Returns the foreign type.
81    */

82   public String JavaDoc getForeignTypeName()
83   {
84     return _parentId.getForeignTypeName();
85   }
86
87   /**
88    * Generates any prologue.
89    */

90   public void generatePrologue(JavaWriter out, HashSet JavaDoc<Object JavaDoc> completedSet)
91     throws IOException JavaDoc
92   {
93   }
94
95   /**
96    * Returns the foreign type.
97    */

98   public int generateLoadForeign(JavaWriter out, String JavaDoc rs,
99                                  String JavaDoc indexVar, int index)
100     throws IOException JavaDoc
101   {
102     return _parentId.generateLoadForeign(out, rs, indexVar, index);
103   }
104
105   /**
106    * Returns the foreign type.
107    */

108   public int generateLoadForeign(JavaWriter out, String JavaDoc rs,
109                                  String JavaDoc indexVar, int index,
110                                  String JavaDoc name)
111     throws IOException JavaDoc
112   {
113     return _parentId.generateLoadForeign(out, rs, indexVar, index, name);
114   }
115
116   /**
117    * Generates the select clause.
118    */

119   public String JavaDoc generateSelect(String JavaDoc id)
120   {
121     ArrayList JavaDoc<IdField> keys = getParentKeys();
122
123     CharBuffer cb = CharBuffer.allocate();
124
125     for (int i = 0; i < keys.size(); i++) {
126       if (i != 0)
127         cb.append(", ");
128
129       cb.append(keys.get(i).generateSelect(id));
130     }
131
132     return cb.close();
133   }
134
135   /**
136    * Generates the select clause.
137    */

138   public String JavaDoc generateLoadSelect(String JavaDoc id)
139   {
140     return null;
141   }
142
143   /**
144    * Returns the key for the value
145    */

146   public String JavaDoc generateGetProperty(String JavaDoc value)
147   {
148     return _parentId.generateGetProperty(value);
149   }
150
151   /**
152    * Generates loading cache
153    */

154   public void generateLoadFromObject(JavaWriter out, String JavaDoc obj)
155     throws IOException JavaDoc
156   {
157     _parentId.generateLoadFromObject(out, obj);
158   }
159
160   /**
161    * Generates loading cache
162    */

163   public void generateSet(JavaWriter out, String JavaDoc obj)
164     throws IOException JavaDoc
165   {
166     _parentId.generateSet(out, obj);
167   }
168
169   /**
170    * Generates loading cache
171    */

172   public void generateUpdateFromObject(JavaWriter out, String JavaDoc obj)
173     throws IOException JavaDoc
174   {
175     _parentId.generateUpdateFromObject(out, obj);
176   }
177
178   /**
179    * Generates the where clause.
180    */

181   public String JavaDoc generateMatchArgWhere(String JavaDoc id)
182   {
183     ArrayList JavaDoc<IdField> keys = getParentKeys();
184
185     CharBuffer cb = CharBuffer.allocate();
186
187     for (int i = 0; i < keys.size(); i++) {
188       if (i != 0)
189         cb.append(" and ");
190
191       generateMatchArgWhere(cb, keys.get(i), id);
192     }
193
194     return cb.close();
195   }
196
197
198   /**
199    * Generates the where clause.
200    */

201   private void generateMatchArgWhere(CharBuffer cb, IdField parentId, String JavaDoc id)
202   {
203     LinkColumns link = getOwnerType().getTable().getDependentIdLink();
204
205     ArrayList JavaDoc<Column> columns = parentId.getColumns();
206
207     for (int i = 0; i < columns.size(); i++) {
208       Column column = columns.get(i);
209
210       if (i != 0)
211         cb.append(" and ");
212
213       cb.append(id);
214       cb.append('.');
215       cb.append(link.getSourceColumn(column).getName());
216       cb.append("=?");
217     }
218   }
219
220   /**
221    * Generates the where clause.
222    */

223   public String JavaDoc generateCreateTableSQL(AmberPersistenceUnit manager)
224   {
225     return null;
226   }
227
228   /**
229    * Generates the set clause.
230    */

231   public void generateSetKey(JavaWriter out, String JavaDoc pstmt,
232                              String JavaDoc obj, String JavaDoc index)
233     throws IOException JavaDoc
234   {
235     _parentId.generateSetKey(out, pstmt, obj, index);
236   }
237
238   /**
239    * Generates the set clause.
240    */

241   public void generateSet(JavaWriter out, String JavaDoc pstmt,
242                           String JavaDoc obj, String JavaDoc index)
243     throws IOException JavaDoc
244   {
245     _parentId.generateSet(out, pstmt, obj, index);
246   }
247
248   /**
249    * Generates the set clause.
250    */

251   public void generateSet(JavaWriter out, String JavaDoc pstmt, String JavaDoc index)
252     throws IOException JavaDoc
253   {
254     _parentId.generateSet(out, pstmt, index);
255   }
256
257   /**
258    * Generates the set clause.
259    */

260   public void generateSetInsert(JavaWriter out, String JavaDoc pstmt, String JavaDoc index)
261     throws IOException JavaDoc
262   {
263     _parentId.generateSetInsert(out, pstmt, index);
264   }
265
266   /**
267    * Generates code to convert to the type from the object.
268    */

269   public String JavaDoc generateCastFromObject(String JavaDoc value)
270   {
271     return value;
272   }
273
274   /**
275    * Generates code for a match.
276    */

277   public void generateMatch(JavaWriter out, String JavaDoc key)
278     throws IOException JavaDoc
279   {
280     out.println("return " + generateEquals("super", key) + ";");
281   }
282
283   /**
284    * Generates code to test the equals.
285    */

286   public String JavaDoc generateEquals(String JavaDoc leftBase, String JavaDoc value)
287   {
288     return leftBase + ".equals(" + value + ")";
289   }
290
291   /**
292    * Generates the set clause.
293    */

294   public void generateCheckCreateKey(JavaWriter out)
295     throws IOException JavaDoc
296   {
297   }
298
299   /**
300    * Generates code to convert to the object.
301    */

302   public String JavaDoc toObject(String JavaDoc value)
303   {
304     return value;
305   }
306 }
307
Popular Tags