/** * Copyright (c) 2000-present Liferay, Inc. All rights reserved. * * This library is free software; you can redistribute it and/or modify it under * the terms of the GNU Lesser General Public License as published by the Free * Software Foundation; either version 2.1 of the License, or (at your option) * any later version. * * This library is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more * details. */ package com.liferay.mail.reader.service.persistence.impl; import aQute.bnd.annotation.ProviderType; import com.liferay.mail.reader.exception.NoSuchMessageException; import com.liferay.mail.reader.model.Message; import com.liferay.mail.reader.model.impl.MessageImpl; import com.liferay.mail.reader.model.impl.MessageModelImpl; import com.liferay.mail.reader.service.persistence.MessagePersistence; import com.liferay.portal.kernel.dao.orm.EntityCache; import com.liferay.portal.kernel.dao.orm.FinderCache; import com.liferay.portal.kernel.dao.orm.FinderPath; import com.liferay.portal.kernel.dao.orm.Query; import com.liferay.portal.kernel.dao.orm.QueryPos; import com.liferay.portal.kernel.dao.orm.QueryUtil; import com.liferay.portal.kernel.dao.orm.Session; import com.liferay.portal.kernel.log.Log; import com.liferay.portal.kernel.log.LogFactoryUtil; import com.liferay.portal.kernel.service.ServiceContext; import com.liferay.portal.kernel.service.ServiceContextThreadLocal; import com.liferay.portal.kernel.service.persistence.CompanyProvider; import com.liferay.portal.kernel.service.persistence.CompanyProviderWrapper; import com.liferay.portal.kernel.service.persistence.impl.BasePersistenceImpl; import com.liferay.portal.kernel.util.OrderByComparator; import com.liferay.portal.kernel.util.ReflectionUtil; 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.spring.extender.service.ServiceReference; import java.io.Serializable; import java.lang.reflect.Field; import java.util.Collections; import java.util.Date; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Set; /** * The persistence implementation for the message service. * * <p> * Caching information and settings can be found in <code>portal.properties</code> * </p> * * @author Brian Wing Shun Chan * @see MessagePersistence * @see com.liferay.mail.reader.service.persistence.MessageUtil * @generated */ @ProviderType public class MessagePersistenceImpl extends BasePersistenceImpl<Message> implements MessagePersistence { /* * NOTE FOR DEVELOPERS: * * Never modify or reference this class directly. Always use {@link MessageUtil} to access the message persistence. Modify <code>service.xml</code> and rerun ServiceBuilder to regenerate this class. */ public static final String FINDER_CLASS_NAME_ENTITY = MessageImpl.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(MessageModelImpl.ENTITY_CACHE_ENABLED, MessageModelImpl.FINDER_CACHE_ENABLED, MessageImpl.class, FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findAll", new String[0]); public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL = new FinderPath(MessageModelImpl.ENTITY_CACHE_ENABLED, MessageModelImpl.FINDER_CACHE_ENABLED, MessageImpl.class, FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findAll", new String[0]); public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(MessageModelImpl.ENTITY_CACHE_ENABLED, MessageModelImpl.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_COMPANYID = new FinderPath(MessageModelImpl.ENTITY_CACHE_ENABLED, MessageModelImpl.FINDER_CACHE_ENABLED, MessageImpl.class, FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findByCompanyId", new String[] { Long.class.getName(), Integer.class.getName(), Integer.class.getName(), OrderByComparator.class.getName() }); public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_COMPANYID = new FinderPath(MessageModelImpl.ENTITY_CACHE_ENABLED, MessageModelImpl.FINDER_CACHE_ENABLED, MessageImpl.class, FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findByCompanyId", new String[] { Long.class.getName() }, MessageModelImpl.COMPANYID_COLUMN_BITMASK | MessageModelImpl.SENTDATE_COLUMN_BITMASK); public static final FinderPath FINDER_PATH_COUNT_BY_COMPANYID = new FinderPath(MessageModelImpl.ENTITY_CACHE_ENABLED, MessageModelImpl.FINDER_CACHE_ENABLED, Long.class, FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByCompanyId", new String[] { Long.class.getName() }); /** * Returns all the messages where companyId = ?. * * @param companyId the company ID * @return the matching messages */ @Override public List<Message> findByCompanyId(long companyId) { return findByCompanyId(companyId, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null); } /** * Returns a range of all the messages where companyId = ?. * * <p> * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link MessageModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order. * </p> * * @param companyId the company ID * @param start the lower bound of the range of messages * @param end the upper bound of the range of messages (not inclusive) * @return the range of matching messages */ @Override public List<Message> findByCompanyId(long companyId, int start, int end) { return findByCompanyId(companyId, start, end, null); } /** * Returns an ordered range of all the messages where companyId = ?. * * <p> * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link MessageModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order. * </p> * * @param companyId the company ID * @param start the lower bound of the range of messages * @param end the upper bound of the range of messages (not inclusive) * @param orderByComparator the comparator to order the results by (optionally <code>null</code>) * @return the ordered range of matching messages */ @Override public List<Message> findByCompanyId(long companyId, int start, int end, OrderByComparator<Message> orderByComparator) { return findByCompanyId(companyId, start, end, orderByComparator, true); } /** * Returns an ordered range of all the messages where companyId = ?. * * <p> * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link MessageModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order. * </p> * * @param companyId the company ID * @param start the lower bound of the range of messages * @param end the upper bound of the range of messages (not inclusive) * @param orderByComparator the comparator to order the results by (optionally <code>null</code>) * @param retrieveFromCache whether to retrieve from the finder cache * @return the ordered range of matching messages */ @Override public List<Message> findByCompanyId(long companyId, int start, int end, OrderByComparator<Message> orderByComparator, boolean retrieveFromCache) { boolean pagination = true; FinderPath finderPath = null; Object[] finderArgs = null; if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) && (orderByComparator == null)) { pagination = false; finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_COMPANYID; finderArgs = new Object[] { companyId }; } else { finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_COMPANYID; finderArgs = new Object[] { companyId, start, end, orderByComparator }; } List<Message> list = null; if (retrieveFromCache) { list = (List<Message>)finderCache.getResult(finderPath, finderArgs, this); if ((list != null) && !list.isEmpty()) { for (Message message : list) { if ((companyId != message.getCompanyId())) { list = null; break; } } } } if (list == null) { StringBundler query = null; if (orderByComparator != null) { query = new StringBundler(3 + (orderByComparator.getOrderByFields().length * 2)); } else { query = new StringBundler(3); } query.append(_SQL_SELECT_MESSAGE_WHERE); query.append(_FINDER_COLUMN_COMPANYID_COMPANYID_2); if (orderByComparator != null) { appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS, orderByComparator); } else if (pagination) { query.append(MessageModelImpl.ORDER_BY_JPQL); } String sql = query.toString(); Session session = null; try { session = openSession(); Query q = session.createQuery(sql); QueryPos qPos = QueryPos.getInstance(q); qPos.add(companyId); if (!pagination) { list = (List<Message>)QueryUtil.list(q, getDialect(), start, end, false); Collections.sort(list); list = Collections.unmodifiableList(list); } else { list = (List<Message>)QueryUtil.list(q, getDialect(), start, end); } cacheResult(list); finderCache.putResult(finderPath, finderArgs, list); } catch (Exception e) { finderCache.removeResult(finderPath, finderArgs); throw processException(e); } finally { closeSession(session); } } return list; } /** * Returns the first message in the ordered set where companyId = ?. * * @param companyId the company ID * @param orderByComparator the comparator to order the set by (optionally <code>null</code>) * @return the first matching message * @throws NoSuchMessageException if a matching message could not be found */ @Override public Message findByCompanyId_First(long companyId, OrderByComparator<Message> orderByComparator) throws NoSuchMessageException { Message message = fetchByCompanyId_First(companyId, orderByComparator); if (message != null) { return message; } StringBundler msg = new StringBundler(4); msg.append(_NO_SUCH_ENTITY_WITH_KEY); msg.append("companyId="); msg.append(companyId); msg.append(StringPool.CLOSE_CURLY_BRACE); throw new NoSuchMessageException(msg.toString()); } /** * Returns the first message in the ordered set where companyId = ?. * * @param companyId the company ID * @param orderByComparator the comparator to order the set by (optionally <code>null</code>) * @return the first matching message, or <code>null</code> if a matching message could not be found */ @Override public Message fetchByCompanyId_First(long companyId, OrderByComparator<Message> orderByComparator) { List<Message> list = findByCompanyId(companyId, 0, 1, orderByComparator); if (!list.isEmpty()) { return list.get(0); } return null; } /** * Returns the last message in the ordered set where companyId = ?. * * @param companyId the company ID * @param orderByComparator the comparator to order the set by (optionally <code>null</code>) * @return the last matching message * @throws NoSuchMessageException if a matching message could not be found */ @Override public Message findByCompanyId_Last(long companyId, OrderByComparator<Message> orderByComparator) throws NoSuchMessageException { Message message = fetchByCompanyId_Last(companyId, orderByComparator); if (message != null) { return message; } StringBundler msg = new StringBundler(4); msg.append(_NO_SUCH_ENTITY_WITH_KEY); msg.append("companyId="); msg.append(companyId); msg.append(StringPool.CLOSE_CURLY_BRACE); throw new NoSuchMessageException(msg.toString()); } /** * Returns the last message in the ordered set where companyId = ?. * * @param companyId the company ID * @param orderByComparator the comparator to order the set by (optionally <code>null</code>) * @return the last matching message, or <code>null</code> if a matching message could not be found */ @Override public Message fetchByCompanyId_Last(long companyId, OrderByComparator<Message> orderByComparator) { int count = countByCompanyId(companyId); if (count == 0) { return null; } List<Message> list = findByCompanyId(companyId, count - 1, count, orderByComparator); if (!list.isEmpty()) { return list.get(0); } return null; } /** * Returns the messages before and after the current message in the ordered set where companyId = ?. * * @param messageId the primary key of the current message * @param companyId the company ID * @param orderByComparator the comparator to order the set by (optionally <code>null</code>) * @return the previous, current, and next message * @throws NoSuchMessageException if a message with the primary key could not be found */ @Override public Message[] findByCompanyId_PrevAndNext(long messageId, long companyId, OrderByComparator<Message> orderByComparator) throws NoSuchMessageException { Message message = findByPrimaryKey(messageId); Session session = null; try { session = openSession(); Message[] array = new MessageImpl[3]; array[0] = getByCompanyId_PrevAndNext(session, message, companyId, orderByComparator, true); array[1] = message; array[2] = getByCompanyId_PrevAndNext(session, message, companyId, orderByComparator, false); return array; } catch (Exception e) { throw processException(e); } finally { closeSession(session); } } protected Message getByCompanyId_PrevAndNext(Session session, Message message, long companyId, OrderByComparator<Message> orderByComparator, boolean previous) { StringBundler query = null; if (orderByComparator != null) { query = new StringBundler(4 + (orderByComparator.getOrderByConditionFields().length * 3) + (orderByComparator.getOrderByFields().length * 3)); } else { query = new StringBundler(3); } query.append(_SQL_SELECT_MESSAGE_WHERE); query.append(_FINDER_COLUMN_COMPANYID_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(MessageModelImpl.ORDER_BY_JPQL); } String sql = query.toString(); Query q = session.createQuery(sql); q.setFirstResult(0); q.setMaxResults(2); QueryPos qPos = QueryPos.getInstance(q); qPos.add(companyId); if (orderByComparator != null) { Object[] values = orderByComparator.getOrderByConditionValues(message); for (Object value : values) { qPos.add(value); } } List<Message> list = q.list(); if (list.size() == 2) { return list.get(1); } else { return null; } } /** * Removes all the messages where companyId = ? from the database. * * @param companyId the company ID */ @Override public void removeByCompanyId(long companyId) { for (Message message : findByCompanyId(companyId, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null)) { remove(message); } } /** * Returns the number of messages where companyId = ?. * * @param companyId the company ID * @return the number of matching messages */ @Override public int countByCompanyId(long companyId) { FinderPath finderPath = FINDER_PATH_COUNT_BY_COMPANYID; Object[] finderArgs = new Object[] { companyId }; Long count = (Long)finderCache.getResult(finderPath, finderArgs, this); if (count == null) { StringBundler query = new StringBundler(2); query.append(_SQL_COUNT_MESSAGE_WHERE); query.append(_FINDER_COLUMN_COMPANYID_COMPANYID_2); String sql = query.toString(); Session session = null; try { session = openSession(); Query q = session.createQuery(sql); QueryPos qPos = QueryPos.getInstance(q); qPos.add(companyId); count = (Long)q.uniqueResult(); finderCache.putResult(finderPath, finderArgs, count); } catch (Exception e) { finderCache.removeResult(finderPath, finderArgs); throw processException(e); } finally { closeSession(session); } } return count.intValue(); } private static final String _FINDER_COLUMN_COMPANYID_COMPANYID_2 = "message.companyId = ?"; public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_FOLDERID = new FinderPath(MessageModelImpl.ENTITY_CACHE_ENABLED, MessageModelImpl.FINDER_CACHE_ENABLED, MessageImpl.class, FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findByFolderId", new String[] { Long.class.getName(), Integer.class.getName(), Integer.class.getName(), OrderByComparator.class.getName() }); public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_FOLDERID = new FinderPath(MessageModelImpl.ENTITY_CACHE_ENABLED, MessageModelImpl.FINDER_CACHE_ENABLED, MessageImpl.class, FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findByFolderId", new String[] { Long.class.getName() }, MessageModelImpl.FOLDERID_COLUMN_BITMASK | MessageModelImpl.SENTDATE_COLUMN_BITMASK); public static final FinderPath FINDER_PATH_COUNT_BY_FOLDERID = new FinderPath(MessageModelImpl.ENTITY_CACHE_ENABLED, MessageModelImpl.FINDER_CACHE_ENABLED, Long.class, FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByFolderId", new String[] { Long.class.getName() }); /** * Returns all the messages where folderId = ?. * * @param folderId the folder ID * @return the matching messages */ @Override public List<Message> findByFolderId(long folderId) { return findByFolderId(folderId, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null); } /** * Returns a range of all the messages where folderId = ?. * * <p> * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link MessageModelImpl}. 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 folderId the folder ID * @param start the lower bound of the range of messages * @param end the upper bound of the range of messages (not inclusive) * @return the range of matching messages */ @Override public List<Message> findByFolderId(long folderId, int start, int end) { return findByFolderId(folderId, start, end, null); } /** * Returns an ordered range of all the messages where folderId = ?. * * <p> * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link MessageModelImpl}. 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 folderId the folder ID * @param start the lower bound of the range of messages * @param end the upper bound of the range of messages (not inclusive) * @param orderByComparator the comparator to order the results by (optionally <code>null</code>) * @return the ordered range of matching messages */ @Override public List<Message> findByFolderId(long folderId, int start, int end, OrderByComparator<Message> orderByComparator) { return findByFolderId(folderId, start, end, orderByComparator, true); } /** * Returns an ordered range of all the messages where folderId = ?. * * <p> * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link MessageModelImpl}. 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 folderId the folder ID * @param start the lower bound of the range of messages * @param end the upper bound of the range of messages (not inclusive) * @param orderByComparator the comparator to order the results by (optionally <code>null</code>) * @param retrieveFromCache whether to retrieve from the finder cache * @return the ordered range of matching messages */ @Override public List<Message> findByFolderId(long folderId, int start, int end, OrderByComparator<Message> orderByComparator, boolean retrieveFromCache) { boolean pagination = true; FinderPath finderPath = null; Object[] finderArgs = null; if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) && (orderByComparator == null)) { pagination = false; finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_FOLDERID; finderArgs = new Object[] { folderId }; } else { finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_FOLDERID; finderArgs = new Object[] { folderId, start, end, orderByComparator }; } List<Message> list = null; if (retrieveFromCache) { list = (List<Message>)finderCache.getResult(finderPath, finderArgs, this); if ((list != null) && !list.isEmpty()) { for (Message message : list) { if ((folderId != message.getFolderId())) { list = null; break; } } } } if (list == null) { StringBundler query = null; if (orderByComparator != null) { query = new StringBundler(3 + (orderByComparator.getOrderByFields().length * 2)); } else { query = new StringBundler(3); } query.append(_SQL_SELECT_MESSAGE_WHERE); query.append(_FINDER_COLUMN_FOLDERID_FOLDERID_2); if (orderByComparator != null) { appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS, orderByComparator); } else if (pagination) { query.append(MessageModelImpl.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(folderId); if (!pagination) { list = (List<Message>)QueryUtil.list(q, getDialect(), start, end, false); Collections.sort(list); list = Collections.unmodifiableList(list); } else { list = (List<Message>)QueryUtil.list(q, getDialect(), start, end); } cacheResult(list); finderCache.putResult(finderPath, finderArgs, list); } catch (Exception e) { finderCache.removeResult(finderPath, finderArgs); throw processException(e); } finally { closeSession(session); } } return list; } /** * Returns the first message in the ordered set where folderId = ?. * * @param folderId the folder ID * @param orderByComparator the comparator to order the set by (optionally <code>null</code>) * @return the first matching message * @throws NoSuchMessageException if a matching message could not be found */ @Override public Message findByFolderId_First(long folderId, OrderByComparator<Message> orderByComparator) throws NoSuchMessageException { Message message = fetchByFolderId_First(folderId, orderByComparator); if (message != null) { return message; } StringBundler msg = new StringBundler(4); msg.append(_NO_SUCH_ENTITY_WITH_KEY); msg.append("folderId="); msg.append(folderId); msg.append(StringPool.CLOSE_CURLY_BRACE); throw new NoSuchMessageException(msg.toString()); } /** * Returns the first message in the ordered set where folderId = ?. * * @param folderId the folder ID * @param orderByComparator the comparator to order the set by (optionally <code>null</code>) * @return the first matching message, or <code>null</code> if a matching message could not be found */ @Override public Message fetchByFolderId_First(long folderId, OrderByComparator<Message> orderByComparator) { List<Message> list = findByFolderId(folderId, 0, 1, orderByComparator); if (!list.isEmpty()) { return list.get(0); } return null; } /** * Returns the last message in the ordered set where folderId = ?. * * @param folderId the folder ID * @param orderByComparator the comparator to order the set by (optionally <code>null</code>) * @return the last matching message * @throws NoSuchMessageException if a matching message could not be found */ @Override public Message findByFolderId_Last(long folderId, OrderByComparator<Message> orderByComparator) throws NoSuchMessageException { Message message = fetchByFolderId_Last(folderId, orderByComparator); if (message != null) { return message; } StringBundler msg = new StringBundler(4); msg.append(_NO_SUCH_ENTITY_WITH_KEY); msg.append("folderId="); msg.append(folderId); msg.append(StringPool.CLOSE_CURLY_BRACE); throw new NoSuchMessageException(msg.toString()); } /** * Returns the last message in the ordered set where folderId = ?. * * @param folderId the folder ID * @param orderByComparator the comparator to order the set by (optionally <code>null</code>) * @return the last matching message, or <code>null</code> if a matching message could not be found */ @Override public Message fetchByFolderId_Last(long folderId, OrderByComparator<Message> orderByComparator) { int count = countByFolderId(folderId); if (count == 0) { return null; } List<Message> list = findByFolderId(folderId, count - 1, count, orderByComparator); if (!list.isEmpty()) { return list.get(0); } return null; } /** * Returns the messages before and after the current message in the ordered set where folderId = ?. * * @param messageId the primary key of the current message * @param folderId the folder ID * @param orderByComparator the comparator to order the set by (optionally <code>null</code>) * @return the previous, current, and next message * @throws NoSuchMessageException if a message with the primary key could not be found */ @Override public Message[] findByFolderId_PrevAndNext(long messageId, long folderId, OrderByComparator<Message> orderByComparator) throws NoSuchMessageException { Message message = findByPrimaryKey(messageId); Session session = null; try { session = openSession(); Message[] array = new MessageImpl[3]; array[0] = getByFolderId_PrevAndNext(session, message, folderId, orderByComparator, true); array[1] = message; array[2] = getByFolderId_PrevAndNext(session, message, folderId, orderByComparator, false); return array; } catch (Exception e) { throw processException(e); } finally { closeSession(session); } } protected Message getByFolderId_PrevAndNext(Session session, Message message, long folderId, OrderByComparator<Message> orderByComparator, boolean previous) { StringBundler query = null; if (orderByComparator != null) { query = new StringBundler(4 + (orderByComparator.getOrderByConditionFields().length * 3) + (orderByComparator.getOrderByFields().length * 3)); } else { query = new StringBundler(3); } query.append(_SQL_SELECT_MESSAGE_WHERE); query.append(_FINDER_COLUMN_FOLDERID_FOLDERID_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(MessageModelImpl.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(folderId); if (orderByComparator != null) { Object[] values = orderByComparator.getOrderByConditionValues(message); for (Object value : values) { qPos.add(value); } } List<Message> list = q.list(); if (list.size() == 2) { return list.get(1); } else { return null; } } /** * Removes all the messages where folderId = ? from the database. * * @param folderId the folder ID */ @Override public void removeByFolderId(long folderId) { for (Message message : findByFolderId(folderId, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null)) { remove(message); } } /** * Returns the number of messages where folderId = ?. * * @param folderId the folder ID * @return the number of matching messages */ @Override public int countByFolderId(long folderId) { FinderPath finderPath = FINDER_PATH_COUNT_BY_FOLDERID; Object[] finderArgs = new Object[] { folderId }; Long count = (Long)finderCache.getResult(finderPath, finderArgs, this); if (count == null) { StringBundler query = new StringBundler(2); query.append(_SQL_COUNT_MESSAGE_WHERE); query.append(_FINDER_COLUMN_FOLDERID_FOLDERID_2); String sql = query.toString(); Session session = null; try { session = openSession(); Query q = session.createQuery(sql); QueryPos qPos = QueryPos.getInstance(q); qPos.add(folderId); count = (Long)q.uniqueResult(); finderCache.putResult(finderPath, finderArgs, count); } catch (Exception e) { finderCache.removeResult(finderPath, finderArgs); throw processException(e); } finally { closeSession(session); } } return count.intValue(); } private static final String _FINDER_COLUMN_FOLDERID_FOLDERID_2 = "message.folderId = ?"; public static final FinderPath FINDER_PATH_FETCH_BY_F_R = new FinderPath(MessageModelImpl.ENTITY_CACHE_ENABLED, MessageModelImpl.FINDER_CACHE_ENABLED, MessageImpl.class, FINDER_CLASS_NAME_ENTITY, "fetchByF_R", new String[] { Long.class.getName(), Long.class.getName() }, MessageModelImpl.FOLDERID_COLUMN_BITMASK | MessageModelImpl.REMOTEMESSAGEID_COLUMN_BITMASK); public static final FinderPath FINDER_PATH_COUNT_BY_F_R = new FinderPath(MessageModelImpl.ENTITY_CACHE_ENABLED, MessageModelImpl.FINDER_CACHE_ENABLED, Long.class, FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByF_R", new String[] { Long.class.getName(), Long.class.getName() }); /** * Returns the message where folderId = ? and remoteMessageId = ? or throws a {@link NoSuchMessageException} if it could not be found. * * @param folderId the folder ID * @param remoteMessageId the remote message ID * @return the matching message * @throws NoSuchMessageException if a matching message could not be found */ @Override public Message findByF_R(long folderId, long remoteMessageId) throws NoSuchMessageException { Message message = fetchByF_R(folderId, remoteMessageId); if (message == null) { StringBundler msg = new StringBundler(6); msg.append(_NO_SUCH_ENTITY_WITH_KEY); msg.append("folderId="); msg.append(folderId); msg.append(", remoteMessageId="); msg.append(remoteMessageId); msg.append(StringPool.CLOSE_CURLY_BRACE); if (_log.isDebugEnabled()) { _log.debug(msg.toString()); } throw new NoSuchMessageException(msg.toString()); } return message; } /** * Returns the message where folderId = ? and remoteMessageId = ? or returns <code>null</code> if it could not be found. Uses the finder cache. * * @param folderId the folder ID * @param remoteMessageId the remote message ID * @return the matching message, or <code>null</code> if a matching message could not be found */ @Override public Message fetchByF_R(long folderId, long remoteMessageId) { return fetchByF_R(folderId, remoteMessageId, true); } /** * Returns the message where folderId = ? and remoteMessageId = ? or returns <code>null</code> if it could not be found, optionally using the finder cache. * * @param folderId the folder ID * @param remoteMessageId the remote message ID * @param retrieveFromCache whether to retrieve from the finder cache * @return the matching message, or <code>null</code> if a matching message could not be found */ @Override public Message fetchByF_R(long folderId, long remoteMessageId, boolean retrieveFromCache) { Object[] finderArgs = new Object[] { folderId, remoteMessageId }; Object result = null; if (retrieveFromCache) { result = finderCache.getResult(FINDER_PATH_FETCH_BY_F_R, finderArgs, this); } if (result instanceof Message) { Message message = (Message)result; if ((folderId != message.getFolderId()) || (remoteMessageId != message.getRemoteMessageId())) { result = null; } } if (result == null) { StringBundler query = new StringBundler(4); query.append(_SQL_SELECT_MESSAGE_WHERE); query.append(_FINDER_COLUMN_F_R_FOLDERID_2); query.append(_FINDER_COLUMN_F_R_REMOTEMESSAGEID_2); String sql = query.toString(); Session session = null; try { session = openSession(); Query q = session.createQuery(sql); QueryPos qPos = QueryPos.getInstance(q); qPos.add(folderId); qPos.add(remoteMessageId); List<Message> list = q.list(); if (list.isEmpty()) { finderCache.putResult(FINDER_PATH_FETCH_BY_F_R, finderArgs, list); } else { if (list.size() > 1) { Collections.sort(list, Collections.reverseOrder()); if (_log.isWarnEnabled()) { _log.warn( "MessagePersistenceImpl.fetchByF_R(long, long, boolean) with parameters (" + StringUtil.merge(finderArgs) + ") yields a result set with more than 1 result. This violates the logical unique restriction. There is no order guarantee on which result is returned by this finder."); } } Message message = list.get(0); result = message; cacheResult(message); if ((message.getFolderId() != folderId) || (message.getRemoteMessageId() != remoteMessageId)) { finderCache.putResult(FINDER_PATH_FETCH_BY_F_R, finderArgs, message); } } } catch (Exception e) { finderCache.removeResult(FINDER_PATH_FETCH_BY_F_R, finderArgs); throw processException(e); } finally { closeSession(session); } } if (result instanceof List<?>) { return null; } else { return (Message)result; } } /** * Removes the message where folderId = ? and remoteMessageId = ? from the database. * * @param folderId the folder ID * @param remoteMessageId the remote message ID * @return the message that was removed */ @Override public Message removeByF_R(long folderId, long remoteMessageId) throws NoSuchMessageException { Message message = findByF_R(folderId, remoteMessageId); return remove(message); } /** * Returns the number of messages where folderId = ? and remoteMessageId = ?. * * @param folderId the folder ID * @param remoteMessageId the remote message ID * @return the number of matching messages */ @Override public int countByF_R(long folderId, long remoteMessageId) { FinderPath finderPath = FINDER_PATH_COUNT_BY_F_R; Object[] finderArgs = new Object[] { folderId, remoteMessageId }; Long count = (Long)finderCache.getResult(finderPath, finderArgs, this); if (count == null) { StringBundler query = new StringBundler(3); query.append(_SQL_COUNT_MESSAGE_WHERE); query.append(_FINDER_COLUMN_F_R_FOLDERID_2); query.append(_FINDER_COLUMN_F_R_REMOTEMESSAGEID_2); String sql = query.toString(); Session session = null; try { session = openSession(); Query q = session.createQuery(sql); QueryPos qPos = QueryPos.getInstance(q); qPos.add(folderId); qPos.add(remoteMessageId); count = (Long)q.uniqueResult(); finderCache.putResult(finderPath, finderArgs, count); } catch (Exception e) { finderCache.removeResult(finderPath, finderArgs); throw processException(e); } finally { closeSession(session); } } return count.intValue(); } private static final String _FINDER_COLUMN_F_R_FOLDERID_2 = "message.folderId = ? AND "; private static final String _FINDER_COLUMN_F_R_REMOTEMESSAGEID_2 = "message.remoteMessageId = ?"; public MessagePersistenceImpl() { setModelClass(Message.class); try { Field field = ReflectionUtil.getDeclaredField(BasePersistenceImpl.class, "_dbColumnNames"); Map<String, String> dbColumnNames = new HashMap<String, String>(); dbColumnNames.put("to", "to_"); dbColumnNames.put("size", "size_"); field.set(this, dbColumnNames); } catch (Exception e) { if (_log.isDebugEnabled()) { _log.debug(e, e); } } } /** * Caches the message in the entity cache if it is enabled. * * @param message the message */ @Override public void cacheResult(Message message) { entityCache.putResult(MessageModelImpl.ENTITY_CACHE_ENABLED, MessageImpl.class, message.getPrimaryKey(), message); finderCache.putResult(FINDER_PATH_FETCH_BY_F_R, new Object[] { message.getFolderId(), message.getRemoteMessageId() }, message); message.resetOriginalValues(); } /** * Caches the messages in the entity cache if it is enabled. * * @param messages the messages */ @Override public void cacheResult(List<Message> messages) { for (Message message : messages) { if (entityCache.getResult(MessageModelImpl.ENTITY_CACHE_ENABLED, MessageImpl.class, message.getPrimaryKey()) == null) { cacheResult(message); } else { message.resetOriginalValues(); } } } /** * Clears the cache for all messages. * * <p> * The {@link EntityCache} and {@link FinderCache} are both cleared by this method. * </p> */ @Override public void clearCache() { entityCache.clearCache(MessageImpl.class); finderCache.clearCache(FINDER_CLASS_NAME_ENTITY); finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION); finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION); } /** * Clears the cache for the message. * * <p> * The {@link EntityCache} and {@link FinderCache} are both cleared by this method. * </p> */ @Override public void clearCache(Message message) { entityCache.removeResult(MessageModelImpl.ENTITY_CACHE_ENABLED, MessageImpl.class, message.getPrimaryKey()); finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION); finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION); clearUniqueFindersCache((MessageModelImpl)message, true); } @Override public void clearCache(List<Message> messages) { finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION); finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION); for (Message message : messages) { entityCache.removeResult(MessageModelImpl.ENTITY_CACHE_ENABLED, MessageImpl.class, message.getPrimaryKey()); clearUniqueFindersCache((MessageModelImpl)message, true); } } protected void cacheUniqueFindersCache(MessageModelImpl messageModelImpl) { Object[] args = new Object[] { messageModelImpl.getFolderId(), messageModelImpl.getRemoteMessageId() }; finderCache.putResult(FINDER_PATH_COUNT_BY_F_R, args, Long.valueOf(1), false); finderCache.putResult(FINDER_PATH_FETCH_BY_F_R, args, messageModelImpl, false); } protected void clearUniqueFindersCache(MessageModelImpl messageModelImpl, boolean clearCurrent) { if (clearCurrent) { Object[] args = new Object[] { messageModelImpl.getFolderId(), messageModelImpl.getRemoteMessageId() }; finderCache.removeResult(FINDER_PATH_COUNT_BY_F_R, args); finderCache.removeResult(FINDER_PATH_FETCH_BY_F_R, args); } if ((messageModelImpl.getColumnBitmask() & FINDER_PATH_FETCH_BY_F_R.getColumnBitmask()) != 0) { Object[] args = new Object[] { messageModelImpl.getOriginalFolderId(), messageModelImpl.getOriginalRemoteMessageId() }; finderCache.removeResult(FINDER_PATH_COUNT_BY_F_R, args); finderCache.removeResult(FINDER_PATH_FETCH_BY_F_R, args); } } /** * Creates a new message with the primary key. Does not add the message to the database. * * @param messageId the primary key for the new message * @return the new message */ @Override public Message create(long messageId) { Message message = new MessageImpl(); message.setNew(true); message.setPrimaryKey(messageId); message.setCompanyId(companyProvider.getCompanyId()); return message; } /** * Removes the message with the primary key from the database. Also notifies the appropriate model listeners. * * @param messageId the primary key of the message * @return the message that was removed * @throws NoSuchMessageException if a message with the primary key could not be found */ @Override public Message remove(long messageId) throws NoSuchMessageException { return remove((Serializable)messageId); } /** * Removes the message with the primary key from the database. Also notifies the appropriate model listeners. * * @param primaryKey the primary key of the message * @return the message that was removed * @throws NoSuchMessageException if a message with the primary key could not be found */ @Override public Message remove(Serializable primaryKey) throws NoSuchMessageException { Session session = null; try { session = openSession(); Message message = (Message)session.get(MessageImpl.class, primaryKey); if (message == null) { if (_log.isDebugEnabled()) { _log.debug(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey); } throw new NoSuchMessageException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey); } return remove(message); } catch (NoSuchMessageException nsee) { throw nsee; } catch (Exception e) { throw processException(e); } finally { closeSession(session); } } @Override protected Message removeImpl(Message message) { message = toUnwrappedModel(message); Session session = null; try { session = openSession(); if (!session.contains(message)) { message = (Message)session.get(MessageImpl.class, message.getPrimaryKeyObj()); } if (message != null) { session.delete(message); } } catch (Exception e) { throw processException(e); } finally { closeSession(session); } if (message != null) { clearCache(message); } return message; } @Override public Message updateImpl(Message message) { message = toUnwrappedModel(message); boolean isNew = message.isNew(); MessageModelImpl messageModelImpl = (MessageModelImpl)message; ServiceContext serviceContext = ServiceContextThreadLocal.getServiceContext(); Date now = new Date(); if (isNew && (message.getCreateDate() == null)) { if (serviceContext == null) { message.setCreateDate(now); } else { message.setCreateDate(serviceContext.getCreateDate(now)); } } if (!messageModelImpl.hasSetModifiedDate()) { if (serviceContext == null) { message.setModifiedDate(now); } else { message.setModifiedDate(serviceContext.getModifiedDate(now)); } } Session session = null; try { session = openSession(); if (message.isNew()) { session.save(message); message.setNew(false); } else { message = (Message)session.merge(message); } } catch (Exception e) { throw processException(e); } finally { closeSession(session); } finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION); if (!MessageModelImpl.COLUMN_BITMASK_ENABLED) { finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION); } else if (isNew) { Object[] args = new Object[] { messageModelImpl.getCompanyId() }; finderCache.removeResult(FINDER_PATH_COUNT_BY_COMPANYID, args); finderCache.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_COMPANYID, args); args = new Object[] { messageModelImpl.getFolderId() }; finderCache.removeResult(FINDER_PATH_COUNT_BY_FOLDERID, args); finderCache.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_FOLDERID, args); finderCache.removeResult(FINDER_PATH_COUNT_ALL, FINDER_ARGS_EMPTY); finderCache.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL, FINDER_ARGS_EMPTY); } else { if ((messageModelImpl.getColumnBitmask() & FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_COMPANYID.getColumnBitmask()) != 0) { Object[] args = new Object[] { messageModelImpl.getOriginalCompanyId() }; finderCache.removeResult(FINDER_PATH_COUNT_BY_COMPANYID, args); finderCache.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_COMPANYID, args); args = new Object[] { messageModelImpl.getCompanyId() }; finderCache.removeResult(FINDER_PATH_COUNT_BY_COMPANYID, args); finderCache.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_COMPANYID, args); } if ((messageModelImpl.getColumnBitmask() & FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_FOLDERID.getColumnBitmask()) != 0) { Object[] args = new Object[] { messageModelImpl.getOriginalFolderId() }; finderCache.removeResult(FINDER_PATH_COUNT_BY_FOLDERID, args); finderCache.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_FOLDERID, args); args = new Object[] { messageModelImpl.getFolderId() }; finderCache.removeResult(FINDER_PATH_COUNT_BY_FOLDERID, args); finderCache.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_FOLDERID, args); } } entityCache.putResult(MessageModelImpl.ENTITY_CACHE_ENABLED, MessageImpl.class, message.getPrimaryKey(), message, false); clearUniqueFindersCache(messageModelImpl, false); cacheUniqueFindersCache(messageModelImpl); message.resetOriginalValues(); return message; } protected Message toUnwrappedModel(Message message) { if (message instanceof MessageImpl) { return message; } MessageImpl messageImpl = new MessageImpl(); messageImpl.setNew(message.isNew()); messageImpl.setPrimaryKey(message.getPrimaryKey()); messageImpl.setMessageId(message.getMessageId()); messageImpl.setCompanyId(message.getCompanyId()); messageImpl.setUserId(message.getUserId()); messageImpl.setUserName(message.getUserName()); messageImpl.setCreateDate(message.getCreateDate()); messageImpl.setModifiedDate(message.getModifiedDate()); messageImpl.setAccountId(message.getAccountId()); messageImpl.setFolderId(message.getFolderId()); messageImpl.setSender(message.getSender()); messageImpl.setTo(message.getTo()); messageImpl.setCc(message.getCc()); messageImpl.setBcc(message.getBcc()); messageImpl.setSentDate(message.getSentDate()); messageImpl.setSubject(message.getSubject()); messageImpl.setPreview(message.getPreview()); messageImpl.setBody(message.getBody()); messageImpl.setFlags(message.getFlags()); messageImpl.setSize(message.getSize()); messageImpl.setRemoteMessageId(message.getRemoteMessageId()); messageImpl.setContentType(message.getContentType()); return messageImpl; } /** * Returns the message with the primary key or throws a {@link com.liferay.portal.kernel.exception.NoSuchModelException} if it could not be found. * * @param primaryKey the primary key of the message * @return the message * @throws NoSuchMessageException if a message with the primary key could not be found */ @Override public Message findByPrimaryKey(Serializable primaryKey) throws NoSuchMessageException { Message message = fetchByPrimaryKey(primaryKey); if (message == null) { if (_log.isDebugEnabled()) { _log.debug(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey); } throw new NoSuchMessageException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey); } return message; } /** * Returns the message with the primary key or throws a {@link NoSuchMessageException} if it could not be found. * * @param messageId the primary key of the message * @return the message * @throws NoSuchMessageException if a message with the primary key could not be found */ @Override public Message findByPrimaryKey(long messageId) throws NoSuchMessageException { return findByPrimaryKey((Serializable)messageId); } /** * Returns the message with the primary key or returns <code>null</code> if it could not be found. * * @param primaryKey the primary key of the message * @return the message, or <code>null</code> if a message with the primary key could not be found */ @Override public Message fetchByPrimaryKey(Serializable primaryKey) { Serializable serializable = entityCache.getResult(MessageModelImpl.ENTITY_CACHE_ENABLED, MessageImpl.class, primaryKey); if (serializable == nullModel) { return null; } Message message = (Message)serializable; if (message == null) { Session session = null; try { session = openSession(); message = (Message)session.get(MessageImpl.class, primaryKey); if (message != null) { cacheResult(message); } else { entityCache.putResult(MessageModelImpl.ENTITY_CACHE_ENABLED, MessageImpl.class, primaryKey, nullModel); } } catch (Exception e) { entityCache.removeResult(MessageModelImpl.ENTITY_CACHE_ENABLED, MessageImpl.class, primaryKey); throw processException(e); } finally { closeSession(session); } } return message; } /** * Returns the message with the primary key or returns <code>null</code> if it could not be found. * * @param messageId the primary key of the message * @return the message, or <code>null</code> if a message with the primary key could not be found */ @Override public Message fetchByPrimaryKey(long messageId) { return fetchByPrimaryKey((Serializable)messageId); } @Override public Map<Serializable, Message> fetchByPrimaryKeys( Set<Serializable> primaryKeys) { if (primaryKeys.isEmpty()) { return Collections.emptyMap(); } Map<Serializable, Message> map = new HashMap<Serializable, Message>(); if (primaryKeys.size() == 1) { Iterator<Serializable> iterator = primaryKeys.iterator(); Serializable primaryKey = iterator.next(); Message message = fetchByPrimaryKey(primaryKey); if (message != null) { map.put(primaryKey, message); } return map; } Set<Serializable> uncachedPrimaryKeys = null; for (Serializable primaryKey : primaryKeys) { Serializable serializable = entityCache.getResult(MessageModelImpl.ENTITY_CACHE_ENABLED, MessageImpl.class, primaryKey); if (serializable != nullModel) { if (serializable == null) { if (uncachedPrimaryKeys == null) { uncachedPrimaryKeys = new HashSet<Serializable>(); } uncachedPrimaryKeys.add(primaryKey); } else { map.put(primaryKey, (Message)serializable); } } } if (uncachedPrimaryKeys == null) { return map; } StringBundler query = new StringBundler((uncachedPrimaryKeys.size() * 2) + 1); query.append(_SQL_SELECT_MESSAGE_WHERE_PKS_IN); for (Serializable primaryKey : uncachedPrimaryKeys) { query.append((long)primaryKey); query.append(StringPool.COMMA); } query.setIndex(query.index() - 1); query.append(StringPool.CLOSE_PARENTHESIS); String sql = query.toString(); Session session = null; try { session = openSession(); Query q = session.createQuery(sql); for (Message message : (List<Message>)q.list()) { map.put(message.getPrimaryKeyObj(), message); cacheResult(message); uncachedPrimaryKeys.remove(message.getPrimaryKeyObj()); } for (Serializable primaryKey : uncachedPrimaryKeys) { entityCache.putResult(MessageModelImpl.ENTITY_CACHE_ENABLED, MessageImpl.class, primaryKey, nullModel); } } catch (Exception e) { throw processException(e); } finally { closeSession(session); } return map; } /** * Returns all the messages. * * @return the messages */ @Override public List<Message> findAll() { return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null); } /** * Returns a range of all the messages. * * <p> * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link MessageModelImpl}. 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 messages * @param end the upper bound of the range of messages (not inclusive) * @return the range of messages */ @Override public List<Message> findAll(int start, int end) { return findAll(start, end, null); } /** * Returns an ordered range of all the messages. * * <p> * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link MessageModelImpl}. 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 messages * @param end the upper bound of the range of messages (not inclusive) * @param orderByComparator the comparator to order the results by (optionally <code>null</code>) * @return the ordered range of messages */ @Override public List<Message> findAll(int start, int end, OrderByComparator<Message> orderByComparator) { return findAll(start, end, orderByComparator, true); } /** * Returns an ordered range of all the messages. * * <p> * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link MessageModelImpl}. 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 messages * @param end the upper bound of the range of messages (not inclusive) * @param orderByComparator the comparator to order the results by (optionally <code>null</code>) * @param retrieveFromCache whether to retrieve from the finder cache * @return the ordered range of messages */ @Override public List<Message> findAll(int start, int end, OrderByComparator<Message> orderByComparator, boolean retrieveFromCache) { boolean pagination = true; FinderPath finderPath = null; Object[] finderArgs = null; if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) && (orderByComparator == null)) { pagination = false; finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL; finderArgs = FINDER_ARGS_EMPTY; } else { finderPath = FINDER_PATH_WITH_PAGINATION_FIND_ALL; finderArgs = new Object[] { start, end, orderByComparator }; } List<Message> list = null; if (retrieveFromCache) { list = (List<Message>)finderCache.getResult(finderPath, finderArgs, this); } if (list == null) { StringBundler query = null; String sql = null; if (orderByComparator != null) { query = new StringBundler(2 + (orderByComparator.getOrderByFields().length * 2)); query.append(_SQL_SELECT_MESSAGE); appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS, orderByComparator); sql = query.toString(); } else { sql = _SQL_SELECT_MESSAGE; if (pagination) { sql = sql.concat(MessageModelImpl.ORDER_BY_JPQL); } } Session session = null; try { session = openSession(); Query q = session.createQuery(sql); if (!pagination) { list = (List<Message>)QueryUtil.list(q, getDialect(), start, end, false); Collections.sort(list); list = Collections.unmodifiableList(list); } else { list = (List<Message>)QueryUtil.list(q, getDialect(), start, end); } cacheResult(list); finderCache.putResult(finderPath, finderArgs, list); } catch (Exception e) { finderCache.removeResult(finderPath, finderArgs); throw processException(e); } finally { closeSession(session); } } return list; } /** * Removes all the messages from the database. * */ @Override public void removeAll() { for (Message message : findAll()) { remove(message); } } /** * Returns the number of messages. * * @return the number of messages */ @Override public int countAll() { Long count = (Long)finderCache.getResult(FINDER_PATH_COUNT_ALL, FINDER_ARGS_EMPTY, this); if (count == null) { Session session = null; try { session = openSession(); Query q = session.createQuery(_SQL_COUNT_MESSAGE); count = (Long)q.uniqueResult(); finderCache.putResult(FINDER_PATH_COUNT_ALL, FINDER_ARGS_EMPTY, count); } catch (Exception e) { finderCache.removeResult(FINDER_PATH_COUNT_ALL, FINDER_ARGS_EMPTY); throw processException(e); } finally { closeSession(session); } } return count.intValue(); } @Override public Set<String> getBadColumnNames() { return _badColumnNames; } @Override protected Map<String, Integer> getTableColumnsMap() { return MessageModelImpl.TABLE_COLUMNS_MAP; } /** * Initializes the message persistence. */ public void afterPropertiesSet() { } public void destroy() { entityCache.removeCache(MessageImpl.class.getName()); finderCache.removeCache(FINDER_CLASS_NAME_ENTITY); finderCache.removeCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION); finderCache.removeCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION); } @ServiceReference(type = CompanyProviderWrapper.class) protected CompanyProvider companyProvider; @ServiceReference(type = EntityCache.class) protected EntityCache entityCache; @ServiceReference(type = FinderCache.class) protected FinderCache finderCache; private static final String _SQL_SELECT_MESSAGE = "SELECT message FROM Message message"; private static final String _SQL_SELECT_MESSAGE_WHERE_PKS_IN = "SELECT message FROM Message message WHERE messageId IN ("; private static final String _SQL_SELECT_MESSAGE_WHERE = "SELECT message FROM Message message WHERE "; private static final String _SQL_COUNT_MESSAGE = "SELECT COUNT(message) FROM Message message"; private static final String _SQL_COUNT_MESSAGE_WHERE = "SELECT COUNT(message) FROM Message message WHERE "; private static final String _ORDER_BY_ENTITY_ALIAS = "message."; private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No Message exists with the primary key "; private static final String _NO_SUCH_ENTITY_WITH_KEY = "No Message exists with the key {"; private static final Log _log = LogFactoryUtil.getLog(MessagePersistenceImpl.class); private static final Set<String> _badColumnNames = SetUtil.fromArray(new String[] { "to", "size" }); }