KickJava   Java API By Example, From Geeks To Geeks.

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


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

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

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

72   public void assembleBean(BeanAssembler beanAssembler, String JavaDoc fullClassName)
73     throws ConfigException
74   {
75     beanAssembler.addMethod(new BeanMethod(getImplMethod()));
76   }
77
78   class BeanMethod extends BaseMethod {
79     BeanMethod(JMethod method)
80     {
81       super(method);
82     }
83   
84     /**
85      * Generates the code for the call.
86      *
87      * @param out the writer to the output stream.
88      * @param args the arguments
89      */

90     protected void generateCall(JavaWriter out, String JavaDoc []args)
91       throws IOException JavaDoc
92     {
93       out.println();
94       out.println("try {");
95       out.pushDepth();
96
97       out.println("com.caucho.amber.AmberQuery query;");
98
99       String JavaDoc abstractSchema = _map.getTargetBean().getAbstractSchemaName();
100
101       String JavaDoc id = _map.getIdName();
102       IdField index = null;
103
104       EntityType targetType = _map.getTargetBean().getEntityType();
105       for (IdField key : targetType.getId().getKeys()) {
106     if (key.getName().equals(id)) {
107     }
108     else {
109       index = key;
110     }
111       }
112       
113       out.print("String sql = \"SELECT o");
114       out.print(" FROM " + abstractSchema + " o");
115       out.print(" WHERE ");
116
117       EntityType sourceType = _map.getBean().getEntityType();
118       ArrayList JavaDoc<IdField> keys = sourceType.getId().getKeys();
119
120       out.print("o." + index.getName() + "=?1");
121
122       for (int i = 0; i < keys.size(); i++) {
123     IdField key = keys.get(i);
124     
125     out.print(" AND ");
126
127     out.print("o." + id + "." + key.getName() + "=?" + (i + 2));
128       }
129
130       out.println("\";");
131       
132       out.println("query = _ejb_trans.getAmberConnection().prepareQuery(sql);");
133
134       EjbConfig config = _map.getBean().getConfig();
135  
136       out.println("int index = 2;");
137       AbstractQueryMethod.generateSetParameter(out,
138                            config,
139                            index.getJavaType(),
140                            "query",
141                            args[0]);
142       
143       for (int i = 0; i < keys.size(); i++) {
144     IdField key = keys.get(i);
145
146     AbstractQueryMethod.generateSetParameter(out,
147                          config,
148                          key.getJavaType(),
149                          "query",
150                          key.generateGet("this"));
151       }
152       
153       out.print("return (");
154       out.print(getMethod().getReturnType().getPrintName());
155       out.println(") query.getSingleResult();");
156
157       out.popDepth();
158       out.println("} catch (RuntimeException e) {");
159       out.println(" throw e;");
160       out.println("} catch (java.sql.SQLException e) {");
161       out.println(" throw com.caucho.ejb.EJBExceptionWrapper.createRuntime(e);");
162       out.println("}");
163     }
164   }
165 }
166
Popular Tags