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.JavaVisibility;
33  import org.mybatis.generator.api.dom.java.Method;
34  import org.mybatis.generator.api.dom.java.TopLevelClass;
35  import org.mybatis.generator.codegen.AbstractJavaGenerator;
36  import org.mybatis.generator.codegen.RootClassInfo;
37  
38  /**
39   * 
40   * @author Jeff Butler
41   * 
42   */
43  public class RecordWithBLOBsGenerator extends AbstractJavaGenerator {
44  
45      public RecordWithBLOBsGenerator() {
46          super();
47      }
48  
49      @Override
50      public List<CompilationUnit> getCompilationUnits() {
51          FullyQualifiedTable table = introspectedTable.getFullyQualifiedTable();
52          progressCallback.startTask(getString(
53                  "Progress.9", table.toString())); //$NON-NLS-1$
54          Plugin plugins = context.getPlugins();
55          CommentGenerator commentGenerator = context.getCommentGenerator();
56  
57          TopLevelClass topLevelClass = new TopLevelClass(introspectedTable
58                  .getRecordWithBLOBsType());
59          topLevelClass.setVisibility(JavaVisibility.PUBLIC);
60          commentGenerator.addJavaFileComment(topLevelClass);
61  
62          if (introspectedTable.getRules().generateBaseRecordClass()) {
63              topLevelClass.setSuperClass(introspectedTable.getBaseRecordType());
64          } else {
65              topLevelClass.setSuperClass(introspectedTable.getPrimaryKeyType());
66          }
67  
68          String rootClass = getRootClass();
69          for (IntrospectedColumn introspectedColumn : introspectedTable
70                  .getBLOBColumns()) {
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.RECORD_WITH_BLOBS)) {
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.RECORD_WITH_BLOBS)) {
88                  topLevelClass.addMethod(method);
89              }
90  
91              method = getJavaBeansSetter(introspectedColumn, context, introspectedTable);
92              if (plugins.modelSetterMethodGenerated(method, topLevelClass,
93                      introspectedColumn, introspectedTable,
94                      Plugin.ModelClassType.RECORD_WITH_BLOBS)) {
95                  topLevelClass.addMethod(method);
96              }
97          }
98  
99          List<CompilationUnit> answer = new ArrayList<CompilationUnit>();
100         if (context.getPlugins().modelRecordWithBLOBsClassGenerated(
101                 topLevelClass, introspectedTable)) {
102             answer.add(topLevelClass);
103         }
104         return answer;
105     }
106 }