KickJava   Java API By Example, From Geeks To Geeks.

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


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

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

68   public JClass []getParameterTypes()
69   {
70     return _method.getParameterTypes();
71   }
72
73   /**
74    * Gets the return type.
75    */

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

86   public void generateCall(JavaWriter out, String JavaDoc []args)
87     throws IOException JavaDoc
88   {
89     out.print("com.caucho.ejb.xa.TransactionContext trans");
90     out.println(" = _ejb_context.getTransactionManager().beginSupports();");
91
92     out.println("try {");
93     out.pushDepth();
94
95     generatePrepareQuery(out, args);
96
97     out.println("com.caucho.amber.query.ResultSetImpl rs = (com.caucho.amber.query.ResultSetImpl) query.executeQuery();");
98     out.println("java.util.ArrayList list = new java.util.ArrayList();");
99
100     out.println("while (rs.next()) {");
101     out.pushDepth();
102
103     String JavaDoc beanClass = _returnType.getEJBClass().getName();
104
105     // out.println("com.caucho.amber.entity.EntityItem item = rs.findEntityItem(1);");
106
// out.println(beanClass + " bean = (" + beanClass + ") rs.getObject(1);");
107
// out.println(_contextClassName + " context = (" + _contextClassName + ") _ejb_context.getServer().getContext(" + generateBeanId() ");");
108
// out.println("list.add(context.getEJBLocalObject());");
109
out.println("list.add(rs.getObject(1));");
110     
111     out.popDepth();
112     out.println("}");
113     out.println();
114     
115     // XXX:
116
JClass retType = _method.getReturnType();
117     if (retType.isAssignableTo(java.util.Collection JavaDoc.class))
118       out.println("return list;");
119     else
120       out.println("return null;");
121     
122     
123     /*
124     Class retType = getReturnType();
125
126     if ("RemoteHome".equals(_prefix))
127       out.println("return (" + retType.getName() + ") _server.getContext(" + args[0] + ", true).getEJBObject();");
128     else if ("LocalHome".equals(_prefix))
129       out.println("return (" + retType.getName() + ") _server.getContext(" + args[0] + ", true).getEJBLocalObject();");
130     else
131       throw new IllegalStateException(L.l("'{0}' is an unknown type",
132                       _prefix));
133     */

134
135     out.popDepth();
136     out.println("} catch (java.sql.SQLException e) {");
137     out.println(" throw new com.caucho.ejb.FinderExceptionWrapper(e);");
138     out.println("} finally {");
139     out.println(" trans.commit();");
140     out.println("}");
141   }
142 }
143
Popular Tags