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.mybatis3.xmlmapper.elements;
17  
18  import static org.mybatis.generator.internal.util.StringUtility.stringHasValue;
19  
20  import java.util.List;
21  
22  import org.mybatis.generator.api.IntrospectedColumn;
23  import org.mybatis.generator.api.dom.xml.Attribute;
24  import org.mybatis.generator.api.dom.xml.XmlElement;
25  import org.mybatis.generator.codegen.mybatis3.MyBatis3FormattingUtilities;
26  
27  /**
28   * 
29   * @author Jeff Butler
30   * 
31   */
32  public class ResultMapWithoutBLOBsElementGenerator extends
33          AbstractXmlElementGenerator {
34  
35      private boolean isSimple;
36  
37      public ResultMapWithoutBLOBsElementGenerator(boolean isSimple) {
38          super();
39          this.isSimple = isSimple;
40      }
41  
42      @Override
43      public void addElements(XmlElement parentElement) {
44          XmlElement answer = new XmlElement("resultMap"); //$NON-NLS-1$
45          answer.addAttribute(new Attribute("id", //$NON-NLS-1$
46                  introspectedTable.getBaseResultMapId()));
47  
48          String returnType;
49          if (isSimple) {
50              returnType = introspectedTable.getBaseRecordType();
51          } else {
52              if (introspectedTable.getRules().generateBaseRecordClass()) {
53                  returnType = introspectedTable.getBaseRecordType();
54              } else {
55                  returnType = introspectedTable.getPrimaryKeyType();
56              }
57          }
58  
59          answer.addAttribute(new Attribute("type", //$NON-NLS-1$
60                  returnType));
61  
62          context.getCommentGenerator().addComment(answer);
63  
64          if (introspectedTable.isConstructorBased()) {
65              addResultMapConstructorElements(answer);
66          } else {
67              addResultMapElements(answer);
68          }
69  
70          if (context.getPlugins().sqlMapResultMapWithoutBLOBsElementGenerated(
71                  answer, introspectedTable)) {
72              parentElement.addElement(answer);
73          }
74      }
75  
76      private void addResultMapElements(XmlElement answer) {
77          for (IntrospectedColumn introspectedColumn : introspectedTable
78                  .getPrimaryKeyColumns()) {
79              XmlElement resultElement = new XmlElement("id"); //$NON-NLS-1$
80  
81              resultElement
82                      .addAttribute(new Attribute(
83                              "column", MyBatis3FormattingUtilities.getRenamedColumnNameForResultMap(introspectedColumn))); //$NON-NLS-1$
84              resultElement.addAttribute(new Attribute(
85                      "property", introspectedColumn.getJavaProperty())); //$NON-NLS-1$
86              resultElement.addAttribute(new Attribute("jdbcType", //$NON-NLS-1$
87                      introspectedColumn.getJdbcTypeName()));
88  
89              if (stringHasValue(introspectedColumn.getTypeHandler())) {
90                  resultElement.addAttribute(new Attribute(
91                          "typeHandler", introspectedColumn.getTypeHandler())); //$NON-NLS-1$
92              }
93  
94              answer.addElement(resultElement);
95          }
96  
97          List<IntrospectedColumn> columns;
98          if (isSimple) {
99              columns = introspectedTable.getNonPrimaryKeyColumns();
100         } else {
101             columns = introspectedTable.getBaseColumns();
102         }
103         for (IntrospectedColumn introspectedColumn : columns) {
104             XmlElement resultElement = new XmlElement("result"); //$NON-NLS-1$
105 
106             resultElement
107                     .addAttribute(new Attribute(
108                             "column", MyBatis3FormattingUtilities.getRenamedColumnNameForResultMap(introspectedColumn))); //$NON-NLS-1$
109             resultElement.addAttribute(new Attribute(
110                     "property", introspectedColumn.getJavaProperty())); //$NON-NLS-1$
111             resultElement.addAttribute(new Attribute("jdbcType", //$NON-NLS-1$
112                     introspectedColumn.getJdbcTypeName()));
113 
114             if (stringHasValue(introspectedColumn.getTypeHandler())) {
115                 resultElement.addAttribute(new Attribute(
116                         "typeHandler", introspectedColumn.getTypeHandler())); //$NON-NLS-1$
117             }
118 
119             answer.addElement(resultElement);
120         }
121     }
122 
123     private void addResultMapConstructorElements(XmlElement answer) {
124         XmlElement constructor = new XmlElement("constructor"); //$NON-NLS-1$
125 
126         for (IntrospectedColumn introspectedColumn : introspectedTable
127                 .getPrimaryKeyColumns()) {
128             XmlElement resultElement = new XmlElement("idArg"); //$NON-NLS-1$
129 
130             resultElement
131                     .addAttribute(new Attribute(
132                             "column", MyBatis3FormattingUtilities.getRenamedColumnNameForResultMap(introspectedColumn))); //$NON-NLS-1$
133             resultElement.addAttribute(new Attribute("jdbcType", //$NON-NLS-1$
134                     introspectedColumn.getJdbcTypeName()));
135             resultElement.addAttribute(new Attribute("javaType", //$NON-NLS-1$
136                     introspectedColumn.getFullyQualifiedJavaType()
137                             .getFullyQualifiedName()));
138 
139             if (stringHasValue(introspectedColumn.getTypeHandler())) {
140                 resultElement.addAttribute(new Attribute(
141                         "typeHandler", introspectedColumn.getTypeHandler())); //$NON-NLS-1$
142             }
143 
144             constructor.addElement(resultElement);
145         }
146 
147         List<IntrospectedColumn> columns;
148         if (isSimple) {
149             columns = introspectedTable.getNonPrimaryKeyColumns();
150         } else {
151             columns = introspectedTable.getBaseColumns();
152         }
153         for (IntrospectedColumn introspectedColumn : columns) {
154             XmlElement resultElement = new XmlElement("arg"); //$NON-NLS-1$
155 
156             resultElement
157                     .addAttribute(new Attribute(
158                             "column", MyBatis3FormattingUtilities.getRenamedColumnNameForResultMap(introspectedColumn))); //$NON-NLS-1$
159             resultElement.addAttribute(new Attribute("jdbcType", //$NON-NLS-1$
160                     introspectedColumn.getJdbcTypeName()));
161             resultElement.addAttribute(new Attribute("javaType", //$NON-NLS-1$
162                     introspectedColumn.getFullyQualifiedJavaType()
163                             .getFullyQualifiedName()));
164 
165             if (stringHasValue(introspectedColumn.getTypeHandler())) {
166                 resultElement.addAttribute(new Attribute(
167                         "typeHandler", introspectedColumn.getTypeHandler())); //$NON-NLS-1$
168             }
169 
170             constructor.addElement(resultElement);
171         }
172 
173         answer.addElement(constructor);
174     }
175 }