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.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 * Instantiates a new conditional model rules.
32 *
33 * @param introspectedTable
34 * the introspected table
35 */
36 public ConditionalModelRules(IntrospectedTable introspectedTable) {
37 super(introspectedTable);
38 }
39
40 /**
41 * We generate a primary key if there is more than one primary key field.
42 *
43 * @return true if the primary key should be generated
44 */
45 public boolean generatePrimaryKeyClass() {
46 return introspectedTable.getPrimaryKeyColumns().size() > 1;
47 }
48
49 /**
50 * Generate a base record if there are any base columns, or if there is only
51 * one primary key coulmn (in which case we will not generate a primary key
52 * class), or if there is only one BLOB column (in which case we will not
53 * generate a record with BLOBs class).
54 *
55 * @return true if the class should be generated
56 */
57 public boolean generateBaseRecordClass() {
58 return introspectedTable.hasBaseColumns()
59 || introspectedTable.getPrimaryKeyColumns().size() == 1
60 || blobsAreInBaseRecord();
61 }
62
63 /**
64 * Blobs will be in the base record class if there is only one blob column
65 *
66 * @return true if there are blobs but they are in the base record class
67 */
68 private boolean blobsAreInBaseRecord() {
69 return introspectedTable.hasBLOBColumns() && !generateRecordWithBLOBsClass();
70 }
71
72 /**
73 * We generate a record with BLOBs class if there is more than one BLOB
74 * column. Do not generate a BLOBs class if any other super class would only
75 * contain one field
76 *
77 * @return true if the record with BLOBs class should be generated
78 */
79 public boolean generateRecordWithBLOBsClass() {
80 int otherColumnCount = introspectedTable.getPrimaryKeyColumns().size()
81 + introspectedTable.getBaseColumns().size();
82
83 return otherColumnCount > 1
84 && introspectedTable.getBLOBColumns().size() > 1;
85 }
86 }