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.JavaBeansUtil.getSetterMethodName;
19  
20  import java.util.Set;
21  import java.util.TreeSet;
22  
23  import org.mybatis.generator.api.IntrospectedColumn;
24  import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
25  import org.mybatis.generator.api.dom.java.Interface;
26  import org.mybatis.generator.api.dom.java.JavaVisibility;
27  import org.mybatis.generator.api.dom.java.Method;
28  import org.mybatis.generator.api.dom.java.Parameter;
29  import org.mybatis.generator.api.dom.java.TopLevelClass;
30  
31  /**
32   * 
33   * @author Jeff Butler
34   * 
35   */
36  public class SelectByPrimaryKeyMethodGenerator extends
37          AbstractDAOElementGenerator {
38  
39      public SelectByPrimaryKeyMethodGenerator() {
40          super();
41      }
42  
43      @Override
44      public void addImplementationElements(TopLevelClass topLevelClass) {
45          Set<FullyQualifiedJavaType> importedTypes = new TreeSet<FullyQualifiedJavaType>();
46          Method method = getMethodShell(importedTypes);
47  
48          // generate the implementation method
49          StringBuilder sb = new StringBuilder();
50  
51          if (!introspectedTable.getRules().generatePrimaryKeyClass()) {
52              // no primary key class, but primary key is enabled. Primary
53              // key columns must be in the base class.
54              FullyQualifiedJavaType keyType = new FullyQualifiedJavaType(
55                      introspectedTable.getBaseRecordType());
56              topLevelClass.addImportedType(keyType);
57  
58              sb.setLength(0);
59              sb.append(keyType.getShortName());
60              sb.append(" _key = new "); //$NON-NLS-1$
61              sb.append(keyType.getShortName());
62              sb.append("();"); //$NON-NLS-1$
63              method.addBodyLine(sb.toString());
64  
65              for (IntrospectedColumn introspectedColumn : introspectedTable
66                      .getPrimaryKeyColumns()) {
67                  sb.setLength(0);
68                  sb.append("_key."); //$NON-NLS-1$
69                  sb.append(getSetterMethodName(introspectedColumn
70                          .getJavaProperty()));
71                  sb.append('(');
72                  sb.append(introspectedColumn.getJavaProperty());
73                  sb.append(");"); //$NON-NLS-1$
74                  method.addBodyLine(sb.toString());
75              }
76          }
77  
78          FullyQualifiedJavaType returnType = method.getReturnType();
79  
80          sb.setLength(0);
81          sb.append(returnType.getShortName());
82          sb.append(" record = ("); //$NON-NLS-1$
83          sb.append(returnType.getShortName());
84          sb.append(") "); //$NON-NLS-1$
85          sb.append(daoTemplate.getQueryForObjectMethod(introspectedTable
86                  .getIbatis2SqlMapNamespace(), introspectedTable
87                  .getSelectByPrimaryKeyStatementId(), "_key")); //$NON-NLS-1$
88          method.addBodyLine(sb.toString());
89          method.addBodyLine("return record;"); //$NON-NLS-1$
90  
91          if (context.getPlugins().clientSelectByPrimaryKeyMethodGenerated(
92                  method, topLevelClass, introspectedTable)) {
93              topLevelClass.addImportedTypes(importedTypes);
94              topLevelClass.addMethod(method);
95          }
96      }
97  
98      @Override
99      public void addInterfaceElements(Interface interfaze) {
100         Set<FullyQualifiedJavaType> importedTypes = new TreeSet<FullyQualifiedJavaType>();
101         Method method = getMethodShell(importedTypes);
102 
103         if (context.getPlugins().clientSelectByPrimaryKeyMethodGenerated(
104                 method, interfaze, introspectedTable)) {
105             interfaze.addImportedTypes(importedTypes);
106             interfaze.addMethod(method);
107         }
108     }
109 
110     private Method getMethodShell(Set<FullyQualifiedJavaType> importedTypes) {
111         Method method = new Method();
112         method.setVisibility(JavaVisibility.PUBLIC);
113 
114         FullyQualifiedJavaType returnType = introspectedTable.getRules()
115                 .calculateAllFieldsClass();
116         method.setReturnType(returnType);
117         importedTypes.add(returnType);
118 
119         method.setName(getDAOMethodNameCalculator()
120                 .getSelectByPrimaryKeyMethodName(introspectedTable));
121 
122         if (introspectedTable.getRules().generatePrimaryKeyClass()) {
123             FullyQualifiedJavaType type = new FullyQualifiedJavaType(
124                     introspectedTable.getPrimaryKeyType());
125             importedTypes.add(type);
126             method.addParameter(new Parameter(type, "_key")); //$NON-NLS-1$
127         } else {
128             for (IntrospectedColumn introspectedColumn : introspectedTable
129                     .getPrimaryKeyColumns()) {
130                 FullyQualifiedJavaType type = introspectedColumn
131                         .getFullyQualifiedJavaType();
132                 importedTypes.add(type);
133                 method.addParameter(new Parameter(type, introspectedColumn
134                         .getJavaProperty()));
135             }
136         }
137 
138         for (FullyQualifiedJavaType fqjt : daoTemplate.getCheckedExceptions()) {
139             method.addException(fqjt);
140             importedTypes.add(fqjt);
141         }
142 
143         context.getCommentGenerator().addGeneralMethodComment(method,
144                 introspectedTable);
145 
146         return method;
147     }
148 }