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.List;
19  
20  import org.mybatis.generator.api.PluginAdapter;
21  import org.mybatis.generator.api.IntrospectedColumn;
22  import org.mybatis.generator.api.IntrospectedTable;
23  import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
24  import org.mybatis.generator.api.dom.java.InnerClass;
25  import org.mybatis.generator.api.dom.java.JavaVisibility;
26  import org.mybatis.generator.api.dom.java.Method;
27  import org.mybatis.generator.api.dom.java.Parameter;
28  import org.mybatis.generator.api.dom.java.TopLevelClass;
29  import org.mybatis.generator.codegen.ibatis2.Ibatis2FormattingUtilities;
30  
31  /**
32   * This plugin demonstrates adding methods to the example class to enable
33   * case-insensitive LIKE searches. It shows hows to construct new methods and
34   * add them to an existing class.
35   * 
36   * This plugin only adds methods for String fields mapped to a JDBC character
37   * type (CHAR, VARCHAR, etc.)
38   * 
39   * @author Jeff Butler
40   * 
41   */
42  public class CaseInsensitiveLikePlugin extends PluginAdapter {
43  
44      /**
45       * 
46       */
47      public CaseInsensitiveLikePlugin() {
48          super();
49      }
50  
51      public boolean validate(List<String> warnings) {
52          return true;
53      }
54  
55      @Override
56      public boolean modelExampleClassGenerated(TopLevelClass topLevelClass,
57              IntrospectedTable introspectedTable) {
58  
59          InnerClass criteria = null;
60          // first, find the Criteria inner class
61          for (InnerClass innerClass : topLevelClass.getInnerClasses()) {
62              if ("GeneratedCriteria".equals(innerClass.getType().getShortName())) { //$NON-NLS-1$
63                  criteria = innerClass;
64                  break;
65              }
66          }
67  
68          if (criteria == null) {
69              // can't find the inner class for some reason, bail out.
70              return true;
71          }
72  
73          for (IntrospectedColumn introspectedColumn : introspectedTable
74                  .getNonBLOBColumns()) {
75              if (!introspectedColumn.isJdbcCharacterColumn()
76                      || !introspectedColumn.isStringColumn()) {
77                  continue;
78              }
79  
80              Method method = new Method();
81              method.setVisibility(JavaVisibility.PUBLIC);
82              method.addParameter(new Parameter(introspectedColumn
83                      .getFullyQualifiedJavaType(), "value")); //$NON-NLS-1$
84  
85              StringBuilder sb = new StringBuilder();
86              sb.append(introspectedColumn.getJavaProperty());
87              sb.setCharAt(0, Character.toUpperCase(sb.charAt(0)));
88              sb.insert(0, "and"); //$NON-NLS-1$
89              sb.append("LikeInsensitive"); //$NON-NLS-1$
90              method.setName(sb.toString());
91              method.setReturnType(FullyQualifiedJavaType.getCriteriaInstance());
92  
93              sb.setLength(0);
94              sb.append("addCriterion(\"upper("); //$NON-NLS-1$
95              sb.append(Ibatis2FormattingUtilities
96                      .getAliasedActualColumnName(introspectedColumn));
97              sb.append(") like\", value.toUpperCase(), \""); //$NON-NLS-1$
98              sb.append(introspectedColumn.getJavaProperty());
99              sb.append("\");"); //$NON-NLS-1$
100             method.addBodyLine(sb.toString());
101             method.addBodyLine("return (Criteria) this;"); //$NON-NLS-1$
102 
103             criteria.addMethod(method);
104         }
105 
106         return true;
107     }
108 }