KickJava   Java API By Example, From Geeks To Geeks.

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


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.gen;
31
32 import com.caucho.amber.type.Type;
33 import com.caucho.bytecode.JClass;
34 import com.caucho.bytecode.JMethod;
35 import com.caucho.config.ConfigException;
36 import com.caucho.ejb.cfg.EjbEntityBean;
37 import com.caucho.ejb.ql.EjbSelectQuery;
38 import com.caucho.java.JavaWriter;
39 import com.caucho.util.L10N;
40
41 import java.io.IOException JavaDoc;
42
43 /**
44  * Generates the code for a query
45  */

46 public class AmberSelectMethod extends AbstractQueryMethod {
47   private static L10N L = new L10N(AmberSelectMethod.class);
48
49   private EjbEntityBean _returnType;
50   private JMethod _method;
51   private String JavaDoc _contextClassName;
52   private EjbSelectQuery _query;
53   private Type _amberType;
54   
55   public AmberSelectMethod(EjbEntityBean type,
56                JMethod method,
57                String JavaDoc contextClassName,
58                EjbSelectQuery query,
59                Type amberType)
60     throws ConfigException
61   {
62     super(type, method, query);
63
64     _returnType = type;
65     _method = method;
66     _contextClassName = contextClassName;
67     _query = query;
68     _amberType = amberType;
69
70     if (amberType == null)
71       throw new NullPointerException JavaDoc();
72   }
73
74   /**
75    * Gets the parameter types
76    */

77   public JClass []getParameterTypes()
78   {
79     return _method.getParameterTypes();
80   }
81
82   /**
83    * Gets the return type.
84    */

85   public JClass getReturnType()
86   {
87     return _method.getReturnType();
88   }
89
90   /**
91    * Prints the create method
92    *
93    * @param method the create method
94    */

95   public void generateCall(JavaWriter out, String JavaDoc []args)
96     throws IOException JavaDoc
97   {
98     out.print("com.caucho.ejb.xa.TransactionContext trans");
99     out.println(" = _ejb_context.getTransactionManager().beginSupports();");
100
101     out.println("com.caucho.amber.query.ResultSetImpl rs = null;");
102     out.println("try {");
103     out.pushDepth();
104
105     generatePrepareQuery(out, args);
106
107     out.println("rs = (com.caucho.amber.query.ResultSetImpl) query.executeQuery();");
108
109     out.println("if (rs.next()) {");
110     out.pushDepth();
111
112     out.print(getReturnType().getPrintName());
113     out.print(" v = ");
114
115     if (getReturnType().isPrimitive()) {
116       _amberType.generateLoad(out, "rs", "0", 1);
117       out.println(";");
118     }
119     else {
120       _amberType.generateLoad(out, "rs", "0", 1, getReturnType());
121       out.println(";");
122     }
123
124     out.println();
125     out.println("return v;");
126     
127     out.popDepth();
128     out.println("}");
129     out.println();
130
131     if (getReturnType().isPrimitive())
132       out.println("return 0;");
133     else
134       out.println("return null;");
135
136     out.popDepth();
137     out.println("} catch (java.sql.SQLException e) {");
138     out.println(" throw new com.caucho.ejb.FinderExceptionWrapper(e);");
139     out.println("} finally {");
140     out.println("if (rs != null)");
141     out.println(" rs.close();");
142
143     out.println(" trans.commit();");
144     out.println("}");
145   }
146 }
147
Popular Tags