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.sqlmap.elements;
17  
18  import static org.mybatis.generator.internal.util.StringUtility.stringHasValue;
19  
20  import org.mybatis.generator.api.IntrospectedColumn;
21  import org.mybatis.generator.api.dom.xml.Attribute;
22  import org.mybatis.generator.api.dom.xml.TextElement;
23  import org.mybatis.generator.api.dom.xml.XmlElement;
24  import org.mybatis.generator.codegen.AbstractGenerator;
25  import org.mybatis.generator.config.GeneratedKey;
26  
27  /**
28   * 
29   * @author Jeff Butler
30   * 
31   */
32  public abstract class AbstractXmlElementGenerator extends AbstractGenerator {
33      public abstract void addElements(XmlElement parentElement);
34  
35      public AbstractXmlElementGenerator() {
36          super();
37      }
38  
39      /**
40       * This method should return an XmlElement for the select key used to
41       * automatically generate keys.
42       * 
43       * @param introspectedColumn
44       *            the column related to the select key statement
45       * @param generatedKey
46       *            the generated key for the current table
47       * @return the selectKey element
48       */
49      protected XmlElement getSelectKey(IntrospectedColumn introspectedColumn,
50              GeneratedKey generatedKey) {
51          String identityColumnType = introspectedColumn
52                  .getFullyQualifiedJavaType().getFullyQualifiedName();
53  
54          XmlElement answer = new XmlElement("selectKey"); //$NON-NLS-1$
55          answer.addAttribute(new Attribute("resultClass", identityColumnType)); //$NON-NLS-1$
56          answer.addAttribute(new Attribute(
57                  "keyProperty", introspectedColumn.getJavaProperty())); //$NON-NLS-1$
58          if (stringHasValue(generatedKey.getType())) {
59              answer.addAttribute(new Attribute("type", generatedKey.getType())); //$NON-NLS-1$  
60          }
61          answer
62                  .addElement(new TextElement(generatedKey
63                          .getRuntimeSqlStatement()));
64  
65          return answer;
66      }
67  
68      protected XmlElement getBaseColumnListElement() {
69          XmlElement answer = new XmlElement("include"); //$NON-NLS-1$
70          answer.addAttribute(new Attribute("refid", //$NON-NLS-1$
71                  introspectedTable.getIbatis2SqlMapNamespace()
72                          + "." + introspectedTable.getBaseColumnListId())); //$NON-NLS-1$
73          return answer;
74      }
75  
76      protected XmlElement getBlobColumnListElement() {
77          XmlElement answer = new XmlElement("include"); //$NON-NLS-1$
78          answer.addAttribute(new Attribute("refid", //$NON-NLS-1$
79                  introspectedTable.getIbatis2SqlMapNamespace()
80                          + "." + introspectedTable.getBlobColumnListId())); //$NON-NLS-1$
81          return answer;
82      }
83  }