Package com.ctp.cdi.query.builder.part

Source Code of com.ctp.cdi.query.builder.part.QueryRoot

package com.ctp.cdi.query.builder.part;

import static com.ctp.cdi.query.util.QueryUtils.splitByKeyword;

import org.jboss.logging.Logger;

import com.ctp.cdi.query.builder.QueryBuilderContext;
import com.ctp.cdi.query.builder.QueryBuilder;

/**
* Root of the query tree. Also the only exposed class in the package.
* @author thomashug
*/
public class QueryRoot extends QueryPart {
   
    public static final QueryRoot UNKNOWN_ROOT = new QueryRoot("null-object");
   
    public static final String QUERY_PREFIX = "findBy";
   
    private final Logger log = Logger.getLogger(QueryRoot.class);
   
    private final String entityName;
   
    private String jpqlQuery;
   
    protected QueryRoot(String entityName) {
        this.entityName = entityName;
    }
   
    public static QueryRoot create(String method, String entityName) {
        QueryRoot root = new QueryRoot(entityName);
        root.build(method);
        root.createJpql();
        return root;
    }
   
    public String getJpqlQuery() {
        return jpqlQuery;
    }

    @Override
    protected QueryPart build(String queryPart) {
        String[] orParts = splitByKeyword(removePrefix(queryPart), "Or");
        boolean first = true;
        for (String or : orParts) {
            OrQueryPart orPart = new OrQueryPart(first);
            first = false;
            children.add(orPart.build(or));
        }
        return this;
    }
   
    @Override
    protected QueryPart buildQuery(QueryBuilderContext ctx) {
        ctx.append(QueryBuilder.selectQuery(entityName));
        if (hasChildren()) {
            ctx.append(" where ");
        }
        buildQueryForChildren(ctx);
        return this;
    }
   
    protected String createJpql() {
        QueryBuilderContext ctx = new QueryBuilderContext();
        buildQuery(ctx);
        jpqlQuery = ctx.resultString();
        log.debugv("createJpql: Query is {0}", jpqlQuery);
        return jpqlQuery;
    }
   
    private String removePrefix(String queryPart) {
        if (queryPart.startsWith(QUERY_PREFIX)) {
            return queryPart.substring(QUERY_PREFIX.length());
        }
        return queryPart;
    }

}
TOP

Related Classes of com.ctp.cdi.query.builder.part.QueryRoot

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.