View Javadoc
1   /**
2    *    Copyright 2006-2015 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.internal.rules;
17  
18  import org.mybatis.generator.api.IntrospectedTable;
19  
20  /**
21   * This class encapsulates all the code generation rules for a table using the
22   * conditional model. In this model we do not generate primary key or record
23   * with BLOBs classes if the class would only hold one field.
24   * 
25   * @author Jeff Butler
26   * 
27   */
28  public class ConditionalModelRules extends BaseRules {
29  
30      /**
31       * 
32       */
33      public ConditionalModelRules(IntrospectedTable introspectedTable) {
34          super(introspectedTable);
35      }
36  
37      /**
38       * We generate a primary key if there is more than one primary key field.
39       * 
40       * @return true if the primary key should be generated
41       */
42      public boolean generatePrimaryKeyClass() {
43          return introspectedTable.getPrimaryKeyColumns().size() > 1;
44      }
45  
46      /**
47       * Generate a base record if there are any base columns, or if there is only
48       * one primary key coulmn (in which case we will not generate a primary key
49       * class), or if there is only one BLOB column (in which case we will not
50       * generate a record with BLOBs class).
51       * 
52       * @return true if the class should be generated
53       */
54      public boolean generateBaseRecordClass() {
55          return introspectedTable.getBaseColumns().size() > 0
56                  || introspectedTable.getPrimaryKeyColumns().size() == 1
57                  || (introspectedTable.getBLOBColumns().size() > 0 && !generateRecordWithBLOBsClass());
58  
59      }
60  
61      /**
62       * We generate a record with BLOBs class if there is more than one BLOB
63       * column. Do not generate a BLOBs class if any other super class would only
64       * contain one field
65       * 
66       * @return true if the record with BLOBs class should be generated
67       */
68      public boolean generateRecordWithBLOBsClass() {
69          int otherColumnCount = introspectedTable.getPrimaryKeyColumns().size()
70                  + introspectedTable.getBaseColumns().size();
71  
72          return otherColumnCount > 1
73                  && introspectedTable.getBLOBColumns().size() > 1;
74      }
75  }