View Javadoc
1   /*
2    *    Copyright 2006-2023 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    *       https://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.javamapper.elements.sqlprovider;
17  
18  import java.io.BufferedReader;
19  import java.io.IOException;
20  import java.io.InputStream;
21  import java.io.InputStreamReader;
22  import java.util.ArrayList;
23  import java.util.List;
24  import java.util.Objects;
25  import java.util.Set;
26  
27  import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
28  import org.mybatis.generator.api.dom.java.JavaVisibility;
29  import org.mybatis.generator.api.dom.java.Method;
30  import org.mybatis.generator.api.dom.java.Parameter;
31  import org.mybatis.generator.api.dom.java.TopLevelClass;
32  
33  public class ProviderApplyWhereMethodGenerator extends AbstractJavaProviderMethodGenerator {
34  
35      private static final List<String> METHOD_LINES = getMethodLines();
36  
37      @Override
38      public void addClassElements(TopLevelClass topLevelClass) {
39          Set<FullyQualifiedJavaType> importedTypes = initializeImportedTypes("java.util.List"); //$NON-NLS-1$
40  
41          FullyQualifiedJavaType fqjt = new FullyQualifiedJavaType(introspectedTable.getExampleType());
42          importedTypes.add(fqjt);
43          importedTypes.add(new FullyQualifiedJavaType(
44                  String.format("%s.Criteria", fqjt.getFullyQualifiedName()))); //$NON-NLS-1$
45          importedTypes.add(new FullyQualifiedJavaType(
46                  String.format("%s.Criterion", fqjt.getFullyQualifiedName()))); //$NON-NLS-1$
47  
48          Method method = new Method("applyWhere"); //$NON-NLS-1$
49          method.setVisibility(JavaVisibility.PROTECTED);
50          method.addParameter(new Parameter(BUILDER_IMPORT, "sql")); //$NON-NLS-1$
51          method.addParameter(new Parameter(fqjt, "example")); //$NON-NLS-1$
52          method.addParameter(new Parameter(FullyQualifiedJavaType.getBooleanPrimitiveInstance(),
53                  "includeExamplePhrase")); //$NON-NLS-1$
54  
55          context.getCommentGenerator().addGeneralMethodComment(method, introspectedTable);
56  
57          METHOD_LINES.forEach(method::addBodyLine);
58  
59          if (context.getPlugins().providerApplyWhereMethodGenerated(method, topLevelClass, introspectedTable)) {
60              topLevelClass.addImportedTypes(importedTypes);
61              topLevelClass.addMethod(method);
62          }
63      }
64  
65      protected static List<String> getMethodLines() {
66          List<String> answer = new ArrayList<>();
67  
68          InputStream is =
69                  ProviderApplyWhereMethodGenerator.class.getResourceAsStream("ApplyWhereMethod.txt"); //$NON-NLS-1$
70          try (BufferedReader br = new BufferedReader(new InputStreamReader(Objects.requireNonNull(is)))) {
71              String line;
72              boolean foundDelimiter = false;
73              while ((line = br.readLine()) != null) {
74                  if (foundDelimiter) {
75                      answer.add(line.trim());
76                  } else {
77                      foundDelimiter = line.equals("--- method lines below ---"); //$NON-NLS-1$
78                  }
79              }
80          } catch (IOException e) {
81              throw new RuntimeException("IOException reading ApplyWhere method lines", e); //$NON-NLS-1$
82          }
83  
84          return answer;
85      }
86  }