ProviderApplyWhereMethodGenerator.java

/*
 *    Copyright 2006-2026 the original author or authors.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *       https://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */
package org.mybatis.generator.runtime.mybatis3.javamapper.elements.sqlprovider;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;

import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
import org.mybatis.generator.api.dom.java.JavaVisibility;
import org.mybatis.generator.api.dom.java.Method;
import org.mybatis.generator.api.dom.java.Parameter;
import org.mybatis.generator.api.dom.java.TopLevelClass;
import org.mybatis.generator.exception.InternalException;
import org.mybatis.generator.runtime.AbstractJavaClassMethodGenerator;
import org.mybatis.generator.runtime.JavaMethodAndImports;
import org.mybatis.generator.runtime.mybatis3.MyBatis3FormattingUtilities;

public class ProviderApplyWhereMethodGenerator extends AbstractJavaClassMethodGenerator {
    private static final List<String> METHOD_LINES = getMethodLines();

    protected ProviderApplyWhereMethodGenerator(Builder builder) {
        super(builder);
    }

    @Override
    public Optional<JavaMethodAndImports> generateMethodAndImports() {
        Set<FullyQualifiedJavaType> importedTypes = new HashSet<>();
        importedTypes.add(MyBatis3FormattingUtilities.BUILDER_IMPORT);
        importedTypes.add(new FullyQualifiedJavaType("java.util.List")); //$NON-NLS-1$

        FullyQualifiedJavaType fqjt = new FullyQualifiedJavaType(introspectedTable.getExampleType());
        importedTypes.add(fqjt);
        importedTypes.add(new FullyQualifiedJavaType(
                String.format("%s.Criteria", fqjt.getFullyQualifiedName()))); //$NON-NLS-1$
        importedTypes.add(new FullyQualifiedJavaType(
                String.format("%s.Criterion", fqjt.getFullyQualifiedName()))); //$NON-NLS-1$

        Method method = new Method("applyWhere"); //$NON-NLS-1$
        method.setVisibility(JavaVisibility.PROTECTED);
        method.addParameter(new Parameter(MyBatis3FormattingUtilities.BUILDER_IMPORT, "sql")); //$NON-NLS-1$
        method.addParameter(new Parameter(fqjt, "example")); //$NON-NLS-1$
        method.addParameter(new Parameter(FullyQualifiedJavaType.getBooleanPrimitiveInstance(),
                "includeExamplePhrase")); //$NON-NLS-1$

        commentGenerator.addGeneralMethodAnnotation(method, introspectedTable, importedTypes);

        METHOD_LINES.forEach(method::addBodyLine);

        JavaMethodAndImports answer = JavaMethodAndImports.withMethod(method)
                .withImports(importedTypes)
                .build();

        return Optional.of(answer);
    }

    @Override
    public boolean callPlugins(Method method, TopLevelClass topLevelClass) {
        return pluginAggregator.providerApplyWhereMethodGenerated(method, topLevelClass, introspectedTable);
    }

    protected static List<String> getMethodLines() {
        List<String> answer = new ArrayList<>();

        try (InputStream is =
                ProviderApplyWhereMethodGenerator.class.getResourceAsStream("ApplyWhereMethod.txt")) { //$NON-NLS-1$
            try (BufferedReader br = new BufferedReader(new InputStreamReader(Objects.requireNonNull(is)))) {
                String line;
                boolean foundDelimiter = false;
                while ((line = br.readLine()) != null) {
                    if (foundDelimiter) {
                        answer.add(line.trim());
                    } else {
                        foundDelimiter = line.equals("--- method lines below ---"); //$NON-NLS-1$
                    }
                }
            }
        } catch (IOException e) {
            throw new InternalException("IOException reading ApplyWhere method lines", e); //$NON-NLS-1$
        }

        return answer;
    }

    public static class Builder extends AbstractGeneratorBuilder<Builder> {
        @Override
        protected Builder getThis() {
            return this;
        }

        public ProviderApplyWhereMethodGenerator build() {
            return new ProviderApplyWhereMethodGenerator(this);
        }
    }
}