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.cfg21;
031:
032: import com.caucho.ejb.cfg.*;
033: import com.caucho.ejb.cfg21.CmrMap;
034: import com.caucho.ejb.cfg21.CmpGetter;
035: import com.caucho.amber.field.IdField;
036: import com.caucho.amber.type.EntityType;
037: import com.caucho.config.ConfigException;
038: import com.caucho.ejb.gen21.AbstractQueryMethod;
039: import com.caucho.ejb.gen21.BeanAssembler;
040: import com.caucho.java.JavaWriter;
041: import com.caucho.java.gen.BaseMethod;
042: import com.caucho.util.L10N;
043:
044: import java.io.IOException;
045: import java.util.ArrayList;
046:
047: /**
048: * Configuration for a one-to-many CMP method.
049: */
050: public class EjbMapGetter extends CmpGetter {
051: private static final L10N L = new L10N(EjbMapGetter.class);
052:
053: private CmrMap _map;
054:
055: /**
056: * Creates a new method.
057: *
058: * @param view the owning view
059: * @param apiMethod the method from the view
060: * @param implMethod the method from the implementation
061: */
062: public EjbMapGetter(EjbView view, ApiMethod apiMethod,
063: ApiMethod implMethod, CmrMap map) {
064: super (view, apiMethod, implMethod);
065:
066: _map = map;
067: }
068:
069: /**
070: * Assembles the bean method.
071: */
072: public void assembleBean(BeanAssembler beanAssembler,
073: String fullClassName) throws ConfigException {
074: beanAssembler.addMethod(new BeanMethod(getImplMethod()));
075: }
076:
077: class BeanMethod extends BaseMethod {
078: BeanMethod(ApiMethod method) {
079: super (method.getMethod());
080: }
081:
082: /**
083: * Generates the code for the call.
084: *
085: * @param out the writer to the output stream.
086: * @param args the arguments
087: */
088: protected void generateCall(JavaWriter out, String[] args)
089: throws IOException {
090: out.println();
091: out.println("try {");
092: out.pushDepth();
093:
094: out.println("com.caucho.amber.AmberQuery query;");
095:
096: String abstractSchema = _map.getTargetBean()
097: .getAbstractSchemaName();
098:
099: String id = _map.getIdName();
100: IdField index = null;
101:
102: EntityType targetType = _map.getTargetBean()
103: .getEntityType();
104: for (IdField key : targetType.getId().getKeys()) {
105: if (key.getName().equals(id)) {
106: } else {
107: index = key;
108: }
109: }
110:
111: out.print("String sql = \"SELECT o");
112: out.print(" FROM " + abstractSchema + " o");
113: out.print(" WHERE ");
114:
115: EntityType sourceType = _map.getBean().getEntityType();
116: ArrayList<IdField> keys = sourceType.getId().getKeys();
117:
118: out.print("o." + index.getName() + "=?1");
119:
120: for (int i = 0; i < keys.size(); i++) {
121: IdField key = keys.get(i);
122:
123: out.print(" AND ");
124:
125: out.print("o." + id + "." + key.getName() + "=?"
126: + (i + 2));
127: }
128:
129: out.println("\";");
130:
131: out
132: .println("query = _ejb_trans.getAmberConnection().prepareQuery(sql);");
133:
134: EjbConfig config = _map.getBean().getConfig();
135:
136: out.println("int index = 2;");
137: Class indexClass = index.getJavaType().getRawType()
138: .getJavaClass();
139: AbstractQueryMethod.generateSetParameter(out, config,
140: indexClass, "query", args[0]);
141:
142: for (int i = 0; i < keys.size(); i++) {
143: IdField key = keys.get(i);
144:
145: Class keyClass = key.getJavaType().getRawType()
146: .getJavaClass();
147: AbstractQueryMethod.generateSetParameter(out, config,
148: keyClass, "query", key.generateGet("this"));
149: }
150:
151: out.print("return (");
152: out.printClass(getMethod().getReturnType());
153: out.println(") query.getSingleResult();");
154:
155: out.popDepth();
156: out.println("} catch (RuntimeException e) {");
157: out.println(" throw e;");
158: out.println("} catch (java.sql.SQLException e) {");
159: out
160: .println(" throw com.caucho.ejb.EJBExceptionWrapper.createRuntime(e);");
161: out.println("}");
162: }
163: }
164: }
|