1 29 30 package com.caucho.amber.field; 31 32 import com.caucho.amber.expr.AmberExpr; 33 import com.caucho.amber.expr.DependentEntityOneToOneExpr; 34 import com.caucho.amber.expr.PathExpr; 35 import com.caucho.amber.query.QueryParser; 36 import com.caucho.amber.table.Column; 37 import com.caucho.amber.table.ForeignColumn; 38 import com.caucho.amber.table.LinkColumns; 39 import com.caucho.amber.type.RelatedType; 40 import com.caucho.amber.type.Type; 41 import com.caucho.config.ConfigException; 42 import com.caucho.java.JavaWriter; 43 import com.caucho.log.Log; 44 import com.caucho.util.CharBuffer; 45 import com.caucho.util.L10N; 46 47 import java.io.IOException ; 48 import java.util.ArrayList ; 49 import java.util.HashSet ; 50 import java.util.logging.Logger ; 51 52 import javax.persistence.CascadeType; 53 54 57 public class DependentEntityOneToOneField extends CascadableField { 58 private static final L10N L = new L10N(DependentEntityOneToOneField.class); 59 protected static final Logger log = Log.open(DependentEntityOneToOneField.class); 60 61 private EntityManyToOneField _targetField; 62 private long _targetLoadIndex; 63 private boolean _isCascadeDelete; 64 65 public DependentEntityOneToOneField(RelatedType relatedType, 66 String name) 67 throws ConfigException 68 { 69 super(relatedType, name, null); 70 } 71 72 75 public void setTargetField(EntityManyToOneField targetField) 76 { 77 _targetField = targetField; 78 } 79 80 83 public EntityManyToOneField getTargetField() 84 { 85 return _targetField; 86 } 87 88 92 public RelatedType getEntitySourceType() 93 { 94 return (RelatedType) getSourceType(); 95 } 96 97 101 public RelatedType getEntityTargetType() 102 { 103 return (RelatedType) _targetField.getSourceType(); 104 } 105 106 109 public Type getType() 110 { 111 return getEntityTargetType(); 112 } 113 114 117 public String getForeignTypeName() 118 { 119 return getEntityTargetType().getForeignTypeName(); 121 } 122 123 126 public void setColumn(Column column) 127 { 128 throw new IllegalStateException (); 129 } 130 131 134 public void setCascadeDelete(boolean isCascadeDelete) 135 { 136 _isCascadeDelete = isCascadeDelete; 137 } 138 139 142 public boolean isCascadeDelete() 143 { 144 return _isCascadeDelete; 145 } 146 147 public void init() 148 throws ConfigException 149 { 150 super.init(); 151 152 _targetLoadIndex = getEntitySourceType().nextLoadGroupIndex(); 153 } 154 155 158 public AmberExpr createExpr(QueryParser parser, PathExpr parent) 159 { 160 return new DependentEntityOneToOneExpr(parent, 161 _targetField.getLinkColumns()); 162 } 163 164 167 public ForeignColumn getColumn(IdField targetField) 168 { 169 182 183 return null; 184 } 185 186 189 public boolean generateFlushCheck(JavaWriter out) 190 throws IOException 191 { 192 String getter = generateSuperGetter(); 193 194 out.println("if (" + getter + " != null) {"); 195 out.pushDepth(); 196 out.println("int state = ((com.caucho.amber.entity.Entity) " + getter + ").__caucho_getEntityState();"); 197 198 out.println("if (this.__caucho_state < com.caucho.amber.entity.Entity.P_DELETING"); 200 out.println(" && (state <= com.caucho.amber.entity.Entity.P_NEW || "); 201 out.println(" state >= com.caucho.amber.entity.Entity.P_DELETED))"); 202 203 String errorString = ("(\"amber flush: unable to flush " + 204 getEntitySourceType().getName() + "[\" + __caucho_getPrimaryKey() + \"] "+ 205 "with non-managed dependent relationship one-to-one to "+ 206 getEntityTargetType().getName() + "\")"); 207 208 out.println(" throw new IllegalStateException" + errorString + ";"); 209 out.popDepth(); 210 out.println("}"); 211 212 return true; 213 } 214 215 218 public void generatePrologue(JavaWriter out, HashSet <Object > completedSet) 219 throws IOException 220 { 221 super.generatePrologue(out, completedSet); 222 223 out.println(); 224 225 Id id = getEntityTargetType().getId(); 226 227 id.generatePrologue(out, completedSet, getName()); 228 } 229 230 233 public void generateJoin(CharBuffer cb, 234 String sourceTable, 235 String targetTable) 236 { 237 LinkColumns linkColumns = _targetField.getLinkColumns(); 238 239 cb.append(linkColumns.generateJoin(sourceTable, targetTable)); 240 } 241 242 245 public int generateLoad(JavaWriter out, String rs, 246 String indexVar, int index) 247 throws IOException 248 { 249 if (isLazy()) { 250 out.println(generateSuperSetter("null") + ";"); 251 252 String loadVar = "__caucho_loadMask_" + (_targetLoadIndex / 64); 253 long loadMask = (1L << _targetLoadIndex); 254 255 out.println(loadVar + " &= ~" + loadMask + "L;"); 256 } 257 258 return index; 260 } 261 262 265 public int generateLoadEager(JavaWriter out, String rs, 266 String indexVar, int index) 267 throws IOException 268 { 269 if (! isLazy()) { 270 271 String loadVar = "__caucho_loadMask_" + (_targetLoadIndex / 64); 272 long loadMask = 1L << (_targetLoadIndex % 64); 273 274 out.println(loadVar + " |= " + loadMask + "L;"); 275 276 String javaType = getJavaTypeName(); 277 278 out.println("if ((preloadedProperties == null) || (! preloadedProperties.containsKey(\"" + getName() + "\"))) {"); 279 out.pushDepth(); 280 281 String indexS = "_" + (_targetLoadIndex / 64); 282 indexS += "_" + (1L << (_targetLoadIndex % 64)); 283 284 generateLoadProperty(out, indexS, "aConn"); 285 286 out.popDepth(); 287 out.println("} else {"); 288 out.println(" " + generateSuperSetter("(" + javaType + ") preloadedProperties.get(\"" + getName() + "\")") + ";"); 289 out.println("}"); 290 } 291 292 return index; 293 } 294 295 298 public void generateGetProperty(JavaWriter out) 299 throws IOException 300 { 301 String loadVar = "__caucho_loadMask_" + (_targetLoadIndex / 64); 302 long loadMask = 1L << (_targetLoadIndex % 64); 303 304 String index = "_" + (_targetLoadIndex / 64); 305 index += "_" + (1L << (_targetLoadIndex % 64)); 306 307 String javaType = getJavaTypeName(); 308 309 out.println(); 310 out.println("public " + javaType + " " + getGetterName() + "()"); 311 out.println("{"); 312 out.pushDepth(); 313 314 out.print("if (__caucho_session != null && "); 315 out.println("(" + loadVar + " & " + loadMask + "L) == 0) {"); 316 out.pushDepth(); 317 out.println("__caucho_load_" + getLoadGroupIndex() + "(__caucho_session);"); 318 out.println(loadVar + " |= " + loadMask + "L;"); 319 320 generateLoadProperty(out, index, "__caucho_session"); 321 322 326 327 out.println("return v"+index+";"); 328 out.popDepth(); 329 out.println("}"); 330 out.println("else {"); 331 out.println(" return " + generateSuperGetter() + ";"); 332 out.println("}"); 333 334 out.popDepth(); 335 out.println("}"); 336 } 337 338 341 public void generateLoadProperty(JavaWriter out, 342 String index, 343 String session) 344 throws IOException 345 { 346 String javaType = getJavaTypeName(); 347 348 out.println(javaType + " v"+index+" = null;"); 349 350 out.println("try {"); 351 out.pushDepth(); 352 353 out.print("String sql"+index+" = \""); 354 out.print("SELECT o." + getName() + 355 " FROM " + getEntitySourceType().getName() + " o" + 356 " WHERE "); 357 358 ArrayList <IdField> sourceKeys = getEntitySourceType().getId().getKeys(); 359 for (int i = 0; i < sourceKeys.size(); i++) { 360 if (i != 0) 361 out.print(" and "); 362 363 IdField key = sourceKeys.get(i); 364 365 out.print("o." + key.getName() + "=?"); 366 } 367 out.println("\";"); 368 369 out.println("com.caucho.amber.AmberQuery query"+index+" = "+session+".prepareQuery(sql"+index+");"); 370 371 out.println("int index"+index+" = 1;"); 372 373 getEntitySourceType().getId().generateSet(out, "query"+index, "index"+index, "super"); 374 375 out.println("v"+index+" = (" + javaType + ") query"+index+".getSingleResult();"); 376 377 out.popDepth(); 378 out.println("} catch (java.sql.SQLException e) {"); 379 out.println(" throw new RuntimeException(e);"); 380 out.println("}"); 381 382 out.println(generateSuperSetter("v"+index) + ";"); 383 } 384 385 388 public void generateCopyUpdateObject(JavaWriter out, 389 String dst, String src, 390 int updateIndex) 391 throws IOException 392 { 393 if (getIndex() == updateIndex) { 394 String value = generateGet(src); 395 out.println(generateSet(dst, value) + ";"); 396 } 397 } 398 399 402 public void generateCopyLoadObject(JavaWriter out, 403 String dst, String src, 404 int updateIndex) 405 throws IOException 406 { 407 if (getLoadGroupIndex() == updateIndex) { 408 String value = generateGet(src); 409 out.println(generateSet(dst, value) + ";"); 410 } 411 } 412 413 416 public void generateSetProperty(JavaWriter out) 417 throws IOException 418 { 419 Id id = getEntityTargetType().getId(); 420 421 String keyType = getEntityTargetType().getId().getForeignTypeName(); 422 423 out.println(); 424 out.println("public void " + getSetterName() + "(" + getJavaTypeName() + " v)"); 425 out.println("{"); 426 out.pushDepth(); 427 428 out.println(generateSuperSetter("v") + ";"); 429 out.println("if (__caucho_session != null) {"); 430 out.pushDepth(); 431 432 String updateVar = "__caucho_updateMask_" + (_targetLoadIndex / 64); 433 long updateMask = (1L << _targetLoadIndex); 434 435 out.println(updateVar + " |= " + updateMask + "L;"); 436 out.println("__caucho_session.update(this);"); 437 out.popDepth(); 438 out.println("}"); 439 440 out.popDepth(); 441 out.println("}"); 442 } 443 444 447 public void generateSet(JavaWriter out, String pstmt, String index) 448 throws IOException 449 { 450 } 451 452 455 public void generateUpdateFromObject(JavaWriter out, String obj) 456 throws IOException 457 { 458 } 459 460 463 public void generateInvalidateForeign(JavaWriter out) 464 throws IOException 465 { 466 out.println("if (\"" + getEntityTargetType().getTable().getName() + "\".equals(table)) {"); 467 out.pushDepth(); 468 String loadVar = "__caucho_loadMask_" + (_targetLoadIndex / 64); 469 out.println(loadVar + " = 0;"); 470 out.popDepth(); 471 out.println("}"); 472 } 473 } 474 | Popular Tags |