1 package com.bull.eclipse.jonas.actions.ejb; 2 3 import java.beans.Introspector ; 4 import java.io.File ; 5 import java.util.Collection ; 6 import java.util.Collections ; 7 import java.util.Enumeration ; 8 import java.util.HashMap ; 9 import java.util.Hashtable ; 10 import java.util.List ; 11 import java.util.Map ; 12 import java.util.Vector ; 13 14 import org.eclipse.core.resources.IFile; 15 import org.eclipse.core.resources.IProject; 16 import org.eclipse.core.resources.IResource; 17 import org.eclipse.core.runtime.IPath; 18 import org.eclipse.core.runtime.IProgressMonitor; 19 import org.eclipse.jdt.core.ICompilationUnit; 20 import org.eclipse.jdt.core.IImportDeclaration; 21 import org.eclipse.jdt.core.IJavaElement; 22 import org.eclipse.jdt.core.IJavaModel; 23 import org.eclipse.jdt.core.IJavaProject; 24 import org.eclipse.jdt.core.IMethod; 25 import org.eclipse.jdt.core.IPackageFragment; 26 import org.eclipse.jdt.core.IType; 27 import org.eclipse.jdt.core.JavaCore; 28 import org.eclipse.jdt.core.JavaModelException; 29 import org.eclipse.jdt.core.Signature; 30 import org.eclipse.jdt.core.dom.AST; 31 import org.eclipse.jdt.internal.core.JavaProject; 32 import org.eclipse.jdt.internal.core.PackageFragment; 33 import org.eclipse.jdt.internal.ui.JavaPlugin; 34 import org.eclipse.jface.dialogs.InputDialog; 35 import org.eclipse.jface.preference.PreferenceContentProvider; 36 import org.eclipse.jface.viewers.IStructuredContentProvider; 37 import org.eclipse.jface.viewers.ITreeContentProvider; 38 import org.eclipse.jface.viewers.LabelProvider; 39 import org.eclipse.jface.viewers.Viewer; 40 import org.eclipse.ui.PlatformUI; 41 import org.eclipse.ui.dialogs.CheckedTreeSelectionDialog; 42 import org.eclipse.ui.dialogs.ListSelectionDialog; 43 44 import com.bull.eclipse.jonas.JonasLauncherPlugin; 45 import com.bull.eclipse.jonas.JonasPluginResources; 46 import com.bull.eclipse.jonas.JonasProject; 47 import com.bull.eclipse.jonas.actions.JonasPackageAbstractActionDelegate; 48 import com.bull.eclipse.jonas.utils.AntUtils; 49 import com.bull.eclipse.jonas.utils.EjbManagement; 50 import com.bull.eclipse.jonas.utils.UpdatePropsFile; 51 import com.bull.eclipse.jonas.utils.xml.FileXmlUtils; 52 import com.bull.eclipse.jonas.utils.xml.UpdateXmlFile; 53 import com.bull.eclipse.jonas.utils.xml.XMLUtils; 54 import com.bull.eclipse.newbean.FacadeEJBGenerate; 55 import com.bull.eclipse.newbean.EntityDescValue; 56 import com.bull.eclipse.newbean.FacadeValue; 57 import com.bull.eclipse.newbean.FkTableAndColumns; 58 import com.bull.eclipse.newbean.MethodDescValue; 59 import com.bull.eclipse.newbean.NewFacadeValue; 60 61 62 63 public class AggregateFacadeEjbActionDelegate extends JonasPackageAbstractActionDelegate 64 { 65 static private Object [] oldSelection = null; 66 static private String oldBeanName = null; 67 static private String suffixValue = "TO"; 68 static private int lengthSuffixValue = 2; 69 70 public boolean doActionOn(IPackageFragment pack) throws Exception { 71 if (pack.containsJavaResources()) 73 throw new Exception ("You must execute this function on a deployment unit !"); 74 75 JonasProject jp = (JonasProject)pack.getJavaProject().getProject().getNature(JonasLauncherPlugin.NATURE_ID); 76 UpdatePropsFile.updatePropsProject(jp); 77 78 79 String projDir = jp.getProject().getLocation().toOSString(); 80 String packName = pack.getPath().removeFirstSegments(1).toOSString(); 81 File ejb_standard_xml = new File (projDir , pack.getPath().removeFirstSegments(1).append("ejb-jar.xml").toOSString()); 82 File ejb_jonas_xml = new File (projDir , pack.getPath().removeFirstSegments(1).append("jonas-ejb-jar.xml").toOSString()); 83 if ((!ejb_standard_xml.exists()) || (!ejb_jonas_xml.exists())) 84 throw new Exception ("The deployment unit must be create by the middlegen functionnality"); 85 86 Hashtable hashtable = FileXmlUtils.getAllNameAndPackageEJBEntity(ejb_standard_xml); 87 88 Vector vec = new Vector (hashtable.keySet()); 90 Vector entitiesChoice = ejbEntitiesNameChoice(vec); 91 Enumeration nameEnum = entitiesChoice.elements(); 92 Vector entitesChoicePackage = new Vector (); 93 while (nameEnum.hasMoreElements()) { 94 entitesChoicePackage.add(hashtable.get(nameEnum.nextElement())); 95 } 96 Vector vecValue = new Vector (); 97 for (int i = 0; i < entitesChoicePackage.size(); i++) { 98 EntityDescValue napv = new EntityDescValue(); 99 napv.setBeanName((String ) entitiesChoice.get(i)); 100 napv.setPackageName((String ) entitesChoicePackage.get(i)); 101 vecValue.add(napv); 102 } 103 104 vecValue = getMethods(vecValue,pack.getJavaProject(),jp); 105 106 File tempFile = new File (jp.getProject().getLocation().append("temp").toOSString()); 107 if (!tempFile.exists()) 108 tempFile.mkdirs(); 109 110 111 FacadeEJBGenerate feg = new FacadeEJBGenerate(jp); 112 NewFacadeValue nfv = new NewFacadeValue(); 113 nfv.setVecValue(vecValue); 114 String beanName = "Persistence"; 116 oldBeanName = beanName; 117 String firstChar = beanName.substring(0,1); 118 String endString = beanName.substring(1,beanName.length()); 119 nfv.setBeanName(firstChar.toUpperCase().concat(endString)); 120 nfv.setPackageName(pack.getPath().removeFirstSegments(2).toOSString().replace(File.separatorChar, '.').concat(".persistenceservices")); 121 122 nfv.setFacadeMethods(getFacadeEJBMethods(nfv.getBeanName(),jp,nfv.getPackageName(),pack.getJavaProject())); 123 124 feg.runNewFacade(nfv); 125 UpdateXmlFile.updateEjbStandardXmlFile(ejb_standard_xml, new File (tempFile, "facadeNode.xml")); 127 UpdateXmlFile.updateEjbJOnASXmlFile(ejb_jonas_xml, new File (tempFile, "JOnASFacadeNode.xml")); 128 129 jp.getProject().refreshLocal( IResource.DEPTH_INFINITE, null ); 130 131 132 133 return false; 134 } 135 136 private Vector ejbEntitiesNameChoice(Vector vecEjbName) throws Exception { 137 final String [] listEJBEntity = (String []) vecEjbName.toArray(new String [0]); 138 ListSelectionDialog lsd = 139 new ListSelectionDialog( 140 PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), 141 new Object (), 142 new IStructuredContentProvider() { 143 144 public void dispose() { 145 } 146 147 public Object [] getElements(Object inputElement) { 148 return listEJBEntity; 149 } 150 151 public void inputChanged(Viewer viewer1, Object obj, Object obj1) { 152 int i = 0; 153 } 154 155 }, new LabelProvider(), "Select entities beans you want to access by the ejb session"); 156 157 lsd.setTitle("Select j2ee modules"); 158 if (oldSelection == null) { 159 lsd.setInitialSelections(listEJBEntity); 160 } else { 161 lsd.setInitialSelections(oldSelection); 162 } 163 164 lsd.open(); 165 166 Object [] results_upd = lsd.getResult(); 167 168 oldSelection = results_upd; 169 170 int ret = lsd.getReturnCode(); 171 if (ret == 1) 173 throw new Exception ("Operation is cancelled"); 174 if ((results_upd == null) || (results_upd.length == 0)) 175 throw new Exception ("There is no element to build the application"); 176 177 Vector entityChoice = new Vector (); 178 for(int i=0;i<oldSelection.length;i++) { 179 entityChoice.add(oldSelection[i]); 180 } 181 182 return entityChoice; 183 } 184 185 private String getChoice(String defaultValue, String title_label, String label) { 186 InputDialog id = new InputDialog(JavaPlugin.getActiveWorkbenchShell(), 187 title_label, 188 label, defaultValue, null); 189 190 if (id.open() == org.eclipse.jface.window.Window.OK) 191 return id.getValue(); 192 else 193 return null; 194 195 } 196 197 protected static Vector getMethods(Vector vecValue, IJavaProject javp, JonasProject jp) throws JavaModelException { 198 IPath ejbPath = jp.getProject().getLocation().append(jp.getSrcEJBDir()); 199 Vector napvVec = new Vector (); 200 for (int i=0; i < vecValue.size(); i++) { 201 EntityDescValue napv = (EntityDescValue) vecValue.elementAt(i); 202 IPath packFragPath = ejbPath.append(napv.getPackageName().replace('.',File.separatorChar)); 203 IPath classes = packFragPath.append(napv.getBeanName().concat("LocalHome.java")); 204 String className = napv.getPackageName().concat(".").concat(napv.getBeanName()).concat("LocalHome"); 205 napv.setMethodHomeDesc(seekMethod(napv, className, javp)); 206 classes = packFragPath.append(napv.getBeanName().concat("Local.java")); 207 className = napv.getPackageName().concat(".").concat(napv.getBeanName()).concat("Local"); 208 napv.setMethodDesc(seekMethod(napv, className, javp)); 209 210 napvVec.add(napv); 211 } 212 return napvVec; 213 214 } 215 216 private static Vector seekMethod(EntityDescValue napv, String className, 217 IJavaProject japv) throws JavaModelException { 218 IType ty = japv.findType(className); 219 IMethod[] ime = ty.getMethods(); 220 Vector vecSignature = new Vector (); 221 MethodDescValue mdv = null; 222 for (int j = 0; j < ime.length; j++) { 223 if ((className 224 .substring(className.length() - 5, className.length()) 225 .equals("Local")) 226 && (!ime[j].getElementName().substring(0, 3).equals("set"))) { 227 mdv = buildSignatureOnLocalInterface(ime[j], napv); 228 if (mdv != null) 229 vecSignature.add(mdv); 230 } else { 231 mdv = buildSignatureOnLocalHomeInterface(ime[j], napv); 232 if (mdv != null) 233 vecSignature.add(mdv); 234 } 235 } 236 237 238 return vecSignature; 239 } 240 241 public static MethodDescValue buildSignatureOnLocalInterface(IMethod ime, EntityDescValue napv) throws JavaModelException { 242 MethodDescValue mdv = new MethodDescValue(); 243 String methodName = null; 244 String signature = null; 245 String parameterName = ""; 246 if (ime.getElementName().substring(0,3).equals("add")) { 247 methodName = napv.getBeanName().toLowerCase().concat("Add"); 248 mdv.setPkGetName(singularise(napv.getBeanName())); 249 if (ime.getElementName().length() > 3) { 250 String fkName = ime.getElementName().substring(ime.getElementName().lastIndexOf("By"),ime.getElementName().length()); 251 methodName = methodName.concat(fkName); 252 mdv.setFkID(fkName); 253 mdv.setAddMethodName(ime.getElementName().substring(3,ime.getElementName().length())); 254 } else { 255 mdv.setFkID(""); 256 mdv.setAddMethodName(""); 257 } 258 String [] typeParams = ime.getParameterTypes(); 259 String [] nameParams = ime.getParameterNames(); 260 Vector vecParam = new Vector (); 261 for (int h = 0; h < typeParams.length; h++) { 262 String param = Signature.toString(typeParams[h]); 263 FkTableAndColumns ftac = new FkTableAndColumns(); 264 if (!param 265 .substring(param.length() - lengthSuffixValue, param.length()) 266 .equals(suffixValue)) { 267 String fkBeanName = param.substring(param 268 .lastIndexOf(".") + 1, param.length() - 5); 269 ftac.setFkTable(fkBeanName); 270 String first = nameParams[h].substring(0,1).toUpperCase(); 271 ftac.setFkColumn(first.concat(nameParams[h].substring(1,nameParams[h].length()))); 272 vecParam.add(ftac); 273 } else { 274 mdv.setFkValueName(param.substring(param 275 .lastIndexOf(".") + 1, param.length() - 5)); 276 } 277 } 278 mdv.setListLocalAdd(vecParam); 279 signature = Signature.toString(ime.getReturnType()); 280 signature = signature + " "; 281 signature = signature + methodName + "("; 282 signature = signature + Signature.toString((ime.getParameterTypes())[0]) + " "; 283 signature = signature + nameParams[0] + ")"; 284 mdv.setMethodName("add"); 285 } else { 286 if ((ime.getElementName().substring(0, 3).equals("get"))) { 287 if (!ime.getElementName().substring( 288 ime.getElementName().length()-lengthSuffixValue, 289 ime.getElementName().length()).equals(suffixValue)) { 290 return null; 292 } 293 294 if (!(ime.getElementName().substring( 295 3,ime.getElementName().length()-lengthSuffixValue) 296 .equals(napv.getBeanName()))) { 297 methodName = ime.getElementName().concat("Of") 298 .concat(napv.getBeanName()); 299 signature = Signature.toString(ime 300 .getReturnType()) 301 + " " 302 + methodName 303 + "(" 304 + napv.getTypePK() 305 + " pk)"; 306 parameterName = "pk"; 307 mdv.setMethodName("get"); 308 } else { 309 return null; 310 } 311 312 } else { 313 methodName = ime.getElementName(); 314 signature = Signature.toString(ime.getSignature(), 315 methodName, ime.getParameterNames(), true, 316 true); 317 } 318 } 319 String [] exceptionListe = ime.getExceptionTypes(); 320 String exception = " throws "; 321 for (int g = 0; g < exceptionListe.length; g++) { 322 if (!exception.equals(" throws ")) 323 exception = exception.concat( 324 Signature.toString(exceptionListe[g])).concat( 325 ", "); 326 else 327 exception = exception.concat( 328 Signature.toString(exceptionListe[g])).concat( 329 ", "); 330 } 331 mdv.setSignature(signature.concat(exception)); 332 mdv.setName(ime.getElementName()); 333 if (parameterName.equals("")) { 334 String [] parameterNameList = ime.getParameterNames(); 335 for (int k = 0; k < parameterNameList.length; k++) { 336 if (parameterName.equals("")) 337 parameterName = parameterNameList[k]; 338 else 339 parameterName = ", ".concat(parameterName); 340 } 341 } 342 mdv.setParameterName(parameterName); 343 return mdv; 344 } 345 346 public static MethodDescValue buildSignatureOnLocalHomeInterface(IMethod ime, EntityDescValue napv) throws JavaModelException { 347 MethodDescValue mdv = new MethodDescValue(); 348 String methodName = null; 349 String signature = null; 350 String parameterName = ""; 351 352 if (ime.getElementName().equals("findByPrimaryKey")) { 353 napv.setTypePK(Signature.toString(ime 354 .getParameterTypes()[0])); 355 return null; 356 } else if (ime.getElementName().equals("create")) { 357 String [] typeParams = ime.getParameterTypes(); 359 Vector vecParam = new Vector (); 360 for(int h=0; h<typeParams.length; h++) { 361 FkTableAndColumns ftac = new FkTableAndColumns(); 362 String param = Signature.toString(typeParams[h]); 363 if (!param.substring(param.length()-lengthSuffixValue,param.length()).equals(suffixValue)) { 364 String fkBeanName = param.substring(param.lastIndexOf(".")+1,param.length()-5); 365 ftac.setFkTable(fkBeanName); 366 parameterName = (ime.getParameterNames())[h]; 367 String first = parameterName.substring(0,1).toUpperCase(); 368 ftac.setFkColumn(first.concat(parameterName.substring(1,parameterName.length()))); 369 vecParam.add(ftac); 370 } 371 } 372 napv.setListeFk(vecParam); 373 return null; 374 } else if (ime.getElementName().substring(0,4).equals("find")) { 375 methodName = napv.getBeanName().toLowerCase().concat("find"); 377 String finderName = methodName.concat(ime.getElementName().substring(4)); 378 mdv.setName(ime.getElementName()); 379 String [] typeParams = ime.getParameterTypes(); 380 String [] nameParams = ime.getParameterNames(); 381 Vector vecParam = new Vector (); 382 signature = Signature.toString(ime.getReturnType()); 383 if (!signature.equals("java.util.Collection")) { 384 signature = signature.substring(0,signature.length()-lengthSuffixValue).concat(suffixValue); 385 mdv.setMethodName("value"); 386 } 387 signature = signature + " "; 388 signature = signature + Signature.toString(ime.getSignature(),finderName,nameParams,true,false); 389 mdv.setSignature(signature); 390 parameterName = ""; 391 for (int name=0;name<nameParams.length;name++) { 392 if (parameterName.equals("")) 393 parameterName = nameParams[name]; 394 else 395 parameterName = parameterName + ", ".concat(nameParams[name]); 396 } 397 mdv.setParameterName(parameterName); 398 return mdv; 399 } 400 return null; 401 } 402 403 public static Vector getFacadeEJBMethods(String facadeName, 404 JonasProject jp, String duPackage, IJavaProject javaProj) 405 throws JavaModelException { 406 String sessionPackage = duPackage.replaceAll("entity", "session"); 407 String sessionFolder = sessionPackage.replace('.', File.separatorChar); 408 File sessionFile = new File (jp.getProject().getLocation().append( 409 jp.getSrcEJBDir()).toOSString(), sessionFolder); 410 File ejbFile = new File (sessionFolder, facadeName + "FacadeEJB.java"); 411 412 Vector vec = new Vector (); 413 MethodDescValue mdv = null; 414 IType ty = javaProj.findType(sessionPackage.concat(".").concat( 415 facadeName).concat("FacadeEJB")); 416 if (ty != null) { 417 IMethod[] ime = ty.getMethods(); 418 for (int j = 0; j < ime.length; j++) { 419 String type = ime[j].getSource().substring(0, 6); 420 if (type.equals("public")) { 421 mdv = new MethodDescValue(); 422 mdv.setName(ime[j].getElementName()); 423 String [] typeParams = ime[j].getParameterTypes(); 424 String [] nameParams = ime[j].getParameterNames(); 425 String signature = Signature.toString(ime[j] 426 .getReturnType()); 427 signature = signature + " "; 428 signature = signature 429 + Signature.toString(ime[j].getSignature(), mdv 430 .getName(), nameParams, true, false); 431 String [] exceptionListe = ime[j].getExceptionTypes(); 432 String exception = null; 433 for (int g = 0; g < exceptionListe.length; g++) { 434 if (exception == null) 435 exception = " throws ".concat(Signature 436 .toString(exceptionListe[g])); 437 else 438 exception = exception.concat(", ".concat(Signature 439 .toString(exceptionListe[g]))); 440 } 441 mdv.setSignature(signature.concat(exception)); 442 vec.add(mdv); 443 } 444 } 445 } 446 return vec; 447 448 } 449 450 public static String singularise(String name) { 451 String result = name; 452 if (seemsPluralised(name)) { 453 String lower = name.toLowerCase(); 454 if (lower.endsWith("ies")) { 455 result = name.substring(0, name.length() - 3) + "y"; 457 } else if (lower.endsWith("ches") || lower.endsWith("ses")) { 458 result = name.substring(0, name.length() - 2); 460 } else if (lower.endsWith("s")) { 461 result = name.substring(0, name.length() - 1); 463 } 464 } 465 return result; 466 } 467 468 private static boolean seemsPluralised(String name) { 469 name = name.toLowerCase(); 470 boolean pluralised = false; 471 pluralised |= name.endsWith("es"); 472 pluralised |= name.endsWith("s"); 473 pluralised &= !(name.endsWith("ss") || name.endsWith("us")); 474 return pluralised; 475 } 476 477 } 478 479 | Popular Tags |