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