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.actions.persistence.PersistenceValue; 22 import com.bull.eclipse.jonas.utils.TemplateDirUtil; 23 24 25 26 31 public class EJB3ServicePersistenceGenerate { 32 33 private static final int EXIT_SUCCESS = 0; 34 private static final int EXIT_FAILURE = 1; 35 36 private VelocityEngine vEngine = null; 37 private VelocityContext vContext = null; 38 private JonasProject prj = null; 39 40 41 public EJB3ServicePersistenceGenerate(JonasProject prj) { 42 this.prj = prj; 43 } 44 45 46 49 public void generateServicePersistence(PersistenceValue psv) { 50 51 try { 52 vEngine = new VelocityEngine(); 53 vEngine.setProperty(VelocityEngine.VM_LIBRARY, ""); 54 vEngine.setProperty(VelocityEngine.RESOURCE_LOADER, "file"); 55 vEngine.setProperty(VelocityEngine.FILE_RESOURCE_LOADER_PATH, 56 TemplateDirUtil.getTemplateDir().concat("/persistence")); 57 try { 58 vEngine.init(); 59 } catch (Exception e) { 60 fatalError("unable to initilise Velocity engine (" + e + ")"); 61 } 62 63 vContext = new VelocityContext(); 66 vContext.put("persistenceServicesValue", psv); 67 68 File persistenceServiceDir = new File (prj.getProject().getLocation().append("persistence").append(psv.getPersistencePackage().replace('.',File.separatorChar)).append("persistenceservices").toOSString()); 69 if (!persistenceServiceDir.exists()) 70 persistenceServiceDir.mkdirs(); 71 File utilPersistenceServiceDir = new File (prj.getProject().getLocation().append("persistence").append(psv.getPersistencePackage().replace('.',File.separatorChar)).append("persistence").append("util").toOSString()); 72 if (!utilPersistenceServiceDir.exists()) 73 utilPersistenceServiceDir.mkdirs(); 74 File exceptionPersistenceServiceDir = new File (prj.getProject().getLocation().append("persistence").append(psv.getPersistencePackage().replace('.',File.separatorChar)).append("persistence").append("exception").toOSString()); 75 if (!exceptionPersistenceServiceDir.exists()) 76 exceptionPersistenceServiceDir.mkdirs(); 77 78 generate("persistenceInterface.vm", new File (persistenceServiceDir, psv.getPersistenceName() + ".java")); 79 generate("queryInterface.vm", new File (persistenceServiceDir, psv.getQueryInterfaceName() + ".java")); 80 generate("PersistenceUtil.vm", new File (utilPersistenceServiceDir, psv.getPersistenceName() + "Util.java")); 81 generate("EntityNotFoundException.vm", new File (exceptionPersistenceServiceDir, "EntityNotFoundException.java")); 82 generate("NonUniqueResultException.vm", new File (exceptionPersistenceServiceDir, "NonUniqueResultException.java")); 83 } catch (Exception e) { 84 error(e.toString()); 85 } 86 87 } 88 89 90 public void generateCompositePK(ArrayList cpk, String packageName, String className) { 91 92 try { 93 vEngine = new VelocityEngine(); 94 vEngine.setProperty(VelocityEngine.VM_LIBRARY, ""); 95 vEngine.setProperty(VelocityEngine.RESOURCE_LOADER, "file"); 96 vEngine.setProperty(VelocityEngine.FILE_RESOURCE_LOADER_PATH, 97 TemplateDirUtil.getTemplateDir().concat("/hibernate")); 98 try { 99 vEngine.init(); 100 } catch (Exception e) { 101 fatalError("unable to initilise Velocity engine (" + e + ")"); 102 } 103 104 vContext = new VelocityContext(); 107 vContext.put("listeCompositePK", cpk); 108 vContext.put("packageName", packageName); 109 vContext.put("className", className); 110 111 File tempDir = new File (prj.getProject().getLocation().append("temp").toOSString()); 112 if (!tempDir.exists()) 113 tempDir.mkdirs(); 114 generate("composite_id.vm", new File (tempDir, "composite_id.xml")); 115 } catch (Exception e) { 116 error(e.toString()); 117 } 118 119 } 120 121 126 private void generate(String templateFileName, 127 File targetFileName) throws Exception , IOException , ResourceNotFoundException, ParseErrorException, MethodInvocationException { 128 FileWriter fileWriter = null; 129 fileWriter = new FileWriter (targetFileName); 130 vEngine.mergeTemplate(templateFileName, vContext, fileWriter); 131 fileWriter.close(); 132 } 133 134 135 139 static void error(String errMsg) { 140 System.err.println("NewBean error: " + errMsg); 141 } 142 143 144 149 static void fatalError(String errMsg) { 150 System.err.println("NewBean fatal error: " + errMsg); 151 System.exit(EXIT_FAILURE); 152 } 153 154 } 155 | Popular Tags |