1 19 20 package org.netbeans.modules.j2ee.ejbcore.action; 21 22 import java.io.IOException ; 23 import java.util.ArrayList ; 24 import java.util.Collections ; 25 import java.util.List ; 26 import javax.lang.model.element.Modifier; 27 import org.netbeans.modules.j2ee.common.method.MethodModel; 28 import org.netbeans.modules.j2ee.dd.api.ejb.Entity; 29 import org.netbeans.modules.j2ee.dd.api.ejb.EntityAndSession; 30 import org.netbeans.modules.j2ee.dd.api.ejb.Session; 31 import org.netbeans.modules.j2ee.ejbcore._RetoucheUtil; 32 import org.openide.filesystems.FileObject; 33 34 38 public final class CreateMethodGenerator extends AbstractMethodGenerator { 39 40 private CreateMethodGenerator(EntityAndSession ejb, FileObject ejbClassFileObject) { 41 super(ejb, ejbClassFileObject, null); 42 } 43 44 public static CreateMethodGenerator create(EntityAndSession ejb, FileObject ejbClassFileObject) { 45 return new CreateMethodGenerator(ejb, ejbClassFileObject); 46 } 47 48 public void generate(MethodModel methodModel, boolean generateLocal, boolean generateRemote) throws IOException { 49 if (ejb instanceof Session) { 50 generateSession(methodModel, generateLocal, generateRemote); 51 } else if (ejb instanceof Entity) { 52 generateEntity(methodModel, generateLocal, generateRemote); 53 } 54 } 55 56 private void generateSession(MethodModel methodModel, boolean generateLocal, boolean generateRemote) throws IOException { 57 58 if (!methodModel.getName().startsWith("create")) { 59 throw new IllegalArgumentException ("The method name must have create as its prefix."); 60 } 61 62 if (generateLocal && ejb.getLocal() != null && ejb.getLocalHome() != null) { 64 List <String > exceptions = new ArrayList <String >(methodModel.getExceptions()); 65 if (!methodModel.getExceptions().contains("javax.ejb.CreateException")) { 66 exceptions.add("javax.ejb.CreateException"); 67 } 68 MethodModel methodModelCopy = MethodModel.create( 69 methodModel.getName(), 70 ejb.getLocal(), 71 null, 72 methodModel.getParameters(), 73 exceptions, 74 Collections.<Modifier>emptySet() 75 ); 76 FileObject fileObject = _RetoucheUtil.resolveFileObjectForClass(ejbClassFileObject, ejb.getLocalHome()); 77 addMethod(methodModelCopy, fileObject, ejb.getLocalHome()); 78 } 79 80 if (generateRemote && ejb.getRemote() != null && ejb.getHome() != null) { 82 List <String > exceptions = exceptions = new ArrayList <String >(methodModel.getExceptions()); 83 if (!methodModel.getExceptions().contains("javax.ejb.CreateException")) { 84 exceptions.add("javax.ejb.CreateException"); 85 } 86 if (!methodModel.getExceptions().contains("java.rmi.RemoteException")) { 87 exceptions.add("java.rmi.RemoteException"); 88 } 89 MethodModel methodModelCopy = MethodModel.create( 90 methodModel.getName(), 91 ejb.getRemote(), 92 null, 93 methodModel.getParameters(), 94 exceptions, 95 Collections.<Modifier>emptySet() 96 ); 97 FileObject fileObject = _RetoucheUtil.resolveFileObjectForClass(ejbClassFileObject, ejb.getHome()); 98 addMethod(methodModelCopy, fileObject, ejb.getHome()); 99 } 100 101 List <String > exceptions = new ArrayList <String >(methodModel.getExceptions()); 103 if (!methodModel.getExceptions().contains("javax.ejb.CreateException")) { 104 exceptions.add("javax.ejb.CreateException"); 105 } 106 MethodModel methodModelCopy = MethodModel.create( 107 "ejbC" + methodModel.getName().substring(1), 108 methodModel.getReturnType(), 109 methodModel.getBody(), 110 methodModel.getParameters(), 111 exceptions, 112 Collections.singleton(Modifier.PUBLIC) 113 ); 114 addMethod(methodModelCopy, ejbClassFileObject, ejb.getEjbClass()); 115 116 } 117 118 private void generateEntity(MethodModel methodModel, boolean generateLocal, boolean generateRemote) throws IOException { 119 120 if (!methodModel.getName().startsWith("create")) { 121 throw new IllegalArgumentException ("The method name must have create as its prefix."); 122 } 123 124 if (generateLocal && ejb.getLocal() != null && ejb.getLocalHome() != null) { 126 List <String > exceptions = new ArrayList <String >(methodModel.getExceptions()); 127 if (!methodModel.getExceptions().contains("javax.ejb.CreateException")) { 128 exceptions.add("javax.ejb.CreateException"); 129 } 130 MethodModel methodModelCopy = MethodModel.create( 131 methodModel.getName(), 132 ejb.getLocal(), 133 null, 134 methodModel.getParameters(), 135 exceptions, 136 Collections.<Modifier>emptySet() 137 ); 138 FileObject fileObject = _RetoucheUtil.resolveFileObjectForClass(ejbClassFileObject, ejb.getLocalHome()); 139 addMethod(methodModelCopy, fileObject, ejb.getLocalHome()); 140 141 } 142 143 if (generateRemote && ejb.getRemote() != null && ejb.getHome() != null) { 145 List <String > exceptions = new ArrayList <String >(methodModel.getExceptions()); 146 if (!methodModel.getExceptions().contains("javax.ejb.CreateException")) { 147 exceptions.add("javax.ejb.CreateException"); 148 } 149 if (!methodModel.getExceptions().contains("java.rmi.RemoteException")) { 150 exceptions.add("java.rmi.RemoteException"); 151 } 152 MethodModel methodModelCopy = MethodModel.create( 153 methodModel.getName(), 154 ejb.getRemote(), 155 null, 156 methodModel.getParameters(), 157 exceptions, 158 Collections.<Modifier>emptySet() 159 ); 160 FileObject fileObject = _RetoucheUtil.resolveFileObjectForClass(ejbClassFileObject, ejb.getHome()); 161 addMethod(methodModelCopy, fileObject, ejb.getHome()); 162 } 163 164 List <String > exceptions = new ArrayList <String >(methodModel.getExceptions()); 166 if (!methodModel.getExceptions().contains("javax.ejb.CreateException")) { 167 exceptions.add("javax.ejb.CreateException"); 168 } 169 MethodModel methodModelCopy = MethodModel.create( 170 "ejbC" + methodModel.getName().substring(1), 171 ((Entity) ejb).getPrimKeyClass(), 172 methodModel.getBody(), 173 methodModel.getParameters(), 174 exceptions, 175 Collections.singleton(Modifier.PUBLIC) 176 ); 177 addMethod(methodModelCopy, ejbClassFileObject, ejb.getEjbClass()); 178 MethodModel postCreateMethodModel = MethodModel.create( 179 "ejbPostC" + methodModel.getName().substring(1), 180 "void", 181 "", 182 methodModel.getParameters(), 183 exceptions, 184 Collections.singleton(Modifier.PUBLIC) 185 ); 186 addMethod(postCreateMethodModel, ejbClassFileObject, ejb.getEjbClass()); 187 } 188 189 } 190 | Popular Tags |