View Javadoc

1   package com.salto.db.generator.plugin;
2   
3   import java.util.Date;
4   import java.util.HashMap;
5   import java.util.Map;
6   
7   import org.apache.velocity.Template;
8   import org.apache.velocity.app.Velocity;
9   
10  import com.salto.db.generator.Constants;
11  
12  import salto.tool.jdo.data.JdoInfo;
13  import salto.tool.jdo.util.CreatorUtil;
14  
15  public class SpringEJB3AndHibernateDAOPlugin extends VelocityAbstractPlugin {
16  
17  	protected EJB3Plugin ejb3Plugin = null;
18  
19  	public static final String TEMPLATE_PATH = "springhibernatedao";
20  
21  	public void execute(String className, JdoInfo info) throws Exception {
22  
23  		ejb3Plugin.execute(className, info);
24  
25  		CreatorUtil.getDoNameCreator().setPrefix(doPrefix);
26  		CreatorUtil.getDoNameCreator().setDefaultNameOffset(this.defaultNameOffset);
27  		Map context = new HashMap();
28  		context.put("jdoInfo", info);
29  		context.put("util", CreatorUtil.getInstance());
30  		context.put("templatePath", TEMPLATE_PATH);
31  		context.put("generatorVersion", Constants.GENERATOR_NAME + " / " + this.getShortDescription());
32  		context.put("date", new Date());
33  		info.setJavaPckName(pckName);
34  		info.setJavaClassName(className);
35  
36  		runVelocity(EJB3AndHibernateDAOPlugin.TEMPLATE_PATH, "hibernatedao.vm", srcPath + "/" + (pckName + ".hibernate").replace('.', '/') + "/", className + "HibernateDAO.java", context);
37  
38  		runVelocity(EJB3AndHibernateDAOPlugin.TEMPLATE_PATH, "dao.vm", srcPath + "/" + pckName.replace('.', '/') + "/", className + "DAO.java", context);
39  
40  	}
41  
42  	public String getDefaultPrefix() {
43  		return "";
44  	}
45  
46  	public String getLongDescription() {
47  		return "This plugin will generate Hibernate EJB3 POJOS, hibernate.cfg.xml and Spring DAOs for each class.";
48  	}
49  
50  	public String getName() {
51  		return "SpringEJB3AndHibernateDao";
52  	}
53  
54  	public String getShortDescription() {
55  		return "EJB3 + Spring/Hibernate DAO";
56  	}
57  
58  	public void postExecute(JdoInfo[] infos) throws Exception {
59  
60  		CreatorUtil.getDoNameCreator().setPrefix(doPrefix);
61  		CreatorUtil.getDoNameCreator().setDefaultNameOffset(this.defaultNameOffset);
62  		Map context = new HashMap();
63  		context.put("jdoInfos", infos);
64  		context.put("connInfo", infos[0].getConnInfo());
65  		context.put("util", CreatorUtil.getInstance());
66  		context.put("templatePath", TEMPLATE_PATH);
67  		context.put("generatorVersion", Constants.GENERATOR_NAME + " / " + this.getShortDescription());
68  		context.put("date", new Date());
69  		context.put("dialect", this.ejb3Plugin);
70  		context.put("pckName", pckName);
71  		context.put("annotations", String.valueOf(ejb3Plugin.isGenerateAnnotations()));
72  		context.put("hibernateDialectHelper", new HibernateDialectHelper());
73  		context.put("ejb3daotemplatePath", EJB3AndHibernateDAOPlugin.TEMPLATE_PATH);
74  
75  		runVelocity(TEMPLATE_PATH, "springabstracthibernatedao.vm", srcPath + "/" + (pckName + ".hibernate").replace('.', '/') + "/", "AbstractHibernateDAO.java", context);
76  
77  		runVelocity(EJB3AndHibernateDAOPlugin.TEMPLATE_PATH, "genericdao.vm", srcPath + "/" + pckName.replace('.', '/') + "/", "GenericDAO.java", context);
78  
79  		runVelocity(TEMPLATE_PATH, "applicationContext-resources.xml.vm", srcPath, "applicationContext-resources.xml", context);
80  		runVelocity(TEMPLATE_PATH, "applicationContext-hibernate.xml.vm", srcPath, "applicationContext-hibernate.xml", context);
81  
82  	}
83  
84  	public void init(String doPrefix, int defaultNameOffset, String srcPath, String pckName) throws Exception {
85  		super.init(doPrefix, defaultNameOffset, srcPath, pckName);
86  		ejb3Plugin = new EJB3Plugin();
87  		ejb3Plugin.init(doPrefix, defaultNameOffset, srcPath, pckName + ".pojo");
88  	}
89  
90  	public EJB3Plugin getEjb3Plugin() {
91  		return ejb3Plugin;
92  	}
93  
94  	public void setEjb3Plugin(EJB3Plugin ejb3Plugin) {
95  		this.ejb3Plugin = ejb3Plugin;
96  	}
97  
98  }