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 DeleteByPrimaryKeyMethodGenerator extends
37          AbstractDAOElementGenerator {
38  
39      public DeleteByPrimaryKeyMethodGenerator() {
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          StringBuilder sb = new StringBuilder();
49  
50          if (!introspectedTable.getRules().generatePrimaryKeyClass()) {
51              // no primary key class, but primary key is enabled. Primary
52              // key columns must be in the base class.
53              FullyQualifiedJavaType keyType = new FullyQualifiedJavaType(
54                      introspectedTable.getBaseRecordType());
55              topLevelClass.addImportedType(keyType);
56  
57              sb.setLength(0);
58              sb.append(keyType.getShortName());
59              sb.append(" _key = new "); //$NON-NLS-1$
60              sb.append(keyType.getShortName());
61              sb.append("();"); //$NON-NLS-1$
62              method.addBodyLine(sb.toString());
63  
64              for (IntrospectedColumn introspectedColumn : introspectedTable
65                      .getPrimaryKeyColumns()) {
66                  sb.setLength(0);
67                  sb.append("_key."); //$NON-NLS-1$
68                  sb.append(getSetterMethodName(introspectedColumn
69                          .getJavaProperty()));
70                  sb.append('(');
71                  sb.append(introspectedColumn.getJavaProperty());
72                  sb.append(");"); //$NON-NLS-1$
73                  method.addBodyLine(sb.toString());
74              }
75          }
76  
77          sb.setLength(0);
78          sb.append("int rows = "); //$NON-NLS-1$
79          sb.append(daoTemplate.getDeleteMethod(introspectedTable
80                  .getIbatis2SqlMapNamespace(), introspectedTable
81                  .getDeleteByPrimaryKeyStatementId(), "_key")); //$NON-NLS-1$
82          method.addBodyLine(sb.toString());
83          method.addBodyLine("return rows;"); //$NON-NLS-1$
84  
85          if (context.getPlugins().clientDeleteByPrimaryKeyMethodGenerated(
86                  method, topLevelClass, introspectedTable)) {
87              topLevelClass.addImportedTypes(importedTypes);
88              topLevelClass.addMethod(method);
89          }
90      }
91  
92      @Override
93      public void addInterfaceElements(Interface interfaze) {
94          Set<FullyQualifiedJavaType> importedTypes = new TreeSet<FullyQualifiedJavaType>();
95          Method method = getMethodShell(importedTypes);
96  
97          if (context.getPlugins().clientDeleteByPrimaryKeyMethodGenerated(
98                  method, interfaze, introspectedTable)) {
99              interfaze.addImportedTypes(importedTypes);
100             interfaze.addMethod(method);
101         }
102     }
103 
104     private Method getMethodShell(Set<FullyQualifiedJavaType> importedTypes) {
105         Method method = new Method();
106         method.setVisibility(JavaVisibility.PUBLIC);
107         method.setReturnType(FullyQualifiedJavaType.getIntInstance());
108         method.setName(getDAOMethodNameCalculator()
109                 .getDeleteByPrimaryKeyMethodName(introspectedTable));
110 
111         if (introspectedTable.getRules().generatePrimaryKeyClass()) {
112             FullyQualifiedJavaType type = new FullyQualifiedJavaType(
113                     introspectedTable.getPrimaryKeyType());
114             importedTypes.add(type);
115             method.addParameter(new Parameter(type, "_key")); //$NON-NLS-1$
116         } else {
117             for (IntrospectedColumn introspectedColumn : introspectedTable
118                     .getPrimaryKeyColumns()) {
119                 FullyQualifiedJavaType type = introspectedColumn
120                         .getFullyQualifiedJavaType();
121                 importedTypes.add(type);
122                 method.addParameter(new Parameter(type, introspectedColumn
123                         .getJavaProperty()));
124             }
125         }
126 
127         for (FullyQualifiedJavaType fqjt : daoTemplate.getCheckedExceptions()) {
128             method.addException(fqjt);
129             importedTypes.add(fqjt);
130         }
131 
132         context.getCommentGenerator().addGeneralMethodComment(method,
133                 introspectedTable);
134 
135         return method;
136     }
137 }