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