KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > bull > eclipse > persitence > EJB3ServicePersistenceGenerate


1 package com.bull.eclipse.persitence;
2
3
4 import java.io.File JavaDoc;
5 import java.io.FileWriter JavaDoc;
6 import java.io.IOException JavaDoc;
7 import java.util.ArrayList JavaDoc;
8 import java.util.Vector JavaDoc;
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 /**
27  * This class is a “bean generator”. It uses Velocity and
28  * a set of template files to generate some files that can serve as a
29  * startup point for the developpement of a new bean.
30  */

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     /**
47      * Generates files for this new bean.
48      */

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 JavaDoc e) {
60                 fatalError("unable to initilise Velocity engine (" + e + ")");
61             }
62
63             // Creates a Velocity context and populates it by walking
64
// through the parameter set
65
vContext = new VelocityContext();
66             vContext.put("persistenceServicesValue", psv);
67             
68             File JavaDoc persistenceServiceDir = new File JavaDoc(prj.getProject().getLocation().append("persistence").append(psv.getPersistencePackage().replace('.',File.separatorChar)).append("persistenceservices").toOSString());
69             if (!persistenceServiceDir.exists())
70                 persistenceServiceDir.mkdirs();
71             File JavaDoc utilPersistenceServiceDir = new File JavaDoc(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 JavaDoc exceptionPersistenceServiceDir = new File JavaDoc(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 JavaDoc(persistenceServiceDir, psv.getPersistenceName() + ".java"));
79             generate("queryInterface.vm", new File JavaDoc(persistenceServiceDir, psv.getQueryInterfaceName() + ".java"));
80             generate("PersistenceUtil.vm", new File JavaDoc(utilPersistenceServiceDir, psv.getPersistenceName() + "Util.java"));
81             generate("EntityNotFoundException.vm", new File JavaDoc(exceptionPersistenceServiceDir, "EntityNotFoundException.java"));
82             generate("NonUniqueResultException.vm", new File JavaDoc(exceptionPersistenceServiceDir, "NonUniqueResultException.java"));
83         } catch (Exception JavaDoc e) {
84             error(e.toString());
85         }
86     
87     }
88
89
90     public void generateCompositePK(ArrayList JavaDoc cpk, String JavaDoc packageName, String JavaDoc 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 JavaDoc e) {
101                 fatalError("unable to initilise Velocity engine (" + e + ")");
102             }
103
104             // Creates a Velocity context and populates it by walking
105
// through the parameter set
106
vContext = new VelocityContext();
107             vContext.put("listeCompositePK", cpk);
108             vContext.put("packageName", packageName);
109             vContext.put("className", className);
110             
111             File JavaDoc tempDir = new File JavaDoc(prj.getProject().getLocation().append("temp").toOSString());
112             if (!tempDir.exists())
113                 tempDir.mkdirs();
114             generate("composite_id.vm", new File JavaDoc(tempDir, "composite_id.xml"));
115         } catch (Exception JavaDoc e) {
116             error(e.toString());
117         }
118     
119     }
120
121     /**
122      * Generates a file from the specified template.
123      * @param templateFileName the name of the template file
124      * @param targetFileName the name of the generated file
125      */

126     private void generate(String JavaDoc templateFileName,
127                           File JavaDoc targetFileName) throws Exception JavaDoc, IOException JavaDoc, ResourceNotFoundException, ParseErrorException, MethodInvocationException {
128         FileWriter JavaDoc fileWriter = null;
129         fileWriter = new FileWriter JavaDoc(targetFileName);
130         vEngine.mergeTemplate(templateFileName, vContext, fileWriter);
131         fileWriter.close();
132     }
133
134
135     /**
136      * Display the specified error message.
137      * @param errMsg the error message to display
138      */

139     static void error(String JavaDoc errMsg) {
140         System.err.println("NewBean error: " + errMsg);
141     }
142
143
144     /**
145      * Display the specified error message and exits with an
146      * EXIT_FAILURE status.
147      * @param errMsg the error message to display
148      */

149     static void fatalError(String JavaDoc errMsg) {
150         System.err.println("NewBean fatal error: " + errMsg);
151         System.exit(EXIT_FAILURE);
152     }
153
154 }
155
Popular Tags