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