KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > bull > eclipse > jonas > actions > ejb > AggregateFacadeEjbActionDelegate


1 package com.bull.eclipse.jonas.actions.ejb;
2
3 import java.beans.Introspector JavaDoc;
4 import java.io.File JavaDoc;
5 import java.util.Collection JavaDoc;
6 import java.util.Collections JavaDoc;
7 import java.util.Enumeration JavaDoc;
8 import java.util.HashMap JavaDoc;
9 import java.util.Hashtable JavaDoc;
10 import java.util.List JavaDoc;
11 import java.util.Map JavaDoc;
12 import java.util.Vector JavaDoc;
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 JavaDoc[] oldSelection = null;
66     static private String JavaDoc oldBeanName = null;
67     static private String JavaDoc suffixValue = "TO";
68     static private int lengthSuffixValue = 2;
69     
70     public boolean doActionOn(IPackageFragment pack) throws Exception JavaDoc {
71         // Update build.properties with JONAS_ROOT
72
if (pack.containsJavaResources())
73             throw new Exception JavaDoc("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 JavaDoc projDir = jp.getProject().getLocation().toOSString();
80         String JavaDoc packName = pack.getPath().removeFirstSegments(1).toOSString();
81         File JavaDoc ejb_standard_xml = new File JavaDoc(projDir , pack.getPath().removeFirstSegments(1).append("ejb-jar.xml").toOSString());
82         File JavaDoc ejb_jonas_xml = new File JavaDoc(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 JavaDoc("The deployment unit must be create by the middlegen functionnality");
85                 
86         Hashtable JavaDoc hashtable = FileXmlUtils.getAllNameAndPackageEJBEntity(ejb_standard_xml);
87         
88         // Entities EJB name
89
Vector JavaDoc vec = new Vector JavaDoc(hashtable.keySet());
90         Vector JavaDoc entitiesChoice = ejbEntitiesNameChoice(vec);
91         Enumeration JavaDoc nameEnum = entitiesChoice.elements();
92         Vector JavaDoc entitesChoicePackage = new Vector JavaDoc();
93         while (nameEnum.hasMoreElements()) {
94             entitesChoicePackage.add(hashtable.get(nameEnum.nextElement()));
95         }
96         Vector JavaDoc vecValue = new Vector JavaDoc();
97         for (int i = 0; i < entitesChoicePackage.size(); i++) {
98             EntityDescValue napv = new EntityDescValue();
99             napv.setBeanName((String JavaDoc) entitiesChoice.get(i));
100             napv.setPackageName((String JavaDoc) entitesChoicePackage.get(i));
101             vecValue.add(napv);
102         }
103         
104         vecValue = getMethods(vecValue,pack.getJavaProject(),jp);
105         
106         File JavaDoc tempFile = new File JavaDoc(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 = getChoice(oldBeanName,"Facade EJB name","What is the facade ejb name ?");
115
String JavaDoc beanName = "Persistence";
116         oldBeanName = beanName;
117         String JavaDoc firstChar = beanName.substring(0,1);
118         String JavaDoc 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         // Update standard deployment descriptor
126
UpdateXmlFile.updateEjbStandardXmlFile(ejb_standard_xml, new File JavaDoc(tempFile, "facadeNode.xml"));
127         UpdateXmlFile.updateEjbJOnASXmlFile(ejb_jonas_xml, new File JavaDoc(tempFile, "JOnASFacadeNode.xml"));
128         
129         jp.getProject().refreshLocal( IResource.DEPTH_INFINITE, null );
130         
131         
132         
133         return false;
134     }
135
136     private Vector JavaDoc ejbEntitiesNameChoice(Vector JavaDoc vecEjbName) throws Exception JavaDoc {
137         final String JavaDoc[] listEJBEntity = (String JavaDoc[]) vecEjbName.toArray(new String JavaDoc[0]);
138         ListSelectionDialog lsd =
139             new ListSelectionDialog(
140                 PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(),
141                 new Object JavaDoc(),
142                 new IStructuredContentProvider() {
143
144             public void dispose() {
145             }
146
147             public Object JavaDoc[] getElements(Object JavaDoc inputElement) {
148                 return listEJBEntity;
149             }
150
151             public void inputChanged(Viewer viewer1, Object JavaDoc obj, Object JavaDoc 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 JavaDoc[] results_upd = lsd.getResult();
167         
168         oldSelection = results_upd;
169         
170         int ret = lsd.getReturnCode();
171         // return null ( list of methods) , if user press cancel or nothing is selected
172
if (ret == 1)
173             throw new Exception JavaDoc("Operation is cancelled");
174         if ((results_upd == null) || (results_upd.length == 0))
175             throw new Exception JavaDoc("There is no element to build the application");
176         
177         Vector JavaDoc entityChoice = new Vector JavaDoc();
178         for(int i=0;i<oldSelection.length;i++) {
179             entityChoice.add(oldSelection[i]);
180         }
181
182         return entityChoice;
183     }
184     
185     private String JavaDoc getChoice(String JavaDoc defaultValue, String JavaDoc title_label, String JavaDoc 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 JavaDoc getMethods(Vector JavaDoc vecValue, IJavaProject javp, JonasProject jp) throws JavaModelException {
198         IPath ejbPath = jp.getProject().getLocation().append(jp.getSrcEJBDir());
199         Vector JavaDoc napvVec = new Vector JavaDoc();
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 JavaDoc 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 JavaDoc seekMethod(EntityDescValue napv, String JavaDoc className,
217             IJavaProject japv) throws JavaModelException {
218         IType ty = japv.findType(className);
219         IMethod[] ime = ty.getMethods();
220         Vector JavaDoc vecSignature = new Vector JavaDoc();
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 JavaDoc methodName = null;
244         String JavaDoc signature = null;
245         String JavaDoc 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 JavaDoc 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 JavaDoc[] typeParams = ime.getParameterTypes();
259             String JavaDoc[] nameParams = ime.getParameterNames();
260             Vector JavaDoc vecParam = new Vector JavaDoc();
261             for (int h = 0; h < typeParams.length; h++) {
262                 String JavaDoc 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 JavaDoc fkBeanName = param.substring(param
268                             .lastIndexOf(".") + 1, param.length() - 5);
269                     ftac.setFkTable(fkBeanName);
270                     String JavaDoc 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                     // On n'expose pas les méthodes get sur les cmp fields !!
291
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 JavaDoc[] exceptionListe = ime.getExceptionTypes();
320         String JavaDoc 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 JavaDoc[] 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 JavaDoc methodName = null;
349         String JavaDoc signature = null;
350         String JavaDoc 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             // On récupère les paramètres dans le cas de FK
358
String JavaDoc[] typeParams = ime.getParameterTypes();
359             Vector JavaDoc vecParam = new Vector JavaDoc();
360             for(int h=0; h<typeParams.length; h++) {
361                 FkTableAndColumns ftac = new FkTableAndColumns();
362                 String JavaDoc param = Signature.toString(typeParams[h]);
363                 if (!param.substring(param.length()-lengthSuffixValue,param.length()).equals(suffixValue)) {
364                     String JavaDoc fkBeanName = param.substring(param.lastIndexOf(".")+1,param.length()-5);
365                     ftac.setFkTable(fkBeanName);
366                     parameterName = (ime.getParameterNames())[h];
367                     String JavaDoc 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             // On traite les méthodes find
376
methodName = napv.getBeanName().toLowerCase().concat("find");
377             String JavaDoc finderName = methodName.concat(ime.getElementName().substring(4));
378             mdv.setName(ime.getElementName());
379             String JavaDoc[] typeParams = ime.getParameterTypes();
380             String JavaDoc[] nameParams = ime.getParameterNames();
381             Vector JavaDoc vecParam = new Vector JavaDoc();
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 JavaDoc getFacadeEJBMethods(String JavaDoc facadeName,
404             JonasProject jp, String JavaDoc duPackage, IJavaProject javaProj)
405             throws JavaModelException {
406         String JavaDoc sessionPackage = duPackage.replaceAll("entity", "session");
407         String JavaDoc sessionFolder = sessionPackage.replace('.', File.separatorChar);
408         File JavaDoc sessionFile = new File JavaDoc(jp.getProject().getLocation().append(
409                 jp.getSrcEJBDir()).toOSString(), sessionFolder);
410         File JavaDoc ejbFile = new File JavaDoc(sessionFolder, facadeName + "FacadeEJB.java");
411
412         Vector JavaDoc vec = new Vector JavaDoc();
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 JavaDoc type = ime[j].getSource().substring(0, 6);
420                 if (type.equals("public")) {
421                     mdv = new MethodDescValue();
422                     mdv.setName(ime[j].getElementName());
423                     String JavaDoc[] typeParams = ime[j].getParameterTypes();
424                     String JavaDoc[] nameParams = ime[j].getParameterNames();
425                     String JavaDoc 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 JavaDoc[] exceptionListe = ime[j].getExceptionTypes();
432                     String JavaDoc 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 JavaDoc singularise(String JavaDoc name) {
451         String JavaDoc result = name;
452         if (seemsPluralised(name)) {
453             String JavaDoc lower = name.toLowerCase();
454             if (lower.endsWith("ies")) {
455                 // cities --> city
456
result = name.substring(0, name.length() - 3) + "y";
457             } else if (lower.endsWith("ches") || lower.endsWith("ses")) {
458                 // switches --> switch or buses --> bus
459
result = name.substring(0, name.length() - 2);
460             } else if (lower.endsWith("s")) {
461                 // customers --> customer
462
result = name.substring(0, name.length() - 1);
463             }
464         }
465         return result;
466     }
467
468     private static boolean seemsPluralised(String JavaDoc 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