All Downloads are FREE. Search and download functionalities are using the official Maven repository.

it.ness.queryable.util.GetSearchMethodV3 Maven / Gradle / Ivy

There is a newer version: 3.0.3
Show newest version
package it.ness.queryable.util;

import it.ness.queryable.model.v3.FilterDefBase;
import org.apache.maven.plugin.logging.Log;

import java.util.Collection;

public class GetSearchMethodV3 {
    protected Log log;
    protected Collection preQueryFilters;
    protected Collection postQueryFilters;
    protected String className;

    public GetSearchMethodV3(Log log, Collection preQueryFilters, Collection postQueryFilters, String className) {
        this.log = log;
        this.preQueryFilters = preQueryFilters;
        this.postQueryFilters = postQueryFilters;
        this.className = className;
    }


    private String getQuery(String className, boolean existsPreQueryFilters) {
        if (!existsPreQueryFilters) {
            // using null,  Panache will generate query in Hibernate way (ie : " from com.flower.User ")
            String formatBody = "PanacheQuery<%s> search; Sort sort = sort(orderBy);" +
                    "if (sort != null) {" +
                    "search = %s.find(null, sort);" +
                    "} else {" +
                    "search = %s.find(null);" +
                    "}";

            return String.format(formatBody, className, className, className);
        }
        String formatBody = "PanacheQuery<%s> search; Sort sort = sort(orderBy);" +
                "if (sort != null) {" +
                "search = %s.find(query, sort, params);" +
                "} else {" +
                "search = %s.find(query, params);" +
                "}";

        return String.format(formatBody, className, className, className);
    }

    private String getPreQuery(boolean existsPreQueryFilters) {
        if (existsPreQueryFilters) {
            return "String query = null;" +
                    "Map params = null;";
        }
        return "";
    }

    public String create() {
        StringBuilder sb = new StringBuilder();
        boolean existsPreQueryFilters = preQueryFilters != null && preQueryFilters.size() > 0;
        boolean existsPostQueryFilters = postQueryFilters != null && postQueryFilters.size() > 0;
        sb.append(getPreQuery(existsPreQueryFilters));
        if (existsPreQueryFilters) {
            for (FilterDefBase f : preQueryFilters) {
                sb.append(f.getSearchMethod());
            }
        }
        sb.append(getQuery(className, existsPreQueryFilters));
        if (existsPostQueryFilters) {
            for (FilterDefBase f : postQueryFilters) {
                sb.append(f.getSearchMethod());
            }
        }
        sb.append("return search;");
        return sb.toString();
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy