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 java.util.Set;
19  import java.util.TreeSet;
20  
21  import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
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.Method;
25  import org.mybatis.generator.api.dom.java.Parameter;
26  import org.mybatis.generator.api.dom.java.TopLevelClass;
27  
28  /**
29   * 
30   * @author Jeff Butler
31   * 
32   */
33  public class CountByExampleMethodGenerator extends AbstractDAOElementGenerator {
34  
35      private boolean generateForJava5;
36  
37      public CountByExampleMethodGenerator(boolean generateForJava5) {
38          super();
39          this.generateForJava5 = generateForJava5;
40      }
41  
42      @Override
43      public void addImplementationElements(TopLevelClass topLevelClass) {
44          Set<FullyQualifiedJavaType> importedTypes = new TreeSet<FullyQualifiedJavaType>();
45          Method method = getMethodShell(importedTypes);
46  
47          // generate the implementation method
48          StringBuilder sb = new StringBuilder();
49  
50          sb.append("Integer count = (Integer)  "); //$NON-NLS-1$
51          sb.append(daoTemplate.getQueryForObjectMethod(introspectedTable
52                  .getIbatis2SqlMapNamespace(), introspectedTable
53                  .getCountByExampleStatementId(), "example")); //$NON-NLS-1$
54          method.addBodyLine(sb.toString());
55  
56          if (generateForJava5) {
57              method.addBodyLine("return count;"); //$NON-NLS-1$
58          } else {
59              method.addBodyLine("return count.intValue();"); //$NON-NLS-1$
60          }
61  
62          if (context.getPlugins().clientCountByExampleMethodGenerated(method,
63                  topLevelClass, introspectedTable)) {
64              topLevelClass.addImportedTypes(importedTypes);
65              topLevelClass.addMethod(method);
66          }
67      }
68  
69      @Override
70      public void addInterfaceElements(Interface interfaze) {
71          if (getExampleMethodVisibility() == JavaVisibility.PUBLIC) {
72              Set<FullyQualifiedJavaType> importedTypes = new TreeSet<FullyQualifiedJavaType>();
73              Method method = getMethodShell(importedTypes);
74  
75              if (context.getPlugins().clientCountByExampleMethodGenerated(
76                      method, interfaze, introspectedTable)) {
77                  interfaze.addImportedTypes(importedTypes);
78                  interfaze.addMethod(method);
79              }
80          }
81      }
82  
83      private Method getMethodShell(Set<FullyQualifiedJavaType> importedTypes) {
84          FullyQualifiedJavaType type = new FullyQualifiedJavaType(
85                  introspectedTable.getExampleType());
86          importedTypes.add(type);
87  
88          Method method = new Method();
89          method.setVisibility(getExampleMethodVisibility());
90          method.setReturnType(FullyQualifiedJavaType.getIntInstance());
91          method.setName(getDAOMethodNameCalculator()
92                  .getCountByExampleMethodName(introspectedTable));
93          method.addParameter(new Parameter(type, "example")); //$NON-NLS-1$
94  
95          for (FullyQualifiedJavaType fqjt : daoTemplate.getCheckedExceptions()) {
96              method.addException(fqjt);
97              importedTypes.add(fqjt);
98          }
99  
100         context.getCommentGenerator().addGeneralMethodComment(method,
101                 introspectedTable);
102 
103         return method;
104     }
105 }