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

Java tutorial

Introduction

Here is the source code for com.bemis.portal.report.service.persistence.impl.ReportPreferencesPersistenceImpl.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.NoSuchReportPreferencesException;
import com.bemis.portal.report.model.ReportPreferences;
import com.bemis.portal.report.model.impl.ReportPreferencesImpl;
import com.bemis.portal.report.model.impl.ReportPreferencesModelImpl;
import com.bemis.portal.report.service.persistence.ReportPreferencesPersistence;

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.Session;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
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.OrderByComparator;
import com.liferay.portal.kernel.util.StringBundler;
import com.liferay.portal.kernel.util.StringPool;
import com.liferay.portal.spring.extender.service.ServiceReference;

import java.io.Serializable;

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.Set;

/**
 * The persistence implementation for the report preferences service.
 *
 * <p>
 * Caching information and settings can be found in <code>portal.properties</code>
 * </p>
 *
 * @author Michael C. Han
 * @see ReportPreferencesPersistence
 * @see com.bemis.portal.report.service.persistence.ReportPreferencesUtil
 * @generated
 */
@ProviderType
public class ReportPreferencesPersistenceImpl extends BasePersistenceImpl<ReportPreferences>
        implements ReportPreferencesPersistence {
    /*
     * NOTE FOR DEVELOPERS:
     *
     * Never modify or reference this class directly. Always use {@link ReportPreferencesUtil} to access the report preferences persistence. Modify <code>service.xml</code> and rerun ServiceBuilder to regenerate this class.
     */
    public static final String FINDER_CLASS_NAME_ENTITY = ReportPreferencesImpl.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(
            ReportPreferencesModelImpl.ENTITY_CACHE_ENABLED, ReportPreferencesModelImpl.FINDER_CACHE_ENABLED,
            ReportPreferencesImpl.class, FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findAll", new String[0]);
    public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL = new FinderPath(
            ReportPreferencesModelImpl.ENTITY_CACHE_ENABLED, ReportPreferencesModelImpl.FINDER_CACHE_ENABLED,
            ReportPreferencesImpl.class, FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findAll", new String[0]);
    public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(
            ReportPreferencesModelImpl.ENTITY_CACHE_ENABLED, ReportPreferencesModelImpl.FINDER_CACHE_ENABLED,
            Long.class, FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countAll", new String[0]);
    public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_REPORTDEFINITIONID = new FinderPath(
            ReportPreferencesModelImpl.ENTITY_CACHE_ENABLED, ReportPreferencesModelImpl.FINDER_CACHE_ENABLED,
            ReportPreferencesImpl.class, FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findByReportDefinitionId",
            new String[] { Long.class.getName(),

                    Integer.class.getName(), Integer.class.getName(), OrderByComparator.class.getName() });
    public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_REPORTDEFINITIONID = new FinderPath(
            ReportPreferencesModelImpl.ENTITY_CACHE_ENABLED, ReportPreferencesModelImpl.FINDER_CACHE_ENABLED,
            ReportPreferencesImpl.class, FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findByReportDefinitionId",
            new String[] { Long.class.getName() }, ReportPreferencesModelImpl.REPORTDEFINITIONID_COLUMN_BITMASK);
    public static final FinderPath FINDER_PATH_COUNT_BY_REPORTDEFINITIONID = new FinderPath(
            ReportPreferencesModelImpl.ENTITY_CACHE_ENABLED, ReportPreferencesModelImpl.FINDER_CACHE_ENABLED,
            Long.class, FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByReportDefinitionId",
            new String[] { Long.class.getName() });

    /**
     * Returns all the report preferenceses where reportDefinitionId = &#63;.
     *
     * @param reportDefinitionId the report definition ID
     * @return the matching report preferenceses
     */
    @Override
    public List<ReportPreferences> findByReportDefinitionId(long reportDefinitionId) {
        return findByReportDefinitionId(reportDefinitionId, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
    }

    /**
     * Returns a range of all the report preferenceses where reportDefinitionId = &#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 ReportPreferencesModelImpl}. 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 reportDefinitionId the report definition ID
     * @param start the lower bound of the range of report preferenceses
     * @param end the upper bound of the range of report preferenceses (not inclusive)
     * @return the range of matching report preferenceses
     */
    @Override
    public List<ReportPreferences> findByReportDefinitionId(long reportDefinitionId, int start, int end) {
        return findByReportDefinitionId(reportDefinitionId, start, end, null);
    }

    /**
     * Returns an ordered range of all the report preferenceses where reportDefinitionId = &#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 ReportPreferencesModelImpl}. 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 reportDefinitionId the report definition ID
     * @param start the lower bound of the range of report preferenceses
     * @param end the upper bound of the range of report preferenceses (not inclusive)
     * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
     * @return the ordered range of matching report preferenceses
     */
    @Override
    public List<ReportPreferences> findByReportDefinitionId(long reportDefinitionId, int start, int end,
            OrderByComparator<ReportPreferences> orderByComparator) {
        return findByReportDefinitionId(reportDefinitionId, start, end, orderByComparator, true);
    }

    /**
     * Returns an ordered range of all the report preferenceses where reportDefinitionId = &#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 ReportPreferencesModelImpl}. 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 reportDefinitionId the report definition ID
     * @param start the lower bound of the range of report preferenceses
     * @param end the upper bound of the range of report preferenceses (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 preferenceses
     */
    @Override
    public List<ReportPreferences> findByReportDefinitionId(long reportDefinitionId, int start, int end,
            OrderByComparator<ReportPreferences> 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_REPORTDEFINITIONID;
            finderArgs = new Object[] { reportDefinitionId };
        } else {
            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_REPORTDEFINITIONID;
            finderArgs = new Object[] { reportDefinitionId,

                    start, end, orderByComparator };
        }

        List<ReportPreferences> list = null;

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

            if ((list != null) && !list.isEmpty()) {
                for (ReportPreferences reportPreferences : list) {
                    if ((reportDefinitionId != reportPreferences.getReportDefinitionId())) {
                        list = null;

                        break;
                    }
                }
            }
        }

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

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

            query.append(_SQL_SELECT_REPORTPREFERENCES_WHERE);

            query.append(_FINDER_COLUMN_REPORTDEFINITIONID_REPORTDEFINITIONID_2);

            if (orderByComparator != null) {
                appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS, orderByComparator);
            } else if (pagination) {
                query.append(ReportPreferencesModelImpl.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(reportDefinitionId);

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

                    Collections.sort(list);

                    list = Collections.unmodifiableList(list);
                } else {
                    list = (List<ReportPreferences>) 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 preferences in the ordered set where reportDefinitionId = &#63;.
     *
     * @param reportDefinitionId the report definition ID
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the first matching report preferences
     * @throws NoSuchReportPreferencesException if a matching report preferences could not be found
     */
    @Override
    public ReportPreferences findByReportDefinitionId_First(long reportDefinitionId,
            OrderByComparator<ReportPreferences> orderByComparator) throws NoSuchReportPreferencesException {
        ReportPreferences reportPreferences = fetchByReportDefinitionId_First(reportDefinitionId,
                orderByComparator);

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

        StringBundler msg = new StringBundler(4);

        msg.append(_NO_SUCH_ENTITY_WITH_KEY);

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

        msg.append(StringPool.CLOSE_CURLY_BRACE);

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

    /**
     * Returns the first report preferences in the ordered set where reportDefinitionId = &#63;.
     *
     * @param reportDefinitionId the report definition ID
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the first matching report preferences, or <code>null</code> if a matching report preferences could not be found
     */
    @Override
    public ReportPreferences fetchByReportDefinitionId_First(long reportDefinitionId,
            OrderByComparator<ReportPreferences> orderByComparator) {
        List<ReportPreferences> list = findByReportDefinitionId(reportDefinitionId, 0, 1, orderByComparator);

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

        return null;
    }

    /**
     * Returns the last report preferences in the ordered set where reportDefinitionId = &#63;.
     *
     * @param reportDefinitionId the report definition ID
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the last matching report preferences
     * @throws NoSuchReportPreferencesException if a matching report preferences could not be found
     */
    @Override
    public ReportPreferences findByReportDefinitionId_Last(long reportDefinitionId,
            OrderByComparator<ReportPreferences> orderByComparator) throws NoSuchReportPreferencesException {
        ReportPreferences reportPreferences = fetchByReportDefinitionId_Last(reportDefinitionId, orderByComparator);

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

        StringBundler msg = new StringBundler(4);

        msg.append(_NO_SUCH_ENTITY_WITH_KEY);

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

        msg.append(StringPool.CLOSE_CURLY_BRACE);

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

    /**
     * Returns the last report preferences in the ordered set where reportDefinitionId = &#63;.
     *
     * @param reportDefinitionId the report definition ID
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the last matching report preferences, or <code>null</code> if a matching report preferences could not be found
     */
    @Override
    public ReportPreferences fetchByReportDefinitionId_Last(long reportDefinitionId,
            OrderByComparator<ReportPreferences> orderByComparator) {
        int count = countByReportDefinitionId(reportDefinitionId);

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

        List<ReportPreferences> list = findByReportDefinitionId(reportDefinitionId, count - 1, count,
                orderByComparator);

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

        return null;
    }

    /**
     * Returns the report preferenceses before and after the current report preferences in the ordered set where reportDefinitionId = &#63;.
     *
     * @param reportPreferencesId the primary key of the current report preferences
     * @param reportDefinitionId the report definition ID
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the previous, current, and next report preferences
     * @throws NoSuchReportPreferencesException if a report preferences with the primary key could not be found
     */
    @Override
    public ReportPreferences[] findByReportDefinitionId_PrevAndNext(long reportPreferencesId,
            long reportDefinitionId, OrderByComparator<ReportPreferences> orderByComparator)
            throws NoSuchReportPreferencesException {
        ReportPreferences reportPreferences = findByPrimaryKey(reportPreferencesId);

        Session session = null;

        try {
            session = openSession();

            ReportPreferences[] array = new ReportPreferencesImpl[3];

            array[0] = getByReportDefinitionId_PrevAndNext(session, reportPreferences, reportDefinitionId,
                    orderByComparator, true);

            array[1] = reportPreferences;

            array[2] = getByReportDefinitionId_PrevAndNext(session, reportPreferences, reportDefinitionId,
                    orderByComparator, false);

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

    protected ReportPreferences getByReportDefinitionId_PrevAndNext(Session session,
            ReportPreferences reportPreferences, long reportDefinitionId,
            OrderByComparator<ReportPreferences> orderByComparator, boolean previous) {
        StringBundler query = null;

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

        query.append(_SQL_SELECT_REPORTPREFERENCES_WHERE);

        query.append(_FINDER_COLUMN_REPORTDEFINITIONID_REPORTDEFINITIONID_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(ReportPreferencesModelImpl.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(reportDefinitionId);

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

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

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

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

    /**
     * Removes all the report preferenceses where reportDefinitionId = &#63; from the database.
     *
     * @param reportDefinitionId the report definition ID
     */
    @Override
    public void removeByReportDefinitionId(long reportDefinitionId) {
        for (ReportPreferences reportPreferences : findByReportDefinitionId(reportDefinitionId, QueryUtil.ALL_POS,
                QueryUtil.ALL_POS, null)) {
            remove(reportPreferences);
        }
    }

    /**
     * Returns the number of report preferenceses where reportDefinitionId = &#63;.
     *
     * @param reportDefinitionId the report definition ID
     * @return the number of matching report preferenceses
     */
    @Override
    public int countByReportDefinitionId(long reportDefinitionId) {
        FinderPath finderPath = FINDER_PATH_COUNT_BY_REPORTDEFINITIONID;

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

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

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

            query.append(_SQL_COUNT_REPORTPREFERENCES_WHERE);

            query.append(_FINDER_COLUMN_REPORTDEFINITIONID_REPORTDEFINITIONID_2);

            String sql = query.toString();

            Session session = null;

            try {
                session = openSession();

                Query q = session.createQuery(sql);

                QueryPos qPos = QueryPos.getInstance(q);

                qPos.add(reportDefinitionId);

                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_REPORTDEFINITIONID_REPORTDEFINITIONID_2 = "reportPreferences.reportDefinitionId = ?";
    public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_USERID = new FinderPath(
            ReportPreferencesModelImpl.ENTITY_CACHE_ENABLED, ReportPreferencesModelImpl.FINDER_CACHE_ENABLED,
            ReportPreferencesImpl.class, FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findByUserId",
            new String[] { Long.class.getName(),

                    Integer.class.getName(), Integer.class.getName(), OrderByComparator.class.getName() });
    public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_USERID = new FinderPath(
            ReportPreferencesModelImpl.ENTITY_CACHE_ENABLED, ReportPreferencesModelImpl.FINDER_CACHE_ENABLED,
            ReportPreferencesImpl.class, FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findByUserId",
            new String[] { Long.class.getName() }, ReportPreferencesModelImpl.USERID_COLUMN_BITMASK);
    public static final FinderPath FINDER_PATH_COUNT_BY_USERID = new FinderPath(
            ReportPreferencesModelImpl.ENTITY_CACHE_ENABLED, ReportPreferencesModelImpl.FINDER_CACHE_ENABLED,
            Long.class, FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByUserId",
            new String[] { Long.class.getName() });

    /**
     * Returns all the report preferenceses where userId = &#63;.
     *
     * @param userId the user ID
     * @return the matching report preferenceses
     */
    @Override
    public List<ReportPreferences> findByUserId(long userId) {
        return findByUserId(userId, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
    }

    /**
     * Returns a range of all the report preferenceses where userId = &#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 ReportPreferencesModelImpl}. 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 userId the user ID
     * @param start the lower bound of the range of report preferenceses
     * @param end the upper bound of the range of report preferenceses (not inclusive)
     * @return the range of matching report preferenceses
     */
    @Override
    public List<ReportPreferences> findByUserId(long userId, int start, int end) {
        return findByUserId(userId, start, end, null);
    }

    /**
     * Returns an ordered range of all the report preferenceses where userId = &#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 ReportPreferencesModelImpl}. 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 userId the user ID
     * @param start the lower bound of the range of report preferenceses
     * @param end the upper bound of the range of report preferenceses (not inclusive)
     * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
     * @return the ordered range of matching report preferenceses
     */
    @Override
    public List<ReportPreferences> findByUserId(long userId, int start, int end,
            OrderByComparator<ReportPreferences> orderByComparator) {
        return findByUserId(userId, start, end, orderByComparator, true);
    }

    /**
     * Returns an ordered range of all the report preferenceses where userId = &#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 ReportPreferencesModelImpl}. 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 userId the user ID
     * @param start the lower bound of the range of report preferenceses
     * @param end the upper bound of the range of report preferenceses (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 preferenceses
     */
    @Override
    public List<ReportPreferences> findByUserId(long userId, int start, int end,
            OrderByComparator<ReportPreferences> 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_USERID;
            finderArgs = new Object[] { userId };
        } else {
            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_USERID;
            finderArgs = new Object[] { userId, start, end, orderByComparator };
        }

        List<ReportPreferences> list = null;

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

            if ((list != null) && !list.isEmpty()) {
                for (ReportPreferences reportPreferences : list) {
                    if ((userId != reportPreferences.getUserId())) {
                        list = null;

                        break;
                    }
                }
            }
        }

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

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

            query.append(_SQL_SELECT_REPORTPREFERENCES_WHERE);

            query.append(_FINDER_COLUMN_USERID_USERID_2);

            if (orderByComparator != null) {
                appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS, orderByComparator);
            } else if (pagination) {
                query.append(ReportPreferencesModelImpl.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(userId);

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

                    Collections.sort(list);

                    list = Collections.unmodifiableList(list);
                } else {
                    list = (List<ReportPreferences>) 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 preferences in the ordered set where userId = &#63;.
     *
     * @param userId the user ID
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the first matching report preferences
     * @throws NoSuchReportPreferencesException if a matching report preferences could not be found
     */
    @Override
    public ReportPreferences findByUserId_First(long userId, OrderByComparator<ReportPreferences> orderByComparator)
            throws NoSuchReportPreferencesException {
        ReportPreferences reportPreferences = fetchByUserId_First(userId, orderByComparator);

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

        StringBundler msg = new StringBundler(4);

        msg.append(_NO_SUCH_ENTITY_WITH_KEY);

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

        msg.append(StringPool.CLOSE_CURLY_BRACE);

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

    /**
     * Returns the first report preferences in the ordered set where userId = &#63;.
     *
     * @param userId the user ID
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the first matching report preferences, or <code>null</code> if a matching report preferences could not be found
     */
    @Override
    public ReportPreferences fetchByUserId_First(long userId,
            OrderByComparator<ReportPreferences> orderByComparator) {
        List<ReportPreferences> list = findByUserId(userId, 0, 1, orderByComparator);

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

        return null;
    }

    /**
     * Returns the last report preferences in the ordered set where userId = &#63;.
     *
     * @param userId the user ID
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the last matching report preferences
     * @throws NoSuchReportPreferencesException if a matching report preferences could not be found
     */
    @Override
    public ReportPreferences findByUserId_Last(long userId, OrderByComparator<ReportPreferences> orderByComparator)
            throws NoSuchReportPreferencesException {
        ReportPreferences reportPreferences = fetchByUserId_Last(userId, orderByComparator);

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

        StringBundler msg = new StringBundler(4);

        msg.append(_NO_SUCH_ENTITY_WITH_KEY);

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

        msg.append(StringPool.CLOSE_CURLY_BRACE);

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

    /**
     * Returns the last report preferences in the ordered set where userId = &#63;.
     *
     * @param userId the user ID
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the last matching report preferences, or <code>null</code> if a matching report preferences could not be found
     */
    @Override
    public ReportPreferences fetchByUserId_Last(long userId,
            OrderByComparator<ReportPreferences> orderByComparator) {
        int count = countByUserId(userId);

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

        List<ReportPreferences> list = findByUserId(userId, count - 1, count, orderByComparator);

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

        return null;
    }

    /**
     * Returns the report preferenceses before and after the current report preferences in the ordered set where userId = &#63;.
     *
     * @param reportPreferencesId the primary key of the current report preferences
     * @param userId the user ID
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the previous, current, and next report preferences
     * @throws NoSuchReportPreferencesException if a report preferences with the primary key could not be found
     */
    @Override
    public ReportPreferences[] findByUserId_PrevAndNext(long reportPreferencesId, long userId,
            OrderByComparator<ReportPreferences> orderByComparator) throws NoSuchReportPreferencesException {
        ReportPreferences reportPreferences = findByPrimaryKey(reportPreferencesId);

        Session session = null;

        try {
            session = openSession();

            ReportPreferences[] array = new ReportPreferencesImpl[3];

            array[0] = getByUserId_PrevAndNext(session, reportPreferences, userId, orderByComparator, true);

            array[1] = reportPreferences;

            array[2] = getByUserId_PrevAndNext(session, reportPreferences, userId, orderByComparator, false);

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

    protected ReportPreferences getByUserId_PrevAndNext(Session session, ReportPreferences reportPreferences,
            long userId, OrderByComparator<ReportPreferences> orderByComparator, boolean previous) {
        StringBundler query = null;

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

        query.append(_SQL_SELECT_REPORTPREFERENCES_WHERE);

        query.append(_FINDER_COLUMN_USERID_USERID_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(ReportPreferencesModelImpl.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(userId);

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

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

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

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

    /**
     * Removes all the report preferenceses where userId = &#63; from the database.
     *
     * @param userId the user ID
     */
    @Override
    public void removeByUserId(long userId) {
        for (ReportPreferences reportPreferences : findByUserId(userId, QueryUtil.ALL_POS, QueryUtil.ALL_POS,
                null)) {
            remove(reportPreferences);
        }
    }

    /**
     * Returns the number of report preferenceses where userId = &#63;.
     *
     * @param userId the user ID
     * @return the number of matching report preferenceses
     */
    @Override
    public int countByUserId(long userId) {
        FinderPath finderPath = FINDER_PATH_COUNT_BY_USERID;

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

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

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

            query.append(_SQL_COUNT_REPORTPREFERENCES_WHERE);

            query.append(_FINDER_COLUMN_USERID_USERID_2);

            String sql = query.toString();

            Session session = null;

            try {
                session = openSession();

                Query q = session.createQuery(sql);

                QueryPos qPos = QueryPos.getInstance(q);

                qPos.add(userId);

                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_USERID_USERID_2 = "reportPreferences.userId = ?";
    public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_C_G = new FinderPath(
            ReportPreferencesModelImpl.ENTITY_CACHE_ENABLED, ReportPreferencesModelImpl.FINDER_CACHE_ENABLED,
            ReportPreferencesImpl.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(
            ReportPreferencesModelImpl.ENTITY_CACHE_ENABLED, ReportPreferencesModelImpl.FINDER_CACHE_ENABLED,
            ReportPreferencesImpl.class, FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findByC_G",
            new String[] { Long.class.getName(), Long.class.getName() },
            ReportPreferencesModelImpl.COMPANYID_COLUMN_BITMASK
                    | ReportPreferencesModelImpl.GROUPID_COLUMN_BITMASK);
    public static final FinderPath FINDER_PATH_COUNT_BY_C_G = new FinderPath(
            ReportPreferencesModelImpl.ENTITY_CACHE_ENABLED, ReportPreferencesModelImpl.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 preferenceses where companyId = &#63; and groupId = &#63;.
     *
     * @param companyId the company ID
     * @param groupId the group ID
     * @return the matching report preferenceses
     */
    @Override
    public List<ReportPreferences> 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 preferenceses 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 ReportPreferencesModelImpl}. 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 preferenceses
     * @param end the upper bound of the range of report preferenceses (not inclusive)
     * @return the range of matching report preferenceses
     */
    @Override
    public List<ReportPreferences> 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 preferenceses 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 ReportPreferencesModelImpl}. 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 preferenceses
     * @param end the upper bound of the range of report preferenceses (not inclusive)
     * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
     * @return the ordered range of matching report preferenceses
     */
    @Override
    public List<ReportPreferences> findByC_G(long companyId, long groupId, int start, int end,
            OrderByComparator<ReportPreferences> orderByComparator) {
        return findByC_G(companyId, groupId, start, end, orderByComparator, true);
    }

    /**
     * Returns an ordered range of all the report preferenceses 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 ReportPreferencesModelImpl}. 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 preferenceses
     * @param end the upper bound of the range of report preferenceses (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 preferenceses
     */
    @Override
    public List<ReportPreferences> findByC_G(long companyId, long groupId, int start, int end,
            OrderByComparator<ReportPreferences> 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<ReportPreferences> list = null;

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

            if ((list != null) && !list.isEmpty()) {
                for (ReportPreferences reportPreferences : list) {
                    if ((companyId != reportPreferences.getCompanyId())
                            || (groupId != reportPreferences.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_REPORTPREFERENCES_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(ReportPreferencesModelImpl.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<ReportPreferences>) QueryUtil.list(q, getDialect(), start, end, false);

                    Collections.sort(list);

                    list = Collections.unmodifiableList(list);
                } else {
                    list = (List<ReportPreferences>) 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 preferences 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 preferences
     * @throws NoSuchReportPreferencesException if a matching report preferences could not be found
     */
    @Override
    public ReportPreferences findByC_G_First(long companyId, long groupId,
            OrderByComparator<ReportPreferences> orderByComparator) throws NoSuchReportPreferencesException {
        ReportPreferences reportPreferences = fetchByC_G_First(companyId, groupId, orderByComparator);

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

        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 NoSuchReportPreferencesException(msg.toString());
    }

    /**
     * Returns the first report preferences 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 preferences, or <code>null</code> if a matching report preferences could not be found
     */
    @Override
    public ReportPreferences fetchByC_G_First(long companyId, long groupId,
            OrderByComparator<ReportPreferences> orderByComparator) {
        List<ReportPreferences> list = findByC_G(companyId, groupId, 0, 1, orderByComparator);

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

        return null;
    }

    /**
     * Returns the last report preferences 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 preferences
     * @throws NoSuchReportPreferencesException if a matching report preferences could not be found
     */
    @Override
    public ReportPreferences findByC_G_Last(long companyId, long groupId,
            OrderByComparator<ReportPreferences> orderByComparator) throws NoSuchReportPreferencesException {
        ReportPreferences reportPreferences = fetchByC_G_Last(companyId, groupId, orderByComparator);

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

        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 NoSuchReportPreferencesException(msg.toString());
    }

    /**
     * Returns the last report preferences 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 preferences, or <code>null</code> if a matching report preferences could not be found
     */
    @Override
    public ReportPreferences fetchByC_G_Last(long companyId, long groupId,
            OrderByComparator<ReportPreferences> orderByComparator) {
        int count = countByC_G(companyId, groupId);

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

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

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

        return null;
    }

    /**
     * Returns the report preferenceses before and after the current report preferences in the ordered set where companyId = &#63; and groupId = &#63;.
     *
     * @param reportPreferencesId the primary key of the current report preferences
     * @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 preferences
     * @throws NoSuchReportPreferencesException if a report preferences with the primary key could not be found
     */
    @Override
    public ReportPreferences[] findByC_G_PrevAndNext(long reportPreferencesId, long companyId, long groupId,
            OrderByComparator<ReportPreferences> orderByComparator) throws NoSuchReportPreferencesException {
        ReportPreferences reportPreferences = findByPrimaryKey(reportPreferencesId);

        Session session = null;

        try {
            session = openSession();

            ReportPreferences[] array = new ReportPreferencesImpl[3];

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

            array[1] = reportPreferences;

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

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

    protected ReportPreferences getByC_G_PrevAndNext(Session session, ReportPreferences reportPreferences,
            long companyId, long groupId, OrderByComparator<ReportPreferences> 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_REPORTPREFERENCES_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(ReportPreferencesModelImpl.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(reportPreferences);

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

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

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

    /**
     * Removes all the report preferenceses 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 (ReportPreferences reportPreferences : findByC_G(companyId, groupId, QueryUtil.ALL_POS,
                QueryUtil.ALL_POS, null)) {
            remove(reportPreferences);
        }
    }

    /**
     * Returns the number of report preferenceses where companyId = &#63; and groupId = &#63;.
     *
     * @param companyId the company ID
     * @param groupId the group ID
     * @return the number of matching report preferenceses
     */
    @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_REPORTPREFERENCES_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();
    }

    private static final String _FINDER_COLUMN_C_G_COMPANYID_2 = "reportPreferences.companyId = ? AND ";
    private static final String _FINDER_COLUMN_C_G_GROUPID_2 = "reportPreferences.groupId = ?";
    public static final FinderPath FINDER_PATH_FETCH_BY_RDI_U = new FinderPath(
            ReportPreferencesModelImpl.ENTITY_CACHE_ENABLED, ReportPreferencesModelImpl.FINDER_CACHE_ENABLED,
            ReportPreferencesImpl.class, FINDER_CLASS_NAME_ENTITY, "fetchByRDI_U",
            new String[] { Long.class.getName(), Long.class.getName() },
            ReportPreferencesModelImpl.REPORTDEFINITIONID_COLUMN_BITMASK
                    | ReportPreferencesModelImpl.USERID_COLUMN_BITMASK);
    public static final FinderPath FINDER_PATH_COUNT_BY_RDI_U = new FinderPath(
            ReportPreferencesModelImpl.ENTITY_CACHE_ENABLED, ReportPreferencesModelImpl.FINDER_CACHE_ENABLED,
            Long.class, FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByRDI_U",
            new String[] { Long.class.getName(), Long.class.getName() });

    /**
     * Returns the report preferences where reportDefinitionId = &#63; and userId = &#63; or throws a {@link NoSuchReportPreferencesException} if it could not be found.
     *
     * @param reportDefinitionId the report definition ID
     * @param userId the user ID
     * @return the matching report preferences
     * @throws NoSuchReportPreferencesException if a matching report preferences could not be found
     */
    @Override
    public ReportPreferences findByRDI_U(long reportDefinitionId, long userId)
            throws NoSuchReportPreferencesException {
        ReportPreferences reportPreferences = fetchByRDI_U(reportDefinitionId, userId);

        if (reportPreferences == null) {
            StringBundler msg = new StringBundler(6);

            msg.append(_NO_SUCH_ENTITY_WITH_KEY);

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

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

            msg.append(StringPool.CLOSE_CURLY_BRACE);

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

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

        return reportPreferences;
    }

    /**
     * Returns the report preferences where reportDefinitionId = &#63; and userId = &#63; or returns <code>null</code> if it could not be found. Uses the finder cache.
     *
     * @param reportDefinitionId the report definition ID
     * @param userId the user ID
     * @return the matching report preferences, or <code>null</code> if a matching report preferences could not be found
     */
    @Override
    public ReportPreferences fetchByRDI_U(long reportDefinitionId, long userId) {
        return fetchByRDI_U(reportDefinitionId, userId, true);
    }

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

        Object result = null;

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

        if (result instanceof ReportPreferences) {
            ReportPreferences reportPreferences = (ReportPreferences) result;

            if ((reportDefinitionId != reportPreferences.getReportDefinitionId())
                    || (userId != reportPreferences.getUserId())) {
                result = null;
            }
        }

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

            query.append(_SQL_SELECT_REPORTPREFERENCES_WHERE);

            query.append(_FINDER_COLUMN_RDI_U_REPORTDEFINITIONID_2);

            query.append(_FINDER_COLUMN_RDI_U_USERID_2);

            String sql = query.toString();

            Session session = null;

            try {
                session = openSession();

                Query q = session.createQuery(sql);

                QueryPos qPos = QueryPos.getInstance(q);

                qPos.add(reportDefinitionId);

                qPos.add(userId);

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

                if (list.isEmpty()) {
                    finderCache.putResult(FINDER_PATH_FETCH_BY_RDI_U, finderArgs, list);
                } else {
                    ReportPreferences reportPreferences = list.get(0);

                    result = reportPreferences;

                    cacheResult(reportPreferences);

                    if ((reportPreferences.getReportDefinitionId() != reportDefinitionId)
                            || (reportPreferences.getUserId() != userId)) {
                        finderCache.putResult(FINDER_PATH_FETCH_BY_RDI_U, finderArgs, reportPreferences);
                    }
                }
            } catch (Exception e) {
                finderCache.removeResult(FINDER_PATH_FETCH_BY_RDI_U, finderArgs);

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

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

    /**
     * Removes the report preferences where reportDefinitionId = &#63; and userId = &#63; from the database.
     *
     * @param reportDefinitionId the report definition ID
     * @param userId the user ID
     * @return the report preferences that was removed
     */
    @Override
    public ReportPreferences removeByRDI_U(long reportDefinitionId, long userId)
            throws NoSuchReportPreferencesException {
        ReportPreferences reportPreferences = findByRDI_U(reportDefinitionId, userId);

        return remove(reportPreferences);
    }

    /**
     * Returns the number of report preferenceses where reportDefinitionId = &#63; and userId = &#63;.
     *
     * @param reportDefinitionId the report definition ID
     * @param userId the user ID
     * @return the number of matching report preferenceses
     */
    @Override
    public int countByRDI_U(long reportDefinitionId, long userId) {
        FinderPath finderPath = FINDER_PATH_COUNT_BY_RDI_U;

        Object[] finderArgs = new Object[] { reportDefinitionId, userId };

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

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

            query.append(_SQL_COUNT_REPORTPREFERENCES_WHERE);

            query.append(_FINDER_COLUMN_RDI_U_REPORTDEFINITIONID_2);

            query.append(_FINDER_COLUMN_RDI_U_USERID_2);

            String sql = query.toString();

            Session session = null;

            try {
                session = openSession();

                Query q = session.createQuery(sql);

                QueryPos qPos = QueryPos.getInstance(q);

                qPos.add(reportDefinitionId);

                qPos.add(userId);

                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_RDI_U_REPORTDEFINITIONID_2 = "reportPreferences.reportDefinitionId = ? AND ";
    private static final String _FINDER_COLUMN_RDI_U_USERID_2 = "reportPreferences.userId = ?";

    public ReportPreferencesPersistenceImpl() {
        setModelClass(ReportPreferences.class);
    }

    /**
     * Caches the report preferences in the entity cache if it is enabled.
     *
     * @param reportPreferences the report preferences
     */
    @Override
    public void cacheResult(ReportPreferences reportPreferences) {
        entityCache.putResult(ReportPreferencesModelImpl.ENTITY_CACHE_ENABLED, ReportPreferencesImpl.class,
                reportPreferences.getPrimaryKey(), reportPreferences);

        finderCache.putResult(FINDER_PATH_FETCH_BY_RDI_U,
                new Object[] { reportPreferences.getReportDefinitionId(), reportPreferences.getUserId() },
                reportPreferences);

        reportPreferences.resetOriginalValues();
    }

    /**
     * Caches the report preferenceses in the entity cache if it is enabled.
     *
     * @param reportPreferenceses the report preferenceses
     */
    @Override
    public void cacheResult(List<ReportPreferences> reportPreferenceses) {
        for (ReportPreferences reportPreferences : reportPreferenceses) {
            if (entityCache.getResult(ReportPreferencesModelImpl.ENTITY_CACHE_ENABLED, ReportPreferencesImpl.class,
                    reportPreferences.getPrimaryKey()) == null) {
                cacheResult(reportPreferences);
            } else {
                reportPreferences.resetOriginalValues();
            }
        }
    }

    /**
     * Clears the cache for all report preferenceses.
     *
     * <p>
     * The {@link EntityCache} and {@link FinderCache} are both cleared by this method.
     * </p>
     */
    @Override
    public void clearCache() {
        entityCache.clearCache(ReportPreferencesImpl.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 preferences.
     *
     * <p>
     * The {@link EntityCache} and {@link FinderCache} are both cleared by this method.
     * </p>
     */
    @Override
    public void clearCache(ReportPreferences reportPreferences) {
        entityCache.removeResult(ReportPreferencesModelImpl.ENTITY_CACHE_ENABLED, ReportPreferencesImpl.class,
                reportPreferences.getPrimaryKey());

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

        clearUniqueFindersCache((ReportPreferencesModelImpl) reportPreferences);
    }

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

        for (ReportPreferences reportPreferences : reportPreferenceses) {
            entityCache.removeResult(ReportPreferencesModelImpl.ENTITY_CACHE_ENABLED, ReportPreferencesImpl.class,
                    reportPreferences.getPrimaryKey());

            clearUniqueFindersCache((ReportPreferencesModelImpl) reportPreferences);
        }
    }

    protected void cacheUniqueFindersCache(ReportPreferencesModelImpl reportPreferencesModelImpl, boolean isNew) {
        if (isNew) {
            Object[] args = new Object[] { reportPreferencesModelImpl.getReportDefinitionId(),
                    reportPreferencesModelImpl.getUserId() };

            finderCache.putResult(FINDER_PATH_COUNT_BY_RDI_U, args, Long.valueOf(1));
            finderCache.putResult(FINDER_PATH_FETCH_BY_RDI_U, args, reportPreferencesModelImpl);
        } else {
            if ((reportPreferencesModelImpl.getColumnBitmask()
                    & FINDER_PATH_FETCH_BY_RDI_U.getColumnBitmask()) != 0) {
                Object[] args = new Object[] { reportPreferencesModelImpl.getReportDefinitionId(),
                        reportPreferencesModelImpl.getUserId() };

                finderCache.putResult(FINDER_PATH_COUNT_BY_RDI_U, args, Long.valueOf(1));
                finderCache.putResult(FINDER_PATH_FETCH_BY_RDI_U, args, reportPreferencesModelImpl);
            }
        }
    }

    protected void clearUniqueFindersCache(ReportPreferencesModelImpl reportPreferencesModelImpl) {
        Object[] args = new Object[] { reportPreferencesModelImpl.getReportDefinitionId(),
                reportPreferencesModelImpl.getUserId() };

        finderCache.removeResult(FINDER_PATH_COUNT_BY_RDI_U, args);
        finderCache.removeResult(FINDER_PATH_FETCH_BY_RDI_U, args);

        if ((reportPreferencesModelImpl.getColumnBitmask() & FINDER_PATH_FETCH_BY_RDI_U.getColumnBitmask()) != 0) {
            args = new Object[] { reportPreferencesModelImpl.getOriginalReportDefinitionId(),
                    reportPreferencesModelImpl.getOriginalUserId() };

            finderCache.removeResult(FINDER_PATH_COUNT_BY_RDI_U, args);
            finderCache.removeResult(FINDER_PATH_FETCH_BY_RDI_U, args);
        }
    }

    /**
     * Creates a new report preferences with the primary key. Does not add the report preferences to the database.
     *
     * @param reportPreferencesId the primary key for the new report preferences
     * @return the new report preferences
     */
    @Override
    public ReportPreferences create(long reportPreferencesId) {
        ReportPreferences reportPreferences = new ReportPreferencesImpl();

        reportPreferences.setNew(true);
        reportPreferences.setPrimaryKey(reportPreferencesId);

        reportPreferences.setCompanyId(companyProvider.getCompanyId());

        return reportPreferences;
    }

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

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

        try {
            session = openSession();

            ReportPreferences reportPreferences = (ReportPreferences) session.get(ReportPreferencesImpl.class,
                    primaryKey);

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

                throw new NoSuchReportPreferencesException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
            }

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

    @Override
    protected ReportPreferences removeImpl(ReportPreferences reportPreferences) {
        reportPreferences = toUnwrappedModel(reportPreferences);

        Session session = null;

        try {
            session = openSession();

            if (!session.contains(reportPreferences)) {
                reportPreferences = (ReportPreferences) session.get(ReportPreferencesImpl.class,
                        reportPreferences.getPrimaryKeyObj());
            }

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

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

        return reportPreferences;
    }

    @Override
    public ReportPreferences updateImpl(ReportPreferences reportPreferences) {
        reportPreferences = toUnwrappedModel(reportPreferences);

        boolean isNew = reportPreferences.isNew();

        ReportPreferencesModelImpl reportPreferencesModelImpl = (ReportPreferencesModelImpl) reportPreferences;

        ServiceContext serviceContext = ServiceContextThreadLocal.getServiceContext();

        Date now = new Date();

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

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

        Session session = null;

        try {
            session = openSession();

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

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

        finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);

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

        else {
            if ((reportPreferencesModelImpl.getColumnBitmask()
                    & FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_REPORTDEFINITIONID.getColumnBitmask()) != 0) {
                Object[] args = new Object[] { reportPreferencesModelImpl.getOriginalReportDefinitionId() };

                finderCache.removeResult(FINDER_PATH_COUNT_BY_REPORTDEFINITIONID, args);
                finderCache.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_REPORTDEFINITIONID, args);

                args = new Object[] { reportPreferencesModelImpl.getReportDefinitionId() };

                finderCache.removeResult(FINDER_PATH_COUNT_BY_REPORTDEFINITIONID, args);
                finderCache.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_REPORTDEFINITIONID, args);
            }

            if ((reportPreferencesModelImpl.getColumnBitmask()
                    & FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_USERID.getColumnBitmask()) != 0) {
                Object[] args = new Object[] { reportPreferencesModelImpl.getOriginalUserId() };

                finderCache.removeResult(FINDER_PATH_COUNT_BY_USERID, args);
                finderCache.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_USERID, args);

                args = new Object[] { reportPreferencesModelImpl.getUserId() };

                finderCache.removeResult(FINDER_PATH_COUNT_BY_USERID, args);
                finderCache.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_USERID, args);
            }

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

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

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

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

        entityCache.putResult(ReportPreferencesModelImpl.ENTITY_CACHE_ENABLED, ReportPreferencesImpl.class,
                reportPreferences.getPrimaryKey(), reportPreferences, false);

        clearUniqueFindersCache(reportPreferencesModelImpl);
        cacheUniqueFindersCache(reportPreferencesModelImpl, isNew);

        reportPreferences.resetOriginalValues();

        return reportPreferences;
    }

    protected ReportPreferences toUnwrappedModel(ReportPreferences reportPreferences) {
        if (reportPreferences instanceof ReportPreferencesImpl) {
            return reportPreferences;
        }

        ReportPreferencesImpl reportPreferencesImpl = new ReportPreferencesImpl();

        reportPreferencesImpl.setNew(reportPreferences.isNew());
        reportPreferencesImpl.setPrimaryKey(reportPreferences.getPrimaryKey());

        reportPreferencesImpl.setReportPreferencesId(reportPreferences.getReportPreferencesId());
        reportPreferencesImpl.setGroupId(reportPreferences.getGroupId());
        reportPreferencesImpl.setCompanyId(reportPreferences.getCompanyId());
        reportPreferencesImpl.setUserId(reportPreferences.getUserId());
        reportPreferencesImpl.setUserName(reportPreferences.getUserName());
        reportPreferencesImpl.setCreateDate(reportPreferences.getCreateDate());
        reportPreferencesImpl.setModifiedDate(reportPreferences.getModifiedDate());
        reportPreferencesImpl.setReportDefinitionId(reportPreferences.getReportDefinitionId());
        reportPreferencesImpl.setParametersValues(reportPreferences.getParametersValues());
        reportPreferencesImpl.setSelectedFields(reportPreferences.getSelectedFields());
        reportPreferencesImpl.setSortFields(reportPreferences.getSortFields());

        return reportPreferencesImpl;
    }

    /**
     * Returns the report preferences 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 preferences
     * @return the report preferences
     * @throws NoSuchReportPreferencesException if a report preferences with the primary key could not be found
     */
    @Override
    public ReportPreferences findByPrimaryKey(Serializable primaryKey) throws NoSuchReportPreferencesException {
        ReportPreferences reportPreferences = fetchByPrimaryKey(primaryKey);

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

            throw new NoSuchReportPreferencesException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
        }

        return reportPreferences;
    }

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

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

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

        ReportPreferences reportPreferences = (ReportPreferences) serializable;

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

            try {
                session = openSession();

                reportPreferences = (ReportPreferences) session.get(ReportPreferencesImpl.class, primaryKey);

                if (reportPreferences != null) {
                    cacheResult(reportPreferences);
                } else {
                    entityCache.putResult(ReportPreferencesModelImpl.ENTITY_CACHE_ENABLED,
                            ReportPreferencesImpl.class, primaryKey, nullModel);
                }
            } catch (Exception e) {
                entityCache.removeResult(ReportPreferencesModelImpl.ENTITY_CACHE_ENABLED,
                        ReportPreferencesImpl.class, primaryKey);

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

        return reportPreferences;
    }

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

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

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

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

            Serializable primaryKey = iterator.next();

            ReportPreferences reportPreferences = fetchByPrimaryKey(primaryKey);

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

            return map;
        }

        Set<Serializable> uncachedPrimaryKeys = null;

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

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

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

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

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

        query.append(_SQL_SELECT_REPORTPREFERENCES_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 (ReportPreferences reportPreferences : (List<ReportPreferences>) q.list()) {
                map.put(reportPreferences.getPrimaryKeyObj(), reportPreferences);

                cacheResult(reportPreferences);

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

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

        return map;
    }

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

    /**
     * Returns a range of all the report preferenceses.
     *
     * <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 ReportPreferencesModelImpl}. 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 preferenceses
     * @param end the upper bound of the range of report preferenceses (not inclusive)
     * @return the range of report preferenceses
     */
    @Override
    public List<ReportPreferences> findAll(int start, int end) {
        return findAll(start, end, null);
    }

    /**
     * Returns an ordered range of all the report preferenceses.
     *
     * <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 ReportPreferencesModelImpl}. 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 preferenceses
     * @param end the upper bound of the range of report preferenceses (not inclusive)
     * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
     * @return the ordered range of report preferenceses
     */
    @Override
    public List<ReportPreferences> findAll(int start, int end,
            OrderByComparator<ReportPreferences> orderByComparator) {
        return findAll(start, end, orderByComparator, true);
    }

    /**
     * Returns an ordered range of all the report preferenceses.
     *
     * <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 ReportPreferencesModelImpl}. 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 preferenceses
     * @param end the upper bound of the range of report preferenceses (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 preferenceses
     */
    @Override
    public List<ReportPreferences> findAll(int start, int end,
            OrderByComparator<ReportPreferences> 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<ReportPreferences> list = null;

        if (retrieveFromCache) {
            list = (List<ReportPreferences>) 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_REPORTPREFERENCES);

                appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS, orderByComparator);

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

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

            Session session = null;

            try {
                session = openSession();

                Query q = session.createQuery(sql);

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

                    Collections.sort(list);

                    list = Collections.unmodifiableList(list);
                } else {
                    list = (List<ReportPreferences>) 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 preferenceses from the database.
     *
     */
    @Override
    public void removeAll() {
        for (ReportPreferences reportPreferences : findAll()) {
            remove(reportPreferences);
        }
    }

    /**
     * Returns the number of report preferenceses.
     *
     * @return the number of report preferenceses
     */
    @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_REPORTPREFERENCES);

                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 ReportPreferencesModelImpl.TABLE_COLUMNS_MAP;
    }

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

    public void destroy() {
        entityCache.removeCache(ReportPreferencesImpl.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_REPORTPREFERENCES = "SELECT reportPreferences FROM ReportPreferences reportPreferences";
    private static final String _SQL_SELECT_REPORTPREFERENCES_WHERE_PKS_IN = "SELECT reportPreferences FROM ReportPreferences reportPreferences WHERE reportPreferencesId IN (";
    private static final String _SQL_SELECT_REPORTPREFERENCES_WHERE = "SELECT reportPreferences FROM ReportPreferences reportPreferences WHERE ";
    private static final String _SQL_COUNT_REPORTPREFERENCES = "SELECT COUNT(reportPreferences) FROM ReportPreferences reportPreferences";
    private static final String _SQL_COUNT_REPORTPREFERENCES_WHERE = "SELECT COUNT(reportPreferences) FROM ReportPreferences reportPreferences WHERE ";
    private static final String _ORDER_BY_ENTITY_ALIAS = "reportPreferences.";
    private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No ReportPreferences exists with the primary key ";
    private static final String _NO_SUCH_ENTITY_WITH_KEY = "No ReportPreferences exists with the key {";
    private static final Log _log = LogFactoryUtil.getLog(ReportPreferencesPersistenceImpl.class);
}