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

import br.mp.mprj.portal.portlet.gestaomensagens.service.NoSuchAgendamentoException;
import br.mp.mprj.portal.portlet.gestaomensagens.service.model.Agendamento;
import br.mp.mprj.portal.portlet.gestaomensagens.service.model.impl.AgendamentoImpl;
import br.mp.mprj.portal.portlet.gestaomensagens.service.model.impl.AgendamentoModelImpl;
import br.mp.mprj.portal.portlet.gestaomensagens.service.service.persistence.AgendamentoPersistence;

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.CalendarUtil;
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.Date;
import java.util.List;
import java.util.Set;

/**
 * The persistence implementation for the agendamento service.
 *
 * <p>
 * Caching information and settings can be found in <code>portal.properties</code>
 * </p>
 *
 * @author Henrique Simoes
 * @see AgendamentoPersistence
 * @see AgendamentoUtil
 * @generated
 */
public class AgendamentoPersistenceImpl extends BasePersistenceImpl<Agendamento>
    implements AgendamentoPersistence {
    /*
     * NOTE FOR DEVELOPERS:
     *
     * Never modify or reference this class directly. Always use {@link AgendamentoUtil} to access the agendamento persistence. Modify <code>service.xml</code> and rerun ServiceBuilder to regenerate this class.
     */
    public static final String FINDER_CLASS_NAME_ENTITY = AgendamentoImpl.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(AgendamentoModelImpl.ENTITY_CACHE_ENABLED,
            AgendamentoModelImpl.FINDER_CACHE_ENABLED, AgendamentoImpl.class,
            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findAll", new String[0]);
    public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL = new FinderPath(AgendamentoModelImpl.ENTITY_CACHE_ENABLED,
            AgendamentoModelImpl.FINDER_CACHE_ENABLED, AgendamentoImpl.class,
            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findAll", new String[0]);
    public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(AgendamentoModelImpl.ENTITY_CACHE_ENABLED,
            AgendamentoModelImpl.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(AgendamentoModelImpl.ENTITY_CACHE_ENABLED,
            AgendamentoModelImpl.FINDER_CACHE_ENABLED, AgendamentoImpl.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(AgendamentoModelImpl.ENTITY_CACHE_ENABLED,
            AgendamentoModelImpl.FINDER_CACHE_ENABLED, AgendamentoImpl.class,
            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findByUuid",
            new String[] { String.class.getName() },
            AgendamentoModelImpl.UUID_COLUMN_BITMASK |
            AgendamentoModelImpl.DATAINICIO_COLUMN_BITMASK);
    public static final FinderPath FINDER_PATH_COUNT_BY_UUID = new FinderPath(AgendamentoModelImpl.ENTITY_CACHE_ENABLED,
            AgendamentoModelImpl.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 = "agendamento.uuid IS NULL";
    private static final String _FINDER_COLUMN_UUID_UUID_2 = "agendamento.uuid = ?";
    private static final String _FINDER_COLUMN_UUID_UUID_3 = "(agendamento.uuid IS NULL OR agendamento.uuid = '')";
    public static final FinderPath FINDER_PATH_FETCH_BY_UUID_G = new FinderPath(AgendamentoModelImpl.ENTITY_CACHE_ENABLED,
            AgendamentoModelImpl.FINDER_CACHE_ENABLED, AgendamentoImpl.class,
            FINDER_CLASS_NAME_ENTITY, "fetchByUUID_G",
            new String[] { String.class.getName(), Long.class.getName() },
            AgendamentoModelImpl.UUID_COLUMN_BITMASK |
            AgendamentoModelImpl.GROUPID_COLUMN_BITMASK);
    public static final FinderPath FINDER_PATH_COUNT_BY_UUID_G = new FinderPath(AgendamentoModelImpl.ENTITY_CACHE_ENABLED,
            AgendamentoModelImpl.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 = "agendamento.uuid IS NULL AND ";
    private static final String _FINDER_COLUMN_UUID_G_UUID_2 = "agendamento.uuid = ? AND ";
    private static final String _FINDER_COLUMN_UUID_G_UUID_3 = "(agendamento.uuid IS NULL OR agendamento.uuid = '') AND ";
    private static final String _FINDER_COLUMN_UUID_G_GROUPID_2 = "agendamento.groupId = ?";
    public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_UUID_C = new FinderPath(AgendamentoModelImpl.ENTITY_CACHE_ENABLED,
            AgendamentoModelImpl.FINDER_CACHE_ENABLED, AgendamentoImpl.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(AgendamentoModelImpl.ENTITY_CACHE_ENABLED,
            AgendamentoModelImpl.FINDER_CACHE_ENABLED, AgendamentoImpl.class,
            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findByUuid_C",
            new String[] { String.class.getName(), Long.class.getName() },
            AgendamentoModelImpl.UUID_COLUMN_BITMASK |
            AgendamentoModelImpl.COMPANYID_COLUMN_BITMASK |
            AgendamentoModelImpl.DATAINICIO_COLUMN_BITMASK);
    public static final FinderPath FINDER_PATH_COUNT_BY_UUID_C = new FinderPath(AgendamentoModelImpl.ENTITY_CACHE_ENABLED,
            AgendamentoModelImpl.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 = "agendamento.uuid IS NULL AND ";
    private static final String _FINDER_COLUMN_UUID_C_UUID_2 = "agendamento.uuid = ? AND ";
    private static final String _FINDER_COLUMN_UUID_C_UUID_3 = "(agendamento.uuid IS NULL OR agendamento.uuid = '') AND ";
    private static final String _FINDER_COLUMN_UUID_C_COMPANYID_2 = "agendamento.companyId = ?";
    public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_MENSAGEMTIPO =
        new FinderPath(AgendamentoModelImpl.ENTITY_CACHE_ENABLED,
            AgendamentoModelImpl.FINDER_CACHE_ENABLED, AgendamentoImpl.class,
            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findByMensagemTipo",
            new String[] {
                Long.class.getName(), Integer.class.getName(),
                
            Integer.class.getName(), Integer.class.getName(),
                OrderByComparator.class.getName()
            });
    public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_MENSAGEMTIPO =
        new FinderPath(AgendamentoModelImpl.ENTITY_CACHE_ENABLED,
            AgendamentoModelImpl.FINDER_CACHE_ENABLED, AgendamentoImpl.class,
            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findByMensagemTipo",
            new String[] { Long.class.getName(), Integer.class.getName() },
            AgendamentoModelImpl.MENSAGEMID_COLUMN_BITMASK |
            AgendamentoModelImpl.TIPOAGENDAMENTO_COLUMN_BITMASK |
            AgendamentoModelImpl.DATAINICIO_COLUMN_BITMASK);
    public static final FinderPath FINDER_PATH_COUNT_BY_MENSAGEMTIPO = new FinderPath(AgendamentoModelImpl.ENTITY_CACHE_ENABLED,
            AgendamentoModelImpl.FINDER_CACHE_ENABLED, Long.class,
            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByMensagemTipo",
            new String[] { Long.class.getName(), Integer.class.getName() });
    private static final String _FINDER_COLUMN_MENSAGEMTIPO_MENSAGEMID_2 = "agendamento.mensagemId = ? AND ";
    private static final String _FINDER_COLUMN_MENSAGEMTIPO_TIPOAGENDAMENTO_2 = "agendamento.tipoAgendamento = ?";
    public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_MENSAGEMTIPOMULTIPO =
        new FinderPath(AgendamentoModelImpl.ENTITY_CACHE_ENABLED,
            AgendamentoModelImpl.FINDER_CACHE_ENABLED, AgendamentoImpl.class,
            FINDER_CLASS_NAME_LIST_WITH_PAGINATION,
            "findByMensagemTipoMultipo",
            new String[] {
                Long.class.getName(), Integer.class.getName(),
                
            Integer.class.getName(), Integer.class.getName(),
                OrderByComparator.class.getName()
            });
    public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_MENSAGEMTIPOMULTIPO =
        new FinderPath(AgendamentoModelImpl.ENTITY_CACHE_ENABLED,
            AgendamentoModelImpl.FINDER_CACHE_ENABLED, AgendamentoImpl.class,
            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION,
            "findByMensagemTipoMultipo",
            new String[] { Long.class.getName(), Integer.class.getName() },
            AgendamentoModelImpl.MENSAGEMID_COLUMN_BITMASK |
            AgendamentoModelImpl.TIPOAGENDAMENTO_COLUMN_BITMASK |
            AgendamentoModelImpl.DATAINICIO_COLUMN_BITMASK);
    public static final FinderPath FINDER_PATH_COUNT_BY_MENSAGEMTIPOMULTIPO = new FinderPath(AgendamentoModelImpl.ENTITY_CACHE_ENABLED,
            AgendamentoModelImpl.FINDER_CACHE_ENABLED, Long.class,
            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION,
            "countByMensagemTipoMultipo",
            new String[] { Long.class.getName(), Integer.class.getName() });
    public static final FinderPath FINDER_PATH_WITH_PAGINATION_COUNT_BY_MENSAGEMTIPOMULTIPO =
        new FinderPath(AgendamentoModelImpl.ENTITY_CACHE_ENABLED,
            AgendamentoModelImpl.FINDER_CACHE_ENABLED, Long.class,
            FINDER_CLASS_NAME_LIST_WITH_PAGINATION,
            "countByMensagemTipoMultipo",
            new String[] { Long.class.getName(), Integer.class.getName() });
    private static final String _FINDER_COLUMN_MENSAGEMTIPOMULTIPO_MENSAGEMID_2 = "agendamento.mensagemId = ? AND ";
    private static final String _FINDER_COLUMN_MENSAGEMTIPOMULTIPO_MENSAGEMID_5 = "(" +
        removeConjunction(_FINDER_COLUMN_MENSAGEMTIPOMULTIPO_MENSAGEMID_2) +
        ")";
    private static final String _FINDER_COLUMN_MENSAGEMTIPOMULTIPO_TIPOAGENDAMENTO_2 =
        "agendamento.tipoAgendamento = ?";
    private static final String _FINDER_COLUMN_MENSAGEMTIPOMULTIPO_TIPOAGENDAMENTO_5 =
        "(" +
        removeConjunction(_FINDER_COLUMN_MENSAGEMTIPOMULTIPO_TIPOAGENDAMENTO_2) +
        ")";
    public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_MENSAGEMDATACALENDARIO =
        new FinderPath(AgendamentoModelImpl.ENTITY_CACHE_ENABLED,
            AgendamentoModelImpl.FINDER_CACHE_ENABLED, AgendamentoImpl.class,
            FINDER_CLASS_NAME_LIST_WITH_PAGINATION,
            "findByMensagemDataCalendario",
            new String[] {
                Long.class.getName(), Date.class.getName(),
                
            Integer.class.getName(), Integer.class.getName(),
                OrderByComparator.class.getName()
            });
    public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_MENSAGEMDATACALENDARIO =
        new FinderPath(AgendamentoModelImpl.ENTITY_CACHE_ENABLED,
            AgendamentoModelImpl.FINDER_CACHE_ENABLED, AgendamentoImpl.class,
            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION,
            "findByMensagemDataCalendario",
            new String[] { Long.class.getName(), Date.class.getName() },
            AgendamentoModelImpl.MENSAGEMID_COLUMN_BITMASK |
            AgendamentoModelImpl.DATACALENDARIO_COLUMN_BITMASK |
            AgendamentoModelImpl.DATAINICIO_COLUMN_BITMASK);
    public static final FinderPath FINDER_PATH_COUNT_BY_MENSAGEMDATACALENDARIO = new FinderPath(AgendamentoModelImpl.ENTITY_CACHE_ENABLED,
            AgendamentoModelImpl.FINDER_CACHE_ENABLED, Long.class,
            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION,
            "countByMensagemDataCalendario",
            new String[] { Long.class.getName(), Date.class.getName() });
    private static final String _FINDER_COLUMN_MENSAGEMDATACALENDARIO_MENSAGEMID_2 =
        "agendamento.mensagemId = ? AND ";
    private static final String _FINDER_COLUMN_MENSAGEMDATACALENDARIO_DATACALENDARIO_1 =
        "agendamento.dataCalendario IS NULL";
    private static final String _FINDER_COLUMN_MENSAGEMDATACALENDARIO_DATACALENDARIO_2 =
        "agendamento.dataCalendario = ?";
    public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_SITUACAO = new FinderPath(AgendamentoModelImpl.ENTITY_CACHE_ENABLED,
            AgendamentoModelImpl.FINDER_CACHE_ENABLED, AgendamentoImpl.class,
            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findBySituacao",
            new String[] {
                Integer.class.getName(),
                
            Integer.class.getName(), Integer.class.getName(),
                OrderByComparator.class.getName()
            });
    public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_SITUACAO =
        new FinderPath(AgendamentoModelImpl.ENTITY_CACHE_ENABLED,
            AgendamentoModelImpl.FINDER_CACHE_ENABLED, AgendamentoImpl.class,
            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findBySituacao",
            new String[] { Integer.class.getName() },
            AgendamentoModelImpl.SITUACAOENVIO_COLUMN_BITMASK |
            AgendamentoModelImpl.DATAINICIO_COLUMN_BITMASK);
    public static final FinderPath FINDER_PATH_COUNT_BY_SITUACAO = new FinderPath(AgendamentoModelImpl.ENTITY_CACHE_ENABLED,
            AgendamentoModelImpl.FINDER_CACHE_ENABLED, Long.class,
            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countBySituacao",
            new String[] { Integer.class.getName() });
    private static final String _FINDER_COLUMN_SITUACAO_SITUACAOENVIO_2 = "agendamento.situacaoEnvio = ?";
    public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_SITUACAOMENSAGEM =
        new FinderPath(AgendamentoModelImpl.ENTITY_CACHE_ENABLED,
            AgendamentoModelImpl.FINDER_CACHE_ENABLED, AgendamentoImpl.class,
            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findBySituacaoMensagem",
            new String[] {
                Integer.class.getName(), Long.class.getName(),
                
            Integer.class.getName(), Integer.class.getName(),
                OrderByComparator.class.getName()
            });
    public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_SITUACAOMENSAGEM =
        new FinderPath(AgendamentoModelImpl.ENTITY_CACHE_ENABLED,
            AgendamentoModelImpl.FINDER_CACHE_ENABLED, AgendamentoImpl.class,
            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION,
            "findBySituacaoMensagem",
            new String[] { Integer.class.getName(), Long.class.getName() },
            AgendamentoModelImpl.SITUACAOENVIO_COLUMN_BITMASK |
            AgendamentoModelImpl.MENSAGEMID_COLUMN_BITMASK |
            AgendamentoModelImpl.DATAINICIO_COLUMN_BITMASK);
    public static final FinderPath FINDER_PATH_COUNT_BY_SITUACAOMENSAGEM = new FinderPath(AgendamentoModelImpl.ENTITY_CACHE_ENABLED,
            AgendamentoModelImpl.FINDER_CACHE_ENABLED, Long.class,
            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION,
            "countBySituacaoMensagem",
            new String[] { Integer.class.getName(), Long.class.getName() });
    private static final String _FINDER_COLUMN_SITUACAOMENSAGEM_SITUACAOENVIO_2 = "agendamento.situacaoEnvio = ? AND ";
    private static final String _FINDER_COLUMN_SITUACAOMENSAGEM_MENSAGEMID_2 = "agendamento.mensagemId = ?";
    public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_MENSAGEMID =
        new FinderPath(AgendamentoModelImpl.ENTITY_CACHE_ENABLED,
            AgendamentoModelImpl.FINDER_CACHE_ENABLED, AgendamentoImpl.class,
            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findByMensagemId",
            new String[] {
                Long.class.getName(),
                
            Integer.class.getName(), Integer.class.getName(),
                OrderByComparator.class.getName()
            });
    public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_MENSAGEMID =
        new FinderPath(AgendamentoModelImpl.ENTITY_CACHE_ENABLED,
            AgendamentoModelImpl.FINDER_CACHE_ENABLED, AgendamentoImpl.class,
            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findByMensagemId",
            new String[] { Long.class.getName() },
            AgendamentoModelImpl.MENSAGEMID_COLUMN_BITMASK |
            AgendamentoModelImpl.DATAINICIO_COLUMN_BITMASK);
    public static final FinderPath FINDER_PATH_COUNT_BY_MENSAGEMID = new FinderPath(AgendamentoModelImpl.ENTITY_CACHE_ENABLED,
            AgendamentoModelImpl.FINDER_CACHE_ENABLED, Long.class,
            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByMensagemId",
            new String[] { Long.class.getName() });
    private static final String _FINDER_COLUMN_MENSAGEMID_MENSAGEMID_2 = "agendamento.mensagemId = ?";
    public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_MENSAGEMIDSITUACAOENVIO =
        new FinderPath(AgendamentoModelImpl.ENTITY_CACHE_ENABLED,
            AgendamentoModelImpl.FINDER_CACHE_ENABLED, AgendamentoImpl.class,
            FINDER_CLASS_NAME_LIST_WITH_PAGINATION,
            "findByMensagemIdSituacaoEnvio",
            new String[] {
                Long.class.getName(), Integer.class.getName(),
                
            Integer.class.getName(), Integer.class.getName(),
                OrderByComparator.class.getName()
            });
    public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_MENSAGEMIDSITUACAOENVIO =
        new FinderPath(AgendamentoModelImpl.ENTITY_CACHE_ENABLED,
            AgendamentoModelImpl.FINDER_CACHE_ENABLED, AgendamentoImpl.class,
            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION,
            "findByMensagemIdSituacaoEnvio",
            new String[] { Long.class.getName(), Integer.class.getName() },
            AgendamentoModelImpl.MENSAGEMID_COLUMN_BITMASK |
            AgendamentoModelImpl.SITUACAOENVIO_COLUMN_BITMASK |
            AgendamentoModelImpl.DATAINICIO_COLUMN_BITMASK);
    public static final FinderPath FINDER_PATH_COUNT_BY_MENSAGEMIDSITUACAOENVIO = new FinderPath(AgendamentoModelImpl.ENTITY_CACHE_ENABLED,
            AgendamentoModelImpl.FINDER_CACHE_ENABLED, Long.class,
            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION,
            "countByMensagemIdSituacaoEnvio",
            new String[] { Long.class.getName(), Integer.class.getName() });
    private static final String _FINDER_COLUMN_MENSAGEMIDSITUACAOENVIO_MENSAGEMID_2 =
        "agendamento.mensagemId = ? AND ";
    private static final String _FINDER_COLUMN_MENSAGEMIDSITUACAOENVIO_SITUACAOENVIO_2 =
        "agendamento.situacaoEnvio = ?";
    public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_AGENDAMENTOEXCLUSAO =
        new FinderPath(AgendamentoModelImpl.ENTITY_CACHE_ENABLED,
            AgendamentoModelImpl.FINDER_CACHE_ENABLED, AgendamentoImpl.class,
            FINDER_CLASS_NAME_LIST_WITH_PAGINATION,
            "findByAgendamentoExclusao",
            new String[] {
                Long.class.getName(), Integer.class.getName(),
                Integer.class.getName(), Long.class.getName(),
                
            Integer.class.getName(), Integer.class.getName(),
                OrderByComparator.class.getName()
            });
    public static final FinderPath FINDER_PATH_WITH_PAGINATION_COUNT_BY_AGENDAMENTOEXCLUSAO =
        new FinderPath(AgendamentoModelImpl.ENTITY_CACHE_ENABLED,
            AgendamentoModelImpl.FINDER_CACHE_ENABLED, Long.class,
            FINDER_CLASS_NAME_LIST_WITH_PAGINATION,
            "countByAgendamentoExclusao",
            new String[] {
                Long.class.getName(), Integer.class.getName(),
                Integer.class.getName(), Long.class.getName()
            });
    private static final String _FINDER_COLUMN_AGENDAMENTOEXCLUSAO_MENSAGEMID_2 = "agendamento.mensagemId = ? AND ";
    private static final String _FINDER_COLUMN_AGENDAMENTOEXCLUSAO_MENSAGEMID_5 = "(" +
        removeConjunction(_FINDER_COLUMN_AGENDAMENTOEXCLUSAO_MENSAGEMID_2) +
        ")";
    private static final String _FINDER_COLUMN_AGENDAMENTOEXCLUSAO_TIPOAGENDAMENTO_2 =
        "agendamento.tipoAgendamento = ? AND ";
    private static final String _FINDER_COLUMN_AGENDAMENTOEXCLUSAO_TIPOAGENDAMENTO_5 =
        "(" +
        removeConjunction(_FINDER_COLUMN_AGENDAMENTOEXCLUSAO_TIPOAGENDAMENTO_2) +
        ")";
    private static final String _FINDER_COLUMN_AGENDAMENTOEXCLUSAO_SITUACAOENVIO_2 =
        "agendamento.situacaoEnvio = ? AND ";
    private static final String _FINDER_COLUMN_AGENDAMENTOEXCLUSAO_SITUACAOENVIO_5 =
        "(" +
        removeConjunction(_FINDER_COLUMN_AGENDAMENTOEXCLUSAO_SITUACAOENVIO_2) +
        ")";
    private static final String _FINDER_COLUMN_AGENDAMENTOEXCLUSAO_AGENDAMENTOID_2 =
        "agendamento.agendamentoId != ?";
    private static final String _FINDER_COLUMN_AGENDAMENTOEXCLUSAO_AGENDAMENTOID_5 =
        "(" +
        removeConjunction(_FINDER_COLUMN_AGENDAMENTOEXCLUSAO_AGENDAMENTOID_2) +
        ")";
    private static final String _SQL_SELECT_AGENDAMENTO = "SELECT agendamento FROM Agendamento agendamento";
    private static final String _SQL_SELECT_AGENDAMENTO_WHERE = "SELECT agendamento FROM Agendamento agendamento WHERE ";
    private static final String _SQL_COUNT_AGENDAMENTO = "SELECT COUNT(agendamento) FROM Agendamento agendamento";
    private static final String _SQL_COUNT_AGENDAMENTO_WHERE = "SELECT COUNT(agendamento) FROM Agendamento agendamento WHERE ";
    private static final String _ORDER_BY_ENTITY_ALIAS = "agendamento.";
    private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No Agendamento exists with the primary key ";
    private static final String _NO_SUCH_ENTITY_WITH_KEY = "No Agendamento 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(AgendamentoPersistenceImpl.class);
    private static Set<String> _badColumnNames = SetUtil.fromArray(new String[] {
                "uuid"
            });
    private static Agendamento _nullAgendamento = new AgendamentoImpl() {
            @Override
            public Object clone() {
                return this;
            }

            @Override
            public CacheModel<Agendamento> toCacheModel() {
                return _nullAgendamentoCacheModel;
            }
        };

    private static CacheModel<Agendamento> _nullAgendamentoCacheModel = new CacheModel<Agendamento>() {
            @Override
            public Agendamento toEntityModel() {
                return _nullAgendamento;
            }
        };

    public AgendamentoPersistenceImpl() {
        setModelClass(Agendamento.class);
    }

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

    /**
     * Returns a range of all the agendamentos 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.AgendamentoModelImpl}. 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 agendamentos
     * @param end the upper bound of the range of agendamentos (not inclusive)
     * @return the range of matching agendamentos
     * @throws SystemException if a system exception occurred
     */
    @Override
    public List<Agendamento> findByUuid(String uuid, int start, int end)
        throws SystemException {
        return findByUuid(uuid, start, end, null);
    }

    /**
     * Returns an ordered range of all the agendamentos 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.AgendamentoModelImpl}. 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 agendamentos
     * @param end the upper bound of the range of agendamentos (not inclusive)
     * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
     * @return the ordered range of matching agendamentos
     * @throws SystemException if a system exception occurred
     */
    @Override
    public List<Agendamento> 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<Agendamento> list = (List<Agendamento>) FinderCacheUtil.getResult(finderPath,
                finderArgs, this);

        if ((list != null) && !list.isEmpty()) {
            for (Agendamento agendamento : list) {
                if (!Validator.equals(uuid, agendamento.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_AGENDAMENTO_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(AgendamentoModelImpl.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<Agendamento>) QueryUtil.list(q, getDialect(),
                            start, end, false);

                    Collections.sort(list);

                    list = new UnmodifiableList<Agendamento>(list);
                } else {
                    list = (List<Agendamento>) 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 agendamento 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 agendamento
     * @throws br.mp.mprj.portal.portlet.gestaomensagens.service.NoSuchAgendamentoException if a matching agendamento could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public Agendamento findByUuid_First(String uuid,
        OrderByComparator orderByComparator)
        throws NoSuchAgendamentoException, SystemException {
        Agendamento agendamento = fetchByUuid_First(uuid, orderByComparator);

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

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

    /**
     * Returns the first agendamento 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 agendamento, or <code>null</code> if a matching agendamento could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public Agendamento fetchByUuid_First(String uuid,
        OrderByComparator orderByComparator) throws SystemException {
        List<Agendamento> list = findByUuid(uuid, 0, 1, orderByComparator);

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

        return null;
    }

    /**
     * Returns the last agendamento 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 agendamento
     * @throws br.mp.mprj.portal.portlet.gestaomensagens.service.NoSuchAgendamentoException if a matching agendamento could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public Agendamento findByUuid_Last(String uuid,
        OrderByComparator orderByComparator)
        throws NoSuchAgendamentoException, SystemException {
        Agendamento agendamento = fetchByUuid_Last(uuid, orderByComparator);

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

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

    /**
     * Returns the last agendamento 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 agendamento, or <code>null</code> if a matching agendamento could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public Agendamento fetchByUuid_Last(String uuid,
        OrderByComparator orderByComparator) throws SystemException {
        int count = countByUuid(uuid);

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

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

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

        return null;
    }

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

        Session session = null;

        try {
            session = openSession();

            Agendamento[] array = new AgendamentoImpl[3];

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

            array[1] = agendamento;

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

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

    protected Agendamento getByUuid_PrevAndNext(Session session,
        Agendamento agendamento, 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_AGENDAMENTO_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(AgendamentoModelImpl.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(agendamento);

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

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

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

    /**
     * Removes all the agendamentos 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 (Agendamento agendamento : findByUuid(uuid, QueryUtil.ALL_POS,
                QueryUtil.ALL_POS, null)) {
            remove(agendamento);
        }
    }

    /**
     * Returns the number of agendamentos where uuid = &#63;.
     *
     * @param uuid the uuid
     * @return the number of matching agendamentos
     * @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_AGENDAMENTO_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 agendamento where uuid = &#63; and groupId = &#63; or throws a {@link br.mp.mprj.portal.portlet.gestaomensagens.service.NoSuchAgendamentoException} if it could not be found.
     *
     * @param uuid the uuid
     * @param groupId the group ID
     * @return the matching agendamento
     * @throws br.mp.mprj.portal.portlet.gestaomensagens.service.NoSuchAgendamentoException if a matching agendamento could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public Agendamento findByUUID_G(String uuid, long groupId)
        throws NoSuchAgendamentoException, SystemException {
        Agendamento agendamento = fetchByUUID_G(uuid, groupId);

        if (agendamento == 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 NoSuchAgendamentoException(msg.toString());
        }

        return agendamento;
    }

    /**
     * Returns the agendamento 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 agendamento, or <code>null</code> if a matching agendamento could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public Agendamento fetchByUUID_G(String uuid, long groupId)
        throws SystemException {
        return fetchByUUID_G(uuid, groupId, true);
    }

    /**
     * Returns the agendamento 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 agendamento, or <code>null</code> if a matching agendamento could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public Agendamento 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 Agendamento) {
            Agendamento agendamento = (Agendamento) result;

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

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

            query.append(_SQL_SELECT_AGENDAMENTO_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<Agendamento> list = q.list();

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

                    result = agendamento;

                    cacheResult(agendamento);

                    if ((agendamento.getUuid() == null) ||
                            !agendamento.getUuid().equals(uuid) ||
                            (agendamento.getGroupId() != groupId)) {
                        FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_UUID_G,
                            finderArgs, agendamento);
                    }
                }
            } 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 (Agendamento) result;
        }
    }

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

        return remove(agendamento);
    }

    /**
     * Returns the number of agendamentos where uuid = &#63; and groupId = &#63;.
     *
     * @param uuid the uuid
     * @param groupId the group ID
     * @return the number of matching agendamentos
     * @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_AGENDAMENTO_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 agendamentos where uuid = &#63; and companyId = &#63;.
     *
     * @param uuid the uuid
     * @param companyId the company ID
     * @return the matching agendamentos
     * @throws SystemException if a system exception occurred
     */
    @Override
    public List<Agendamento> 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 agendamentos 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.AgendamentoModelImpl}. 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 agendamentos
     * @param end the upper bound of the range of agendamentos (not inclusive)
     * @return the range of matching agendamentos
     * @throws SystemException if a system exception occurred
     */
    @Override
    public List<Agendamento> 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 agendamentos 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.AgendamentoModelImpl}. 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 agendamentos
     * @param end the upper bound of the range of agendamentos (not inclusive)
     * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
     * @return the ordered range of matching agendamentos
     * @throws SystemException if a system exception occurred
     */
    @Override
    public List<Agendamento> 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<Agendamento> list = (List<Agendamento>) FinderCacheUtil.getResult(finderPath,
                finderArgs, this);

        if ((list != null) && !list.isEmpty()) {
            for (Agendamento agendamento : list) {
                if (!Validator.equals(uuid, agendamento.getUuid()) ||
                        (companyId != agendamento.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_AGENDAMENTO_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(AgendamentoModelImpl.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<Agendamento>) QueryUtil.list(q, getDialect(),
                            start, end, false);

                    Collections.sort(list);

                    list = new UnmodifiableList<Agendamento>(list);
                } else {
                    list = (List<Agendamento>) 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 agendamento 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 agendamento
     * @throws br.mp.mprj.portal.portlet.gestaomensagens.service.NoSuchAgendamentoException if a matching agendamento could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public Agendamento findByUuid_C_First(String uuid, long companyId,
        OrderByComparator orderByComparator)
        throws NoSuchAgendamentoException, SystemException {
        Agendamento agendamento = fetchByUuid_C_First(uuid, companyId,
                orderByComparator);

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

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

    /**
     * Returns the first agendamento 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 agendamento, or <code>null</code> if a matching agendamento could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public Agendamento fetchByUuid_C_First(String uuid, long companyId,
        OrderByComparator orderByComparator) throws SystemException {
        List<Agendamento> list = findByUuid_C(uuid, companyId, 0, 1,
                orderByComparator);

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

        return null;
    }

    /**
     * Returns the last agendamento 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 agendamento
     * @throws br.mp.mprj.portal.portlet.gestaomensagens.service.NoSuchAgendamentoException if a matching agendamento could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public Agendamento findByUuid_C_Last(String uuid, long companyId,
        OrderByComparator orderByComparator)
        throws NoSuchAgendamentoException, SystemException {
        Agendamento agendamento = fetchByUuid_C_Last(uuid, companyId,
                orderByComparator);

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

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

    /**
     * Returns the last agendamento 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 agendamento, or <code>null</code> if a matching agendamento could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public Agendamento fetchByUuid_C_Last(String uuid, long companyId,
        OrderByComparator orderByComparator) throws SystemException {
        int count = countByUuid_C(uuid, companyId);

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

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

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

        return null;
    }

    /**
     * Returns the agendamentos before and after the current agendamento in the ordered set where uuid = &#63; and companyId = &#63;.
     *
     * @param agendamentoId the primary key of the current agendamento
     * @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 agendamento
     * @throws br.mp.mprj.portal.portlet.gestaomensagens.service.NoSuchAgendamentoException if a agendamento with the primary key could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public Agendamento[] findByUuid_C_PrevAndNext(long agendamentoId,
        String uuid, long companyId, OrderByComparator orderByComparator)
        throws NoSuchAgendamentoException, SystemException {
        Agendamento agendamento = findByPrimaryKey(agendamentoId);

        Session session = null;

        try {
            session = openSession();

            Agendamento[] array = new AgendamentoImpl[3];

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

            array[1] = agendamento;

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

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

    protected Agendamento getByUuid_C_PrevAndNext(Session session,
        Agendamento agendamento, 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_AGENDAMENTO_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(AgendamentoModelImpl.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(agendamento);

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

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

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

    /**
     * Removes all the agendamentos 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 (Agendamento agendamento : findByUuid_C(uuid, companyId,
                QueryUtil.ALL_POS, QueryUtil.ALL_POS, null)) {
            remove(agendamento);
        }
    }

    /**
     * Returns the number of agendamentos where uuid = &#63; and companyId = &#63;.
     *
     * @param uuid the uuid
     * @param companyId the company ID
     * @return the number of matching agendamentos
     * @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_AGENDAMENTO_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 agendamentos where mensagemId = &#63; and tipoAgendamento = &#63;.
     *
     * @param mensagemId the mensagem ID
     * @param tipoAgendamento the tipo agendamento
     * @return the matching agendamentos
     * @throws SystemException if a system exception occurred
     */
    @Override
    public List<Agendamento> findByMensagemTipo(long mensagemId,
        int tipoAgendamento) throws SystemException {
        return findByMensagemTipo(mensagemId, tipoAgendamento,
            QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
    }

    /**
     * Returns a range of all the agendamentos where mensagemId = &#63; and tipoAgendamento = &#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.AgendamentoModelImpl}. 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 mensagemId the mensagem ID
     * @param tipoAgendamento the tipo agendamento
     * @param start the lower bound of the range of agendamentos
     * @param end the upper bound of the range of agendamentos (not inclusive)
     * @return the range of matching agendamentos
     * @throws SystemException if a system exception occurred
     */
    @Override
    public List<Agendamento> findByMensagemTipo(long mensagemId,
        int tipoAgendamento, int start, int end) throws SystemException {
        return findByMensagemTipo(mensagemId, tipoAgendamento, start, end, null);
    }

    /**
     * Returns an ordered range of all the agendamentos where mensagemId = &#63; and tipoAgendamento = &#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.AgendamentoModelImpl}. 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 mensagemId the mensagem ID
     * @param tipoAgendamento the tipo agendamento
     * @param start the lower bound of the range of agendamentos
     * @param end the upper bound of the range of agendamentos (not inclusive)
     * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
     * @return the ordered range of matching agendamentos
     * @throws SystemException if a system exception occurred
     */
    @Override
    public List<Agendamento> findByMensagemTipo(long mensagemId,
        int tipoAgendamento, 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_MENSAGEMTIPO;
            finderArgs = new Object[] { mensagemId, tipoAgendamento };
        } else {
            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_MENSAGEMTIPO;
            finderArgs = new Object[] {
                    mensagemId, tipoAgendamento,
                    
                    start, end, orderByComparator
                };
        }

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

        if ((list != null) && !list.isEmpty()) {
            for (Agendamento agendamento : list) {
                if ((mensagemId != agendamento.getMensagemId()) ||
                        (tipoAgendamento != agendamento.getTipoAgendamento())) {
                    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_AGENDAMENTO_WHERE);

            query.append(_FINDER_COLUMN_MENSAGEMTIPO_MENSAGEMID_2);

            query.append(_FINDER_COLUMN_MENSAGEMTIPO_TIPOAGENDAMENTO_2);

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

                qPos.add(tipoAgendamento);

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

                    Collections.sort(list);

                    list = new UnmodifiableList<Agendamento>(list);
                } else {
                    list = (List<Agendamento>) 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 agendamento in the ordered set where mensagemId = &#63; and tipoAgendamento = &#63;.
     *
     * @param mensagemId the mensagem ID
     * @param tipoAgendamento the tipo agendamento
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the first matching agendamento
     * @throws br.mp.mprj.portal.portlet.gestaomensagens.service.NoSuchAgendamentoException if a matching agendamento could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public Agendamento findByMensagemTipo_First(long mensagemId,
        int tipoAgendamento, OrderByComparator orderByComparator)
        throws NoSuchAgendamentoException, SystemException {
        Agendamento agendamento = fetchByMensagemTipo_First(mensagemId,
                tipoAgendamento, orderByComparator);

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

        StringBundler msg = new StringBundler(6);

        msg.append(_NO_SUCH_ENTITY_WITH_KEY);

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

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

        msg.append(StringPool.CLOSE_CURLY_BRACE);

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

    /**
     * Returns the first agendamento in the ordered set where mensagemId = &#63; and tipoAgendamento = &#63;.
     *
     * @param mensagemId the mensagem ID
     * @param tipoAgendamento the tipo agendamento
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the first matching agendamento, or <code>null</code> if a matching agendamento could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public Agendamento fetchByMensagemTipo_First(long mensagemId,
        int tipoAgendamento, OrderByComparator orderByComparator)
        throws SystemException {
        List<Agendamento> list = findByMensagemTipo(mensagemId,
                tipoAgendamento, 0, 1, orderByComparator);

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

        return null;
    }

    /**
     * Returns the last agendamento in the ordered set where mensagemId = &#63; and tipoAgendamento = &#63;.
     *
     * @param mensagemId the mensagem ID
     * @param tipoAgendamento the tipo agendamento
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the last matching agendamento
     * @throws br.mp.mprj.portal.portlet.gestaomensagens.service.NoSuchAgendamentoException if a matching agendamento could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public Agendamento findByMensagemTipo_Last(long mensagemId,
        int tipoAgendamento, OrderByComparator orderByComparator)
        throws NoSuchAgendamentoException, SystemException {
        Agendamento agendamento = fetchByMensagemTipo_Last(mensagemId,
                tipoAgendamento, orderByComparator);

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

        StringBundler msg = new StringBundler(6);

        msg.append(_NO_SUCH_ENTITY_WITH_KEY);

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

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

        msg.append(StringPool.CLOSE_CURLY_BRACE);

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

    /**
     * Returns the last agendamento in the ordered set where mensagemId = &#63; and tipoAgendamento = &#63;.
     *
     * @param mensagemId the mensagem ID
     * @param tipoAgendamento the tipo agendamento
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the last matching agendamento, or <code>null</code> if a matching agendamento could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public Agendamento fetchByMensagemTipo_Last(long mensagemId,
        int tipoAgendamento, OrderByComparator orderByComparator)
        throws SystemException {
        int count = countByMensagemTipo(mensagemId, tipoAgendamento);

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

        List<Agendamento> list = findByMensagemTipo(mensagemId,
                tipoAgendamento, count - 1, count, orderByComparator);

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

        return null;
    }

    /**
     * Returns the agendamentos before and after the current agendamento in the ordered set where mensagemId = &#63; and tipoAgendamento = &#63;.
     *
     * @param agendamentoId the primary key of the current agendamento
     * @param mensagemId the mensagem ID
     * @param tipoAgendamento the tipo agendamento
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the previous, current, and next agendamento
     * @throws br.mp.mprj.portal.portlet.gestaomensagens.service.NoSuchAgendamentoException if a agendamento with the primary key could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public Agendamento[] findByMensagemTipo_PrevAndNext(long agendamentoId,
        long mensagemId, int tipoAgendamento,
        OrderByComparator orderByComparator)
        throws NoSuchAgendamentoException, SystemException {
        Agendamento agendamento = findByPrimaryKey(agendamentoId);

        Session session = null;

        try {
            session = openSession();

            Agendamento[] array = new AgendamentoImpl[3];

            array[0] = getByMensagemTipo_PrevAndNext(session, agendamento,
                    mensagemId, tipoAgendamento, orderByComparator, true);

            array[1] = agendamento;

            array[2] = getByMensagemTipo_PrevAndNext(session, agendamento,
                    mensagemId, tipoAgendamento, orderByComparator, false);

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

    protected Agendamento getByMensagemTipo_PrevAndNext(Session session,
        Agendamento agendamento, long mensagemId, int tipoAgendamento,
        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_AGENDAMENTO_WHERE);

        query.append(_FINDER_COLUMN_MENSAGEMTIPO_MENSAGEMID_2);

        query.append(_FINDER_COLUMN_MENSAGEMTIPO_TIPOAGENDAMENTO_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(AgendamentoModelImpl.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(mensagemId);

        qPos.add(tipoAgendamento);

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

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

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

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

    /**
     * Removes all the agendamentos where mensagemId = &#63; and tipoAgendamento = &#63; from the database.
     *
     * @param mensagemId the mensagem ID
     * @param tipoAgendamento the tipo agendamento
     * @throws SystemException if a system exception occurred
     */
    @Override
    public void removeByMensagemTipo(long mensagemId, int tipoAgendamento)
        throws SystemException {
        for (Agendamento agendamento : findByMensagemTipo(mensagemId,
                tipoAgendamento, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null)) {
            remove(agendamento);
        }
    }

    /**
     * Returns the number of agendamentos where mensagemId = &#63; and tipoAgendamento = &#63;.
     *
     * @param mensagemId the mensagem ID
     * @param tipoAgendamento the tipo agendamento
     * @return the number of matching agendamentos
     * @throws SystemException if a system exception occurred
     */
    @Override
    public int countByMensagemTipo(long mensagemId, int tipoAgendamento)
        throws SystemException {
        FinderPath finderPath = FINDER_PATH_COUNT_BY_MENSAGEMTIPO;

        Object[] finderArgs = new Object[] { mensagemId, tipoAgendamento };

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

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

            query.append(_SQL_COUNT_AGENDAMENTO_WHERE);

            query.append(_FINDER_COLUMN_MENSAGEMTIPO_MENSAGEMID_2);

            query.append(_FINDER_COLUMN_MENSAGEMTIPO_TIPOAGENDAMENTO_2);

            String sql = query.toString();

            Session session = null;

            try {
                session = openSession();

                Query q = session.createQuery(sql);

                QueryPos qPos = QueryPos.getInstance(q);

                qPos.add(mensagemId);

                qPos.add(tipoAgendamento);

                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 agendamentos where mensagemId = &#63; and tipoAgendamento = &#63;.
     *
     * @param mensagemId the mensagem ID
     * @param tipoAgendamento the tipo agendamento
     * @return the matching agendamentos
     * @throws SystemException if a system exception occurred
     */
    @Override
    public List<Agendamento> findByMensagemTipoMultipo(long mensagemId,
        int tipoAgendamento) throws SystemException {
        return findByMensagemTipoMultipo(mensagemId, tipoAgendamento,
            QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
    }

    /**
     * Returns a range of all the agendamentos where mensagemId = &#63; and tipoAgendamento = &#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.AgendamentoModelImpl}. 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 mensagemId the mensagem ID
     * @param tipoAgendamento the tipo agendamento
     * @param start the lower bound of the range of agendamentos
     * @param end the upper bound of the range of agendamentos (not inclusive)
     * @return the range of matching agendamentos
     * @throws SystemException if a system exception occurred
     */
    @Override
    public List<Agendamento> findByMensagemTipoMultipo(long mensagemId,
        int tipoAgendamento, int start, int end) throws SystemException {
        return findByMensagemTipoMultipo(mensagemId, tipoAgendamento, start,
            end, null);
    }

    /**
     * Returns an ordered range of all the agendamentos where mensagemId = &#63; and tipoAgendamento = &#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.AgendamentoModelImpl}. 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 mensagemId the mensagem ID
     * @param tipoAgendamento the tipo agendamento
     * @param start the lower bound of the range of agendamentos
     * @param end the upper bound of the range of agendamentos (not inclusive)
     * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
     * @return the ordered range of matching agendamentos
     * @throws SystemException if a system exception occurred
     */
    @Override
    public List<Agendamento> findByMensagemTipoMultipo(long mensagemId,
        int tipoAgendamento, 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_MENSAGEMTIPOMULTIPO;
            finderArgs = new Object[] { mensagemId, tipoAgendamento };
        } else {
            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_MENSAGEMTIPOMULTIPO;
            finderArgs = new Object[] {
                    mensagemId, tipoAgendamento,
                    
                    start, end, orderByComparator
                };
        }

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

        if ((list != null) && !list.isEmpty()) {
            for (Agendamento agendamento : list) {
                if ((mensagemId != agendamento.getMensagemId()) ||
                        (tipoAgendamento != agendamento.getTipoAgendamento())) {
                    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_AGENDAMENTO_WHERE);

            query.append(_FINDER_COLUMN_MENSAGEMTIPOMULTIPO_MENSAGEMID_2);

            query.append(_FINDER_COLUMN_MENSAGEMTIPOMULTIPO_TIPOAGENDAMENTO_2);

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

                qPos.add(tipoAgendamento);

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

                    Collections.sort(list);

                    list = new UnmodifiableList<Agendamento>(list);
                } else {
                    list = (List<Agendamento>) 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 agendamento in the ordered set where mensagemId = &#63; and tipoAgendamento = &#63;.
     *
     * @param mensagemId the mensagem ID
     * @param tipoAgendamento the tipo agendamento
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the first matching agendamento
     * @throws br.mp.mprj.portal.portlet.gestaomensagens.service.NoSuchAgendamentoException if a matching agendamento could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public Agendamento findByMensagemTipoMultipo_First(long mensagemId,
        int tipoAgendamento, OrderByComparator orderByComparator)
        throws NoSuchAgendamentoException, SystemException {
        Agendamento agendamento = fetchByMensagemTipoMultipo_First(mensagemId,
                tipoAgendamento, orderByComparator);

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

        StringBundler msg = new StringBundler(6);

        msg.append(_NO_SUCH_ENTITY_WITH_KEY);

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

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

        msg.append(StringPool.CLOSE_CURLY_BRACE);

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

    /**
     * Returns the first agendamento in the ordered set where mensagemId = &#63; and tipoAgendamento = &#63;.
     *
     * @param mensagemId the mensagem ID
     * @param tipoAgendamento the tipo agendamento
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the first matching agendamento, or <code>null</code> if a matching agendamento could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public Agendamento fetchByMensagemTipoMultipo_First(long mensagemId,
        int tipoAgendamento, OrderByComparator orderByComparator)
        throws SystemException {
        List<Agendamento> list = findByMensagemTipoMultipo(mensagemId,
                tipoAgendamento, 0, 1, orderByComparator);

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

        return null;
    }

    /**
     * Returns the last agendamento in the ordered set where mensagemId = &#63; and tipoAgendamento = &#63;.
     *
     * @param mensagemId the mensagem ID
     * @param tipoAgendamento the tipo agendamento
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the last matching agendamento
     * @throws br.mp.mprj.portal.portlet.gestaomensagens.service.NoSuchAgendamentoException if a matching agendamento could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public Agendamento findByMensagemTipoMultipo_Last(long mensagemId,
        int tipoAgendamento, OrderByComparator orderByComparator)
        throws NoSuchAgendamentoException, SystemException {
        Agendamento agendamento = fetchByMensagemTipoMultipo_Last(mensagemId,
                tipoAgendamento, orderByComparator);

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

        StringBundler msg = new StringBundler(6);

        msg.append(_NO_SUCH_ENTITY_WITH_KEY);

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

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

        msg.append(StringPool.CLOSE_CURLY_BRACE);

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

    /**
     * Returns the last agendamento in the ordered set where mensagemId = &#63; and tipoAgendamento = &#63;.
     *
     * @param mensagemId the mensagem ID
     * @param tipoAgendamento the tipo agendamento
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the last matching agendamento, or <code>null</code> if a matching agendamento could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public Agendamento fetchByMensagemTipoMultipo_Last(long mensagemId,
        int tipoAgendamento, OrderByComparator orderByComparator)
        throws SystemException {
        int count = countByMensagemTipoMultipo(mensagemId, tipoAgendamento);

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

        List<Agendamento> list = findByMensagemTipoMultipo(mensagemId,
                tipoAgendamento, count - 1, count, orderByComparator);

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

        return null;
    }

    /**
     * Returns the agendamentos before and after the current agendamento in the ordered set where mensagemId = &#63; and tipoAgendamento = &#63;.
     *
     * @param agendamentoId the primary key of the current agendamento
     * @param mensagemId the mensagem ID
     * @param tipoAgendamento the tipo agendamento
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the previous, current, and next agendamento
     * @throws br.mp.mprj.portal.portlet.gestaomensagens.service.NoSuchAgendamentoException if a agendamento with the primary key could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public Agendamento[] findByMensagemTipoMultipo_PrevAndNext(
        long agendamentoId, long mensagemId, int tipoAgendamento,
        OrderByComparator orderByComparator)
        throws NoSuchAgendamentoException, SystemException {
        Agendamento agendamento = findByPrimaryKey(agendamentoId);

        Session session = null;

        try {
            session = openSession();

            Agendamento[] array = new AgendamentoImpl[3];

            array[0] = getByMensagemTipoMultipo_PrevAndNext(session,
                    agendamento, mensagemId, tipoAgendamento,
                    orderByComparator, true);

            array[1] = agendamento;

            array[2] = getByMensagemTipoMultipo_PrevAndNext(session,
                    agendamento, mensagemId, tipoAgendamento,
                    orderByComparator, false);

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

    protected Agendamento getByMensagemTipoMultipo_PrevAndNext(
        Session session, Agendamento agendamento, long mensagemId,
        int tipoAgendamento, 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_AGENDAMENTO_WHERE);

        query.append(_FINDER_COLUMN_MENSAGEMTIPOMULTIPO_MENSAGEMID_2);

        query.append(_FINDER_COLUMN_MENSAGEMTIPOMULTIPO_TIPOAGENDAMENTO_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(AgendamentoModelImpl.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(mensagemId);

        qPos.add(tipoAgendamento);

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

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

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

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

    /**
     * Returns all the agendamentos where mensagemId = &#63; and tipoAgendamento = 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.AgendamentoModelImpl}. 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 mensagemId the mensagem ID
     * @param tipoAgendamentos the tipo agendamentos
     * @return the matching agendamentos
     * @throws SystemException if a system exception occurred
     */
    @Override
    public List<Agendamento> findByMensagemTipoMultipo(long mensagemId,
        int[] tipoAgendamentos) throws SystemException {
        return findByMensagemTipoMultipo(mensagemId, tipoAgendamentos,
            QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
    }

    /**
     * Returns a range of all the agendamentos where mensagemId = &#63; and tipoAgendamento = 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.AgendamentoModelImpl}. 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 mensagemId the mensagem ID
     * @param tipoAgendamentos the tipo agendamentos
     * @param start the lower bound of the range of agendamentos
     * @param end the upper bound of the range of agendamentos (not inclusive)
     * @return the range of matching agendamentos
     * @throws SystemException if a system exception occurred
     */
    @Override
    public List<Agendamento> findByMensagemTipoMultipo(long mensagemId,
        int[] tipoAgendamentos, int start, int end) throws SystemException {
        return findByMensagemTipoMultipo(mensagemId, tipoAgendamentos, start,
            end, null);
    }

    /**
     * Returns an ordered range of all the agendamentos where mensagemId = &#63; and tipoAgendamento = 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.AgendamentoModelImpl}. 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 mensagemId the mensagem ID
     * @param tipoAgendamentos the tipo agendamentos
     * @param start the lower bound of the range of agendamentos
     * @param end the upper bound of the range of agendamentos (not inclusive)
     * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
     * @return the ordered range of matching agendamentos
     * @throws SystemException if a system exception occurred
     */
    @Override
    public List<Agendamento> findByMensagemTipoMultipo(long mensagemId,
        int[] tipoAgendamentos, int start, int end,
        OrderByComparator orderByComparator) throws SystemException {
        if ((tipoAgendamentos != null) && (tipoAgendamentos.length == 1)) {
            return findByMensagemTipoMultipo(mensagemId, tipoAgendamentos[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[] {
                    mensagemId, StringUtil.merge(tipoAgendamentos)
                };
        } else {
            finderArgs = new Object[] {
                    mensagemId, StringUtil.merge(tipoAgendamentos),
                    
                    start, end, orderByComparator
                };
        }

        List<Agendamento> list = (List<Agendamento>) FinderCacheUtil.getResult(FINDER_PATH_WITH_PAGINATION_FIND_BY_MENSAGEMTIPOMULTIPO,
                finderArgs, this);

        if ((list != null) && !list.isEmpty()) {
            for (Agendamento agendamento : list) {
                if ((mensagemId != agendamento.getMensagemId()) ||
                        !ArrayUtil.contains(tipoAgendamentos,
                            agendamento.getTipoAgendamento())) {
                    list = null;

                    break;
                }
            }
        }

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

            query.append(_SQL_SELECT_AGENDAMENTO_WHERE);

            boolean conjunctionable = false;

            if (conjunctionable) {
                query.append(WHERE_AND);
            }

            query.append(_FINDER_COLUMN_MENSAGEMTIPOMULTIPO_MENSAGEMID_5);

            conjunctionable = true;

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

                query.append(StringPool.OPEN_PARENTHESIS);

                for (int i = 0; i < tipoAgendamentos.length; i++) {
                    query.append(_FINDER_COLUMN_MENSAGEMTIPOMULTIPO_TIPOAGENDAMENTO_5);

                    if ((i + 1) < tipoAgendamentos.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(AgendamentoModelImpl.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(mensagemId);

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

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

                    Collections.sort(list);

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

                cacheResult(list);

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

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

        return list;
    }

    /**
     * Removes all the agendamentos where mensagemId = &#63; and tipoAgendamento = &#63; from the database.
     *
     * @param mensagemId the mensagem ID
     * @param tipoAgendamento the tipo agendamento
     * @throws SystemException if a system exception occurred
     */
    @Override
    public void removeByMensagemTipoMultipo(long mensagemId, int tipoAgendamento)
        throws SystemException {
        for (Agendamento agendamento : findByMensagemTipoMultipo(mensagemId,
                tipoAgendamento, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null)) {
            remove(agendamento);
        }
    }

    /**
     * Returns the number of agendamentos where mensagemId = &#63; and tipoAgendamento = &#63;.
     *
     * @param mensagemId the mensagem ID
     * @param tipoAgendamento the tipo agendamento
     * @return the number of matching agendamentos
     * @throws SystemException if a system exception occurred
     */
    @Override
    public int countByMensagemTipoMultipo(long mensagemId, int tipoAgendamento)
        throws SystemException {
        FinderPath finderPath = FINDER_PATH_COUNT_BY_MENSAGEMTIPOMULTIPO;

        Object[] finderArgs = new Object[] { mensagemId, tipoAgendamento };

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

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

            query.append(_SQL_COUNT_AGENDAMENTO_WHERE);

            query.append(_FINDER_COLUMN_MENSAGEMTIPOMULTIPO_MENSAGEMID_2);

            query.append(_FINDER_COLUMN_MENSAGEMTIPOMULTIPO_TIPOAGENDAMENTO_2);

            String sql = query.toString();

            Session session = null;

            try {
                session = openSession();

                Query q = session.createQuery(sql);

                QueryPos qPos = QueryPos.getInstance(q);

                qPos.add(mensagemId);

                qPos.add(tipoAgendamento);

                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 agendamentos where mensagemId = &#63; and tipoAgendamento = any &#63;.
     *
     * @param mensagemId the mensagem ID
     * @param tipoAgendamentos the tipo agendamentos
     * @return the number of matching agendamentos
     * @throws SystemException if a system exception occurred
     */
    @Override
    public int countByMensagemTipoMultipo(long mensagemId,
        int[] tipoAgendamentos) throws SystemException {
        Object[] finderArgs = new Object[] {
                mensagemId, StringUtil.merge(tipoAgendamentos)
            };

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

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

            query.append(_SQL_COUNT_AGENDAMENTO_WHERE);

            boolean conjunctionable = false;

            if (conjunctionable) {
                query.append(WHERE_AND);
            }

            query.append(_FINDER_COLUMN_MENSAGEMTIPOMULTIPO_MENSAGEMID_5);

            conjunctionable = true;

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

                query.append(StringPool.OPEN_PARENTHESIS);

                for (int i = 0; i < tipoAgendamentos.length; i++) {
                    query.append(_FINDER_COLUMN_MENSAGEMTIPOMULTIPO_TIPOAGENDAMENTO_5);

                    if ((i + 1) < tipoAgendamentos.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);

                qPos.add(mensagemId);

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

                count = (Long) q.uniqueResult();

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

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

        return count.intValue();
    }

    /**
     * Returns all the agendamentos where mensagemId = &#63; and dataCalendario = &#63;.
     *
     * @param mensagemId the mensagem ID
     * @param dataCalendario the data calendario
     * @return the matching agendamentos
     * @throws SystemException if a system exception occurred
     */
    @Override
    public List<Agendamento> findByMensagemDataCalendario(long mensagemId,
        Date dataCalendario) throws SystemException {
        return findByMensagemDataCalendario(mensagemId, dataCalendario,
            QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
    }

    /**
     * Returns a range of all the agendamentos where mensagemId = &#63; and dataCalendario = &#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.AgendamentoModelImpl}. 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 mensagemId the mensagem ID
     * @param dataCalendario the data calendario
     * @param start the lower bound of the range of agendamentos
     * @param end the upper bound of the range of agendamentos (not inclusive)
     * @return the range of matching agendamentos
     * @throws SystemException if a system exception occurred
     */
    @Override
    public List<Agendamento> findByMensagemDataCalendario(long mensagemId,
        Date dataCalendario, int start, int end) throws SystemException {
        return findByMensagemDataCalendario(mensagemId, dataCalendario, start,
            end, null);
    }

    /**
     * Returns an ordered range of all the agendamentos where mensagemId = &#63; and dataCalendario = &#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.AgendamentoModelImpl}. 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 mensagemId the mensagem ID
     * @param dataCalendario the data calendario
     * @param start the lower bound of the range of agendamentos
     * @param end the upper bound of the range of agendamentos (not inclusive)
     * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
     * @return the ordered range of matching agendamentos
     * @throws SystemException if a system exception occurred
     */
    @Override
    public List<Agendamento> findByMensagemDataCalendario(long mensagemId,
        Date dataCalendario, 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_MENSAGEMDATACALENDARIO;
            finderArgs = new Object[] { mensagemId, dataCalendario };
        } else {
            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_MENSAGEMDATACALENDARIO;
            finderArgs = new Object[] {
                    mensagemId, dataCalendario,
                    
                    start, end, orderByComparator
                };
        }

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

        if ((list != null) && !list.isEmpty()) {
            for (Agendamento agendamento : list) {
                if ((mensagemId != agendamento.getMensagemId()) ||
                        !Validator.equals(dataCalendario,
                            agendamento.getDataCalendario())) {
                    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_AGENDAMENTO_WHERE);

            query.append(_FINDER_COLUMN_MENSAGEMDATACALENDARIO_MENSAGEMID_2);

            boolean bindDataCalendario = false;

            if (dataCalendario == null) {
                query.append(_FINDER_COLUMN_MENSAGEMDATACALENDARIO_DATACALENDARIO_1);
            } else {
                bindDataCalendario = true;

                query.append(_FINDER_COLUMN_MENSAGEMDATACALENDARIO_DATACALENDARIO_2);
            }

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

                if (bindDataCalendario) {
                    qPos.add(CalendarUtil.getTimestamp(dataCalendario));
                }

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

                    Collections.sort(list);

                    list = new UnmodifiableList<Agendamento>(list);
                } else {
                    list = (List<Agendamento>) 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 agendamento in the ordered set where mensagemId = &#63; and dataCalendario = &#63;.
     *
     * @param mensagemId the mensagem ID
     * @param dataCalendario the data calendario
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the first matching agendamento
     * @throws br.mp.mprj.portal.portlet.gestaomensagens.service.NoSuchAgendamentoException if a matching agendamento could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public Agendamento findByMensagemDataCalendario_First(long mensagemId,
        Date dataCalendario, OrderByComparator orderByComparator)
        throws NoSuchAgendamentoException, SystemException {
        Agendamento agendamento = fetchByMensagemDataCalendario_First(mensagemId,
                dataCalendario, orderByComparator);

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

        StringBundler msg = new StringBundler(6);

        msg.append(_NO_SUCH_ENTITY_WITH_KEY);

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

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

        msg.append(StringPool.CLOSE_CURLY_BRACE);

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

    /**
     * Returns the first agendamento in the ordered set where mensagemId = &#63; and dataCalendario = &#63;.
     *
     * @param mensagemId the mensagem ID
     * @param dataCalendario the data calendario
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the first matching agendamento, or <code>null</code> if a matching agendamento could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public Agendamento fetchByMensagemDataCalendario_First(long mensagemId,
        Date dataCalendario, OrderByComparator orderByComparator)
        throws SystemException {
        List<Agendamento> list = findByMensagemDataCalendario(mensagemId,
                dataCalendario, 0, 1, orderByComparator);

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

        return null;
    }

    /**
     * Returns the last agendamento in the ordered set where mensagemId = &#63; and dataCalendario = &#63;.
     *
     * @param mensagemId the mensagem ID
     * @param dataCalendario the data calendario
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the last matching agendamento
     * @throws br.mp.mprj.portal.portlet.gestaomensagens.service.NoSuchAgendamentoException if a matching agendamento could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public Agendamento findByMensagemDataCalendario_Last(long mensagemId,
        Date dataCalendario, OrderByComparator orderByComparator)
        throws NoSuchAgendamentoException, SystemException {
        Agendamento agendamento = fetchByMensagemDataCalendario_Last(mensagemId,
                dataCalendario, orderByComparator);

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

        StringBundler msg = new StringBundler(6);

        msg.append(_NO_SUCH_ENTITY_WITH_KEY);

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

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

        msg.append(StringPool.CLOSE_CURLY_BRACE);

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

    /**
     * Returns the last agendamento in the ordered set where mensagemId = &#63; and dataCalendario = &#63;.
     *
     * @param mensagemId the mensagem ID
     * @param dataCalendario the data calendario
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the last matching agendamento, or <code>null</code> if a matching agendamento could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public Agendamento fetchByMensagemDataCalendario_Last(long mensagemId,
        Date dataCalendario, OrderByComparator orderByComparator)
        throws SystemException {
        int count = countByMensagemDataCalendario(mensagemId, dataCalendario);

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

        List<Agendamento> list = findByMensagemDataCalendario(mensagemId,
                dataCalendario, count - 1, count, orderByComparator);

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

        return null;
    }

    /**
     * Returns the agendamentos before and after the current agendamento in the ordered set where mensagemId = &#63; and dataCalendario = &#63;.
     *
     * @param agendamentoId the primary key of the current agendamento
     * @param mensagemId the mensagem ID
     * @param dataCalendario the data calendario
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the previous, current, and next agendamento
     * @throws br.mp.mprj.portal.portlet.gestaomensagens.service.NoSuchAgendamentoException if a agendamento with the primary key could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public Agendamento[] findByMensagemDataCalendario_PrevAndNext(
        long agendamentoId, long mensagemId, Date dataCalendario,
        OrderByComparator orderByComparator)
        throws NoSuchAgendamentoException, SystemException {
        Agendamento agendamento = findByPrimaryKey(agendamentoId);

        Session session = null;

        try {
            session = openSession();

            Agendamento[] array = new AgendamentoImpl[3];

            array[0] = getByMensagemDataCalendario_PrevAndNext(session,
                    agendamento, mensagemId, dataCalendario, orderByComparator,
                    true);

            array[1] = agendamento;

            array[2] = getByMensagemDataCalendario_PrevAndNext(session,
                    agendamento, mensagemId, dataCalendario, orderByComparator,
                    false);

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

    protected Agendamento getByMensagemDataCalendario_PrevAndNext(
        Session session, Agendamento agendamento, long mensagemId,
        Date dataCalendario, 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_AGENDAMENTO_WHERE);

        query.append(_FINDER_COLUMN_MENSAGEMDATACALENDARIO_MENSAGEMID_2);

        boolean bindDataCalendario = false;

        if (dataCalendario == null) {
            query.append(_FINDER_COLUMN_MENSAGEMDATACALENDARIO_DATACALENDARIO_1);
        } else {
            bindDataCalendario = true;

            query.append(_FINDER_COLUMN_MENSAGEMDATACALENDARIO_DATACALENDARIO_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(AgendamentoModelImpl.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(mensagemId);

        if (bindDataCalendario) {
            qPos.add(CalendarUtil.getTimestamp(dataCalendario));
        }

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

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

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

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

    /**
     * Removes all the agendamentos where mensagemId = &#63; and dataCalendario = &#63; from the database.
     *
     * @param mensagemId the mensagem ID
     * @param dataCalendario the data calendario
     * @throws SystemException if a system exception occurred
     */
    @Override
    public void removeByMensagemDataCalendario(long mensagemId,
        Date dataCalendario) throws SystemException {
        for (Agendamento agendamento : findByMensagemDataCalendario(
                mensagemId, dataCalendario, QueryUtil.ALL_POS,
                QueryUtil.ALL_POS, null)) {
            remove(agendamento);
        }
    }

    /**
     * Returns the number of agendamentos where mensagemId = &#63; and dataCalendario = &#63;.
     *
     * @param mensagemId the mensagem ID
     * @param dataCalendario the data calendario
     * @return the number of matching agendamentos
     * @throws SystemException if a system exception occurred
     */
    @Override
    public int countByMensagemDataCalendario(long mensagemId,
        Date dataCalendario) throws SystemException {
        FinderPath finderPath = FINDER_PATH_COUNT_BY_MENSAGEMDATACALENDARIO;

        Object[] finderArgs = new Object[] { mensagemId, dataCalendario };

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

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

            query.append(_SQL_COUNT_AGENDAMENTO_WHERE);

            query.append(_FINDER_COLUMN_MENSAGEMDATACALENDARIO_MENSAGEMID_2);

            boolean bindDataCalendario = false;

            if (dataCalendario == null) {
                query.append(_FINDER_COLUMN_MENSAGEMDATACALENDARIO_DATACALENDARIO_1);
            } else {
                bindDataCalendario = true;

                query.append(_FINDER_COLUMN_MENSAGEMDATACALENDARIO_DATACALENDARIO_2);
            }

            String sql = query.toString();

            Session session = null;

            try {
                session = openSession();

                Query q = session.createQuery(sql);

                QueryPos qPos = QueryPos.getInstance(q);

                qPos.add(mensagemId);

                if (bindDataCalendario) {
                    qPos.add(CalendarUtil.getTimestamp(dataCalendario));
                }

                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 agendamentos where situacaoEnvio = &#63;.
     *
     * @param situacaoEnvio the situacao envio
     * @return the matching agendamentos
     * @throws SystemException if a system exception occurred
     */
    @Override
    public List<Agendamento> findBySituacao(int situacaoEnvio)
        throws SystemException {
        return findBySituacao(situacaoEnvio, QueryUtil.ALL_POS,
            QueryUtil.ALL_POS, null);
    }

    /**
     * Returns a range of all the agendamentos where situacaoEnvio = &#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.AgendamentoModelImpl}. 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 situacaoEnvio the situacao envio
     * @param start the lower bound of the range of agendamentos
     * @param end the upper bound of the range of agendamentos (not inclusive)
     * @return the range of matching agendamentos
     * @throws SystemException if a system exception occurred
     */
    @Override
    public List<Agendamento> findBySituacao(int situacaoEnvio, int start,
        int end) throws SystemException {
        return findBySituacao(situacaoEnvio, start, end, null);
    }

    /**
     * Returns an ordered range of all the agendamentos where situacaoEnvio = &#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.AgendamentoModelImpl}. 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 situacaoEnvio the situacao envio
     * @param start the lower bound of the range of agendamentos
     * @param end the upper bound of the range of agendamentos (not inclusive)
     * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
     * @return the ordered range of matching agendamentos
     * @throws SystemException if a system exception occurred
     */
    @Override
    public List<Agendamento> findBySituacao(int situacaoEnvio, 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_SITUACAO;
            finderArgs = new Object[] { situacaoEnvio };
        } else {
            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_SITUACAO;
            finderArgs = new Object[] {
                    situacaoEnvio,
                    
                    start, end, orderByComparator
                };
        }

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

        if ((list != null) && !list.isEmpty()) {
            for (Agendamento agendamento : list) {
                if ((situacaoEnvio != agendamento.getSituacaoEnvio())) {
                    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_AGENDAMENTO_WHERE);

            query.append(_FINDER_COLUMN_SITUACAO_SITUACAOENVIO_2);

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

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

                    Collections.sort(list);

                    list = new UnmodifiableList<Agendamento>(list);
                } else {
                    list = (List<Agendamento>) 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 agendamento in the ordered set where situacaoEnvio = &#63;.
     *
     * @param situacaoEnvio the situacao envio
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the first matching agendamento
     * @throws br.mp.mprj.portal.portlet.gestaomensagens.service.NoSuchAgendamentoException if a matching agendamento could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public Agendamento findBySituacao_First(int situacaoEnvio,
        OrderByComparator orderByComparator)
        throws NoSuchAgendamentoException, SystemException {
        Agendamento agendamento = fetchBySituacao_First(situacaoEnvio,
                orderByComparator);

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

        StringBundler msg = new StringBundler(4);

        msg.append(_NO_SUCH_ENTITY_WITH_KEY);

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

        msg.append(StringPool.CLOSE_CURLY_BRACE);

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

    /**
     * Returns the first agendamento in the ordered set where situacaoEnvio = &#63;.
     *
     * @param situacaoEnvio the situacao envio
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the first matching agendamento, or <code>null</code> if a matching agendamento could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public Agendamento fetchBySituacao_First(int situacaoEnvio,
        OrderByComparator orderByComparator) throws SystemException {
        List<Agendamento> list = findBySituacao(situacaoEnvio, 0, 1,
                orderByComparator);

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

        return null;
    }

    /**
     * Returns the last agendamento in the ordered set where situacaoEnvio = &#63;.
     *
     * @param situacaoEnvio the situacao envio
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the last matching agendamento
     * @throws br.mp.mprj.portal.portlet.gestaomensagens.service.NoSuchAgendamentoException if a matching agendamento could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public Agendamento findBySituacao_Last(int situacaoEnvio,
        OrderByComparator orderByComparator)
        throws NoSuchAgendamentoException, SystemException {
        Agendamento agendamento = fetchBySituacao_Last(situacaoEnvio,
                orderByComparator);

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

        StringBundler msg = new StringBundler(4);

        msg.append(_NO_SUCH_ENTITY_WITH_KEY);

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

        msg.append(StringPool.CLOSE_CURLY_BRACE);

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

    /**
     * Returns the last agendamento in the ordered set where situacaoEnvio = &#63;.
     *
     * @param situacaoEnvio the situacao envio
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the last matching agendamento, or <code>null</code> if a matching agendamento could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public Agendamento fetchBySituacao_Last(int situacaoEnvio,
        OrderByComparator orderByComparator) throws SystemException {
        int count = countBySituacao(situacaoEnvio);

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

        List<Agendamento> list = findBySituacao(situacaoEnvio, count - 1,
                count, orderByComparator);

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

        return null;
    }

    /**
     * Returns the agendamentos before and after the current agendamento in the ordered set where situacaoEnvio = &#63;.
     *
     * @param agendamentoId the primary key of the current agendamento
     * @param situacaoEnvio the situacao envio
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the previous, current, and next agendamento
     * @throws br.mp.mprj.portal.portlet.gestaomensagens.service.NoSuchAgendamentoException if a agendamento with the primary key could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public Agendamento[] findBySituacao_PrevAndNext(long agendamentoId,
        int situacaoEnvio, OrderByComparator orderByComparator)
        throws NoSuchAgendamentoException, SystemException {
        Agendamento agendamento = findByPrimaryKey(agendamentoId);

        Session session = null;

        try {
            session = openSession();

            Agendamento[] array = new AgendamentoImpl[3];

            array[0] = getBySituacao_PrevAndNext(session, agendamento,
                    situacaoEnvio, orderByComparator, true);

            array[1] = agendamento;

            array[2] = getBySituacao_PrevAndNext(session, agendamento,
                    situacaoEnvio, orderByComparator, false);

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

    protected Agendamento getBySituacao_PrevAndNext(Session session,
        Agendamento agendamento, int situacaoEnvio,
        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_AGENDAMENTO_WHERE);

        query.append(_FINDER_COLUMN_SITUACAO_SITUACAOENVIO_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(AgendamentoModelImpl.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(situacaoEnvio);

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

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

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

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

    /**
     * Removes all the agendamentos where situacaoEnvio = &#63; from the database.
     *
     * @param situacaoEnvio the situacao envio
     * @throws SystemException if a system exception occurred
     */
    @Override
    public void removeBySituacao(int situacaoEnvio) throws SystemException {
        for (Agendamento agendamento : findBySituacao(situacaoEnvio,
                QueryUtil.ALL_POS, QueryUtil.ALL_POS, null)) {
            remove(agendamento);
        }
    }

    /**
     * Returns the number of agendamentos where situacaoEnvio = &#63;.
     *
     * @param situacaoEnvio the situacao envio
     * @return the number of matching agendamentos
     * @throws SystemException if a system exception occurred
     */
    @Override
    public int countBySituacao(int situacaoEnvio) throws SystemException {
        FinderPath finderPath = FINDER_PATH_COUNT_BY_SITUACAO;

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

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

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

            query.append(_SQL_COUNT_AGENDAMENTO_WHERE);

            query.append(_FINDER_COLUMN_SITUACAO_SITUACAOENVIO_2);

            String sql = query.toString();

            Session session = null;

            try {
                session = openSession();

                Query q = session.createQuery(sql);

                QueryPos qPos = QueryPos.getInstance(q);

                qPos.add(situacaoEnvio);

                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 agendamentos where situacaoEnvio = &#63; and mensagemId = &#63;.
     *
     * @param situacaoEnvio the situacao envio
     * @param mensagemId the mensagem ID
     * @return the matching agendamentos
     * @throws SystemException if a system exception occurred
     */
    @Override
    public List<Agendamento> findBySituacaoMensagem(int situacaoEnvio,
        long mensagemId) throws SystemException {
        return findBySituacaoMensagem(situacaoEnvio, mensagemId,
            QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
    }

    /**
     * Returns a range of all the agendamentos where situacaoEnvio = &#63; and mensagemId = &#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.AgendamentoModelImpl}. 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 situacaoEnvio the situacao envio
     * @param mensagemId the mensagem ID
     * @param start the lower bound of the range of agendamentos
     * @param end the upper bound of the range of agendamentos (not inclusive)
     * @return the range of matching agendamentos
     * @throws SystemException if a system exception occurred
     */
    @Override
    public List<Agendamento> findBySituacaoMensagem(int situacaoEnvio,
        long mensagemId, int start, int end) throws SystemException {
        return findBySituacaoMensagem(situacaoEnvio, mensagemId, start, end,
            null);
    }

    /**
     * Returns an ordered range of all the agendamentos where situacaoEnvio = &#63; and mensagemId = &#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.AgendamentoModelImpl}. 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 situacaoEnvio the situacao envio
     * @param mensagemId the mensagem ID
     * @param start the lower bound of the range of agendamentos
     * @param end the upper bound of the range of agendamentos (not inclusive)
     * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
     * @return the ordered range of matching agendamentos
     * @throws SystemException if a system exception occurred
     */
    @Override
    public List<Agendamento> findBySituacaoMensagem(int situacaoEnvio,
        long mensagemId, 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_SITUACAOMENSAGEM;
            finderArgs = new Object[] { situacaoEnvio, mensagemId };
        } else {
            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_SITUACAOMENSAGEM;
            finderArgs = new Object[] {
                    situacaoEnvio, mensagemId,
                    
                    start, end, orderByComparator
                };
        }

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

        if ((list != null) && !list.isEmpty()) {
            for (Agendamento agendamento : list) {
                if ((situacaoEnvio != agendamento.getSituacaoEnvio()) ||
                        (mensagemId != agendamento.getMensagemId())) {
                    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_AGENDAMENTO_WHERE);

            query.append(_FINDER_COLUMN_SITUACAOMENSAGEM_SITUACAOENVIO_2);

            query.append(_FINDER_COLUMN_SITUACAOMENSAGEM_MENSAGEMID_2);

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

                qPos.add(mensagemId);

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

                    Collections.sort(list);

                    list = new UnmodifiableList<Agendamento>(list);
                } else {
                    list = (List<Agendamento>) 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 agendamento in the ordered set where situacaoEnvio = &#63; and mensagemId = &#63;.
     *
     * @param situacaoEnvio the situacao envio
     * @param mensagemId the mensagem ID
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the first matching agendamento
     * @throws br.mp.mprj.portal.portlet.gestaomensagens.service.NoSuchAgendamentoException if a matching agendamento could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public Agendamento findBySituacaoMensagem_First(int situacaoEnvio,
        long mensagemId, OrderByComparator orderByComparator)
        throws NoSuchAgendamentoException, SystemException {
        Agendamento agendamento = fetchBySituacaoMensagem_First(situacaoEnvio,
                mensagemId, orderByComparator);

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

        StringBundler msg = new StringBundler(6);

        msg.append(_NO_SUCH_ENTITY_WITH_KEY);

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

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

        msg.append(StringPool.CLOSE_CURLY_BRACE);

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

    /**
     * Returns the first agendamento in the ordered set where situacaoEnvio = &#63; and mensagemId = &#63;.
     *
     * @param situacaoEnvio the situacao envio
     * @param mensagemId the mensagem ID
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the first matching agendamento, or <code>null</code> if a matching agendamento could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public Agendamento fetchBySituacaoMensagem_First(int situacaoEnvio,
        long mensagemId, OrderByComparator orderByComparator)
        throws SystemException {
        List<Agendamento> list = findBySituacaoMensagem(situacaoEnvio,
                mensagemId, 0, 1, orderByComparator);

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

        return null;
    }

    /**
     * Returns the last agendamento in the ordered set where situacaoEnvio = &#63; and mensagemId = &#63;.
     *
     * @param situacaoEnvio the situacao envio
     * @param mensagemId the mensagem ID
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the last matching agendamento
     * @throws br.mp.mprj.portal.portlet.gestaomensagens.service.NoSuchAgendamentoException if a matching agendamento could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public Agendamento findBySituacaoMensagem_Last(int situacaoEnvio,
        long mensagemId, OrderByComparator orderByComparator)
        throws NoSuchAgendamentoException, SystemException {
        Agendamento agendamento = fetchBySituacaoMensagem_Last(situacaoEnvio,
                mensagemId, orderByComparator);

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

        StringBundler msg = new StringBundler(6);

        msg.append(_NO_SUCH_ENTITY_WITH_KEY);

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

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

        msg.append(StringPool.CLOSE_CURLY_BRACE);

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

    /**
     * Returns the last agendamento in the ordered set where situacaoEnvio = &#63; and mensagemId = &#63;.
     *
     * @param situacaoEnvio the situacao envio
     * @param mensagemId the mensagem ID
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the last matching agendamento, or <code>null</code> if a matching agendamento could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public Agendamento fetchBySituacaoMensagem_Last(int situacaoEnvio,
        long mensagemId, OrderByComparator orderByComparator)
        throws SystemException {
        int count = countBySituacaoMensagem(situacaoEnvio, mensagemId);

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

        List<Agendamento> list = findBySituacaoMensagem(situacaoEnvio,
                mensagemId, count - 1, count, orderByComparator);

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

        return null;
    }

    /**
     * Returns the agendamentos before and after the current agendamento in the ordered set where situacaoEnvio = &#63; and mensagemId = &#63;.
     *
     * @param agendamentoId the primary key of the current agendamento
     * @param situacaoEnvio the situacao envio
     * @param mensagemId the mensagem ID
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the previous, current, and next agendamento
     * @throws br.mp.mprj.portal.portlet.gestaomensagens.service.NoSuchAgendamentoException if a agendamento with the primary key could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public Agendamento[] findBySituacaoMensagem_PrevAndNext(
        long agendamentoId, int situacaoEnvio, long mensagemId,
        OrderByComparator orderByComparator)
        throws NoSuchAgendamentoException, SystemException {
        Agendamento agendamento = findByPrimaryKey(agendamentoId);

        Session session = null;

        try {
            session = openSession();

            Agendamento[] array = new AgendamentoImpl[3];

            array[0] = getBySituacaoMensagem_PrevAndNext(session, agendamento,
                    situacaoEnvio, mensagemId, orderByComparator, true);

            array[1] = agendamento;

            array[2] = getBySituacaoMensagem_PrevAndNext(session, agendamento,
                    situacaoEnvio, mensagemId, orderByComparator, false);

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

    protected Agendamento getBySituacaoMensagem_PrevAndNext(Session session,
        Agendamento agendamento, int situacaoEnvio, long mensagemId,
        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_AGENDAMENTO_WHERE);

        query.append(_FINDER_COLUMN_SITUACAOMENSAGEM_SITUACAOENVIO_2);

        query.append(_FINDER_COLUMN_SITUACAOMENSAGEM_MENSAGEMID_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(AgendamentoModelImpl.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(situacaoEnvio);

        qPos.add(mensagemId);

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

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

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

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

    /**
     * Removes all the agendamentos where situacaoEnvio = &#63; and mensagemId = &#63; from the database.
     *
     * @param situacaoEnvio the situacao envio
     * @param mensagemId the mensagem ID
     * @throws SystemException if a system exception occurred
     */
    @Override
    public void removeBySituacaoMensagem(int situacaoEnvio, long mensagemId)
        throws SystemException {
        for (Agendamento agendamento : findBySituacaoMensagem(situacaoEnvio,
                mensagemId, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null)) {
            remove(agendamento);
        }
    }

    /**
     * Returns the number of agendamentos where situacaoEnvio = &#63; and mensagemId = &#63;.
     *
     * @param situacaoEnvio the situacao envio
     * @param mensagemId the mensagem ID
     * @return the number of matching agendamentos
     * @throws SystemException if a system exception occurred
     */
    @Override
    public int countBySituacaoMensagem(int situacaoEnvio, long mensagemId)
        throws SystemException {
        FinderPath finderPath = FINDER_PATH_COUNT_BY_SITUACAOMENSAGEM;

        Object[] finderArgs = new Object[] { situacaoEnvio, mensagemId };

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

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

            query.append(_SQL_COUNT_AGENDAMENTO_WHERE);

            query.append(_FINDER_COLUMN_SITUACAOMENSAGEM_SITUACAOENVIO_2);

            query.append(_FINDER_COLUMN_SITUACAOMENSAGEM_MENSAGEMID_2);

            String sql = query.toString();

            Session session = null;

            try {
                session = openSession();

                Query q = session.createQuery(sql);

                QueryPos qPos = QueryPos.getInstance(q);

                qPos.add(situacaoEnvio);

                qPos.add(mensagemId);

                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 agendamentos where mensagemId = &#63;.
     *
     * @param mensagemId the mensagem ID
     * @return the matching agendamentos
     * @throws SystemException if a system exception occurred
     */
    @Override
    public List<Agendamento> findByMensagemId(long mensagemId)
        throws SystemException {
        return findByMensagemId(mensagemId, QueryUtil.ALL_POS,
            QueryUtil.ALL_POS, null);
    }

    /**
     * Returns a range of all the agendamentos where mensagemId = &#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.AgendamentoModelImpl}. 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 mensagemId the mensagem ID
     * @param start the lower bound of the range of agendamentos
     * @param end the upper bound of the range of agendamentos (not inclusive)
     * @return the range of matching agendamentos
     * @throws SystemException if a system exception occurred
     */
    @Override
    public List<Agendamento> findByMensagemId(long mensagemId, int start,
        int end) throws SystemException {
        return findByMensagemId(mensagemId, start, end, null);
    }

    /**
     * Returns an ordered range of all the agendamentos where mensagemId = &#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.AgendamentoModelImpl}. 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 mensagemId the mensagem ID
     * @param start the lower bound of the range of agendamentos
     * @param end the upper bound of the range of agendamentos (not inclusive)
     * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
     * @return the ordered range of matching agendamentos
     * @throws SystemException if a system exception occurred
     */
    @Override
    public List<Agendamento> findByMensagemId(long mensagemId, 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_MENSAGEMID;
            finderArgs = new Object[] { mensagemId };
        } else {
            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_MENSAGEMID;
            finderArgs = new Object[] { mensagemId, start, end, orderByComparator };
        }

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

        if ((list != null) && !list.isEmpty()) {
            for (Agendamento agendamento : list) {
                if ((mensagemId != agendamento.getMensagemId())) {
                    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_AGENDAMENTO_WHERE);

            query.append(_FINDER_COLUMN_MENSAGEMID_MENSAGEMID_2);

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

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

                    Collections.sort(list);

                    list = new UnmodifiableList<Agendamento>(list);
                } else {
                    list = (List<Agendamento>) 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 agendamento in the ordered set where mensagemId = &#63;.
     *
     * @param mensagemId the mensagem ID
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the first matching agendamento
     * @throws br.mp.mprj.portal.portlet.gestaomensagens.service.NoSuchAgendamentoException if a matching agendamento could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public Agendamento findByMensagemId_First(long mensagemId,
        OrderByComparator orderByComparator)
        throws NoSuchAgendamentoException, SystemException {
        Agendamento agendamento = fetchByMensagemId_First(mensagemId,
                orderByComparator);

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

        StringBundler msg = new StringBundler(4);

        msg.append(_NO_SUCH_ENTITY_WITH_KEY);

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

        msg.append(StringPool.CLOSE_CURLY_BRACE);

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

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

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

        return null;
    }

    /**
     * Returns the last agendamento in the ordered set where mensagemId = &#63;.
     *
     * @param mensagemId the mensagem ID
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the last matching agendamento
     * @throws br.mp.mprj.portal.portlet.gestaomensagens.service.NoSuchAgendamentoException if a matching agendamento could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public Agendamento findByMensagemId_Last(long mensagemId,
        OrderByComparator orderByComparator)
        throws NoSuchAgendamentoException, SystemException {
        Agendamento agendamento = fetchByMensagemId_Last(mensagemId,
                orderByComparator);

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

        StringBundler msg = new StringBundler(4);

        msg.append(_NO_SUCH_ENTITY_WITH_KEY);

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

        msg.append(StringPool.CLOSE_CURLY_BRACE);

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

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

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

        List<Agendamento> list = findByMensagemId(mensagemId, count - 1, count,
                orderByComparator);

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

        return null;
    }

    /**
     * Returns the agendamentos before and after the current agendamento in the ordered set where mensagemId = &#63;.
     *
     * @param agendamentoId the primary key of the current agendamento
     * @param mensagemId the mensagem ID
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the previous, current, and next agendamento
     * @throws br.mp.mprj.portal.portlet.gestaomensagens.service.NoSuchAgendamentoException if a agendamento with the primary key could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public Agendamento[] findByMensagemId_PrevAndNext(long agendamentoId,
        long mensagemId, OrderByComparator orderByComparator)
        throws NoSuchAgendamentoException, SystemException {
        Agendamento agendamento = findByPrimaryKey(agendamentoId);

        Session session = null;

        try {
            session = openSession();

            Agendamento[] array = new AgendamentoImpl[3];

            array[0] = getByMensagemId_PrevAndNext(session, agendamento,
                    mensagemId, orderByComparator, true);

            array[1] = agendamento;

            array[2] = getByMensagemId_PrevAndNext(session, agendamento,
                    mensagemId, orderByComparator, false);

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

    protected Agendamento getByMensagemId_PrevAndNext(Session session,
        Agendamento agendamento, long mensagemId,
        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_AGENDAMENTO_WHERE);

        query.append(_FINDER_COLUMN_MENSAGEMID_MENSAGEMID_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(AgendamentoModelImpl.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(mensagemId);

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

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

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

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

    /**
     * Removes all the agendamentos where mensagemId = &#63; from the database.
     *
     * @param mensagemId the mensagem ID
     * @throws SystemException if a system exception occurred
     */
    @Override
    public void removeByMensagemId(long mensagemId) throws SystemException {
        for (Agendamento agendamento : findByMensagemId(mensagemId,
                QueryUtil.ALL_POS, QueryUtil.ALL_POS, null)) {
            remove(agendamento);
        }
    }

    /**
     * Returns the number of agendamentos where mensagemId = &#63;.
     *
     * @param mensagemId the mensagem ID
     * @return the number of matching agendamentos
     * @throws SystemException if a system exception occurred
     */
    @Override
    public int countByMensagemId(long mensagemId) throws SystemException {
        FinderPath finderPath = FINDER_PATH_COUNT_BY_MENSAGEMID;

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

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

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

            query.append(_SQL_COUNT_AGENDAMENTO_WHERE);

            query.append(_FINDER_COLUMN_MENSAGEMID_MENSAGEMID_2);

            String sql = query.toString();

            Session session = null;

            try {
                session = openSession();

                Query q = session.createQuery(sql);

                QueryPos qPos = QueryPos.getInstance(q);

                qPos.add(mensagemId);

                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 agendamentos where mensagemId = &#63; and situacaoEnvio = &#63;.
     *
     * @param mensagemId the mensagem ID
     * @param situacaoEnvio the situacao envio
     * @return the matching agendamentos
     * @throws SystemException if a system exception occurred
     */
    @Override
    public List<Agendamento> findByMensagemIdSituacaoEnvio(long mensagemId,
        int situacaoEnvio) throws SystemException {
        return findByMensagemIdSituacaoEnvio(mensagemId, situacaoEnvio,
            QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
    }

    /**
     * Returns a range of all the agendamentos where mensagemId = &#63; and situacaoEnvio = &#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.AgendamentoModelImpl}. 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 mensagemId the mensagem ID
     * @param situacaoEnvio the situacao envio
     * @param start the lower bound of the range of agendamentos
     * @param end the upper bound of the range of agendamentos (not inclusive)
     * @return the range of matching agendamentos
     * @throws SystemException if a system exception occurred
     */
    @Override
    public List<Agendamento> findByMensagemIdSituacaoEnvio(long mensagemId,
        int situacaoEnvio, int start, int end) throws SystemException {
        return findByMensagemIdSituacaoEnvio(mensagemId, situacaoEnvio, start,
            end, null);
    }

    /**
     * Returns an ordered range of all the agendamentos where mensagemId = &#63; and situacaoEnvio = &#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.AgendamentoModelImpl}. 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 mensagemId the mensagem ID
     * @param situacaoEnvio the situacao envio
     * @param start the lower bound of the range of agendamentos
     * @param end the upper bound of the range of agendamentos (not inclusive)
     * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
     * @return the ordered range of matching agendamentos
     * @throws SystemException if a system exception occurred
     */
    @Override
    public List<Agendamento> findByMensagemIdSituacaoEnvio(long mensagemId,
        int situacaoEnvio, 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_MENSAGEMIDSITUACAOENVIO;
            finderArgs = new Object[] { mensagemId, situacaoEnvio };
        } else {
            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_MENSAGEMIDSITUACAOENVIO;
            finderArgs = new Object[] {
                    mensagemId, situacaoEnvio,
                    
                    start, end, orderByComparator
                };
        }

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

        if ((list != null) && !list.isEmpty()) {
            for (Agendamento agendamento : list) {
                if ((mensagemId != agendamento.getMensagemId()) ||
                        (situacaoEnvio != agendamento.getSituacaoEnvio())) {
                    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_AGENDAMENTO_WHERE);

            query.append(_FINDER_COLUMN_MENSAGEMIDSITUACAOENVIO_MENSAGEMID_2);

            query.append(_FINDER_COLUMN_MENSAGEMIDSITUACAOENVIO_SITUACAOENVIO_2);

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

                qPos.add(situacaoEnvio);

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

                    Collections.sort(list);

                    list = new UnmodifiableList<Agendamento>(list);
                } else {
                    list = (List<Agendamento>) 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 agendamento in the ordered set where mensagemId = &#63; and situacaoEnvio = &#63;.
     *
     * @param mensagemId the mensagem ID
     * @param situacaoEnvio the situacao envio
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the first matching agendamento
     * @throws br.mp.mprj.portal.portlet.gestaomensagens.service.NoSuchAgendamentoException if a matching agendamento could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public Agendamento findByMensagemIdSituacaoEnvio_First(long mensagemId,
        int situacaoEnvio, OrderByComparator orderByComparator)
        throws NoSuchAgendamentoException, SystemException {
        Agendamento agendamento = fetchByMensagemIdSituacaoEnvio_First(mensagemId,
                situacaoEnvio, orderByComparator);

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

        StringBundler msg = new StringBundler(6);

        msg.append(_NO_SUCH_ENTITY_WITH_KEY);

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

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

        msg.append(StringPool.CLOSE_CURLY_BRACE);

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

    /**
     * Returns the first agendamento in the ordered set where mensagemId = &#63; and situacaoEnvio = &#63;.
     *
     * @param mensagemId the mensagem ID
     * @param situacaoEnvio the situacao envio
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the first matching agendamento, or <code>null</code> if a matching agendamento could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public Agendamento fetchByMensagemIdSituacaoEnvio_First(long mensagemId,
        int situacaoEnvio, OrderByComparator orderByComparator)
        throws SystemException {
        List<Agendamento> list = findByMensagemIdSituacaoEnvio(mensagemId,
                situacaoEnvio, 0, 1, orderByComparator);

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

        return null;
    }

    /**
     * Returns the last agendamento in the ordered set where mensagemId = &#63; and situacaoEnvio = &#63;.
     *
     * @param mensagemId the mensagem ID
     * @param situacaoEnvio the situacao envio
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the last matching agendamento
     * @throws br.mp.mprj.portal.portlet.gestaomensagens.service.NoSuchAgendamentoException if a matching agendamento could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public Agendamento findByMensagemIdSituacaoEnvio_Last(long mensagemId,
        int situacaoEnvio, OrderByComparator orderByComparator)
        throws NoSuchAgendamentoException, SystemException {
        Agendamento agendamento = fetchByMensagemIdSituacaoEnvio_Last(mensagemId,
                situacaoEnvio, orderByComparator);

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

        StringBundler msg = new StringBundler(6);

        msg.append(_NO_SUCH_ENTITY_WITH_KEY);

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

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

        msg.append(StringPool.CLOSE_CURLY_BRACE);

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

    /**
     * Returns the last agendamento in the ordered set where mensagemId = &#63; and situacaoEnvio = &#63;.
     *
     * @param mensagemId the mensagem ID
     * @param situacaoEnvio the situacao envio
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the last matching agendamento, or <code>null</code> if a matching agendamento could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public Agendamento fetchByMensagemIdSituacaoEnvio_Last(long mensagemId,
        int situacaoEnvio, OrderByComparator orderByComparator)
        throws SystemException {
        int count = countByMensagemIdSituacaoEnvio(mensagemId, situacaoEnvio);

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

        List<Agendamento> list = findByMensagemIdSituacaoEnvio(mensagemId,
                situacaoEnvio, count - 1, count, orderByComparator);

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

        return null;
    }

    /**
     * Returns the agendamentos before and after the current agendamento in the ordered set where mensagemId = &#63; and situacaoEnvio = &#63;.
     *
     * @param agendamentoId the primary key of the current agendamento
     * @param mensagemId the mensagem ID
     * @param situacaoEnvio the situacao envio
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the previous, current, and next agendamento
     * @throws br.mp.mprj.portal.portlet.gestaomensagens.service.NoSuchAgendamentoException if a agendamento with the primary key could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public Agendamento[] findByMensagemIdSituacaoEnvio_PrevAndNext(
        long agendamentoId, long mensagemId, int situacaoEnvio,
        OrderByComparator orderByComparator)
        throws NoSuchAgendamentoException, SystemException {
        Agendamento agendamento = findByPrimaryKey(agendamentoId);

        Session session = null;

        try {
            session = openSession();

            Agendamento[] array = new AgendamentoImpl[3];

            array[0] = getByMensagemIdSituacaoEnvio_PrevAndNext(session,
                    agendamento, mensagemId, situacaoEnvio, orderByComparator,
                    true);

            array[1] = agendamento;

            array[2] = getByMensagemIdSituacaoEnvio_PrevAndNext(session,
                    agendamento, mensagemId, situacaoEnvio, orderByComparator,
                    false);

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

    protected Agendamento getByMensagemIdSituacaoEnvio_PrevAndNext(
        Session session, Agendamento agendamento, long mensagemId,
        int situacaoEnvio, 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_AGENDAMENTO_WHERE);

        query.append(_FINDER_COLUMN_MENSAGEMIDSITUACAOENVIO_MENSAGEMID_2);

        query.append(_FINDER_COLUMN_MENSAGEMIDSITUACAOENVIO_SITUACAOENVIO_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(AgendamentoModelImpl.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(mensagemId);

        qPos.add(situacaoEnvio);

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

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

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

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

    /**
     * Removes all the agendamentos where mensagemId = &#63; and situacaoEnvio = &#63; from the database.
     *
     * @param mensagemId the mensagem ID
     * @param situacaoEnvio the situacao envio
     * @throws SystemException if a system exception occurred
     */
    @Override
    public void removeByMensagemIdSituacaoEnvio(long mensagemId,
        int situacaoEnvio) throws SystemException {
        for (Agendamento agendamento : findByMensagemIdSituacaoEnvio(
                mensagemId, situacaoEnvio, QueryUtil.ALL_POS,
                QueryUtil.ALL_POS, null)) {
            remove(agendamento);
        }
    }

    /**
     * Returns the number of agendamentos where mensagemId = &#63; and situacaoEnvio = &#63;.
     *
     * @param mensagemId the mensagem ID
     * @param situacaoEnvio the situacao envio
     * @return the number of matching agendamentos
     * @throws SystemException if a system exception occurred
     */
    @Override
    public int countByMensagemIdSituacaoEnvio(long mensagemId, int situacaoEnvio)
        throws SystemException {
        FinderPath finderPath = FINDER_PATH_COUNT_BY_MENSAGEMIDSITUACAOENVIO;

        Object[] finderArgs = new Object[] { mensagemId, situacaoEnvio };

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

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

            query.append(_SQL_COUNT_AGENDAMENTO_WHERE);

            query.append(_FINDER_COLUMN_MENSAGEMIDSITUACAOENVIO_MENSAGEMID_2);

            query.append(_FINDER_COLUMN_MENSAGEMIDSITUACAOENVIO_SITUACAOENVIO_2);

            String sql = query.toString();

            Session session = null;

            try {
                session = openSession();

                Query q = session.createQuery(sql);

                QueryPos qPos = QueryPos.getInstance(q);

                qPos.add(mensagemId);

                qPos.add(situacaoEnvio);

                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 agendamentos where mensagemId = &#63; and tipoAgendamento = &#63; and situacaoEnvio = &#63; and agendamentoId &ne; &#63;.
     *
     * @param mensagemId the mensagem ID
     * @param tipoAgendamento the tipo agendamento
     * @param situacaoEnvio the situacao envio
     * @param agendamentoId the agendamento ID
     * @return the matching agendamentos
     * @throws SystemException if a system exception occurred
     */
    @Override
    public List<Agendamento> findByAgendamentoExclusao(long mensagemId,
        int tipoAgendamento, int situacaoEnvio, long agendamentoId)
        throws SystemException {
        return findByAgendamentoExclusao(mensagemId, tipoAgendamento,
            situacaoEnvio, agendamentoId, QueryUtil.ALL_POS, QueryUtil.ALL_POS,
            null);
    }

    /**
     * Returns a range of all the agendamentos where mensagemId = &#63; and tipoAgendamento = &#63; and situacaoEnvio = &#63; and agendamentoId &ne; &#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.AgendamentoModelImpl}. 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 mensagemId the mensagem ID
     * @param tipoAgendamento the tipo agendamento
     * @param situacaoEnvio the situacao envio
     * @param agendamentoId the agendamento ID
     * @param start the lower bound of the range of agendamentos
     * @param end the upper bound of the range of agendamentos (not inclusive)
     * @return the range of matching agendamentos
     * @throws SystemException if a system exception occurred
     */
    @Override
    public List<Agendamento> findByAgendamentoExclusao(long mensagemId,
        int tipoAgendamento, int situacaoEnvio, long agendamentoId, int start,
        int end) throws SystemException {
        return findByAgendamentoExclusao(mensagemId, tipoAgendamento,
            situacaoEnvio, agendamentoId, start, end, null);
    }

    /**
     * Returns an ordered range of all the agendamentos where mensagemId = &#63; and tipoAgendamento = &#63; and situacaoEnvio = &#63; and agendamentoId &ne; &#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.AgendamentoModelImpl}. 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 mensagemId the mensagem ID
     * @param tipoAgendamento the tipo agendamento
     * @param situacaoEnvio the situacao envio
     * @param agendamentoId the agendamento ID
     * @param start the lower bound of the range of agendamentos
     * @param end the upper bound of the range of agendamentos (not inclusive)
     * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
     * @return the ordered range of matching agendamentos
     * @throws SystemException if a system exception occurred
     */
    @Override
    public List<Agendamento> findByAgendamentoExclusao(long mensagemId,
        int tipoAgendamento, int situacaoEnvio, long agendamentoId, int start,
        int end, OrderByComparator orderByComparator) throws SystemException {
        boolean pagination = true;
        FinderPath finderPath = null;
        Object[] finderArgs = null;

        finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_AGENDAMENTOEXCLUSAO;
        finderArgs = new Object[] {
                mensagemId, tipoAgendamento, situacaoEnvio, agendamentoId,
                
                start, end, orderByComparator
            };

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

        if ((list != null) && !list.isEmpty()) {
            for (Agendamento agendamento : list) {
                if ((mensagemId != agendamento.getMensagemId()) ||
                        (tipoAgendamento != agendamento.getTipoAgendamento()) ||
                        (situacaoEnvio != agendamento.getSituacaoEnvio()) ||
                        (agendamentoId == agendamento.getAgendamentoId())) {
                    list = null;

                    break;
                }
            }
        }

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

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

            query.append(_SQL_SELECT_AGENDAMENTO_WHERE);

            query.append(_FINDER_COLUMN_AGENDAMENTOEXCLUSAO_MENSAGEMID_2);

            query.append(_FINDER_COLUMN_AGENDAMENTOEXCLUSAO_TIPOAGENDAMENTO_2);

            query.append(_FINDER_COLUMN_AGENDAMENTOEXCLUSAO_SITUACAOENVIO_2);

            query.append(_FINDER_COLUMN_AGENDAMENTOEXCLUSAO_AGENDAMENTOID_2);

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

                qPos.add(tipoAgendamento);

                qPos.add(situacaoEnvio);

                qPos.add(agendamentoId);

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

                    Collections.sort(list);

                    list = new UnmodifiableList<Agendamento>(list);
                } else {
                    list = (List<Agendamento>) 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 agendamento in the ordered set where mensagemId = &#63; and tipoAgendamento = &#63; and situacaoEnvio = &#63; and agendamentoId &ne; &#63;.
     *
     * @param mensagemId the mensagem ID
     * @param tipoAgendamento the tipo agendamento
     * @param situacaoEnvio the situacao envio
     * @param agendamentoId the agendamento ID
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the first matching agendamento
     * @throws br.mp.mprj.portal.portlet.gestaomensagens.service.NoSuchAgendamentoException if a matching agendamento could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public Agendamento findByAgendamentoExclusao_First(long mensagemId,
        int tipoAgendamento, int situacaoEnvio, long agendamentoId,
        OrderByComparator orderByComparator)
        throws NoSuchAgendamentoException, SystemException {
        Agendamento agendamento = fetchByAgendamentoExclusao_First(mensagemId,
                tipoAgendamento, situacaoEnvio, agendamentoId, orderByComparator);

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

        StringBundler msg = new StringBundler(10);

        msg.append(_NO_SUCH_ENTITY_WITH_KEY);

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

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

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

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

        msg.append(StringPool.CLOSE_CURLY_BRACE);

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

    /**
     * Returns the first agendamento in the ordered set where mensagemId = &#63; and tipoAgendamento = &#63; and situacaoEnvio = &#63; and agendamentoId &ne; &#63;.
     *
     * @param mensagemId the mensagem ID
     * @param tipoAgendamento the tipo agendamento
     * @param situacaoEnvio the situacao envio
     * @param agendamentoId the agendamento ID
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the first matching agendamento, or <code>null</code> if a matching agendamento could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public Agendamento fetchByAgendamentoExclusao_First(long mensagemId,
        int tipoAgendamento, int situacaoEnvio, long agendamentoId,
        OrderByComparator orderByComparator) throws SystemException {
        List<Agendamento> list = findByAgendamentoExclusao(mensagemId,
                tipoAgendamento, situacaoEnvio, agendamentoId, 0, 1,
                orderByComparator);

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

        return null;
    }

    /**
     * Returns the last agendamento in the ordered set where mensagemId = &#63; and tipoAgendamento = &#63; and situacaoEnvio = &#63; and agendamentoId &ne; &#63;.
     *
     * @param mensagemId the mensagem ID
     * @param tipoAgendamento the tipo agendamento
     * @param situacaoEnvio the situacao envio
     * @param agendamentoId the agendamento ID
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the last matching agendamento
     * @throws br.mp.mprj.portal.portlet.gestaomensagens.service.NoSuchAgendamentoException if a matching agendamento could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public Agendamento findByAgendamentoExclusao_Last(long mensagemId,
        int tipoAgendamento, int situacaoEnvio, long agendamentoId,
        OrderByComparator orderByComparator)
        throws NoSuchAgendamentoException, SystemException {
        Agendamento agendamento = fetchByAgendamentoExclusao_Last(mensagemId,
                tipoAgendamento, situacaoEnvio, agendamentoId, orderByComparator);

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

        StringBundler msg = new StringBundler(10);

        msg.append(_NO_SUCH_ENTITY_WITH_KEY);

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

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

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

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

        msg.append(StringPool.CLOSE_CURLY_BRACE);

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

    /**
     * Returns the last agendamento in the ordered set where mensagemId = &#63; and tipoAgendamento = &#63; and situacaoEnvio = &#63; and agendamentoId &ne; &#63;.
     *
     * @param mensagemId the mensagem ID
     * @param tipoAgendamento the tipo agendamento
     * @param situacaoEnvio the situacao envio
     * @param agendamentoId the agendamento ID
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the last matching agendamento, or <code>null</code> if a matching agendamento could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public Agendamento fetchByAgendamentoExclusao_Last(long mensagemId,
        int tipoAgendamento, int situacaoEnvio, long agendamentoId,
        OrderByComparator orderByComparator) throws SystemException {
        int count = countByAgendamentoExclusao(mensagemId, tipoAgendamento,
                situacaoEnvio, agendamentoId);

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

        List<Agendamento> list = findByAgendamentoExclusao(mensagemId,
                tipoAgendamento, situacaoEnvio, agendamentoId, count - 1,
                count, orderByComparator);

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

        return null;
    }

    /**
     * Returns all the agendamentos where mensagemId = &#63; and tipoAgendamento = any &#63; and situacaoEnvio = &#63; and agendamentoId &ne; all &#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.AgendamentoModelImpl}. 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 mensagemId the mensagem ID
     * @param tipoAgendamentos the tipo agendamentos
     * @param situacaoEnvio the situacao envio
     * @param agendamentoIds the agendamento IDs
     * @return the matching agendamentos
     * @throws SystemException if a system exception occurred
     */
    @Override
    public List<Agendamento> findByAgendamentoExclusao(long mensagemId,
        int[] tipoAgendamentos, int situacaoEnvio, long[] agendamentoIds)
        throws SystemException {
        return findByAgendamentoExclusao(mensagemId, tipoAgendamentos,
            situacaoEnvio, agendamentoIds, QueryUtil.ALL_POS,
            QueryUtil.ALL_POS, null);
    }

    /**
     * Returns a range of all the agendamentos where mensagemId = &#63; and tipoAgendamento = any &#63; and situacaoEnvio = &#63; and agendamentoId &ne; all &#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.AgendamentoModelImpl}. 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 mensagemId the mensagem ID
     * @param tipoAgendamentos the tipo agendamentos
     * @param situacaoEnvio the situacao envio
     * @param agendamentoIds the agendamento IDs
     * @param start the lower bound of the range of agendamentos
     * @param end the upper bound of the range of agendamentos (not inclusive)
     * @return the range of matching agendamentos
     * @throws SystemException if a system exception occurred
     */
    @Override
    public List<Agendamento> findByAgendamentoExclusao(long mensagemId,
        int[] tipoAgendamentos, int situacaoEnvio, long[] agendamentoIds,
        int start, int end) throws SystemException {
        return findByAgendamentoExclusao(mensagemId, tipoAgendamentos,
            situacaoEnvio, agendamentoIds, start, end, null);
    }

    /**
     * Returns an ordered range of all the agendamentos where mensagemId = &#63; and tipoAgendamento = any &#63; and situacaoEnvio = &#63; and agendamentoId &ne; all &#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.AgendamentoModelImpl}. 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 mensagemId the mensagem ID
     * @param tipoAgendamentos the tipo agendamentos
     * @param situacaoEnvio the situacao envio
     * @param agendamentoIds the agendamento IDs
     * @param start the lower bound of the range of agendamentos
     * @param end the upper bound of the range of agendamentos (not inclusive)
     * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
     * @return the ordered range of matching agendamentos
     * @throws SystemException if a system exception occurred
     */
    @Override
    public List<Agendamento> findByAgendamentoExclusao(long mensagemId,
        int[] tipoAgendamentos, int situacaoEnvio, long[] agendamentoIds,
        int start, int end, OrderByComparator orderByComparator)
        throws SystemException {
        if ((tipoAgendamentos != null) && (tipoAgendamentos.length == 1) &&
                (agendamentoIds != null) && (agendamentoIds.length == 1)) {
            return findByAgendamentoExclusao(mensagemId, tipoAgendamentos[0],
                situacaoEnvio, agendamentoIds[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[] {
                    mensagemId, StringUtil.merge(tipoAgendamentos),
                    situacaoEnvio, StringUtil.merge(agendamentoIds)
                };
        } else {
            finderArgs = new Object[] {
                    mensagemId, StringUtil.merge(tipoAgendamentos),
                    situacaoEnvio, StringUtil.merge(agendamentoIds),
                    
                    start, end, orderByComparator
                };
        }

        List<Agendamento> list = (List<Agendamento>) FinderCacheUtil.getResult(FINDER_PATH_WITH_PAGINATION_FIND_BY_AGENDAMENTOEXCLUSAO,
                finderArgs, this);

        if ((list != null) && !list.isEmpty()) {
            for (Agendamento agendamento : list) {
                if ((mensagemId != agendamento.getMensagemId()) ||
                        !ArrayUtil.contains(tipoAgendamentos,
                            agendamento.getTipoAgendamento()) ||
                        (situacaoEnvio != agendamento.getSituacaoEnvio()) ||
                        !ArrayUtil.contains(agendamentoIds,
                            agendamento.getAgendamentoId())) {
                    list = null;

                    break;
                }
            }
        }

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

            query.append(_SQL_SELECT_AGENDAMENTO_WHERE);

            boolean conjunctionable = false;

            if (conjunctionable) {
                query.append(WHERE_AND);
            }

            query.append(_FINDER_COLUMN_AGENDAMENTOEXCLUSAO_MENSAGEMID_5);

            conjunctionable = true;

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

                query.append(StringPool.OPEN_PARENTHESIS);

                for (int i = 0; i < tipoAgendamentos.length; i++) {
                    query.append(_FINDER_COLUMN_AGENDAMENTOEXCLUSAO_TIPOAGENDAMENTO_5);

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

                query.append(StringPool.CLOSE_PARENTHESIS);

                conjunctionable = true;
            }

            if (conjunctionable) {
                query.append(WHERE_AND);
            }

            query.append(_FINDER_COLUMN_AGENDAMENTOEXCLUSAO_SITUACAOENVIO_5);

            conjunctionable = true;

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

                query.append(StringPool.OPEN_PARENTHESIS);

                for (int i = 0; i < agendamentoIds.length; i++) {
                    query.append(_FINDER_COLUMN_AGENDAMENTOEXCLUSAO_AGENDAMENTOID_5);

                    if ((i + 1) < agendamentoIds.length) {
                        query.append(WHERE_AND);
                    }
                }

                query.append(StringPool.CLOSE_PARENTHESIS);

                conjunctionable = true;
            }

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

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

                qPos.add(situacaoEnvio);

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

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

                    Collections.sort(list);

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

                cacheResult(list);

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

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

        return list;
    }

    /**
     * Removes all the agendamentos where mensagemId = &#63; and tipoAgendamento = &#63; and situacaoEnvio = &#63; and agendamentoId &ne; &#63; from the database.
     *
     * @param mensagemId the mensagem ID
     * @param tipoAgendamento the tipo agendamento
     * @param situacaoEnvio the situacao envio
     * @param agendamentoId the agendamento ID
     * @throws SystemException if a system exception occurred
     */
    @Override
    public void removeByAgendamentoExclusao(long mensagemId,
        int tipoAgendamento, int situacaoEnvio, long agendamentoId)
        throws SystemException {
        for (Agendamento agendamento : findByAgendamentoExclusao(mensagemId,
                tipoAgendamento, situacaoEnvio, agendamentoId,
                QueryUtil.ALL_POS, QueryUtil.ALL_POS, null)) {
            remove(agendamento);
        }
    }

    /**
     * Returns the number of agendamentos where mensagemId = &#63; and tipoAgendamento = &#63; and situacaoEnvio = &#63; and agendamentoId &ne; &#63;.
     *
     * @param mensagemId the mensagem ID
     * @param tipoAgendamento the tipo agendamento
     * @param situacaoEnvio the situacao envio
     * @param agendamentoId the agendamento ID
     * @return the number of matching agendamentos
     * @throws SystemException if a system exception occurred
     */
    @Override
    public int countByAgendamentoExclusao(long mensagemId, int tipoAgendamento,
        int situacaoEnvio, long agendamentoId) throws SystemException {
        FinderPath finderPath = FINDER_PATH_WITH_PAGINATION_COUNT_BY_AGENDAMENTOEXCLUSAO;

        Object[] finderArgs = new Object[] {
                mensagemId, tipoAgendamento, situacaoEnvio, agendamentoId
            };

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

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

            query.append(_SQL_COUNT_AGENDAMENTO_WHERE);

            query.append(_FINDER_COLUMN_AGENDAMENTOEXCLUSAO_MENSAGEMID_2);

            query.append(_FINDER_COLUMN_AGENDAMENTOEXCLUSAO_TIPOAGENDAMENTO_2);

            query.append(_FINDER_COLUMN_AGENDAMENTOEXCLUSAO_SITUACAOENVIO_2);

            query.append(_FINDER_COLUMN_AGENDAMENTOEXCLUSAO_AGENDAMENTOID_2);

            String sql = query.toString();

            Session session = null;

            try {
                session = openSession();

                Query q = session.createQuery(sql);

                QueryPos qPos = QueryPos.getInstance(q);

                qPos.add(mensagemId);

                qPos.add(tipoAgendamento);

                qPos.add(situacaoEnvio);

                qPos.add(agendamentoId);

                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 agendamentos where mensagemId = &#63; and tipoAgendamento = any &#63; and situacaoEnvio = &#63; and agendamentoId &ne; all &#63;.
     *
     * @param mensagemId the mensagem ID
     * @param tipoAgendamentos the tipo agendamentos
     * @param situacaoEnvio the situacao envio
     * @param agendamentoIds the agendamento IDs
     * @return the number of matching agendamentos
     * @throws SystemException if a system exception occurred
     */
    @Override
    public int countByAgendamentoExclusao(long mensagemId,
        int[] tipoAgendamentos, int situacaoEnvio, long[] agendamentoIds)
        throws SystemException {
        Object[] finderArgs = new Object[] {
                mensagemId, StringUtil.merge(tipoAgendamentos), situacaoEnvio,
                StringUtil.merge(agendamentoIds)
            };

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

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

            query.append(_SQL_COUNT_AGENDAMENTO_WHERE);

            boolean conjunctionable = false;

            if (conjunctionable) {
                query.append(WHERE_AND);
            }

            query.append(_FINDER_COLUMN_AGENDAMENTOEXCLUSAO_MENSAGEMID_5);

            conjunctionable = true;

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

                query.append(StringPool.OPEN_PARENTHESIS);

                for (int i = 0; i < tipoAgendamentos.length; i++) {
                    query.append(_FINDER_COLUMN_AGENDAMENTOEXCLUSAO_TIPOAGENDAMENTO_5);

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

                query.append(StringPool.CLOSE_PARENTHESIS);

                conjunctionable = true;
            }

            if (conjunctionable) {
                query.append(WHERE_AND);
            }

            query.append(_FINDER_COLUMN_AGENDAMENTOEXCLUSAO_SITUACAOENVIO_5);

            conjunctionable = true;

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

                query.append(StringPool.OPEN_PARENTHESIS);

                for (int i = 0; i < agendamentoIds.length; i++) {
                    query.append(_FINDER_COLUMN_AGENDAMENTOEXCLUSAO_AGENDAMENTOID_5);

                    if ((i + 1) < agendamentoIds.length) {
                        query.append(WHERE_AND);
                    }
                }

                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);

                qPos.add(mensagemId);

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

                qPos.add(situacaoEnvio);

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

                count = (Long) q.uniqueResult();

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

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

        return count.intValue();
    }

    /**
     * Caches the agendamento in the entity cache if it is enabled.
     *
     * @param agendamento the agendamento
     */
    @Override
    public void cacheResult(Agendamento agendamento) {
        EntityCacheUtil.putResult(AgendamentoModelImpl.ENTITY_CACHE_ENABLED,
            AgendamentoImpl.class, agendamento.getPrimaryKey(), agendamento);

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

        agendamento.resetOriginalValues();
    }

    /**
     * Caches the agendamentos in the entity cache if it is enabled.
     *
     * @param agendamentos the agendamentos
     */
    @Override
    public void cacheResult(List<Agendamento> agendamentos) {
        for (Agendamento agendamento : agendamentos) {
            if (EntityCacheUtil.getResult(
                        AgendamentoModelImpl.ENTITY_CACHE_ENABLED,
                        AgendamentoImpl.class, agendamento.getPrimaryKey()) == null) {
                cacheResult(agendamento);
            } else {
                agendamento.resetOriginalValues();
            }
        }
    }

    /**
     * Clears the cache for all agendamentos.
     *
     * <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(AgendamentoImpl.class.getName());
        }

        EntityCacheUtil.clearCache(AgendamentoImpl.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 agendamento.
     *
     * <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(Agendamento agendamento) {
        EntityCacheUtil.removeResult(AgendamentoModelImpl.ENTITY_CACHE_ENABLED,
            AgendamentoImpl.class, agendamento.getPrimaryKey());

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

        clearUniqueFindersCache(agendamento);
    }

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

        for (Agendamento agendamento : agendamentos) {
            EntityCacheUtil.removeResult(AgendamentoModelImpl.ENTITY_CACHE_ENABLED,
                AgendamentoImpl.class, agendamento.getPrimaryKey());

            clearUniqueFindersCache(agendamento);
        }
    }

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

            FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_UUID_G, args,
                Long.valueOf(1));
            FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_UUID_G, args,
                agendamento);
        } else {
            AgendamentoModelImpl agendamentoModelImpl = (AgendamentoModelImpl) agendamento;

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

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

    protected void clearUniqueFindersCache(Agendamento agendamento) {
        AgendamentoModelImpl agendamentoModelImpl = (AgendamentoModelImpl) agendamento;

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

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

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

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

    /**
     * Creates a new agendamento with the primary key. Does not add the agendamento to the database.
     *
     * @param agendamentoId the primary key for the new agendamento
     * @return the new agendamento
     */
    @Override
    public Agendamento create(long agendamentoId) {
        Agendamento agendamento = new AgendamentoImpl();

        agendamento.setNew(true);
        agendamento.setPrimaryKey(agendamentoId);

        String uuid = PortalUUIDUtil.generate();

        agendamento.setUuid(uuid);

        return agendamento;
    }

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

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

        try {
            session = openSession();

            Agendamento agendamento = (Agendamento) session.get(AgendamentoImpl.class,
                    primaryKey);

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

                throw new NoSuchAgendamentoException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
                    primaryKey);
            }

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

    @Override
    protected Agendamento removeImpl(Agendamento agendamento)
        throws SystemException {
        agendamento = toUnwrappedModel(agendamento);

        Session session = null;

        try {
            session = openSession();

            if (!session.contains(agendamento)) {
                agendamento = (Agendamento) session.get(AgendamentoImpl.class,
                        agendamento.getPrimaryKeyObj());
            }

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

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

        return agendamento;
    }

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

        boolean isNew = agendamento.isNew();

        AgendamentoModelImpl agendamentoModelImpl = (AgendamentoModelImpl) agendamento;

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

            agendamento.setUuid(uuid);
        }

        Session session = null;

        try {
            session = openSession();

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

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

        FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);

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

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

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

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

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

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

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

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

            if ((agendamentoModelImpl.getColumnBitmask() &
                    FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_MENSAGEMTIPO.getColumnBitmask()) != 0) {
                Object[] args = new Object[] {
                        agendamentoModelImpl.getOriginalMensagemId(),
                        agendamentoModelImpl.getOriginalTipoAgendamento()
                    };

                FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_MENSAGEMTIPO,
                    args);
                FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_MENSAGEMTIPO,
                    args);

                args = new Object[] {
                        agendamentoModelImpl.getMensagemId(),
                        agendamentoModelImpl.getTipoAgendamento()
                    };

                FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_MENSAGEMTIPO,
                    args);
                FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_MENSAGEMTIPO,
                    args);
            }

            if ((agendamentoModelImpl.getColumnBitmask() &
                    FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_MENSAGEMTIPOMULTIPO.getColumnBitmask()) != 0) {
                Object[] args = new Object[] {
                        agendamentoModelImpl.getOriginalMensagemId(),
                        agendamentoModelImpl.getOriginalTipoAgendamento()
                    };

                FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_MENSAGEMTIPOMULTIPO,
                    args);
                FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_MENSAGEMTIPOMULTIPO,
                    args);

                args = new Object[] {
                        agendamentoModelImpl.getMensagemId(),
                        agendamentoModelImpl.getTipoAgendamento()
                    };

                FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_MENSAGEMTIPOMULTIPO,
                    args);
                FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_MENSAGEMTIPOMULTIPO,
                    args);
            }

            if ((agendamentoModelImpl.getColumnBitmask() &
                    FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_MENSAGEMDATACALENDARIO.getColumnBitmask()) != 0) {
                Object[] args = new Object[] {
                        agendamentoModelImpl.getOriginalMensagemId(),
                        agendamentoModelImpl.getOriginalDataCalendario()
                    };

                FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_MENSAGEMDATACALENDARIO,
                    args);
                FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_MENSAGEMDATACALENDARIO,
                    args);

                args = new Object[] {
                        agendamentoModelImpl.getMensagemId(),
                        agendamentoModelImpl.getDataCalendario()
                    };

                FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_MENSAGEMDATACALENDARIO,
                    args);
                FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_MENSAGEMDATACALENDARIO,
                    args);
            }

            if ((agendamentoModelImpl.getColumnBitmask() &
                    FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_SITUACAO.getColumnBitmask()) != 0) {
                Object[] args = new Object[] {
                        agendamentoModelImpl.getOriginalSituacaoEnvio()
                    };

                FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_SITUACAO, args);
                FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_SITUACAO,
                    args);

                args = new Object[] { agendamentoModelImpl.getSituacaoEnvio() };

                FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_SITUACAO, args);
                FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_SITUACAO,
                    args);
            }

            if ((agendamentoModelImpl.getColumnBitmask() &
                    FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_SITUACAOMENSAGEM.getColumnBitmask()) != 0) {
                Object[] args = new Object[] {
                        agendamentoModelImpl.getOriginalSituacaoEnvio(),
                        agendamentoModelImpl.getOriginalMensagemId()
                    };

                FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_SITUACAOMENSAGEM,
                    args);
                FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_SITUACAOMENSAGEM,
                    args);

                args = new Object[] {
                        agendamentoModelImpl.getSituacaoEnvio(),
                        agendamentoModelImpl.getMensagemId()
                    };

                FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_SITUACAOMENSAGEM,
                    args);
                FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_SITUACAOMENSAGEM,
                    args);
            }

            if ((agendamentoModelImpl.getColumnBitmask() &
                    FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_MENSAGEMID.getColumnBitmask()) != 0) {
                Object[] args = new Object[] {
                        agendamentoModelImpl.getOriginalMensagemId()
                    };

                FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_MENSAGEMID,
                    args);
                FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_MENSAGEMID,
                    args);

                args = new Object[] { agendamentoModelImpl.getMensagemId() };

                FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_MENSAGEMID,
                    args);
                FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_MENSAGEMID,
                    args);
            }

            if ((agendamentoModelImpl.getColumnBitmask() &
                    FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_MENSAGEMIDSITUACAOENVIO.getColumnBitmask()) != 0) {
                Object[] args = new Object[] {
                        agendamentoModelImpl.getOriginalMensagemId(),
                        agendamentoModelImpl.getOriginalSituacaoEnvio()
                    };

                FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_MENSAGEMIDSITUACAOENVIO,
                    args);
                FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_MENSAGEMIDSITUACAOENVIO,
                    args);

                args = new Object[] {
                        agendamentoModelImpl.getMensagemId(),
                        agendamentoModelImpl.getSituacaoEnvio()
                    };

                FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_MENSAGEMIDSITUACAOENVIO,
                    args);
                FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_MENSAGEMIDSITUACAOENVIO,
                    args);
            }
        }

        EntityCacheUtil.putResult(AgendamentoModelImpl.ENTITY_CACHE_ENABLED,
            AgendamentoImpl.class, agendamento.getPrimaryKey(), agendamento);

        clearUniqueFindersCache(agendamento);
        cacheUniqueFindersCache(agendamento);

        return agendamento;
    }

    protected Agendamento toUnwrappedModel(Agendamento agendamento) {
        if (agendamento instanceof AgendamentoImpl) {
            return agendamento;
        }

        AgendamentoImpl agendamentoImpl = new AgendamentoImpl();

        agendamentoImpl.setNew(agendamento.isNew());
        agendamentoImpl.setPrimaryKey(agendamento.getPrimaryKey());

        agendamentoImpl.setUuid(agendamento.getUuid());
        agendamentoImpl.setAgendamentoId(agendamento.getAgendamentoId());
        agendamentoImpl.setMensagemId(agendamento.getMensagemId());
        agendamentoImpl.setCompanyId(agendamento.getCompanyId());
        agendamentoImpl.setGroupId(agendamento.getGroupId());
        agendamentoImpl.setTipoAgendamento(agendamento.getTipoAgendamento());
        agendamentoImpl.setDataCalendario(agendamento.getDataCalendario());
        agendamentoImpl.setDataInicio(agendamento.getDataInicio());
        agendamentoImpl.setDataFim(agendamento.getDataFim());
        agendamentoImpl.setPadraoRecorrencia(agendamento.getPadraoRecorrencia());
        agendamentoImpl.setFrequencia(agendamento.getFrequencia());
        agendamentoImpl.setDiarioTodos(agendamento.isDiarioTodos());
        agendamentoImpl.setSemanalDias(agendamento.getSemanalDias());
        agendamentoImpl.setMensalDiaFrequencia(agendamento.getMensalDiaFrequencia());
        agendamentoImpl.setDataAtualizacao(agendamento.getDataAtualizacao());
        agendamentoImpl.setDataEnvio(agendamento.getDataEnvio());
        agendamentoImpl.setSituacaoEnvio(agendamento.getSituacaoEnvio());
        agendamentoImpl.setErroEnvio(agendamento.isErroEnvio());
        agendamentoImpl.setMensagemErro(agendamento.getMensagemErro());

        return agendamentoImpl;
    }

    /**
     * Returns the agendamento 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 agendamento
     * @return the agendamento
     * @throws br.mp.mprj.portal.portlet.gestaomensagens.service.NoSuchAgendamentoException if a agendamento with the primary key could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public Agendamento findByPrimaryKey(Serializable primaryKey)
        throws NoSuchAgendamentoException, SystemException {
        Agendamento agendamento = fetchByPrimaryKey(primaryKey);

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

            throw new NoSuchAgendamentoException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
                primaryKey);
        }

        return agendamento;
    }

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

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

        if (agendamento == _nullAgendamento) {
            return null;
        }

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

            try {
                session = openSession();

                agendamento = (Agendamento) session.get(AgendamentoImpl.class,
                        primaryKey);

                if (agendamento != null) {
                    cacheResult(agendamento);
                } else {
                    EntityCacheUtil.putResult(AgendamentoModelImpl.ENTITY_CACHE_ENABLED,
                        AgendamentoImpl.class, primaryKey, _nullAgendamento);
                }
            } catch (Exception e) {
                EntityCacheUtil.removeResult(AgendamentoModelImpl.ENTITY_CACHE_ENABLED,
                    AgendamentoImpl.class, primaryKey);

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

        return agendamento;
    }

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

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

    /**
     * Returns a range of all the agendamentos.
     *
     * <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.AgendamentoModelImpl}. 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 agendamentos
     * @param end the upper bound of the range of agendamentos (not inclusive)
     * @return the range of agendamentos
     * @throws SystemException if a system exception occurred
     */
    @Override
    public List<Agendamento> findAll(int start, int end)
        throws SystemException {
        return findAll(start, end, null);
    }

    /**
     * Returns an ordered range of all the agendamentos.
     *
     * <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.AgendamentoModelImpl}. 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 agendamentos
     * @param end the upper bound of the range of agendamentos (not inclusive)
     * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
     * @return the ordered range of agendamentos
     * @throws SystemException if a system exception occurred
     */
    @Override
    public List<Agendamento> 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<Agendamento> list = (List<Agendamento>) 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_AGENDAMENTO);

                appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
                    orderByComparator);

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

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

            Session session = null;

            try {
                session = openSession();

                Query q = session.createQuery(sql);

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

                    Collections.sort(list);

                    list = new UnmodifiableList<Agendamento>(list);
                } else {
                    list = (List<Agendamento>) 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 agendamentos from the database.
     *
     * @throws SystemException if a system exception occurred
     */
    @Override
    public void removeAll() throws SystemException {
        for (Agendamento agendamento : findAll()) {
            remove(agendamento);
        }
    }

    /**
     * Returns the number of agendamentos.
     *
     * @return the number of agendamentos
     * @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_AGENDAMENTO);

                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 agendamento 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.Agendamento")));

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

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

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

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