1 28 29 package com.caucho.amber.field; 30 31 import com.caucho.amber.expr.AmberExpr; 32 import com.caucho.amber.expr.KeyManyToOneExpr; 33 import com.caucho.amber.expr.PathExpr; 34 import com.caucho.amber.query.QueryParser; 35 import com.caucho.amber.table.Column; 36 import com.caucho.amber.table.ForeignColumn; 37 import com.caucho.amber.table.LinkColumns; 38 import com.caucho.amber.type.EntityType; 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 55 public class KeyManyToOneField extends EntityManyToOneField implements IdField { 56 private static final L10N L = new L10N(KeyManyToOneField.class); 57 protected static final Logger log = Log.open(KeyManyToOneField.class); 58 59 private ArrayList <KeyPropertyField> _idFields = 61 new ArrayList <KeyPropertyField>(); 62 63 private boolean _isKeyField; 65 66 public KeyManyToOneField(RelatedType entityType, String name) 67 throws ConfigException 68 { 69 super(entityType, name); 70 } 71 72 public KeyManyToOneField(RelatedType entityType, 73 String name, 74 LinkColumns columns) 75 throws ConfigException 76 { 77 super(entityType, name); 78 79 setLinkColumns(columns); 80 81 setSourceCascadeDelete(true); 82 } 83 84 87 public String getGenerator() 88 { 89 return null; 90 } 91 92 96 public EntityType getEntityType() 97 { 98 return (EntityType) getEntityTargetType(); 99 } 100 101 public Type getType() 102 { 103 return getEntityTargetType(); 104 } 105 106 public Column getColumn() 107 { 108 throw new UnsupportedOperationException (); 109 } 110 111 114 public void setKeyField(boolean isKeyField) 115 { 116 _isKeyField = isKeyField; 117 } 118 119 122 public String getForeignTypeName() 123 { 124 return getJavaTypeName(); 125 } 126 127 130 public boolean isTargetCascadeDelete() 131 { 132 return false; 133 } 134 135 138 public boolean isSourceCascadeDelete() 139 { 140 return true; 141 } 142 143 146 public void init() 147 throws ConfigException 148 { 149 super.init(); 150 151 ArrayList <IdField> keys = getEntityTargetType().getId().getKeys(); 152 153 ArrayList <ForeignColumn> columns = getLinkColumns().getColumns(); 154 155 for (int i = 0; i < keys.size(); i++) { 156 IdField key = keys.get(i); 157 ForeignColumn column = columns.get(i); 158 159 KeyPropertyField field; 160 field = new IdentifyingKeyPropertyField(getEntitySourceType(), column); 161 162 _idFields.add(field); 163 } 164 } 165 166 169 public int getComponentCount() 170 { 171 return getEntityTargetType().getId().getKeyCount(); 172 } 173 174 177 public ArrayList <Column> getColumns() 178 { 179 ArrayList <Column> columns = new ArrayList <Column>(); 180 181 columns.addAll(getLinkColumns().getColumns()); 182 183 return columns; 184 } 185 186 189 public KeyPropertyField getIdField(IdField field) 190 { 191 ArrayList <IdField> keys = getEntityTargetType().getId().getKeys(); 192 193 if (_idFields.size() != keys.size()) { 194 try { 195 init(); 196 } catch (Exception e) { 197 throw new RuntimeException (e); 198 } 199 } 200 201 for (int i = 0; i < keys.size(); i++) { 202 if (keys.get(i) == field) 203 return _idFields.get(i); 204 } 205 206 throw new IllegalStateException (field.toString()); 207 } 208 209 212 public AmberExpr createExpr(QueryParser parser, PathExpr parent) 213 { 214 return new KeyManyToOneExpr(parent, this); 215 } 216 217 220 public String generateMatchArgWhere(String id) 221 { 222 return getLinkColumns().generateMatchArgSQL(id); 223 } 224 225 228 public String generateRawWhere(String id) 229 { 230 CharBuffer cb = new CharBuffer(); 231 232 String prefix = id + "." + getName(); 233 234 ArrayList <IdField> keys = getEntityTargetType().getId().getKeys(); 235 236 for (int i = 0; i < keys.size(); i++) { 237 if (i != 0) 238 cb.append(" and "); 239 240 cb.append(keys.get(i).generateRawWhere(prefix)); 241 } 242 243 return cb.toString(); 244 } 245 246 251 public String generateGetProxyProperty(String value) 252 { 253 throw new UnsupportedOperationException (); 254 } 255 256 259 public void generateJoin(CharBuffer cb, String table1, String table2) 260 { 261 cb.append(getLinkColumns().generateJoin(table1, table2)); 262 } 263 264 267 public ForeignColumn getColumn(Column key) 268 { 269 return getLinkColumns().getSourceColumn(key); 270 } 271 272 275 public int generateLoadForeign(JavaWriter out, String rs, 276 String indexVar, int index) 277 throws IOException 278 { 279 return generateLoadForeign(out, rs, indexVar, index, 280 getForeignTypeName().replace('.', '_')); 281 } 282 283 286 public String generateSuperGetter() 287 { 288 if (isAbstract() || getGetterMethod() == null) 289 return getFieldName(); 290 else 291 return getGetterMethod().getName() + "()"; 292 } 293 294 297 public String generateSuperSetter(String value) 298 { 299 if (isAbstract() || getGetterMethod() == null || getSetterMethod() == null) 300 return(getFieldName() + " = " + value + ";"); 301 else 302 return getSetterMethod().getName() + "(" + value + ")"; 303 } 304 305 308 public int generateLoadForeign(JavaWriter out, String rs, 309 String indexVar, int index, 310 String name) 311 throws IOException 312 { 313 out.print("(" + getForeignTypeName() + ") "); 314 315 out.print("aConn.loadProxy(\"" + getEntityTargetType().getName() + "\", "); 316 index = getEntityTargetType().getId().generateLoadForeign(out, rs, indexVar, index, 317 getName()); 318 319 out.println(");"); 320 321 return index; 322 } 323 324 327 public void generatePrologue(JavaWriter out, HashSet <Object > completedSet) 328 throws IOException 329 { 330 super.generatePrologue(out, completedSet); 331 332 if (isAbstract()) { 333 out.println(); 334 335 out.println(); 336 out.println("public " + getJavaTypeName() + " " + getGetterName() + "()"); 337 out.println("{"); 338 out.println(" return " + getFieldName() + ";"); 339 out.println("}"); 340 341 out.println(); 342 out.println("public void " + getSetterName() + "(" + getJavaTypeName() + " v)"); 343 out.println("{"); 344 out.println(" " + getFieldName() + " = v;"); 345 out.println("}"); 346 } 347 } 348 349 352 public void generateSet(JavaWriter out, String pstmt, 353 String index, String value) 354 throws IOException 355 { 356 ArrayList <ForeignColumn> columns = getLinkColumns().getColumns(); 357 358 Id id = getEntityTargetType().getId(); 359 ArrayList <IdField> keys = id.getKeys(); 360 361 String prop = value != null ? generateGet(value) : null; 362 for (int i = 0; i < columns.size(); i++ ){ 363 IdField key = keys.get(i); 364 ForeignColumn column = columns.get(i); 365 366 column.generateSet(out, pstmt, index, key.generateGet(prop)); 367 } 368 } 369 370 373 public void generateSet(JavaWriter out, String pstmt, String index) 374 throws IOException 375 { 376 String var = getFieldName(); 377 378 Id id = getEntityTargetType().getId(); 379 ArrayList <IdField> keys = id.getKeys(); 380 381 for (int i = 0; i < keys.size(); i++) { 382 IdField key = keys.get(i); 383 384 key.getType().generateSet(out, pstmt, index, key.generateGet(var)); 385 } 386 } 387 388 391 public void generateSetInsert(JavaWriter out, String pstmt, String index) 392 throws IOException 393 { 394 String value = generateSuperGetter(); 395 396 out.println("if (" + getEntityTargetType().generateIsNull(value) + ") {"); 397 out.pushDepth(); 398 399 getEntityTargetType().generateSetNull(out, pstmt, index); 400 401 out.popDepth(); 402 out.println("} else {"); 403 out.pushDepth(); 404 405 generateSet(out, pstmt, index); 406 407 out.popDepth(); 408 out.println("}"); 409 } 410 411 414 public String generateSetKeyProperty(String key, String value) 415 throws IOException 416 { 417 if (_isKeyField) 418 return key + "." + getName() + " = " + value; 419 else 420 return generateSet(key, value); 421 } 422 423 426 public String generateGetKeyProperty(String key) 427 throws IOException 428 { 429 if (_isKeyField) 430 return key + "." + getName(); 431 else 432 return generateGet(key); 433 } 434 435 438 public void generateSetGeneratedKeys(JavaWriter out, String pstmt) 439 throws IOException 440 { 441 } 442 443 446 public void generateCheckCreateKey(JavaWriter out) 447 throws IOException 448 { 449 out.println("if (" + generateSuperGetter() + " == null)"); 450 out.println(" throw new com.caucho.amber.AmberException(\"primary key must not be null on creation. " + getGetterName() + "() must not return null.\");"); 451 } 452 453 456 public String generateIsNull(String value) 457 { 458 return "(" + value + " == null)"; 459 } 460 461 464 public String toValue(String value) 465 { 466 return "((" + getJavaTypeName() + ") " + value + ")"; 467 } 468 } 469 | Popular Tags |