KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > amber > expr > LoadEntityExpr


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.expr;
30
31 import com.caucho.amber.entity.Entity;
32 import com.caucho.amber.entity.EntityItem;
33 import com.caucho.amber.field.AmberField;
34 import com.caucho.amber.manager.AmberConnection;
35 import com.caucho.amber.query.FromItem;
36 import com.caucho.amber.query.QueryParser;
37 import com.caucho.amber.table.LinkColumns;
38 import com.caucho.amber.table.Table;
39 import com.caucho.amber.type.EntityType;
40 import com.caucho.amber.type.Type;
41 import com.caucho.util.CharBuffer;
42 import com.caucho.util.L10N;
43
44 import java.lang.reflect.Method JavaDoc;
45 import java.sql.ResultSet JavaDoc;
46 import java.sql.SQLException JavaDoc;
47 import java.util.ArrayList JavaDoc;
48 import java.util.HashSet JavaDoc;
49 import java.util.Iterator JavaDoc;
50 import java.util.Map JavaDoc;
51 import java.util.logging.Level JavaDoc;
52 import java.util.logging.Logger JavaDoc;
53
54 /**
55  * An entity expression which should be loaded.
56  */

57 public class LoadEntityExpr extends LoadExpr {
58   private static final L10N L = new L10N(LoadEntityExpr.class);
59   private static final Logger JavaDoc log
60     = Logger.getLogger(LoadEntityExpr.class.getName());
61
62   LoadEntityExpr(PathExpr expr)
63   {
64     super(expr);
65   }
66
67   /**
68    * Returns the entity type.
69    */

70   public EntityType getEntityType()
71   {
72     return (EntityType) getType();
73   }
74
75   /**
76    * Binds the expression as a select item.
77    */

78   public AmberExpr bindSelect(QueryParser parser)
79   {
80     _fromItem = _expr.bindSubPath(parser);
81
82     if (_fromItem == null)
83       throw new NullPointerException JavaDoc(_expr.getClass().getName() + " " + _expr);
84
85     EntityType type = getEntityType();
86
87     if (type.getSecondaryTables().size() > 0) {
88       for (AmberField field : type.getFields()) {
89         Table subTable = field.getTable();
90
91         if (subTable != null && subTable != type.getTable()) {
92           LinkColumns link = subTable.getDependentIdLink();
93
94           FromItem item = parser.createDependentFromItem(_fromItem, link);
95
96           _subItems.add(item);
97         }
98       }
99     }
100
101     return this;
102   }
103
104   /**
105    * Returns the object for the expr.
106    */

107   public Object JavaDoc getObject(AmberConnection aConn, ResultSet JavaDoc rs, int index)
108     throws SQLException JavaDoc
109   {
110     return getEntityType().getLoadObject(aConn, rs, index);
111   }
112
113   /**
114    * Returns the object for the expr.
115    */

116   public Object JavaDoc getCacheObject(AmberConnection aConn,
117                                ResultSet JavaDoc rs,
118                                int index)
119     throws SQLException JavaDoc
120   {
121     return getCacheObject(aConn, rs, index, null);
122   }
123
124   /**
125    * Returns the object for the expr.
126    */

127   public Object JavaDoc getCacheObject(AmberConnection aConn,
128                                ResultSet JavaDoc rs,
129                                int index,
130                                Map JavaDoc<AmberExpr, String JavaDoc> joinFetchMap)
131     throws SQLException JavaDoc
132   {
133     return findItem(aConn, rs, index, joinFetchMap);
134   }
135
136   /**
137    * Returns the object for the expr.
138    */

139   public EntityItem findItem(AmberConnection aConn,
140                              ResultSet JavaDoc rs,
141                              int index)
142     throws SQLException JavaDoc
143   {
144     return findItem(aConn, rs, index, null);
145   }
146
147   /**
148    * Returns the object for the expr.
149    */

150   public EntityItem findItem(AmberConnection aConn,
151                              ResultSet JavaDoc rs,
152                              int index,
153                              Map JavaDoc<AmberExpr, String JavaDoc> joinFetchMap)
154     throws SQLException JavaDoc
155   {
156     // jpa/0h13, jpa/1160
157

158     EntityType entityType = getEntityType();
159
160     EntityItem item = entityType.getHome().findItem(aConn, rs, index);
161
162     if (item == null)
163       return null;
164
165     int keyLength = entityType.getId().getKeyCount();
166
167     Entity entity = item.getEntity();
168
169     _index = entity.__caucho_load(aConn, rs, index + keyLength);
170
171     item.setNumberOfLoadingColumns(_index);
172
173     String JavaDoc property = joinFetchMap.get(this._expr);
174
175     Iterator JavaDoc eagerFieldsIterator = null;
176
177     HashSet JavaDoc<String JavaDoc> eagerFieldNames = entityType.getEagerFieldNames();
178
179     if (eagerFieldNames != null)
180       eagerFieldsIterator = eagerFieldNames.iterator();
181
182     // XXX: needs to handle field-based access
183
if (! entityType.isFieldAccess()) {
184       if (property == null)
185         if ((eagerFieldsIterator != null) && eagerFieldsIterator.hasNext())
186           property = (String JavaDoc) eagerFieldsIterator.next();
187     }
188
189     if (property != null) {
190
191       try {
192
193         // XXX: Review if this entity can be attached.
194
entity.__caucho_setConnection(aConn);
195
196         Class JavaDoc cl = entityType.getInstanceClass();
197
198         do {
199
200           String JavaDoc methodName = "get" +
201             Character.toUpperCase(property.charAt(0)) +
202             property.substring(1);
203
204           Method JavaDoc method = cl.getDeclaredMethod(methodName, null);
205
206           Object JavaDoc field = method.invoke(entity, null);
207
208           // XXX: for now, invoke the toString() method on
209
// the collection to fetch all the objects (join fetch).
210

211           if (field == null) {
212             try {
213               methodName = "__caucho_item_" + methodName;
214
215               method = cl.getDeclaredMethod(methodName, new Class JavaDoc[] {AmberConnection.class});
216
217               field = method.invoke(entity, aConn);
218             } catch (Exception JavaDoc ex) {
219             }
220           }
221
222           if (field != null) {
223
224             Class JavaDoc fieldClass = field.getClass();
225
226             method = fieldClass.getMethod("toString", null);
227
228             method.invoke(field, null);
229           }
230
231           property = null;
232
233           if ((eagerFieldsIterator != null) && (eagerFieldsIterator.hasNext()))
234             property = (String JavaDoc) eagerFieldsIterator.next();
235         }
236         while (property != null);
237
238       } catch (NoSuchMethodException JavaDoc e) {
239         log.log(Level.FINER, e.toString(), e);
240       } catch (IllegalAccessException JavaDoc e) {
241         log.log(Level.FINER, e.toString(), e);
242       } catch (java.lang.reflect.InvocationTargetException JavaDoc e) {
243         log.log(Level.FINER, e.toString(), e);
244       }
245     }
246
247     return item;
248   }
249 }
250
Popular Tags