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.CmrManyToMany;
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 EjbManyToManyMethod extends CmpGetter {
052: private static final L10N L = new L10N(EjbManyToManyMethod.class);
053:
054: private CmrManyToMany _manyToMany;
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 EjbManyToManyMethod(EjbView view, ApiMethod apiMethod,
064: ApiMethod implMethod, CmrManyToMany manyToMany) {
065: super (view, apiMethod, implMethod);
066:
067: _manyToMany = manyToMany;
068: }
069:
070: /**
071: * Assembles the bean method.
072: */
073: public void assembleBean(BeanAssembler beanAssembler,
074: String fullClassName) throws ConfigException {
075: String listName = _manyToMany.getName() + "_List";
076:
077: CollectionClass list = new CollectionClass(_manyToMany,
078: listName);
079:
080: beanAssembler.addComponent(list);
081:
082: beanAssembler.addMethod(new BeanMethod(getImplMethod()));
083: }
084:
085: class BeanMethod extends BaseMethod {
086: BeanMethod(ApiMethod method) {
087: super (method.getMethod());
088: }
089:
090: /**
091: * Generates the code for the call.
092: *
093: * @param out the writer to the output stream.
094: * @param args the arguments
095: */
096: protected void generateCall(JavaWriter out, String[] args)
097: throws IOException {
098: out.println("try {");
099: out.pushDepth();
100:
101: out.println("com.caucho.amber.AmberQuery query;");
102:
103: String abstractSchema = _manyToMany.getBean()
104: .getAbstractSchemaName();
105:
106: out.print("String sql = \"SELECT o."
107: + _manyToMany.getName());
108: out.print(" FROM " + abstractSchema + " o");
109: out.print(" WHERE ");
110:
111: EntityType type = _manyToMany.getBean().getEntityType();
112: ArrayList<IdField> keys = type.getId().getKeys();
113:
114: for (int i = 0; i < keys.size(); i++) {
115: IdField key = keys.get(i);
116:
117: if (i != 0)
118: out.print(" AND ");
119:
120: out.print("o." + key.getName() + "=?" + (i + 1));
121: }
122:
123: out.println("\";");
124:
125: out
126: .println("query = _ejb_trans.getAmberConnection().prepareQuery(sql);");
127:
128: EjbConfig config = _manyToMany.getBean().getConfig();
129:
130: out.println("int index = 1;");
131: for (int i = 0; i < keys.size(); i++) {
132: IdField key = keys.get(i);
133:
134: Class keyClass = key.getJavaType().getRawType()
135: .getJavaClass();
136: AbstractQueryMethod.generateSetParameter(out, config,
137: keyClass, "query", key.generateGet("this"));
138: }
139:
140: String listName = _manyToMany.getName() + "_List";
141:
142: out.print(listName + " list = ");
143:
144: out.println("new " + listName + "(this, query);");
145:
146: out.println("return list;");
147:
148: out.popDepth();
149: out.println("} catch (RuntimeException e) {");
150: out.println(" throw e;");
151: out.println("} catch (java.sql.SQLException e) {");
152: out
153: .println(" throw com.caucho.ejb.EJBExceptionWrapper.createRuntime(e);");
154: out.println("}");
155: }
156: }
157: }
|