KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > amber > field > EntityMapField


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  * Free Software Foundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.amber.field;
30
31 import com.caucho.amber.table.Column;
32 import com.caucho.amber.type.EntityType;
33 import com.caucho.bytecode.JMethod;
34 import com.caucho.config.ConfigException;
35 import com.caucho.java.JavaWriter;
36 import com.caucho.log.Log;
37 import com.caucho.util.L10N;
38
39 import java.io.IOException JavaDoc;
40 import java.util.ArrayList JavaDoc;
41 import java.util.logging.Level JavaDoc;
42 import java.util.logging.Logger JavaDoc;
43
44 /**
45  * Represents a map to entities.
46  */

47 public class EntityMapField extends AbstractField {
48   private static final L10N L = new L10N(EntityMapField.class);
49   protected static final Logger JavaDoc log = Log.open(EntityMapField.class);
50
51   private ArrayList JavaDoc<Column> _indexColumns;
52   private JMethod _mapMethod;
53
54   private EntityType _targetType;
55
56   private IdField _id;
57   private IdField _index;
58
59   public EntityMapField(EntityType entityType)
60   {
61     super(entityType);
62   }
63
64   /**
65    * Sets the field name.
66    */

67   public void setName(String JavaDoc name)
68   {
69     // hack for EJB maps
70
try {
71       super.setName(name);
72     } catch (ConfigException e) {
73       log.log(Level.FINEST, e.toString(), e);
74     }
75
76     setJavaType(java.util.Map JavaDoc.class);
77   }
78
79   /**
80    * Sets the target type.
81    */

82   public void setTargetType(EntityType type)
83   {
84     _targetType = type;
85   }
86
87   /**
88    * Returns true if the methods are abstract.
89    */

90   public boolean isUpdateable()
91   {
92     return false;
93   }
94
95   /**
96    * Sets the map method.
97    */

98   public void setMapMethod(JMethod method)
99   {
100     _mapMethod = method;
101   }
102
103   /**
104    * Sets the id field.
105    */

106   public void setId(IdField id)
107   {
108     _id = id;
109   }
110
111   /**
112    * Sets the index field.
113    */

114   public void setIndex(IdField index)
115   {
116     _index = index;
117   }
118
119   /**
120    * Sets the index columns.
121    */

122   public void setIndexColumns(ArrayList JavaDoc<Column> columns)
123   {
124     _indexColumns = columns;
125   }
126
127   /**
128    * Sets the index columns.
129    */

130   public ArrayList JavaDoc<Column> getIndexColumns()
131   {
132     return _indexColumns;
133   }
134
135   /**
136    * Generates loading cache
137    */

138   public void generateUpdate(JavaWriter out, String JavaDoc mask, String JavaDoc pstmt,
139                              String JavaDoc index)
140     throws IOException JavaDoc
141   {
142   }
143
144   /**
145    * Generates loading cache
146    */

147   public void generateLoadFromObject(JavaWriter out, String JavaDoc obj)
148     throws IOException JavaDoc
149   {
150   }
151
152   /**
153    * Generates loading cache
154    */

155   public void generateUpdateFromObject(JavaWriter out, String JavaDoc obj)
156     throws IOException JavaDoc
157   {
158   }
159
160   /**
161    * Generates the select clause.
162    */

163   public String JavaDoc generateLoadSelect(String JavaDoc id)
164   {
165     return null;
166   }
167
168   /**
169    * Updates the cached copy.
170    */

171   public void generateCopyUpdateObject(JavaWriter out,
172                                        String JavaDoc dst, String JavaDoc src,
173                                        int updateIndex)
174     throws IOException JavaDoc
175   {
176   }
177
178   /**
179    * Updates the cached copy.
180    */

181   public void generateCopyLoadObject(JavaWriter out,
182                                      String JavaDoc dst, String JavaDoc src,
183                                      int loadIndex)
184     throws IOException JavaDoc
185   {
186   }
187
188   /**
189    * Generates the get property.
190    */

191   public void generateSuperGetter(JavaWriter out)
192     throws IOException JavaDoc
193   {
194   }
195
196   /**
197    * Generates the get property.
198    */

199   public void generateSuperSetter(JavaWriter out)
200     throws IOException JavaDoc
201   {
202   }
203
204   /**
205    * Generates the set property.
206    */

207   public void generateGetProperty(JavaWriter out)
208     throws IOException JavaDoc
209   {
210     if (getGetterMethod() != null) {
211       out.println();
212       out.println("public " + getJavaTypeName() + " " + getGetterName() + "()");
213       out.println("{");
214       out.pushDepth();
215
216       out.println("return null;");
217
218       out.popDepth();
219       out.println("}");
220     }
221
222     if (_mapMethod != null) {
223       out.println();
224       out.print("public ");
225       out.print(_mapMethod.getReturnType().getPrintName());
226       out.print(" " + _mapMethod.getName() + "(");
227       out.print(_mapMethod.getParameterTypes()[0].getPrintName());
228       out.println(" a0)");
229       out.println("{");
230       out.pushDepth();
231
232       out.println("if (__caucho_session == null)");
233       out.println(" return null;");
234       out.println();
235
236
237       out.println("try {");
238       out.pushDepth();
239
240       out.println("com.caucho.amber.AmberQuery query;");
241
242       EntityType targetType = _targetType;
243
244       String JavaDoc table = targetType.getName();
245
246       out.print("String sql = \"SELECT o");
247       out.print(" FROM " + table + " o");
248       out.print(" WHERE ");
249
250       EntityType sourceType = (EntityType) getSourceType();
251       ArrayList JavaDoc<IdField> keys = sourceType.getId().getKeys();
252
253       out.print("o." + _index.getName() + "=?1");
254
255       for (int i = 0; i < keys.size(); i++) {
256         IdField key = keys.get(i);
257
258         out.print(" and ");
259
260         out.print("o." + _id.getName() + "." + key.getName() + "=?" + (i + 2));
261       }
262
263       out.println("\";");
264
265       out.println("query = __caucho_session.prepareQuery(sql);");
266
267       out.println("int index = 1;");
268       _index.getType().generateSet(out, "query", "index", "a0");
269
270       for (int i = 0; i < keys.size(); i++) {
271         IdField key = keys.get(i);
272
273         key.generateSet(out, "query", "index", "this");
274       }
275
276       out.print("return (");
277       out.print(_mapMethod.getReturnType().getPrintName());
278       out.println(") query.getSingleResult();");
279
280       out.popDepth();
281       out.println("} catch (Exception e) {");
282       out.println(" throw com.caucho.amber.AmberRuntimeException.create(e);");
283       out.println("}");
284
285       out.popDepth();
286       out.println("}");
287     }
288   }
289 }
290
Popular Tags