KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > ejb > gen > AmberQueryMethod


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  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.ejb.gen;
30
31 import com.caucho.bytecode.JClass;
32 import com.caucho.bytecode.JMethod;
33 import com.caucho.config.ConfigException;
34 import com.caucho.ejb.cfg.EjbEntityBean;
35 import com.caucho.ejb.ql.EjbSelectQuery;
36 import com.caucho.java.JavaWriter;
37 import com.caucho.util.L10N;
38
39 import java.io.IOException JavaDoc;
40 import java.util.Collection JavaDoc;
41
42 /**
43  * Generates the code for a query
44  */

45 public class AmberQueryMethod extends AbstractQueryMethod {
46   private static final L10N L = new L10N(AmberQueryMethod.class);
47
48   private EjbEntityBean _returnType;
49   private JMethod _method;
50   private String JavaDoc _contextClassName;
51   private String JavaDoc _prefix;
52   
53   public AmberQueryMethod(EjbEntityBean type,
54               JMethod method,
55               String JavaDoc contextClassName,
56               String JavaDoc prefix,
57               EjbSelectQuery query)
58     throws ConfigException
59   {
60     super(type, method, query);
61
62     _returnType = type;
63     _method = method;
64     _contextClassName = contextClassName;
65     _prefix = prefix;
66   }
67
68   /**
69    * Gets the parameter types
70    */

71   public JClass []getParameterTypes()
72   {
73     return _method.getParameterTypes();
74   }
75
76   /**
77    * Gets the return type.
78    */

79   public JClass getReturnType()
80   {
81     return _method.getReturnType();
82   }
83
84   /**
85    * Prints the create method
86    *
87    * @param method the create method
88    */

89   public void generateCall(JavaWriter out, String JavaDoc []args)
90     throws IOException JavaDoc
91   {
92     out.print("com.caucho.ejb.xa.TransactionContext trans");
93     out.println(" = _xaManager.beginSupports();");
94
95     out.println("try {");
96     out.pushDepth();
97
98     generatePrepareQuery(out, args);
99
100     out.println("com.caucho.amber.query.ResultSetImpl rs = (com.caucho.amber.query.ResultSetImpl) query.executeQuery();");
101     out.println("java.util.ArrayList list = new java.util.ArrayList();");
102
103     boolean isCollection =
104       _method.getReturnType().isAssignableTo(Collection JavaDoc.class);
105
106     if (isCollection) {
107       out.println("while (rs.next()) {");
108       out.pushDepth();
109
110       String JavaDoc beanClass = _returnType.getFullImplName();
111
112       out.println("com.caucho.ejb.entity.EntityObject item = (com.caucho.ejb.entity.EntityObject) rs.getObject(1);");
113       if ("RemoteHome".equals(_prefix))
114     out.println("list.add(item.getEJBObject());");
115       else if ("LocalHome".equals(_prefix))
116     out.println("list.add(item);");
117       else
118     out.println("list.add(item);");
119     
120       out.popDepth();
121       out.println("}");
122       out.println("rs.close();");
123       out.println();
124       out.println("return list;");
125     }
126     else {
127       out.println("if (rs.next()) {");
128       out.pushDepth();
129
130       String JavaDoc beanClass = _returnType.getFullImplName();
131
132       out.println("com.caucho.ejb.entity.EntityObject entity = (com.caucho.ejb.entity.EntityObject) rs.getObject(1);");
133       out.println("rs.close();");
134       out.println();
135       
136       String JavaDoc retType = getReturnType().getName();
137
138       if ("RemoteHome".equals(_prefix))
139     out.println("return (" + retType + ") entity.getEJBObject();");
140       else if ("LocalHome".equals(_prefix))
141     out.println("return (" + retType + ") entity;");
142       else
143     throw new IllegalStateException JavaDoc(L.l("'{0}' is an unknown type",
144                         _prefix));
145
146       /*
147       String retType = getReturnType().getName();
148
149       out.println(retType + " v = (" + retType + ") rs.getObject(1);");
150       out.println("rs.close();");
151
152       out.println("return v;");
153       
154       if (! "LocalHome".equals(_prefix))
155     throw new IllegalStateException(L.l("'{0}' is an unknown type",
156                         _prefix));
157       */

158     
159       out.popDepth();
160       out.println("}");
161       out.println();
162       out.println("throw new ObjectNotFoundException(\"no matching object found\");");
163     }
164     
165     /*
166     Class retType = getReturnType();
167
168     if ("RemoteHome".equals(_prefix))
169       out.println("return (" + retType.getName() + ") _server.getContext(" + args[0] + ", true).getEJBObject();");
170     else if ("LocalHome".equals(_prefix))
171       out.println("return (" + retType.getName() + ") _server.getContext(" + args[0] + ", true).getEJBLocalObject();");
172     else
173       throw new IllegalStateException(L.l("'{0}' is an unknown type",
174                       _prefix));
175     */

176
177     out.popDepth();
178     out.println("} catch (java.sql.SQLException e) {");
179     out.println(" throw new com.caucho.ejb.FinderExceptionWrapper(e);");
180     out.println("} finally {");
181     out.println(" trans.commit();");
182     out.println("}");
183   }
184 }
185
Popular Tags