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.HashSet ; 25 import java.util.List ; 26 import java.util.Set ; 27 import javax.lang.model.element.Modifier; 28 import org.netbeans.modules.j2ee.common.method.MethodModel; 29 import org.netbeans.modules.j2ee.dd.api.ejb.Entity; 30 import org.netbeans.modules.j2ee.dd.api.ejb.MethodParams; 31 import org.netbeans.modules.j2ee.dd.api.ejb.Query; 32 import org.netbeans.modules.j2ee.dd.api.ejb.QueryMethod; 33 import org.openide.filesystems.FileObject; 34 35 39 public class SelectMethodGenerator extends AbstractMethodGenerator { 40 41 public SelectMethodGenerator(Entity ejb, FileObject ejbClassFileObject, FileObject ddFileObject) { 42 super(ejb, ejbClassFileObject, ddFileObject); 43 } 44 45 public static SelectMethodGenerator create(Entity ejb, FileObject ejbClassFileObject, FileObject ddFileObject) { 46 return new SelectMethodGenerator(ejb, ejbClassFileObject, ddFileObject); 47 } 48 49 public void generate(MethodModel methodModel, boolean generateLocal, boolean generateRemote, 50 boolean isOneReturn, String ejbql) throws IOException { 51 52 if (!methodModel.getName().startsWith("ejbSelect")) { 53 throw new IllegalArgumentException ("The select method name must have ejbSelect as its prefix."); 54 } 55 56 List <String > exceptions = new ArrayList <String >(methodModel.getExceptions()); 57 if (!methodModel.getExceptions().contains("javax.ejb.FinderException")) { 58 exceptions.add("javax.ejb.FinderException"); 59 } 60 Set <Modifier> modifiers = new HashSet <Modifier>(2); 61 modifiers.add(Modifier.PUBLIC); 62 modifiers.add(Modifier.ABSTRACT); 63 MethodModel methodModelCopy = MethodModel.create( 64 methodModel.getName(), 65 methodModel.getReturnType(), 66 null, 67 methodModel.getParameters(), 68 exceptions, 69 modifiers 70 ); 71 addMethod(methodModelCopy, ejbClassFileObject, ejb.getEjbClass()); 72 73 addQueryToXml(methodModel, ejbql); 75 76 } 77 78 private void addQueryToXml(MethodModel methodModel, String ejbql) throws IOException { 79 Entity entity = (Entity) ejb; 80 Query query = entity.newQuery(); 81 QueryMethod queryMethod = query.newQueryMethod(); 82 queryMethod.setMethodName(methodModel.getName()); 83 MethodParams methodParams = queryMethod.newMethodParams(); 84 for (MethodModel.Variable parameter : methodModel.getParameters()) { 85 methodParams.addMethodParam(parameter.getType()); 86 } 87 queryMethod.setMethodParams(methodParams); 88 query.setQueryMethod(queryMethod); 89 query.setEjbQl(ejbql); 90 entity.addQuery(query); 91 saveXml(); 92 } 93 94 } 95 | Popular Tags |