1 package com.bull.eclipse.jonas.actions.hibernate; 2 3 import java.beans.Introspector ; 4 import java.io.File ; 5 import java.util.ArrayList ; 6 import java.util.Collection ; 7 import java.util.Collections ; 8 import java.util.Enumeration ; 9 import java.util.HashMap ; 10 import java.util.Hashtable ; 11 import java.util.Iterator ; 12 import java.util.List ; 13 import java.util.Map ; 14 import java.util.Vector ; 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 [] oldSelection = null; 80 static private String oldBeanName = null; 81 82 public boolean doActionOn(PackageFragmentRoot packageRoot) throws Exception { 83 JonasProject jp = (JonasProject)packageRoot.getJavaProject().getProject().getNature(JonasLauncherPlugin.NATURE_ID); 85 UpdatePropsFile.updatePropsProject(jp); 86 87 88 ArrayList hbmList = new ArrayList (); 89 90 String projDir = jp.getProject().getLocation().toOSString(); 91 String 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 [] 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 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 118 PersistenceServicesValue psv = new PersistenceServicesValue(); 120 psv.setPersistencePackage(packagePersistence); 121 psv.setPersistenceName("Persistence"); 122 ArrayList valueObjects = new ArrayList (); 123 for(int i=0;i<res.length;i++) { 125 IJavaElement ele = (IJavaElement)res[i]; 126 Object [] 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 hbmFile = new File (resource.getLocation().toOSString()); 132 String classValue = (String ) FileXmlUtils.getAttributeValuesOfNode(hbmFile, "class", "name").iterator().next(); 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 tableValue = (String ) FileXmlUtils.getAttributeValuesOfNode(hbmFile, "class", "table").iterator().next(); vod.setObjectName(DBToJavaNameTransformUtil.dbNameToVariableName(tableValue)); 138 String pkType = null; 139 Collection pkTypes = FileXmlUtils.getAttributeValuesOfNode(hbmFile, "id", "type"); if (pkTypes.size() == 0) { 141 pkType = (String ) FileXmlUtils.getAttributeValuesOfNode(hbmFile, "composite-id", "class").iterator().next(); 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 alPk = new ArrayList (); 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 ) pkTypes.iterator().next(); 161 } 162 vod.setPkJavaType(pkType); 163 Collection manyRelationsName = FileXmlUtils.getAttributeValuesOfNode(hbmFile, "set", "name"); 164 if (manyRelationsName != null) { 165 ArrayList manyRelationNameCapitalise = new ArrayList (); 166 Iterator ite = manyRelationsName.iterator(); 167 while (ite.hasNext()) { 168 manyRelationNameCapitalise.add(DBToJavaNameTransformUtil.capitalise((String )ite.next())); 169 } 170 Collection manyRelationsObject = FileXmlUtils.getAttributeValuesOfNode(hbmFile, "one-to-many", "class"); 171 ArrayList manyRelationObjectValue = new ArrayList (); 172 ite = manyRelationsObject.iterator(); 173 while (ite.hasNext()) { 174 String manyRelationObject = (String )ite.next(); 175 manyRelationObjectValue.add(manyRelationObject); 176 } 177 178 if (manyRelationNameCapitalise.size() == manyRelationObjectValue.size()) { 180 ArrayList manyRelationObject = new ArrayList (); 181 for(int u=0;u<manyRelationNameCapitalise.size();u++) { 182 ValueObjectDescription vodMany = new ValueObjectDescription(); 183 184 vodMany.setPackageName(((String )manyRelationObjectValue.get(u)).substring(0,((String )manyRelationObjectValue.get(u)).lastIndexOf("."))); 185 vodMany.setValueName(((String )manyRelationObjectValue.get(u)).substring(((String )manyRelationObjectValue.get(u)).lastIndexOf(".")+1,((String )manyRelationObjectValue.get(u)).length())); 186 vodMany.setValueTOName(vodMany.getValueName().substring(0,vodMany.getValueName().length()-5) + "TO"); 187 vodMany.setObjectName((String )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 throw new Exception ("There is a mysterious problem !!"); 199 } 200 } 201 Collection oneRelationsName = FileXmlUtils.getAttributeValuesOfNode(hbmFile, "many-to-one", "name"); 202 Collection oneRelationsClass = FileXmlUtils.getAttributeValuesOfNode(hbmFile, "many-to-one", "class"); 203 if (oneRelationsName != null) { 204 ArrayList oneRelationNameCapitalise = new ArrayList (); 205 Iterator iteName = oneRelationsName.iterator(); 206 Iterator iteClass = oneRelationsClass.iterator(); 207 if (oneRelationsName.size() == oneRelationsClass.size()) { 209 ArrayList oneRelationObject = new ArrayList (); 210 while (iteName.hasNext()) { 211 ValueObjectDescription vodOne = new ValueObjectDescription(); 212 String name = (String ) iteName.next(); 213 String value = (String ) 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 throw new Exception ("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 hbmFile, JonasProject jp) { 249 UpdateXmlFile.updateHbmXmlFile(hbmFile, jp.getProject().getLocation().append("temp").append("composite_id.xml").toFile()); 250 } 251 } 252 253 | Popular Tags |