pepness commented on code in PR #5444:
URL: https://github.com/apache/netbeans/pull/5444#discussion_r1102925584


##########
java/j2ee.persistence/src/org/netbeans/modules/j2ee/persistence/wizard/jpacontroller/JpaControllerGenerator.java:
##########
@@ -226,488 +219,501 @@ private static FileObject generateJpaController(
                     return controllerFileObject;//don't need npe later
                 }
             }
-            controllerJavaSource.runModificationTask(new Task<WorkingCopy>() {
-            @Override
-                public void run(WorkingCopy workingCopy) throws IOException {
-                    workingCopy.toPhase(JavaSource.Phase.RESOLVED);
-
-                    ExecutableElement idGetterElement = 
idGetter.resolve(workingCopy);
-                    idGetterName[0] = 
idGetterElement.getSimpleName().toString();
-                    TypeMirror idType = idGetterElement.getReturnType();
-                    TypeElement idClass = null;
-                    if (TypeKind.DECLARED == idType.getKind()) {
-                        DeclaredType declaredType = (DeclaredType) idType;
-                        idClass = (TypeElement) declaredType.asElement();
-                        embeddable[0] = idClass != null && 
JpaControllerUtil.isEmbeddableClass(idClass);
-                        idPropertyType[0] = 
idClass.getQualifiedName().toString();
-                        if(!embeddable[0] && 
JpaControllerUtil.haveId(idClass)){//NOI18N
-                            //handle derived id, case entity/relationship 
without composite keys
-                             derived[0] =  
JpaControllerUtil.isRelationship(idGetterElement 
,JpaControllerUtil.isFieldAccess(idClass)) != JpaControllerUtil.REL_NONE;
-                             if(derived[0]){
-                                ExecutableElement derivedIdGetterElement  = 
findPrimaryKeyGetter(workingCopy, idClass);
-                                derivedIdGetterName[0] = 
derivedIdGetterElement.getSimpleName().toString();
-                                TypeMirror derivedIdType = 
derivedIdGetterElement.getReturnType();
-                                TypeElement derivedIdClass;
-                                if (TypeKind.DECLARED == idType.getKind()) {
-                                    DeclaredType derivedDeclaredType = 
(DeclaredType) derivedIdType;
-                                    derivedIdClass = (TypeElement) 
derivedDeclaredType.asElement();
-                                    derivedIdPropertyType[0] = 
derivedIdClass.getQualifiedName().toString();
-                                 }
+            controllerJavaSource.runModificationTask( (WorkingCopy 
workingCopy) -> {
+                workingCopy.toPhase(JavaSource.Phase.RESOLVED);
+                
+                ExecutableElement idGetterElement = 
idGetter.resolve(workingCopy);
+                idGetterName[0] = idGetterElement.getSimpleName().toString();
+                TypeMirror idType = idGetterElement.getReturnType();
+                TypeElement idClass = null;
+                if (null == idType.getKind()) {
+                    //instead of throwing exceptions later, just use Object
+                    idPropertyType[0] = "java.lang.Object";//NOI18N
+                } else {
+                    switch (idType.getKind()) {
+                        case DECLARED:
+                            DeclaredType declaredType = (DeclaredType) idType;
+                            idClass = (TypeElement) declaredType.asElement();
+                            embeddable[0] = idClass != null && 
JpaControllerUtil.isEmbeddableClass(idClass);
+                            idPropertyType[0] = 
idClass.getQualifiedName().toString();
+                            if(!embeddable[0] && 
JpaControllerUtil.haveId(idClass)){//NOI18N
+                                //handle derived id, case entity/relationship 
without composite keys
+                                derived[0] =  
JpaControllerUtil.isRelationship(idGetterElement 
,JpaControllerUtil.isFieldAccess(idClass)) != JpaControllerUtil.REL_NONE;
+                                if(derived[0]){
+                                    ExecutableElement derivedIdGetterElement  
= findPrimaryKeyGetter(workingCopy, idClass);
+                                    derivedIdGetterName[0] = 
derivedIdGetterElement.getSimpleName().toString();
+                                    TypeMirror derivedIdType = 
derivedIdGetterElement.getReturnType();
+                                    TypeElement derivedIdClass;
+                                    if (TypeKind.DECLARED == idType.getKind()) 
{
+                                        DeclaredType derivedDeclaredType = 
(DeclaredType) derivedIdType;
+                                        derivedIdClass = (TypeElement) 
derivedDeclaredType.asElement();
+                                        derivedIdPropertyType[0] = 
derivedIdClass.getQualifiedName().toString();
+                                    }
+                                }
                             }
-                        }
-                    } else if (TypeKind.BOOLEAN == idType.getKind()) {
-                        idPropertyType[0] = "boolean";//NOI18N
-                    } else if (TypeKind.BYTE == idType.getKind()) {
-                        idPropertyType[0] = "byte";//NOI18N
-                    } else if (TypeKind.CHAR == idType.getKind()) {
-                        idPropertyType[0] = "char";//NOI18N
-                    } else if (TypeKind.DOUBLE == idType.getKind()) {
-                        idPropertyType[0] = "double";//NOI18N
-                    } else if (TypeKind.FLOAT == idType.getKind()) {
-                        idPropertyType[0] = "float";//NOI18N
-                    } else if (TypeKind.INT == idType.getKind()) {
-                        idPropertyType[0] = "int";//NOI18N
-                    } else if (TypeKind.LONG == idType.getKind()) {
-                        idPropertyType[0] = "long";//NOI18N
-                    } else if (TypeKind.SHORT == idType.getKind()) {
-                        idPropertyType[0] = "short";//NOI18N
-                    } else {
-                        //instead of throwing exceptions later, just use Object
-                        idPropertyType[0] = "java.lang.Object";//NOI18N
+                            break;
+                        case BOOLEAN:
+                            idPropertyType[0] = "boolean";//NOI18N
+                            break;
+                        case BYTE:
+                            idPropertyType[0] = "byte";//NOI18N
+                            break;
+                        case CHAR:
+                            idPropertyType[0] = "char";//NOI18N
+                            break;
+                        case DOUBLE:
+                            idPropertyType[0] = "double";//NOI18N
+                            break;
+                        case FLOAT:
+                            idPropertyType[0] = "float";//NOI18N
+                            break;
+                        case INT:
+                            idPropertyType[0] = "int";//NOI18N
+                            break;
+                        case LONG:
+                            idPropertyType[0] = "long";//NOI18N
+                            break;
+                        case SHORT:
+                            idPropertyType[0] = "short";//NOI18N
+                            break;
+                        default:
+                            //instead of throwing exceptions later, just use 
Object
+                            idPropertyType[0] = "java.lang.Object";//NOI18N
+                            break;
                     }
+                }
+                String simpleIdPropertyType = 
JpaControllerUtil.simpleClassName(idPropertyType[0]);
+                
+                TypeElement controllerTypeElement = 
SourceUtils.getPublicTopLevelElement(workingCopy);
+                ClassTree classTree = 
workingCopy.getTrees().getTree(controllerTypeElement);
+                ClassTree modifiedClassTree = classTree;
+                
+                int privateModifier = java.lang.reflect.Modifier.PRIVATE;
+                int publicModifier = java.lang.reflect.Modifier.PUBLIC;
+                
+                CompilationUnitTree modifiedImportCut = null;
+                
+                List<String> parameterTypes = new ArrayList<>();
+                List<String> parameterNames = new ArrayList<>();
+                String body = "";   //NOI18N
+                boolean isUserTransaction = 
workingCopy.getClasspathInfo().getClassPath(ClasspathInfo.PathKind.COMPILE).findResource("javax/transaction/UserTransaction.class")!=null;
  //NOI18N
+                if (isUserTransaction && isInjection) {
+                    modifiedClassTree = 
JpaControllerUtil.TreeMakerUtils.addVariable(modifiedClassTree, workingCopy, 
"utx", "javax.transaction.UserTransaction", privateModifier, null, null);   
//NOI18N
+                    parameterTypes.add("javax.transaction.UserTransaction");   
//NOI18N
+                    parameterNames.add("utx");   //NOI18N
+                    body = "this.utx = utx;\n";   //NOI18N
                     
-                    String simpleIdPropertyType = 
JpaControllerUtil.simpleClassName(idPropertyType[0]);
-                         
-                    TypeElement controllerTypeElement = 
SourceUtils.getPublicTopLevelElement(workingCopy);
-                    ClassTree classTree = 
workingCopy.getTrees().getTree(controllerTypeElement);
-                    ClassTree modifiedClassTree = classTree;
-                    
-                    int privateModifier = java.lang.reflect.Modifier.PRIVATE;
-                    int publicModifier = java.lang.reflect.Modifier.PUBLIC;
-                    
-                    CompilationUnitTree modifiedImportCut = null;
-                    
-                    List<String> parameterTypes = new ArrayList<String>();
-                    List<String> parameterNames = new ArrayList<String>();
-                    String body = "";   //NOI18N
-                    boolean isUserTransaction = 
workingCopy.getClasspathInfo().getClassPath(ClasspathInfo.PathKind.COMPILE).findResource("javax/transaction/UserTransaction.class")!=null;
  //NOI18N
-                    if (isUserTransaction && isInjection) {
-                        modifiedClassTree = 
JpaControllerUtil.TreeMakerUtils.addVariable(modifiedClassTree, workingCopy, 
"utx", "javax.transaction.UserTransaction", privateModifier, null, null);   
//NOI18N
-                        
parameterTypes.add("javax.transaction.UserTransaction");   //NOI18N
-                        parameterNames.add("utx");   //NOI18N
-                        body = "this.utx = utx;\n";   //NOI18N
-
-                    }
-                    modifiedClassTree = 
JpaControllerUtil.TreeMakerUtils.addVariable(modifiedClassTree, workingCopy, 
"emf", "javax.persistence.EntityManagerFactory", privateModifier, null, null);  
 //NOI18N
-                    
parameterTypes.add("javax.persistence.EntityManagerFactory");   //NOI18N
-                    parameterNames.add("emf");   //NOI18N
-                    body += "this.emf = emf;";   //NOI18N
-                    MethodInfo mi = new MethodInfo("<init>", publicModifier, 
"void", null, parameterTypes.toArray(new String[parameterTypes.size()]),   
//NOI18N
-                                                    parameterNames.toArray(new 
String[parameterNames.size()]), body, null, null);
-                    modifiedClassTree = 
JpaControllerUtil.TreeMakerUtils.modifyDefaultConstructor(classTree, 
modifiedClassTree, workingCopy, mi);
-                    
-                    MethodInfo methodInfo = new MethodInfo("getEntityManager", 
publicModifier, "javax.persistence.EntityManager", null, null, null, "return 
emf.createEntityManager();", null, null);
-                    modifiedClassTree = 
JpaControllerUtil.TreeMakerUtils.addMethod(modifiedClassTree, workingCopy, 
methodInfo);
-
-                    String bodyText;
-                    StringBuilder updateRelatedInCreate = new StringBuilder();
-                    StringBuilder updateRelatedInEditPre = new StringBuilder();
-                    StringBuilder attachRelatedInEdit = new StringBuilder();
-                    StringBuilder updateRelatedInEditPost = new 
StringBuilder();
-                    StringBuilder updateRelatedInDestroy = new StringBuilder();
-                    StringBuilder initRelatedInCreate = new StringBuilder();
-                    StringBuilder illegalOrphansInCreate = new StringBuilder();
-                    StringBuilder illegalOrphansInEdit = new StringBuilder();
-                    StringBuilder illegalOrphansInDestroy = new 
StringBuilder();
-                    StringBuilder initCollectionsInCreate = new 
StringBuilder();  //useful in case user removes listbox from New.jsp
-
-                    List<ElementHandle<ExecutableElement>> allRelMethods = new 
ArrayList<ElementHandle<ExecutableElement>>(toOneRelMethods);
-                    allRelMethods.addAll(toManyRelMethods);
-                    
-                    String[] importFqs = {"javax.persistence.Query",
-                                "javax.persistence.EntityNotFoundException"    
                          
-                    };
+                }
+                modifiedClassTree = 
JpaControllerUtil.TreeMakerUtils.addVariable(modifiedClassTree, workingCopy, 
"emf", "javax.persistence.EntityManagerFactory", privateModifier, null, null);  
 //NOI18N
+                parameterTypes.add("javax.persistence.EntityManagerFactory");  
 //NOI18N
+                parameterNames.add("emf");   //NOI18N
+                body += "this.emf = emf;";   //NOI18N
+                MethodInfo mi = new MethodInfo("<init>", publicModifier, 
"void", null, parameterTypes.toArray(new String[0]),   //NOI18N
+                parameterNames.toArray(new String[0]), body, null, null);
+                modifiedClassTree = 
JpaControllerUtil.TreeMakerUtils.modifyDefaultConstructor(classTree, 
modifiedClassTree, workingCopy, mi);
+                
+                MethodInfo methodInfo = new MethodInfo("getEntityManager", 
publicModifier, "javax.persistence.EntityManager", null, null, null, "return 
emf.createEntityManager();", null, null);
+                modifiedClassTree = 
JpaControllerUtil.TreeMakerUtils.addMethod(modifiedClassTree, workingCopy, 
methodInfo);
+                
+                String bodyText;
+                StringBuilder updateRelatedInCreate = new StringBuilder();
+                StringBuilder updateRelatedInEditPre = new StringBuilder();
+                StringBuilder attachRelatedInEdit = new StringBuilder();
+                StringBuilder updateRelatedInEditPost = new StringBuilder();
+                StringBuilder updateRelatedInDestroy = new StringBuilder();
+                StringBuilder initRelatedInCreate = new StringBuilder();
+                StringBuilder illegalOrphansInCreate = new StringBuilder();
+                StringBuilder illegalOrphansInEdit = new StringBuilder();
+                StringBuilder illegalOrphansInDestroy = new StringBuilder();
+                StringBuilder initCollectionsInCreate = new StringBuilder();  
//useful in case user removes listbox from New.jsp
+                
+                List<ElementHandle<ExecutableElement>> allRelMethods = new 
ArrayList<>(toOneRelMethods);
+                allRelMethods.addAll(toManyRelMethods);
+                
+                String[] importFqs = {"javax.persistence.Query",
+                    "javax.persistence.EntityNotFoundException"
+                };
+                
+                for (String importFq : importFqs) {
+                    modifiedImportCut = 
JpaControllerUtil.TreeMakerUtils.createImport(workingCopy, modifiedImportCut, 
importFq);
+                }
+                if(version!=null && 
!Persistence.VERSION_1_0.equals(version)){//add criteria classes if appropriate

Review Comment:
   Thanks for the suggestions, I will create another PR with better formatting, 
meanwhile lets get this merged.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists

Reply via email to