KickJava   Java API By Example, From Geeks To Geeks.

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


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.utils.TemplateDirUtil;
22
23
24
25 /**
26  * This class is a “bean generator”. It uses Velocity and
27  * a set of template files to generate some files that can serve as a
28  * startup point for the developpement of a new bean.
29  */

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

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

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

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

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