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.MethodParams; 30 import org.netbeans.modules.j2ee.dd.api.ejb.Query; 31 import org.netbeans.modules.j2ee.dd.api.ejb.QueryMethod; 32 import org.netbeans.modules.j2ee.ejbcore._RetoucheUtil; 33 import org.openide.filesystems.FileObject; 34 35 39 public class FinderMethodGenerator extends AbstractMethodGenerator { 40 41 private FinderMethodGenerator(Entity ejb, FileObject ejbClassFileObject, FileObject ddFileObject) { 42 super(ejb, ejbClassFileObject, ddFileObject); 43 } 44 45 public static FinderMethodGenerator create(Entity ejb, FileObject ejbClassFileObject, FileObject ddFileObject) { 46 return new FinderMethodGenerator(ejb, ejbClassFileObject, ddFileObject); 47 } 48 49 public void generate(MethodModel methodModel, boolean generateLocal, boolean generateRemote, 50 boolean isOneReturn, String ejbql) throws IOException { 51 Entity entity = (Entity) ejb; 52 if (Entity.PERSISTENCE_TYPE_CONTAINER.equals(entity.getPersistenceType())) { 53 generateCmp(methodModel, generateLocal, generateRemote, isOneReturn, ejbql); 54 } else if (Entity.PERSISTENCE_TYPE_BEAN.equals(entity.getPersistenceType())) { 55 generateBmp(methodModel, generateLocal, generateRemote, isOneReturn, ejbql); 56 } 57 } 58 59 private void generateCmp(MethodModel methodModel, boolean generateLocal, boolean generateRemote, 60 boolean isOneReturn, String ejbql) throws IOException { 61 62 if (!methodModel.getName().startsWith("find")) { 63 throw new IllegalArgumentException ("The finder method name must have find as its prefix."); 64 } 65 66 if (generateLocal && ejb.getLocal() != null && ejb.getLocalHome() != null) { 68 List <String > exceptions = new ArrayList <String >(methodModel.getExceptions()); 69 if (!methodModel.getExceptions().contains("javax.ejb.FinderException")) { 70 exceptions.add("javax.ejb.FinderException"); 71 } 72 MethodModel methodModelCopy = MethodModel.create( 73 methodModel.getName(), 74 isOneReturn ? ejb.getLocal() : "java.util.Collection", 75 null, 76 methodModel.getParameters(), 77 exceptions, 78 Collections.<Modifier>emptySet() 79 ); 80 FileObject fileObject = _RetoucheUtil.resolveFileObjectForClass(ejbClassFileObject, ejb.getLocalHome()); 81 addMethod(methodModelCopy, fileObject, ejb.getLocalHome()); 82 83 } 84 85 if (generateRemote && ejb.getRemote() != null && ejb.getHome() != null) { 87 List <String > exceptions = new ArrayList <String >(methodModel.getExceptions()); 88 if (!methodModel.getExceptions().contains("javax.ejb.FinderException")) { 89 exceptions.add("javax.ejb.FinderException"); 90 } 91 if (!methodModel.getExceptions().contains("java.rmi.RemoteException")) { 92 exceptions.add("java.rmi.RemoteException"); 93 } 94 MethodModel methodModelCopy = MethodModel.create( 95 methodModel.getName(), 96 isOneReturn ? ejb.getRemote() : "java.util.Collection", 97 null, 98 methodModel.getParameters(), 99 exceptions, 100 Collections.<Modifier>emptySet() 101 ); 102 FileObject fileObject = _RetoucheUtil.resolveFileObjectForClass(ejbClassFileObject, ejb.getHome()); 103 addMethod(methodModelCopy, fileObject, ejb.getHome()); 104 } 105 106 addQueryToXml(methodModel, ejbql); 108 109 } 110 111 private void generateBmp(MethodModel methodModel, boolean generateLocal, boolean generateRemote, 112 boolean isOneReturn, String ejbql) throws IOException { 113 114 if (!methodModel.getName().startsWith("find")) { 115 throw new IllegalArgumentException ("The finder method name must have find as its prefix."); 116 } 117 118 if (generateLocal && ejb.getLocal() != null && ejb.getLocalHome() != null) { 120 List <String > exceptions = new ArrayList <String >(methodModel.getExceptions()); 121 if (!methodModel.getExceptions().contains("javax.ejb.FinderException")) { 122 exceptions.add("javax.ejb.FinderException"); 123 } 124 MethodModel methodModelCopy = MethodModel.create( 126 methodModel.getName(), 127 isOneReturn ? ejb.getLocal() : "java.util.Collection", 128 null, 129 methodModel.getParameters(), 130 exceptions, 131 Collections.<Modifier>emptySet() 132 ); 133 FileObject fileObject = _RetoucheUtil.resolveFileObjectForClass(ejbClassFileObject, ejb.getLocalHome()); 134 addMethod(methodModelCopy, fileObject, ejb.getLocalHome()); 135 } 136 137 if (generateRemote && ejb.getRemote() != null && ejb.getHome() != null) { 139 List <String > exceptions = new ArrayList <String >(methodModel.getExceptions()); 140 if (!methodModel.getExceptions().contains("javax.ejb.FinderException")) { 141 exceptions.add("javax.ejb.FinderException"); 142 } 143 if (!methodModel.getExceptions().contains("java.rmi.RemoteException")) { 144 exceptions.add("java.rmi.RemoteException"); 145 } 146 MethodModel methodModelCopy = MethodModel.create( 148 'c' + methodModel.getName().substring(4), 149 isOneReturn ? ejb.getRemote() : "java.util.Collection", 150 null, 151 methodModel.getParameters(), 152 exceptions, 153 Collections.<Modifier>emptySet() 154 ); 155 FileObject fileObject = _RetoucheUtil.resolveFileObjectForClass(ejbClassFileObject, ejb.getHome()); 156 addMethod(methodModelCopy, fileObject, ejb.getHome()); 157 158 Entity entity = (Entity) ejb; 160 methodModelCopy = MethodModel.create( 161 "ejbF" + methodModelCopy.getName().substring(1), 162 isOneReturn ? entity.getPrimKeyClass() : "java.util.Collection", 163 methodModelCopy.getBody(), 164 methodModelCopy.getParameters(), 165 methodModelCopy.getExceptions(), 166 Collections.singleton(Modifier.PUBLIC) 167 ); 168 addMethod(methodModelCopy, ejbClassFileObject, ejb.getEjbClass()); 169 170 } 171 172 Entity entity = (Entity) ejb; 174 List <String > exceptions = new ArrayList <String >(methodModel.getExceptions()); 175 if (!methodModel.getExceptions().contains("javax.ejb.FinderException")) { 176 exceptions.add("javax.ejb.FinderException"); 177 } 178 MethodModel methodModelCopy = MethodModel.create( 179 "ejbF" + methodModel.getName().substring(1), 180 isOneReturn ? entity.getPrimKeyClass() : "java.util.Collection", 181 "return null;", 182 methodModel.getParameters(), 183 exceptions, 184 Collections.singleton(Modifier.PUBLIC) 185 ); 186 addMethod(methodModelCopy, ejbClassFileObject, ejb.getEjbClass()); 187 188 } 189 190 private void addQueryToXml(MethodModel methodModel, String ejbql) throws IOException { 191 Entity entity = (Entity) ejb; 192 Query query = entity.newQuery(); 193 QueryMethod queryMethod = query.newQueryMethod(); 194 queryMethod.setMethodName(methodModel.getName()); 195 MethodParams methodParams = queryMethod.newMethodParams(); 196 for (MethodModel.Variable parameter : methodModel.getParameters()) { 197 methodParams.addMethodParam(parameter.getType()); 198 } 199 queryMethod.setMethodParams(methodParams); 200 query.setQueryMethod(queryMethod); 201 query.setEjbQl(ejbql); 202 entity.addQuery(query); 203 saveXml(); 204 } 205 206 } 207 | Popular Tags |