View Javadoc
1   /**
2    *    Copyright 2006-2016 the original author or authors.
3    *
4    *    Licensed under the Apache 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.apache.org/licenses/LICENSE-2.0
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  package org.mybatis.generator.codegen.ibatis2.dao.elements;
17  
18  import static org.mybatis.generator.internal.util.StringUtility.stringHasValue;
19  import static org.mybatis.generator.internal.util.messages.Messages.getString;
20  
21  import org.mybatis.generator.api.DAOMethodNameCalculator;
22  import org.mybatis.generator.api.dom.java.Interface;
23  import org.mybatis.generator.api.dom.java.JavaVisibility;
24  import org.mybatis.generator.api.dom.java.TopLevelClass;
25  import org.mybatis.generator.codegen.AbstractGenerator;
26  import org.mybatis.generator.codegen.ibatis2.dao.templates.AbstractDAOTemplate;
27  import org.mybatis.generator.config.PropertyRegistry;
28  import org.mybatis.generator.internal.DefaultDAOMethodNameCalculator;
29  import org.mybatis.generator.internal.ExtendedDAOMethodNameCalculator;
30  import org.mybatis.generator.internal.ObjectFactory;
31  
32  /**
33   * 
34   * @author Jeff Butler
35   */
36  public abstract class AbstractDAOElementGenerator extends AbstractGenerator {
37      public abstract void addInterfaceElements(Interface interfaze);
38  
39      public abstract void addImplementationElements(TopLevelClass topLevelClass);
40  
41      protected AbstractDAOTemplate daoTemplate;
42      private DAOMethodNameCalculator dAOMethodNameCalculator;
43      private JavaVisibility exampleMethodVisibility;
44  
45      public AbstractDAOElementGenerator() {
46          super();
47      }
48  
49      public void setDAOTemplate(AbstractDAOTemplate abstractDAOTemplate) {
50          this.daoTemplate = abstractDAOTemplate;
51      }
52  
53      public DAOMethodNameCalculator getDAOMethodNameCalculator() {
54          if (dAOMethodNameCalculator == null) {
55              String type = context.getJavaClientGeneratorConfiguration()
56                      .getProperty(PropertyRegistry.DAO_METHOD_NAME_CALCULATOR);
57              if (stringHasValue(type)) {
58                  if ("extended".equalsIgnoreCase(type)) { //$NON-NLS-1$
59                      type = ExtendedDAOMethodNameCalculator.class.getName();
60                  } else if ("default".equalsIgnoreCase(type)) { //$NON-NLS-1$
61                      type = DefaultDAOMethodNameCalculator.class.getName();
62                  }
63              } else {
64                  type = DefaultDAOMethodNameCalculator.class.getName();
65              }
66  
67              try {
68                  dAOMethodNameCalculator = (DAOMethodNameCalculator) ObjectFactory
69                          .createInternalObject(type);
70              } catch (Exception e) {
71                  dAOMethodNameCalculator = new DefaultDAOMethodNameCalculator();
72                  warnings.add(getString(
73                          "Warning.17", type, e.getMessage())); //$NON-NLS-1$
74              }
75          }
76  
77          return dAOMethodNameCalculator;
78      }
79  
80      public JavaVisibility getExampleMethodVisibility() {
81          if (exampleMethodVisibility == null) {
82              String type = context
83                      .getJavaClientGeneratorConfiguration()
84                      .getProperty(PropertyRegistry.DAO_EXAMPLE_METHOD_VISIBILITY);
85              if (stringHasValue(type)) {
86                  if ("public".equalsIgnoreCase(type)) { //$NON-NLS-1$
87                      exampleMethodVisibility = JavaVisibility.PUBLIC;
88                  } else if ("private".equalsIgnoreCase(type)) { //$NON-NLS-1$
89                      exampleMethodVisibility = JavaVisibility.PRIVATE;
90                  } else if ("protected".equalsIgnoreCase(type)) { //$NON-NLS-1$
91                      exampleMethodVisibility = JavaVisibility.PROTECTED;
92                  } else if ("default".equalsIgnoreCase(type)) { //$NON-NLS-1$
93                      exampleMethodVisibility = JavaVisibility.DEFAULT;
94                  } else {
95                      exampleMethodVisibility = JavaVisibility.PUBLIC;
96                      warnings.add(getString("Warning.16", type)); //$NON-NLS-1$
97                  }
98              } else {
99                  exampleMethodVisibility = JavaVisibility.PUBLIC;
100             }
101         }
102 
103         return exampleMethodVisibility;
104     }
105 }