1 package com.bull.eclipse.persitence; 2 3 4 import java.io.File ; 5 import java.io.FileWriter ; 6 import java.io.IOException ; 7 import java.util.ArrayList ; 8 import java.util.Vector ; 9 10 import org.apache.velocity.VelocityContext; 11 import org.apache.velocity.app.VelocityEngine; 12 import org.apache.velocity.exception.MethodInvocationException; 13 import org.apache.velocity.exception.ParseErrorException; 14 import org.apache.velocity.exception.ResourceNotFoundException; 15 import org.eclipse.jdt.ui.wizards.NewTypeWizardPage; 16 17 import com.bull.eclipse.jonas.JonasProject; 18 import com.bull.eclipse.jonas.actions.hibernate.CompositePKValue; 19 import com.bull.eclipse.jonas.actions.hibernate.GlobalHBMValue; 20 import com.bull.eclipse.jonas.actions.hibernate.PersistenceServicesValue; 21 import com.bull.eclipse.jonas.utils.TemplateDirUtil; 22 23 24 25 30 public class HibernateServicePersistenceGenerate { 31 32 private static final int EXIT_SUCCESS = 0; 33 private static final int EXIT_FAILURE = 1; 34 35 private VelocityEngine vEngine = null; 36 private VelocityContext vContext = null; 37 private JonasProject prj = null; 38 39 40 public HibernateServicePersistenceGenerate(JonasProject prj) { 41 this.prj = prj; 42 } 43 44 45 48 public void generateServicePersistence(PersistenceServicesValue psv) { 49 50 try { 51 vEngine = new VelocityEngine(); 52 vEngine.setProperty(VelocityEngine.VM_LIBRARY, ""); 53 vEngine.setProperty(VelocityEngine.RESOURCE_LOADER, "file"); 54 vEngine.setProperty(VelocityEngine.FILE_RESOURCE_LOADER_PATH, 55 TemplateDirUtil.getTemplateDir().concat("/hibernate")); 56 try { 57 vEngine.init(); 58 } catch (Exception e) { 59 fatalError("unable to initilise Velocity engine (" + e + ")"); 60 } 61 62 vContext = new VelocityContext(); 65 vContext.put("persistenceServicesValue", psv); 66 67 File persistenceServiceDir = new File (prj.getProject().getLocation().append("persistence").append(psv.getPersistencePackage().replace('.',File.separatorChar)).append("persistenceservices").toOSString()); 68 if (!persistenceServiceDir.exists()) 69 persistenceServiceDir.mkdirs(); 70 File utilPersistenceServiceDir = new File (prj.getProject().getLocation().append("persistence").append(psv.getPersistencePackage().replace('.',File.separatorChar)).append("persistence").append("util").toOSString()); 71 if (!utilPersistenceServiceDir.exists()) 72 utilPersistenceServiceDir.mkdirs(); 73 File exceptionPersistenceServiceDir = new File (prj.getProject().getLocation().append("persistence").append(psv.getPersistencePackage().replace('.',File.separatorChar)).append("persistence").append("exception").toOSString()); 74 if (!exceptionPersistenceServiceDir.exists()) 75 exceptionPersistenceServiceDir.mkdirs(); 76 77 generate("HibernatePersistenceServices.vm", new File (persistenceServiceDir, psv.getPersistenceName() + "Impl.java")); 78 generate("InterfacePersistence.vm", new File (persistenceServiceDir, psv.getPersistenceName() + ".java")); 79 generate("PersistenceUtil.vm", new File (persistenceServiceDir, psv.getPersistenceName() + "Util.java")); 80 generate("HibernateUtil.vm", new File (utilPersistenceServiceDir, "HibernateUtil.java")); 81 generate("PersistenceException.vm", new File (exceptionPersistenceServiceDir, "PersistenceException.java")); 82 } catch (Exception e) { 83 error(e.toString()); 84 } 85 86 } 87 88 89 public void generateCompositePK(ArrayList cpk, String packageName, String className) { 90 91 try { 92 vEngine = new VelocityEngine(); 93 vEngine.setProperty(VelocityEngine.VM_LIBRARY, ""); 94 vEngine.setProperty(VelocityEngine.RESOURCE_LOADER, "file"); 95 vEngine.setProperty(VelocityEngine.FILE_RESOURCE_LOADER_PATH, 96 TemplateDirUtil.getTemplateDir().concat("/hibernate")); 97 try { 98 vEngine.init(); 99 } catch (Exception e) { 100 fatalError("unable to initilise Velocity engine (" + e + ")"); 101 } 102 103 vContext = new VelocityContext(); 106 vContext.put("listeCompositePK", cpk); 107 vContext.put("packageName", packageName); 108 vContext.put("className", className); 109 110 File tempDir = new File (prj.getProject().getLocation().append("temp").toOSString()); 111 if (!tempDir.exists()) 112 tempDir.mkdirs(); 113 generate("composite_id.vm", new File (tempDir, "composite_id.xml")); 114 } catch (Exception e) { 115 error(e.toString()); 116 } 117 118 } 119 120 125 private void generate(String templateFileName, 126 File targetFileName) throws Exception , IOException , ResourceNotFoundException, ParseErrorException, MethodInvocationException { 127 FileWriter fileWriter = null; 128 fileWriter = new FileWriter (targetFileName); 129 vEngine.mergeTemplate(templateFileName, vContext, fileWriter); 130 fileWriter.close(); 131 } 132 133 134 138 static void error(String errMsg) { 139 System.err.println("NewBean error: " + errMsg); 140 } 141 142 143 148 static void fatalError(String errMsg) { 149 System.err.println("NewBean fatal error: " + errMsg); 150 System.exit(EXIT_FAILURE); 151 } 152 153 } 154 | Popular Tags |