package br.mp.mprj.portal.portlet.gestaomensagens.service.service.persistence;

import br.mp.mprj.portal.portlet.gestaomensagens.service.NoSuchGrupoDestinatarioException;
import br.mp.mprj.portal.portlet.gestaomensagens.service.model.GrupoDestinatario;
import br.mp.mprj.portal.portlet.gestaomensagens.service.model.impl.GrupoDestinatarioImpl;
import br.mp.mprj.portal.portlet.gestaomensagens.service.model.impl.GrupoDestinatarioModelImpl;
import br.mp.mprj.portal.portlet.gestaomensagens.service.service.persistence.GrupoDestinatarioPersistence;

import com.liferay.portal.kernel.cache.CacheRegistryUtil;
import com.liferay.portal.kernel.dao.orm.EntityCacheUtil;
import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
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.exception.SystemException;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.util.ArrayUtil;
import com.liferay.portal.kernel.util.GetterUtil;
import com.liferay.portal.kernel.util.InstanceFactory;
import com.liferay.portal.kernel.util.OrderByComparator;
import com.liferay.portal.kernel.util.PropsKeys;
import com.liferay.portal.kernel.util.PropsUtil;
import com.liferay.portal.kernel.util.SetUtil;
import com.liferay.portal.kernel.util.StringBundler;
import com.liferay.portal.kernel.util.StringPool;
import com.liferay.portal.kernel.util.StringUtil;
import com.liferay.portal.kernel.util.UnmodifiableList;
import com.liferay.portal.kernel.util.Validator;
import com.liferay.portal.kernel.uuid.PortalUUIDUtil;
import com.liferay.portal.model.CacheModel;
import com.liferay.portal.model.ModelListener;
import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;

import java.io.Serializable;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;

/**
 * The persistence implementation for the grupo destinatario service.
 *
 * <p>
 * Caching information and settings can be found in <code>portal.properties</code>
 * </p>
 *
 * @author Henrique Simoes
 * @see GrupoDestinatarioPersistence
 * @see GrupoDestinatarioUtil
 * @generated
 */
public class GrupoDestinatarioPersistenceImpl extends BasePersistenceImpl<GrupoDestinatario>
    implements GrupoDestinatarioPersistence {
    /*
     * NOTE FOR DEVELOPERS:
     *
     * Never modify or reference this class directly. Always use {@link GrupoDestinatarioUtil} to access the grupo destinatario persistence. Modify <code>service.xml</code> and rerun ServiceBuilder to regenerate this class.
     */
    public static final String FINDER_CLASS_NAME_ENTITY = GrupoDestinatarioImpl.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(GrupoDestinatarioModelImpl.ENTITY_CACHE_ENABLED,
            GrupoDestinatarioModelImpl.FINDER_CACHE_ENABLED,
            GrupoDestinatarioImpl.class,
            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findAll", new String[0]);
    public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL = new FinderPath(GrupoDestinatarioModelImpl.ENTITY_CACHE_ENABLED,
            GrupoDestinatarioModelImpl.FINDER_CACHE_ENABLED,
            GrupoDestinatarioImpl.class,
            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findAll", new String[0]);
    public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(GrupoDestinatarioModelImpl.ENTITY_CACHE_ENABLED,
            GrupoDestinatarioModelImpl.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_UUID = new FinderPath(GrupoDestinatarioModelImpl.ENTITY_CACHE_ENABLED,
            GrupoDestinatarioModelImpl.FINDER_CACHE_ENABLED,
            GrupoDestinatarioImpl.class,
            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findByUuid",
            new String[] {
                String.class.getName(),
                
            Integer.class.getName(), Integer.class.getName(),
                OrderByComparator.class.getName()
            });
    public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_UUID = new FinderPath(GrupoDestinatarioModelImpl.ENTITY_CACHE_ENABLED,
            GrupoDestinatarioModelImpl.FINDER_CACHE_ENABLED,
            GrupoDestinatarioImpl.class,
            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findByUuid",
            new String[] { String.class.getName() },
            GrupoDestinatarioModelImpl.UUID_COLUMN_BITMASK);
    public static final FinderPath FINDER_PATH_COUNT_BY_UUID = new FinderPath(GrupoDestinatarioModelImpl.ENTITY_CACHE_ENABLED,
            GrupoDestinatarioModelImpl.FINDER_CACHE_ENABLED, Long.class,
            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByUuid",
            new String[] { String.class.getName() });
    private static final String _FINDER_COLUMN_UUID_UUID_1 = "grupoDestinatario.uuid IS NULL";
    private static final String _FINDER_COLUMN_UUID_UUID_2 = "grupoDestinatario.uuid = ?";
    private static final String _FINDER_COLUMN_UUID_UUID_3 = "(grupoDestinatario.uuid IS NULL OR grupoDestinatario.uuid = '')";
    public static final FinderPath FINDER_PATH_FETCH_BY_UUID_G = new FinderPath(GrupoDestinatarioModelImpl.ENTITY_CACHE_ENABLED,
            GrupoDestinatarioModelImpl.FINDER_CACHE_ENABLED,
            GrupoDestinatarioImpl.class, FINDER_CLASS_NAME_ENTITY,
            "fetchByUUID_G",
            new String[] { String.class.getName(), Long.class.getName() },
            GrupoDestinatarioModelImpl.UUID_COLUMN_BITMASK |
            GrupoDestinatarioModelImpl.GROUPID_COLUMN_BITMASK);
    public static final FinderPath FINDER_PATH_COUNT_BY_UUID_G = new FinderPath(GrupoDestinatarioModelImpl.ENTITY_CACHE_ENABLED,
            GrupoDestinatarioModelImpl.FINDER_CACHE_ENABLED, Long.class,
            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByUUID_G",
            new String[] { String.class.getName(), Long.class.getName() });
    private static final String _FINDER_COLUMN_UUID_G_UUID_1 = "grupoDestinatario.uuid IS NULL AND ";
    private static final String _FINDER_COLUMN_UUID_G_UUID_2 = "grupoDestinatario.uuid = ? AND ";
    private static final String _FINDER_COLUMN_UUID_G_UUID_3 = "(grupoDestinatario.uuid IS NULL OR grupoDestinatario.uuid = '') AND ";
    private static final String _FINDER_COLUMN_UUID_G_GROUPID_2 = "grupoDestinatario.groupId = ?";
    public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_UUID_C = new FinderPath(GrupoDestinatarioModelImpl.ENTITY_CACHE_ENABLED,
            GrupoDestinatarioModelImpl.FINDER_CACHE_ENABLED,
            GrupoDestinatarioImpl.class,
            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findByUuid_C",
            new String[] {
                String.class.getName(), Long.class.getName(),
                
            Integer.class.getName(), Integer.class.getName(),
                OrderByComparator.class.getName()
            });
    public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_UUID_C =
        new FinderPath(GrupoDestinatarioModelImpl.ENTITY_CACHE_ENABLED,
            GrupoDestinatarioModelImpl.FINDER_CACHE_ENABLED,
            GrupoDestinatarioImpl.class,
            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findByUuid_C",
            new String[] { String.class.getName(), Long.class.getName() },
            GrupoDestinatarioModelImpl.UUID_COLUMN_BITMASK |
            GrupoDestinatarioModelImpl.COMPANYID_COLUMN_BITMASK);
    public static final FinderPath FINDER_PATH_COUNT_BY_UUID_C = new FinderPath(GrupoDestinatarioModelImpl.ENTITY_CACHE_ENABLED,
            GrupoDestinatarioModelImpl.FINDER_CACHE_ENABLED, Long.class,
            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByUuid_C",
            new String[] { String.class.getName(), Long.class.getName() });
    private static final String _FINDER_COLUMN_UUID_C_UUID_1 = "grupoDestinatario.uuid IS NULL AND ";
    private static final String _FINDER_COLUMN_UUID_C_UUID_2 = "grupoDestinatario.uuid = ? AND ";
    private static final String _FINDER_COLUMN_UUID_C_UUID_3 = "(grupoDestinatario.uuid IS NULL OR grupoDestinatario.uuid = '') AND ";
    private static final String _FINDER_COLUMN_UUID_C_COMPANYID_2 = "grupoDestinatario.companyId = ?";
    public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_TIPOLISTAATIVO =
        new FinderPath(GrupoDestinatarioModelImpl.ENTITY_CACHE_ENABLED,
            GrupoDestinatarioModelImpl.FINDER_CACHE_ENABLED,
            GrupoDestinatarioImpl.class,
            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findByTipoListaAtivo",
            new String[] {
                Integer.class.getName(), Boolean.class.getName(),
                
            Integer.class.getName(), Integer.class.getName(),
                OrderByComparator.class.getName()
            });
    public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_TIPOLISTAATIVO =
        new FinderPath(GrupoDestinatarioModelImpl.ENTITY_CACHE_ENABLED,
            GrupoDestinatarioModelImpl.FINDER_CACHE_ENABLED,
            GrupoDestinatarioImpl.class,
            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findByTipoListaAtivo",
            new String[] { Integer.class.getName(), Boolean.class.getName() },
            GrupoDestinatarioModelImpl.TIPOLISTA_COLUMN_BITMASK |
            GrupoDestinatarioModelImpl.ATIVO_COLUMN_BITMASK);
    public static final FinderPath FINDER_PATH_COUNT_BY_TIPOLISTAATIVO = new FinderPath(GrupoDestinatarioModelImpl.ENTITY_CACHE_ENABLED,
            GrupoDestinatarioModelImpl.FINDER_CACHE_ENABLED, Long.class,
            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByTipoListaAtivo",
            new String[] { Integer.class.getName(), Boolean.class.getName() });
    private static final String _FINDER_COLUMN_TIPOLISTAATIVO_TIPOLISTA_2 = "grupoDestinatario.tipoLista = ? AND ";
    private static final String _FINDER_COLUMN_TIPOLISTAATIVO_ATIVO_2 = "grupoDestinatario.ativo = ?";
    public static final FinderPath FINDER_PATH_FETCH_BY_NOMETIPOLISTAATIVO = new FinderPath(GrupoDestinatarioModelImpl.ENTITY_CACHE_ENABLED,
            GrupoDestinatarioModelImpl.FINDER_CACHE_ENABLED,
            GrupoDestinatarioImpl.class, FINDER_CLASS_NAME_ENTITY,
            "fetchByNomeTipoListaAtivo",
            new String[] {
                String.class.getName(), Integer.class.getName(),
                Boolean.class.getName()
            },
            GrupoDestinatarioModelImpl.NOME_COLUMN_BITMASK |
            GrupoDestinatarioModelImpl.TIPOLISTA_COLUMN_BITMASK |
            GrupoDestinatarioModelImpl.ATIVO_COLUMN_BITMASK);
    public static final FinderPath FINDER_PATH_COUNT_BY_NOMETIPOLISTAATIVO = new FinderPath(GrupoDestinatarioModelImpl.ENTITY_CACHE_ENABLED,
            GrupoDestinatarioModelImpl.FINDER_CACHE_ENABLED, Long.class,
            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION,
            "countByNomeTipoListaAtivo",
            new String[] {
                String.class.getName(), Integer.class.getName(),
                Boolean.class.getName()
            });
    private static final String _FINDER_COLUMN_NOMETIPOLISTAATIVO_NOME_1 = "grupoDestinatario.nome IS NULL AND ";
    private static final String _FINDER_COLUMN_NOMETIPOLISTAATIVO_NOME_2 = "grupoDestinatario.nome = ? AND ";
    private static final String _FINDER_COLUMN_NOMETIPOLISTAATIVO_NOME_3 = "(grupoDestinatario.nome IS NULL OR grupoDestinatario.nome = '') AND ";
    private static final String _FINDER_COLUMN_NOMETIPOLISTAATIVO_TIPOLISTA_2 = "grupoDestinatario.tipoLista = ? AND ";
    private static final String _FINDER_COLUMN_NOMETIPOLISTAATIVO_ATIVO_2 = "grupoDestinatario.ativo = ?";
    public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_GRUPODESTINATARIOIDARRAY =
        new FinderPath(GrupoDestinatarioModelImpl.ENTITY_CACHE_ENABLED,
            GrupoDestinatarioModelImpl.FINDER_CACHE_ENABLED,
            GrupoDestinatarioImpl.class,
            FINDER_CLASS_NAME_LIST_WITH_PAGINATION,
            "findByGrupoDestinatarioIdArray",
            new String[] {
                Long.class.getName(),
                
            Integer.class.getName(), Integer.class.getName(),
                OrderByComparator.class.getName()
            });
    public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_GRUPODESTINATARIOIDARRAY =
        new FinderPath(GrupoDestinatarioModelImpl.ENTITY_CACHE_ENABLED,
            GrupoDestinatarioModelImpl.FINDER_CACHE_ENABLED,
            GrupoDestinatarioImpl.class,
            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION,
            "findByGrupoDestinatarioIdArray",
            new String[] { Long.class.getName() },
            GrupoDestinatarioModelImpl.GRUPODESTINATARIOID_COLUMN_BITMASK);
    public static final FinderPath FINDER_PATH_COUNT_BY_GRUPODESTINATARIOIDARRAY =
        new FinderPath(GrupoDestinatarioModelImpl.ENTITY_CACHE_ENABLED,
            GrupoDestinatarioModelImpl.FINDER_CACHE_ENABLED, Long.class,
            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION,
            "countByGrupoDestinatarioIdArray",
            new String[] { Long.class.getName() });
    public static final FinderPath FINDER_PATH_WITH_PAGINATION_COUNT_BY_GRUPODESTINATARIOIDARRAY =
        new FinderPath(GrupoDestinatarioModelImpl.ENTITY_CACHE_ENABLED,
            GrupoDestinatarioModelImpl.FINDER_CACHE_ENABLED, Long.class,
            FINDER_CLASS_NAME_LIST_WITH_PAGINATION,
            "countByGrupoDestinatarioIdArray",
            new String[] { Long.class.getName() });
    private static final String _FINDER_COLUMN_GRUPODESTINATARIOIDARRAY_GRUPODESTINATARIOID_2 =
        "grupoDestinatario.grupoDestinatarioId = ?";
    private static final String _FINDER_COLUMN_GRUPODESTINATARIOIDARRAY_GRUPODESTINATARIOID_5 =
        "(" +
        removeConjunction(_FINDER_COLUMN_GRUPODESTINATARIOIDARRAY_GRUPODESTINATARIOID_2) +
        ")";
    private static final String _SQL_SELECT_GRUPODESTINATARIO = "SELECT grupoDestinatario FROM GrupoDestinatario grupoDestinatario";
    private static final String _SQL_SELECT_GRUPODESTINATARIO_WHERE = "SELECT grupoDestinatario FROM GrupoDestinatario grupoDestinatario WHERE ";
    private static final String _SQL_COUNT_GRUPODESTINATARIO = "SELECT COUNT(grupoDestinatario) FROM GrupoDestinatario grupoDestinatario";
    private static final String _SQL_COUNT_GRUPODESTINATARIO_WHERE = "SELECT COUNT(grupoDestinatario) FROM GrupoDestinatario grupoDestinatario WHERE ";
    private static final String _ORDER_BY_ENTITY_ALIAS = "grupoDestinatario.";
    private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No GrupoDestinatario exists with the primary key ";
    private static final String _NO_SUCH_ENTITY_WITH_KEY = "No GrupoDestinatario exists with the key {";
    private static final boolean _HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE = GetterUtil.getBoolean(PropsUtil.get(
                PropsKeys.HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE));
    private static Log _log = LogFactoryUtil.getLog(GrupoDestinatarioPersistenceImpl.class);
    private static Set<String> _badColumnNames = SetUtil.fromArray(new String[] {
                "uuid"
            });
    private static GrupoDestinatario _nullGrupoDestinatario = new GrupoDestinatarioImpl() {
            @Override
            public Object clone() {
                return this;
            }

            @Override
            public CacheModel<GrupoDestinatario> toCacheModel() {
                return _nullGrupoDestinatarioCacheModel;
            }
        };

    private static CacheModel<GrupoDestinatario> _nullGrupoDestinatarioCacheModel =
        new CacheModel<GrupoDestinatario>() {
            @Override
            public GrupoDestinatario toEntityModel() {
                return _nullGrupoDestinatario;
            }
        };

    public GrupoDestinatarioPersistenceImpl() {
        setModelClass(GrupoDestinatario.class);
    }

    /**
     * Returns all the grupo destinatarios where uuid = &#63;.
     *
     * @param uuid the uuid
     * @return the matching grupo destinatarios
     * @throws SystemException if a system exception occurred
     */
    @Override
    public List<GrupoDestinatario> findByUuid(String uuid)
        throws SystemException {
        return findByUuid(uuid, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
    }

    /**
     * Returns a range of all the grupo destinatarios where uuid = &#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 com.liferay.portal.kernel.dao.orm.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 com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link br.mp.mprj.portal.portlet.gestaomensagens.service.model.impl.GrupoDestinatarioModelImpl}. 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 uuid the uuid
     * @param start the lower bound of the range of grupo destinatarios
     * @param end the upper bound of the range of grupo destinatarios (not inclusive)
     * @return the range of matching grupo destinatarios
     * @throws SystemException if a system exception occurred
     */
    @Override
    public List<GrupoDestinatario> findByUuid(String uuid, int start, int end)
        throws SystemException {
        return findByUuid(uuid, start, end, null);
    }

    /**
     * Returns an ordered range of all the grupo destinatarios where uuid = &#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 com.liferay.portal.kernel.dao.orm.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 com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link br.mp.mprj.portal.portlet.gestaomensagens.service.model.impl.GrupoDestinatarioModelImpl}. 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 uuid the uuid
     * @param start the lower bound of the range of grupo destinatarios
     * @param end the upper bound of the range of grupo destinatarios (not inclusive)
     * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
     * @return the ordered range of matching grupo destinatarios
     * @throws SystemException if a system exception occurred
     */
    @Override
    public List<GrupoDestinatario> findByUuid(String uuid, int start, int end,
        OrderByComparator orderByComparator) throws SystemException {
        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_UUID;
            finderArgs = new Object[] { uuid };
        } else {
            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_UUID;
            finderArgs = new Object[] { uuid, start, end, orderByComparator };
        }

        List<GrupoDestinatario> list = (List<GrupoDestinatario>) FinderCacheUtil.getResult(finderPath,
                finderArgs, this);

        if ((list != null) && !list.isEmpty()) {
            for (GrupoDestinatario grupoDestinatario : list) {
                if (!Validator.equals(uuid, grupoDestinatario.getUuid())) {
                    list = null;

                    break;
                }
            }
        }

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

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

            query.append(_SQL_SELECT_GRUPODESTINATARIO_WHERE);

            boolean bindUuid = false;

            if (uuid == null) {
                query.append(_FINDER_COLUMN_UUID_UUID_1);
            } else if (uuid.equals(StringPool.BLANK)) {
                query.append(_FINDER_COLUMN_UUID_UUID_3);
            } else {
                bindUuid = true;

                query.append(_FINDER_COLUMN_UUID_UUID_2);
            }

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

            String sql = query.toString();

            Session session = null;

            try {
                session = openSession();

                Query q = session.createQuery(sql);

                QueryPos qPos = QueryPos.getInstance(q);

                if (bindUuid) {
                    qPos.add(uuid);
                }

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

                    Collections.sort(list);

                    list = new UnmodifiableList<GrupoDestinatario>(list);
                } else {
                    list = (List<GrupoDestinatario>) QueryUtil.list(q,
                            getDialect(), start, end);
                }

                cacheResult(list);

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

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

        return list;
    }

    /**
     * Returns the first grupo destinatario in the ordered set where uuid = &#63;.
     *
     * @param uuid the uuid
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the first matching grupo destinatario
     * @throws br.mp.mprj.portal.portlet.gestaomensagens.service.NoSuchGrupoDestinatarioException if a matching grupo destinatario could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public GrupoDestinatario findByUuid_First(String uuid,
        OrderByComparator orderByComparator)
        throws NoSuchGrupoDestinatarioException, SystemException {
        GrupoDestinatario grupoDestinatario = fetchByUuid_First(uuid,
                orderByComparator);

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

        StringBundler msg = new StringBundler(4);

        msg.append(_NO_SUCH_ENTITY_WITH_KEY);

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

        msg.append(StringPool.CLOSE_CURLY_BRACE);

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

    /**
     * Returns the first grupo destinatario in the ordered set where uuid = &#63;.
     *
     * @param uuid the uuid
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the first matching grupo destinatario, or <code>null</code> if a matching grupo destinatario could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public GrupoDestinatario fetchByUuid_First(String uuid,
        OrderByComparator orderByComparator) throws SystemException {
        List<GrupoDestinatario> list = findByUuid(uuid, 0, 1, orderByComparator);

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

        return null;
    }

    /**
     * Returns the last grupo destinatario in the ordered set where uuid = &#63;.
     *
     * @param uuid the uuid
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the last matching grupo destinatario
     * @throws br.mp.mprj.portal.portlet.gestaomensagens.service.NoSuchGrupoDestinatarioException if a matching grupo destinatario could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public GrupoDestinatario findByUuid_Last(String uuid,
        OrderByComparator orderByComparator)
        throws NoSuchGrupoDestinatarioException, SystemException {
        GrupoDestinatario grupoDestinatario = fetchByUuid_Last(uuid,
                orderByComparator);

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

        StringBundler msg = new StringBundler(4);

        msg.append(_NO_SUCH_ENTITY_WITH_KEY);

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

        msg.append(StringPool.CLOSE_CURLY_BRACE);

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

    /**
     * Returns the last grupo destinatario in the ordered set where uuid = &#63;.
     *
     * @param uuid the uuid
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the last matching grupo destinatario, or <code>null</code> if a matching grupo destinatario could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public GrupoDestinatario fetchByUuid_Last(String uuid,
        OrderByComparator orderByComparator) throws SystemException {
        int count = countByUuid(uuid);

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

        List<GrupoDestinatario> list = findByUuid(uuid, count - 1, count,
                orderByComparator);

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

        return null;
    }

    /**
     * Returns the grupo destinatarios before and after the current grupo destinatario in the ordered set where uuid = &#63;.
     *
     * @param grupoDestinatarioId the primary key of the current grupo destinatario
     * @param uuid the uuid
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the previous, current, and next grupo destinatario
     * @throws br.mp.mprj.portal.portlet.gestaomensagens.service.NoSuchGrupoDestinatarioException if a grupo destinatario with the primary key could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public GrupoDestinatario[] findByUuid_PrevAndNext(
        long grupoDestinatarioId, String uuid,
        OrderByComparator orderByComparator)
        throws NoSuchGrupoDestinatarioException, SystemException {
        GrupoDestinatario grupoDestinatario = findByPrimaryKey(grupoDestinatarioId);

        Session session = null;

        try {
            session = openSession();

            GrupoDestinatario[] array = new GrupoDestinatarioImpl[3];

            array[0] = getByUuid_PrevAndNext(session, grupoDestinatario, uuid,
                    orderByComparator, true);

            array[1] = grupoDestinatario;

            array[2] = getByUuid_PrevAndNext(session, grupoDestinatario, uuid,
                    orderByComparator, false);

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

    protected GrupoDestinatario getByUuid_PrevAndNext(Session session,
        GrupoDestinatario grupoDestinatario, String uuid,
        OrderByComparator orderByComparator, boolean previous) {
        StringBundler query = null;

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

        query.append(_SQL_SELECT_GRUPODESTINATARIO_WHERE);

        boolean bindUuid = false;

        if (uuid == null) {
            query.append(_FINDER_COLUMN_UUID_UUID_1);
        } else if (uuid.equals(StringPool.BLANK)) {
            query.append(_FINDER_COLUMN_UUID_UUID_3);
        } else {
            bindUuid = true;

            query.append(_FINDER_COLUMN_UUID_UUID_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(GrupoDestinatarioModelImpl.ORDER_BY_JPQL);
        }

        String sql = query.toString();

        Query q = session.createQuery(sql);

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

        QueryPos qPos = QueryPos.getInstance(q);

        if (bindUuid) {
            qPos.add(uuid);
        }

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

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

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

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

    /**
     * Removes all the grupo destinatarios where uuid = &#63; from the database.
     *
     * @param uuid the uuid
     * @throws SystemException if a system exception occurred
     */
    @Override
    public void removeByUuid(String uuid) throws SystemException {
        for (GrupoDestinatario grupoDestinatario : findByUuid(uuid,
                QueryUtil.ALL_POS, QueryUtil.ALL_POS, null)) {
            remove(grupoDestinatario);
        }
    }

    /**
     * Returns the number of grupo destinatarios where uuid = &#63;.
     *
     * @param uuid the uuid
     * @return the number of matching grupo destinatarios
     * @throws SystemException if a system exception occurred
     */
    @Override
    public int countByUuid(String uuid) throws SystemException {
        FinderPath finderPath = FINDER_PATH_COUNT_BY_UUID;

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

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

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

            query.append(_SQL_COUNT_GRUPODESTINATARIO_WHERE);

            boolean bindUuid = false;

            if (uuid == null) {
                query.append(_FINDER_COLUMN_UUID_UUID_1);
            } else if (uuid.equals(StringPool.BLANK)) {
                query.append(_FINDER_COLUMN_UUID_UUID_3);
            } else {
                bindUuid = true;

                query.append(_FINDER_COLUMN_UUID_UUID_2);
            }

            String sql = query.toString();

            Session session = null;

            try {
                session = openSession();

                Query q = session.createQuery(sql);

                QueryPos qPos = QueryPos.getInstance(q);

                if (bindUuid) {
                    qPos.add(uuid);
                }

                count = (Long) q.uniqueResult();

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

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

        return count.intValue();
    }

    /**
     * Returns the grupo destinatario where uuid = &#63; and groupId = &#63; or throws a {@link br.mp.mprj.portal.portlet.gestaomensagens.service.NoSuchGrupoDestinatarioException} if it could not be found.
     *
     * @param uuid the uuid
     * @param groupId the group ID
     * @return the matching grupo destinatario
     * @throws br.mp.mprj.portal.portlet.gestaomensagens.service.NoSuchGrupoDestinatarioException if a matching grupo destinatario could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public GrupoDestinatario findByUUID_G(String uuid, long groupId)
        throws NoSuchGrupoDestinatarioException, SystemException {
        GrupoDestinatario grupoDestinatario = fetchByUUID_G(uuid, groupId);

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

            msg.append(_NO_SUCH_ENTITY_WITH_KEY);

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

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

            msg.append(StringPool.CLOSE_CURLY_BRACE);

            if (_log.isWarnEnabled()) {
                _log.warn(msg.toString());
            }

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

        return grupoDestinatario;
    }

    /**
     * Returns the grupo destinatario where uuid = &#63; and groupId = &#63; or returns <code>null</code> if it could not be found. Uses the finder cache.
     *
     * @param uuid the uuid
     * @param groupId the group ID
     * @return the matching grupo destinatario, or <code>null</code> if a matching grupo destinatario could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public GrupoDestinatario fetchByUUID_G(String uuid, long groupId)
        throws SystemException {
        return fetchByUUID_G(uuid, groupId, true);
    }

    /**
     * Returns the grupo destinatario where uuid = &#63; and groupId = &#63; or returns <code>null</code> if it could not be found, optionally using the finder cache.
     *
     * @param uuid the uuid
     * @param groupId the group ID
     * @param retrieveFromCache whether to use the finder cache
     * @return the matching grupo destinatario, or <code>null</code> if a matching grupo destinatario could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public GrupoDestinatario fetchByUUID_G(String uuid, long groupId,
        boolean retrieveFromCache) throws SystemException {
        Object[] finderArgs = new Object[] { uuid, groupId };

        Object result = null;

        if (retrieveFromCache) {
            result = FinderCacheUtil.getResult(FINDER_PATH_FETCH_BY_UUID_G,
                    finderArgs, this);
        }

        if (result instanceof GrupoDestinatario) {
            GrupoDestinatario grupoDestinatario = (GrupoDestinatario) result;

            if (!Validator.equals(uuid, grupoDestinatario.getUuid()) ||
                    (groupId != grupoDestinatario.getGroupId())) {
                result = null;
            }
        }

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

            query.append(_SQL_SELECT_GRUPODESTINATARIO_WHERE);

            boolean bindUuid = false;

            if (uuid == null) {
                query.append(_FINDER_COLUMN_UUID_G_UUID_1);
            } else if (uuid.equals(StringPool.BLANK)) {
                query.append(_FINDER_COLUMN_UUID_G_UUID_3);
            } else {
                bindUuid = true;

                query.append(_FINDER_COLUMN_UUID_G_UUID_2);
            }

            query.append(_FINDER_COLUMN_UUID_G_GROUPID_2);

            String sql = query.toString();

            Session session = null;

            try {
                session = openSession();

                Query q = session.createQuery(sql);

                QueryPos qPos = QueryPos.getInstance(q);

                if (bindUuid) {
                    qPos.add(uuid);
                }

                qPos.add(groupId);

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

                if (list.isEmpty()) {
                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_UUID_G,
                        finderArgs, list);
                } else {
                    GrupoDestinatario grupoDestinatario = list.get(0);

                    result = grupoDestinatario;

                    cacheResult(grupoDestinatario);

                    if ((grupoDestinatario.getUuid() == null) ||
                            !grupoDestinatario.getUuid().equals(uuid) ||
                            (grupoDestinatario.getGroupId() != groupId)) {
                        FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_UUID_G,
                            finderArgs, grupoDestinatario);
                    }
                }
            } catch (Exception e) {
                FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_UUID_G,
                    finderArgs);

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

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

    /**
     * Removes the grupo destinatario where uuid = &#63; and groupId = &#63; from the database.
     *
     * @param uuid the uuid
     * @param groupId the group ID
     * @return the grupo destinatario that was removed
     * @throws SystemException if a system exception occurred
     */
    @Override
    public GrupoDestinatario removeByUUID_G(String uuid, long groupId)
        throws NoSuchGrupoDestinatarioException, SystemException {
        GrupoDestinatario grupoDestinatario = findByUUID_G(uuid, groupId);

        return remove(grupoDestinatario);
    }

    /**
     * Returns the number of grupo destinatarios where uuid = &#63; and groupId = &#63;.
     *
     * @param uuid the uuid
     * @param groupId the group ID
     * @return the number of matching grupo destinatarios
     * @throws SystemException if a system exception occurred
     */
    @Override
    public int countByUUID_G(String uuid, long groupId)
        throws SystemException {
        FinderPath finderPath = FINDER_PATH_COUNT_BY_UUID_G;

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

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

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

            query.append(_SQL_COUNT_GRUPODESTINATARIO_WHERE);

            boolean bindUuid = false;

            if (uuid == null) {
                query.append(_FINDER_COLUMN_UUID_G_UUID_1);
            } else if (uuid.equals(StringPool.BLANK)) {
                query.append(_FINDER_COLUMN_UUID_G_UUID_3);
            } else {
                bindUuid = true;

                query.append(_FINDER_COLUMN_UUID_G_UUID_2);
            }

            query.append(_FINDER_COLUMN_UUID_G_GROUPID_2);

            String sql = query.toString();

            Session session = null;

            try {
                session = openSession();

                Query q = session.createQuery(sql);

                QueryPos qPos = QueryPos.getInstance(q);

                if (bindUuid) {
                    qPos.add(uuid);
                }

                qPos.add(groupId);

                count = (Long) q.uniqueResult();

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

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

        return count.intValue();
    }

    /**
     * Returns all the grupo destinatarios where uuid = &#63; and companyId = &#63;.
     *
     * @param uuid the uuid
     * @param companyId the company ID
     * @return the matching grupo destinatarios
     * @throws SystemException if a system exception occurred
     */
    @Override
    public List<GrupoDestinatario> findByUuid_C(String uuid, long companyId)
        throws SystemException {
        return findByUuid_C(uuid, companyId, QueryUtil.ALL_POS,
            QueryUtil.ALL_POS, null);
    }

    /**
     * Returns a range of all the grupo destinatarios where uuid = &#63; and companyId = &#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 com.liferay.portal.kernel.dao.orm.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 com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link br.mp.mprj.portal.portlet.gestaomensagens.service.model.impl.GrupoDestinatarioModelImpl}. 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 uuid the uuid
     * @param companyId the company ID
     * @param start the lower bound of the range of grupo destinatarios
     * @param end the upper bound of the range of grupo destinatarios (not inclusive)
     * @return the range of matching grupo destinatarios
     * @throws SystemException if a system exception occurred
     */
    @Override
    public List<GrupoDestinatario> findByUuid_C(String uuid, long companyId,
        int start, int end) throws SystemException {
        return findByUuid_C(uuid, companyId, start, end, null);
    }

    /**
     * Returns an ordered range of all the grupo destinatarios where uuid = &#63; and companyId = &#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 com.liferay.portal.kernel.dao.orm.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 com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link br.mp.mprj.portal.portlet.gestaomensagens.service.model.impl.GrupoDestinatarioModelImpl}. 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 uuid the uuid
     * @param companyId the company ID
     * @param start the lower bound of the range of grupo destinatarios
     * @param end the upper bound of the range of grupo destinatarios (not inclusive)
     * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
     * @return the ordered range of matching grupo destinatarios
     * @throws SystemException if a system exception occurred
     */
    @Override
    public List<GrupoDestinatario> findByUuid_C(String uuid, long companyId,
        int start, int end, OrderByComparator orderByComparator)
        throws SystemException {
        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_UUID_C;
            finderArgs = new Object[] { uuid, companyId };
        } else {
            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_UUID_C;
            finderArgs = new Object[] {
                    uuid, companyId,
                    
                    start, end, orderByComparator
                };
        }

        List<GrupoDestinatario> list = (List<GrupoDestinatario>) FinderCacheUtil.getResult(finderPath,
                finderArgs, this);

        if ((list != null) && !list.isEmpty()) {
            for (GrupoDestinatario grupoDestinatario : list) {
                if (!Validator.equals(uuid, grupoDestinatario.getUuid()) ||
                        (companyId != grupoDestinatario.getCompanyId())) {
                    list = null;

                    break;
                }
            }
        }

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

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

            query.append(_SQL_SELECT_GRUPODESTINATARIO_WHERE);

            boolean bindUuid = false;

            if (uuid == null) {
                query.append(_FINDER_COLUMN_UUID_C_UUID_1);
            } else if (uuid.equals(StringPool.BLANK)) {
                query.append(_FINDER_COLUMN_UUID_C_UUID_3);
            } else {
                bindUuid = true;

                query.append(_FINDER_COLUMN_UUID_C_UUID_2);
            }

            query.append(_FINDER_COLUMN_UUID_C_COMPANYID_2);

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

            String sql = query.toString();

            Session session = null;

            try {
                session = openSession();

                Query q = session.createQuery(sql);

                QueryPos qPos = QueryPos.getInstance(q);

                if (bindUuid) {
                    qPos.add(uuid);
                }

                qPos.add(companyId);

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

                    Collections.sort(list);

                    list = new UnmodifiableList<GrupoDestinatario>(list);
                } else {
                    list = (List<GrupoDestinatario>) QueryUtil.list(q,
                            getDialect(), start, end);
                }

                cacheResult(list);

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

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

        return list;
    }

    /**
     * Returns the first grupo destinatario in the ordered set where uuid = &#63; and companyId = &#63;.
     *
     * @param uuid the uuid
     * @param companyId the company ID
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the first matching grupo destinatario
     * @throws br.mp.mprj.portal.portlet.gestaomensagens.service.NoSuchGrupoDestinatarioException if a matching grupo destinatario could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public GrupoDestinatario findByUuid_C_First(String uuid, long companyId,
        OrderByComparator orderByComparator)
        throws NoSuchGrupoDestinatarioException, SystemException {
        GrupoDestinatario grupoDestinatario = fetchByUuid_C_First(uuid,
                companyId, orderByComparator);

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

        StringBundler msg = new StringBundler(6);

        msg.append(_NO_SUCH_ENTITY_WITH_KEY);

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

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

        msg.append(StringPool.CLOSE_CURLY_BRACE);

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

    /**
     * Returns the first grupo destinatario in the ordered set where uuid = &#63; and companyId = &#63;.
     *
     * @param uuid the uuid
     * @param companyId the company ID
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the first matching grupo destinatario, or <code>null</code> if a matching grupo destinatario could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public GrupoDestinatario fetchByUuid_C_First(String uuid, long companyId,
        OrderByComparator orderByComparator) throws SystemException {
        List<GrupoDestinatario> list = findByUuid_C(uuid, companyId, 0, 1,
                orderByComparator);

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

        return null;
    }

    /**
     * Returns the last grupo destinatario in the ordered set where uuid = &#63; and companyId = &#63;.
     *
     * @param uuid the uuid
     * @param companyId the company ID
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the last matching grupo destinatario
     * @throws br.mp.mprj.portal.portlet.gestaomensagens.service.NoSuchGrupoDestinatarioException if a matching grupo destinatario could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public GrupoDestinatario findByUuid_C_Last(String uuid, long companyId,
        OrderByComparator orderByComparator)
        throws NoSuchGrupoDestinatarioException, SystemException {
        GrupoDestinatario grupoDestinatario = fetchByUuid_C_Last(uuid,
                companyId, orderByComparator);

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

        StringBundler msg = new StringBundler(6);

        msg.append(_NO_SUCH_ENTITY_WITH_KEY);

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

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

        msg.append(StringPool.CLOSE_CURLY_BRACE);

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

    /**
     * Returns the last grupo destinatario in the ordered set where uuid = &#63; and companyId = &#63;.
     *
     * @param uuid the uuid
     * @param companyId the company ID
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the last matching grupo destinatario, or <code>null</code> if a matching grupo destinatario could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public GrupoDestinatario fetchByUuid_C_Last(String uuid, long companyId,
        OrderByComparator orderByComparator) throws SystemException {
        int count = countByUuid_C(uuid, companyId);

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

        List<GrupoDestinatario> list = findByUuid_C(uuid, companyId, count - 1,
                count, orderByComparator);

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

        return null;
    }

    /**
     * Returns the grupo destinatarios before and after the current grupo destinatario in the ordered set where uuid = &#63; and companyId = &#63;.
     *
     * @param grupoDestinatarioId the primary key of the current grupo destinatario
     * @param uuid the uuid
     * @param companyId the company ID
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the previous, current, and next grupo destinatario
     * @throws br.mp.mprj.portal.portlet.gestaomensagens.service.NoSuchGrupoDestinatarioException if a grupo destinatario with the primary key could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public GrupoDestinatario[] findByUuid_C_PrevAndNext(
        long grupoDestinatarioId, String uuid, long companyId,
        OrderByComparator orderByComparator)
        throws NoSuchGrupoDestinatarioException, SystemException {
        GrupoDestinatario grupoDestinatario = findByPrimaryKey(grupoDestinatarioId);

        Session session = null;

        try {
            session = openSession();

            GrupoDestinatario[] array = new GrupoDestinatarioImpl[3];

            array[0] = getByUuid_C_PrevAndNext(session, grupoDestinatario,
                    uuid, companyId, orderByComparator, true);

            array[1] = grupoDestinatario;

            array[2] = getByUuid_C_PrevAndNext(session, grupoDestinatario,
                    uuid, companyId, orderByComparator, false);

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

    protected GrupoDestinatario getByUuid_C_PrevAndNext(Session session,
        GrupoDestinatario grupoDestinatario, String uuid, long companyId,
        OrderByComparator orderByComparator, boolean previous) {
        StringBundler query = null;

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

        query.append(_SQL_SELECT_GRUPODESTINATARIO_WHERE);

        boolean bindUuid = false;

        if (uuid == null) {
            query.append(_FINDER_COLUMN_UUID_C_UUID_1);
        } else if (uuid.equals(StringPool.BLANK)) {
            query.append(_FINDER_COLUMN_UUID_C_UUID_3);
        } else {
            bindUuid = true;

            query.append(_FINDER_COLUMN_UUID_C_UUID_2);
        }

        query.append(_FINDER_COLUMN_UUID_C_COMPANYID_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(GrupoDestinatarioModelImpl.ORDER_BY_JPQL);
        }

        String sql = query.toString();

        Query q = session.createQuery(sql);

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

        QueryPos qPos = QueryPos.getInstance(q);

        if (bindUuid) {
            qPos.add(uuid);
        }

        qPos.add(companyId);

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

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

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

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

    /**
     * Removes all the grupo destinatarios where uuid = &#63; and companyId = &#63; from the database.
     *
     * @param uuid the uuid
     * @param companyId the company ID
     * @throws SystemException if a system exception occurred
     */
    @Override
    public void removeByUuid_C(String uuid, long companyId)
        throws SystemException {
        for (GrupoDestinatario grupoDestinatario : findByUuid_C(uuid,
                companyId, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null)) {
            remove(grupoDestinatario);
        }
    }

    /**
     * Returns the number of grupo destinatarios where uuid = &#63; and companyId = &#63;.
     *
     * @param uuid the uuid
     * @param companyId the company ID
     * @return the number of matching grupo destinatarios
     * @throws SystemException if a system exception occurred
     */
    @Override
    public int countByUuid_C(String uuid, long companyId)
        throws SystemException {
        FinderPath finderPath = FINDER_PATH_COUNT_BY_UUID_C;

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

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

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

            query.append(_SQL_COUNT_GRUPODESTINATARIO_WHERE);

            boolean bindUuid = false;

            if (uuid == null) {
                query.append(_FINDER_COLUMN_UUID_C_UUID_1);
            } else if (uuid.equals(StringPool.BLANK)) {
                query.append(_FINDER_COLUMN_UUID_C_UUID_3);
            } else {
                bindUuid = true;

                query.append(_FINDER_COLUMN_UUID_C_UUID_2);
            }

            query.append(_FINDER_COLUMN_UUID_C_COMPANYID_2);

            String sql = query.toString();

            Session session = null;

            try {
                session = openSession();

                Query q = session.createQuery(sql);

                QueryPos qPos = QueryPos.getInstance(q);

                if (bindUuid) {
                    qPos.add(uuid);
                }

                qPos.add(companyId);

                count = (Long) q.uniqueResult();

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

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

        return count.intValue();
    }

    /**
     * Returns all the grupo destinatarios where tipoLista = &#63; and ativo = &#63;.
     *
     * @param tipoLista the tipo lista
     * @param ativo the ativo
     * @return the matching grupo destinatarios
     * @throws SystemException if a system exception occurred
     */
    @Override
    public List<GrupoDestinatario> findByTipoListaAtivo(int tipoLista,
        boolean ativo) throws SystemException {
        return findByTipoListaAtivo(tipoLista, ativo, QueryUtil.ALL_POS,
            QueryUtil.ALL_POS, null);
    }

    /**
     * Returns a range of all the grupo destinatarios where tipoLista = &#63; and ativo = &#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 com.liferay.portal.kernel.dao.orm.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 com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link br.mp.mprj.portal.portlet.gestaomensagens.service.model.impl.GrupoDestinatarioModelImpl}. 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 tipoLista the tipo lista
     * @param ativo the ativo
     * @param start the lower bound of the range of grupo destinatarios
     * @param end the upper bound of the range of grupo destinatarios (not inclusive)
     * @return the range of matching grupo destinatarios
     * @throws SystemException if a system exception occurred
     */
    @Override
    public List<GrupoDestinatario> findByTipoListaAtivo(int tipoLista,
        boolean ativo, int start, int end) throws SystemException {
        return findByTipoListaAtivo(tipoLista, ativo, start, end, null);
    }

    /**
     * Returns an ordered range of all the grupo destinatarios where tipoLista = &#63; and ativo = &#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 com.liferay.portal.kernel.dao.orm.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 com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link br.mp.mprj.portal.portlet.gestaomensagens.service.model.impl.GrupoDestinatarioModelImpl}. 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 tipoLista the tipo lista
     * @param ativo the ativo
     * @param start the lower bound of the range of grupo destinatarios
     * @param end the upper bound of the range of grupo destinatarios (not inclusive)
     * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
     * @return the ordered range of matching grupo destinatarios
     * @throws SystemException if a system exception occurred
     */
    @Override
    public List<GrupoDestinatario> findByTipoListaAtivo(int tipoLista,
        boolean ativo, int start, int end, OrderByComparator orderByComparator)
        throws SystemException {
        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_TIPOLISTAATIVO;
            finderArgs = new Object[] { tipoLista, ativo };
        } else {
            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_TIPOLISTAATIVO;
            finderArgs = new Object[] {
                    tipoLista, ativo,
                    
                    start, end, orderByComparator
                };
        }

        List<GrupoDestinatario> list = (List<GrupoDestinatario>) FinderCacheUtil.getResult(finderPath,
                finderArgs, this);

        if ((list != null) && !list.isEmpty()) {
            for (GrupoDestinatario grupoDestinatario : list) {
                if ((tipoLista != grupoDestinatario.getTipoLista()) ||
                        (ativo != grupoDestinatario.getAtivo())) {
                    list = null;

                    break;
                }
            }
        }

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

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

            query.append(_SQL_SELECT_GRUPODESTINATARIO_WHERE);

            query.append(_FINDER_COLUMN_TIPOLISTAATIVO_TIPOLISTA_2);

            query.append(_FINDER_COLUMN_TIPOLISTAATIVO_ATIVO_2);

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

                qPos.add(ativo);

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

                    Collections.sort(list);

                    list = new UnmodifiableList<GrupoDestinatario>(list);
                } else {
                    list = (List<GrupoDestinatario>) QueryUtil.list(q,
                            getDialect(), start, end);
                }

                cacheResult(list);

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

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

        return list;
    }

    /**
     * Returns the first grupo destinatario in the ordered set where tipoLista = &#63; and ativo = &#63;.
     *
     * @param tipoLista the tipo lista
     * @param ativo the ativo
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the first matching grupo destinatario
     * @throws br.mp.mprj.portal.portlet.gestaomensagens.service.NoSuchGrupoDestinatarioException if a matching grupo destinatario could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public GrupoDestinatario findByTipoListaAtivo_First(int tipoLista,
        boolean ativo, OrderByComparator orderByComparator)
        throws NoSuchGrupoDestinatarioException, SystemException {
        GrupoDestinatario grupoDestinatario = fetchByTipoListaAtivo_First(tipoLista,
                ativo, orderByComparator);

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

        StringBundler msg = new StringBundler(6);

        msg.append(_NO_SUCH_ENTITY_WITH_KEY);

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

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

        msg.append(StringPool.CLOSE_CURLY_BRACE);

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

    /**
     * Returns the first grupo destinatario in the ordered set where tipoLista = &#63; and ativo = &#63;.
     *
     * @param tipoLista the tipo lista
     * @param ativo the ativo
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the first matching grupo destinatario, or <code>null</code> if a matching grupo destinatario could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public GrupoDestinatario fetchByTipoListaAtivo_First(int tipoLista,
        boolean ativo, OrderByComparator orderByComparator)
        throws SystemException {
        List<GrupoDestinatario> list = findByTipoListaAtivo(tipoLista, ativo,
                0, 1, orderByComparator);

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

        return null;
    }

    /**
     * Returns the last grupo destinatario in the ordered set where tipoLista = &#63; and ativo = &#63;.
     *
     * @param tipoLista the tipo lista
     * @param ativo the ativo
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the last matching grupo destinatario
     * @throws br.mp.mprj.portal.portlet.gestaomensagens.service.NoSuchGrupoDestinatarioException if a matching grupo destinatario could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public GrupoDestinatario findByTipoListaAtivo_Last(int tipoLista,
        boolean ativo, OrderByComparator orderByComparator)
        throws NoSuchGrupoDestinatarioException, SystemException {
        GrupoDestinatario grupoDestinatario = fetchByTipoListaAtivo_Last(tipoLista,
                ativo, orderByComparator);

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

        StringBundler msg = new StringBundler(6);

        msg.append(_NO_SUCH_ENTITY_WITH_KEY);

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

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

        msg.append(StringPool.CLOSE_CURLY_BRACE);

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

    /**
     * Returns the last grupo destinatario in the ordered set where tipoLista = &#63; and ativo = &#63;.
     *
     * @param tipoLista the tipo lista
     * @param ativo the ativo
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the last matching grupo destinatario, or <code>null</code> if a matching grupo destinatario could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public GrupoDestinatario fetchByTipoListaAtivo_Last(int tipoLista,
        boolean ativo, OrderByComparator orderByComparator)
        throws SystemException {
        int count = countByTipoListaAtivo(tipoLista, ativo);

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

        List<GrupoDestinatario> list = findByTipoListaAtivo(tipoLista, ativo,
                count - 1, count, orderByComparator);

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

        return null;
    }

    /**
     * Returns the grupo destinatarios before and after the current grupo destinatario in the ordered set where tipoLista = &#63; and ativo = &#63;.
     *
     * @param grupoDestinatarioId the primary key of the current grupo destinatario
     * @param tipoLista the tipo lista
     * @param ativo the ativo
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the previous, current, and next grupo destinatario
     * @throws br.mp.mprj.portal.portlet.gestaomensagens.service.NoSuchGrupoDestinatarioException if a grupo destinatario with the primary key could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public GrupoDestinatario[] findByTipoListaAtivo_PrevAndNext(
        long grupoDestinatarioId, int tipoLista, boolean ativo,
        OrderByComparator orderByComparator)
        throws NoSuchGrupoDestinatarioException, SystemException {
        GrupoDestinatario grupoDestinatario = findByPrimaryKey(grupoDestinatarioId);

        Session session = null;

        try {
            session = openSession();

            GrupoDestinatario[] array = new GrupoDestinatarioImpl[3];

            array[0] = getByTipoListaAtivo_PrevAndNext(session,
                    grupoDestinatario, tipoLista, ativo, orderByComparator, true);

            array[1] = grupoDestinatario;

            array[2] = getByTipoListaAtivo_PrevAndNext(session,
                    grupoDestinatario, tipoLista, ativo, orderByComparator,
                    false);

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

    protected GrupoDestinatario getByTipoListaAtivo_PrevAndNext(
        Session session, GrupoDestinatario grupoDestinatario, int tipoLista,
        boolean ativo, OrderByComparator orderByComparator, boolean previous) {
        StringBundler query = null;

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

        query.append(_SQL_SELECT_GRUPODESTINATARIO_WHERE);

        query.append(_FINDER_COLUMN_TIPOLISTAATIVO_TIPOLISTA_2);

        query.append(_FINDER_COLUMN_TIPOLISTAATIVO_ATIVO_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(GrupoDestinatarioModelImpl.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(tipoLista);

        qPos.add(ativo);

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

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

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

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

    /**
     * Removes all the grupo destinatarios where tipoLista = &#63; and ativo = &#63; from the database.
     *
     * @param tipoLista the tipo lista
     * @param ativo the ativo
     * @throws SystemException if a system exception occurred
     */
    @Override
    public void removeByTipoListaAtivo(int tipoLista, boolean ativo)
        throws SystemException {
        for (GrupoDestinatario grupoDestinatario : findByTipoListaAtivo(
                tipoLista, ativo, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null)) {
            remove(grupoDestinatario);
        }
    }

    /**
     * Returns the number of grupo destinatarios where tipoLista = &#63; and ativo = &#63;.
     *
     * @param tipoLista the tipo lista
     * @param ativo the ativo
     * @return the number of matching grupo destinatarios
     * @throws SystemException if a system exception occurred
     */
    @Override
    public int countByTipoListaAtivo(int tipoLista, boolean ativo)
        throws SystemException {
        FinderPath finderPath = FINDER_PATH_COUNT_BY_TIPOLISTAATIVO;

        Object[] finderArgs = new Object[] { tipoLista, ativo };

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

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

            query.append(_SQL_COUNT_GRUPODESTINATARIO_WHERE);

            query.append(_FINDER_COLUMN_TIPOLISTAATIVO_TIPOLISTA_2);

            query.append(_FINDER_COLUMN_TIPOLISTAATIVO_ATIVO_2);

            String sql = query.toString();

            Session session = null;

            try {
                session = openSession();

                Query q = session.createQuery(sql);

                QueryPos qPos = QueryPos.getInstance(q);

                qPos.add(tipoLista);

                qPos.add(ativo);

                count = (Long) q.uniqueResult();

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

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

        return count.intValue();
    }

    /**
     * Returns the grupo destinatario where nome = &#63; and tipoLista = &#63; and ativo = &#63; or throws a {@link br.mp.mprj.portal.portlet.gestaomensagens.service.NoSuchGrupoDestinatarioException} if it could not be found.
     *
     * @param nome the nome
     * @param tipoLista the tipo lista
     * @param ativo the ativo
     * @return the matching grupo destinatario
     * @throws br.mp.mprj.portal.portlet.gestaomensagens.service.NoSuchGrupoDestinatarioException if a matching grupo destinatario could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public GrupoDestinatario findByNomeTipoListaAtivo(String nome,
        int tipoLista, boolean ativo)
        throws NoSuchGrupoDestinatarioException, SystemException {
        GrupoDestinatario grupoDestinatario = fetchByNomeTipoListaAtivo(nome,
                tipoLista, ativo);

        if (grupoDestinatario == null) {
            StringBundler msg = new StringBundler(8);

            msg.append(_NO_SUCH_ENTITY_WITH_KEY);

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

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

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

            msg.append(StringPool.CLOSE_CURLY_BRACE);

            if (_log.isWarnEnabled()) {
                _log.warn(msg.toString());
            }

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

        return grupoDestinatario;
    }

    /**
     * Returns the grupo destinatario where nome = &#63; and tipoLista = &#63; and ativo = &#63; or returns <code>null</code> if it could not be found. Uses the finder cache.
     *
     * @param nome the nome
     * @param tipoLista the tipo lista
     * @param ativo the ativo
     * @return the matching grupo destinatario, or <code>null</code> if a matching grupo destinatario could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public GrupoDestinatario fetchByNomeTipoListaAtivo(String nome,
        int tipoLista, boolean ativo) throws SystemException {
        return fetchByNomeTipoListaAtivo(nome, tipoLista, ativo, true);
    }

    /**
     * Returns the grupo destinatario where nome = &#63; and tipoLista = &#63; and ativo = &#63; or returns <code>null</code> if it could not be found, optionally using the finder cache.
     *
     * @param nome the nome
     * @param tipoLista the tipo lista
     * @param ativo the ativo
     * @param retrieveFromCache whether to use the finder cache
     * @return the matching grupo destinatario, or <code>null</code> if a matching grupo destinatario could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public GrupoDestinatario fetchByNomeTipoListaAtivo(String nome,
        int tipoLista, boolean ativo, boolean retrieveFromCache)
        throws SystemException {
        Object[] finderArgs = new Object[] { nome, tipoLista, ativo };

        Object result = null;

        if (retrieveFromCache) {
            result = FinderCacheUtil.getResult(FINDER_PATH_FETCH_BY_NOMETIPOLISTAATIVO,
                    finderArgs, this);
        }

        if (result instanceof GrupoDestinatario) {
            GrupoDestinatario grupoDestinatario = (GrupoDestinatario) result;

            if (!Validator.equals(nome, grupoDestinatario.getNome()) ||
                    (tipoLista != grupoDestinatario.getTipoLista()) ||
                    (ativo != grupoDestinatario.getAtivo())) {
                result = null;
            }
        }

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

            query.append(_SQL_SELECT_GRUPODESTINATARIO_WHERE);

            boolean bindNome = false;

            if (nome == null) {
                query.append(_FINDER_COLUMN_NOMETIPOLISTAATIVO_NOME_1);
            } else if (nome.equals(StringPool.BLANK)) {
                query.append(_FINDER_COLUMN_NOMETIPOLISTAATIVO_NOME_3);
            } else {
                bindNome = true;

                query.append(_FINDER_COLUMN_NOMETIPOLISTAATIVO_NOME_2);
            }

            query.append(_FINDER_COLUMN_NOMETIPOLISTAATIVO_TIPOLISTA_2);

            query.append(_FINDER_COLUMN_NOMETIPOLISTAATIVO_ATIVO_2);

            String sql = query.toString();

            Session session = null;

            try {
                session = openSession();

                Query q = session.createQuery(sql);

                QueryPos qPos = QueryPos.getInstance(q);

                if (bindNome) {
                    qPos.add(nome);
                }

                qPos.add(tipoLista);

                qPos.add(ativo);

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

                if (list.isEmpty()) {
                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_NOMETIPOLISTAATIVO,
                        finderArgs, list);
                } else {
                    if ((list.size() > 1) && _log.isWarnEnabled()) {
                        _log.warn(
                            "GrupoDestinatarioPersistenceImpl.fetchByNomeTipoListaAtivo(String, int, boolean, boolean) with parameters (" +
                            StringUtil.merge(finderArgs) +
                            ") yields a result set with more than 1 result. This violates the logical unique restriction. There is no order guarantee on which result is returned by this finder.");
                    }

                    GrupoDestinatario grupoDestinatario = list.get(0);

                    result = grupoDestinatario;

                    cacheResult(grupoDestinatario);

                    if ((grupoDestinatario.getNome() == null) ||
                            !grupoDestinatario.getNome().equals(nome) ||
                            (grupoDestinatario.getTipoLista() != tipoLista) ||
                            (grupoDestinatario.getAtivo() != ativo)) {
                        FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_NOMETIPOLISTAATIVO,
                            finderArgs, grupoDestinatario);
                    }
                }
            } catch (Exception e) {
                FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_NOMETIPOLISTAATIVO,
                    finderArgs);

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

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

    /**
     * Removes the grupo destinatario where nome = &#63; and tipoLista = &#63; and ativo = &#63; from the database.
     *
     * @param nome the nome
     * @param tipoLista the tipo lista
     * @param ativo the ativo
     * @return the grupo destinatario that was removed
     * @throws SystemException if a system exception occurred
     */
    @Override
    public GrupoDestinatario removeByNomeTipoListaAtivo(String nome,
        int tipoLista, boolean ativo)
        throws NoSuchGrupoDestinatarioException, SystemException {
        GrupoDestinatario grupoDestinatario = findByNomeTipoListaAtivo(nome,
                tipoLista, ativo);

        return remove(grupoDestinatario);
    }

    /**
     * Returns the number of grupo destinatarios where nome = &#63; and tipoLista = &#63; and ativo = &#63;.
     *
     * @param nome the nome
     * @param tipoLista the tipo lista
     * @param ativo the ativo
     * @return the number of matching grupo destinatarios
     * @throws SystemException if a system exception occurred
     */
    @Override
    public int countByNomeTipoListaAtivo(String nome, int tipoLista,
        boolean ativo) throws SystemException {
        FinderPath finderPath = FINDER_PATH_COUNT_BY_NOMETIPOLISTAATIVO;

        Object[] finderArgs = new Object[] { nome, tipoLista, ativo };

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

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

            query.append(_SQL_COUNT_GRUPODESTINATARIO_WHERE);

            boolean bindNome = false;

            if (nome == null) {
                query.append(_FINDER_COLUMN_NOMETIPOLISTAATIVO_NOME_1);
            } else if (nome.equals(StringPool.BLANK)) {
                query.append(_FINDER_COLUMN_NOMETIPOLISTAATIVO_NOME_3);
            } else {
                bindNome = true;

                query.append(_FINDER_COLUMN_NOMETIPOLISTAATIVO_NOME_2);
            }

            query.append(_FINDER_COLUMN_NOMETIPOLISTAATIVO_TIPOLISTA_2);

            query.append(_FINDER_COLUMN_NOMETIPOLISTAATIVO_ATIVO_2);

            String sql = query.toString();

            Session session = null;

            try {
                session = openSession();

                Query q = session.createQuery(sql);

                QueryPos qPos = QueryPos.getInstance(q);

                if (bindNome) {
                    qPos.add(nome);
                }

                qPos.add(tipoLista);

                qPos.add(ativo);

                count = (Long) q.uniqueResult();

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

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

        return count.intValue();
    }

    /**
     * Returns all the grupo destinatarios where grupoDestinatarioId = &#63;.
     *
     * @param grupoDestinatarioId the grupo destinatario ID
     * @return the matching grupo destinatarios
     * @throws SystemException if a system exception occurred
     */
    @Override
    public List<GrupoDestinatario> findByGrupoDestinatarioIdArray(
        long grupoDestinatarioId) throws SystemException {
        return findByGrupoDestinatarioIdArray(grupoDestinatarioId,
            QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
    }

    /**
     * Returns a range of all the grupo destinatarios where grupoDestinatarioId = &#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 com.liferay.portal.kernel.dao.orm.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 com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link br.mp.mprj.portal.portlet.gestaomensagens.service.model.impl.GrupoDestinatarioModelImpl}. 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 grupoDestinatarioId the grupo destinatario ID
     * @param start the lower bound of the range of grupo destinatarios
     * @param end the upper bound of the range of grupo destinatarios (not inclusive)
     * @return the range of matching grupo destinatarios
     * @throws SystemException if a system exception occurred
     */
    @Override
    public List<GrupoDestinatario> findByGrupoDestinatarioIdArray(
        long grupoDestinatarioId, int start, int end) throws SystemException {
        return findByGrupoDestinatarioIdArray(grupoDestinatarioId, start, end,
            null);
    }

    /**
     * Returns an ordered range of all the grupo destinatarios where grupoDestinatarioId = &#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 com.liferay.portal.kernel.dao.orm.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 com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link br.mp.mprj.portal.portlet.gestaomensagens.service.model.impl.GrupoDestinatarioModelImpl}. 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 grupoDestinatarioId the grupo destinatario ID
     * @param start the lower bound of the range of grupo destinatarios
     * @param end the upper bound of the range of grupo destinatarios (not inclusive)
     * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
     * @return the ordered range of matching grupo destinatarios
     * @throws SystemException if a system exception occurred
     */
    @Override
    public List<GrupoDestinatario> findByGrupoDestinatarioIdArray(
        long grupoDestinatarioId, int start, int end,
        OrderByComparator orderByComparator) throws SystemException {
        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_GRUPODESTINATARIOIDARRAY;
            finderArgs = new Object[] { grupoDestinatarioId };
        } else {
            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_GRUPODESTINATARIOIDARRAY;
            finderArgs = new Object[] {
                    grupoDestinatarioId,
                    
                    start, end, orderByComparator
                };
        }

        List<GrupoDestinatario> list = (List<GrupoDestinatario>) FinderCacheUtil.getResult(finderPath,
                finderArgs, this);

        if ((list != null) && !list.isEmpty()) {
            for (GrupoDestinatario grupoDestinatario : list) {
                if ((grupoDestinatarioId != grupoDestinatario.getGrupoDestinatarioId())) {
                    list = null;

                    break;
                }
            }
        }

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

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

            query.append(_SQL_SELECT_GRUPODESTINATARIO_WHERE);

            query.append(_FINDER_COLUMN_GRUPODESTINATARIOIDARRAY_GRUPODESTINATARIOID_2);

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

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

                    Collections.sort(list);

                    list = new UnmodifiableList<GrupoDestinatario>(list);
                } else {
                    list = (List<GrupoDestinatario>) QueryUtil.list(q,
                            getDialect(), start, end);
                }

                cacheResult(list);

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

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

        return list;
    }

    /**
     * Returns the first grupo destinatario in the ordered set where grupoDestinatarioId = &#63;.
     *
     * @param grupoDestinatarioId the grupo destinatario ID
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the first matching grupo destinatario
     * @throws br.mp.mprj.portal.portlet.gestaomensagens.service.NoSuchGrupoDestinatarioException if a matching grupo destinatario could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public GrupoDestinatario findByGrupoDestinatarioIdArray_First(
        long grupoDestinatarioId, OrderByComparator orderByComparator)
        throws NoSuchGrupoDestinatarioException, SystemException {
        GrupoDestinatario grupoDestinatario = fetchByGrupoDestinatarioIdArray_First(grupoDestinatarioId,
                orderByComparator);

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

        StringBundler msg = new StringBundler(4);

        msg.append(_NO_SUCH_ENTITY_WITH_KEY);

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

        msg.append(StringPool.CLOSE_CURLY_BRACE);

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

    /**
     * Returns the first grupo destinatario in the ordered set where grupoDestinatarioId = &#63;.
     *
     * @param grupoDestinatarioId the grupo destinatario ID
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the first matching grupo destinatario, or <code>null</code> if a matching grupo destinatario could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public GrupoDestinatario fetchByGrupoDestinatarioIdArray_First(
        long grupoDestinatarioId, OrderByComparator orderByComparator)
        throws SystemException {
        List<GrupoDestinatario> list = findByGrupoDestinatarioIdArray(grupoDestinatarioId,
                0, 1, orderByComparator);

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

        return null;
    }

    /**
     * Returns the last grupo destinatario in the ordered set where grupoDestinatarioId = &#63;.
     *
     * @param grupoDestinatarioId the grupo destinatario ID
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the last matching grupo destinatario
     * @throws br.mp.mprj.portal.portlet.gestaomensagens.service.NoSuchGrupoDestinatarioException if a matching grupo destinatario could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public GrupoDestinatario findByGrupoDestinatarioIdArray_Last(
        long grupoDestinatarioId, OrderByComparator orderByComparator)
        throws NoSuchGrupoDestinatarioException, SystemException {
        GrupoDestinatario grupoDestinatario = fetchByGrupoDestinatarioIdArray_Last(grupoDestinatarioId,
                orderByComparator);

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

        StringBundler msg = new StringBundler(4);

        msg.append(_NO_SUCH_ENTITY_WITH_KEY);

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

        msg.append(StringPool.CLOSE_CURLY_BRACE);

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

    /**
     * Returns the last grupo destinatario in the ordered set where grupoDestinatarioId = &#63;.
     *
     * @param grupoDestinatarioId the grupo destinatario ID
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the last matching grupo destinatario, or <code>null</code> if a matching grupo destinatario could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public GrupoDestinatario fetchByGrupoDestinatarioIdArray_Last(
        long grupoDestinatarioId, OrderByComparator orderByComparator)
        throws SystemException {
        int count = countByGrupoDestinatarioIdArray(grupoDestinatarioId);

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

        List<GrupoDestinatario> list = findByGrupoDestinatarioIdArray(grupoDestinatarioId,
                count - 1, count, orderByComparator);

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

        return null;
    }

    /**
     * Returns all the grupo destinatarios where grupoDestinatarioId = any &#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 com.liferay.portal.kernel.dao.orm.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 com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link br.mp.mprj.portal.portlet.gestaomensagens.service.model.impl.GrupoDestinatarioModelImpl}. 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 grupoDestinatarioIds the grupo destinatario IDs
     * @return the matching grupo destinatarios
     * @throws SystemException if a system exception occurred
     */
    @Override
    public List<GrupoDestinatario> findByGrupoDestinatarioIdArray(
        long[] grupoDestinatarioIds) throws SystemException {
        return findByGrupoDestinatarioIdArray(grupoDestinatarioIds,
            QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
    }

    /**
     * Returns a range of all the grupo destinatarios where grupoDestinatarioId = any &#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 com.liferay.portal.kernel.dao.orm.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 com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link br.mp.mprj.portal.portlet.gestaomensagens.service.model.impl.GrupoDestinatarioModelImpl}. 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 grupoDestinatarioIds the grupo destinatario IDs
     * @param start the lower bound of the range of grupo destinatarios
     * @param end the upper bound of the range of grupo destinatarios (not inclusive)
     * @return the range of matching grupo destinatarios
     * @throws SystemException if a system exception occurred
     */
    @Override
    public List<GrupoDestinatario> findByGrupoDestinatarioIdArray(
        long[] grupoDestinatarioIds, int start, int end)
        throws SystemException {
        return findByGrupoDestinatarioIdArray(grupoDestinatarioIds, start, end,
            null);
    }

    /**
     * Returns an ordered range of all the grupo destinatarios where grupoDestinatarioId = any &#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 com.liferay.portal.kernel.dao.orm.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 com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link br.mp.mprj.portal.portlet.gestaomensagens.service.model.impl.GrupoDestinatarioModelImpl}. 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 grupoDestinatarioIds the grupo destinatario IDs
     * @param start the lower bound of the range of grupo destinatarios
     * @param end the upper bound of the range of grupo destinatarios (not inclusive)
     * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
     * @return the ordered range of matching grupo destinatarios
     * @throws SystemException if a system exception occurred
     */
    @Override
    public List<GrupoDestinatario> findByGrupoDestinatarioIdArray(
        long[] grupoDestinatarioIds, int start, int end,
        OrderByComparator orderByComparator) throws SystemException {
        if ((grupoDestinatarioIds != null) &&
                (grupoDestinatarioIds.length == 1)) {
            return findByGrupoDestinatarioIdArray(grupoDestinatarioIds[0],
                start, end, orderByComparator);
        }

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

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

        List<GrupoDestinatario> list = (List<GrupoDestinatario>) FinderCacheUtil.getResult(FINDER_PATH_WITH_PAGINATION_FIND_BY_GRUPODESTINATARIOIDARRAY,
                finderArgs, this);

        if ((list != null) && !list.isEmpty()) {
            for (GrupoDestinatario grupoDestinatario : list) {
                if (!ArrayUtil.contains(grupoDestinatarioIds,
                            grupoDestinatario.getGrupoDestinatarioId())) {
                    list = null;

                    break;
                }
            }
        }

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

            query.append(_SQL_SELECT_GRUPODESTINATARIO_WHERE);

            boolean conjunctionable = false;

            if ((grupoDestinatarioIds == null) ||
                    (grupoDestinatarioIds.length > 0)) {
                if (conjunctionable) {
                    query.append(WHERE_AND);
                }

                query.append(StringPool.OPEN_PARENTHESIS);

                for (int i = 0; i < grupoDestinatarioIds.length; i++) {
                    query.append(_FINDER_COLUMN_GRUPODESTINATARIOIDARRAY_GRUPODESTINATARIOID_5);

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

                query.append(StringPool.CLOSE_PARENTHESIS);

                conjunctionable = true;
            }

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

            String sql = query.toString();

            Session session = null;

            try {
                session = openSession();

                Query q = session.createQuery(sql);

                QueryPos qPos = QueryPos.getInstance(q);

                if (grupoDestinatarioIds != null) {
                    qPos.add(grupoDestinatarioIds);
                }

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

                    Collections.sort(list);

                    list = new UnmodifiableList<GrupoDestinatario>(list);
                } else {
                    list = (List<GrupoDestinatario>) QueryUtil.list(q,
                            getDialect(), start, end);
                }

                cacheResult(list);

                FinderCacheUtil.putResult(FINDER_PATH_WITH_PAGINATION_FIND_BY_GRUPODESTINATARIOIDARRAY,
                    finderArgs, list);
            } catch (Exception e) {
                FinderCacheUtil.removeResult(FINDER_PATH_WITH_PAGINATION_FIND_BY_GRUPODESTINATARIOIDARRAY,
                    finderArgs);

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

        return list;
    }

    /**
     * Removes all the grupo destinatarios where grupoDestinatarioId = &#63; from the database.
     *
     * @param grupoDestinatarioId the grupo destinatario ID
     * @throws SystemException if a system exception occurred
     */
    @Override
    public void removeByGrupoDestinatarioIdArray(long grupoDestinatarioId)
        throws SystemException {
        for (GrupoDestinatario grupoDestinatario : findByGrupoDestinatarioIdArray(
                grupoDestinatarioId, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null)) {
            remove(grupoDestinatario);
        }
    }

    /**
     * Returns the number of grupo destinatarios where grupoDestinatarioId = &#63;.
     *
     * @param grupoDestinatarioId the grupo destinatario ID
     * @return the number of matching grupo destinatarios
     * @throws SystemException if a system exception occurred
     */
    @Override
    public int countByGrupoDestinatarioIdArray(long grupoDestinatarioId)
        throws SystemException {
        FinderPath finderPath = FINDER_PATH_COUNT_BY_GRUPODESTINATARIOIDARRAY;

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

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

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

            query.append(_SQL_COUNT_GRUPODESTINATARIO_WHERE);

            query.append(_FINDER_COLUMN_GRUPODESTINATARIOIDARRAY_GRUPODESTINATARIOID_2);

            String sql = query.toString();

            Session session = null;

            try {
                session = openSession();

                Query q = session.createQuery(sql);

                QueryPos qPos = QueryPos.getInstance(q);

                qPos.add(grupoDestinatarioId);

                count = (Long) q.uniqueResult();

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

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

        return count.intValue();
    }

    /**
     * Returns the number of grupo destinatarios where grupoDestinatarioId = any &#63;.
     *
     * @param grupoDestinatarioIds the grupo destinatario IDs
     * @return the number of matching grupo destinatarios
     * @throws SystemException if a system exception occurred
     */
    @Override
    public int countByGrupoDestinatarioIdArray(long[] grupoDestinatarioIds)
        throws SystemException {
        Object[] finderArgs = new Object[] {
                StringUtil.merge(grupoDestinatarioIds)
            };

        Long count = (Long) FinderCacheUtil.getResult(FINDER_PATH_WITH_PAGINATION_COUNT_BY_GRUPODESTINATARIOIDARRAY,
                finderArgs, this);

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

            query.append(_SQL_COUNT_GRUPODESTINATARIO_WHERE);

            boolean conjunctionable = false;

            if ((grupoDestinatarioIds == null) ||
                    (grupoDestinatarioIds.length > 0)) {
                if (conjunctionable) {
                    query.append(WHERE_AND);
                }

                query.append(StringPool.OPEN_PARENTHESIS);

                for (int i = 0; i < grupoDestinatarioIds.length; i++) {
                    query.append(_FINDER_COLUMN_GRUPODESTINATARIOIDARRAY_GRUPODESTINATARIOID_5);

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

                query.append(StringPool.CLOSE_PARENTHESIS);

                conjunctionable = true;
            }

            String sql = query.toString();

            Session session = null;

            try {
                session = openSession();

                Query q = session.createQuery(sql);

                QueryPos qPos = QueryPos.getInstance(q);

                if (grupoDestinatarioIds != null) {
                    qPos.add(grupoDestinatarioIds);
                }

                count = (Long) q.uniqueResult();

                FinderCacheUtil.putResult(FINDER_PATH_WITH_PAGINATION_COUNT_BY_GRUPODESTINATARIOIDARRAY,
                    finderArgs, count);
            } catch (Exception e) {
                FinderCacheUtil.removeResult(FINDER_PATH_WITH_PAGINATION_COUNT_BY_GRUPODESTINATARIOIDARRAY,
                    finderArgs);

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

        return count.intValue();
    }

    /**
     * Caches the grupo destinatario in the entity cache if it is enabled.
     *
     * @param grupoDestinatario the grupo destinatario
     */
    @Override
    public void cacheResult(GrupoDestinatario grupoDestinatario) {
        EntityCacheUtil.putResult(GrupoDestinatarioModelImpl.ENTITY_CACHE_ENABLED,
            GrupoDestinatarioImpl.class, grupoDestinatario.getPrimaryKey(),
            grupoDestinatario);

        FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_UUID_G,
            new Object[] {
                grupoDestinatario.getUuid(), grupoDestinatario.getGroupId()
            }, grupoDestinatario);

        FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_NOMETIPOLISTAATIVO,
            new Object[] {
                grupoDestinatario.getNome(), grupoDestinatario.getTipoLista(),
                grupoDestinatario.getAtivo()
            }, grupoDestinatario);

        grupoDestinatario.resetOriginalValues();
    }

    /**
     * Caches the grupo destinatarios in the entity cache if it is enabled.
     *
     * @param grupoDestinatarios the grupo destinatarios
     */
    @Override
    public void cacheResult(List<GrupoDestinatario> grupoDestinatarios) {
        for (GrupoDestinatario grupoDestinatario : grupoDestinatarios) {
            if (EntityCacheUtil.getResult(
                        GrupoDestinatarioModelImpl.ENTITY_CACHE_ENABLED,
                        GrupoDestinatarioImpl.class,
                        grupoDestinatario.getPrimaryKey()) == null) {
                cacheResult(grupoDestinatario);
            } else {
                grupoDestinatario.resetOriginalValues();
            }
        }
    }

    /**
     * Clears the cache for all grupo destinatarios.
     *
     * <p>
     * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
     * </p>
     */
    @Override
    public void clearCache() {
        if (_HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE) {
            CacheRegistryUtil.clear(GrupoDestinatarioImpl.class.getName());
        }

        EntityCacheUtil.clearCache(GrupoDestinatarioImpl.class.getName());

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

    /**
     * Clears the cache for the grupo destinatario.
     *
     * <p>
     * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
     * </p>
     */
    @Override
    public void clearCache(GrupoDestinatario grupoDestinatario) {
        EntityCacheUtil.removeResult(GrupoDestinatarioModelImpl.ENTITY_CACHE_ENABLED,
            GrupoDestinatarioImpl.class, grupoDestinatario.getPrimaryKey());

        FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
        FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);

        clearUniqueFindersCache(grupoDestinatario);
    }

    @Override
    public void clearCache(List<GrupoDestinatario> grupoDestinatarios) {
        FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
        FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);

        for (GrupoDestinatario grupoDestinatario : grupoDestinatarios) {
            EntityCacheUtil.removeResult(GrupoDestinatarioModelImpl.ENTITY_CACHE_ENABLED,
                GrupoDestinatarioImpl.class, grupoDestinatario.getPrimaryKey());

            clearUniqueFindersCache(grupoDestinatario);
        }
    }

    protected void cacheUniqueFindersCache(GrupoDestinatario grupoDestinatario) {
        if (grupoDestinatario.isNew()) {
            Object[] args = new Object[] {
                    grupoDestinatario.getUuid(), grupoDestinatario.getGroupId()
                };

            FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_UUID_G, args,
                Long.valueOf(1));
            FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_UUID_G, args,
                grupoDestinatario);

            args = new Object[] {
                    grupoDestinatario.getNome(),
                    grupoDestinatario.getTipoLista(),
                    grupoDestinatario.getAtivo()
                };

            FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_NOMETIPOLISTAATIVO,
                args, Long.valueOf(1));
            FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_NOMETIPOLISTAATIVO,
                args, grupoDestinatario);
        } else {
            GrupoDestinatarioModelImpl grupoDestinatarioModelImpl = (GrupoDestinatarioModelImpl) grupoDestinatario;

            if ((grupoDestinatarioModelImpl.getColumnBitmask() &
                    FINDER_PATH_FETCH_BY_UUID_G.getColumnBitmask()) != 0) {
                Object[] args = new Object[] {
                        grupoDestinatario.getUuid(),
                        grupoDestinatario.getGroupId()
                    };

                FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_UUID_G, args,
                    Long.valueOf(1));
                FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_UUID_G, args,
                    grupoDestinatario);
            }

            if ((grupoDestinatarioModelImpl.getColumnBitmask() &
                    FINDER_PATH_FETCH_BY_NOMETIPOLISTAATIVO.getColumnBitmask()) != 0) {
                Object[] args = new Object[] {
                        grupoDestinatario.getNome(),
                        grupoDestinatario.getTipoLista(),
                        grupoDestinatario.getAtivo()
                    };

                FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_NOMETIPOLISTAATIVO,
                    args, Long.valueOf(1));
                FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_NOMETIPOLISTAATIVO,
                    args, grupoDestinatario);
            }
        }
    }

    protected void clearUniqueFindersCache(GrupoDestinatario grupoDestinatario) {
        GrupoDestinatarioModelImpl grupoDestinatarioModelImpl = (GrupoDestinatarioModelImpl) grupoDestinatario;

        Object[] args = new Object[] {
                grupoDestinatario.getUuid(), grupoDestinatario.getGroupId()
            };

        FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_UUID_G, args);
        FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_UUID_G, args);

        if ((grupoDestinatarioModelImpl.getColumnBitmask() &
                FINDER_PATH_FETCH_BY_UUID_G.getColumnBitmask()) != 0) {
            args = new Object[] {
                    grupoDestinatarioModelImpl.getOriginalUuid(),
                    grupoDestinatarioModelImpl.getOriginalGroupId()
                };

            FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_UUID_G, args);
            FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_UUID_G, args);
        }

        args = new Object[] {
                grupoDestinatario.getNome(), grupoDestinatario.getTipoLista(),
                grupoDestinatario.getAtivo()
            };

        FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_NOMETIPOLISTAATIVO,
            args);
        FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_NOMETIPOLISTAATIVO,
            args);

        if ((grupoDestinatarioModelImpl.getColumnBitmask() &
                FINDER_PATH_FETCH_BY_NOMETIPOLISTAATIVO.getColumnBitmask()) != 0) {
            args = new Object[] {
                    grupoDestinatarioModelImpl.getOriginalNome(),
                    grupoDestinatarioModelImpl.getOriginalTipoLista(),
                    grupoDestinatarioModelImpl.getOriginalAtivo()
                };

            FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_NOMETIPOLISTAATIVO,
                args);
            FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_NOMETIPOLISTAATIVO,
                args);
        }
    }

    /**
     * Creates a new grupo destinatario with the primary key. Does not add the grupo destinatario to the database.
     *
     * @param grupoDestinatarioId the primary key for the new grupo destinatario
     * @return the new grupo destinatario
     */
    @Override
    public GrupoDestinatario create(long grupoDestinatarioId) {
        GrupoDestinatario grupoDestinatario = new GrupoDestinatarioImpl();

        grupoDestinatario.setNew(true);
        grupoDestinatario.setPrimaryKey(grupoDestinatarioId);

        String uuid = PortalUUIDUtil.generate();

        grupoDestinatario.setUuid(uuid);

        return grupoDestinatario;
    }

    /**
     * Removes the grupo destinatario with the primary key from the database. Also notifies the appropriate model listeners.
     *
     * @param grupoDestinatarioId the primary key of the grupo destinatario
     * @return the grupo destinatario that was removed
     * @throws br.mp.mprj.portal.portlet.gestaomensagens.service.NoSuchGrupoDestinatarioException if a grupo destinatario with the primary key could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public GrupoDestinatario remove(long grupoDestinatarioId)
        throws NoSuchGrupoDestinatarioException, SystemException {
        return remove((Serializable) grupoDestinatarioId);
    }

    /**
     * Removes the grupo destinatario with the primary key from the database. Also notifies the appropriate model listeners.
     *
     * @param primaryKey the primary key of the grupo destinatario
     * @return the grupo destinatario that was removed
     * @throws br.mp.mprj.portal.portlet.gestaomensagens.service.NoSuchGrupoDestinatarioException if a grupo destinatario with the primary key could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public GrupoDestinatario remove(Serializable primaryKey)
        throws NoSuchGrupoDestinatarioException, SystemException {
        Session session = null;

        try {
            session = openSession();

            GrupoDestinatario grupoDestinatario = (GrupoDestinatario) session.get(GrupoDestinatarioImpl.class,
                    primaryKey);

            if (grupoDestinatario == null) {
                if (_log.isWarnEnabled()) {
                    _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
                }

                throw new NoSuchGrupoDestinatarioException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
                    primaryKey);
            }

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

    @Override
    protected GrupoDestinatario removeImpl(GrupoDestinatario grupoDestinatario)
        throws SystemException {
        grupoDestinatario = toUnwrappedModel(grupoDestinatario);

        Session session = null;

        try {
            session = openSession();

            if (!session.contains(grupoDestinatario)) {
                grupoDestinatario = (GrupoDestinatario) session.get(GrupoDestinatarioImpl.class,
                        grupoDestinatario.getPrimaryKeyObj());
            }

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

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

        return grupoDestinatario;
    }

    @Override
    public GrupoDestinatario updateImpl(
        br.mp.mprj.portal.portlet.gestaomensagens.service.model.GrupoDestinatario grupoDestinatario)
        throws SystemException {
        grupoDestinatario = toUnwrappedModel(grupoDestinatario);

        boolean isNew = grupoDestinatario.isNew();

        GrupoDestinatarioModelImpl grupoDestinatarioModelImpl = (GrupoDestinatarioModelImpl) grupoDestinatario;

        if (Validator.isNull(grupoDestinatario.getUuid())) {
            String uuid = PortalUUIDUtil.generate();

            grupoDestinatario.setUuid(uuid);
        }

        Session session = null;

        try {
            session = openSession();

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

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

        FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);

        if (isNew || !GrupoDestinatarioModelImpl.COLUMN_BITMASK_ENABLED) {
            FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
        }
        else {
            if ((grupoDestinatarioModelImpl.getColumnBitmask() &
                    FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_UUID.getColumnBitmask()) != 0) {
                Object[] args = new Object[] {
                        grupoDestinatarioModelImpl.getOriginalUuid()
                    };

                FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_UUID, args);
                FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_UUID,
                    args);

                args = new Object[] { grupoDestinatarioModelImpl.getUuid() };

                FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_UUID, args);
                FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_UUID,
                    args);
            }

            if ((grupoDestinatarioModelImpl.getColumnBitmask() &
                    FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_UUID_C.getColumnBitmask()) != 0) {
                Object[] args = new Object[] {
                        grupoDestinatarioModelImpl.getOriginalUuid(),
                        grupoDestinatarioModelImpl.getOriginalCompanyId()
                    };

                FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_UUID_C, args);
                FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_UUID_C,
                    args);

                args = new Object[] {
                        grupoDestinatarioModelImpl.getUuid(),
                        grupoDestinatarioModelImpl.getCompanyId()
                    };

                FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_UUID_C, args);
                FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_UUID_C,
                    args);
            }

            if ((grupoDestinatarioModelImpl.getColumnBitmask() &
                    FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_TIPOLISTAATIVO.getColumnBitmask()) != 0) {
                Object[] args = new Object[] {
                        grupoDestinatarioModelImpl.getOriginalTipoLista(),
                        grupoDestinatarioModelImpl.getOriginalAtivo()
                    };

                FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_TIPOLISTAATIVO,
                    args);
                FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_TIPOLISTAATIVO,
                    args);

                args = new Object[] {
                        grupoDestinatarioModelImpl.getTipoLista(),
                        grupoDestinatarioModelImpl.getAtivo()
                    };

                FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_TIPOLISTAATIVO,
                    args);
                FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_TIPOLISTAATIVO,
                    args);
            }

            if ((grupoDestinatarioModelImpl.getColumnBitmask() &
                    FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_GRUPODESTINATARIOIDARRAY.getColumnBitmask()) != 0) {
                Object[] args = new Object[] {
                        grupoDestinatarioModelImpl.getOriginalGrupoDestinatarioId()
                    };

                FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_GRUPODESTINATARIOIDARRAY,
                    args);
                FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_GRUPODESTINATARIOIDARRAY,
                    args);

                args = new Object[] {
                        grupoDestinatarioModelImpl.getGrupoDestinatarioId()
                    };

                FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_GRUPODESTINATARIOIDARRAY,
                    args);
                FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_GRUPODESTINATARIOIDARRAY,
                    args);
            }
        }

        EntityCacheUtil.putResult(GrupoDestinatarioModelImpl.ENTITY_CACHE_ENABLED,
            GrupoDestinatarioImpl.class, grupoDestinatario.getPrimaryKey(),
            grupoDestinatario);

        clearUniqueFindersCache(grupoDestinatario);
        cacheUniqueFindersCache(grupoDestinatario);

        return grupoDestinatario;
    }

    protected GrupoDestinatario toUnwrappedModel(
        GrupoDestinatario grupoDestinatario) {
        if (grupoDestinatario instanceof GrupoDestinatarioImpl) {
            return grupoDestinatario;
        }

        GrupoDestinatarioImpl grupoDestinatarioImpl = new GrupoDestinatarioImpl();

        grupoDestinatarioImpl.setNew(grupoDestinatario.isNew());
        grupoDestinatarioImpl.setPrimaryKey(grupoDestinatario.getPrimaryKey());

        grupoDestinatarioImpl.setUuid(grupoDestinatario.getUuid());
        grupoDestinatarioImpl.setGrupoDestinatarioId(grupoDestinatario.getGrupoDestinatarioId());
        grupoDestinatarioImpl.setCompanyId(grupoDestinatario.getCompanyId());
        grupoDestinatarioImpl.setGroupId(grupoDestinatario.getGroupId());
        grupoDestinatarioImpl.setUserId(grupoDestinatario.getUserId());
        grupoDestinatarioImpl.setNome(grupoDestinatario.getNome());
        grupoDestinatarioImpl.setFinalidade(grupoDestinatario.getFinalidade());
        grupoDestinatarioImpl.setUserIdResponsaveis(grupoDestinatario.getUserIdResponsaveis());
        grupoDestinatarioImpl.setListaTemporaria(grupoDestinatario.isListaTemporaria());
        grupoDestinatarioImpl.setDataFinalListaTemporaria(grupoDestinatario.getDataFinalListaTemporaria());
        grupoDestinatarioImpl.setEnvioEmNomeLista(grupoDestinatario.isEnvioEmNomeLista());
        grupoDestinatarioImpl.setUserIdNomesLista(grupoDestinatario.getUserIdNomesLista());
        grupoDestinatarioImpl.setListaModerada(grupoDestinatario.isListaModerada());
        grupoDestinatarioImpl.setUserIdListaModerada(grupoDestinatario.getUserIdListaModerada());
        grupoDestinatarioImpl.setAtivo(grupoDestinatario.isAtivo());
        grupoDestinatarioImpl.setDataCriacao(grupoDestinatario.getDataCriacao());
        grupoDestinatarioImpl.setDataAtualizacao(grupoDestinatario.getDataAtualizacao());
        grupoDestinatarioImpl.setTipoLista(grupoDestinatario.getTipoLista());

        return grupoDestinatarioImpl;
    }

    /**
     * Returns the grupo destinatario with the primary key or throws a {@link com.liferay.portal.NoSuchModelException} if it could not be found.
     *
     * @param primaryKey the primary key of the grupo destinatario
     * @return the grupo destinatario
     * @throws br.mp.mprj.portal.portlet.gestaomensagens.service.NoSuchGrupoDestinatarioException if a grupo destinatario with the primary key could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public GrupoDestinatario findByPrimaryKey(Serializable primaryKey)
        throws NoSuchGrupoDestinatarioException, SystemException {
        GrupoDestinatario grupoDestinatario = fetchByPrimaryKey(primaryKey);

        if (grupoDestinatario == null) {
            if (_log.isWarnEnabled()) {
                _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
            }

            throw new NoSuchGrupoDestinatarioException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
                primaryKey);
        }

        return grupoDestinatario;
    }

    /**
     * Returns the grupo destinatario with the primary key or throws a {@link br.mp.mprj.portal.portlet.gestaomensagens.service.NoSuchGrupoDestinatarioException} if it could not be found.
     *
     * @param grupoDestinatarioId the primary key of the grupo destinatario
     * @return the grupo destinatario
     * @throws br.mp.mprj.portal.portlet.gestaomensagens.service.NoSuchGrupoDestinatarioException if a grupo destinatario with the primary key could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public GrupoDestinatario findByPrimaryKey(long grupoDestinatarioId)
        throws NoSuchGrupoDestinatarioException, SystemException {
        return findByPrimaryKey((Serializable) grupoDestinatarioId);
    }

    /**
     * Returns the grupo destinatario with the primary key or returns <code>null</code> if it could not be found.
     *
     * @param primaryKey the primary key of the grupo destinatario
     * @return the grupo destinatario, or <code>null</code> if a grupo destinatario with the primary key could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public GrupoDestinatario fetchByPrimaryKey(Serializable primaryKey)
        throws SystemException {
        GrupoDestinatario grupoDestinatario = (GrupoDestinatario) EntityCacheUtil.getResult(GrupoDestinatarioModelImpl.ENTITY_CACHE_ENABLED,
                GrupoDestinatarioImpl.class, primaryKey);

        if (grupoDestinatario == _nullGrupoDestinatario) {
            return null;
        }

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

            try {
                session = openSession();

                grupoDestinatario = (GrupoDestinatario) session.get(GrupoDestinatarioImpl.class,
                        primaryKey);

                if (grupoDestinatario != null) {
                    cacheResult(grupoDestinatario);
                } else {
                    EntityCacheUtil.putResult(GrupoDestinatarioModelImpl.ENTITY_CACHE_ENABLED,
                        GrupoDestinatarioImpl.class, primaryKey,
                        _nullGrupoDestinatario);
                }
            } catch (Exception e) {
                EntityCacheUtil.removeResult(GrupoDestinatarioModelImpl.ENTITY_CACHE_ENABLED,
                    GrupoDestinatarioImpl.class, primaryKey);

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

        return grupoDestinatario;
    }

    /**
     * Returns the grupo destinatario with the primary key or returns <code>null</code> if it could not be found.
     *
     * @param grupoDestinatarioId the primary key of the grupo destinatario
     * @return the grupo destinatario, or <code>null</code> if a grupo destinatario with the primary key could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public GrupoDestinatario fetchByPrimaryKey(long grupoDestinatarioId)
        throws SystemException {
        return fetchByPrimaryKey((Serializable) grupoDestinatarioId);
    }

    /**
     * Returns all the grupo destinatarios.
     *
     * @return the grupo destinatarios
     * @throws SystemException if a system exception occurred
     */
    @Override
    public List<GrupoDestinatario> findAll() throws SystemException {
        return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
    }

    /**
     * Returns a range of all the grupo destinatarios.
     *
     * <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 com.liferay.portal.kernel.dao.orm.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 com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link br.mp.mprj.portal.portlet.gestaomensagens.service.model.impl.GrupoDestinatarioModelImpl}. 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 grupo destinatarios
     * @param end the upper bound of the range of grupo destinatarios (not inclusive)
     * @return the range of grupo destinatarios
     * @throws SystemException if a system exception occurred
     */
    @Override
    public List<GrupoDestinatario> findAll(int start, int end)
        throws SystemException {
        return findAll(start, end, null);
    }

    /**
     * Returns an ordered range of all the grupo destinatarios.
     *
     * <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 com.liferay.portal.kernel.dao.orm.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 com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link br.mp.mprj.portal.portlet.gestaomensagens.service.model.impl.GrupoDestinatarioModelImpl}. 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 grupo destinatarios
     * @param end the upper bound of the range of grupo destinatarios (not inclusive)
     * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
     * @return the ordered range of grupo destinatarios
     * @throws SystemException if a system exception occurred
     */
    @Override
    public List<GrupoDestinatario> findAll(int start, int end,
        OrderByComparator orderByComparator) throws SystemException {
        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<GrupoDestinatario> list = (List<GrupoDestinatario>) FinderCacheUtil.getResult(finderPath,
                finderArgs, this);

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

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

                query.append(_SQL_SELECT_GRUPODESTINATARIO);

                appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
                    orderByComparator);

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

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

            Session session = null;

            try {
                session = openSession();

                Query q = session.createQuery(sql);

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

                    Collections.sort(list);

                    list = new UnmodifiableList<GrupoDestinatario>(list);
                } else {
                    list = (List<GrupoDestinatario>) QueryUtil.list(q,
                            getDialect(), start, end);
                }

                cacheResult(list);

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

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

        return list;
    }

    /**
     * Removes all the grupo destinatarios from the database.
     *
     * @throws SystemException if a system exception occurred
     */
    @Override
    public void removeAll() throws SystemException {
        for (GrupoDestinatario grupoDestinatario : findAll()) {
            remove(grupoDestinatario);
        }
    }

    /**
     * Returns the number of grupo destinatarios.
     *
     * @return the number of grupo destinatarios
     * @throws SystemException if a system exception occurred
     */
    @Override
    public int countAll() throws SystemException {
        Long count = (Long) FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
                FINDER_ARGS_EMPTY, this);

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

            try {
                session = openSession();

                Query q = session.createQuery(_SQL_COUNT_GRUPODESTINATARIO);

                count = (Long) q.uniqueResult();

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

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

        return count.intValue();
    }

    @Override
    protected Set<String> getBadColumnNames() {
        return _badColumnNames;
    }

    /**
     * Initializes the grupo destinatario persistence.
     */
    public void afterPropertiesSet() {
        String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
                    com.liferay.util.service.ServiceProps.get(
                        "value.object.listener.br.mp.mprj.portal.portlet.gestaomensagens.service.model.GrupoDestinatario")));

        if (listenerClassNames.length > 0) {
            try {
                List<ModelListener<GrupoDestinatario>> listenersList = new ArrayList<ModelListener<GrupoDestinatario>>();

                for (String listenerClassName : listenerClassNames) {
                    listenersList.add((ModelListener<GrupoDestinatario>) InstanceFactory.newInstance(
                            getClassLoader(), listenerClassName));
                }

                listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
            } catch (Exception e) {
                _log.error(e);
            }
        }
    }

    public void destroy() {
        EntityCacheUtil.removeCache(GrupoDestinatarioImpl.class.getName());
        FinderCacheUtil.removeCache(FINDER_CLASS_NAME_ENTITY);
        FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
        FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
    }
}
