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.plugins;
17  
18  import java.util.ArrayList;
19  import java.util.HashMap;
20  import java.util.Iterator;
21  import java.util.List;
22  import java.util.Map;
23  
24  import org.mybatis.generator.api.FullyQualifiedTable;
25  import org.mybatis.generator.api.IntrospectedTable;
26  import org.mybatis.generator.api.IntrospectedTable.TargetRuntime;
27  import org.mybatis.generator.api.PluginAdapter;
28  import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
29  import org.mybatis.generator.api.dom.java.Interface;
30  import org.mybatis.generator.api.dom.java.Method;
31  import org.mybatis.generator.api.dom.java.Parameter;
32  import org.mybatis.generator.api.dom.xml.Attribute;
33  import org.mybatis.generator.api.dom.xml.Document;
34  import org.mybatis.generator.api.dom.xml.XmlElement;
35  
36  /**
37   * This plugin will add selectByExample methods that include rowBounds
38   * parameters to the generated mapper interface.  This plugin is only
39   * valid for MyBatis3.
40   * 
41   * @author Jeff Butler
42   */
43  public class RowBoundsPlugin extends PluginAdapter {
44      
45      private FullyQualifiedJavaType rowBounds;
46      private Map<FullyQualifiedTable, List<XmlElement>> elementsToAdd;
47  
48      public RowBoundsPlugin() {
49          rowBounds = new FullyQualifiedJavaType("org.apache.ibatis.session.RowBounds"); //$NON-NLS-1$
50          elementsToAdd = new HashMap<FullyQualifiedTable, List<XmlElement>>();
51      }
52      
53      public boolean validate(List<String> warnings) {
54          return true;
55      }
56  
57      @Override
58      public boolean clientSelectByExampleWithBLOBsMethodGenerated(Method method,
59              Interface interfaze, IntrospectedTable introspectedTable) {
60          if (introspectedTable.getTargetRuntime() == TargetRuntime.MYBATIS3) {
61              copyAndAddMethod(method, interfaze);
62          }
63          return true;
64      }
65  
66      @Override
67      public boolean clientSelectByExampleWithoutBLOBsMethodGenerated(
68              Method method, Interface interfaze,
69              IntrospectedTable introspectedTable) {
70          if (introspectedTable.getTargetRuntime() == TargetRuntime.MYBATIS3) {
71              copyAndAddMethod(method, interfaze);
72          }
73          return true;
74      }
75  
76      @Override
77      public boolean sqlMapSelectByExampleWithoutBLOBsElementGenerated(
78              XmlElement element, IntrospectedTable introspectedTable) {
79          if (introspectedTable.getTargetRuntime() == TargetRuntime.MYBATIS3) {
80              copyAndSaveElement(element, introspectedTable.getFullyQualifiedTable());
81          }
82          return true;
83      }
84  
85      @Override
86      public boolean sqlMapSelectByExampleWithBLOBsElementGenerated(
87              XmlElement element, IntrospectedTable introspectedTable) {
88          if (introspectedTable.getTargetRuntime() == TargetRuntime.MYBATIS3) {
89              copyAndSaveElement(element, introspectedTable.getFullyQualifiedTable());
90          }
91          return true;
92      }
93  
94      /**
95       * We'll override this method and add any new elements generated by
96       * previous calls
97       */
98      @Override
99      public boolean sqlMapDocumentGenerated(Document document,
100             IntrospectedTable introspectedTable) {
101         List<XmlElement> elements = elementsToAdd.get(introspectedTable.getFullyQualifiedTable());
102         if (elements != null) {
103             for (XmlElement element : elements) {
104                 document.getRootElement().addElement(element);
105             }
106         }
107 
108         return true;
109     }
110     
111     /**
112      * Use the method copy constructor to create a new method, then
113      * add the rowBounds parameter.
114      * 
115      * @param fullyQualifiedTable
116      * @param method
117      */
118     private void copyAndAddMethod(Method method, Interface interfaze) {
119         Method newMethod = new Method(method);
120         newMethod.setName(method.getName() + "WithRowbounds"); //$NON-NLS-1$
121         newMethod.addParameter(new Parameter(rowBounds, "rowBounds")); //$NON-NLS-1$
122         interfaze.addMethod(newMethod);
123         interfaze.addImportedType(rowBounds);
124     }
125 
126     /**
127      * Use the method copy constructor to create a new element
128      * 
129      * @param fullyQualifiedTable
130      * @param method
131      */
132     private void copyAndSaveElement(XmlElement element, FullyQualifiedTable fqt) {
133         XmlElement newElement = new XmlElement(element);
134             
135         // remove old id attribute and add a new one with the new name
136         for (Iterator<Attribute> iterator = newElement.getAttributes().iterator(); iterator.hasNext();) {
137             Attribute attribute = iterator.next();
138             if ("id".equals(attribute.getName())) { //$NON-NLS-1$
139                 iterator.remove();
140                 Attribute newAttribute = new Attribute("id", attribute.getValue() + "WithRowbounds"); //$NON-NLS-1$ //$NON-NLS-2$
141                 newElement.addAttribute(newAttribute);
142                 break;
143             }
144         }
145             
146         // save the new element locally.   We'll add it to the document
147         // later
148         List<XmlElement> elements = elementsToAdd.get(fqt);
149         if (elements == null) {
150             elements = new ArrayList<XmlElement>();
151             elementsToAdd.put(fqt, elements);
152         }
153         elements.add(newElement);
154     }
155 }