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