com.kunckle.jetpower.core.base.search.SearchAdapter.java Source code

Java tutorial

Introduction

Here is the source code for com.kunckle.jetpower.core.base.search.SearchAdapter.java

Source

/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.
 * The ASF licenses this file to You 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
 *
 *       http://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 com.kunckle.jetpower.core.base.search;

import org.hibernate.Criteria;
import org.hibernate.FetchMode;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Projection;
import org.hibernate.criterion.ProjectionList;
import org.hibernate.criterion.Projections;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ??
 * <p>???</p>
 */
public class SearchAdapter {

    private List<Criterion> criterions = null;
    private ProjectionList projectionList = null;
    private Map<String, String> aliaes = null;
    private Map<String, FetchMode> fetchModes = null;
    private String prepQL = null;

    /**
     * ?
     *
     * @param criterion Hibernate Criterion ??
     * @return 
     */

    public SearchAdapter addCriterions(Criterion criterion) {
        if (criterions == null)
            criterions = new ArrayList<>();
        criterions.add(criterion);
        return this;
    }

    /**
     * ?
     *
     * @param criterion Hibernate Criterion ???
     * @return 
     */

    public SearchAdapter removeCriterions(Criterion criterion) {
        if (criterions == null)
            criterions = new ArrayList<>();
        criterions.remove(criterion);
        return this;
    }

    /**
     * ??
     *
     * @param projection Hibernate Criterion ?
     * @return 
     */

    public SearchAdapter addProjection(Projection projection) {
        if (projectionList == null)
            projectionList = Projections.projectionList();
        projectionList.add(projection);
        return this;
    }

    /**
     * ?
     *
     * @param alias ?
     * @param as    
     * @return 
     */

    public SearchAdapter addAlias(String alias, String as) {
        if (aliaes == null)
            aliaes = new HashMap<>();
        aliaes.put(alias, as);
        return this;
    }

    /**
     * ?
     *
     * @param alias ??
     * @return 
     */

    public SearchAdapter addAlias(String alias) {
        if (aliaes == null)
            aliaes = new HashMap<>();
        aliaes.remove(alias);
        return this;
    }

    /**
     * ???
     *
     * @param field     ?
     * @param fetchMode ??
     * @return 
     */

    public SearchAdapter addFetchMode(String field, FetchMode fetchMode) {
        if (fetchModes == null)
            fetchModes = new HashMap<>();
        fetchModes.put(field, fetchMode);
        return this;
    }

    /**
     * ???
     *
     * @param field ?
     * @return 
     */

    public SearchAdapter addFetchMode(String field) {
        if (fetchModes == null)
            fetchModes = new HashMap<>();
        fetchModes.remove(field);
        return this;
    }

    /**
     * Criteria
     *
     * @param criteria Session?Criteria
     * @return ?Criteria?
     */

    public Criteria getCriteria(Criteria criteria) {
        if (criterions != null) {
            for (Criterion criterion : criterions) {
                criteria.add(criterion);
            }
        }

        if (projectionList != null) {
            criteria.setProjection(projectionList);
        }

        if (aliaes != null) {
            for (String key : aliaes.keySet()) {
                criteria.createAlias(key, aliaes.get(key));
            }
        }

        if (fetchModes != null) {
            for (String key : fetchModes.keySet()) {
                criteria.setFetchMode(key, fetchModes.get(key));
            }
        }
        return criteria;
    }

    /**
     * Criteria
     *
     * @param criteria Session?Criteria
     * @return ?Criteria
     */

    public Criteria getCountCriteria(Criteria criteria) {
        if (criterions != null) {
            for (Criterion criterion : criterions) {
                criteria.add(criterion);
            }
        }

        criteria.setProjection(Projections.rowCount());

        return criteria;
    }

    /**
     * QL
     *
     * @return ???QLnull
     */

    public String getPrepQL() {
        return prepQL;
    }

    /**
     * QL
     *
     * @param prepQL QL??
     */

    public void setPrepQL(String prepQL) {
        this.prepQL = prepQL;
    }
}