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 UpdateByPrimaryKeyWithoutBLOBsMethodGenerator extends
34          AbstractDAOElementGenerator {
35  
36      public UpdateByPrimaryKeyWithoutBLOBsMethodGenerator() {
37          super();
38      }
39  
40      @Override
41      public void addImplementationElements(TopLevelClass topLevelClass) {
42          Set<FullyQualifiedJavaType> importedTypes = new TreeSet<FullyQualifiedJavaType>();
43          Method method = getMethodShell(importedTypes);
44  
45          StringBuilder sb = new StringBuilder();
46          sb.append("int rows = "); //$NON-NLS-1$
47          sb.append(daoTemplate.getUpdateMethod(introspectedTable
48                  .getIbatis2SqlMapNamespace(), introspectedTable
49                  .getUpdateByPrimaryKeyStatementId(), "record")); //$NON-NLS-1$
50          method.addBodyLine(sb.toString());
51  
52          method.addBodyLine("return rows;"); //$NON-NLS-1$
53  
54          if (context.getPlugins()
55                  .clientUpdateByPrimaryKeyWithoutBLOBsMethodGenerated(method,
56                          topLevelClass, introspectedTable)) {
57              topLevelClass.addImportedTypes(importedTypes);
58              topLevelClass.addMethod(method);
59          }
60      }
61  
62      @Override
63      public void addInterfaceElements(Interface interfaze) {
64          Set<FullyQualifiedJavaType> importedTypes = new TreeSet<FullyQualifiedJavaType>();
65          Method method = getMethodShell(importedTypes);
66  
67          if (context.getPlugins()
68                  .clientUpdateByPrimaryKeyWithoutBLOBsMethodGenerated(method,
69                          interfaze, introspectedTable)) {
70              interfaze.addImportedTypes(importedTypes);
71              interfaze.addMethod(method);
72          }
73      }
74  
75      private Method getMethodShell(Set<FullyQualifiedJavaType> importedTypes) {
76          FullyQualifiedJavaType parameterType = new FullyQualifiedJavaType(
77                  introspectedTable.getBaseRecordType());
78          importedTypes.add(parameterType);
79  
80          Method method = new Method();
81          method.setVisibility(JavaVisibility.PUBLIC);
82          method.setReturnType(FullyQualifiedJavaType.getIntInstance());
83          method
84                  .setName(getDAOMethodNameCalculator()
85                          .getUpdateByPrimaryKeyWithoutBLOBsMethodName(
86                                  introspectedTable));
87          method.addParameter(new Parameter(parameterType, "record")); //$NON-NLS-1$
88  
89          for (FullyQualifiedJavaType fqjt : daoTemplate.getCheckedExceptions()) {
90              method.addException(fqjt);
91              importedTypes.add(fqjt);
92          }
93  
94          context.getCommentGenerator().addGeneralMethodComment(method,
95                  introspectedTable);
96  
97          return method;
98      }
99  }