KickJava   Java API By Example, From Geeks To Geeks.

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


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.type.RelatedType;
35 import com.caucho.config.ConfigException;
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.sql.ResultSet JavaDoc;
43 import java.sql.SQLException JavaDoc;
44 import java.util.ArrayList JavaDoc;
45 import java.util.HashMap JavaDoc;
46 import java.util.HashSet JavaDoc;
47 import java.util.Map JavaDoc;
48 import java.util.logging.Logger JavaDoc;
49
50 /**
51  * Configuration for a bean's field
52  */

53 public class Id {
54   private static final L10N L = new L10N(Id.class);
55   protected static final Logger JavaDoc log = Log.open(Id.class);
56
57   private RelatedType _ownerType;
58
59   private ArrayList JavaDoc<IdField> _keys = new ArrayList JavaDoc<IdField>();
60   private ArrayList JavaDoc<Column> _columns = new ArrayList JavaDoc<Column>();
61
62   private EmbeddedIdField _embeddedIdField;
63
64   public Id(RelatedType ownerType, ArrayList JavaDoc<IdField> keys)
65   {
66     _ownerType = ownerType;
67
68     for (IdField key : keys) {
69       addKey(key);
70     }
71   }
72
73   public Id(RelatedType ownerType, IdField key)
74   {
75     _ownerType = ownerType;
76
77     if (key instanceof EmbeddedIdField) {
78       _embeddedIdField = (EmbeddedIdField) key;
79     }
80     else {
81       // ejb/0623
82
addKey(key);
83     }
84   }
85
86   /**
87    * Adds a new field to the id.
88    */

89   private void addKey(IdField key)
90   {
91     _keys.add(key);
92     // ejb/0a04
93
// Collections.sort(_keys, new AmberFieldCompare());
94

95     _columns.addAll(key.getColumns());
96     // Collections.sort(_columns, new ColumnCompare());
97

98     for (Column column : key.getColumns()) {
99       _ownerType.getTable().addIdColumn(column);
100     }
101   }
102
103   private static ArrayList JavaDoc<IdField> createField(IdField field)
104   {
105     ArrayList JavaDoc<IdField> fields = new ArrayList JavaDoc<IdField>();
106     fields.add(field);
107     return fields;
108   }
109
110   /**
111    * Returns the owner type.
112    */

113   public RelatedType getOwnerType()
114   {
115     return _ownerType;
116   }
117
118   /**
119    * Returns all the column.
120    */

121   public ArrayList JavaDoc<Column> getColumns()
122   {
123     return _columns;
124   }
125
126   /**
127    * Returns all the keys.
128    */

129   public ArrayList JavaDoc<IdField> getKeys()
130   {
131     return _keys;
132   }
133
134   /**
135    * Returns all the keys.
136    */

137   public int getKeyCount()
138   {
139     return _columns.size();
140   }
141
142   /**
143    * Returns the keys.
144    */

145   public IdField getKey()
146   {
147     return _keys.get(0);
148   }
149
150   /**
151    * Returns the foreign type.
152    */

153   public String JavaDoc getForeignTypeName()
154   {
155     return _keys.get(0).getForeignTypeName();
156   }
157
158   /**
159    * Initialize the id.
160    */

161   public void init()
162     throws ConfigException
163   {
164   }
165
166   /**
167    * Generates any prologue.
168    */

169   public void generatePrologue(JavaWriter out,
170                                HashSet JavaDoc<Object JavaDoc> completedSet,
171                                String JavaDoc name)
172     throws IOException JavaDoc
173   {
174   }
175
176   /**
177    * Generates any prologue.
178    */

179   public void generatePrologue(JavaWriter out, HashSet JavaDoc<Object JavaDoc> completedSet)
180     throws IOException JavaDoc
181   {
182     for (int i = 0; i < _keys.size(); i++)
183       _keys.get(i).generatePrologue(out, completedSet);
184   }
185
186   /**
187    * Returns the foreign type.
188    */

189   public int generateLoadForeign(JavaWriter out, String JavaDoc rs,
190                                  String JavaDoc indexVar, int index)
191     throws IOException JavaDoc
192   {
193     return generateLoadForeign(out, rs, indexVar, index,
194                                getForeignTypeName().replace('.', '_'));
195   }
196
197   /**
198    * Returns the foreign type.
199    */

200   public int generateLoadForeign(JavaWriter out, String JavaDoc rs,
201                                  String JavaDoc indexVar, int index,
202                                  String JavaDoc name)
203     throws IOException JavaDoc
204   {
205     if (_keys.size() > 1)
206       throw new UnsupportedOperationException JavaDoc();
207
208     return _keys.get(0).generateLoadForeign(out, rs, indexVar, index, name);
209   }
210
211   /**
212    * Generates the select clause.
213    */

214   public String JavaDoc generateSelect(String JavaDoc id)
215   {
216     return getKey().generateSelect(id);
217   }
218
219   /**
220    * Generates the JPA QL select clause.
221    */

222   public String JavaDoc generateJavaSelect(String JavaDoc id)
223   {
224     return getKey().generateJavaSelect(id);
225   }
226
227   /**
228    * Generates the select clause.
229    */

230   public String JavaDoc generateLoadSelect(String JavaDoc id)
231   {
232     return null;
233   }
234
235   /**
236    * Returns the key for the value
237    */

238   public String JavaDoc generateGetProperty(String JavaDoc value)
239   {
240     return getKey().generateGet(value);
241   }
242
243   /**
244    * Returns the key for the value
245    */

246   public String JavaDoc generateGetProxyKey(String JavaDoc value)
247   {
248     return "((" + getForeignTypeName() + ") " + value + ".getPrimaryKey())";
249   }
250
251   /**
252    * Generates loading cache
253    */

254   public void generateLoadFromObject(JavaWriter out, String JavaDoc obj)
255     throws IOException JavaDoc
256   {
257     ArrayList JavaDoc<IdField> keys = getKeys();
258
259     for (int i = 0; i < keys.size(); i++) {
260       keys.get(i).generateLoadFromObject(out, obj);
261     }
262   }
263
264   /**
265    * Generates loading cache
266    */

267   public void generateSet(JavaWriter out, String JavaDoc obj)
268     throws IOException JavaDoc
269   {
270     IdField key = getKey();
271
272     key.generateSet(out, key.toValue(obj));
273     // key.generateSet(out, key.getColumn().getType().generateCastFromObject(obj));
274
}
275
276   /**
277    * Generates loading cache
278    */

279   public void generateUpdateFromObject(JavaWriter out, String JavaDoc obj)
280     throws IOException JavaDoc
281   {
282     ArrayList JavaDoc<IdField> keys = getKeys();
283
284     for (int i = 0; i < keys.size(); i++) {
285       keys.get(i).generateUpdateFromObject(out, obj);
286     }
287   }
288
289   /**
290    * Generates the where clause.
291    */

292   public String JavaDoc generateMatchArgWhere(String JavaDoc id)
293   {
294     CharBuffer cb = new CharBuffer();
295
296     boolean isFirst = true;
297
298     if (_embeddedIdField == null) {
299       for (IdField field : getKeys()) {
300         for (Column column : field.getColumns()) {
301           if (! isFirst)
302             cb.append(" and ");
303           isFirst = false;
304
305           cb.append(column.generateMatchArgWhere(id));
306         }
307       }
308     } else {
309       HashMap JavaDoc<String JavaDoc, Column> columns
310         = _embeddedIdField.getEmbeddedColumns();
311
312       for (Map.Entry JavaDoc<String JavaDoc, Column> entry : columns.entrySet()) {
313         Column column = entry.getValue();
314         if (isFirst)
315           isFirst = false;
316         else
317           cb.append(" and ");
318         cb.append(column.generateMatchArgWhere(id));
319       }
320     }
321
322     return cb.toString();
323   }
324
325   /**
326    * Generates the where clause.
327    */

328   public String JavaDoc generateRawWhere(String JavaDoc id)
329   {
330     ArrayList JavaDoc<IdField> keys = getKeys();
331
332     CharBuffer cb = CharBuffer.allocate();
333
334     for (int i = 0; i < keys.size(); i++) {
335       if (i != 0)
336         cb.append(" and ");
337
338       cb.append(keys.get(i).generateRawWhere(id));
339     }
340
341     return cb.close();
342   }
343
344   /**
345    * Generates the where clause.
346    */

347   public String JavaDoc generateCreateTableSQL(AmberPersistenceUnit manager)
348   {
349     String JavaDoc sql = getKey().generateCreateTableSQL(manager);
350
351     if (getKey().getGenerator() != null)
352       return sql + " PRIMARY KEY auto_increment";
353     else
354       return sql + " PRIMARY KEY";
355   }
356
357   /**
358    * Generates the set clause.
359    */

360   public void generateSetKey(JavaWriter out, String JavaDoc pstmt,
361                              String JavaDoc index, String JavaDoc keyObject)
362     throws IOException JavaDoc
363   {
364     IdField key = getKey();
365
366     key.getType().generateSet(out, pstmt, index, keyObject);
367   }
368
369   /**
370    * Generates the set clause.
371    */

372   public void generateSet(JavaWriter out, String JavaDoc pstmt,
373                           String JavaDoc index, String JavaDoc value)
374     throws IOException JavaDoc
375   {
376     ArrayList JavaDoc<IdField> keys = getKeys();
377
378     for (int i = 0; i < keys.size(); i++) {
379       keys.get(i).generateSet(out, pstmt, index, value);
380     }
381   }
382
383   /**
384    * Generates the set clause.
385    */

386   public void generateSet(JavaWriter out, String JavaDoc pstmt, String JavaDoc index)
387     throws IOException JavaDoc
388   {
389     ArrayList JavaDoc<IdField> keys = getKeys();
390
391     for (int i = 0; i < keys.size(); i++) {
392       keys.get(i).generateSet(out, pstmt, index);
393     }
394   }
395
396   /**
397    * Generates the set clause.
398    */

399   /*
400     public String generateInsert()
401     {
402     String value = null;
403
404     ArrayList<IdField> keys = getKeys();
405
406     for (int i = 0; i < keys.size(); i++) {
407     String next = keys.get(i).generateInsert();
408
409     if (value == null)
410     value = next;
411     else if (next == null) {
412     }
413     else
414     value += ", " + next;
415     }
416
417     return value;
418     }
419   */

420
421   /**
422    * Generates the set clause.
423    */

424   public void generateSetInsert(JavaWriter out, String JavaDoc pstmt, String JavaDoc index)
425     throws IOException JavaDoc
426   {
427     ArrayList JavaDoc<IdField> keys = getKeys();
428
429     for (int i = 0; i < keys.size(); i++) {
430       keys.get(i).generateSetInsert(out, pstmt, index);
431     }
432   }
433
434   /**
435    * Generates code to convert to the type from the object.
436    */

437   public String JavaDoc generateCastFromObject(String JavaDoc value)
438   {
439     return value;
440   }
441
442   /**
443    * Generates code for a match.
444    */

445   public void generateMatch(JavaWriter out, String JavaDoc key)
446     throws IOException JavaDoc
447   {
448     IdField id = getKeys().get(0);
449
450     out.println("return (" + id.generateEquals(id.generateSuperGetter(),
451                                                id.toValue(key)) + ");");
452   }
453
454   /**
455    * Generates code to test the equals.
456    */

457   public String JavaDoc generateEquals(String JavaDoc leftBase, String JavaDoc value)
458   {
459     ArrayList JavaDoc<IdField> keys = getKeys();
460
461     String JavaDoc eq = "(";
462
463     for (int i = 0; i < keys.size(); i++) {
464       IdField key = keys.get(i);
465
466       if (i != 0)
467         eq += " && ";
468
469       eq += key.generateEquals(leftBase, value);
470     }
471
472     return eq + ")";
473   }
474
475   /**
476    * Generates the set clause.
477    */

478   public void generateCheckCreateKey(JavaWriter out)
479     throws IOException JavaDoc
480   {
481     ArrayList JavaDoc<IdField> keys = getKeys();
482
483     for (int i = 0; i < keys.size(); i++) {
484       keys.get(i).generateCheckCreateKey(out);
485     }
486   }
487
488   /**
489    * Generates the set clause.
490    */

491   public void generateSetGeneratedKeys(JavaWriter out, String JavaDoc pstmt)
492     throws IOException JavaDoc
493   {
494     ArrayList JavaDoc<IdField> keys = getKeys();
495
496     for (int i = 0; i < keys.size(); i++) {
497       keys.get(i).generateSetGeneratedKeys(out, pstmt);
498     }
499   }
500
501   /**
502    * Returns the embedded id field
503    */

504   public EmbeddedIdField getEmbeddedIdField()
505   {
506     return _embeddedIdField;
507   }
508
509   /**
510    * Returns true if this is an @EmbeddedId
511    */

512   public boolean isEmbeddedId()
513   {
514     return _embeddedIdField != null;
515   }
516
517   /**
518    * Generates code to convert to the object.
519    */

520   public String JavaDoc toObject(String JavaDoc value)
521   {
522     return getKey().toObject(value);
523   }
524
525   /**
526    * Generates code to convert to the object.
527    */

528   public Object JavaDoc toObjectKey(long value)
529   {
530     // return getColumn().toObjectKey(value);
531
return new Long JavaDoc(value);
532   }
533
534   /**
535    * Generates code to convert to the object.
536    */

537   public Object JavaDoc getObject(ResultSet JavaDoc rs, int index)
538     throws SQLException JavaDoc
539   {
540     return null;
541   }
542 }
543
Popular Tags