KickJava   Java API By Example, From Geeks To Geeks.

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


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.MapClass;
39 import com.caucho.ejb.gen.ViewClass;
40 import com.caucho.java.JavaWriter;
41 import com.caucho.java.gen.BaseMethod;
42 import com.caucho.util.L10N;
43
44 import java.io.IOException JavaDoc;
45 import java.util.ArrayList JavaDoc;
46
47 /**
48  * Configuration for a one-to-many CMP method.
49  */

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

62   public EjbMapMethod(EjbView view,
63               JMethod apiMethod, JMethod implMethod,
64               CmrMap map)
65   {
66     super(view, apiMethod, implMethod);
67
68     _map = map;
69   }
70
71   /**
72    * Assembles the method.
73    */

74   public BaseMethod assemble(ViewClass viewAssembler, String JavaDoc fullClassName)
75   {
76     BaseMethod method = viewAssembler.createBusinessMethod(this);
77     
78     method.setCall(assembleCallChain(method.getCall()));
79     
80     return method;
81   }
82
83   /**
84    * Assembles the bean method.
85    */

86   public void assembleBean(BeanAssembler beanAssembler, String JavaDoc fullClassName)
87     throws ConfigException
88   {
89     String JavaDoc mapName = _map.getName() + "_Map";
90     
91     MapClass map = new MapClass(_map, mapName);
92     
93     beanAssembler.addComponent(map);
94
95     beanAssembler.addMethod(new BeanMethod(getImplMethod()));
96   }
97
98   class BeanMethod extends BaseMethod {
99     BeanMethod(JMethod method)
100     {
101       super(method);
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 JavaDoc []args)
111       throws IOException JavaDoc
112     {
113       out.println("try {");
114       out.pushDepth();
115       
116       out.println("com.caucho.amber.AmberQuery query;");
117
118       String JavaDoc abstractSchema = _map.getTargetBean().getAbstractSchemaName();
119       String JavaDoc id = _map.getIdName();
120       IdField index = null;
121
122       EntityType sourceType = _map.getBean().getEntityType();
123       for (IdField key : sourceType.getId().getKeys()) {
124     if (key.getName().equals(id)) {
125     }
126     else {
127       index = key;
128     }
129       }
130       
131       out.print("String sql = \"SELECT o." + _map.getIndexName() + ", o");
132       out.print(" FROM " + abstractSchema + " o");
133       out.print(" WHERE ");
134
135       EntityType targetType = _map.getBean().getEntityType();
136       ArrayList JavaDoc<IdField> keys = targetType.getId().getKeys();
137
138       for (int i = 0; i < keys.size(); i++) {
139     IdField key = keys.get(i);
140
141     if (i != 0)
142       out.print(" AND ");
143
144     out.print("o." + id + "." + key.getName() + "=?" + (i + 1));
145       }
146
147       out.println("\";");
148       
149       out.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     AbstractQueryMethod.generateSetParameter(out,
158                          config,
159                          key.getJavaType(),
160                          "query",
161                          key.generateGet("this"));
162       }
163       
164       String JavaDoc mapName = _map.getName() + "_Map";
165
166       out.print(mapName + " map = ");
167       
168       out.println("new " + mapName + "(this, query);");
169
170       out.println("return map;");
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.println(" throw com.caucho.ejb.EJBExceptionWrapper.createRuntime(e);");
177       out.println("}");
178     }
179   }
180 }
181
Popular Tags