1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.geekologue.md4j.generators;
18
19 import java.util.Map;
20 import org.apache.tools.ant.Project;
21 import org.apache.tools.ant.Task;
22 import org.apache.tools.ant.util.FileNameMapper;
23 import org.apache.tools.ant.util.RegexpPatternMapper;
24 import com.geekologue.md4j.tools.ant.HibernateMappingProcessorLiason;
25 import com.geekologue.md4j.tools.ant.HibernateMappingProcessorTask;
26
27 /***
28 * Context parameters:
29 * <ul>
30 * <li>singletonDaos: Whether to use JVM Singleton DAOs
31 * </ul>
32 * Please not that if singletonDaos is set to true, your Hibernate configuration
33 * file should include the following line: <property
34 * name="current_session_context_class">thread</property>
35 *
36 * @author manos
37 */
38 public class HibernateDaoGenerator extends AbstractXsltBasedGenerator implements
39 HibernateMappingProcessorLiason {
40 private static final String XSLT_PATH = "md4j-dao-hbm.xsl";
41
42 /***
43 *
44 */
45 public HibernateDaoGenerator() {
46 super(HibernateDaoGenerator.XSLT_PATH);
47 }
48
49 /***
50 * Equal to <regexpmapper handledirsep="true" from="(.*)/(.*).hbm.xml"
51 * to="\1/dao/\2DAO.java" />
52 *
53 * @see com.geekologue.md4j.tools.ant.HibernateMappingProcessorLiason#getSimpleMapper()
54 */
55 public FileNameMapper getSimpleMapper() {
56 RegexpPatternMapper simpleMapper = new RegexpPatternMapper();
57 simpleMapper.setHandleDirSep(true);
58 simpleMapper.setFrom("(.*)/(.*).hbm.xml");
59 simpleMapper.setTo("//1/dao///2DAO.java");
60 return simpleMapper;
61 }
62
63 /***
64 *
65 * @see com.geekologue.md4j.tools.ant.HibernateMappingProcessorLiason#getSimpleMapper()
66 */
67 public FileNameMapper getNestedMapper() {
68 throw new RuntimeException("Nested mapppers are yet to be suported");
69 }
70
71 /***
72 * @see com.geekologue.md4j.tools.ant.HibernateMappingProcessorLiason#init(java.util.Map)
73 */
74 public void init(Map context) {
75 this.styleParameters.put("package", context.get("package"));
76 String singletonDaos = ((String) context.get("singletonDaos"))
77 .toLowerCase();
78 this.styleParameters.put("singletonDaos", singletonDaos);
79 if (singletonDaos.equals("true")) {
80 this.task = (HibernateMappingProcessorTask) context.get("task");
81 if (this.task != null) {
82 this.task
83 .log(
84 "You are using Singleton DAOs. Please set the 'current_session_context_class' property of your Hibernate configuration to 'thread'",
85 Project.MSG_WARN);
86 }
87 }
88 super.init(context);
89 }
90 }