KickJava   Java API By Example, From Geeks To Geeks.

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


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.AbstractStatefulType;
35 import com.caucho.amber.type.EmbeddableType;
36 import com.caucho.amber.type.PrimitiveType;
37 import com.caucho.amber.type.RelatedType;
38 import com.caucho.amber.type.Type;
39 import com.caucho.bytecode.JClass;
40 import com.caucho.bytecode.JMethod;
41 import com.caucho.java.JavaWriter;
42 import com.caucho.log.Log;
43 import com.caucho.util.CharBuffer;
44 import com.caucho.util.L10N;
45
46 import java.io.IOException JavaDoc;
47 import java.util.ArrayList JavaDoc;
48 import java.util.HashSet JavaDoc;
49 import java.util.logging.Logger JavaDoc;
50
51
52 /**
53  * Configuration for a bean's field
54  */

55 public class CompositeId extends Id {
56   private static final L10N L = new L10N(CompositeId.class);
57   protected static final Logger JavaDoc log = Log.open(CompositeId.class);
58
59   private JClass _keyClass;
60
61   public CompositeId(RelatedType ownerType,
62                      ArrayList JavaDoc<IdField> keys)
63   {
64     super(ownerType, keys);
65   }
66
67   public CompositeId(RelatedType ownerType,
68                      EmbeddedIdField embeddedId)
69   {
70     super(ownerType, embeddedId);
71   }
72
73   /**
74    * Sets the foreign key type.
75    */

76   public void setKeyClass(JClass keyClass)
77   {
78     _keyClass = keyClass;
79
80     getOwnerType().addDependency(keyClass);
81   }
82
83   /**
84    * Returns the foreign type.
85    */

86   public String JavaDoc getForeignTypeName()
87   {
88     if (_keyClass != null)
89       return _keyClass.getName();
90     else if (isEmbeddedId())
91       return getEmbeddedIdField().getJavaTypeName();
92     else
93       return getOwnerType().getName();
94   }
95
96   /**
97    * Returns the foreign type.
98    */

99   public String JavaDoc getForeignMakeKeyName()
100   {
101     return getOwnerType().getName().replace('.', '_').replace('/', '_');
102   }
103
104   /**
105    * Generates any prologue.
106    */

107   public void generatePrologue(JavaWriter out, HashSet JavaDoc<Object JavaDoc> completedSet)
108     throws IOException JavaDoc
109   {
110     super.generatePrologue(out, completedSet);
111
112     generatePrologue(out, completedSet, getForeignMakeKeyName());
113   }
114
115   /**
116    * Generates any prologue.
117    */

118   public void generatePrologue(JavaWriter out,
119                                HashSet JavaDoc<Object JavaDoc> completedSet,
120                                String JavaDoc name)
121     throws IOException JavaDoc
122   {
123     generatePrologueMake(out, completedSet);
124     generatePrologueLoad(out, completedSet);
125   }
126
127   /**
128    * Generates any prologue.
129    */

130   public void generatePrologueMake(JavaWriter out,
131                                    HashSet JavaDoc<Object JavaDoc> completedSet)
132     throws IOException JavaDoc
133   {
134     String JavaDoc makeName = "__caucho_make_key_" + getForeignMakeKeyName();
135
136     if (completedSet.contains(makeName))
137       return;
138
139     completedSet.add(makeName);
140
141     out.println();
142     out.print("private static ");
143     out.print(getForeignTypeName() + " " + makeName);
144     out.print("(");
145
146     if (! isEmbeddedId()) {
147       ArrayList JavaDoc<IdField> keys = getKeys();
148       for (int i = 0; i < keys.size(); i++) {
149         if (i != 0)
150           out.print(", ");
151
152         IdField key = keys.get(i);
153
154         out.print(key.getJavaTypeName() + " a" + i);
155       }
156     }
157     else {
158       EmbeddableType embeddable = (EmbeddableType) getEmbeddedIdField().getType();
159
160       ArrayList JavaDoc<AmberField> fields = embeddable.getFields();
161       for (int i = 0; i < fields.size(); i++) {
162         if (i != 0)
163           out.print(", ");
164
165         AmberField field = fields.get(i);
166
167         out.print(field.getJavaTypeName() + " a" + i);
168       }
169     }
170
171     out.println(")");
172     out.println("{");
173     out.pushDepth();
174
175     out.println();
176     out.println(getForeignTypeName() + " key = new " + getForeignTypeName() + "();");
177
178     if (! isEmbeddedId()) {
179       String JavaDoc args = "";
180
181       ArrayList JavaDoc<IdField> keys = getKeys();
182       for (int i = 0; i < keys.size(); i++) {
183         KeyPropertyField key = (KeyPropertyField) keys.get(i);
184
185         String JavaDoc name = key.getName();
186
187         char ch = Character.toUpperCase(name.charAt(0));
188         if (name.length() == 1)
189           name = "get" + ch;
190         else
191           name = "get" + ch + key.getName().substring(1);
192
193         JMethod method = AbstractStatefulType.getGetter(_keyClass, name);
194
195         if (key.isKeyField() || (method != null)) {
196           out.println(key.generateSetKeyProperty("key", "a" + i) + ";");
197         }
198         else {
199           // Arg. constructor jpa/0u21
200
if (i != 0)
201             args += ", ";
202
203           args += " a" + i;
204
205           out.println("if (a" + i + " == null)");
206           out.println(" return new " + getForeignTypeName() + "();");
207
208           if (i + 1 == keys.size())
209             out.print("key = new " + getForeignTypeName() + "(" + args + ");");
210         }
211       }
212
213     }
214     else {
215       EmbeddableType embeddable = (EmbeddableType) getEmbeddedIdField().getType();
216
217       ArrayList JavaDoc<AmberField> fields = embeddable.getFields();
218       for (int i = 0; i < fields.size(); i++) {
219         AmberField field = fields.get(i);
220
221         if (getOwnerType().isFieldAccess())
222           out.println(field.generateSet("key", "a" + i) + ";");
223         else {
224           String JavaDoc setter = field.getName();
225
226           setter = "set" + Character.toUpperCase(setter.charAt(0)) +
227             (setter.length() == 1 ? "" : setter.substring(1));
228
229           out.println("key." + setter + "(a" + i + ");");
230         }
231       }
232     }
233
234     out.println("return key;");
235
236     out.popDepth();
237     out.println("}");
238   }
239
240   /**
241    * Generates any prologue.
242    */

243   public void generatePrologueLoad(JavaWriter out,
244                                    HashSet JavaDoc<Object JavaDoc> completedSet)
245     throws IOException JavaDoc
246   {
247     String JavaDoc loadName = "__caucho_load_key_" + getForeignMakeKeyName();
248
249     if (completedSet.contains(loadName))
250       return;
251
252     completedSet.add(loadName);
253
254     out.println();
255     out.print("private static ");
256     out.print(getForeignTypeName() + " " + loadName);
257     out.println("(com.caucho.amber.manager.AmberConnection aConn, java.sql.ResultSet rs, int index)");
258     out.println(" throws java.sql.SQLException");
259
260     out.println("{");
261     out.pushDepth();
262
263     int index = 0;
264     ArrayList JavaDoc<IdField> keys = getKeys();
265     for (int i = 0; i < keys.size(); i++) {
266       IdField key = keys.get(i);
267
268       String JavaDoc javaType = key.getJavaTypeName();
269       out.print(javaType + " a" + i + " = (" + javaType + ") ");
270       index = key.getType().generateLoad(out, "rs", "index", index);
271       out.println(";");
272
273       out.println("if (rs.wasNull())");
274       out.println(" return null;");
275     }
276
277     out.print(getForeignTypeName() + " key = new " + getForeignTypeName() + "(");
278
279     if (isEmbeddedId()) {
280       out.println(");");
281
282       for (int i = 0; i < keys.size(); i++) {
283         out.println(keys.get(i).generateSetKeyProperty("key", "a" + i) + ";");
284       }
285     }
286     else {
287       for (int i = 0; i < keys.size(); i++) {
288         KeyPropertyField key = (KeyPropertyField) keys.get(i);
289
290         String JavaDoc name = key.getName();
291
292         char ch = Character.toUpperCase(name.charAt(0));
293         if (name.length() == 1)
294           name = "get" + ch;
295         else
296           name = "get" + ch + key.getName().substring(1);
297
298         JMethod method = AbstractStatefulType.getGetter(_keyClass, name);
299
300         if (key.isKeyField() || (method != null)) {
301           if (i == 0)
302             out.println(");");
303
304           out.println(key.generateSetKeyProperty("key", "a" + i) + ";");
305         }
306         else {
307           // Arg. constructor jpa/0u21
308
if (i != 0)
309             out.print(", ");
310
311           out.print(" a" + i);
312
313           if (i + 1 == keys.size())
314             out.println(");");
315         }
316       }
317     }
318
319     out.println("return key;");
320
321     out.popDepth();
322     out.println("}");
323   }
324
325   /**
326    * Returns the foreign type.
327    */

328   public int generateLoadForeign(JavaWriter out, String JavaDoc rs,
329                                  String JavaDoc indexVar, int index)
330     throws IOException JavaDoc
331   {
332     return generateLoadForeign(out, rs, indexVar, index,
333                                getForeignTypeName().replace('.', '_'));
334   }
335
336   /**
337    * Returns the foreign type.
338    */

339   public int generateLoadForeign(JavaWriter out, String JavaDoc rs,
340                                  String JavaDoc indexVar, int index,
341                                  String JavaDoc name)
342     throws IOException JavaDoc
343   {
344     out.print("__caucho_load_key_" + getForeignMakeKeyName());
345     out.print("(aConn, " + rs + ", " + indexVar + " + " + index + ")");
346
347     ArrayList JavaDoc<IdField> keys = getKeys();
348
349     index += keys.size();
350
351     return index;
352   }
353
354   /**
355    * Generates the select clause.
356    */

357   public String JavaDoc generateSelect(String JavaDoc id)
358   {
359     ArrayList JavaDoc<IdField> keys = getKeys();
360
361     CharBuffer cb = CharBuffer.allocate();
362
363     for (int i = 0; i < keys.size(); i++) {
364       if (i != 0)
365         cb.append(", ");
366
367       cb.append(keys.get(i).generateSelect(id));
368     }
369
370     return cb.close();
371   }
372
373   /**
374    * Generates the JPA QL select clause.
375    */

376   public String JavaDoc generateJavaSelect(String JavaDoc id)
377   {
378     ArrayList JavaDoc<IdField> keys = getKeys();
379
380     CharBuffer cb = CharBuffer.allocate();
381
382     for (int i = 0; i < keys.size(); i++) {
383       if (i != 0)
384         cb.append(", ");
385
386       cb.append(keys.get(i).generateJavaSelect(id));
387     }
388
389     return cb.close();
390   }
391
392   /**
393    * Generates the select clause.
394    */

395   public String JavaDoc generateLoadSelect(String JavaDoc id)
396   {
397     return null;
398   }
399
400   /**
401    * Returns the key for the value
402    */

403   public String JavaDoc generateGetProperty(String JavaDoc value)
404   {
405     CharBuffer cb = CharBuffer.allocate();
406
407     cb.append("__caucho_make_key_" + getForeignMakeKeyName());
408     cb.append("(");
409
410     ArrayList JavaDoc<IdField> keys = getKeys();
411
412     if (! isEmbeddedId()) {
413       for (int i = 0; i < keys.size(); i++) {
414         if (i != 0)
415           cb.append(", ");
416
417         cb.append(keys.get(i).generateGet(value));
418       }
419     }
420     else {
421       getEmbeddedIdField().generateGetPrimaryKey(cb);
422     }
423
424     cb.append(")");
425
426     return cb.close();
427   }
428
429   /**
430    * Returns the key for the value
431    */

432   public String JavaDoc generateGetProxyProperty(String JavaDoc value)
433   {
434     CharBuffer cb = CharBuffer.allocate();
435
436     cb.append("__caucho_make_key_" + getForeignMakeKeyName());
437     cb.append("(");
438
439     ArrayList JavaDoc<IdField> keys = getKeys();
440
441     for (int i = 0; i < keys.size(); i++) {
442       if (i != 0)
443         cb.append(", ");
444
445       cb.append(keys.get(i).generateGetProxyProperty(value));
446     }
447
448     cb.append(")");
449
450     return cb.close();
451   }
452
453   /**
454    * Generates loading cache
455    */

456   public void generateLoadFromObject(JavaWriter out, String JavaDoc obj)
457     throws IOException JavaDoc
458   {
459     ArrayList JavaDoc<IdField> keys = getKeys();
460
461     for (int i = 0; i < keys.size(); i++) {
462       keys.get(i).generateLoadFromObject(out, obj);
463     }
464   }
465
466   /**
467    * Generates loading cache
468    */

469   public void generateSet(JavaWriter out, String JavaDoc obj)
470     throws IOException JavaDoc
471   {
472     out.println("if (" + obj + " != null) {");
473     out.pushDepth();
474
475     if (! isEmbeddedId()) {
476
477       // jpa/0u21
478

479       AmberPersistenceUnit persistenceUnit
480         = getOwnerType().getPersistenceUnit();
481
482       EmbeddableType embeddable
483         = persistenceUnit.getEmbeddable(_keyClass.getName());
484
485       // jpa/0u21 ArrayList<IdField> keys = getKeys();
486
ArrayList JavaDoc<AmberField> keys = embeddable.getFields();
487
488       for (int i = 0; i < keys.size(); i++) {
489         PropertyField key = (PropertyField) keys.get(i);
490
491         String JavaDoc getter = "__caucho_get_field(" + i + ")";
492
493         String JavaDoc value
494           = "((com.caucho.amber.entity.Embeddable) key)." + getter;
495
496         out.println("Object field" + i + " = " + value + ";");
497
498         out.println("if (field" + i + " == null)");
499         out.println(" return;");
500
501         KeyPropertyField prop = null;
502
503         Column column = key.getColumn();
504
505         if (column == null) {
506           ArrayList JavaDoc<IdField> fields = getKeys();
507           for (int j = 0; j < fields.size(); j++) {
508             IdField id = fields.get(j);
509             if (id.getName().equals(key.getName()))
510               if (id instanceof KeyPropertyField)
511                 prop = (KeyPropertyField) id;
512           }
513         }
514
515         if (prop != null)
516           key = prop;
517
518         Type columnType = key.getColumn().getType();
519
520         value = columnType.generateCastFromObject("field" + i);
521
522         key.generateSet(out, value);
523       }
524
525       // jpa/0u21
526
out.println("__caucho_compound_key = (" + getForeignTypeName() + ") " + obj + ";");
527
528       /*
529       for (int i = 0; i < keys.size(); i++) {
530         IdField key = keys.get(i);
531
532         key.generateSet(out, key.generateGetKeyProperty(obj + "_key"));
533       }
534       */

535     }
536     else {
537       out.println(getForeignTypeName() + " " + obj + "_key = (" + getForeignTypeName() + ") " + obj + ";");
538
539       getEmbeddedIdField().generateSet(out, obj+"_key");
540     }
541
542     out.popDepth();
543     out.println("}");
544   }
545
546   /**
547    * Generates loading cache
548    */

549   public void generateUpdateFromObject(JavaWriter out, String JavaDoc obj)
550     throws IOException JavaDoc
551   {
552     ArrayList JavaDoc<IdField> keys = getKeys();
553
554     for (int i = 0; i < keys.size(); i++) {
555       keys.get(i).generateUpdateFromObject(out, obj);
556     }
557   }
558
559   /**
560    * Generates the where clause.
561    */

562   public String JavaDoc generateWhere(String JavaDoc id)
563   {
564     ArrayList JavaDoc<IdField> keys = getKeys();
565
566     CharBuffer cb = CharBuffer.allocate();
567
568     for (int i = 0; i < keys.size(); i++) {
569       if (i != 0)
570         cb.append(" and ");
571
572       cb.append(keys.get(i).generateWhere(id));
573     }
574
575     return cb.close();
576   }
577
578   /**
579    * Generates the where clause.
580    */

581   public String JavaDoc generateCreateTableSQL(AmberPersistenceUnit manager)
582   {
583     return null;
584   }
585
586   /**
587    * Generates the set clause.
588    */

589   public void generateSetKey(JavaWriter out, String JavaDoc pstmt,
590                              String JavaDoc obj, String JavaDoc index)
591     throws IOException JavaDoc
592   {
593     generateSet(out, pstmt, obj, index);
594   }
595
596   /**
597    * Generates the set clause.
598    */

599   public void generateSet(JavaWriter out, String JavaDoc pstmt,
600                           String JavaDoc obj, String JavaDoc index)
601     throws IOException JavaDoc
602   {
603     if (! isEmbeddedId()) {
604
605       // XXX: jpa/0u21
606

607       ArrayList JavaDoc keys = getKeys();
608
609       for (int i = 0; i < keys.size(); i++) {
610         PropertyField key = (PropertyField) keys.get(i);
611
612         String JavaDoc name = key.getName();
613
614         char ch = Character.toUpperCase(name.charAt(0));
615         if (name.length() == 1)
616           name = "get" + ch;
617         else
618           name = "get" + ch + key.getName().substring(1);
619
620         JMethod method = AbstractStatefulType.getGetter(_keyClass, name);
621
622         if ((key instanceof KeyPropertyField) &&
623             (((KeyPropertyField) key).isKeyField() || (method != null))) {
624           key.generateSet(out, pstmt, obj, index);
625         }
626         else {
627           // XXX: jpa/0u21
628

629           String JavaDoc getter = null;
630
631           AmberPersistenceUnit persistenceUnit
632             = getOwnerType().getPersistenceUnit();
633
634           EmbeddableType embeddable
635             = persistenceUnit.getEmbeddable(_keyClass.getName());
636
637           if (embeddable.isIdClass()) {
638             ArrayList JavaDoc<AmberField> fields = embeddable.getFields();
639             for (int j = 0; j < fields.size(); j++) {
640               AmberField id = fields.get(j);
641               if (id.getName().equals(key.getName()))
642                 if (id instanceof PropertyField)
643                   getter = "__caucho_get_field(" + j + ")";
644             }
645           }
646           else
647             getter = key.getGetterMethod().getName() + "()";
648
649           Column column = key.getColumn();
650
651           Type columnType = column.getType();
652
653           String JavaDoc value
654             = "((com.caucho.amber.entity.Embeddable) __caucho_compound_key)." + getter;
655
656           value = columnType.generateCastFromObject(value);
657
658           columnType.generateSet(out, pstmt, obj, value);
659         }
660       }
661     }
662     else {
663       getEmbeddedIdField().generateSet(out, pstmt, obj, index);
664     }
665   }
666
667   /**
668    * Generates the set clause.
669    */

670   public void generateSet(JavaWriter out, String JavaDoc pstmt, String JavaDoc index)
671     throws IOException JavaDoc
672   {
673     ArrayList JavaDoc<IdField> keys = getKeys();
674
675     for (int i = 0; i < keys.size(); i++) {
676       keys.get(i).generateSet(out, pstmt, index);
677     }
678   }
679
680   /**
681    * Generates the set clause.
682    */

683   public void generateSetInsert(JavaWriter out, String JavaDoc pstmt, String JavaDoc index)
684     throws IOException JavaDoc
685   {
686     ArrayList JavaDoc<IdField> keys = getKeys();
687
688     for (int i = 0; i < keys.size(); i++) {
689       keys.get(i).generateSetInsert(out, pstmt, index);
690     }
691   }
692
693   /**
694    * Generates code to convert to the type from the object.
695    */

696   public String JavaDoc generateCastFromObject(String JavaDoc value)
697   {
698     return value;
699   }
700
701   /**
702    * Generates code for a match.
703    */

704   public void generateMatch(JavaWriter out, String JavaDoc key)
705     throws IOException JavaDoc
706   {
707     out.println("return __caucho_getPrimaryKey().equals(" + key + ");");
708   }
709
710   /**
711    * Generates code to test the equals.
712    */

713   public String JavaDoc generateEquals(String JavaDoc leftBase, String JavaDoc value)
714   {
715     return leftBase + ".equals(" + value + ")";
716   }
717   /**
718    * Generates the set clause.
719    */

720   public void generateCheckCreateKey(JavaWriter out)
721     throws IOException JavaDoc
722   {
723   }
724
725   /**
726    * Generates the set clause.
727    */

728   /*
729     public void generateSetGeneratedKeys(JavaWriter out, String pstmt)
730     throws IOException
731     {
732     }
733   */

734
735   /**
736    * Generates code to convert to the object.
737    */

738   public String JavaDoc toObject(String JavaDoc value)
739   {
740     return value;
741   }
742 }
743
Popular Tags