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 salto.tool.jdo.data.JdoInfo; 11 import salto.tool.jdo.util.CreatorUtil; 12 13 import com.salto.db.generator.Constants; 14 15 public class EJB3AndHibernateDAOPlugin extends VelocityAbstractPlugin { 16 17 protected EJB3Plugin ejb3Plugin = null; 18 19 public static final String TEMPLATE_PATH = "hibernateejb3dao"; 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( 27 this.defaultNameOffset); 28 Map context = new HashMap(); 29 context.put("jdoInfo", info); 30 context.put("util", CreatorUtil.getInstance()); 31 context.put("templatePath", TEMPLATE_PATH); 32 context.put("generatorVersion", Constants.GENERATOR_NAME + " / " 33 + this.getShortDescription()); 34 context.put("date", new Date()); 35 info.setJavaPckName(pckName); 36 info.setJavaClassName(className); 37 38 runVelocity(TEMPLATE_PATH, "hibernatedao.vm", srcPath + "/" 39 + (pckName + ".hibernate").replace('.', '/') + "/", className 40 + "HibernateDAO.java", context); 41 42 runVelocity(TEMPLATE_PATH, "dao.vm", srcPath + "/" 43 + pckName.replace('.', '/') + "/", className + "DAO.java", 44 context); 45 46 } 47 48 public String getDefaultPrefix() { 49 return ""; 50 } 51 52 public String getLongDescription() { 53 return "This plugin will generate Hibernate EJB3 POJOS, hibernate.cfg.xml and DAOs for each class."; 54 } 55 56 public String getName() { 57 return "EJB3AndHibernateDao"; 58 } 59 60 public String getShortDescription() { 61 return "EJB3 + Hibernate DAO"; 62 } 63 64 public void postExecute(JdoInfo[] infos) throws Exception { 65 66 Template t = Velocity.getTemplate(TEMPLATE_PATH 67 + "/hibernatedaofactory.vm"); 68 69 CreatorUtil.getDoNameCreator().setPrefix(doPrefix); 70 CreatorUtil.getDoNameCreator().setDefaultNameOffset( 71 this.defaultNameOffset); 72 Map context = new HashMap(); 73 context.put("jdoInfos", infos); 74 context.put("connInfo", infos[0].getConnInfo()); 75 context.put("util", CreatorUtil.getInstance()); 76 context.put("templatePath", TEMPLATE_PATH); 77 context.put("generatorVersion", Constants.GENERATOR_NAME + " / " 78 + this.getShortDescription()); 79 context.put("date", new Date()); 80 context.put("dialect", this.ejb3Plugin); 81 context.put("pckName", pckName); 82 context.put("hibernateDialectHelper", new HibernateDialectHelper()); 83 context.put("annotations", String.valueOf(ejb3Plugin.isGenerateAnnotations())); 84 85 runVelocity(TEMPLATE_PATH, "hibernatedaofactory.vm", srcPath + "/" 86 + (pckName + ".hibernate").replace('.', '/') + "/", 87 "HibernateDAOFactory.java", context); 88 89 runVelocity(TEMPLATE_PATH, "daofactory.vm", srcPath + "/" 90 + pckName.replace('.', '/') + "/", "DAOFactory.java", context); 91 92 runVelocity(TEMPLATE_PATH, "abstracthibernatedao.vm", srcPath + "/" 93 + (pckName + ".hibernate").replace('.', '/') + "/", 94 "AbstractHibernateDAO.java", context); 95 96 runVelocity(TEMPLATE_PATH, "genericdao.vm", srcPath + "/" 97 + pckName.replace('.', '/') + "/", "GenericDAO.java", context); 98 99 runVelocity(TEMPLATE_PATH, "hibernateutil.vm", srcPath + "/" 100 + (pckName + ".hibernate").replace('.', '/') + "/", 101 "HibernateUtil.java", context); 102 103 runVelocity(TEMPLATE_PATH, "hibernate.cfg.xml.vm", srcPath + "/", 104 "hibernate.cfg.xml", context); 105 106 } 107 108 public void init(String doPrefix, int defaultNameOffset, 109 String srcPath, String pckName) throws Exception { 110 super.init(doPrefix, defaultNameOffset, srcPath, pckName); 111 ejb3Plugin = new EJB3Plugin(); 112 ejb3Plugin.init(doPrefix, defaultNameOffset, srcPath, 113 pckName + ".pojo"); 114 } 115 116 public EJB3Plugin getEjb3Plugin() { 117 return ejb3Plugin; 118 } 119 120 public void setEjb3Plugin(EJB3Plugin ejb3Plugin) { 121 this.ejb3Plugin = ejb3Plugin; 122 } 123 124 }