001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.ejb.gen21;
030:
031: import com.caucho.ejb.cfg21.EjbEntityBean;
032: import com.caucho.ejb.gen21.AbstractQueryMethod;
033: import com.caucho.config.ConfigException;
034: import com.caucho.ejb.cfg.*;
035: import com.caucho.ejb.ql.EjbSelectQuery;
036: import com.caucho.java.JavaWriter;
037: import com.caucho.util.L10N;
038:
039: import java.io.IOException;
040: import java.util.Collection;
041:
042: /**
043: * Generates the code for a query
044: */
045: public class AmberQueryMethod extends AbstractQueryMethod {
046: private static final L10N L = new L10N(AmberQueryMethod.class);
047:
048: private EjbEntityBean _returnType;
049: private ApiMethod _method;
050: private String _contextClassName;
051: private String _prefix;
052:
053: public AmberQueryMethod(EjbEntityBean type, ApiMethod method,
054: String contextClassName, String prefix, EjbSelectQuery query)
055: throws ConfigException {
056: super (type, method, query);
057:
058: _returnType = type;
059: _method = method;
060: _contextClassName = contextClassName;
061: _prefix = prefix;
062: }
063:
064: /**
065: * Gets the parameter types
066: */
067: public Class[] getParameterTypes() {
068: return _method.getParameterTypes();
069: }
070:
071: /**
072: * Gets the return type.
073: */
074: public Class getReturnType() {
075: return _method.getReturnType();
076: }
077:
078: /**
079: * Prints the create method
080: *
081: * @param method the create method
082: */
083: public void generateCall(JavaWriter out, String[] args)
084: throws IOException {
085: out.print("com.caucho.ejb.xa.TransactionContext trans");
086: out.println(" = _xaManager.beginSupports();");
087:
088: out.println("try {");
089: out.pushDepth();
090:
091: generatePrepareQuery(out, args);
092:
093: out
094: .println("com.caucho.amber.query.ResultSetImpl rs = (com.caucho.amber.query.ResultSetImpl) query.executeQuery();");
095: out
096: .println("java.util.ArrayList list = new java.util.ArrayList();");
097:
098: boolean isCollection = Collection.class
099: .isAssignableFrom(_method.getReturnType());
100:
101: if (isCollection) {
102: out.println("while (rs.next()) {");
103: out.pushDepth();
104:
105: String beanClass = _returnType.getFullImplName();
106:
107: out
108: .println("com.caucho.ejb.entity.EntityObject item = (com.caucho.ejb.entity.EntityObject) rs.getObject(1);");
109: if ("RemoteHome".equals(_prefix))
110: out.println("list.add(item.getEJBObject());");
111: else if ("LocalHome".equals(_prefix))
112: out.println("list.add(item);");
113: else
114: out.println("list.add(item);");
115:
116: out.popDepth();
117: out.println("}");
118: out.println("rs.close();");
119: out.println();
120: out.println("return list;");
121: } else {
122: out.println("if (rs.next()) {");
123: out.pushDepth();
124:
125: String beanClass = _returnType.getFullImplName();
126:
127: out
128: .println("com.caucho.ejb.entity.EntityObject entity = (com.caucho.ejb.entity.EntityObject) rs.getObject(1);");
129: out.println("rs.close();");
130: out.println();
131:
132: String retType = getReturnType().getName();
133:
134: if ("RemoteHome".equals(_prefix))
135: out.println("return (" + retType
136: + ") entity.getEJBObject();");
137: else if ("LocalHome".equals(_prefix))
138: out.println("return (" + retType + ") entity;");
139: else
140: throw new IllegalStateException(L.l(
141: "'{0}' is an unknown type", _prefix));
142:
143: /*
144: String retType = getReturnType().getName();
145:
146: out.println(retType + " v = (" + retType + ") rs.getObject(1);");
147: out.println("rs.close();");
148:
149: out.println("return v;");
150:
151: if (! "LocalHome".equals(_prefix))
152: throw new IllegalStateException(L.l("'{0}' is an unknown type",
153: _prefix));
154: */
155:
156: out.popDepth();
157: out.println("}");
158: out.println();
159: out
160: .println("throw new ObjectNotFoundException(\"no matching object found\");");
161: }
162:
163: /*
164: Class retType = getReturnType();
165:
166: if ("RemoteHome".equals(_prefix))
167: out.println("return (" + retType.getName() + ") _server.getContext(" + args[0] + ", true).getEJBObject();");
168: else if ("LocalHome".equals(_prefix))
169: out.println("return (" + retType.getName() + ") _server.getContext(" + args[0] + ", true).getEJBLocalObject();");
170: else
171: throw new IllegalStateException(L.l("'{0}' is an unknown type",
172: _prefix));
173: */
174:
175: out.popDepth();
176: out.println("} catch (java.sql.SQLException e) {");
177: out
178: .println(" throw new com.caucho.ejb.FinderExceptionWrapper(e);");
179: out.println("} finally {");
180: out.println(" trans.commit();");
181: out.println("}");
182: }
183: }
|