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 org.mybatis.generator.api.IntrospectedColumn;
19  import org.mybatis.generator.api.dom.xml.Attribute;
20  import org.mybatis.generator.api.dom.xml.TextElement;
21  import org.mybatis.generator.api.dom.xml.XmlElement;
22  import org.mybatis.generator.codegen.AbstractGenerator;
23  import org.mybatis.generator.config.GeneratedKey;
24  
25  /**
26   * 
27   * @author Jeff Butler
28   * 
29   */
30  public abstract class AbstractXmlElementGenerator extends AbstractGenerator {
31      public abstract void addElements(XmlElement parentElement);
32  
33      public AbstractXmlElementGenerator() {
34          super();
35      }
36  
37      /**
38       * This method should return an XmlElement for the select key used to
39       * automatically generate keys.
40       * 
41       * @param introspectedColumn
42       *            the column related to the select key statement
43       * @param generatedKey
44       *            the generated key for the current table
45       * @return the selectKey element
46       */
47      protected XmlElement getSelectKey(IntrospectedColumn introspectedColumn,
48              GeneratedKey generatedKey) {
49          String identityColumnType = introspectedColumn
50                  .getFullyQualifiedJavaType().getFullyQualifiedName();
51  
52          XmlElement answer = new XmlElement("selectKey"); //$NON-NLS-1$
53          answer.addAttribute(new Attribute("resultType", identityColumnType)); //$NON-NLS-1$
54          answer.addAttribute(new Attribute(
55                  "keyProperty", introspectedColumn.getJavaProperty())); //$NON-NLS-1$
56          answer.addAttribute(new Attribute("order", //$NON-NLS-1$
57                  generatedKey.getMyBatis3Order())); 
58          
59          answer.addElement(new TextElement(generatedKey
60                          .getRuntimeSqlStatement()));
61  
62          return answer;
63      }
64  
65      protected XmlElement getBaseColumnListElement() {
66          XmlElement answer = new XmlElement("include"); //$NON-NLS-1$
67          answer.addAttribute(new Attribute("refid", //$NON-NLS-1$
68                  introspectedTable.getBaseColumnListId()));
69          return answer;
70      }
71  
72      protected XmlElement getBlobColumnListElement() {
73          XmlElement answer = new XmlElement("include"); //$NON-NLS-1$
74          answer.addAttribute(new Attribute("refid", //$NON-NLS-1$
75                  introspectedTable.getBlobColumnListId()));
76          return answer;
77      }
78  
79      protected XmlElement getExampleIncludeElement() {
80          XmlElement ifElement = new XmlElement("if"); //$NON-NLS-1$
81          ifElement.addAttribute(new Attribute("test", "_parameter != null")); //$NON-NLS-1$ //$NON-NLS-2$
82  
83          XmlElement includeElement = new XmlElement("include"); //$NON-NLS-1$
84          includeElement.addAttribute(new Attribute("refid", //$NON-NLS-1$
85                  introspectedTable.getExampleWhereClauseId()));
86          ifElement.addElement(includeElement);
87  
88          return ifElement;
89      }
90  
91      protected XmlElement getUpdateByExampleIncludeElement() {
92          XmlElement ifElement = new XmlElement("if"); //$NON-NLS-1$
93          ifElement.addAttribute(new Attribute("test", "_parameter != null")); //$NON-NLS-1$ //$NON-NLS-2$
94  
95          XmlElement includeElement = new XmlElement("include"); //$NON-NLS-1$
96          includeElement.addAttribute(new Attribute("refid", //$NON-NLS-1$
97                  introspectedTable.getMyBatis3UpdateByExampleWhereClauseId()));
98          ifElement.addElement(includeElement);
99  
100         return ifElement;
101     }
102 }