View Javadoc

1   /*
2    * Copyright Emmanouil Batsis
3    * 
4    * Licensed under the GNU General Public License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.gnu.org/licenses/gpl.txt
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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: &lt;property
34   * name="current_session_context_class"&gt;thread&lt;/property&gt;
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 &lt;regexpmapper handledirsep="true" from="(.*)/(.*).hbm.xml"
51       * to="\1/dao/\2DAO.java" /&gt;
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  }