1 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 ; 40 import java.util.ArrayList ; 41 import java.util.logging.Level ; 42 import java.util.logging.Logger ; 43 44 47 public class EntityMapField extends AbstractField { 48 private static final L10N L = new L10N(EntityMapField.class); 49 protected static final Logger log = Log.open(EntityMapField.class); 50 51 private ArrayList <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 67 public void setName(String name) 68 { 69 try { 71 super.setName(name); 72 } catch (ConfigException e) { 73 log.log(Level.FINEST, e.toString(), e); 74 } 75 76 setJavaType(java.util.Map .class); 77 } 78 79 82 public void setTargetType(EntityType type) 83 { 84 _targetType = type; 85 } 86 87 90 public boolean isUpdateable() 91 { 92 return false; 93 } 94 95 98 public void setMapMethod(JMethod method) 99 { 100 _mapMethod = method; 101 } 102 103 106 public void setId(IdField id) 107 { 108 _id = id; 109 } 110 111 114 public void setIndex(IdField index) 115 { 116 _index = index; 117 } 118 119 122 public void setIndexColumns(ArrayList <Column> columns) 123 { 124 _indexColumns = columns; 125 } 126 127 130 public ArrayList <Column> getIndexColumns() 131 { 132 return _indexColumns; 133 } 134 135 138 public void generateUpdate(JavaWriter out, String mask, String pstmt, 139 String index) 140 throws IOException 141 { 142 } 143 144 147 public void generateLoadFromObject(JavaWriter out, String obj) 148 throws IOException 149 { 150 } 151 152 155 public void generateUpdateFromObject(JavaWriter out, String obj) 156 throws IOException 157 { 158 } 159 160 163 public String generateLoadSelect(String id) 164 { 165 return null; 166 } 167 168 171 public void generateCopyUpdateObject(JavaWriter out, 172 String dst, String src, 173 int updateIndex) 174 throws IOException 175 { 176 } 177 178 181 public void generateCopyLoadObject(JavaWriter out, 182 String dst, String src, 183 int loadIndex) 184 throws IOException 185 { 186 } 187 188 191 public void generateSuperGetter(JavaWriter out) 192 throws IOException 193 { 194 } 195 196 199 public void generateSuperSetter(JavaWriter out) 200 throws IOException 201 { 202 } 203 204 207 public void generateGetProperty(JavaWriter out) 208 throws IOException 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 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 <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 |