KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > bull > eclipse > jonas > actions > hibernate > BuildHibernatePersistenceActionDelegate


1 package com.bull.eclipse.jonas.actions.hibernate;
2
3 import java.beans.Introspector JavaDoc;
4 import java.io.File JavaDoc;
5 import java.util.ArrayList JavaDoc;
6 import java.util.Collection JavaDoc;
7 import java.util.Collections JavaDoc;
8 import java.util.Enumeration JavaDoc;
9 import java.util.HashMap JavaDoc;
10 import java.util.Hashtable JavaDoc;
11 import java.util.Iterator JavaDoc;
12 import java.util.List JavaDoc;
13 import java.util.Map JavaDoc;
14 import java.util.Vector JavaDoc;
15
16 import org.eclipse.core.resources.IFile;
17 import org.eclipse.core.resources.IFolder;
18 import org.eclipse.core.resources.IProject;
19 import org.eclipse.core.resources.IResource;
20 import org.eclipse.core.runtime.IPath;
21 import org.eclipse.core.runtime.IProgressMonitor;
22 import org.eclipse.jdt.core.IBuffer;
23 import org.eclipse.jdt.core.ICompilationUnit;
24 import org.eclipse.jdt.core.IField;
25 import org.eclipse.jdt.core.IImportDeclaration;
26 import org.eclipse.jdt.core.IJavaElement;
27 import org.eclipse.jdt.core.IJavaModel;
28 import org.eclipse.jdt.core.IJavaProject;
29 import org.eclipse.jdt.core.IMethod;
30 import org.eclipse.jdt.core.IPackageDeclaration;
31 import org.eclipse.jdt.core.IPackageFragment;
32 import org.eclipse.jdt.core.IType;
33 import org.eclipse.jdt.core.JavaCore;
34 import org.eclipse.jdt.core.JavaModelException;
35 import org.eclipse.jdt.core.Signature;
36 import org.eclipse.jdt.core.dom.AST;
37 import org.eclipse.jdt.internal.core.JavaProject;
38 import org.eclipse.jdt.internal.core.PackageFragment;
39 import org.eclipse.jdt.internal.core.PackageFragmentRoot;
40 import org.eclipse.jdt.internal.ui.JavaPlugin;
41 import org.eclipse.jface.dialogs.InputDialog;
42 import org.eclipse.jface.preference.IPreferenceStore;
43 import org.eclipse.jface.preference.PreferenceContentProvider;
44 import org.eclipse.jface.viewers.IStructuredContentProvider;
45 import org.eclipse.jface.viewers.ITreeContentProvider;
46 import org.eclipse.jface.viewers.LabelProvider;
47 import org.eclipse.jface.viewers.Viewer;
48 import org.eclipse.ui.PlatformUI;
49 import org.eclipse.ui.dialogs.CheckedTreeSelectionDialog;
50 import org.eclipse.ui.dialogs.ListSelectionDialog;
51
52 import com.bull.eclipse.jonas.JonasLauncherPlugin;
53 import com.bull.eclipse.jonas.JonasPluginResources;
54 import com.bull.eclipse.jonas.JonasProject;
55 import com.bull.eclipse.jonas.actions.JonasFolderAbstractActionDelegate;
56 import com.bull.eclipse.jonas.actions.JonasPackageAbstractActionDelegate;
57 import com.bull.eclipse.jonas.actions.JonasPackageRootAbstractActionDelegate;
58 import com.bull.eclipse.jonas.actions.persistence.utils.PersistencePackage;
59 import com.bull.eclipse.jonas.utils.AntUtils;
60 import com.bull.eclipse.jonas.utils.DBToJavaNameTransformUtil;
61 import com.bull.eclipse.jonas.utils.EjbManagement;
62 import com.bull.eclipse.jonas.utils.UpdatePropsFile;
63 import com.bull.eclipse.jonas.utils.xml.FileXmlUtils;
64 import com.bull.eclipse.jonas.utils.xml.UpdateXmlFile;
65 import com.bull.eclipse.jonas.utils.xml.XMLUtils;
66 import com.bull.eclipse.newbean.FacadeEJBGenerate;
67 import com.bull.eclipse.newbean.EntityDescValue;
68 import com.bull.eclipse.newbean.FacadeValue;
69 import com.bull.eclipse.newbean.FkTableAndColumns;
70 import com.bull.eclipse.newbean.MethodDescValue;
71 import com.bull.eclipse.newbean.NewFacadeValue;
72 import com.bull.eclipse.persitence.HibernateGlobalCFGGenerate;
73 import com.bull.eclipse.persitence.HibernateServicePersistenceGenerate;
74
75
76
77 public class BuildHibernatePersistenceActionDelegate extends JonasPackageRootAbstractActionDelegate
78 {
79     static private Object JavaDoc[] oldSelection = null;
80     static private String JavaDoc oldBeanName = null;
81     
82     public boolean doActionOn(PackageFragmentRoot packageRoot) throws Exception JavaDoc {
83         // Update build.properties with JONAS_ROOT
84
JonasProject jp = (JonasProject)packageRoot.getJavaProject().getProject().getNature(JonasLauncherPlugin.NATURE_ID);
85         UpdatePropsFile.updatePropsProject(jp);
86         
87         
88         ArrayList JavaDoc hbmList = new ArrayList JavaDoc();
89         
90         String JavaDoc projDir = jp.getProject().getLocation().toOSString();
91         String JavaDoc packagePersistence = PersistencePackage.getPersistencePackage(packageRoot.getChildren());
92         IJavaElement res[] = packageRoot.getChildren();
93         for(int i=0;i<res.length;i++) {
94             IJavaElement ele = (IJavaElement)res[i];
95             Object JavaDoc[] nonJavaFile = packageRoot.getPackageFragment(ele.getElementName()).getNonJavaResources();
96             if (nonJavaFile.length != 0) {
97                 for (int j=0;j<nonJavaFile.length;j++) {
98                     IResource resource = (IResource) nonJavaFile[j];
99                     if (resource.getName().endsWith(".hbm.xml")) {
100                         hbmList.add(ele.getElementName().replace('.','/') + "/" + resource.getName());
101                     }
102                 }
103             }
104         }
105         
106         // CFG file generation
107
GlobalHBMValue ghv = new GlobalHBMValue();
108         IPreferenceStore pref = JonasLauncherPlugin.getDefault().getPreferenceStore();
109         ghv.setDatasourceJNDI(pref.getString("datasource"));
110         ghv.setDialect("net.sf.hibernate.dialect.DB2Dialect");
111         ghv.setHbmList(hbmList);
112         
113         HibernateGlobalCFGGenerate hghg = new HibernateGlobalCFGGenerate(jp);
114         hghg.generateCFG(ghv);
115         
116         //End of CFG file generation
117

118         //Generate the persistence service
119
PersistenceServicesValue psv = new PersistenceServicesValue();
120         psv.setPersistencePackage(packagePersistence);
121         psv.setPersistenceName("Persistence");
122         ArrayList JavaDoc valueObjects = new ArrayList JavaDoc();
123         // It is not the best thing to re start the loop but it is more clear !!
124
for(int i=0;i<res.length;i++) {
125             IJavaElement ele = (IJavaElement)res[i];
126             Object JavaDoc[] nonJavaFile = packageRoot.getPackageFragment(ele.getElementName()).getNonJavaResources();
127             if (nonJavaFile.length != 0) {
128                 ValueObjectDescription vod = new ValueObjectDescription();
129                 for (int j=0;j<nonJavaFile.length;j++) {
130                     IResource resource = (IResource) nonJavaFile[j];
131                     File JavaDoc hbmFile = new File JavaDoc(resource.getLocation().toOSString());
132                     String JavaDoc classValue = (String JavaDoc) FileXmlUtils.getAttributeValuesOfNode(hbmFile, "class", "name").iterator().next(); //We have only one occurence
133
vod.setPackageName(classValue.substring(0,classValue.lastIndexOf(".")));
134                     vod.setValueName(classValue.substring(classValue.lastIndexOf(".")+1,classValue.length()));
135                     vod.setValueTOName(vod.getValueName().substring(0,vod.getValueName().length()-5) + "TO");
136                     String JavaDoc tableValue = (String JavaDoc) FileXmlUtils.getAttributeValuesOfNode(hbmFile, "class", "table").iterator().next();//We have only one occurence
137
vod.setObjectName(DBToJavaNameTransformUtil.dbNameToVariableName(tableValue));
138                     String JavaDoc pkType = null;
139                     Collection JavaDoc pkTypes = FileXmlUtils.getAttributeValuesOfNode(hbmFile, "id", "type"); //We have only one occurence
140
if (pkTypes.size() == 0) {
141                         //Composite PK
142
pkType = (String JavaDoc) FileXmlUtils.getAttributeValuesOfNode(hbmFile, "composite-id", "class").iterator().next(); //We have only one occurence
143
ICompilationUnit icompil = packageRoot.getPackageFragment(ele.getElementName()).getCompilationUnit(vod.getObjectName() + "PK.java");
144                         IType type = icompil.getTypes()[0];
145                         IField field[] = type.getFields();
146                         CompositePKValue pkValue = new CompositePKValue();
147                         ArrayList JavaDoc alPk = new ArrayList JavaDoc();
148                         for (int f=0;f<field.length;f++) {
149                             pkValue = new CompositePKValue();
150                             pkValue.setJavaName(field[f].getElementName());
151                             pkValue.setColumnName(field[f].getElementName());
152                             alPk.add(pkValue);
153                         }
154                         HibernateServicePersistenceGenerate hspg1 = new HibernateServicePersistenceGenerate(jp);
155                         hspg1.generateCompositePK(alPk, vod.getPackageName(), vod.getObjectName() + "PK");
156                                                 
157                         majHbmFileWithCompositePK(hbmFile,jp);
158                         
159                     } else {
160                         pkType = (String JavaDoc) pkTypes.iterator().next();
161                     }
162                     vod.setPkJavaType(pkType);
163                     Collection JavaDoc manyRelationsName = FileXmlUtils.getAttributeValuesOfNode(hbmFile, "set", "name");
164                     if (manyRelationsName != null) {
165                         ArrayList JavaDoc manyRelationNameCapitalise = new ArrayList JavaDoc();
166                         Iterator JavaDoc ite = manyRelationsName.iterator();
167                         while (ite.hasNext()) {
168                             manyRelationNameCapitalise.add(DBToJavaNameTransformUtil.capitalise((String JavaDoc)ite.next()));
169                         }
170                         Collection JavaDoc manyRelationsObject = FileXmlUtils.getAttributeValuesOfNode(hbmFile, "one-to-many", "class");
171                         ArrayList JavaDoc manyRelationObjectValue = new ArrayList JavaDoc();
172                         ite = manyRelationsObject.iterator();
173                         while (ite.hasNext()) {
174                             String JavaDoc manyRelationObject = (String JavaDoc)ite.next();
175                             manyRelationObjectValue.add(manyRelationObject);
176                         }
177                         
178                         // Many relation information save in the value object
179
if (manyRelationNameCapitalise.size() == manyRelationObjectValue.size()) {
180                             ArrayList JavaDoc manyRelationObject = new ArrayList JavaDoc();
181                             for(int u=0;u<manyRelationNameCapitalise.size();u++) {
182                                 ValueObjectDescription vodMany = new ValueObjectDescription();
183                                 
184                                 vodMany.setPackageName(((String JavaDoc)manyRelationObjectValue.get(u)).substring(0,((String JavaDoc)manyRelationObjectValue.get(u)).lastIndexOf(".")));
185                                 vodMany.setValueName(((String JavaDoc)manyRelationObjectValue.get(u)).substring(((String JavaDoc)manyRelationObjectValue.get(u)).lastIndexOf(".")+1,((String JavaDoc)manyRelationObjectValue.get(u)).length()));
186                                 vodMany.setValueTOName(vodMany.getValueName().substring(0,vodMany.getValueName().length()-5) + "TO");
187                                 vodMany.setObjectName((String JavaDoc)manyRelationNameCapitalise.get(u));
188                                 if (vodMany.getObjectName().indexOf("By") != -1) {
189                                     vodMany.setSuffix(vodMany.getObjectName().substring(vodMany.getObjectName().indexOf("By")));
190                                 } else
191                                     vodMany.setSuffix("");
192                                 
193                                 manyRelationObject.add(vodMany);
194                             }
195                             vod.setRelationMany(manyRelationObject);
196                         } else {
197                             //Problems !!!!!
198
throw new Exception JavaDoc("There is a mysterious problem !!");
199                         }
200                     }
201                     Collection JavaDoc oneRelationsName = FileXmlUtils.getAttributeValuesOfNode(hbmFile, "many-to-one", "name");
202                     Collection JavaDoc oneRelationsClass = FileXmlUtils.getAttributeValuesOfNode(hbmFile, "many-to-one", "class");
203                     if (oneRelationsName != null) {
204                         ArrayList JavaDoc oneRelationNameCapitalise = new ArrayList JavaDoc();
205                         Iterator JavaDoc iteName = oneRelationsName.iterator();
206                         Iterator JavaDoc iteClass = oneRelationsClass.iterator();
207                         // Many relation information save in the value object
208
if (oneRelationsName.size() == oneRelationsClass.size()) {
209                             ArrayList JavaDoc oneRelationObject = new ArrayList JavaDoc();
210                             while (iteName.hasNext()) {
211                                 ValueObjectDescription vodOne = new ValueObjectDescription();
212                                 String JavaDoc name = (String JavaDoc) iteName.next();
213                                 String JavaDoc value = (String JavaDoc) iteClass.next();
214                                 vodOne.setPackageName(value.substring(0,value.lastIndexOf(".")));
215                                 vodOne.setValueName(value.substring(value.lastIndexOf(".")+1,value.length()));
216                                 vodOne.setValueTOName(vodOne.getValueName().substring(0,vodOne.getValueName().length()-5) + "TO");
217                                 vodOne.setObjectName(DBToJavaNameTransformUtil.capitalise(name));
218                                 if (vodOne.getObjectName().indexOf("By") != -1) {
219                                     vodOne.setSuffix(vodOne.getObjectName().substring(vodOne.getObjectName().indexOf("By")));
220                                 } else
221                                     vodOne.setSuffix("");
222                                 
223                                 oneRelationObject.add(vodOne);
224                             }
225                             vod.setRelationOne(oneRelationObject);
226                         } else {
227                             //Problems !!!!!
228
throw new Exception JavaDoc("There is a mysterious problem !!");
229                         }
230                     }
231                 }
232                 valueObjects.add(vod);
233             }
234         }
235         psv.setPackagesOfValueObject(valueObjects);
236         
237         HibernateServicePersistenceGenerate hspg = new HibernateServicePersistenceGenerate(jp);
238         hspg.generateServicePersistence(psv);
239         
240         jp.getProject().refreshLocal( IResource.DEPTH_INFINITE, null );
241         
242         
243         
244         return false;
245     }
246
247
248     public void majHbmFileWithCompositePK(File JavaDoc hbmFile, JonasProject jp) {
249         UpdateXmlFile.updateHbmXmlFile(hbmFile, jp.getProject().getLocation().append("temp").append("composite_id.xml").toFile());
250     }
251 }
252
253
Popular Tags