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: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.ejb.gen21;
031:
032: import com.caucho.ejb.cfg21.EjbEntityBean;
033: import com.caucho.ejb.gen21.AbstractQueryMethod;
034: import com.caucho.config.ConfigException;
035: import com.caucho.ejb.cfg.*;
036: import com.caucho.ejb.ql.EjbSelectQuery;
037: import com.caucho.java.JavaWriter;
038: import com.caucho.util.L10N;
039:
040: import java.io.IOException;
041: import java.util.*;
042:
043: /**
044: * Generates the code for a query
045: */
046: public class AmberSelectCollectionMethod extends AbstractQueryMethod {
047: private static final L10N L = new L10N(
048: AmberSelectCollectionMethod.class);
049:
050: private EjbEntityBean _returnType;
051: private ApiMethod _method;
052: private String _contextClassName;
053:
054: public AmberSelectCollectionMethod(EjbEntityBean type,
055: ApiMethod method, String contextClassName,
056: EjbSelectQuery query) throws ConfigException {
057: super (type, method, query);
058:
059: _returnType = type;
060: _method = method;
061: _contextClassName = contextClassName;
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
087: .println(" = _ejb_context.getTransactionManager().beginSupports();");
088:
089: out.println("try {");
090: out.pushDepth();
091:
092: generatePrepareQuery(out, args);
093:
094: out
095: .println("com.caucho.amber.query.ResultSetImpl rs = (com.caucho.amber.query.ResultSetImpl) query.executeQuery();");
096: out
097: .println("java.util.ArrayList list = new java.util.ArrayList();");
098:
099: out.println("while (rs.next()) {");
100: out.pushDepth();
101:
102: String beanClass = _returnType.getEJBClass().getName();
103:
104: // out.println("com.caucho.amber.entity.EntityItem item = rs.findEntityItem(1);");
105: // out.println(beanClass + " bean = (" + beanClass + ") rs.getObject(1);");
106: // out.println(_contextClassName + " context = (" + _contextClassName + ") _ejb_context.getServer().getContext(" + generateBeanId() ");");
107: // out.println("list.add(context.getEJBLocalObject());");
108: out.println("list.add(rs.getObject(1));");
109:
110: out.popDepth();
111: out.println("}");
112: out.println();
113:
114: // XXX:
115: Class retType = _method.getReturnType();
116: if (Collection.class.isAssignableFrom(retType))
117: out.println("return list;");
118: else
119: out.println("return null;");
120:
121: /*
122: Class retType = getReturnType();
123:
124: if ("RemoteHome".equals(_prefix))
125: out.println("return (" + retType.getName() + ") _server.getContext(" + args[0] + ", true).getEJBObject();");
126: else if ("LocalHome".equals(_prefix))
127: out.println("return (" + retType.getName() + ") _server.getContext(" + args[0] + ", true).getEJBLocalObject();");
128: else
129: throw new IllegalStateException(L.l("'{0}' is an unknown type",
130: _prefix));
131: */
132:
133: out.popDepth();
134: out.println("} catch (java.sql.SQLException e) {");
135: out
136: .println(" throw new com.caucho.ejb.FinderExceptionWrapper(e);");
137: out.println("} finally {");
138: out.println(" trans.commit();");
139: out.println("}");
140: }
141: }
|