KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > ejb > cfg > EjbManyToManyMethod


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.ejb.cfg;
31
32 import com.caucho.amber.field.IdField;
33 import com.caucho.amber.type.EntityType;
34 import com.caucho.bytecode.JMethod;
35 import com.caucho.config.ConfigException;
36 import com.caucho.ejb.gen.AbstractQueryMethod;
37 import com.caucho.ejb.gen.BeanAssembler;
38 import com.caucho.ejb.gen.CollectionClass;
39 import com.caucho.java.JavaWriter;
40 import com.caucho.java.gen.BaseMethod;
41 import com.caucho.util.L10N;
42
43 import java.io.IOException JavaDoc;
44 import java.util.ArrayList JavaDoc;
45
46 /**
47  * Configuration for a one-to-many CMP method.
48  */

49 public class EjbManyToManyMethod extends CmpGetter {
50   private static final L10N L = new L10N(EjbManyToManyMethod.class);
51
52   private CmrManyToMany _manyToMany;
53   
54   /**
55    * Creates a new method.
56    *
57    * @param view the owning view
58    * @param apiMethod the method from the view
59    * @param implMethod the method from the implementation
60    */

61   public EjbManyToManyMethod(EjbView view,
62                 JMethod apiMethod, JMethod implMethod,
63                 CmrManyToMany manyToMany)
64   {
65     super(view, apiMethod, implMethod);
66
67     _manyToMany = manyToMany;
68   }
69
70   /**
71    * Assembles the bean method.
72    */

73   public void assembleBean(BeanAssembler beanAssembler, String JavaDoc fullClassName)
74     throws ConfigException
75   {
76     String JavaDoc listName = _manyToMany.getName() + "_List";
77
78     CollectionClass list = new CollectionClass(_manyToMany, listName);
79     
80     beanAssembler.addComponent(list);
81
82     beanAssembler.addMethod(new BeanMethod(getImplMethod()));
83   }
84
85   class BeanMethod extends BaseMethod {
86     BeanMethod(JMethod method)
87     {
88       super(method);
89     }
90   
91     /**
92      * Generates the code for the call.
93      *
94      * @param out the writer to the output stream.
95      * @param args the arguments
96      */

97     protected void generateCall(JavaWriter out, String JavaDoc []args)
98       throws IOException JavaDoc
99     {
100       out.println("try {");
101       out.pushDepth();
102
103       out.println("com.caucho.amber.AmberQuery query;");
104
105       String JavaDoc abstractSchema = _manyToMany.getBean().getAbstractSchemaName();
106       
107       out.print("String sql = \"SELECT o." + _manyToMany.getName());
108       out.print(" FROM " + abstractSchema + " o");
109       out.print(" WHERE ");
110
111       EntityType type = _manyToMany.getBean().getEntityType();
112       ArrayList JavaDoc<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.println("query = _ejb_trans.getAmberConnection().prepareQuery(sql);");
126
127       EjbConfig config = _manyToMany.getBean().getConfig();
128
129       out.println("int index = 1;");
130       for (int i = 0; i < keys.size(); i++) {
131     IdField key = keys.get(i);
132
133     AbstractQueryMethod.generateSetParameter(out,
134                          config,
135                          key.getJavaType(),
136                          "query",
137                          key.generateGet("this"));
138       }
139       
140       String JavaDoc 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.println(" throw com.caucho.ejb.EJBExceptionWrapper.createRuntime(e);");
153       out.println("}");
154     }
155   }
156 }
157
Popular Tags