KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > ejb > cfg > EjbOneToManyMethod


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.ejb.cfg;
31
32 import com.caucho.amber.field.IdField;
33 import com.caucho.amber.type.EntityType;
34 import com.caucho.bytecode.JMethod;
35 import com.caucho.config.ConfigException;
36 import com.caucho.ejb.gen.AbstractQueryMethod;
37 import com.caucho.ejb.gen.BeanAssembler;
38 import com.caucho.ejb.gen.CollectionClass;
39 import com.caucho.java.JavaWriter;
40 import com.caucho.java.gen.BaseMethod;
41 import com.caucho.util.L10N;
42
43 import java.io.IOException JavaDoc;
44 import java.util.ArrayList JavaDoc;
45
46 /**
47  * Configuration for a one-to-many CMP method.
48  */

49 public class EjbOneToManyMethod extends CmpGetter {
50   private static final L10N L = new L10N(EjbOneToManyMethod.class);
51
52   private CmrOneToMany _oneToMany;
53
54   /**
55    * Creates a new method.
56    *
57    * @param view the owning view
58    * @param apiMethod the method from the view
59    * @param implMethod the method from the implementation
60    */

61   public EjbOneToManyMethod(EjbView view,
62                             JMethod apiMethod, JMethod implMethod,
63                             CmrOneToMany oneToMany)
64   {
65     super(view, apiMethod, implMethod);
66
67     _oneToMany = oneToMany;
68   }
69
70   /**
71    * Assembles the bean method.
72    */

73   public void assembleBean(BeanAssembler beanAssembler, String JavaDoc fullClassName)
74     throws ConfigException
75   {
76     String JavaDoc listName = _oneToMany.getName() + "_List";
77
78     CollectionClass list = new CollectionClass(_oneToMany, listName);
79
80     beanAssembler.addComponent(list);
81
82     beanAssembler.addMethod(new BeanMethod(getImplMethod()));
83   }
84
85   class BeanMethod extends BaseMethod {
86     BeanMethod(JMethod method)
87     {
88       super(method);
89     }
90
91     /**
92      * Generates the code for the call.
93      *
94      * @param out the writer to the output stream.
95      */

96     public void generate(JavaWriter out)
97       throws IOException JavaDoc
98     {
99       String JavaDoc listName = _oneToMany.getName() + "_List";
100       String JavaDoc varName = "__caucho_field_" + _oneToMany.getName();
101
102       out.println(listName + " " + varName + ";");
103       out.println();
104
105       super.generate(out);
106     }
107
108     /**
109      * Generates the code for the call.
110      *
111      * @param out the writer to the output stream.
112      * @param args the arguments
113      */

114     protected void generateCall(JavaWriter out, String JavaDoc []args)
115       throws IOException JavaDoc
116     {
117       String JavaDoc listName = _oneToMany.getName() + "_List";
118       String JavaDoc varName = "__caucho_field_" + _oneToMany.getName();
119
120       out.println("if (" + varName + " != null) {");
121       out.println(" " + varName + ".__caucho_init(_ejb_trans.getAmberConnection());");
122       out.println(" return " + varName + ";");
123       out.println("}");
124
125       out.println();
126       out.println("try {");
127       out.pushDepth();
128
129       out.println("com.caucho.amber.AmberQuery query;");
130
131       String JavaDoc abstractSchema = _oneToMany.getBean().getAbstractSchemaName();
132
133       out.print("String sql = \"SELECT o." + _oneToMany.getName());
134       out.print(" FROM " + abstractSchema + " o");
135       out.print(" WHERE ");
136
137       EntityType type = _oneToMany.getBean().getEntityType();
138       ArrayList JavaDoc<IdField> keys = type.getId().getKeys();
139
140       for (int i = 0; i < keys.size(); i++) {
141         IdField key = keys.get(i);
142
143         if (i != 0)
144           out.print(" AND ");
145
146         out.print("o." + key.getName() + "=?" + (i + 1));
147       }
148
149       out.println("\";");
150
151       out.println("query = _ejb_trans.getAmberConnection().prepareQuery(sql);");
152
153       EjbConfig config = _oneToMany.getBean().getConfig();
154
155       out.println("int index = 1;");
156       for (int i = 0; i < keys.size(); i++) {
157         IdField key = keys.get(i);
158
159         AbstractQueryMethod.generateSetParameter(out,
160                                                  config,
161                                                  key.getJavaType(),
162                                                  "query",
163                                                  key.generateGet("super"));
164       }
165
166       out.println(varName + " = new " + listName + "(this, query);");
167
168       out.println("return " + varName + ";");
169
170       out.popDepth();
171       out.println("} catch (RuntimeException e) {");
172       out.println(" throw e;");
173       out.println("} catch (java.sql.SQLException e) {");
174       out.println(" throw com.caucho.ejb.EJBExceptionWrapper.createRuntime(e);");
175       out.println("}");
176     }
177   }
178 }
179
Popular Tags