1 19 package org.netbeans.modules.j2ee.ejbcore.api.methodcontroller; 20 21 import org.netbeans.modules.j2ee.common.method.MethodModel; 22 import java.io.IOException ; 23 import java.rmi.RemoteException ; 24 import java.util.Collections ; 25 import java.util.HashSet ; 26 import java.util.LinkedList ; 27 import java.util.List ; 28 import java.util.Set ; 29 import javax.lang.model.element.ExecutableElement; 30 import javax.lang.model.element.Modifier; 31 import org.netbeans.modules.j2ee.dd.api.ejb.CmpField; 32 import org.netbeans.modules.j2ee.dd.api.ejb.CmrField; 33 import org.netbeans.modules.j2ee.dd.api.ejb.EjbJar; 34 import org.netbeans.modules.j2ee.dd.api.ejb.EjbRelation; 35 import org.netbeans.modules.j2ee.dd.api.ejb.EjbRelationshipRole; 36 import org.netbeans.modules.j2ee.dd.api.ejb.Entity; 37 import org.netbeans.modules.j2ee.dd.api.ejb.MethodParams; 38 import org.netbeans.modules.j2ee.dd.api.ejb.Query; 39 import org.netbeans.modules.j2ee.dd.api.ejb.QueryMethod; 40 import org.netbeans.modules.j2ee.dd.api.ejb.Relationships; 41 import org.openide.ErrorManager; 42 import org.openide.filesystems.FileObject; 43 44 49 public final class EntityMethodController extends AbstractMethodController { 50 51 private final FileObject ejbClassFO; 52 private final Entity model; 53 private final EjbJar parent; 54 private final Set <Modifier> modifiersPublicAbstract = new HashSet <Modifier>(2); 55 56 public EntityMethodController(FileObject ejbClassFO, Entity model, EjbJar parent) { 57 super(ejbClassFO, model); 58 this.ejbClassFO= ejbClassFO; 59 this.model = model; 60 this.parent = parent; 61 modifiersPublicAbstract.add(Modifier.PUBLIC); 62 modifiersPublicAbstract.add(Modifier.ABSTRACT); 63 } 64 65 public Entity getModelCopy() { 66 return (Entity) model.clone(); 67 } 68 69 public List getMethods(CmpField field) { 70 return getMethods(field.getFieldName()); 71 } 72 73 public List getMethods(CmrField field) { 74 return getMethods(field.getCmrFieldName()); 75 } 76 77 public void deleteQueryMapping(ExecutableElement method, FileObject ddFileObject) throws IOException { 78 Query[] queries = model.getQuery(); 79 for (Query query : queries) { 80 String methodName = query.getQueryMethod().getMethodName(); 81 if (method.getSimpleName().contentEquals(methodName)) { 82 model.removeQuery(query); 83 parent.write(ddFileObject); 84 return; 85 } 86 } 87 } 88 89 103 public void deleteField(CmpField field, FileObject ddFileObject) throws IOException { 104 Query query = null; 105 String fieldName = field.getFieldName(); 106 query = findQueryByCmpField(fieldName); 108 for (String clazz : getLocalInterfaces()) { 110 MethodModel method = getFinderMethod(clazz, fieldName, getGetterMethod(getBeanClass(), fieldName)); 111 if (method != null) { 112 removeMethodFromClass(clazz, method); 113 } 114 } 115 for (String clazz : getRemoteInterfaces()) { 117 MethodModel method = getFinderMethod(clazz, fieldName, getGetterMethod(getBeanClass(), fieldName)); 118 if (method != null) { 119 removeMethodFromClass(clazz, method); 120 } 121 } 122 removeMethodsFromBean(getMethods(fieldName)); 123 updateFieldAccessors(fieldName, false, false, false, false); 124 if (query != null) { 126 model.removeQuery(query); 127 } 128 model.removeCmpField(field); 130 parent.write(ddFileObject); 131 } 132 133 private Query findQueryByCmpField(String fieldName) { 134 Query[] queries = model.getQuery(); 135 for (Query query : queries) { 136 String queryMethodName = query.getQueryMethod().getMethodName(); 137 if (prependAndUpper(fieldName, "findBy").equals(queryMethodName)) { 138 return query; 139 } 140 } 141 return null; 142 } 143 144 private void removeMethodsFromBean(List <MethodModel> methods) throws IOException { 145 for (MethodModel method : methods) { 146 if (hasLocal()) { 148 ClassMethodPair classMethodPair = getInterface(method, true); 149 if (classMethodPair != null) { 150 removeMethodFromClass(classMethodPair.getClassName(), classMethodPair.getMethodModel()); 151 } 152 } 153 if (hasRemote()) { 154 ClassMethodPair classMethodPair = getInterface(method, false); 155 if (classMethodPair != null) { 156 removeMethodFromClass(classMethodPair.getClassName(), classMethodPair.getMethodModel()); 157 } 158 } 159 removeMethodFromClass(getBeanClass(), method); 161 } 162 } 163 164 175 public void deleteField(CmrField field, FileObject ddFileObject) throws IOException { 176 List <MethodModel> methods = getMethods(field.getCmrFieldName()); 177 removeMethodsFromBean(methods); 178 deleteRelationships(field.getCmrFieldName()); 180 parent.write(ddFileObject); 181 } 182 183 public static String getMethodName(String fieldName, boolean get) { 184 String prefix = get ? "get" : "set"; return prependAndUpper(fieldName, prefix); 186 } 187 188 199 @Override 200 public boolean hasJavaImplementation(MethodModel intfView) { 201 return hasJavaImplementation(getMethodTypeFromInterface(intfView)); 202 } 203 204 @Override 205 public boolean hasJavaImplementation(MethodType methodType) { 206 return !(isCMP() && (isFinder(methodType.getKind()) || isSelect(methodType.getKind()))); 207 } 208 209 @Override 210 public MethodType getMethodTypeFromImpl(MethodModel implView) { 211 MethodType methodType = null; 212 if (implView.getName().startsWith("ejbCreate") || implView.getName().startsWith("ejbPostCreate")) { methodType = new MethodType.CreateMethodType(implView); 214 } else if (!implView.getName().startsWith("ejb")) { methodType = new MethodType.BusinessMethodType(implView); 216 } else if (implView.getName().startsWith("ejbFind")) { methodType = new MethodType.FinderMethodType(implView); 218 } else if (implView.getName().startsWith("ejbHome")) { methodType = new MethodType.HomeMethodType(implView); 220 } 221 return methodType; 222 } 223 224 @Override 225 public MethodType getMethodTypeFromInterface(MethodModel clientView) { 226 MethodType methodType; 227 if (findInClass(model.getLocalHome(), clientView) || findInClass(model.getHome(), clientView)) { 228 if (clientView.getName().startsWith("create")) { methodType = new MethodType.CreateMethodType(clientView); 230 } else if (clientView.getName().startsWith("find")) { methodType = new MethodType.FinderMethodType(clientView); 232 } else { 233 methodType = new MethodType.HomeMethodType(clientView); 234 } 235 } else { 236 methodType = new MethodType.BusinessMethodType(clientView); 237 } 238 return methodType; 239 } 240 241 public AbstractMethodController.GenerateFromImpl createGenerateFromImpl() { 242 return new EntityGenerateFromImplVisitor(); 243 } 244 245 public AbstractMethodController.GenerateFromIntf createGenerateFromIntf() { 246 return new EntityGenerateFromIntfVisitor(ejbClassFO, model); 247 } 248 249 public void addSelectMethod(MethodModel selectMethod, String ejbql, FileObject ddFileObject) throws IOException { 250 addMethodToClass(getBeanClass(), selectMethod); 251 addEjbQl(selectMethod, ejbql, ddFileObject); 252 } 253 254 public void addEjbQl(MethodModel clientView, String ejbql, FileObject ddFileObject) throws IOException { 255 if (isBMP()) { 256 super.addEjbQl(clientView, ejbql, ddFileObject); 257 } 258 model.addQuery(buildQuery(clientView, ejbql)); 259 parent.write(ddFileObject); 260 } 261 262 public void addField(MethodModel.Variable field, FileObject ddFile, boolean localGetter, boolean localSetter, 263 boolean remoteGetter, boolean remoteSetter, String description) throws IOException { 264 String beanClass = getBeanClass(); 265 addSetterMethod(beanClass, field, modifiersPublicAbstract, false, model); 266 addGetterMethod(beanClass, field, modifiersPublicAbstract, false, model); 267 final String fieldName = field.getName(); 268 updateFieldAccessors(fieldName, localGetter, localSetter, remoteGetter, remoteSetter); 269 CmpField cmpField = model.newCmpField(); 270 cmpField.setFieldName(field.getName()); 271 cmpField.setDescription(description); 272 model.addCmpField(cmpField); 273 parent.write(ddFile); 274 } 275 276 private MethodModel addSetterMethod(String javaClass, MethodModel.Variable field, Set <Modifier> modifiers, boolean remote, Entity entity) { 277 MethodModel method = createSetterMethod(javaClass, field, modifiers, remote); 278 addMethod(javaClass, method, entity); 279 return method; 280 } 281 282 private MethodModel addGetterMethod(String javaClass, MethodModel.Variable variable, Set <Modifier> modifiers, boolean remote, Entity entity) { 283 MethodModel method = createGetterMethod(javaClass, variable, modifiers, remote); 284 addMethod(javaClass, method, entity); 285 return method; 286 } 287 288 private void addMethod(String javaClass, MethodModel method, Entity entity) { 289 try { 291 addMethodToClass(javaClass, method); 292 } catch (IOException e) { 293 ErrorManager.getDefault().notify(e); 294 } 295 296 } 319 320 private MethodModel createGetterMethod(String javaClass, MethodModel.Variable field, Set <Modifier> modifiers, boolean remote) { 321 final String fieldName = field.getName(); 322 List <String > exceptions = remote ? Collections.<String >singletonList(RemoteException .class.getName()) : Collections.<String >emptyList(); 323 MethodModel method = MethodModel.create( 324 getMethodName(fieldName, true), 325 "void", 326 "", 327 Collections.singletonList(field), 328 exceptions, 329 modifiers 330 ); 331 return method; 332 } 333 334 private MethodModel createSetterMethod(String javaClass, MethodModel.Variable field, Set <Modifier> modifiers, boolean remote) { 335 final String fieldName = field.getName(); 336 List <String > exceptions = remote ? Collections.<String >singletonList(RemoteException .class.getName()) : Collections.<String >emptyList(); 337 MethodModel method = MethodModel.create( 338 getMethodName(fieldName, false), 339 "void", 340 "", 341 Collections.singletonList(field), 342 exceptions, 343 modifiers 344 ); 345 return method; 346 } 347 348 private boolean isBMP() { 349 return Entity.PERSISTENCE_TYPE_BEAN.equals(model.getPersistenceType()); 350 } 351 352 public boolean isCMP() { 353 return !isBMP(); 354 } 355 356 private boolean isFinder(MethodType.Kind methodType) { 357 return methodType == MethodType.Kind.FINDER; 358 } 359 360 private boolean isSelect(MethodType.Kind methodType) { 361 return methodType == MethodType.Kind.SELECT; 362 } 363 364 public String createDefaultQL(MethodType methodType) { 365 String ejbql = null; 366 if (isFinder(methodType.getKind()) && isCMP()) { 367 ejbql = "SELECT OBJECT(o) \nFROM " + model.getAbstractSchemaName() + " o"; 368 } 369 370 if (isSelect(methodType.getKind())) { 371 ejbql = "SELECT COUNT(o) \nFROM " + model.getAbstractSchemaName() + " o"; 372 } 373 374 return ejbql; 375 } 376 377 private Query buildQuery(MethodModel clientView, String ejbql) { 378 Query query = model.newQuery(); 379 QueryMethod queryMethod = query.newQueryMethod(); 380 queryMethod.setMethodName(clientView.getName()); 381 MethodParams mParams = queryMethod.newMethodParams(); 382 for (MethodModel.Variable parameter : clientView.getParameters()) { 383 mParams.addMethodParam(parameter.getType()); 384 } 385 queryMethod.setMethodParams(mParams); 386 query.setQueryMethod(queryMethod); 387 query.setEjbQl(ejbql); 388 return query; 389 } 390 391 private static String prependAndUpper(String fullName, String prefix) { 392 StringBuffer buffer = new StringBuffer (fullName); 393 buffer.setCharAt(0, Character.toUpperCase(buffer.charAt(0))); 394 return prefix+buffer.toString(); 395 } 396 397 403 private boolean isEjbUsed(EjbRelationshipRole role, String ejbName, String fieldName) { 404 return role != null && 405 role.getRelationshipRoleSource() != null && 406 ejbName.equals(role.getRelationshipRoleSource().getEjbName()) && 407 fieldName.equals(role.getCmrField().getCmrFieldName()); 408 } 409 410 private boolean relationContainsField(EjbRelation relation, String ejbName, String fieldName) { 411 return 412 isEjbUsed(relation.getEjbRelationshipRole(), ejbName, fieldName) || 413 isEjbUsed(relation.getEjbRelationshipRole2(), ejbName, fieldName); 414 } 415 416 private void deleteRelationships(String fieldName) { 417 String ejbName = model.getEjbName(); 418 Relationships relationships = parent.getSingleRelationships(); 419 if (relationships != null) { 420 EjbRelation[] relations = relationships.getEjbRelation(); 421 if (relations != null) { 422 for (int i = 0; i < relations.length; i++) { 423 if (relationContainsField(relations[i], ejbName, fieldName)) { 424 boolean uniDirectional = false; 425 EjbRelationshipRole role = relations[i].getEjbRelationshipRole(); 426 if (isEjbUsed(role, ejbName, fieldName)) { 427 role.setCmrField(null); 428 } else { 429 uniDirectional = role.getCmrField()==null; 430 } 431 role = relations[i].getEjbRelationshipRole2(); 432 if (isEjbUsed(role, ejbName, fieldName)) { 433 role.setCmrField(null); 434 } else { 435 uniDirectional = role.getCmrField()==null; 436 } 437 if (uniDirectional) { 438 relationships.removeEjbRelation(relations[i]); 439 } 440 } 441 } 442 if (relationships.sizeEjbRelation() == 0) { 443 parent.setRelationships(null); 444 } 445 } 446 } 447 } 448 449 private List <MethodModel> getMethods(String propName) { 450 assert propName != null; 451 List <MethodModel> resultList = new LinkedList <MethodModel>(); 452 String ejbClass = getBeanClass(); 453 MethodModel getMethod = getGetterMethod(ejbClass, propName); 454 if (getMethod != null) { 455 resultList.add(getMethod); 456 MethodModel setMethod = getSetterMethod(ejbClass, propName, getMethod.getReturnType()); 457 if (setMethod != null) { 458 resultList.add(setMethod); 459 } 460 } 461 return resultList; 462 } 463 464 public MethodModel getGetterMethod(String javaClass, String fieldName) { 465 if (javaClass == null || fieldName == null) { 466 return null; 467 } 468 MethodModel method = MethodModel.create( 469 getMethodName(fieldName, true), 470 "void", 471 "", 472 Collections.<MethodModel.Variable>emptyList(), 473 Collections.<String >emptyList(), 474 Collections.<Modifier>emptySet() 475 ); 476 return findInClass(javaClass, method) ? method : null; 477 } 478 479 public MethodModel getGetterMethod(String fieldName, boolean local) { 480 return getGetterMethod(getBeanInterface(local, true), fieldName); 481 } 482 483 public MethodModel getSetterMethod(String classElement, String fieldName, String type) { 484 if (classElement == null) { 485 return null; 486 } 487 if (type == null) { 488 return null; 489 } 490 MethodModel method = MethodModel.create( 491 getMethodName(fieldName, true), 492 "void", 493 "", 494 Collections.singletonList(MethodModel.Variable.create(type, "arg0")), 495 Collections.<String >emptyList(), 496 Collections.<Modifier>emptySet() 497 ); 498 return findInClass(classElement, method) ? method : null; 499 } 500 501 public MethodModel getSetterMethod(String fieldName, boolean local) { 502 MethodModel.Variable field = getField(fieldName, true); 503 if (field == null) { 504 return null; 505 } else { 506 return getSetterMethod(getBeanInterface(local, true), fieldName, field.getType()); 507 } 508 } 509 510 517 public MethodModel getFinderMethod(String classElement, String fieldName, MethodModel getterMethod) { 518 if (getterMethod == null) { 519 return null; 520 } 521 MethodModel method = MethodModel.create( 522 getMethodName(fieldName, true), 523 "void", 524 "", 525 Collections.singletonList(MethodModel.Variable.create(getterMethod.getReturnType(), "arg0")), 526 Collections.<String >emptyList(), 527 Collections.<Modifier>emptySet() 528 ); 529 return findInClass(classElement, method) ? method : null; 530 } 531 532 @Override 533 public boolean supportsMethodType(MethodType.Kind methodType) { 534 return !isSelect(methodType) || isCMP(); 535 } 536 537 private void updateFieldAccessors(String fieldName, boolean localGetter, boolean localSetter, boolean remoteGetter, 538 boolean remoteSetter) { 539 updateFieldAccessor(fieldName, true, true, localGetter); 540 updateFieldAccessor(fieldName, false, true, localSetter); 541 updateFieldAccessor(fieldName, true, false, remoteGetter); 542 updateFieldAccessor(fieldName, false, false, remoteSetter); 543 } 544 545 public void updateFieldAccessor(String fieldName, boolean getter, boolean local, boolean shouldExist) { 546 MethodModel.Variable field = getField(fieldName, true); 547 if (field == null) { 548 return; 549 } 550 String businessInterface = getBeanInterface(local, true); 551 if (businessInterface != null) { 552 MethodModel method; 553 if (getter) { 554 method = getGetterMethod(businessInterface, fieldName); 555 } else { 556 method = getSetterMethod(businessInterface, fieldName, field.getType()); 557 } 558 if (shouldExist) { 559 if (method == null) { 560 if (getter) { 561 addGetterMethod(businessInterface, field, Collections.<Modifier>emptySet(), !local, model); 562 } else { 563 addSetterMethod(businessInterface, field, Collections.<Modifier>emptySet(), !local, model); 564 } 565 } 566 } else if (method != null) { 567 try { 568 removeMethodFromClass(businessInterface, method); 569 } catch (IOException e) { 570 ErrorManager.getDefault().notify(e); 571 } 572 573 } 574 } 575 } 576 577 private MethodModel.Variable getField(String fieldName, boolean create) { 578 String beanClass = getBeanClass(); 579 MethodModel getterMethod = getGetterMethod(beanClass, fieldName); 580 if (getterMethod == null) { 581 if (!create) { 582 return null; 583 } 584 MethodModel.Variable field = MethodModel.Variable.create(String .class.getName(), fieldName); 585 createGetterMethod(getBeanClass(), field, modifiersPublicAbstract, false); 586 return field; 587 } else { 588 String type = getterMethod.getReturnType(); 589 if (type == null) { 590 return null; 591 } else { 592 return MethodModel.Variable.create(type, fieldName); 593 } 594 } 595 } 596 597 } 598 | Popular Tags |