1 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 ; 45 import java.sql.ResultSet ; 46 import java.sql.SQLException ; 47 import java.util.ArrayList ; 48 import java.util.HashSet ; 49 import java.util.Iterator ; 50 import java.util.Map ; 51 import java.util.logging.Level ; 52 import java.util.logging.Logger ; 53 54 57 public class LoadEntityExpr extends LoadExpr { 58 private static final L10N L = new L10N(LoadEntityExpr.class); 59 private static final Logger log 60 = Logger.getLogger(LoadEntityExpr.class.getName()); 61 62 LoadEntityExpr(PathExpr expr) 63 { 64 super(expr); 65 } 66 67 70 public EntityType getEntityType() 71 { 72 return (EntityType) getType(); 73 } 74 75 78 public AmberExpr bindSelect(QueryParser parser) 79 { 80 _fromItem = _expr.bindSubPath(parser); 81 82 if (_fromItem == null) 83 throw new NullPointerException (_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 107 public Object getObject(AmberConnection aConn, ResultSet rs, int index) 108 throws SQLException 109 { 110 return getEntityType().getLoadObject(aConn, rs, index); 111 } 112 113 116 public Object getCacheObject(AmberConnection aConn, 117 ResultSet rs, 118 int index) 119 throws SQLException 120 { 121 return getCacheObject(aConn, rs, index, null); 122 } 123 124 127 public Object getCacheObject(AmberConnection aConn, 128 ResultSet rs, 129 int index, 130 Map <AmberExpr, String > joinFetchMap) 131 throws SQLException 132 { 133 return findItem(aConn, rs, index, joinFetchMap); 134 } 135 136 139 public EntityItem findItem(AmberConnection aConn, 140 ResultSet rs, 141 int index) 142 throws SQLException 143 { 144 return findItem(aConn, rs, index, null); 145 } 146 147 150 public EntityItem findItem(AmberConnection aConn, 151 ResultSet rs, 152 int index, 153 Map <AmberExpr, String > joinFetchMap) 154 throws SQLException 155 { 156 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 property = joinFetchMap.get(this._expr); 174 175 Iterator eagerFieldsIterator = null; 176 177 HashSet <String > eagerFieldNames = entityType.getEagerFieldNames(); 178 179 if (eagerFieldNames != null) 180 eagerFieldsIterator = eagerFieldNames.iterator(); 181 182 if (! entityType.isFieldAccess()) { 184 if (property == null) 185 if ((eagerFieldsIterator != null) && eagerFieldsIterator.hasNext()) 186 property = (String ) eagerFieldsIterator.next(); 187 } 188 189 if (property != null) { 190 191 try { 192 193 entity.__caucho_setConnection(aConn); 195 196 Class cl = entityType.getInstanceClass(); 197 198 do { 199 200 String methodName = "get" + 201 Character.toUpperCase(property.charAt(0)) + 202 property.substring(1); 203 204 Method method = cl.getDeclaredMethod(methodName, null); 205 206 Object field = method.invoke(entity, null); 207 208 211 if (field == null) { 212 try { 213 methodName = "__caucho_item_" + methodName; 214 215 method = cl.getDeclaredMethod(methodName, new Class [] {AmberConnection.class}); 216 217 field = method.invoke(entity, aConn); 218 } catch (Exception ex) { 219 } 220 } 221 222 if (field != null) { 223 224 Class 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 ) eagerFieldsIterator.next(); 235 } 236 while (property != null); 237 238 } catch (NoSuchMethodException e) { 239 log.log(Level.FINER, e.toString(), e); 240 } catch (IllegalAccessException e) { 241 log.log(Level.FINER, e.toString(), e); 242 } catch (java.lang.reflect.InvocationTargetException e) { 243 log.log(Level.FINER, e.toString(), e); 244 } 245 } 246 247 return item; 248 } 249 } 250 | Popular Tags |