com.bemis.portal.report.service.persistence.impl.ReportDefinitionPersistenceImpl.java Source code

Java tutorial

Introduction

Here is the source code for com.bemis.portal.report.service.persistence.impl.ReportDefinitionPersistenceImpl.java

Source

/**
 * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */

package com.bemis.portal.report.service.persistence.impl;

import aQute.bnd.annotation.ProviderType;

import com.bemis.portal.report.exception.NoSuchReportDefinitionException;
import com.bemis.portal.report.model.ReportDefinition;
import com.bemis.portal.report.model.impl.ReportDefinitionImpl;
import com.bemis.portal.report.model.impl.ReportDefinitionModelImpl;
import com.bemis.portal.report.service.persistence.ReportDefinitionPersistence;

import com.liferay.portal.kernel.dao.orm.EntityCache;
import com.liferay.portal.kernel.dao.orm.FinderCache;
import com.liferay.portal.kernel.dao.orm.FinderPath;
import com.liferay.portal.kernel.dao.orm.Query;
import com.liferay.portal.kernel.dao.orm.QueryPos;
import com.liferay.portal.kernel.dao.orm.QueryUtil;
import com.liferay.portal.kernel.dao.orm.SQLQuery;
import com.liferay.portal.kernel.dao.orm.Session;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.security.permission.InlineSQLHelperUtil;
import com.liferay.portal.kernel.service.ServiceContext;
import com.liferay.portal.kernel.service.ServiceContextThreadLocal;
import com.liferay.portal.kernel.service.persistence.CompanyProvider;
import com.liferay.portal.kernel.service.persistence.CompanyProviderWrapper;
import com.liferay.portal.kernel.service.persistence.impl.BasePersistenceImpl;
import com.liferay.portal.kernel.util.ArrayUtil;
import com.liferay.portal.kernel.util.OrderByComparator;
import com.liferay.portal.kernel.util.StringBundler;
import com.liferay.portal.kernel.util.StringPool;
import com.liferay.portal.kernel.util.StringUtil;
import com.liferay.portal.spring.extender.service.ServiceReference;

import java.io.Serializable;

import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

/**
 * The persistence implementation for the report definition service.
 *
 * <p>
 * Caching information and settings can be found in <code>portal.properties</code>
 * </p>
 *
 * @author Michael C. Han
 * @see ReportDefinitionPersistence
 * @see com.bemis.portal.report.service.persistence.ReportDefinitionUtil
 * @generated
 */
@ProviderType
public class ReportDefinitionPersistenceImpl extends BasePersistenceImpl<ReportDefinition>
        implements ReportDefinitionPersistence {
    /*
     * NOTE FOR DEVELOPERS:
     *
     * Never modify or reference this class directly. Always use {@link ReportDefinitionUtil} to access the report definition persistence. Modify <code>service.xml</code> and rerun ServiceBuilder to regenerate this class.
     */
    public static final String FINDER_CLASS_NAME_ENTITY = ReportDefinitionImpl.class.getName();
    public static final String FINDER_CLASS_NAME_LIST_WITH_PAGINATION = FINDER_CLASS_NAME_ENTITY + ".List1";
    public static final String FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION = FINDER_CLASS_NAME_ENTITY + ".List2";
    public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_ALL = new FinderPath(
            ReportDefinitionModelImpl.ENTITY_CACHE_ENABLED, ReportDefinitionModelImpl.FINDER_CACHE_ENABLED,
            ReportDefinitionImpl.class, FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findAll", new String[0]);
    public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL = new FinderPath(
            ReportDefinitionModelImpl.ENTITY_CACHE_ENABLED, ReportDefinitionModelImpl.FINDER_CACHE_ENABLED,
            ReportDefinitionImpl.class, FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findAll", new String[0]);
    public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(
            ReportDefinitionModelImpl.ENTITY_CACHE_ENABLED, ReportDefinitionModelImpl.FINDER_CACHE_ENABLED,
            Long.class, FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countAll", new String[0]);
    public static final FinderPath FINDER_PATH_FETCH_BY_NAME = new FinderPath(
            ReportDefinitionModelImpl.ENTITY_CACHE_ENABLED, ReportDefinitionModelImpl.FINDER_CACHE_ENABLED,
            ReportDefinitionImpl.class, FINDER_CLASS_NAME_ENTITY, "fetchByName",
            new String[] { String.class.getName() }, ReportDefinitionModelImpl.NAME_COLUMN_BITMASK);
    public static final FinderPath FINDER_PATH_COUNT_BY_NAME = new FinderPath(
            ReportDefinitionModelImpl.ENTITY_CACHE_ENABLED, ReportDefinitionModelImpl.FINDER_CACHE_ENABLED,
            Long.class, FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByName",
            new String[] { String.class.getName() });

    /**
     * Returns the report definition where name = &#63; or throws a {@link NoSuchReportDefinitionException} if it could not be found.
     *
     * @param name the name
     * @return the matching report definition
     * @throws NoSuchReportDefinitionException if a matching report definition could not be found
     */
    @Override
    public ReportDefinition findByName(String name) throws NoSuchReportDefinitionException {
        ReportDefinition reportDefinition = fetchByName(name);

        if (reportDefinition == null) {
            StringBundler msg = new StringBundler(4);

            msg.append(_NO_SUCH_ENTITY_WITH_KEY);

            msg.append("name=");
            msg.append(name);

            msg.append(StringPool.CLOSE_CURLY_BRACE);

            if (_log.isDebugEnabled()) {
                _log.debug(msg.toString());
            }

            throw new NoSuchReportDefinitionException(msg.toString());
        }

        return reportDefinition;
    }

    /**
     * Returns the report definition where name = &#63; or returns <code>null</code> if it could not be found. Uses the finder cache.
     *
     * @param name the name
     * @return the matching report definition, or <code>null</code> if a matching report definition could not be found
     */
    @Override
    public ReportDefinition fetchByName(String name) {
        return fetchByName(name, true);
    }

    /**
     * Returns the report definition where name = &#63; or returns <code>null</code> if it could not be found, optionally using the finder cache.
     *
     * @param name the name
     * @param retrieveFromCache whether to retrieve from the finder cache
     * @return the matching report definition, or <code>null</code> if a matching report definition could not be found
     */
    @Override
    public ReportDefinition fetchByName(String name, boolean retrieveFromCache) {
        Object[] finderArgs = new Object[] { name };

        Object result = null;

        if (retrieveFromCache) {
            result = finderCache.getResult(FINDER_PATH_FETCH_BY_NAME, finderArgs, this);
        }

        if (result instanceof ReportDefinition) {
            ReportDefinition reportDefinition = (ReportDefinition) result;

            if (!Objects.equals(name, reportDefinition.getName())) {
                result = null;
            }
        }

        if (result == null) {
            StringBundler query = new StringBundler(3);

            query.append(_SQL_SELECT_REPORTDEFINITION_WHERE);

            boolean bindName = false;

            if (name == null) {
                query.append(_FINDER_COLUMN_NAME_NAME_1);
            } else if (name.equals(StringPool.BLANK)) {
                query.append(_FINDER_COLUMN_NAME_NAME_3);
            } else {
                bindName = true;

                query.append(_FINDER_COLUMN_NAME_NAME_2);
            }

            String sql = query.toString();

            Session session = null;

            try {
                session = openSession();

                Query q = session.createQuery(sql);

                QueryPos qPos = QueryPos.getInstance(q);

                if (bindName) {
                    qPos.add(name);
                }

                List<ReportDefinition> list = q.list();

                if (list.isEmpty()) {
                    finderCache.putResult(FINDER_PATH_FETCH_BY_NAME, finderArgs, list);
                } else {
                    ReportDefinition reportDefinition = list.get(0);

                    result = reportDefinition;

                    cacheResult(reportDefinition);

                    if ((reportDefinition.getName() == null) || !reportDefinition.getName().equals(name)) {
                        finderCache.putResult(FINDER_PATH_FETCH_BY_NAME, finderArgs, reportDefinition);
                    }
                }
            } catch (Exception e) {
                finderCache.removeResult(FINDER_PATH_FETCH_BY_NAME, finderArgs);

                throw processException(e);
            } finally {
                closeSession(session);
            }
        }

        if (result instanceof List<?>) {
            return null;
        } else {
            return (ReportDefinition) result;
        }
    }

    /**
     * Removes the report definition where name = &#63; from the database.
     *
     * @param name the name
     * @return the report definition that was removed
     */
    @Override
    public ReportDefinition removeByName(String name) throws NoSuchReportDefinitionException {
        ReportDefinition reportDefinition = findByName(name);

        return remove(reportDefinition);
    }

    /**
     * Returns the number of report definitions where name = &#63;.
     *
     * @param name the name
     * @return the number of matching report definitions
     */
    @Override
    public int countByName(String name) {
        FinderPath finderPath = FINDER_PATH_COUNT_BY_NAME;

        Object[] finderArgs = new Object[] { name };

        Long count = (Long) finderCache.getResult(finderPath, finderArgs, this);

        if (count == null) {
            StringBundler query = new StringBundler(2);

            query.append(_SQL_COUNT_REPORTDEFINITION_WHERE);

            boolean bindName = false;

            if (name == null) {
                query.append(_FINDER_COLUMN_NAME_NAME_1);
            } else if (name.equals(StringPool.BLANK)) {
                query.append(_FINDER_COLUMN_NAME_NAME_3);
            } else {
                bindName = true;

                query.append(_FINDER_COLUMN_NAME_NAME_2);
            }

            String sql = query.toString();

            Session session = null;

            try {
                session = openSession();

                Query q = session.createQuery(sql);

                QueryPos qPos = QueryPos.getInstance(q);

                if (bindName) {
                    qPos.add(name);
                }

                count = (Long) q.uniqueResult();

                finderCache.putResult(finderPath, finderArgs, count);
            } catch (Exception e) {
                finderCache.removeResult(finderPath, finderArgs);

                throw processException(e);
            } finally {
                closeSession(session);
            }
        }

        return count.intValue();
    }

    private static final String _FINDER_COLUMN_NAME_NAME_1 = "reportDefinition.name IS NULL";
    private static final String _FINDER_COLUMN_NAME_NAME_2 = "reportDefinition.name = ?";
    private static final String _FINDER_COLUMN_NAME_NAME_3 = "(reportDefinition.name IS NULL OR reportDefinition.name = '')";
    public static final FinderPath FINDER_PATH_FETCH_BY_URLPATH = new FinderPath(
            ReportDefinitionModelImpl.ENTITY_CACHE_ENABLED, ReportDefinitionModelImpl.FINDER_CACHE_ENABLED,
            ReportDefinitionImpl.class, FINDER_CLASS_NAME_ENTITY, "fetchByURLPath",
            new String[] { String.class.getName() }, ReportDefinitionModelImpl.URLPATH_COLUMN_BITMASK);
    public static final FinderPath FINDER_PATH_COUNT_BY_URLPATH = new FinderPath(
            ReportDefinitionModelImpl.ENTITY_CACHE_ENABLED, ReportDefinitionModelImpl.FINDER_CACHE_ENABLED,
            Long.class, FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByURLPath",
            new String[] { String.class.getName() });

    /**
     * Returns the report definition where urlPath = &#63; or throws a {@link NoSuchReportDefinitionException} if it could not be found.
     *
     * @param urlPath the url path
     * @return the matching report definition
     * @throws NoSuchReportDefinitionException if a matching report definition could not be found
     */
    @Override
    public ReportDefinition findByURLPath(String urlPath) throws NoSuchReportDefinitionException {
        ReportDefinition reportDefinition = fetchByURLPath(urlPath);

        if (reportDefinition == null) {
            StringBundler msg = new StringBundler(4);

            msg.append(_NO_SUCH_ENTITY_WITH_KEY);

            msg.append("urlPath=");
            msg.append(urlPath);

            msg.append(StringPool.CLOSE_CURLY_BRACE);

            if (_log.isDebugEnabled()) {
                _log.debug(msg.toString());
            }

            throw new NoSuchReportDefinitionException(msg.toString());
        }

        return reportDefinition;
    }

    /**
     * Returns the report definition where urlPath = &#63; or returns <code>null</code> if it could not be found. Uses the finder cache.
     *
     * @param urlPath the url path
     * @return the matching report definition, or <code>null</code> if a matching report definition could not be found
     */
    @Override
    public ReportDefinition fetchByURLPath(String urlPath) {
        return fetchByURLPath(urlPath, true);
    }

    /**
     * Returns the report definition where urlPath = &#63; or returns <code>null</code> if it could not be found, optionally using the finder cache.
     *
     * @param urlPath the url path
     * @param retrieveFromCache whether to retrieve from the finder cache
     * @return the matching report definition, or <code>null</code> if a matching report definition could not be found
     */
    @Override
    public ReportDefinition fetchByURLPath(String urlPath, boolean retrieveFromCache) {
        Object[] finderArgs = new Object[] { urlPath };

        Object result = null;

        if (retrieveFromCache) {
            result = finderCache.getResult(FINDER_PATH_FETCH_BY_URLPATH, finderArgs, this);
        }

        if (result instanceof ReportDefinition) {
            ReportDefinition reportDefinition = (ReportDefinition) result;

            if (!Objects.equals(urlPath, reportDefinition.getUrlPath())) {
                result = null;
            }
        }

        if (result == null) {
            StringBundler query = new StringBundler(3);

            query.append(_SQL_SELECT_REPORTDEFINITION_WHERE);

            boolean bindUrlPath = false;

            if (urlPath == null) {
                query.append(_FINDER_COLUMN_URLPATH_URLPATH_1);
            } else if (urlPath.equals(StringPool.BLANK)) {
                query.append(_FINDER_COLUMN_URLPATH_URLPATH_3);
            } else {
                bindUrlPath = true;

                query.append(_FINDER_COLUMN_URLPATH_URLPATH_2);
            }

            String sql = query.toString();

            Session session = null;

            try {
                session = openSession();

                Query q = session.createQuery(sql);

                QueryPos qPos = QueryPos.getInstance(q);

                if (bindUrlPath) {
                    qPos.add(urlPath);
                }

                List<ReportDefinition> list = q.list();

                if (list.isEmpty()) {
                    finderCache.putResult(FINDER_PATH_FETCH_BY_URLPATH, finderArgs, list);
                } else {
                    if (list.size() > 1) {
                        Collections.sort(list, Collections.reverseOrder());

                        if (_log.isWarnEnabled()) {
                            _log.warn(
                                    "ReportDefinitionPersistenceImpl.fetchByURLPath(String, boolean) with parameters ("
                                            + StringUtil.merge(finderArgs)
                                            + ") yields a result set with more than 1 result. This violates the logical unique restriction. There is no order guarantee on which result is returned by this finder.");
                        }
                    }

                    ReportDefinition reportDefinition = list.get(0);

                    result = reportDefinition;

                    cacheResult(reportDefinition);

                    if ((reportDefinition.getUrlPath() == null) || !reportDefinition.getUrlPath().equals(urlPath)) {
                        finderCache.putResult(FINDER_PATH_FETCH_BY_URLPATH, finderArgs, reportDefinition);
                    }
                }
            } catch (Exception e) {
                finderCache.removeResult(FINDER_PATH_FETCH_BY_URLPATH, finderArgs);

                throw processException(e);
            } finally {
                closeSession(session);
            }
        }

        if (result instanceof List<?>) {
            return null;
        } else {
            return (ReportDefinition) result;
        }
    }

    /**
     * Removes the report definition where urlPath = &#63; from the database.
     *
     * @param urlPath the url path
     * @return the report definition that was removed
     */
    @Override
    public ReportDefinition removeByURLPath(String urlPath) throws NoSuchReportDefinitionException {
        ReportDefinition reportDefinition = findByURLPath(urlPath);

        return remove(reportDefinition);
    }

    /**
     * Returns the number of report definitions where urlPath = &#63;.
     *
     * @param urlPath the url path
     * @return the number of matching report definitions
     */
    @Override
    public int countByURLPath(String urlPath) {
        FinderPath finderPath = FINDER_PATH_COUNT_BY_URLPATH;

        Object[] finderArgs = new Object[] { urlPath };

        Long count = (Long) finderCache.getResult(finderPath, finderArgs, this);

        if (count == null) {
            StringBundler query = new StringBundler(2);

            query.append(_SQL_COUNT_REPORTDEFINITION_WHERE);

            boolean bindUrlPath = false;

            if (urlPath == null) {
                query.append(_FINDER_COLUMN_URLPATH_URLPATH_1);
            } else if (urlPath.equals(StringPool.BLANK)) {
                query.append(_FINDER_COLUMN_URLPATH_URLPATH_3);
            } else {
                bindUrlPath = true;

                query.append(_FINDER_COLUMN_URLPATH_URLPATH_2);
            }

            String sql = query.toString();

            Session session = null;

            try {
                session = openSession();

                Query q = session.createQuery(sql);

                QueryPos qPos = QueryPos.getInstance(q);

                if (bindUrlPath) {
                    qPos.add(urlPath);
                }

                count = (Long) q.uniqueResult();

                finderCache.putResult(finderPath, finderArgs, count);
            } catch (Exception e) {
                finderCache.removeResult(finderPath, finderArgs);

                throw processException(e);
            } finally {
                closeSession(session);
            }
        }

        return count.intValue();
    }

    private static final String _FINDER_COLUMN_URLPATH_URLPATH_1 = "reportDefinition.urlPath IS NULL";
    private static final String _FINDER_COLUMN_URLPATH_URLPATH_2 = "reportDefinition.urlPath = ?";
    private static final String _FINDER_COLUMN_URLPATH_URLPATH_3 = "(reportDefinition.urlPath IS NULL OR reportDefinition.urlPath = '')";
    public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_C_G = new FinderPath(
            ReportDefinitionModelImpl.ENTITY_CACHE_ENABLED, ReportDefinitionModelImpl.FINDER_CACHE_ENABLED,
            ReportDefinitionImpl.class, FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findByC_G",
            new String[] { Long.class.getName(), Long.class.getName(),

                    Integer.class.getName(), Integer.class.getName(), OrderByComparator.class.getName() });
    public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_G = new FinderPath(
            ReportDefinitionModelImpl.ENTITY_CACHE_ENABLED, ReportDefinitionModelImpl.FINDER_CACHE_ENABLED,
            ReportDefinitionImpl.class, FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findByC_G",
            new String[] { Long.class.getName(), Long.class.getName() },
            ReportDefinitionModelImpl.COMPANYID_COLUMN_BITMASK | ReportDefinitionModelImpl.GROUPID_COLUMN_BITMASK
                    | ReportDefinitionModelImpl.CATEGORY_COLUMN_BITMASK);
    public static final FinderPath FINDER_PATH_COUNT_BY_C_G = new FinderPath(
            ReportDefinitionModelImpl.ENTITY_CACHE_ENABLED, ReportDefinitionModelImpl.FINDER_CACHE_ENABLED,
            Long.class, FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByC_G",
            new String[] { Long.class.getName(), Long.class.getName() });

    /**
     * Returns all the report definitions where companyId = &#63; and groupId = &#63;.
     *
     * @param companyId the company ID
     * @param groupId the group ID
     * @return the matching report definitions
     */
    @Override
    public List<ReportDefinition> findByC_G(long companyId, long groupId) {
        return findByC_G(companyId, groupId, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
    }

    /**
     * Returns a range of all the report definitions where companyId = &#63; and groupId = &#63;.
     *
     * <p>
     * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link ReportDefinitionModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
     * </p>
     *
     * @param companyId the company ID
     * @param groupId the group ID
     * @param start the lower bound of the range of report definitions
     * @param end the upper bound of the range of report definitions (not inclusive)
     * @return the range of matching report definitions
     */
    @Override
    public List<ReportDefinition> findByC_G(long companyId, long groupId, int start, int end) {
        return findByC_G(companyId, groupId, start, end, null);
    }

    /**
     * Returns an ordered range of all the report definitions where companyId = &#63; and groupId = &#63;.
     *
     * <p>
     * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link ReportDefinitionModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
     * </p>
     *
     * @param companyId the company ID
     * @param groupId the group ID
     * @param start the lower bound of the range of report definitions
     * @param end the upper bound of the range of report definitions (not inclusive)
     * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
     * @return the ordered range of matching report definitions
     */
    @Override
    public List<ReportDefinition> findByC_G(long companyId, long groupId, int start, int end,
            OrderByComparator<ReportDefinition> orderByComparator) {
        return findByC_G(companyId, groupId, start, end, orderByComparator, true);
    }

    /**
     * Returns an ordered range of all the report definitions where companyId = &#63; and groupId = &#63;.
     *
     * <p>
     * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link ReportDefinitionModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
     * </p>
     *
     * @param companyId the company ID
     * @param groupId the group ID
     * @param start the lower bound of the range of report definitions
     * @param end the upper bound of the range of report definitions (not inclusive)
     * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
     * @param retrieveFromCache whether to retrieve from the finder cache
     * @return the ordered range of matching report definitions
     */
    @Override
    public List<ReportDefinition> findByC_G(long companyId, long groupId, int start, int end,
            OrderByComparator<ReportDefinition> orderByComparator, boolean retrieveFromCache) {
        boolean pagination = true;
        FinderPath finderPath = null;
        Object[] finderArgs = null;

        if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) && (orderByComparator == null)) {
            pagination = false;
            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_G;
            finderArgs = new Object[] { companyId, groupId };
        } else {
            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_C_G;
            finderArgs = new Object[] { companyId, groupId,

                    start, end, orderByComparator };
        }

        List<ReportDefinition> list = null;

        if (retrieveFromCache) {
            list = (List<ReportDefinition>) finderCache.getResult(finderPath, finderArgs, this);

            if ((list != null) && !list.isEmpty()) {
                for (ReportDefinition reportDefinition : list) {
                    if ((companyId != reportDefinition.getCompanyId())
                            || (groupId != reportDefinition.getGroupId())) {
                        list = null;

                        break;
                    }
                }
            }
        }

        if (list == null) {
            StringBundler query = null;

            if (orderByComparator != null) {
                query = new StringBundler(4 + (orderByComparator.getOrderByFields().length * 2));
            } else {
                query = new StringBundler(4);
            }

            query.append(_SQL_SELECT_REPORTDEFINITION_WHERE);

            query.append(_FINDER_COLUMN_C_G_COMPANYID_2);

            query.append(_FINDER_COLUMN_C_G_GROUPID_2);

            if (orderByComparator != null) {
                appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS, orderByComparator);
            } else if (pagination) {
                query.append(ReportDefinitionModelImpl.ORDER_BY_JPQL);
            }

            String sql = query.toString();

            Session session = null;

            try {
                session = openSession();

                Query q = session.createQuery(sql);

                QueryPos qPos = QueryPos.getInstance(q);

                qPos.add(companyId);

                qPos.add(groupId);

                if (!pagination) {
                    list = (List<ReportDefinition>) QueryUtil.list(q, getDialect(), start, end, false);

                    Collections.sort(list);

                    list = Collections.unmodifiableList(list);
                } else {
                    list = (List<ReportDefinition>) QueryUtil.list(q, getDialect(), start, end);
                }

                cacheResult(list);

                finderCache.putResult(finderPath, finderArgs, list);
            } catch (Exception e) {
                finderCache.removeResult(finderPath, finderArgs);

                throw processException(e);
            } finally {
                closeSession(session);
            }
        }

        return list;
    }

    /**
     * Returns the first report definition in the ordered set where companyId = &#63; and groupId = &#63;.
     *
     * @param companyId the company ID
     * @param groupId the group ID
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the first matching report definition
     * @throws NoSuchReportDefinitionException if a matching report definition could not be found
     */
    @Override
    public ReportDefinition findByC_G_First(long companyId, long groupId,
            OrderByComparator<ReportDefinition> orderByComparator) throws NoSuchReportDefinitionException {
        ReportDefinition reportDefinition = fetchByC_G_First(companyId, groupId, orderByComparator);

        if (reportDefinition != null) {
            return reportDefinition;
        }

        StringBundler msg = new StringBundler(6);

        msg.append(_NO_SUCH_ENTITY_WITH_KEY);

        msg.append("companyId=");
        msg.append(companyId);

        msg.append(", groupId=");
        msg.append(groupId);

        msg.append(StringPool.CLOSE_CURLY_BRACE);

        throw new NoSuchReportDefinitionException(msg.toString());
    }

    /**
     * Returns the first report definition in the ordered set where companyId = &#63; and groupId = &#63;.
     *
     * @param companyId the company ID
     * @param groupId the group ID
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the first matching report definition, or <code>null</code> if a matching report definition could not be found
     */
    @Override
    public ReportDefinition fetchByC_G_First(long companyId, long groupId,
            OrderByComparator<ReportDefinition> orderByComparator) {
        List<ReportDefinition> list = findByC_G(companyId, groupId, 0, 1, orderByComparator);

        if (!list.isEmpty()) {
            return list.get(0);
        }

        return null;
    }

    /**
     * Returns the last report definition in the ordered set where companyId = &#63; and groupId = &#63;.
     *
     * @param companyId the company ID
     * @param groupId the group ID
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the last matching report definition
     * @throws NoSuchReportDefinitionException if a matching report definition could not be found
     */
    @Override
    public ReportDefinition findByC_G_Last(long companyId, long groupId,
            OrderByComparator<ReportDefinition> orderByComparator) throws NoSuchReportDefinitionException {
        ReportDefinition reportDefinition = fetchByC_G_Last(companyId, groupId, orderByComparator);

        if (reportDefinition != null) {
            return reportDefinition;
        }

        StringBundler msg = new StringBundler(6);

        msg.append(_NO_SUCH_ENTITY_WITH_KEY);

        msg.append("companyId=");
        msg.append(companyId);

        msg.append(", groupId=");
        msg.append(groupId);

        msg.append(StringPool.CLOSE_CURLY_BRACE);

        throw new NoSuchReportDefinitionException(msg.toString());
    }

    /**
     * Returns the last report definition in the ordered set where companyId = &#63; and groupId = &#63;.
     *
     * @param companyId the company ID
     * @param groupId the group ID
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the last matching report definition, or <code>null</code> if a matching report definition could not be found
     */
    @Override
    public ReportDefinition fetchByC_G_Last(long companyId, long groupId,
            OrderByComparator<ReportDefinition> orderByComparator) {
        int count = countByC_G(companyId, groupId);

        if (count == 0) {
            return null;
        }

        List<ReportDefinition> list = findByC_G(companyId, groupId, count - 1, count, orderByComparator);

        if (!list.isEmpty()) {
            return list.get(0);
        }

        return null;
    }

    /**
     * Returns the report definitions before and after the current report definition in the ordered set where companyId = &#63; and groupId = &#63;.
     *
     * @param reportDefinitionId the primary key of the current report definition
     * @param companyId the company ID
     * @param groupId the group ID
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the previous, current, and next report definition
     * @throws NoSuchReportDefinitionException if a report definition with the primary key could not be found
     */
    @Override
    public ReportDefinition[] findByC_G_PrevAndNext(long reportDefinitionId, long companyId, long groupId,
            OrderByComparator<ReportDefinition> orderByComparator) throws NoSuchReportDefinitionException {
        ReportDefinition reportDefinition = findByPrimaryKey(reportDefinitionId);

        Session session = null;

        try {
            session = openSession();

            ReportDefinition[] array = new ReportDefinitionImpl[3];

            array[0] = getByC_G_PrevAndNext(session, reportDefinition, companyId, groupId, orderByComparator, true);

            array[1] = reportDefinition;

            array[2] = getByC_G_PrevAndNext(session, reportDefinition, companyId, groupId, orderByComparator,
                    false);

            return array;
        } catch (Exception e) {
            throw processException(e);
        } finally {
            closeSession(session);
        }
    }

    protected ReportDefinition getByC_G_PrevAndNext(Session session, ReportDefinition reportDefinition,
            long companyId, long groupId, OrderByComparator<ReportDefinition> orderByComparator, boolean previous) {
        StringBundler query = null;

        if (orderByComparator != null) {
            query = new StringBundler(5 + (orderByComparator.getOrderByConditionFields().length * 3)
                    + (orderByComparator.getOrderByFields().length * 3));
        } else {
            query = new StringBundler(4);
        }

        query.append(_SQL_SELECT_REPORTDEFINITION_WHERE);

        query.append(_FINDER_COLUMN_C_G_COMPANYID_2);

        query.append(_FINDER_COLUMN_C_G_GROUPID_2);

        if (orderByComparator != null) {
            String[] orderByConditionFields = orderByComparator.getOrderByConditionFields();

            if (orderByConditionFields.length > 0) {
                query.append(WHERE_AND);
            }

            for (int i = 0; i < orderByConditionFields.length; i++) {
                query.append(_ORDER_BY_ENTITY_ALIAS);
                query.append(orderByConditionFields[i]);

                if ((i + 1) < orderByConditionFields.length) {
                    if (orderByComparator.isAscending() ^ previous) {
                        query.append(WHERE_GREATER_THAN_HAS_NEXT);
                    } else {
                        query.append(WHERE_LESSER_THAN_HAS_NEXT);
                    }
                } else {
                    if (orderByComparator.isAscending() ^ previous) {
                        query.append(WHERE_GREATER_THAN);
                    } else {
                        query.append(WHERE_LESSER_THAN);
                    }
                }
            }

            query.append(ORDER_BY_CLAUSE);

            String[] orderByFields = orderByComparator.getOrderByFields();

            for (int i = 0; i < orderByFields.length; i++) {
                query.append(_ORDER_BY_ENTITY_ALIAS);
                query.append(orderByFields[i]);

                if ((i + 1) < orderByFields.length) {
                    if (orderByComparator.isAscending() ^ previous) {
                        query.append(ORDER_BY_ASC_HAS_NEXT);
                    } else {
                        query.append(ORDER_BY_DESC_HAS_NEXT);
                    }
                } else {
                    if (orderByComparator.isAscending() ^ previous) {
                        query.append(ORDER_BY_ASC);
                    } else {
                        query.append(ORDER_BY_DESC);
                    }
                }
            }
        } else {
            query.append(ReportDefinitionModelImpl.ORDER_BY_JPQL);
        }

        String sql = query.toString();

        Query q = session.createQuery(sql);

        q.setFirstResult(0);
        q.setMaxResults(2);

        QueryPos qPos = QueryPos.getInstance(q);

        qPos.add(companyId);

        qPos.add(groupId);

        if (orderByComparator != null) {
            Object[] values = orderByComparator.getOrderByConditionValues(reportDefinition);

            for (Object value : values) {
                qPos.add(value);
            }
        }

        List<ReportDefinition> list = q.list();

        if (list.size() == 2) {
            return list.get(1);
        } else {
            return null;
        }
    }

    /**
     * Returns all the report definitions that the user has permission to view where companyId = &#63; and groupId = &#63;.
     *
     * @param companyId the company ID
     * @param groupId the group ID
     * @return the matching report definitions that the user has permission to view
     */
    @Override
    public List<ReportDefinition> filterFindByC_G(long companyId, long groupId) {
        return filterFindByC_G(companyId, groupId, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
    }

    /**
     * Returns a range of all the report definitions that the user has permission to view where companyId = &#63; and groupId = &#63;.
     *
     * <p>
     * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link ReportDefinitionModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
     * </p>
     *
     * @param companyId the company ID
     * @param groupId the group ID
     * @param start the lower bound of the range of report definitions
     * @param end the upper bound of the range of report definitions (not inclusive)
     * @return the range of matching report definitions that the user has permission to view
     */
    @Override
    public List<ReportDefinition> filterFindByC_G(long companyId, long groupId, int start, int end) {
        return filterFindByC_G(companyId, groupId, start, end, null);
    }

    /**
     * Returns an ordered range of all the report definitions that the user has permissions to view where companyId = &#63; and groupId = &#63;.
     *
     * <p>
     * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link ReportDefinitionModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
     * </p>
     *
     * @param companyId the company ID
     * @param groupId the group ID
     * @param start the lower bound of the range of report definitions
     * @param end the upper bound of the range of report definitions (not inclusive)
     * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
     * @return the ordered range of matching report definitions that the user has permission to view
     */
    @Override
    public List<ReportDefinition> filterFindByC_G(long companyId, long groupId, int start, int end,
            OrderByComparator<ReportDefinition> orderByComparator) {
        if (!InlineSQLHelperUtil.isEnabled(groupId)) {
            return findByC_G(companyId, groupId, start, end, orderByComparator);
        }

        StringBundler query = null;

        if (orderByComparator != null) {
            query = new StringBundler(4 + (orderByComparator.getOrderByFields().length * 2));
        } else {
            query = new StringBundler(5);
        }

        if (getDB().isSupportsInlineDistinct()) {
            query.append(_FILTER_SQL_SELECT_REPORTDEFINITION_WHERE);
        } else {
            query.append(_FILTER_SQL_SELECT_REPORTDEFINITION_NO_INLINE_DISTINCT_WHERE_1);
        }

        query.append(_FINDER_COLUMN_C_G_COMPANYID_2);

        query.append(_FINDER_COLUMN_C_G_GROUPID_2);

        if (!getDB().isSupportsInlineDistinct()) {
            query.append(_FILTER_SQL_SELECT_REPORTDEFINITION_NO_INLINE_DISTINCT_WHERE_2);
        }

        if (orderByComparator != null) {
            if (getDB().isSupportsInlineDistinct()) {
                appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS, orderByComparator, true);
            } else {
                appendOrderByComparator(query, _ORDER_BY_ENTITY_TABLE, orderByComparator, true);
            }
        } else {
            if (getDB().isSupportsInlineDistinct()) {
                query.append(ReportDefinitionModelImpl.ORDER_BY_JPQL);
            } else {
                query.append(ReportDefinitionModelImpl.ORDER_BY_SQL);
            }
        }

        String sql = InlineSQLHelperUtil.replacePermissionCheck(query.toString(), ReportDefinition.class.getName(),
                _FILTER_ENTITY_TABLE_FILTER_PK_COLUMN, groupId);

        Session session = null;

        try {
            session = openSession();

            SQLQuery q = session.createSynchronizedSQLQuery(sql);

            if (getDB().isSupportsInlineDistinct()) {
                q.addEntity(_FILTER_ENTITY_ALIAS, ReportDefinitionImpl.class);
            } else {
                q.addEntity(_FILTER_ENTITY_TABLE, ReportDefinitionImpl.class);
            }

            QueryPos qPos = QueryPos.getInstance(q);

            qPos.add(companyId);

            qPos.add(groupId);

            return (List<ReportDefinition>) QueryUtil.list(q, getDialect(), start, end);
        } catch (Exception e) {
            throw processException(e);
        } finally {
            closeSession(session);
        }
    }

    /**
     * Returns the report definitions before and after the current report definition in the ordered set of report definitions that the user has permission to view where companyId = &#63; and groupId = &#63;.
     *
     * @param reportDefinitionId the primary key of the current report definition
     * @param companyId the company ID
     * @param groupId the group ID
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the previous, current, and next report definition
     * @throws NoSuchReportDefinitionException if a report definition with the primary key could not be found
     */
    @Override
    public ReportDefinition[] filterFindByC_G_PrevAndNext(long reportDefinitionId, long companyId, long groupId,
            OrderByComparator<ReportDefinition> orderByComparator) throws NoSuchReportDefinitionException {
        if (!InlineSQLHelperUtil.isEnabled(groupId)) {
            return findByC_G_PrevAndNext(reportDefinitionId, companyId, groupId, orderByComparator);
        }

        ReportDefinition reportDefinition = findByPrimaryKey(reportDefinitionId);

        Session session = null;

        try {
            session = openSession();

            ReportDefinition[] array = new ReportDefinitionImpl[3];

            array[0] = filterGetByC_G_PrevAndNext(session, reportDefinition, companyId, groupId, orderByComparator,
                    true);

            array[1] = reportDefinition;

            array[2] = filterGetByC_G_PrevAndNext(session, reportDefinition, companyId, groupId, orderByComparator,
                    false);

            return array;
        } catch (Exception e) {
            throw processException(e);
        } finally {
            closeSession(session);
        }
    }

    protected ReportDefinition filterGetByC_G_PrevAndNext(Session session, ReportDefinition reportDefinition,
            long companyId, long groupId, OrderByComparator<ReportDefinition> orderByComparator, boolean previous) {
        StringBundler query = null;

        if (orderByComparator != null) {
            query = new StringBundler(6 + (orderByComparator.getOrderByConditionFields().length * 3)
                    + (orderByComparator.getOrderByFields().length * 3));
        } else {
            query = new StringBundler(5);
        }

        if (getDB().isSupportsInlineDistinct()) {
            query.append(_FILTER_SQL_SELECT_REPORTDEFINITION_WHERE);
        } else {
            query.append(_FILTER_SQL_SELECT_REPORTDEFINITION_NO_INLINE_DISTINCT_WHERE_1);
        }

        query.append(_FINDER_COLUMN_C_G_COMPANYID_2);

        query.append(_FINDER_COLUMN_C_G_GROUPID_2);

        if (!getDB().isSupportsInlineDistinct()) {
            query.append(_FILTER_SQL_SELECT_REPORTDEFINITION_NO_INLINE_DISTINCT_WHERE_2);
        }

        if (orderByComparator != null) {
            String[] orderByConditionFields = orderByComparator.getOrderByConditionFields();

            if (orderByConditionFields.length > 0) {
                query.append(WHERE_AND);
            }

            for (int i = 0; i < orderByConditionFields.length; i++) {
                if (getDB().isSupportsInlineDistinct()) {
                    query.append(_ORDER_BY_ENTITY_ALIAS);
                } else {
                    query.append(_ORDER_BY_ENTITY_TABLE);
                }

                query.append(orderByConditionFields[i]);

                if ((i + 1) < orderByConditionFields.length) {
                    if (orderByComparator.isAscending() ^ previous) {
                        query.append(WHERE_GREATER_THAN_HAS_NEXT);
                    } else {
                        query.append(WHERE_LESSER_THAN_HAS_NEXT);
                    }
                } else {
                    if (orderByComparator.isAscending() ^ previous) {
                        query.append(WHERE_GREATER_THAN);
                    } else {
                        query.append(WHERE_LESSER_THAN);
                    }
                }
            }

            query.append(ORDER_BY_CLAUSE);

            String[] orderByFields = orderByComparator.getOrderByFields();

            for (int i = 0; i < orderByFields.length; i++) {
                if (getDB().isSupportsInlineDistinct()) {
                    query.append(_ORDER_BY_ENTITY_ALIAS);
                } else {
                    query.append(_ORDER_BY_ENTITY_TABLE);
                }

                query.append(orderByFields[i]);

                if ((i + 1) < orderByFields.length) {
                    if (orderByComparator.isAscending() ^ previous) {
                        query.append(ORDER_BY_ASC_HAS_NEXT);
                    } else {
                        query.append(ORDER_BY_DESC_HAS_NEXT);
                    }
                } else {
                    if (orderByComparator.isAscending() ^ previous) {
                        query.append(ORDER_BY_ASC);
                    } else {
                        query.append(ORDER_BY_DESC);
                    }
                }
            }
        } else {
            if (getDB().isSupportsInlineDistinct()) {
                query.append(ReportDefinitionModelImpl.ORDER_BY_JPQL);
            } else {
                query.append(ReportDefinitionModelImpl.ORDER_BY_SQL);
            }
        }

        String sql = InlineSQLHelperUtil.replacePermissionCheck(query.toString(), ReportDefinition.class.getName(),
                _FILTER_ENTITY_TABLE_FILTER_PK_COLUMN, groupId);

        SQLQuery q = session.createSynchronizedSQLQuery(sql);

        q.setFirstResult(0);
        q.setMaxResults(2);

        if (getDB().isSupportsInlineDistinct()) {
            q.addEntity(_FILTER_ENTITY_ALIAS, ReportDefinitionImpl.class);
        } else {
            q.addEntity(_FILTER_ENTITY_TABLE, ReportDefinitionImpl.class);
        }

        QueryPos qPos = QueryPos.getInstance(q);

        qPos.add(companyId);

        qPos.add(groupId);

        if (orderByComparator != null) {
            Object[] values = orderByComparator.getOrderByConditionValues(reportDefinition);

            for (Object value : values) {
                qPos.add(value);
            }
        }

        List<ReportDefinition> list = q.list();

        if (list.size() == 2) {
            return list.get(1);
        } else {
            return null;
        }
    }

    /**
     * Removes all the report definitions where companyId = &#63; and groupId = &#63; from the database.
     *
     * @param companyId the company ID
     * @param groupId the group ID
     */
    @Override
    public void removeByC_G(long companyId, long groupId) {
        for (ReportDefinition reportDefinition : findByC_G(companyId, groupId, QueryUtil.ALL_POS, QueryUtil.ALL_POS,
                null)) {
            remove(reportDefinition);
        }
    }

    /**
     * Returns the number of report definitions where companyId = &#63; and groupId = &#63;.
     *
     * @param companyId the company ID
     * @param groupId the group ID
     * @return the number of matching report definitions
     */
    @Override
    public int countByC_G(long companyId, long groupId) {
        FinderPath finderPath = FINDER_PATH_COUNT_BY_C_G;

        Object[] finderArgs = new Object[] { companyId, groupId };

        Long count = (Long) finderCache.getResult(finderPath, finderArgs, this);

        if (count == null) {
            StringBundler query = new StringBundler(3);

            query.append(_SQL_COUNT_REPORTDEFINITION_WHERE);

            query.append(_FINDER_COLUMN_C_G_COMPANYID_2);

            query.append(_FINDER_COLUMN_C_G_GROUPID_2);

            String sql = query.toString();

            Session session = null;

            try {
                session = openSession();

                Query q = session.createQuery(sql);

                QueryPos qPos = QueryPos.getInstance(q);

                qPos.add(companyId);

                qPos.add(groupId);

                count = (Long) q.uniqueResult();

                finderCache.putResult(finderPath, finderArgs, count);
            } catch (Exception e) {
                finderCache.removeResult(finderPath, finderArgs);

                throw processException(e);
            } finally {
                closeSession(session);
            }
        }

        return count.intValue();
    }

    /**
     * Returns the number of report definitions that the user has permission to view where companyId = &#63; and groupId = &#63;.
     *
     * @param companyId the company ID
     * @param groupId the group ID
     * @return the number of matching report definitions that the user has permission to view
     */
    @Override
    public int filterCountByC_G(long companyId, long groupId) {
        if (!InlineSQLHelperUtil.isEnabled(groupId)) {
            return countByC_G(companyId, groupId);
        }

        StringBundler query = new StringBundler(3);

        query.append(_FILTER_SQL_COUNT_REPORTDEFINITION_WHERE);

        query.append(_FINDER_COLUMN_C_G_COMPANYID_2);

        query.append(_FINDER_COLUMN_C_G_GROUPID_2);

        String sql = InlineSQLHelperUtil.replacePermissionCheck(query.toString(), ReportDefinition.class.getName(),
                _FILTER_ENTITY_TABLE_FILTER_PK_COLUMN, groupId);

        Session session = null;

        try {
            session = openSession();

            SQLQuery q = session.createSynchronizedSQLQuery(sql);

            q.addScalar(COUNT_COLUMN_NAME, com.liferay.portal.kernel.dao.orm.Type.LONG);

            QueryPos qPos = QueryPos.getInstance(q);

            qPos.add(companyId);

            qPos.add(groupId);

            Long count = (Long) q.uniqueResult();

            return count.intValue();
        } catch (Exception e) {
            throw processException(e);
        } finally {
            closeSession(session);
        }
    }

    private static final String _FINDER_COLUMN_C_G_COMPANYID_2 = "reportDefinition.companyId = ? AND ";
    private static final String _FINDER_COLUMN_C_G_GROUPID_2 = "reportDefinition.groupId = ?";
    public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_C_C_G = new FinderPath(
            ReportDefinitionModelImpl.ENTITY_CACHE_ENABLED, ReportDefinitionModelImpl.FINDER_CACHE_ENABLED,
            ReportDefinitionImpl.class, FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findByC_C_G",
            new String[] { String.class.getName(), Long.class.getName(), Long.class.getName(),

                    Integer.class.getName(), Integer.class.getName(), OrderByComparator.class.getName() });
    public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_C_G = new FinderPath(
            ReportDefinitionModelImpl.ENTITY_CACHE_ENABLED, ReportDefinitionModelImpl.FINDER_CACHE_ENABLED,
            ReportDefinitionImpl.class, FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findByC_C_G",
            new String[] { String.class.getName(), Long.class.getName(), Long.class.getName() },
            ReportDefinitionModelImpl.CATEGORY_COLUMN_BITMASK | ReportDefinitionModelImpl.COMPANYID_COLUMN_BITMASK
                    | ReportDefinitionModelImpl.GROUPID_COLUMN_BITMASK);
    public static final FinderPath FINDER_PATH_COUNT_BY_C_C_G = new FinderPath(
            ReportDefinitionModelImpl.ENTITY_CACHE_ENABLED, ReportDefinitionModelImpl.FINDER_CACHE_ENABLED,
            Long.class, FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByC_C_G",
            new String[] { String.class.getName(), Long.class.getName(), Long.class.getName() });
    public static final FinderPath FINDER_PATH_WITH_PAGINATION_COUNT_BY_C_C_G = new FinderPath(
            ReportDefinitionModelImpl.ENTITY_CACHE_ENABLED, ReportDefinitionModelImpl.FINDER_CACHE_ENABLED,
            Long.class, FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "countByC_C_G",
            new String[] { String.class.getName(), Long.class.getName(), Long.class.getName() });

    /**
     * Returns all the report definitions where category = &#63; and companyId = &#63; and groupId = &#63;.
     *
     * @param category the category
     * @param companyId the company ID
     * @param groupId the group ID
     * @return the matching report definitions
     */
    @Override
    public List<ReportDefinition> findByC_C_G(String category, long companyId, long groupId) {
        return findByC_C_G(category, companyId, groupId, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
    }

    /**
     * Returns a range of all the report definitions where category = &#63; and companyId = &#63; and groupId = &#63;.
     *
     * <p>
     * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link ReportDefinitionModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
     * </p>
     *
     * @param category the category
     * @param companyId the company ID
     * @param groupId the group ID
     * @param start the lower bound of the range of report definitions
     * @param end the upper bound of the range of report definitions (not inclusive)
     * @return the range of matching report definitions
     */
    @Override
    public List<ReportDefinition> findByC_C_G(String category, long companyId, long groupId, int start, int end) {
        return findByC_C_G(category, companyId, groupId, start, end, null);
    }

    /**
     * Returns an ordered range of all the report definitions where category = &#63; and companyId = &#63; and groupId = &#63;.
     *
     * <p>
     * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link ReportDefinitionModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
     * </p>
     *
     * @param category the category
     * @param companyId the company ID
     * @param groupId the group ID
     * @param start the lower bound of the range of report definitions
     * @param end the upper bound of the range of report definitions (not inclusive)
     * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
     * @return the ordered range of matching report definitions
     */
    @Override
    public List<ReportDefinition> findByC_C_G(String category, long companyId, long groupId, int start, int end,
            OrderByComparator<ReportDefinition> orderByComparator) {
        return findByC_C_G(category, companyId, groupId, start, end, orderByComparator, true);
    }

    /**
     * Returns an ordered range of all the report definitions where category = &#63; and companyId = &#63; and groupId = &#63;.
     *
     * <p>
     * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link ReportDefinitionModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
     * </p>
     *
     * @param category the category
     * @param companyId the company ID
     * @param groupId the group ID
     * @param start the lower bound of the range of report definitions
     * @param end the upper bound of the range of report definitions (not inclusive)
     * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
     * @param retrieveFromCache whether to retrieve from the finder cache
     * @return the ordered range of matching report definitions
     */
    @Override
    public List<ReportDefinition> findByC_C_G(String category, long companyId, long groupId, int start, int end,
            OrderByComparator<ReportDefinition> orderByComparator, boolean retrieveFromCache) {
        boolean pagination = true;
        FinderPath finderPath = null;
        Object[] finderArgs = null;

        if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) && (orderByComparator == null)) {
            pagination = false;
            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_C_G;
            finderArgs = new Object[] { category, companyId, groupId };
        } else {
            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_C_C_G;
            finderArgs = new Object[] { category, companyId, groupId,

                    start, end, orderByComparator };
        }

        List<ReportDefinition> list = null;

        if (retrieveFromCache) {
            list = (List<ReportDefinition>) finderCache.getResult(finderPath, finderArgs, this);

            if ((list != null) && !list.isEmpty()) {
                for (ReportDefinition reportDefinition : list) {
                    if (!Objects.equals(category, reportDefinition.getCategory())
                            || (companyId != reportDefinition.getCompanyId())
                            || (groupId != reportDefinition.getGroupId())) {
                        list = null;

                        break;
                    }
                }
            }
        }

        if (list == null) {
            StringBundler query = null;

            if (orderByComparator != null) {
                query = new StringBundler(5 + (orderByComparator.getOrderByFields().length * 2));
            } else {
                query = new StringBundler(5);
            }

            query.append(_SQL_SELECT_REPORTDEFINITION_WHERE);

            boolean bindCategory = false;

            if (category == null) {
                query.append(_FINDER_COLUMN_C_C_G_CATEGORY_1);
            } else if (category.equals(StringPool.BLANK)) {
                query.append(_FINDER_COLUMN_C_C_G_CATEGORY_3);
            } else {
                bindCategory = true;

                query.append(_FINDER_COLUMN_C_C_G_CATEGORY_2);
            }

            query.append(_FINDER_COLUMN_C_C_G_COMPANYID_2);

            query.append(_FINDER_COLUMN_C_C_G_GROUPID_2);

            if (orderByComparator != null) {
                appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS, orderByComparator);
            } else if (pagination) {
                query.append(ReportDefinitionModelImpl.ORDER_BY_JPQL);
            }

            String sql = query.toString();

            Session session = null;

            try {
                session = openSession();

                Query q = session.createQuery(sql);

                QueryPos qPos = QueryPos.getInstance(q);

                if (bindCategory) {
                    qPos.add(category);
                }

                qPos.add(companyId);

                qPos.add(groupId);

                if (!pagination) {
                    list = (List<ReportDefinition>) QueryUtil.list(q, getDialect(), start, end, false);

                    Collections.sort(list);

                    list = Collections.unmodifiableList(list);
                } else {
                    list = (List<ReportDefinition>) QueryUtil.list(q, getDialect(), start, end);
                }

                cacheResult(list);

                finderCache.putResult(finderPath, finderArgs, list);
            } catch (Exception e) {
                finderCache.removeResult(finderPath, finderArgs);

                throw processException(e);
            } finally {
                closeSession(session);
            }
        }

        return list;
    }

    /**
     * Returns the first report definition in the ordered set where category = &#63; and companyId = &#63; and groupId = &#63;.
     *
     * @param category the category
     * @param companyId the company ID
     * @param groupId the group ID
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the first matching report definition
     * @throws NoSuchReportDefinitionException if a matching report definition could not be found
     */
    @Override
    public ReportDefinition findByC_C_G_First(String category, long companyId, long groupId,
            OrderByComparator<ReportDefinition> orderByComparator) throws NoSuchReportDefinitionException {
        ReportDefinition reportDefinition = fetchByC_C_G_First(category, companyId, groupId, orderByComparator);

        if (reportDefinition != null) {
            return reportDefinition;
        }

        StringBundler msg = new StringBundler(8);

        msg.append(_NO_SUCH_ENTITY_WITH_KEY);

        msg.append("category=");
        msg.append(category);

        msg.append(", companyId=");
        msg.append(companyId);

        msg.append(", groupId=");
        msg.append(groupId);

        msg.append(StringPool.CLOSE_CURLY_BRACE);

        throw new NoSuchReportDefinitionException(msg.toString());
    }

    /**
     * Returns the first report definition in the ordered set where category = &#63; and companyId = &#63; and groupId = &#63;.
     *
     * @param category the category
     * @param companyId the company ID
     * @param groupId the group ID
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the first matching report definition, or <code>null</code> if a matching report definition could not be found
     */
    @Override
    public ReportDefinition fetchByC_C_G_First(String category, long companyId, long groupId,
            OrderByComparator<ReportDefinition> orderByComparator) {
        List<ReportDefinition> list = findByC_C_G(category, companyId, groupId, 0, 1, orderByComparator);

        if (!list.isEmpty()) {
            return list.get(0);
        }

        return null;
    }

    /**
     * Returns the last report definition in the ordered set where category = &#63; and companyId = &#63; and groupId = &#63;.
     *
     * @param category the category
     * @param companyId the company ID
     * @param groupId the group ID
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the last matching report definition
     * @throws NoSuchReportDefinitionException if a matching report definition could not be found
     */
    @Override
    public ReportDefinition findByC_C_G_Last(String category, long companyId, long groupId,
            OrderByComparator<ReportDefinition> orderByComparator) throws NoSuchReportDefinitionException {
        ReportDefinition reportDefinition = fetchByC_C_G_Last(category, companyId, groupId, orderByComparator);

        if (reportDefinition != null) {
            return reportDefinition;
        }

        StringBundler msg = new StringBundler(8);

        msg.append(_NO_SUCH_ENTITY_WITH_KEY);

        msg.append("category=");
        msg.append(category);

        msg.append(", companyId=");
        msg.append(companyId);

        msg.append(", groupId=");
        msg.append(groupId);

        msg.append(StringPool.CLOSE_CURLY_BRACE);

        throw new NoSuchReportDefinitionException(msg.toString());
    }

    /**
     * Returns the last report definition in the ordered set where category = &#63; and companyId = &#63; and groupId = &#63;.
     *
     * @param category the category
     * @param companyId the company ID
     * @param groupId the group ID
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the last matching report definition, or <code>null</code> if a matching report definition could not be found
     */
    @Override
    public ReportDefinition fetchByC_C_G_Last(String category, long companyId, long groupId,
            OrderByComparator<ReportDefinition> orderByComparator) {
        int count = countByC_C_G(category, companyId, groupId);

        if (count == 0) {
            return null;
        }

        List<ReportDefinition> list = findByC_C_G(category, companyId, groupId, count - 1, count,
                orderByComparator);

        if (!list.isEmpty()) {
            return list.get(0);
        }

        return null;
    }

    /**
     * Returns the report definitions before and after the current report definition in the ordered set where category = &#63; and companyId = &#63; and groupId = &#63;.
     *
     * @param reportDefinitionId the primary key of the current report definition
     * @param category the category
     * @param companyId the company ID
     * @param groupId the group ID
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the previous, current, and next report definition
     * @throws NoSuchReportDefinitionException if a report definition with the primary key could not be found
     */
    @Override
    public ReportDefinition[] findByC_C_G_PrevAndNext(long reportDefinitionId, String category, long companyId,
            long groupId, OrderByComparator<ReportDefinition> orderByComparator)
            throws NoSuchReportDefinitionException {
        ReportDefinition reportDefinition = findByPrimaryKey(reportDefinitionId);

        Session session = null;

        try {
            session = openSession();

            ReportDefinition[] array = new ReportDefinitionImpl[3];

            array[0] = getByC_C_G_PrevAndNext(session, reportDefinition, category, companyId, groupId,
                    orderByComparator, true);

            array[1] = reportDefinition;

            array[2] = getByC_C_G_PrevAndNext(session, reportDefinition, category, companyId, groupId,
                    orderByComparator, false);

            return array;
        } catch (Exception e) {
            throw processException(e);
        } finally {
            closeSession(session);
        }
    }

    protected ReportDefinition getByC_C_G_PrevAndNext(Session session, ReportDefinition reportDefinition,
            String category, long companyId, long groupId, OrderByComparator<ReportDefinition> orderByComparator,
            boolean previous) {
        StringBundler query = null;

        if (orderByComparator != null) {
            query = new StringBundler(6 + (orderByComparator.getOrderByConditionFields().length * 3)
                    + (orderByComparator.getOrderByFields().length * 3));
        } else {
            query = new StringBundler(5);
        }

        query.append(_SQL_SELECT_REPORTDEFINITION_WHERE);

        boolean bindCategory = false;

        if (category == null) {
            query.append(_FINDER_COLUMN_C_C_G_CATEGORY_1);
        } else if (category.equals(StringPool.BLANK)) {
            query.append(_FINDER_COLUMN_C_C_G_CATEGORY_3);
        } else {
            bindCategory = true;

            query.append(_FINDER_COLUMN_C_C_G_CATEGORY_2);
        }

        query.append(_FINDER_COLUMN_C_C_G_COMPANYID_2);

        query.append(_FINDER_COLUMN_C_C_G_GROUPID_2);

        if (orderByComparator != null) {
            String[] orderByConditionFields = orderByComparator.getOrderByConditionFields();

            if (orderByConditionFields.length > 0) {
                query.append(WHERE_AND);
            }

            for (int i = 0; i < orderByConditionFields.length; i++) {
                query.append(_ORDER_BY_ENTITY_ALIAS);
                query.append(orderByConditionFields[i]);

                if ((i + 1) < orderByConditionFields.length) {
                    if (orderByComparator.isAscending() ^ previous) {
                        query.append(WHERE_GREATER_THAN_HAS_NEXT);
                    } else {
                        query.append(WHERE_LESSER_THAN_HAS_NEXT);
                    }
                } else {
                    if (orderByComparator.isAscending() ^ previous) {
                        query.append(WHERE_GREATER_THAN);
                    } else {
                        query.append(WHERE_LESSER_THAN);
                    }
                }
            }

            query.append(ORDER_BY_CLAUSE);

            String[] orderByFields = orderByComparator.getOrderByFields();

            for (int i = 0; i < orderByFields.length; i++) {
                query.append(_ORDER_BY_ENTITY_ALIAS);
                query.append(orderByFields[i]);

                if ((i + 1) < orderByFields.length) {
                    if (orderByComparator.isAscending() ^ previous) {
                        query.append(ORDER_BY_ASC_HAS_NEXT);
                    } else {
                        query.append(ORDER_BY_DESC_HAS_NEXT);
                    }
                } else {
                    if (orderByComparator.isAscending() ^ previous) {
                        query.append(ORDER_BY_ASC);
                    } else {
                        query.append(ORDER_BY_DESC);
                    }
                }
            }
        } else {
            query.append(ReportDefinitionModelImpl.ORDER_BY_JPQL);
        }

        String sql = query.toString();

        Query q = session.createQuery(sql);

        q.setFirstResult(0);
        q.setMaxResults(2);

        QueryPos qPos = QueryPos.getInstance(q);

        if (bindCategory) {
            qPos.add(category);
        }

        qPos.add(companyId);

        qPos.add(groupId);

        if (orderByComparator != null) {
            Object[] values = orderByComparator.getOrderByConditionValues(reportDefinition);

            for (Object value : values) {
                qPos.add(value);
            }
        }

        List<ReportDefinition> list = q.list();

        if (list.size() == 2) {
            return list.get(1);
        } else {
            return null;
        }
    }

    /**
     * Returns all the report definitions that the user has permission to view where category = &#63; and companyId = &#63; and groupId = &#63;.
     *
     * @param category the category
     * @param companyId the company ID
     * @param groupId the group ID
     * @return the matching report definitions that the user has permission to view
     */
    @Override
    public List<ReportDefinition> filterFindByC_C_G(String category, long companyId, long groupId) {
        return filterFindByC_C_G(category, companyId, groupId, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
    }

    /**
     * Returns a range of all the report definitions that the user has permission to view where category = &#63; and companyId = &#63; and groupId = &#63;.
     *
     * <p>
     * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link ReportDefinitionModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
     * </p>
     *
     * @param category the category
     * @param companyId the company ID
     * @param groupId the group ID
     * @param start the lower bound of the range of report definitions
     * @param end the upper bound of the range of report definitions (not inclusive)
     * @return the range of matching report definitions that the user has permission to view
     */
    @Override
    public List<ReportDefinition> filterFindByC_C_G(String category, long companyId, long groupId, int start,
            int end) {
        return filterFindByC_C_G(category, companyId, groupId, start, end, null);
    }

    /**
     * Returns an ordered range of all the report definitions that the user has permissions to view where category = &#63; and companyId = &#63; and groupId = &#63;.
     *
     * <p>
     * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link ReportDefinitionModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
     * </p>
     *
     * @param category the category
     * @param companyId the company ID
     * @param groupId the group ID
     * @param start the lower bound of the range of report definitions
     * @param end the upper bound of the range of report definitions (not inclusive)
     * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
     * @return the ordered range of matching report definitions that the user has permission to view
     */
    @Override
    public List<ReportDefinition> filterFindByC_C_G(String category, long companyId, long groupId, int start,
            int end, OrderByComparator<ReportDefinition> orderByComparator) {
        if (!InlineSQLHelperUtil.isEnabled(groupId)) {
            return findByC_C_G(category, companyId, groupId, start, end, orderByComparator);
        }

        StringBundler query = null;

        if (orderByComparator != null) {
            query = new StringBundler(5 + (orderByComparator.getOrderByFields().length * 2));
        } else {
            query = new StringBundler(6);
        }

        if (getDB().isSupportsInlineDistinct()) {
            query.append(_FILTER_SQL_SELECT_REPORTDEFINITION_WHERE);
        } else {
            query.append(_FILTER_SQL_SELECT_REPORTDEFINITION_NO_INLINE_DISTINCT_WHERE_1);
        }

        boolean bindCategory = false;

        if (category == null) {
            query.append(_FINDER_COLUMN_C_C_G_CATEGORY_1);
        } else if (category.equals(StringPool.BLANK)) {
            query.append(_FINDER_COLUMN_C_C_G_CATEGORY_3);
        } else {
            bindCategory = true;

            query.append(_FINDER_COLUMN_C_C_G_CATEGORY_2);
        }

        query.append(_FINDER_COLUMN_C_C_G_COMPANYID_2);

        query.append(_FINDER_COLUMN_C_C_G_GROUPID_2);

        if (!getDB().isSupportsInlineDistinct()) {
            query.append(_FILTER_SQL_SELECT_REPORTDEFINITION_NO_INLINE_DISTINCT_WHERE_2);
        }

        if (orderByComparator != null) {
            if (getDB().isSupportsInlineDistinct()) {
                appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS, orderByComparator, true);
            } else {
                appendOrderByComparator(query, _ORDER_BY_ENTITY_TABLE, orderByComparator, true);
            }
        } else {
            if (getDB().isSupportsInlineDistinct()) {
                query.append(ReportDefinitionModelImpl.ORDER_BY_JPQL);
            } else {
                query.append(ReportDefinitionModelImpl.ORDER_BY_SQL);
            }
        }

        String sql = InlineSQLHelperUtil.replacePermissionCheck(query.toString(), ReportDefinition.class.getName(),
                _FILTER_ENTITY_TABLE_FILTER_PK_COLUMN, groupId);

        Session session = null;

        try {
            session = openSession();

            SQLQuery q = session.createSynchronizedSQLQuery(sql);

            if (getDB().isSupportsInlineDistinct()) {
                q.addEntity(_FILTER_ENTITY_ALIAS, ReportDefinitionImpl.class);
            } else {
                q.addEntity(_FILTER_ENTITY_TABLE, ReportDefinitionImpl.class);
            }

            QueryPos qPos = QueryPos.getInstance(q);

            if (bindCategory) {
                qPos.add(category);
            }

            qPos.add(companyId);

            qPos.add(groupId);

            return (List<ReportDefinition>) QueryUtil.list(q, getDialect(), start, end);
        } catch (Exception e) {
            throw processException(e);
        } finally {
            closeSession(session);
        }
    }

    /**
     * Returns the report definitions before and after the current report definition in the ordered set of report definitions that the user has permission to view where category = &#63; and companyId = &#63; and groupId = &#63;.
     *
     * @param reportDefinitionId the primary key of the current report definition
     * @param category the category
     * @param companyId the company ID
     * @param groupId the group ID
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the previous, current, and next report definition
     * @throws NoSuchReportDefinitionException if a report definition with the primary key could not be found
     */
    @Override
    public ReportDefinition[] filterFindByC_C_G_PrevAndNext(long reportDefinitionId, String category,
            long companyId, long groupId, OrderByComparator<ReportDefinition> orderByComparator)
            throws NoSuchReportDefinitionException {
        if (!InlineSQLHelperUtil.isEnabled(groupId)) {
            return findByC_C_G_PrevAndNext(reportDefinitionId, category, companyId, groupId, orderByComparator);
        }

        ReportDefinition reportDefinition = findByPrimaryKey(reportDefinitionId);

        Session session = null;

        try {
            session = openSession();

            ReportDefinition[] array = new ReportDefinitionImpl[3];

            array[0] = filterGetByC_C_G_PrevAndNext(session, reportDefinition, category, companyId, groupId,
                    orderByComparator, true);

            array[1] = reportDefinition;

            array[2] = filterGetByC_C_G_PrevAndNext(session, reportDefinition, category, companyId, groupId,
                    orderByComparator, false);

            return array;
        } catch (Exception e) {
            throw processException(e);
        } finally {
            closeSession(session);
        }
    }

    protected ReportDefinition filterGetByC_C_G_PrevAndNext(Session session, ReportDefinition reportDefinition,
            String category, long companyId, long groupId, OrderByComparator<ReportDefinition> orderByComparator,
            boolean previous) {
        StringBundler query = null;

        if (orderByComparator != null) {
            query = new StringBundler(7 + (orderByComparator.getOrderByConditionFields().length * 3)
                    + (orderByComparator.getOrderByFields().length * 3));
        } else {
            query = new StringBundler(6);
        }

        if (getDB().isSupportsInlineDistinct()) {
            query.append(_FILTER_SQL_SELECT_REPORTDEFINITION_WHERE);
        } else {
            query.append(_FILTER_SQL_SELECT_REPORTDEFINITION_NO_INLINE_DISTINCT_WHERE_1);
        }

        boolean bindCategory = false;

        if (category == null) {
            query.append(_FINDER_COLUMN_C_C_G_CATEGORY_1);
        } else if (category.equals(StringPool.BLANK)) {
            query.append(_FINDER_COLUMN_C_C_G_CATEGORY_3);
        } else {
            bindCategory = true;

            query.append(_FINDER_COLUMN_C_C_G_CATEGORY_2);
        }

        query.append(_FINDER_COLUMN_C_C_G_COMPANYID_2);

        query.append(_FINDER_COLUMN_C_C_G_GROUPID_2);

        if (!getDB().isSupportsInlineDistinct()) {
            query.append(_FILTER_SQL_SELECT_REPORTDEFINITION_NO_INLINE_DISTINCT_WHERE_2);
        }

        if (orderByComparator != null) {
            String[] orderByConditionFields = orderByComparator.getOrderByConditionFields();

            if (orderByConditionFields.length > 0) {
                query.append(WHERE_AND);
            }

            for (int i = 0; i < orderByConditionFields.length; i++) {
                if (getDB().isSupportsInlineDistinct()) {
                    query.append(_ORDER_BY_ENTITY_ALIAS);
                } else {
                    query.append(_ORDER_BY_ENTITY_TABLE);
                }

                query.append(orderByConditionFields[i]);

                if ((i + 1) < orderByConditionFields.length) {
                    if (orderByComparator.isAscending() ^ previous) {
                        query.append(WHERE_GREATER_THAN_HAS_NEXT);
                    } else {
                        query.append(WHERE_LESSER_THAN_HAS_NEXT);
                    }
                } else {
                    if (orderByComparator.isAscending() ^ previous) {
                        query.append(WHERE_GREATER_THAN);
                    } else {
                        query.append(WHERE_LESSER_THAN);
                    }
                }
            }

            query.append(ORDER_BY_CLAUSE);

            String[] orderByFields = orderByComparator.getOrderByFields();

            for (int i = 0; i < orderByFields.length; i++) {
                if (getDB().isSupportsInlineDistinct()) {
                    query.append(_ORDER_BY_ENTITY_ALIAS);
                } else {
                    query.append(_ORDER_BY_ENTITY_TABLE);
                }

                query.append(orderByFields[i]);

                if ((i + 1) < orderByFields.length) {
                    if (orderByComparator.isAscending() ^ previous) {
                        query.append(ORDER_BY_ASC_HAS_NEXT);
                    } else {
                        query.append(ORDER_BY_DESC_HAS_NEXT);
                    }
                } else {
                    if (orderByComparator.isAscending() ^ previous) {
                        query.append(ORDER_BY_ASC);
                    } else {
                        query.append(ORDER_BY_DESC);
                    }
                }
            }
        } else {
            if (getDB().isSupportsInlineDistinct()) {
                query.append(ReportDefinitionModelImpl.ORDER_BY_JPQL);
            } else {
                query.append(ReportDefinitionModelImpl.ORDER_BY_SQL);
            }
        }

        String sql = InlineSQLHelperUtil.replacePermissionCheck(query.toString(), ReportDefinition.class.getName(),
                _FILTER_ENTITY_TABLE_FILTER_PK_COLUMN, groupId);

        SQLQuery q = session.createSynchronizedSQLQuery(sql);

        q.setFirstResult(0);
        q.setMaxResults(2);

        if (getDB().isSupportsInlineDistinct()) {
            q.addEntity(_FILTER_ENTITY_ALIAS, ReportDefinitionImpl.class);
        } else {
            q.addEntity(_FILTER_ENTITY_TABLE, ReportDefinitionImpl.class);
        }

        QueryPos qPos = QueryPos.getInstance(q);

        if (bindCategory) {
            qPos.add(category);
        }

        qPos.add(companyId);

        qPos.add(groupId);

        if (orderByComparator != null) {
            Object[] values = orderByComparator.getOrderByConditionValues(reportDefinition);

            for (Object value : values) {
                qPos.add(value);
            }
        }

        List<ReportDefinition> list = q.list();

        if (list.size() == 2) {
            return list.get(1);
        } else {
            return null;
        }
    }

    /**
     * Returns all the report definitions that the user has permission to view where category = any &#63; and companyId = &#63; and groupId = &#63;.
     *
     * @param categories the categories
     * @param companyId the company ID
     * @param groupId the group ID
     * @return the matching report definitions that the user has permission to view
     */
    @Override
    public List<ReportDefinition> filterFindByC_C_G(String[] categories, long companyId, long groupId) {
        return filterFindByC_C_G(categories, companyId, groupId, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
    }

    /**
     * Returns a range of all the report definitions that the user has permission to view where category = any &#63; and companyId = &#63; and groupId = &#63;.
     *
     * <p>
     * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link ReportDefinitionModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
     * </p>
     *
     * @param categories the categories
     * @param companyId the company ID
     * @param groupId the group ID
     * @param start the lower bound of the range of report definitions
     * @param end the upper bound of the range of report definitions (not inclusive)
     * @return the range of matching report definitions that the user has permission to view
     */
    @Override
    public List<ReportDefinition> filterFindByC_C_G(String[] categories, long companyId, long groupId, int start,
            int end) {
        return filterFindByC_C_G(categories, companyId, groupId, start, end, null);
    }

    /**
     * Returns an ordered range of all the report definitions that the user has permission to view where category = any &#63; and companyId = &#63; and groupId = &#63;.
     *
     * <p>
     * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link ReportDefinitionModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
     * </p>
     *
     * @param categories the categories
     * @param companyId the company ID
     * @param groupId the group ID
     * @param start the lower bound of the range of report definitions
     * @param end the upper bound of the range of report definitions (not inclusive)
     * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
     * @return the ordered range of matching report definitions that the user has permission to view
     */
    @Override
    public List<ReportDefinition> filterFindByC_C_G(String[] categories, long companyId, long groupId, int start,
            int end, OrderByComparator<ReportDefinition> orderByComparator) {
        if (!InlineSQLHelperUtil.isEnabled(groupId)) {
            return findByC_C_G(categories, companyId, groupId, start, end, orderByComparator);
        }

        if (categories == null) {
            categories = new String[0];
        } else if (categories.length > 1) {
            categories = ArrayUtil.distinct(categories, NULL_SAFE_STRING_COMPARATOR);

            Arrays.sort(categories, NULL_SAFE_STRING_COMPARATOR);
        }

        StringBundler query = new StringBundler();

        if (getDB().isSupportsInlineDistinct()) {
            query.append(_FILTER_SQL_SELECT_REPORTDEFINITION_WHERE);
        } else {
            query.append(_FILTER_SQL_SELECT_REPORTDEFINITION_NO_INLINE_DISTINCT_WHERE_1);
        }

        if (categories.length > 0) {
            query.append(StringPool.OPEN_PARENTHESIS);

            for (int i = 0; i < categories.length; i++) {
                String category = categories[i];

                if (category == null) {
                    query.append(_FINDER_COLUMN_C_C_G_CATEGORY_4);
                } else if (category.equals(StringPool.BLANK)) {
                    query.append(_FINDER_COLUMN_C_C_G_CATEGORY_6);
                } else {
                    query.append(_FINDER_COLUMN_C_C_G_CATEGORY_5);
                }

                if ((i + 1) < categories.length) {
                    query.append(WHERE_OR);
                }
            }

            query.append(StringPool.CLOSE_PARENTHESIS);

            query.append(WHERE_AND);
        }

        query.append(_FINDER_COLUMN_C_C_G_COMPANYID_2);

        query.append(_FINDER_COLUMN_C_C_G_GROUPID_2);

        query.setStringAt(removeConjunction(query.stringAt(query.index() - 1)), query.index() - 1);

        if (!getDB().isSupportsInlineDistinct()) {
            query.append(_FILTER_SQL_SELECT_REPORTDEFINITION_NO_INLINE_DISTINCT_WHERE_2);
        }

        if (orderByComparator != null) {
            if (getDB().isSupportsInlineDistinct()) {
                appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS, orderByComparator, true);
            } else {
                appendOrderByComparator(query, _ORDER_BY_ENTITY_TABLE, orderByComparator, true);
            }
        } else {
            if (getDB().isSupportsInlineDistinct()) {
                query.append(ReportDefinitionModelImpl.ORDER_BY_JPQL);
            } else {
                query.append(ReportDefinitionModelImpl.ORDER_BY_SQL);
            }
        }

        String sql = InlineSQLHelperUtil.replacePermissionCheck(query.toString(), ReportDefinition.class.getName(),
                _FILTER_ENTITY_TABLE_FILTER_PK_COLUMN, groupId);

        Session session = null;

        try {
            session = openSession();

            SQLQuery q = session.createSynchronizedSQLQuery(sql);

            if (getDB().isSupportsInlineDistinct()) {
                q.addEntity(_FILTER_ENTITY_ALIAS, ReportDefinitionImpl.class);
            } else {
                q.addEntity(_FILTER_ENTITY_TABLE, ReportDefinitionImpl.class);
            }

            QueryPos qPos = QueryPos.getInstance(q);

            for (String category : categories) {
                if ((category != null) && !category.isEmpty()) {
                    qPos.add(category);
                }
            }

            qPos.add(companyId);

            qPos.add(groupId);

            return (List<ReportDefinition>) QueryUtil.list(q, getDialect(), start, end);
        } catch (Exception e) {
            throw processException(e);
        } finally {
            closeSession(session);
        }
    }

    /**
     * Returns all the report definitions where category = any &#63; and companyId = &#63; and groupId = &#63;.
     *
     * <p>
     * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link ReportDefinitionModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
     * </p>
     *
     * @param categories the categories
     * @param companyId the company ID
     * @param groupId the group ID
     * @return the matching report definitions
     */
    @Override
    public List<ReportDefinition> findByC_C_G(String[] categories, long companyId, long groupId) {
        return findByC_C_G(categories, companyId, groupId, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
    }

    /**
     * Returns a range of all the report definitions where category = any &#63; and companyId = &#63; and groupId = &#63;.
     *
     * <p>
     * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link ReportDefinitionModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
     * </p>
     *
     * @param categories the categories
     * @param companyId the company ID
     * @param groupId the group ID
     * @param start the lower bound of the range of report definitions
     * @param end the upper bound of the range of report definitions (not inclusive)
     * @return the range of matching report definitions
     */
    @Override
    public List<ReportDefinition> findByC_C_G(String[] categories, long companyId, long groupId, int start,
            int end) {
        return findByC_C_G(categories, companyId, groupId, start, end, null);
    }

    /**
     * Returns an ordered range of all the report definitions where category = any &#63; and companyId = &#63; and groupId = &#63;.
     *
     * <p>
     * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link ReportDefinitionModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
     * </p>
     *
     * @param categories the categories
     * @param companyId the company ID
     * @param groupId the group ID
     * @param start the lower bound of the range of report definitions
     * @param end the upper bound of the range of report definitions (not inclusive)
     * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
     * @return the ordered range of matching report definitions
     */
    @Override
    public List<ReportDefinition> findByC_C_G(String[] categories, long companyId, long groupId, int start, int end,
            OrderByComparator<ReportDefinition> orderByComparator) {
        return findByC_C_G(categories, companyId, groupId, start, end, orderByComparator, true);
    }

    /**
     * Returns an ordered range of all the report definitions where category = &#63; and companyId = &#63; and groupId = &#63;, optionally using the finder cache.
     *
     * <p>
     * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link ReportDefinitionModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
     * </p>
     *
     * @param category the category
     * @param companyId the company ID
     * @param groupId the group ID
     * @param start the lower bound of the range of report definitions
     * @param end the upper bound of the range of report definitions (not inclusive)
     * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
     * @param retrieveFromCache whether to retrieve from the finder cache
     * @return the ordered range of matching report definitions
     */
    @Override
    public List<ReportDefinition> findByC_C_G(String[] categories, long companyId, long groupId, int start, int end,
            OrderByComparator<ReportDefinition> orderByComparator, boolean retrieveFromCache) {
        if (categories == null) {
            categories = new String[0];
        } else if (categories.length > 1) {
            categories = ArrayUtil.distinct(categories, NULL_SAFE_STRING_COMPARATOR);

            Arrays.sort(categories, NULL_SAFE_STRING_COMPARATOR);
        }

        if (categories.length == 1) {
            return findByC_C_G(categories[0], companyId, groupId, start, end, orderByComparator);
        }

        boolean pagination = true;
        Object[] finderArgs = null;

        if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) && (orderByComparator == null)) {
            pagination = false;
            finderArgs = new Object[] { StringUtil.merge(categories), companyId, groupId };
        } else {
            finderArgs = new Object[] { StringUtil.merge(categories), companyId, groupId,

                    start, end, orderByComparator };
        }

        List<ReportDefinition> list = null;

        if (retrieveFromCache) {
            list = (List<ReportDefinition>) finderCache.getResult(FINDER_PATH_WITH_PAGINATION_FIND_BY_C_C_G,
                    finderArgs, this);

            if ((list != null) && !list.isEmpty()) {
                for (ReportDefinition reportDefinition : list) {
                    if (!ArrayUtil.contains(categories, reportDefinition.getCategory())
                            || (companyId != reportDefinition.getCompanyId())
                            || (groupId != reportDefinition.getGroupId())) {
                        list = null;

                        break;
                    }
                }
            }
        }

        if (list == null) {
            StringBundler query = new StringBundler();

            query.append(_SQL_SELECT_REPORTDEFINITION_WHERE);

            if (categories.length > 0) {
                query.append(StringPool.OPEN_PARENTHESIS);

                for (int i = 0; i < categories.length; i++) {
                    String category = categories[i];

                    if (category == null) {
                        query.append(_FINDER_COLUMN_C_C_G_CATEGORY_4);
                    } else if (category.equals(StringPool.BLANK)) {
                        query.append(_FINDER_COLUMN_C_C_G_CATEGORY_6);
                    } else {
                        query.append(_FINDER_COLUMN_C_C_G_CATEGORY_5);
                    }

                    if ((i + 1) < categories.length) {
                        query.append(WHERE_OR);
                    }
                }

                query.append(StringPool.CLOSE_PARENTHESIS);

                query.append(WHERE_AND);
            }

            query.append(_FINDER_COLUMN_C_C_G_COMPANYID_2);

            query.append(_FINDER_COLUMN_C_C_G_GROUPID_2);

            query.setStringAt(removeConjunction(query.stringAt(query.index() - 1)), query.index() - 1);

            if (orderByComparator != null) {
                appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS, orderByComparator);
            } else if (pagination) {
                query.append(ReportDefinitionModelImpl.ORDER_BY_JPQL);
            }

            String sql = query.toString();

            Session session = null;

            try {
                session = openSession();

                Query q = session.createQuery(sql);

                QueryPos qPos = QueryPos.getInstance(q);

                for (String category : categories) {
                    if ((category != null) && !category.isEmpty()) {
                        qPos.add(category);
                    }
                }

                qPos.add(companyId);

                qPos.add(groupId);

                if (!pagination) {
                    list = (List<ReportDefinition>) QueryUtil.list(q, getDialect(), start, end, false);

                    Collections.sort(list);

                    list = Collections.unmodifiableList(list);
                } else {
                    list = (List<ReportDefinition>) QueryUtil.list(q, getDialect(), start, end);
                }

                cacheResult(list);

                finderCache.putResult(FINDER_PATH_WITH_PAGINATION_FIND_BY_C_C_G, finderArgs, list);
            } catch (Exception e) {
                finderCache.removeResult(FINDER_PATH_WITH_PAGINATION_FIND_BY_C_C_G, finderArgs);

                throw processException(e);
            } finally {
                closeSession(session);
            }
        }

        return list;
    }

    /**
     * Removes all the report definitions where category = &#63; and companyId = &#63; and groupId = &#63; from the database.
     *
     * @param category the category
     * @param companyId the company ID
     * @param groupId the group ID
     */
    @Override
    public void removeByC_C_G(String category, long companyId, long groupId) {
        for (ReportDefinition reportDefinition : findByC_C_G(category, companyId, groupId, QueryUtil.ALL_POS,
                QueryUtil.ALL_POS, null)) {
            remove(reportDefinition);
        }
    }

    /**
     * Returns the number of report definitions where category = &#63; and companyId = &#63; and groupId = &#63;.
     *
     * @param category the category
     * @param companyId the company ID
     * @param groupId the group ID
     * @return the number of matching report definitions
     */
    @Override
    public int countByC_C_G(String category, long companyId, long groupId) {
        FinderPath finderPath = FINDER_PATH_COUNT_BY_C_C_G;

        Object[] finderArgs = new Object[] { category, companyId, groupId };

        Long count = (Long) finderCache.getResult(finderPath, finderArgs, this);

        if (count == null) {
            StringBundler query = new StringBundler(4);

            query.append(_SQL_COUNT_REPORTDEFINITION_WHERE);

            boolean bindCategory = false;

            if (category == null) {
                query.append(_FINDER_COLUMN_C_C_G_CATEGORY_1);
            } else if (category.equals(StringPool.BLANK)) {
                query.append(_FINDER_COLUMN_C_C_G_CATEGORY_3);
            } else {
                bindCategory = true;

                query.append(_FINDER_COLUMN_C_C_G_CATEGORY_2);
            }

            query.append(_FINDER_COLUMN_C_C_G_COMPANYID_2);

            query.append(_FINDER_COLUMN_C_C_G_GROUPID_2);

            String sql = query.toString();

            Session session = null;

            try {
                session = openSession();

                Query q = session.createQuery(sql);

                QueryPos qPos = QueryPos.getInstance(q);

                if (bindCategory) {
                    qPos.add(category);
                }

                qPos.add(companyId);

                qPos.add(groupId);

                count = (Long) q.uniqueResult();

                finderCache.putResult(finderPath, finderArgs, count);
            } catch (Exception e) {
                finderCache.removeResult(finderPath, finderArgs);

                throw processException(e);
            } finally {
                closeSession(session);
            }
        }

        return count.intValue();
    }

    /**
     * Returns the number of report definitions where category = any &#63; and companyId = &#63; and groupId = &#63;.
     *
     * @param categories the categories
     * @param companyId the company ID
     * @param groupId the group ID
     * @return the number of matching report definitions
     */
    @Override
    public int countByC_C_G(String[] categories, long companyId, long groupId) {
        if (categories == null) {
            categories = new String[0];
        } else if (categories.length > 1) {
            categories = ArrayUtil.distinct(categories, NULL_SAFE_STRING_COMPARATOR);

            Arrays.sort(categories, NULL_SAFE_STRING_COMPARATOR);
        }

        Object[] finderArgs = new Object[] { StringUtil.merge(categories), companyId, groupId };

        Long count = (Long) finderCache.getResult(FINDER_PATH_WITH_PAGINATION_COUNT_BY_C_C_G, finderArgs, this);

        if (count == null) {
            StringBundler query = new StringBundler();

            query.append(_SQL_COUNT_REPORTDEFINITION_WHERE);

            if (categories.length > 0) {
                query.append(StringPool.OPEN_PARENTHESIS);

                for (int i = 0; i < categories.length; i++) {
                    String category = categories[i];

                    if (category == null) {
                        query.append(_FINDER_COLUMN_C_C_G_CATEGORY_4);
                    } else if (category.equals(StringPool.BLANK)) {
                        query.append(_FINDER_COLUMN_C_C_G_CATEGORY_6);
                    } else {
                        query.append(_FINDER_COLUMN_C_C_G_CATEGORY_5);
                    }

                    if ((i + 1) < categories.length) {
                        query.append(WHERE_OR);
                    }
                }

                query.append(StringPool.CLOSE_PARENTHESIS);

                query.append(WHERE_AND);
            }

            query.append(_FINDER_COLUMN_C_C_G_COMPANYID_2);

            query.append(_FINDER_COLUMN_C_C_G_GROUPID_2);

            query.setStringAt(removeConjunction(query.stringAt(query.index() - 1)), query.index() - 1);

            String sql = query.toString();

            Session session = null;

            try {
                session = openSession();

                Query q = session.createQuery(sql);

                QueryPos qPos = QueryPos.getInstance(q);

                for (String category : categories) {
                    if ((category != null) && !category.isEmpty()) {
                        qPos.add(category);
                    }
                }

                qPos.add(companyId);

                qPos.add(groupId);

                count = (Long) q.uniqueResult();

                finderCache.putResult(FINDER_PATH_WITH_PAGINATION_COUNT_BY_C_C_G, finderArgs, count);
            } catch (Exception e) {
                finderCache.removeResult(FINDER_PATH_WITH_PAGINATION_COUNT_BY_C_C_G, finderArgs);

                throw processException(e);
            } finally {
                closeSession(session);
            }
        }

        return count.intValue();
    }

    /**
     * Returns the number of report definitions that the user has permission to view where category = &#63; and companyId = &#63; and groupId = &#63;.
     *
     * @param category the category
     * @param companyId the company ID
     * @param groupId the group ID
     * @return the number of matching report definitions that the user has permission to view
     */
    @Override
    public int filterCountByC_C_G(String category, long companyId, long groupId) {
        if (!InlineSQLHelperUtil.isEnabled(groupId)) {
            return countByC_C_G(category, companyId, groupId);
        }

        StringBundler query = new StringBundler(4);

        query.append(_FILTER_SQL_COUNT_REPORTDEFINITION_WHERE);

        boolean bindCategory = false;

        if (category == null) {
            query.append(_FINDER_COLUMN_C_C_G_CATEGORY_1);
        } else if (category.equals(StringPool.BLANK)) {
            query.append(_FINDER_COLUMN_C_C_G_CATEGORY_3);
        } else {
            bindCategory = true;

            query.append(_FINDER_COLUMN_C_C_G_CATEGORY_2);
        }

        query.append(_FINDER_COLUMN_C_C_G_COMPANYID_2);

        query.append(_FINDER_COLUMN_C_C_G_GROUPID_2);

        String sql = InlineSQLHelperUtil.replacePermissionCheck(query.toString(), ReportDefinition.class.getName(),
                _FILTER_ENTITY_TABLE_FILTER_PK_COLUMN, groupId);

        Session session = null;

        try {
            session = openSession();

            SQLQuery q = session.createSynchronizedSQLQuery(sql);

            q.addScalar(COUNT_COLUMN_NAME, com.liferay.portal.kernel.dao.orm.Type.LONG);

            QueryPos qPos = QueryPos.getInstance(q);

            if (bindCategory) {
                qPos.add(category);
            }

            qPos.add(companyId);

            qPos.add(groupId);

            Long count = (Long) q.uniqueResult();

            return count.intValue();
        } catch (Exception e) {
            throw processException(e);
        } finally {
            closeSession(session);
        }
    }

    /**
     * Returns the number of report definitions that the user has permission to view where category = any &#63; and companyId = &#63; and groupId = &#63;.
     *
     * @param categories the categories
     * @param companyId the company ID
     * @param groupId the group ID
     * @return the number of matching report definitions that the user has permission to view
     */
    @Override
    public int filterCountByC_C_G(String[] categories, long companyId, long groupId) {
        if (!InlineSQLHelperUtil.isEnabled(groupId)) {
            return countByC_C_G(categories, companyId, groupId);
        }

        if (categories == null) {
            categories = new String[0];
        } else if (categories.length > 1) {
            categories = ArrayUtil.distinct(categories, NULL_SAFE_STRING_COMPARATOR);

            Arrays.sort(categories, NULL_SAFE_STRING_COMPARATOR);
        }

        StringBundler query = new StringBundler();

        query.append(_FILTER_SQL_COUNT_REPORTDEFINITION_WHERE);

        if (categories.length > 0) {
            query.append(StringPool.OPEN_PARENTHESIS);

            for (int i = 0; i < categories.length; i++) {
                String category = categories[i];

                if (category == null) {
                    query.append(_FINDER_COLUMN_C_C_G_CATEGORY_4);
                } else if (category.equals(StringPool.BLANK)) {
                    query.append(_FINDER_COLUMN_C_C_G_CATEGORY_6);
                } else {
                    query.append(_FINDER_COLUMN_C_C_G_CATEGORY_5);
                }

                if ((i + 1) < categories.length) {
                    query.append(WHERE_OR);
                }
            }

            query.append(StringPool.CLOSE_PARENTHESIS);

            query.append(WHERE_AND);
        }

        query.append(_FINDER_COLUMN_C_C_G_COMPANYID_2);

        query.append(_FINDER_COLUMN_C_C_G_GROUPID_2);

        query.setStringAt(removeConjunction(query.stringAt(query.index() - 1)), query.index() - 1);

        String sql = InlineSQLHelperUtil.replacePermissionCheck(query.toString(), ReportDefinition.class.getName(),
                _FILTER_ENTITY_TABLE_FILTER_PK_COLUMN, groupId);

        Session session = null;

        try {
            session = openSession();

            SQLQuery q = session.createSynchronizedSQLQuery(sql);

            q.addScalar(COUNT_COLUMN_NAME, com.liferay.portal.kernel.dao.orm.Type.LONG);

            QueryPos qPos = QueryPos.getInstance(q);

            for (String category : categories) {
                if ((category != null) && !category.isEmpty()) {
                    qPos.add(category);
                }
            }

            qPos.add(companyId);

            qPos.add(groupId);

            Long count = (Long) q.uniqueResult();

            return count.intValue();
        } catch (Exception e) {
            throw processException(e);
        } finally {
            closeSession(session);
        }
    }

    private static final String _FINDER_COLUMN_C_C_G_CATEGORY_1 = "reportDefinition.category IS NULL AND ";
    private static final String _FINDER_COLUMN_C_C_G_CATEGORY_2 = "reportDefinition.category = ? AND ";
    private static final String _FINDER_COLUMN_C_C_G_CATEGORY_3 = "(reportDefinition.category IS NULL OR reportDefinition.category = '') AND ";
    private static final String _FINDER_COLUMN_C_C_G_CATEGORY_4 = "("
            + removeConjunction(_FINDER_COLUMN_C_C_G_CATEGORY_1) + ")";
    private static final String _FINDER_COLUMN_C_C_G_CATEGORY_5 = "("
            + removeConjunction(_FINDER_COLUMN_C_C_G_CATEGORY_2) + ")";
    private static final String _FINDER_COLUMN_C_C_G_CATEGORY_6 = "("
            + removeConjunction(_FINDER_COLUMN_C_C_G_CATEGORY_3) + ")";
    private static final String _FINDER_COLUMN_C_C_G_COMPANYID_2 = "reportDefinition.companyId = ? AND ";
    private static final String _FINDER_COLUMN_C_C_G_GROUPID_2 = "reportDefinition.groupId = ?";

    public ReportDefinitionPersistenceImpl() {
        setModelClass(ReportDefinition.class);
    }

    /**
     * Caches the report definition in the entity cache if it is enabled.
     *
     * @param reportDefinition the report definition
     */
    @Override
    public void cacheResult(ReportDefinition reportDefinition) {
        entityCache.putResult(ReportDefinitionModelImpl.ENTITY_CACHE_ENABLED, ReportDefinitionImpl.class,
                reportDefinition.getPrimaryKey(), reportDefinition);

        finderCache.putResult(FINDER_PATH_FETCH_BY_NAME, new Object[] { reportDefinition.getName() },
                reportDefinition);

        finderCache.putResult(FINDER_PATH_FETCH_BY_URLPATH, new Object[] { reportDefinition.getUrlPath() },
                reportDefinition);

        reportDefinition.resetOriginalValues();
    }

    /**
     * Caches the report definitions in the entity cache if it is enabled.
     *
     * @param reportDefinitions the report definitions
     */
    @Override
    public void cacheResult(List<ReportDefinition> reportDefinitions) {
        for (ReportDefinition reportDefinition : reportDefinitions) {
            if (entityCache.getResult(ReportDefinitionModelImpl.ENTITY_CACHE_ENABLED, ReportDefinitionImpl.class,
                    reportDefinition.getPrimaryKey()) == null) {
                cacheResult(reportDefinition);
            } else {
                reportDefinition.resetOriginalValues();
            }
        }
    }

    /**
     * Clears the cache for all report definitions.
     *
     * <p>
     * The {@link EntityCache} and {@link FinderCache} are both cleared by this method.
     * </p>
     */
    @Override
    public void clearCache() {
        entityCache.clearCache(ReportDefinitionImpl.class);

        finderCache.clearCache(FINDER_CLASS_NAME_ENTITY);
        finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
        finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
    }

    /**
     * Clears the cache for the report definition.
     *
     * <p>
     * The {@link EntityCache} and {@link FinderCache} are both cleared by this method.
     * </p>
     */
    @Override
    public void clearCache(ReportDefinition reportDefinition) {
        entityCache.removeResult(ReportDefinitionModelImpl.ENTITY_CACHE_ENABLED, ReportDefinitionImpl.class,
                reportDefinition.getPrimaryKey());

        finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
        finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);

        clearUniqueFindersCache((ReportDefinitionModelImpl) reportDefinition);
    }

    @Override
    public void clearCache(List<ReportDefinition> reportDefinitions) {
        finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
        finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);

        for (ReportDefinition reportDefinition : reportDefinitions) {
            entityCache.removeResult(ReportDefinitionModelImpl.ENTITY_CACHE_ENABLED, ReportDefinitionImpl.class,
                    reportDefinition.getPrimaryKey());

            clearUniqueFindersCache((ReportDefinitionModelImpl) reportDefinition);
        }
    }

    protected void cacheUniqueFindersCache(ReportDefinitionModelImpl reportDefinitionModelImpl, boolean isNew) {
        if (isNew) {
            Object[] args = new Object[] { reportDefinitionModelImpl.getName() };

            finderCache.putResult(FINDER_PATH_COUNT_BY_NAME, args, Long.valueOf(1));
            finderCache.putResult(FINDER_PATH_FETCH_BY_NAME, args, reportDefinitionModelImpl);

            args = new Object[] { reportDefinitionModelImpl.getUrlPath() };

            finderCache.putResult(FINDER_PATH_COUNT_BY_URLPATH, args, Long.valueOf(1));
            finderCache.putResult(FINDER_PATH_FETCH_BY_URLPATH, args, reportDefinitionModelImpl);
        } else {
            if ((reportDefinitionModelImpl.getColumnBitmask()
                    & FINDER_PATH_FETCH_BY_NAME.getColumnBitmask()) != 0) {
                Object[] args = new Object[] { reportDefinitionModelImpl.getName() };

                finderCache.putResult(FINDER_PATH_COUNT_BY_NAME, args, Long.valueOf(1));
                finderCache.putResult(FINDER_PATH_FETCH_BY_NAME, args, reportDefinitionModelImpl);
            }

            if ((reportDefinitionModelImpl.getColumnBitmask()
                    & FINDER_PATH_FETCH_BY_URLPATH.getColumnBitmask()) != 0) {
                Object[] args = new Object[] { reportDefinitionModelImpl.getUrlPath() };

                finderCache.putResult(FINDER_PATH_COUNT_BY_URLPATH, args, Long.valueOf(1));
                finderCache.putResult(FINDER_PATH_FETCH_BY_URLPATH, args, reportDefinitionModelImpl);
            }
        }
    }

    protected void clearUniqueFindersCache(ReportDefinitionModelImpl reportDefinitionModelImpl) {
        Object[] args = new Object[] { reportDefinitionModelImpl.getName() };

        finderCache.removeResult(FINDER_PATH_COUNT_BY_NAME, args);
        finderCache.removeResult(FINDER_PATH_FETCH_BY_NAME, args);

        if ((reportDefinitionModelImpl.getColumnBitmask() & FINDER_PATH_FETCH_BY_NAME.getColumnBitmask()) != 0) {
            args = new Object[] { reportDefinitionModelImpl.getOriginalName() };

            finderCache.removeResult(FINDER_PATH_COUNT_BY_NAME, args);
            finderCache.removeResult(FINDER_PATH_FETCH_BY_NAME, args);
        }

        args = new Object[] { reportDefinitionModelImpl.getUrlPath() };

        finderCache.removeResult(FINDER_PATH_COUNT_BY_URLPATH, args);
        finderCache.removeResult(FINDER_PATH_FETCH_BY_URLPATH, args);

        if ((reportDefinitionModelImpl.getColumnBitmask() & FINDER_PATH_FETCH_BY_URLPATH.getColumnBitmask()) != 0) {
            args = new Object[] { reportDefinitionModelImpl.getOriginalUrlPath() };

            finderCache.removeResult(FINDER_PATH_COUNT_BY_URLPATH, args);
            finderCache.removeResult(FINDER_PATH_FETCH_BY_URLPATH, args);
        }
    }

    /**
     * Creates a new report definition with the primary key. Does not add the report definition to the database.
     *
     * @param reportDefinitionId the primary key for the new report definition
     * @return the new report definition
     */
    @Override
    public ReportDefinition create(long reportDefinitionId) {
        ReportDefinition reportDefinition = new ReportDefinitionImpl();

        reportDefinition.setNew(true);
        reportDefinition.setPrimaryKey(reportDefinitionId);

        reportDefinition.setCompanyId(companyProvider.getCompanyId());

        return reportDefinition;
    }

    /**
     * Removes the report definition with the primary key from the database. Also notifies the appropriate model listeners.
     *
     * @param reportDefinitionId the primary key of the report definition
     * @return the report definition that was removed
     * @throws NoSuchReportDefinitionException if a report definition with the primary key could not be found
     */
    @Override
    public ReportDefinition remove(long reportDefinitionId) throws NoSuchReportDefinitionException {
        return remove((Serializable) reportDefinitionId);
    }

    /**
     * Removes the report definition with the primary key from the database. Also notifies the appropriate model listeners.
     *
     * @param primaryKey the primary key of the report definition
     * @return the report definition that was removed
     * @throws NoSuchReportDefinitionException if a report definition with the primary key could not be found
     */
    @Override
    public ReportDefinition remove(Serializable primaryKey) throws NoSuchReportDefinitionException {
        Session session = null;

        try {
            session = openSession();

            ReportDefinition reportDefinition = (ReportDefinition) session.get(ReportDefinitionImpl.class,
                    primaryKey);

            if (reportDefinition == null) {
                if (_log.isDebugEnabled()) {
                    _log.debug(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
                }

                throw new NoSuchReportDefinitionException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
            }

            return remove(reportDefinition);
        } catch (NoSuchReportDefinitionException nsee) {
            throw nsee;
        } catch (Exception e) {
            throw processException(e);
        } finally {
            closeSession(session);
        }
    }

    @Override
    protected ReportDefinition removeImpl(ReportDefinition reportDefinition) {
        reportDefinition = toUnwrappedModel(reportDefinition);

        Session session = null;

        try {
            session = openSession();

            if (!session.contains(reportDefinition)) {
                reportDefinition = (ReportDefinition) session.get(ReportDefinitionImpl.class,
                        reportDefinition.getPrimaryKeyObj());
            }

            if (reportDefinition != null) {
                session.delete(reportDefinition);
            }
        } catch (Exception e) {
            throw processException(e);
        } finally {
            closeSession(session);
        }

        if (reportDefinition != null) {
            clearCache(reportDefinition);
        }

        return reportDefinition;
    }

    @Override
    public ReportDefinition updateImpl(ReportDefinition reportDefinition) {
        reportDefinition = toUnwrappedModel(reportDefinition);

        boolean isNew = reportDefinition.isNew();

        ReportDefinitionModelImpl reportDefinitionModelImpl = (ReportDefinitionModelImpl) reportDefinition;

        ServiceContext serviceContext = ServiceContextThreadLocal.getServiceContext();

        Date now = new Date();

        if (isNew && (reportDefinition.getCreateDate() == null)) {
            if (serviceContext == null) {
                reportDefinition.setCreateDate(now);
            } else {
                reportDefinition.setCreateDate(serviceContext.getCreateDate(now));
            }
        }

        if (!reportDefinitionModelImpl.hasSetModifiedDate()) {
            if (serviceContext == null) {
                reportDefinition.setModifiedDate(now);
            } else {
                reportDefinition.setModifiedDate(serviceContext.getModifiedDate(now));
            }
        }

        Session session = null;

        try {
            session = openSession();

            if (reportDefinition.isNew()) {
                session.save(reportDefinition);

                reportDefinition.setNew(false);
            } else {
                reportDefinition = (ReportDefinition) session.merge(reportDefinition);
            }
        } catch (Exception e) {
            throw processException(e);
        } finally {
            closeSession(session);
        }

        finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);

        if (isNew || !ReportDefinitionModelImpl.COLUMN_BITMASK_ENABLED) {
            finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
        }

        else {
            if ((reportDefinitionModelImpl.getColumnBitmask()
                    & FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_G.getColumnBitmask()) != 0) {
                Object[] args = new Object[] { reportDefinitionModelImpl.getOriginalCompanyId(),
                        reportDefinitionModelImpl.getOriginalGroupId() };

                finderCache.removeResult(FINDER_PATH_COUNT_BY_C_G, args);
                finderCache.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_G, args);

                args = new Object[] { reportDefinitionModelImpl.getCompanyId(),
                        reportDefinitionModelImpl.getGroupId() };

                finderCache.removeResult(FINDER_PATH_COUNT_BY_C_G, args);
                finderCache.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_G, args);
            }

            if ((reportDefinitionModelImpl.getColumnBitmask()
                    & FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_C_G.getColumnBitmask()) != 0) {
                Object[] args = new Object[] { reportDefinitionModelImpl.getOriginalCategory(),
                        reportDefinitionModelImpl.getOriginalCompanyId(),
                        reportDefinitionModelImpl.getOriginalGroupId() };

                finderCache.removeResult(FINDER_PATH_COUNT_BY_C_C_G, args);
                finderCache.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_C_G, args);

                args = new Object[] { reportDefinitionModelImpl.getCategory(),
                        reportDefinitionModelImpl.getCompanyId(), reportDefinitionModelImpl.getGroupId() };

                finderCache.removeResult(FINDER_PATH_COUNT_BY_C_C_G, args);
                finderCache.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_C_G, args);
            }
        }

        entityCache.putResult(ReportDefinitionModelImpl.ENTITY_CACHE_ENABLED, ReportDefinitionImpl.class,
                reportDefinition.getPrimaryKey(), reportDefinition, false);

        clearUniqueFindersCache(reportDefinitionModelImpl);
        cacheUniqueFindersCache(reportDefinitionModelImpl, isNew);

        reportDefinition.resetOriginalValues();

        return reportDefinition;
    }

    protected ReportDefinition toUnwrappedModel(ReportDefinition reportDefinition) {
        if (reportDefinition instanceof ReportDefinitionImpl) {
            return reportDefinition;
        }

        ReportDefinitionImpl reportDefinitionImpl = new ReportDefinitionImpl();

        reportDefinitionImpl.setNew(reportDefinition.isNew());
        reportDefinitionImpl.setPrimaryKey(reportDefinition.getPrimaryKey());

        reportDefinitionImpl.setReportDefinitionId(reportDefinition.getReportDefinitionId());
        reportDefinitionImpl.setGroupId(reportDefinition.getGroupId());
        reportDefinitionImpl.setCompanyId(reportDefinition.getCompanyId());
        reportDefinitionImpl.setUserId(reportDefinition.getUserId());
        reportDefinitionImpl.setUserName(reportDefinition.getUserName());
        reportDefinitionImpl.setCreateDate(reportDefinition.getCreateDate());
        reportDefinitionImpl.setModifiedDate(reportDefinition.getModifiedDate());
        reportDefinitionImpl.setName(reportDefinition.getName());
        reportDefinitionImpl.setCategory(reportDefinition.getCategory());
        reportDefinitionImpl.setTitle(reportDefinition.getTitle());
        reportDefinitionImpl.setDescription(reportDefinition.getDescription());
        reportDefinitionImpl.setUrlPath(reportDefinition.getUrlPath());
        reportDefinitionImpl.setAvailableFields(reportDefinition.getAvailableFields());
        reportDefinitionImpl.setDefaultSelectedFields(reportDefinition.getDefaultSelectedFields());
        reportDefinitionImpl.setParameters(reportDefinition.getParameters());
        reportDefinitionImpl.setSortFields(reportDefinition.getSortFields());

        return reportDefinitionImpl;
    }

    /**
     * Returns the report definition with the primary key or throws a {@link com.liferay.portal.kernel.exception.NoSuchModelException} if it could not be found.
     *
     * @param primaryKey the primary key of the report definition
     * @return the report definition
     * @throws NoSuchReportDefinitionException if a report definition with the primary key could not be found
     */
    @Override
    public ReportDefinition findByPrimaryKey(Serializable primaryKey) throws NoSuchReportDefinitionException {
        ReportDefinition reportDefinition = fetchByPrimaryKey(primaryKey);

        if (reportDefinition == null) {
            if (_log.isDebugEnabled()) {
                _log.debug(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
            }

            throw new NoSuchReportDefinitionException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
        }

        return reportDefinition;
    }

    /**
     * Returns the report definition with the primary key or throws a {@link NoSuchReportDefinitionException} if it could not be found.
     *
     * @param reportDefinitionId the primary key of the report definition
     * @return the report definition
     * @throws NoSuchReportDefinitionException if a report definition with the primary key could not be found
     */
    @Override
    public ReportDefinition findByPrimaryKey(long reportDefinitionId) throws NoSuchReportDefinitionException {
        return findByPrimaryKey((Serializable) reportDefinitionId);
    }

    /**
     * Returns the report definition with the primary key or returns <code>null</code> if it could not be found.
     *
     * @param primaryKey the primary key of the report definition
     * @return the report definition, or <code>null</code> if a report definition with the primary key could not be found
     */
    @Override
    public ReportDefinition fetchByPrimaryKey(Serializable primaryKey) {
        Serializable serializable = entityCache.getResult(ReportDefinitionModelImpl.ENTITY_CACHE_ENABLED,
                ReportDefinitionImpl.class, primaryKey);

        if (serializable == nullModel) {
            return null;
        }

        ReportDefinition reportDefinition = (ReportDefinition) serializable;

        if (reportDefinition == null) {
            Session session = null;

            try {
                session = openSession();

                reportDefinition = (ReportDefinition) session.get(ReportDefinitionImpl.class, primaryKey);

                if (reportDefinition != null) {
                    cacheResult(reportDefinition);
                } else {
                    entityCache.putResult(ReportDefinitionModelImpl.ENTITY_CACHE_ENABLED,
                            ReportDefinitionImpl.class, primaryKey, nullModel);
                }
            } catch (Exception e) {
                entityCache.removeResult(ReportDefinitionModelImpl.ENTITY_CACHE_ENABLED, ReportDefinitionImpl.class,
                        primaryKey);

                throw processException(e);
            } finally {
                closeSession(session);
            }
        }

        return reportDefinition;
    }

    /**
     * Returns the report definition with the primary key or returns <code>null</code> if it could not be found.
     *
     * @param reportDefinitionId the primary key of the report definition
     * @return the report definition, or <code>null</code> if a report definition with the primary key could not be found
     */
    @Override
    public ReportDefinition fetchByPrimaryKey(long reportDefinitionId) {
        return fetchByPrimaryKey((Serializable) reportDefinitionId);
    }

    @Override
    public Map<Serializable, ReportDefinition> fetchByPrimaryKeys(Set<Serializable> primaryKeys) {
        if (primaryKeys.isEmpty()) {
            return Collections.emptyMap();
        }

        Map<Serializable, ReportDefinition> map = new HashMap<Serializable, ReportDefinition>();

        if (primaryKeys.size() == 1) {
            Iterator<Serializable> iterator = primaryKeys.iterator();

            Serializable primaryKey = iterator.next();

            ReportDefinition reportDefinition = fetchByPrimaryKey(primaryKey);

            if (reportDefinition != null) {
                map.put(primaryKey, reportDefinition);
            }

            return map;
        }

        Set<Serializable> uncachedPrimaryKeys = null;

        for (Serializable primaryKey : primaryKeys) {
            Serializable serializable = entityCache.getResult(ReportDefinitionModelImpl.ENTITY_CACHE_ENABLED,
                    ReportDefinitionImpl.class, primaryKey);

            if (serializable != nullModel) {
                if (serializable == null) {
                    if (uncachedPrimaryKeys == null) {
                        uncachedPrimaryKeys = new HashSet<Serializable>();
                    }

                    uncachedPrimaryKeys.add(primaryKey);
                } else {
                    map.put(primaryKey, (ReportDefinition) serializable);
                }
            }
        }

        if (uncachedPrimaryKeys == null) {
            return map;
        }

        StringBundler query = new StringBundler((uncachedPrimaryKeys.size() * 2) + 1);

        query.append(_SQL_SELECT_REPORTDEFINITION_WHERE_PKS_IN);

        for (Serializable primaryKey : uncachedPrimaryKeys) {
            query.append(String.valueOf(primaryKey));

            query.append(StringPool.COMMA);
        }

        query.setIndex(query.index() - 1);

        query.append(StringPool.CLOSE_PARENTHESIS);

        String sql = query.toString();

        Session session = null;

        try {
            session = openSession();

            Query q = session.createQuery(sql);

            for (ReportDefinition reportDefinition : (List<ReportDefinition>) q.list()) {
                map.put(reportDefinition.getPrimaryKeyObj(), reportDefinition);

                cacheResult(reportDefinition);

                uncachedPrimaryKeys.remove(reportDefinition.getPrimaryKeyObj());
            }

            for (Serializable primaryKey : uncachedPrimaryKeys) {
                entityCache.putResult(ReportDefinitionModelImpl.ENTITY_CACHE_ENABLED, ReportDefinitionImpl.class,
                        primaryKey, nullModel);
            }
        } catch (Exception e) {
            throw processException(e);
        } finally {
            closeSession(session);
        }

        return map;
    }

    /**
     * Returns all the report definitions.
     *
     * @return the report definitions
     */
    @Override
    public List<ReportDefinition> findAll() {
        return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
    }

    /**
     * Returns a range of all the report definitions.
     *
     * <p>
     * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link ReportDefinitionModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
     * </p>
     *
     * @param start the lower bound of the range of report definitions
     * @param end the upper bound of the range of report definitions (not inclusive)
     * @return the range of report definitions
     */
    @Override
    public List<ReportDefinition> findAll(int start, int end) {
        return findAll(start, end, null);
    }

    /**
     * Returns an ordered range of all the report definitions.
     *
     * <p>
     * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link ReportDefinitionModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
     * </p>
     *
     * @param start the lower bound of the range of report definitions
     * @param end the upper bound of the range of report definitions (not inclusive)
     * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
     * @return the ordered range of report definitions
     */
    @Override
    public List<ReportDefinition> findAll(int start, int end,
            OrderByComparator<ReportDefinition> orderByComparator) {
        return findAll(start, end, orderByComparator, true);
    }

    /**
     * Returns an ordered range of all the report definitions.
     *
     * <p>
     * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link ReportDefinitionModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
     * </p>
     *
     * @param start the lower bound of the range of report definitions
     * @param end the upper bound of the range of report definitions (not inclusive)
     * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
     * @param retrieveFromCache whether to retrieve from the finder cache
     * @return the ordered range of report definitions
     */
    @Override
    public List<ReportDefinition> findAll(int start, int end, OrderByComparator<ReportDefinition> orderByComparator,
            boolean retrieveFromCache) {
        boolean pagination = true;
        FinderPath finderPath = null;
        Object[] finderArgs = null;

        if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) && (orderByComparator == null)) {
            pagination = false;
            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL;
            finderArgs = FINDER_ARGS_EMPTY;
        } else {
            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_ALL;
            finderArgs = new Object[] { start, end, orderByComparator };
        }

        List<ReportDefinition> list = null;

        if (retrieveFromCache) {
            list = (List<ReportDefinition>) finderCache.getResult(finderPath, finderArgs, this);
        }

        if (list == null) {
            StringBundler query = null;
            String sql = null;

            if (orderByComparator != null) {
                query = new StringBundler(2 + (orderByComparator.getOrderByFields().length * 2));

                query.append(_SQL_SELECT_REPORTDEFINITION);

                appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS, orderByComparator);

                sql = query.toString();
            } else {
                sql = _SQL_SELECT_REPORTDEFINITION;

                if (pagination) {
                    sql = sql.concat(ReportDefinitionModelImpl.ORDER_BY_JPQL);
                }
            }

            Session session = null;

            try {
                session = openSession();

                Query q = session.createQuery(sql);

                if (!pagination) {
                    list = (List<ReportDefinition>) QueryUtil.list(q, getDialect(), start, end, false);

                    Collections.sort(list);

                    list = Collections.unmodifiableList(list);
                } else {
                    list = (List<ReportDefinition>) QueryUtil.list(q, getDialect(), start, end);
                }

                cacheResult(list);

                finderCache.putResult(finderPath, finderArgs, list);
            } catch (Exception e) {
                finderCache.removeResult(finderPath, finderArgs);

                throw processException(e);
            } finally {
                closeSession(session);
            }
        }

        return list;
    }

    /**
     * Removes all the report definitions from the database.
     *
     */
    @Override
    public void removeAll() {
        for (ReportDefinition reportDefinition : findAll()) {
            remove(reportDefinition);
        }
    }

    /**
     * Returns the number of report definitions.
     *
     * @return the number of report definitions
     */
    @Override
    public int countAll() {
        Long count = (Long) finderCache.getResult(FINDER_PATH_COUNT_ALL, FINDER_ARGS_EMPTY, this);

        if (count == null) {
            Session session = null;

            try {
                session = openSession();

                Query q = session.createQuery(_SQL_COUNT_REPORTDEFINITION);

                count = (Long) q.uniqueResult();

                finderCache.putResult(FINDER_PATH_COUNT_ALL, FINDER_ARGS_EMPTY, count);
            } catch (Exception e) {
                finderCache.removeResult(FINDER_PATH_COUNT_ALL, FINDER_ARGS_EMPTY);

                throw processException(e);
            } finally {
                closeSession(session);
            }
        }

        return count.intValue();
    }

    @Override
    protected Map<String, Integer> getTableColumnsMap() {
        return ReportDefinitionModelImpl.TABLE_COLUMNS_MAP;
    }

    /**
     * Initializes the report definition persistence.
     */
    public void afterPropertiesSet() {
    }

    public void destroy() {
        entityCache.removeCache(ReportDefinitionImpl.class.getName());
        finderCache.removeCache(FINDER_CLASS_NAME_ENTITY);
        finderCache.removeCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
        finderCache.removeCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
    }

    @ServiceReference(type = CompanyProviderWrapper.class)
    protected CompanyProvider companyProvider;
    @ServiceReference(type = EntityCache.class)
    protected EntityCache entityCache;
    @ServiceReference(type = FinderCache.class)
    protected FinderCache finderCache;
    private static final String _SQL_SELECT_REPORTDEFINITION = "SELECT reportDefinition FROM ReportDefinition reportDefinition";
    private static final String _SQL_SELECT_REPORTDEFINITION_WHERE_PKS_IN = "SELECT reportDefinition FROM ReportDefinition reportDefinition WHERE reportDefinitionId IN (";
    private static final String _SQL_SELECT_REPORTDEFINITION_WHERE = "SELECT reportDefinition FROM ReportDefinition reportDefinition WHERE ";
    private static final String _SQL_COUNT_REPORTDEFINITION = "SELECT COUNT(reportDefinition) FROM ReportDefinition reportDefinition";
    private static final String _SQL_COUNT_REPORTDEFINITION_WHERE = "SELECT COUNT(reportDefinition) FROM ReportDefinition reportDefinition WHERE ";
    private static final String _FILTER_ENTITY_TABLE_FILTER_PK_COLUMN = "reportDefinition.reportDefinitionId";
    private static final String _FILTER_SQL_SELECT_REPORTDEFINITION_WHERE = "SELECT DISTINCT {reportDefinition.*} FROM Bemis_ReportDefinition reportDefinition WHERE ";
    private static final String _FILTER_SQL_SELECT_REPORTDEFINITION_NO_INLINE_DISTINCT_WHERE_1 = "SELECT {Bemis_ReportDefinition.*} FROM (SELECT DISTINCT reportDefinition.reportDefinitionId FROM Bemis_ReportDefinition reportDefinition WHERE ";
    private static final String _FILTER_SQL_SELECT_REPORTDEFINITION_NO_INLINE_DISTINCT_WHERE_2 = ") TEMP_TABLE INNER JOIN Bemis_ReportDefinition ON TEMP_TABLE.reportDefinitionId = Bemis_ReportDefinition.reportDefinitionId";
    private static final String _FILTER_SQL_COUNT_REPORTDEFINITION_WHERE = "SELECT COUNT(DISTINCT reportDefinition.reportDefinitionId) AS COUNT_VALUE FROM Bemis_ReportDefinition reportDefinition WHERE ";
    private static final String _FILTER_ENTITY_ALIAS = "reportDefinition";
    private static final String _FILTER_ENTITY_TABLE = "Bemis_ReportDefinition";
    private static final String _ORDER_BY_ENTITY_ALIAS = "reportDefinition.";
    private static final String _ORDER_BY_ENTITY_TABLE = "Bemis_ReportDefinition.";
    private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No ReportDefinition exists with the primary key ";
    private static final String _NO_SUCH_ENTITY_WITH_KEY = "No ReportDefinition exists with the key {";
    private static final Log _log = LogFactoryUtil.getLog(ReportDefinitionPersistenceImpl.class);
}