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.DAOMethodNameCalculator;
22  import org.mybatis.generator.api.IntrospectedColumn;
23  import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
24  import org.mybatis.generator.api.dom.java.Interface;
25  import org.mybatis.generator.api.dom.java.JavaVisibility;
26  import org.mybatis.generator.api.dom.java.Method;
27  import org.mybatis.generator.api.dom.java.Parameter;
28  import org.mybatis.generator.api.dom.java.PrimitiveTypeWrapper;
29  import org.mybatis.generator.api.dom.java.TopLevelClass;
30  
31  /**
32   * 
33   * @author Jeff Butler
34   * 
35   */
36  public class InsertMethodGenerator extends AbstractDAOElementGenerator {
37  
38      public InsertMethodGenerator() {
39          super();
40      }
41  
42      @Override
43      public void addImplementationElements(TopLevelClass topLevelClass) {
44          Set<FullyQualifiedJavaType> importedTypes = new TreeSet<FullyQualifiedJavaType>();
45          Method method = getMethodShell(importedTypes);
46  
47          FullyQualifiedJavaType returnType = method.getReturnType();
48  
49          StringBuilder sb = new StringBuilder();
50  
51          if (returnType != null) {
52              sb.append("Object newKey = "); //$NON-NLS-1$
53          }
54  
55          sb.append(daoTemplate.getInsertMethod(introspectedTable
56                  .getIbatis2SqlMapNamespace(), introspectedTable
57                  .getInsertStatementId(), "record")); //$NON-NLS-1$
58          method.addBodyLine(sb.toString());
59  
60          if (returnType != null) {
61              if ("Object".equals(returnType.getShortName())) { //$NON-NLS-1$
62                  // no need to cast if the return type is Object
63                  method.addBodyLine("return newKey;"); //$NON-NLS-1$
64              } else {
65                  sb.setLength(0);
66  
67                  if (returnType.isPrimitive()) {
68                      PrimitiveTypeWrapper ptw = returnType
69                              .getPrimitiveTypeWrapper();
70                      sb.append("return (("); //$NON-NLS-1$
71                      sb.append(ptw.getShortName());
72                      sb.append(") newKey"); //$NON-NLS-1$
73                      sb.append(")."); //$NON-NLS-1$
74                      sb.append(ptw.getToPrimitiveMethod());
75                      sb.append(';');
76                  } else {
77                      sb.append("return ("); //$NON-NLS-1$
78                      sb.append(returnType.getShortName());
79                      sb.append(") newKey;"); //$NON-NLS-1$
80                  }
81  
82                  method.addBodyLine(sb.toString());
83              }
84          }
85  
86          if (context.getPlugins().clientInsertMethodGenerated(method,
87                  topLevelClass, introspectedTable)) {
88              topLevelClass.addImportedTypes(importedTypes);
89              topLevelClass.addMethod(method);
90          }
91      }
92  
93      @Override
94      public void addInterfaceElements(Interface interfaze) {
95          Set<FullyQualifiedJavaType> importedTypes = new TreeSet<FullyQualifiedJavaType>();
96          Method method = getMethodShell(importedTypes);
97  
98          if (context.getPlugins().clientInsertMethodGenerated(method,
99                  interfaze, introspectedTable)) {
100             interfaze.addImportedTypes(importedTypes);
101             interfaze.addMethod(method);
102         }
103     }
104 
105     private Method getMethodShell(Set<FullyQualifiedJavaType> importedTypes) {
106         Method method = new Method();
107 
108         FullyQualifiedJavaType returnType;
109         if (introspectedTable.getGeneratedKey() != null) {
110             IntrospectedColumn introspectedColumn = introspectedTable
111                     .getColumn(introspectedTable.getGeneratedKey().getColumn());
112             if (introspectedColumn == null) {
113                 // the specified column doesn't exist, so don't do the generated
114                 // key
115                 // (the warning has already been reported)
116                 returnType = null;
117             } else {
118                 returnType = introspectedColumn.getFullyQualifiedJavaType();
119                 importedTypes.add(returnType);
120             }
121         } else {
122             returnType = null;
123         }
124 
125         method.setReturnType(returnType);
126         method.setVisibility(JavaVisibility.PUBLIC);
127         DAOMethodNameCalculator methodNameCalculator = getDAOMethodNameCalculator();
128         method.setName(methodNameCalculator
129                 .getInsertMethodName(introspectedTable));
130 
131         FullyQualifiedJavaType parameterType = introspectedTable.getRules()
132                 .calculateAllFieldsClass();
133 
134         importedTypes.add(parameterType);
135         method.addParameter(new Parameter(parameterType, "record")); //$NON-NLS-1$
136 
137         for (FullyQualifiedJavaType fqjt : daoTemplate.getCheckedExceptions()) {
138             method.addException(fqjt);
139             importedTypes.add(fqjt);
140         }
141 
142         context.getCommentGenerator().addGeneralMethodComment(method,
143                 introspectedTable);
144 
145         return method;
146     }
147 }