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.model;
17  
18  import static org.mybatis.generator.internal.util.JavaBeansUtil.getJavaBeansField;
19  import static org.mybatis.generator.internal.util.JavaBeansUtil.getJavaBeansGetter;
20  import static org.mybatis.generator.internal.util.JavaBeansUtil.getJavaBeansSetter;
21  import static org.mybatis.generator.internal.util.messages.Messages.getString;
22  
23  import java.util.ArrayList;
24  import java.util.List;
25  
26  import org.mybatis.generator.api.CommentGenerator;
27  import org.mybatis.generator.api.FullyQualifiedTable;
28  import org.mybatis.generator.api.IntrospectedColumn;
29  import org.mybatis.generator.api.Plugin;
30  import org.mybatis.generator.api.dom.java.CompilationUnit;
31  import org.mybatis.generator.api.dom.java.Field;
32  import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
33  import org.mybatis.generator.api.dom.java.JavaVisibility;
34  import org.mybatis.generator.api.dom.java.Method;
35  import org.mybatis.generator.api.dom.java.TopLevelClass;
36  import org.mybatis.generator.codegen.AbstractJavaGenerator;
37  import org.mybatis.generator.codegen.RootClassInfo;
38  
39  /**
40   * 
41   * @author Jeff Butler
42   * 
43   */
44  public class PrimaryKeyGenerator extends AbstractJavaGenerator {
45  
46      public PrimaryKeyGenerator() {
47          super();
48      }
49  
50      @Override
51      public List<CompilationUnit> getCompilationUnits() {
52          FullyQualifiedTable table = introspectedTable.getFullyQualifiedTable();
53          progressCallback.startTask(getString(
54                  "Progress.7", table.toString())); //$NON-NLS-1$
55          Plugin plugins = context.getPlugins();
56          CommentGenerator commentGenerator = context.getCommentGenerator();
57  
58          TopLevelClass topLevelClass = new TopLevelClass(introspectedTable
59                  .getPrimaryKeyType());
60          topLevelClass.setVisibility(JavaVisibility.PUBLIC);
61          commentGenerator.addJavaFileComment(topLevelClass);
62  
63          String rootClass = getRootClass();
64          if (rootClass != null) {
65              topLevelClass.setSuperClass(new FullyQualifiedJavaType(rootClass));
66              topLevelClass.addImportedType(topLevelClass.getSuperClass());
67          }
68  
69          for (IntrospectedColumn introspectedColumn : introspectedTable
70                  .getPrimaryKeyColumns()) {
71              if (RootClassInfo.getInstance(rootClass, warnings)
72                      .containsProperty(introspectedColumn)) {
73                  continue;
74              }
75  
76              Field field = getJavaBeansField(introspectedColumn, context, introspectedTable);
77              if (plugins.modelFieldGenerated(field, topLevelClass,
78                      introspectedColumn, introspectedTable,
79                      Plugin.ModelClassType.PRIMARY_KEY)) {
80                  topLevelClass.addField(field);
81                  topLevelClass.addImportedType(field.getType());
82              }
83  
84              Method method = getJavaBeansGetter(introspectedColumn, context, introspectedTable);
85              if (plugins.modelGetterMethodGenerated(method, topLevelClass,
86                      introspectedColumn, introspectedTable,
87                      Plugin.ModelClassType.PRIMARY_KEY)) {
88                  topLevelClass.addMethod(method);
89              }
90  
91              method = getJavaBeansSetter(introspectedColumn, context, introspectedTable);
92              if (plugins.modelSetterMethodGenerated(method, topLevelClass,
93                      introspectedColumn, introspectedTable,
94                      Plugin.ModelClassType.PRIMARY_KEY)) {
95                  topLevelClass.addMethod(method);
96              }
97          }
98  
99          List<CompilationUnit> answer = new ArrayList<CompilationUnit>();
100         if (context.getPlugins().modelPrimaryKeyClassGenerated(
101                 topLevelClass, introspectedTable)) {
102             answer.add(topLevelClass);
103         }
104         return answer;
105     }
106 }