001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.announcements.service.persistence.impl;
016    
017    import aQute.bnd.annotation.ProviderType;
018    
019    import com.liferay.portal.kernel.cache.CacheRegistryUtil;
020    import com.liferay.portal.kernel.dao.orm.EntityCacheUtil;
021    import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
022    import com.liferay.portal.kernel.dao.orm.FinderPath;
023    import com.liferay.portal.kernel.dao.orm.Query;
024    import com.liferay.portal.kernel.dao.orm.QueryPos;
025    import com.liferay.portal.kernel.dao.orm.QueryUtil;
026    import com.liferay.portal.kernel.dao.orm.Session;
027    import com.liferay.portal.kernel.log.Log;
028    import com.liferay.portal.kernel.log.LogFactoryUtil;
029    import com.liferay.portal.kernel.util.OrderByComparator;
030    import com.liferay.portal.kernel.util.SetUtil;
031    import com.liferay.portal.kernel.util.StringBundler;
032    import com.liferay.portal.kernel.util.StringPool;
033    import com.liferay.portal.kernel.util.Validator;
034    import com.liferay.portal.model.CacheModel;
035    import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
036    
037    import com.liferay.portlet.announcements.NoSuchDeliveryException;
038    import com.liferay.portlet.announcements.model.AnnouncementsDelivery;
039    import com.liferay.portlet.announcements.model.impl.AnnouncementsDeliveryImpl;
040    import com.liferay.portlet.announcements.model.impl.AnnouncementsDeliveryModelImpl;
041    import com.liferay.portlet.announcements.service.persistence.AnnouncementsDeliveryPersistence;
042    
043    import java.io.Serializable;
044    
045    import java.util.Collections;
046    import java.util.HashMap;
047    import java.util.HashSet;
048    import java.util.Iterator;
049    import java.util.List;
050    import java.util.Map;
051    import java.util.Set;
052    
053    /**
054     * The persistence implementation for the announcements delivery service.
055     *
056     * <p>
057     * Caching information and settings can be found in <code>portal.properties</code>
058     * </p>
059     *
060     * @author Brian Wing Shun Chan
061     * @see AnnouncementsDeliveryPersistence
062     * @see AnnouncementsDeliveryUtil
063     * @generated
064     */
065    @ProviderType
066    public class AnnouncementsDeliveryPersistenceImpl extends BasePersistenceImpl<AnnouncementsDelivery>
067            implements AnnouncementsDeliveryPersistence {
068            /*
069             * NOTE FOR DEVELOPERS:
070             *
071             * Never modify or reference this class directly. Always use {@link AnnouncementsDeliveryUtil} to access the announcements delivery persistence. Modify <code>service.xml</code> and rerun ServiceBuilder to regenerate this class.
072             */
073            public static final String FINDER_CLASS_NAME_ENTITY = AnnouncementsDeliveryImpl.class.getName();
074            public static final String FINDER_CLASS_NAME_LIST_WITH_PAGINATION = FINDER_CLASS_NAME_ENTITY +
075                    ".List1";
076            public static final String FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION = FINDER_CLASS_NAME_ENTITY +
077                    ".List2";
078            public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_ALL = new FinderPath(AnnouncementsDeliveryModelImpl.ENTITY_CACHE_ENABLED,
079                            AnnouncementsDeliveryModelImpl.FINDER_CACHE_ENABLED,
080                            AnnouncementsDeliveryImpl.class,
081                            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findAll", new String[0]);
082            public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL = new FinderPath(AnnouncementsDeliveryModelImpl.ENTITY_CACHE_ENABLED,
083                            AnnouncementsDeliveryModelImpl.FINDER_CACHE_ENABLED,
084                            AnnouncementsDeliveryImpl.class,
085                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findAll", new String[0]);
086            public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(AnnouncementsDeliveryModelImpl.ENTITY_CACHE_ENABLED,
087                            AnnouncementsDeliveryModelImpl.FINDER_CACHE_ENABLED, Long.class,
088                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countAll", new String[0]);
089            public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_USERID = new FinderPath(AnnouncementsDeliveryModelImpl.ENTITY_CACHE_ENABLED,
090                            AnnouncementsDeliveryModelImpl.FINDER_CACHE_ENABLED,
091                            AnnouncementsDeliveryImpl.class,
092                            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findByUserId",
093                            new String[] {
094                                    Long.class.getName(),
095                                    
096                            Integer.class.getName(), Integer.class.getName(),
097                                    OrderByComparator.class.getName()
098                            });
099            public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_USERID =
100                    new FinderPath(AnnouncementsDeliveryModelImpl.ENTITY_CACHE_ENABLED,
101                            AnnouncementsDeliveryModelImpl.FINDER_CACHE_ENABLED,
102                            AnnouncementsDeliveryImpl.class,
103                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findByUserId",
104                            new String[] { Long.class.getName() },
105                            AnnouncementsDeliveryModelImpl.USERID_COLUMN_BITMASK);
106            public static final FinderPath FINDER_PATH_COUNT_BY_USERID = new FinderPath(AnnouncementsDeliveryModelImpl.ENTITY_CACHE_ENABLED,
107                            AnnouncementsDeliveryModelImpl.FINDER_CACHE_ENABLED, Long.class,
108                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByUserId",
109                            new String[] { Long.class.getName() });
110    
111            /**
112             * Returns all the announcements deliveries where userId = &#63;.
113             *
114             * @param userId the user ID
115             * @return the matching announcements deliveries
116             */
117            @Override
118            public List<AnnouncementsDelivery> findByUserId(long userId) {
119                    return findByUserId(userId, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
120            }
121    
122            /**
123             * Returns a range of all the announcements deliveries where userId = &#63;.
124             *
125             * <p>
126             * 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 com.liferay.portlet.announcements.model.impl.AnnouncementsDeliveryModelImpl}. 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.
127             * </p>
128             *
129             * @param userId the user ID
130             * @param start the lower bound of the range of announcements deliveries
131             * @param end the upper bound of the range of announcements deliveries (not inclusive)
132             * @return the range of matching announcements deliveries
133             */
134            @Override
135            public List<AnnouncementsDelivery> findByUserId(long userId, int start,
136                    int end) {
137                    return findByUserId(userId, start, end, null);
138            }
139    
140            /**
141             * Returns an ordered range of all the announcements deliveries where userId = &#63;.
142             *
143             * <p>
144             * 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 com.liferay.portlet.announcements.model.impl.AnnouncementsDeliveryModelImpl}. 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.
145             * </p>
146             *
147             * @param userId the user ID
148             * @param start the lower bound of the range of announcements deliveries
149             * @param end the upper bound of the range of announcements deliveries (not inclusive)
150             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
151             * @return the ordered range of matching announcements deliveries
152             */
153            @Override
154            public List<AnnouncementsDelivery> findByUserId(long userId, int start,
155                    int end, OrderByComparator<AnnouncementsDelivery> orderByComparator) {
156                    boolean pagination = true;
157                    FinderPath finderPath = null;
158                    Object[] finderArgs = null;
159    
160                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
161                                    (orderByComparator == null)) {
162                            pagination = false;
163                            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_USERID;
164                            finderArgs = new Object[] { userId };
165                    }
166                    else {
167                            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_USERID;
168                            finderArgs = new Object[] { userId, start, end, orderByComparator };
169                    }
170    
171                    List<AnnouncementsDelivery> list = (List<AnnouncementsDelivery>)FinderCacheUtil.getResult(finderPath,
172                                    finderArgs, this);
173    
174                    if ((list != null) && !list.isEmpty()) {
175                            for (AnnouncementsDelivery announcementsDelivery : list) {
176                                    if ((userId != announcementsDelivery.getUserId())) {
177                                            list = null;
178    
179                                            break;
180                                    }
181                            }
182                    }
183    
184                    if (list == null) {
185                            StringBundler query = null;
186    
187                            if (orderByComparator != null) {
188                                    query = new StringBundler(3 +
189                                                    (orderByComparator.getOrderByFields().length * 3));
190                            }
191                            else {
192                                    query = new StringBundler(3);
193                            }
194    
195                            query.append(_SQL_SELECT_ANNOUNCEMENTSDELIVERY_WHERE);
196    
197                            query.append(_FINDER_COLUMN_USERID_USERID_2);
198    
199                            if (orderByComparator != null) {
200                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
201                                            orderByComparator);
202                            }
203                            else
204                             if (pagination) {
205                                    query.append(AnnouncementsDeliveryModelImpl.ORDER_BY_JPQL);
206                            }
207    
208                            String sql = query.toString();
209    
210                            Session session = null;
211    
212                            try {
213                                    session = openSession();
214    
215                                    Query q = session.createQuery(sql);
216    
217                                    QueryPos qPos = QueryPos.getInstance(q);
218    
219                                    qPos.add(userId);
220    
221                                    if (!pagination) {
222                                            list = (List<AnnouncementsDelivery>)QueryUtil.list(q,
223                                                            getDialect(), start, end, false);
224    
225                                            Collections.sort(list);
226    
227                                            list = Collections.unmodifiableList(list);
228                                    }
229                                    else {
230                                            list = (List<AnnouncementsDelivery>)QueryUtil.list(q,
231                                                            getDialect(), start, end);
232                                    }
233    
234                                    cacheResult(list);
235    
236                                    FinderCacheUtil.putResult(finderPath, finderArgs, list);
237                            }
238                            catch (Exception e) {
239                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
240    
241                                    throw processException(e);
242                            }
243                            finally {
244                                    closeSession(session);
245                            }
246                    }
247    
248                    return list;
249            }
250    
251            /**
252             * Returns the first announcements delivery in the ordered set where userId = &#63;.
253             *
254             * @param userId the user ID
255             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
256             * @return the first matching announcements delivery
257             * @throws com.liferay.portlet.announcements.NoSuchDeliveryException if a matching announcements delivery could not be found
258             */
259            @Override
260            public AnnouncementsDelivery findByUserId_First(long userId,
261                    OrderByComparator<AnnouncementsDelivery> orderByComparator)
262                    throws NoSuchDeliveryException {
263                    AnnouncementsDelivery announcementsDelivery = fetchByUserId_First(userId,
264                                    orderByComparator);
265    
266                    if (announcementsDelivery != null) {
267                            return announcementsDelivery;
268                    }
269    
270                    StringBundler msg = new StringBundler(4);
271    
272                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
273    
274                    msg.append("userId=");
275                    msg.append(userId);
276    
277                    msg.append(StringPool.CLOSE_CURLY_BRACE);
278    
279                    throw new NoSuchDeliveryException(msg.toString());
280            }
281    
282            /**
283             * Returns the first announcements delivery in the ordered set where userId = &#63;.
284             *
285             * @param userId the user ID
286             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
287             * @return the first matching announcements delivery, or <code>null</code> if a matching announcements delivery could not be found
288             */
289            @Override
290            public AnnouncementsDelivery fetchByUserId_First(long userId,
291                    OrderByComparator<AnnouncementsDelivery> orderByComparator) {
292                    List<AnnouncementsDelivery> list = findByUserId(userId, 0, 1,
293                                    orderByComparator);
294    
295                    if (!list.isEmpty()) {
296                            return list.get(0);
297                    }
298    
299                    return null;
300            }
301    
302            /**
303             * Returns the last announcements delivery in the ordered set where userId = &#63;.
304             *
305             * @param userId the user ID
306             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
307             * @return the last matching announcements delivery
308             * @throws com.liferay.portlet.announcements.NoSuchDeliveryException if a matching announcements delivery could not be found
309             */
310            @Override
311            public AnnouncementsDelivery findByUserId_Last(long userId,
312                    OrderByComparator<AnnouncementsDelivery> orderByComparator)
313                    throws NoSuchDeliveryException {
314                    AnnouncementsDelivery announcementsDelivery = fetchByUserId_Last(userId,
315                                    orderByComparator);
316    
317                    if (announcementsDelivery != null) {
318                            return announcementsDelivery;
319                    }
320    
321                    StringBundler msg = new StringBundler(4);
322    
323                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
324    
325                    msg.append("userId=");
326                    msg.append(userId);
327    
328                    msg.append(StringPool.CLOSE_CURLY_BRACE);
329    
330                    throw new NoSuchDeliveryException(msg.toString());
331            }
332    
333            /**
334             * Returns the last announcements delivery in the ordered set where userId = &#63;.
335             *
336             * @param userId the user ID
337             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
338             * @return the last matching announcements delivery, or <code>null</code> if a matching announcements delivery could not be found
339             */
340            @Override
341            public AnnouncementsDelivery fetchByUserId_Last(long userId,
342                    OrderByComparator<AnnouncementsDelivery> orderByComparator) {
343                    int count = countByUserId(userId);
344    
345                    if (count == 0) {
346                            return null;
347                    }
348    
349                    List<AnnouncementsDelivery> list = findByUserId(userId, count - 1,
350                                    count, orderByComparator);
351    
352                    if (!list.isEmpty()) {
353                            return list.get(0);
354                    }
355    
356                    return null;
357            }
358    
359            /**
360             * Returns the announcements deliveries before and after the current announcements delivery in the ordered set where userId = &#63;.
361             *
362             * @param deliveryId the primary key of the current announcements delivery
363             * @param userId the user ID
364             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
365             * @return the previous, current, and next announcements delivery
366             * @throws com.liferay.portlet.announcements.NoSuchDeliveryException if a announcements delivery with the primary key could not be found
367             */
368            @Override
369            public AnnouncementsDelivery[] findByUserId_PrevAndNext(long deliveryId,
370                    long userId, OrderByComparator<AnnouncementsDelivery> orderByComparator)
371                    throws NoSuchDeliveryException {
372                    AnnouncementsDelivery announcementsDelivery = findByPrimaryKey(deliveryId);
373    
374                    Session session = null;
375    
376                    try {
377                            session = openSession();
378    
379                            AnnouncementsDelivery[] array = new AnnouncementsDeliveryImpl[3];
380    
381                            array[0] = getByUserId_PrevAndNext(session, announcementsDelivery,
382                                            userId, orderByComparator, true);
383    
384                            array[1] = announcementsDelivery;
385    
386                            array[2] = getByUserId_PrevAndNext(session, announcementsDelivery,
387                                            userId, orderByComparator, false);
388    
389                            return array;
390                    }
391                    catch (Exception e) {
392                            throw processException(e);
393                    }
394                    finally {
395                            closeSession(session);
396                    }
397            }
398    
399            protected AnnouncementsDelivery getByUserId_PrevAndNext(Session session,
400                    AnnouncementsDelivery announcementsDelivery, long userId,
401                    OrderByComparator<AnnouncementsDelivery> orderByComparator,
402                    boolean previous) {
403                    StringBundler query = null;
404    
405                    if (orderByComparator != null) {
406                            query = new StringBundler(6 +
407                                            (orderByComparator.getOrderByFields().length * 6));
408                    }
409                    else {
410                            query = new StringBundler(3);
411                    }
412    
413                    query.append(_SQL_SELECT_ANNOUNCEMENTSDELIVERY_WHERE);
414    
415                    query.append(_FINDER_COLUMN_USERID_USERID_2);
416    
417                    if (orderByComparator != null) {
418                            String[] orderByConditionFields = orderByComparator.getOrderByConditionFields();
419    
420                            if (orderByConditionFields.length > 0) {
421                                    query.append(WHERE_AND);
422                            }
423    
424                            for (int i = 0; i < orderByConditionFields.length; i++) {
425                                    query.append(_ORDER_BY_ENTITY_ALIAS);
426                                    query.append(orderByConditionFields[i]);
427    
428                                    if ((i + 1) < orderByConditionFields.length) {
429                                            if (orderByComparator.isAscending() ^ previous) {
430                                                    query.append(WHERE_GREATER_THAN_HAS_NEXT);
431                                            }
432                                            else {
433                                                    query.append(WHERE_LESSER_THAN_HAS_NEXT);
434                                            }
435                                    }
436                                    else {
437                                            if (orderByComparator.isAscending() ^ previous) {
438                                                    query.append(WHERE_GREATER_THAN);
439                                            }
440                                            else {
441                                                    query.append(WHERE_LESSER_THAN);
442                                            }
443                                    }
444                            }
445    
446                            query.append(ORDER_BY_CLAUSE);
447    
448                            String[] orderByFields = orderByComparator.getOrderByFields();
449    
450                            for (int i = 0; i < orderByFields.length; i++) {
451                                    query.append(_ORDER_BY_ENTITY_ALIAS);
452                                    query.append(orderByFields[i]);
453    
454                                    if ((i + 1) < orderByFields.length) {
455                                            if (orderByComparator.isAscending() ^ previous) {
456                                                    query.append(ORDER_BY_ASC_HAS_NEXT);
457                                            }
458                                            else {
459                                                    query.append(ORDER_BY_DESC_HAS_NEXT);
460                                            }
461                                    }
462                                    else {
463                                            if (orderByComparator.isAscending() ^ previous) {
464                                                    query.append(ORDER_BY_ASC);
465                                            }
466                                            else {
467                                                    query.append(ORDER_BY_DESC);
468                                            }
469                                    }
470                            }
471                    }
472                    else {
473                            query.append(AnnouncementsDeliveryModelImpl.ORDER_BY_JPQL);
474                    }
475    
476                    String sql = query.toString();
477    
478                    Query q = session.createQuery(sql);
479    
480                    q.setFirstResult(0);
481                    q.setMaxResults(2);
482    
483                    QueryPos qPos = QueryPos.getInstance(q);
484    
485                    qPos.add(userId);
486    
487                    if (orderByComparator != null) {
488                            Object[] values = orderByComparator.getOrderByConditionValues(announcementsDelivery);
489    
490                            for (Object value : values) {
491                                    qPos.add(value);
492                            }
493                    }
494    
495                    List<AnnouncementsDelivery> list = q.list();
496    
497                    if (list.size() == 2) {
498                            return list.get(1);
499                    }
500                    else {
501                            return null;
502                    }
503            }
504    
505            /**
506             * Removes all the announcements deliveries where userId = &#63; from the database.
507             *
508             * @param userId the user ID
509             */
510            @Override
511            public void removeByUserId(long userId) {
512                    for (AnnouncementsDelivery announcementsDelivery : findByUserId(
513                                    userId, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null)) {
514                            remove(announcementsDelivery);
515                    }
516            }
517    
518            /**
519             * Returns the number of announcements deliveries where userId = &#63;.
520             *
521             * @param userId the user ID
522             * @return the number of matching announcements deliveries
523             */
524            @Override
525            public int countByUserId(long userId) {
526                    FinderPath finderPath = FINDER_PATH_COUNT_BY_USERID;
527    
528                    Object[] finderArgs = new Object[] { userId };
529    
530                    Long count = (Long)FinderCacheUtil.getResult(finderPath, finderArgs,
531                                    this);
532    
533                    if (count == null) {
534                            StringBundler query = new StringBundler(2);
535    
536                            query.append(_SQL_COUNT_ANNOUNCEMENTSDELIVERY_WHERE);
537    
538                            query.append(_FINDER_COLUMN_USERID_USERID_2);
539    
540                            String sql = query.toString();
541    
542                            Session session = null;
543    
544                            try {
545                                    session = openSession();
546    
547                                    Query q = session.createQuery(sql);
548    
549                                    QueryPos qPos = QueryPos.getInstance(q);
550    
551                                    qPos.add(userId);
552    
553                                    count = (Long)q.uniqueResult();
554    
555                                    FinderCacheUtil.putResult(finderPath, finderArgs, count);
556                            }
557                            catch (Exception e) {
558                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
559    
560                                    throw processException(e);
561                            }
562                            finally {
563                                    closeSession(session);
564                            }
565                    }
566    
567                    return count.intValue();
568            }
569    
570            private static final String _FINDER_COLUMN_USERID_USERID_2 = "announcementsDelivery.userId = ?";
571            public static final FinderPath FINDER_PATH_FETCH_BY_U_T = new FinderPath(AnnouncementsDeliveryModelImpl.ENTITY_CACHE_ENABLED,
572                            AnnouncementsDeliveryModelImpl.FINDER_CACHE_ENABLED,
573                            AnnouncementsDeliveryImpl.class, FINDER_CLASS_NAME_ENTITY,
574                            "fetchByU_T",
575                            new String[] { Long.class.getName(), String.class.getName() },
576                            AnnouncementsDeliveryModelImpl.USERID_COLUMN_BITMASK |
577                            AnnouncementsDeliveryModelImpl.TYPE_COLUMN_BITMASK);
578            public static final FinderPath FINDER_PATH_COUNT_BY_U_T = new FinderPath(AnnouncementsDeliveryModelImpl.ENTITY_CACHE_ENABLED,
579                            AnnouncementsDeliveryModelImpl.FINDER_CACHE_ENABLED, Long.class,
580                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByU_T",
581                            new String[] { Long.class.getName(), String.class.getName() });
582    
583            /**
584             * Returns the announcements delivery where userId = &#63; and type = &#63; or throws a {@link com.liferay.portlet.announcements.NoSuchDeliveryException} if it could not be found.
585             *
586             * @param userId the user ID
587             * @param type the type
588             * @return the matching announcements delivery
589             * @throws com.liferay.portlet.announcements.NoSuchDeliveryException if a matching announcements delivery could not be found
590             */
591            @Override
592            public AnnouncementsDelivery findByU_T(long userId, String type)
593                    throws NoSuchDeliveryException {
594                    AnnouncementsDelivery announcementsDelivery = fetchByU_T(userId, type);
595    
596                    if (announcementsDelivery == null) {
597                            StringBundler msg = new StringBundler(6);
598    
599                            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
600    
601                            msg.append("userId=");
602                            msg.append(userId);
603    
604                            msg.append(", type=");
605                            msg.append(type);
606    
607                            msg.append(StringPool.CLOSE_CURLY_BRACE);
608    
609                            if (_log.isWarnEnabled()) {
610                                    _log.warn(msg.toString());
611                            }
612    
613                            throw new NoSuchDeliveryException(msg.toString());
614                    }
615    
616                    return announcementsDelivery;
617            }
618    
619            /**
620             * Returns the announcements delivery where userId = &#63; and type = &#63; or returns <code>null</code> if it could not be found. Uses the finder cache.
621             *
622             * @param userId the user ID
623             * @param type the type
624             * @return the matching announcements delivery, or <code>null</code> if a matching announcements delivery could not be found
625             */
626            @Override
627            public AnnouncementsDelivery fetchByU_T(long userId, String type) {
628                    return fetchByU_T(userId, type, true);
629            }
630    
631            /**
632             * Returns the announcements delivery where userId = &#63; and type = &#63; or returns <code>null</code> if it could not be found, optionally using the finder cache.
633             *
634             * @param userId the user ID
635             * @param type the type
636             * @param retrieveFromCache whether to use the finder cache
637             * @return the matching announcements delivery, or <code>null</code> if a matching announcements delivery could not be found
638             */
639            @Override
640            public AnnouncementsDelivery fetchByU_T(long userId, String type,
641                    boolean retrieveFromCache) {
642                    Object[] finderArgs = new Object[] { userId, type };
643    
644                    Object result = null;
645    
646                    if (retrieveFromCache) {
647                            result = FinderCacheUtil.getResult(FINDER_PATH_FETCH_BY_U_T,
648                                            finderArgs, this);
649                    }
650    
651                    if (result instanceof AnnouncementsDelivery) {
652                            AnnouncementsDelivery announcementsDelivery = (AnnouncementsDelivery)result;
653    
654                            if ((userId != announcementsDelivery.getUserId()) ||
655                                            !Validator.equals(type, announcementsDelivery.getType())) {
656                                    result = null;
657                            }
658                    }
659    
660                    if (result == null) {
661                            StringBundler query = new StringBundler(4);
662    
663                            query.append(_SQL_SELECT_ANNOUNCEMENTSDELIVERY_WHERE);
664    
665                            query.append(_FINDER_COLUMN_U_T_USERID_2);
666    
667                            boolean bindType = false;
668    
669                            if (type == null) {
670                                    query.append(_FINDER_COLUMN_U_T_TYPE_1);
671                            }
672                            else if (type.equals(StringPool.BLANK)) {
673                                    query.append(_FINDER_COLUMN_U_T_TYPE_3);
674                            }
675                            else {
676                                    bindType = true;
677    
678                                    query.append(_FINDER_COLUMN_U_T_TYPE_2);
679                            }
680    
681                            String sql = query.toString();
682    
683                            Session session = null;
684    
685                            try {
686                                    session = openSession();
687    
688                                    Query q = session.createQuery(sql);
689    
690                                    QueryPos qPos = QueryPos.getInstance(q);
691    
692                                    qPos.add(userId);
693    
694                                    if (bindType) {
695                                            qPos.add(type);
696                                    }
697    
698                                    List<AnnouncementsDelivery> list = q.list();
699    
700                                    if (list.isEmpty()) {
701                                            FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_U_T,
702                                                    finderArgs, list);
703                                    }
704                                    else {
705                                            AnnouncementsDelivery announcementsDelivery = list.get(0);
706    
707                                            result = announcementsDelivery;
708    
709                                            cacheResult(announcementsDelivery);
710    
711                                            if ((announcementsDelivery.getUserId() != userId) ||
712                                                            (announcementsDelivery.getType() == null) ||
713                                                            !announcementsDelivery.getType().equals(type)) {
714                                                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_U_T,
715                                                            finderArgs, announcementsDelivery);
716                                            }
717                                    }
718                            }
719                            catch (Exception e) {
720                                    FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_U_T,
721                                            finderArgs);
722    
723                                    throw processException(e);
724                            }
725                            finally {
726                                    closeSession(session);
727                            }
728                    }
729    
730                    if (result instanceof List<?>) {
731                            return null;
732                    }
733                    else {
734                            return (AnnouncementsDelivery)result;
735                    }
736            }
737    
738            /**
739             * Removes the announcements delivery where userId = &#63; and type = &#63; from the database.
740             *
741             * @param userId the user ID
742             * @param type the type
743             * @return the announcements delivery that was removed
744             */
745            @Override
746            public AnnouncementsDelivery removeByU_T(long userId, String type)
747                    throws NoSuchDeliveryException {
748                    AnnouncementsDelivery announcementsDelivery = findByU_T(userId, type);
749    
750                    return remove(announcementsDelivery);
751            }
752    
753            /**
754             * Returns the number of announcements deliveries where userId = &#63; and type = &#63;.
755             *
756             * @param userId the user ID
757             * @param type the type
758             * @return the number of matching announcements deliveries
759             */
760            @Override
761            public int countByU_T(long userId, String type) {
762                    FinderPath finderPath = FINDER_PATH_COUNT_BY_U_T;
763    
764                    Object[] finderArgs = new Object[] { userId, type };
765    
766                    Long count = (Long)FinderCacheUtil.getResult(finderPath, finderArgs,
767                                    this);
768    
769                    if (count == null) {
770                            StringBundler query = new StringBundler(3);
771    
772                            query.append(_SQL_COUNT_ANNOUNCEMENTSDELIVERY_WHERE);
773    
774                            query.append(_FINDER_COLUMN_U_T_USERID_2);
775    
776                            boolean bindType = false;
777    
778                            if (type == null) {
779                                    query.append(_FINDER_COLUMN_U_T_TYPE_1);
780                            }
781                            else if (type.equals(StringPool.BLANK)) {
782                                    query.append(_FINDER_COLUMN_U_T_TYPE_3);
783                            }
784                            else {
785                                    bindType = true;
786    
787                                    query.append(_FINDER_COLUMN_U_T_TYPE_2);
788                            }
789    
790                            String sql = query.toString();
791    
792                            Session session = null;
793    
794                            try {
795                                    session = openSession();
796    
797                                    Query q = session.createQuery(sql);
798    
799                                    QueryPos qPos = QueryPos.getInstance(q);
800    
801                                    qPos.add(userId);
802    
803                                    if (bindType) {
804                                            qPos.add(type);
805                                    }
806    
807                                    count = (Long)q.uniqueResult();
808    
809                                    FinderCacheUtil.putResult(finderPath, finderArgs, count);
810                            }
811                            catch (Exception e) {
812                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
813    
814                                    throw processException(e);
815                            }
816                            finally {
817                                    closeSession(session);
818                            }
819                    }
820    
821                    return count.intValue();
822            }
823    
824            private static final String _FINDER_COLUMN_U_T_USERID_2 = "announcementsDelivery.userId = ? AND ";
825            private static final String _FINDER_COLUMN_U_T_TYPE_1 = "announcementsDelivery.type IS NULL";
826            private static final String _FINDER_COLUMN_U_T_TYPE_2 = "announcementsDelivery.type = ?";
827            private static final String _FINDER_COLUMN_U_T_TYPE_3 = "(announcementsDelivery.type IS NULL OR announcementsDelivery.type = '')";
828    
829            public AnnouncementsDeliveryPersistenceImpl() {
830                    setModelClass(AnnouncementsDelivery.class);
831            }
832    
833            /**
834             * Caches the announcements delivery in the entity cache if it is enabled.
835             *
836             * @param announcementsDelivery the announcements delivery
837             */
838            @Override
839            public void cacheResult(AnnouncementsDelivery announcementsDelivery) {
840                    EntityCacheUtil.putResult(AnnouncementsDeliveryModelImpl.ENTITY_CACHE_ENABLED,
841                            AnnouncementsDeliveryImpl.class,
842                            announcementsDelivery.getPrimaryKey(), announcementsDelivery);
843    
844                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_U_T,
845                            new Object[] {
846                                    announcementsDelivery.getUserId(),
847                                    announcementsDelivery.getType()
848                            }, announcementsDelivery);
849    
850                    announcementsDelivery.resetOriginalValues();
851            }
852    
853            /**
854             * Caches the announcements deliveries in the entity cache if it is enabled.
855             *
856             * @param announcementsDeliveries the announcements deliveries
857             */
858            @Override
859            public void cacheResult(List<AnnouncementsDelivery> announcementsDeliveries) {
860                    for (AnnouncementsDelivery announcementsDelivery : announcementsDeliveries) {
861                            if (EntityCacheUtil.getResult(
862                                                    AnnouncementsDeliveryModelImpl.ENTITY_CACHE_ENABLED,
863                                                    AnnouncementsDeliveryImpl.class,
864                                                    announcementsDelivery.getPrimaryKey()) == null) {
865                                    cacheResult(announcementsDelivery);
866                            }
867                            else {
868                                    announcementsDelivery.resetOriginalValues();
869                            }
870                    }
871            }
872    
873            /**
874             * Clears the cache for all announcements deliveries.
875             *
876             * <p>
877             * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
878             * </p>
879             */
880            @Override
881            public void clearCache() {
882                    if (_HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE) {
883                            CacheRegistryUtil.clear(AnnouncementsDeliveryImpl.class.getName());
884                    }
885    
886                    EntityCacheUtil.clearCache(AnnouncementsDeliveryImpl.class);
887    
888                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
889                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
890                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
891            }
892    
893            /**
894             * Clears the cache for the announcements delivery.
895             *
896             * <p>
897             * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
898             * </p>
899             */
900            @Override
901            public void clearCache(AnnouncementsDelivery announcementsDelivery) {
902                    EntityCacheUtil.removeResult(AnnouncementsDeliveryModelImpl.ENTITY_CACHE_ENABLED,
903                            AnnouncementsDeliveryImpl.class,
904                            announcementsDelivery.getPrimaryKey());
905    
906                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
907                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
908    
909                    clearUniqueFindersCache(announcementsDelivery);
910            }
911    
912            @Override
913            public void clearCache(List<AnnouncementsDelivery> announcementsDeliveries) {
914                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
915                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
916    
917                    for (AnnouncementsDelivery announcementsDelivery : announcementsDeliveries) {
918                            EntityCacheUtil.removeResult(AnnouncementsDeliveryModelImpl.ENTITY_CACHE_ENABLED,
919                                    AnnouncementsDeliveryImpl.class,
920                                    announcementsDelivery.getPrimaryKey());
921    
922                            clearUniqueFindersCache(announcementsDelivery);
923                    }
924            }
925    
926            protected void cacheUniqueFindersCache(
927                    AnnouncementsDelivery announcementsDelivery) {
928                    if (announcementsDelivery.isNew()) {
929                            Object[] args = new Object[] {
930                                            announcementsDelivery.getUserId(),
931                                            announcementsDelivery.getType()
932                                    };
933    
934                            FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_U_T, args,
935                                    Long.valueOf(1));
936                            FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_U_T, args,
937                                    announcementsDelivery);
938                    }
939                    else {
940                            AnnouncementsDeliveryModelImpl announcementsDeliveryModelImpl = (AnnouncementsDeliveryModelImpl)announcementsDelivery;
941    
942                            if ((announcementsDeliveryModelImpl.getColumnBitmask() &
943                                            FINDER_PATH_FETCH_BY_U_T.getColumnBitmask()) != 0) {
944                                    Object[] args = new Object[] {
945                                                    announcementsDelivery.getUserId(),
946                                                    announcementsDelivery.getType()
947                                            };
948    
949                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_U_T, args,
950                                            Long.valueOf(1));
951                                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_U_T, args,
952                                            announcementsDelivery);
953                            }
954                    }
955            }
956    
957            protected void clearUniqueFindersCache(
958                    AnnouncementsDelivery announcementsDelivery) {
959                    AnnouncementsDeliveryModelImpl announcementsDeliveryModelImpl = (AnnouncementsDeliveryModelImpl)announcementsDelivery;
960    
961                    Object[] args = new Object[] {
962                                    announcementsDelivery.getUserId(),
963                                    announcementsDelivery.getType()
964                            };
965    
966                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_U_T, args);
967                    FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_U_T, args);
968    
969                    if ((announcementsDeliveryModelImpl.getColumnBitmask() &
970                                    FINDER_PATH_FETCH_BY_U_T.getColumnBitmask()) != 0) {
971                            args = new Object[] {
972                                            announcementsDeliveryModelImpl.getOriginalUserId(),
973                                            announcementsDeliveryModelImpl.getOriginalType()
974                                    };
975    
976                            FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_U_T, args);
977                            FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_U_T, args);
978                    }
979            }
980    
981            /**
982             * Creates a new announcements delivery with the primary key. Does not add the announcements delivery to the database.
983             *
984             * @param deliveryId the primary key for the new announcements delivery
985             * @return the new announcements delivery
986             */
987            @Override
988            public AnnouncementsDelivery create(long deliveryId) {
989                    AnnouncementsDelivery announcementsDelivery = new AnnouncementsDeliveryImpl();
990    
991                    announcementsDelivery.setNew(true);
992                    announcementsDelivery.setPrimaryKey(deliveryId);
993    
994                    return announcementsDelivery;
995            }
996    
997            /**
998             * Removes the announcements delivery with the primary key from the database. Also notifies the appropriate model listeners.
999             *
1000             * @param deliveryId the primary key of the announcements delivery
1001             * @return the announcements delivery that was removed
1002             * @throws com.liferay.portlet.announcements.NoSuchDeliveryException if a announcements delivery with the primary key could not be found
1003             */
1004            @Override
1005            public AnnouncementsDelivery remove(long deliveryId)
1006                    throws NoSuchDeliveryException {
1007                    return remove((Serializable)deliveryId);
1008            }
1009    
1010            /**
1011             * Removes the announcements delivery with the primary key from the database. Also notifies the appropriate model listeners.
1012             *
1013             * @param primaryKey the primary key of the announcements delivery
1014             * @return the announcements delivery that was removed
1015             * @throws com.liferay.portlet.announcements.NoSuchDeliveryException if a announcements delivery with the primary key could not be found
1016             */
1017            @Override
1018            public AnnouncementsDelivery remove(Serializable primaryKey)
1019                    throws NoSuchDeliveryException {
1020                    Session session = null;
1021    
1022                    try {
1023                            session = openSession();
1024    
1025                            AnnouncementsDelivery announcementsDelivery = (AnnouncementsDelivery)session.get(AnnouncementsDeliveryImpl.class,
1026                                            primaryKey);
1027    
1028                            if (announcementsDelivery == null) {
1029                                    if (_log.isWarnEnabled()) {
1030                                            _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
1031                                    }
1032    
1033                                    throw new NoSuchDeliveryException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
1034                                            primaryKey);
1035                            }
1036    
1037                            return remove(announcementsDelivery);
1038                    }
1039                    catch (NoSuchDeliveryException nsee) {
1040                            throw nsee;
1041                    }
1042                    catch (Exception e) {
1043                            throw processException(e);
1044                    }
1045                    finally {
1046                            closeSession(session);
1047                    }
1048            }
1049    
1050            @Override
1051            protected AnnouncementsDelivery removeImpl(
1052                    AnnouncementsDelivery announcementsDelivery) {
1053                    announcementsDelivery = toUnwrappedModel(announcementsDelivery);
1054    
1055                    Session session = null;
1056    
1057                    try {
1058                            session = openSession();
1059    
1060                            if (!session.contains(announcementsDelivery)) {
1061                                    announcementsDelivery = (AnnouncementsDelivery)session.get(AnnouncementsDeliveryImpl.class,
1062                                                    announcementsDelivery.getPrimaryKeyObj());
1063                            }
1064    
1065                            if (announcementsDelivery != null) {
1066                                    session.delete(announcementsDelivery);
1067                            }
1068                    }
1069                    catch (Exception e) {
1070                            throw processException(e);
1071                    }
1072                    finally {
1073                            closeSession(session);
1074                    }
1075    
1076                    if (announcementsDelivery != null) {
1077                            clearCache(announcementsDelivery);
1078                    }
1079    
1080                    return announcementsDelivery;
1081            }
1082    
1083            @Override
1084            public AnnouncementsDelivery updateImpl(
1085                    com.liferay.portlet.announcements.model.AnnouncementsDelivery announcementsDelivery) {
1086                    announcementsDelivery = toUnwrappedModel(announcementsDelivery);
1087    
1088                    boolean isNew = announcementsDelivery.isNew();
1089    
1090                    AnnouncementsDeliveryModelImpl announcementsDeliveryModelImpl = (AnnouncementsDeliveryModelImpl)announcementsDelivery;
1091    
1092                    Session session = null;
1093    
1094                    try {
1095                            session = openSession();
1096    
1097                            if (announcementsDelivery.isNew()) {
1098                                    session.save(announcementsDelivery);
1099    
1100                                    announcementsDelivery.setNew(false);
1101                            }
1102                            else {
1103                                    session.merge(announcementsDelivery);
1104                            }
1105                    }
1106                    catch (Exception e) {
1107                            throw processException(e);
1108                    }
1109                    finally {
1110                            closeSession(session);
1111                    }
1112    
1113                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
1114    
1115                    if (isNew || !AnnouncementsDeliveryModelImpl.COLUMN_BITMASK_ENABLED) {
1116                            FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
1117                    }
1118    
1119                    else {
1120                            if ((announcementsDeliveryModelImpl.getColumnBitmask() &
1121                                            FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_USERID.getColumnBitmask()) != 0) {
1122                                    Object[] args = new Object[] {
1123                                                    announcementsDeliveryModelImpl.getOriginalUserId()
1124                                            };
1125    
1126                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_USERID, args);
1127                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_USERID,
1128                                            args);
1129    
1130                                    args = new Object[] { announcementsDeliveryModelImpl.getUserId() };
1131    
1132                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_USERID, args);
1133                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_USERID,
1134                                            args);
1135                            }
1136                    }
1137    
1138                    EntityCacheUtil.putResult(AnnouncementsDeliveryModelImpl.ENTITY_CACHE_ENABLED,
1139                            AnnouncementsDeliveryImpl.class,
1140                            announcementsDelivery.getPrimaryKey(), announcementsDelivery, false);
1141    
1142                    clearUniqueFindersCache(announcementsDelivery);
1143                    cacheUniqueFindersCache(announcementsDelivery);
1144    
1145                    announcementsDelivery.resetOriginalValues();
1146    
1147                    return announcementsDelivery;
1148            }
1149    
1150            protected AnnouncementsDelivery toUnwrappedModel(
1151                    AnnouncementsDelivery announcementsDelivery) {
1152                    if (announcementsDelivery instanceof AnnouncementsDeliveryImpl) {
1153                            return announcementsDelivery;
1154                    }
1155    
1156                    AnnouncementsDeliveryImpl announcementsDeliveryImpl = new AnnouncementsDeliveryImpl();
1157    
1158                    announcementsDeliveryImpl.setNew(announcementsDelivery.isNew());
1159                    announcementsDeliveryImpl.setPrimaryKey(announcementsDelivery.getPrimaryKey());
1160    
1161                    announcementsDeliveryImpl.setDeliveryId(announcementsDelivery.getDeliveryId());
1162                    announcementsDeliveryImpl.setCompanyId(announcementsDelivery.getCompanyId());
1163                    announcementsDeliveryImpl.setUserId(announcementsDelivery.getUserId());
1164                    announcementsDeliveryImpl.setType(announcementsDelivery.getType());
1165                    announcementsDeliveryImpl.setEmail(announcementsDelivery.isEmail());
1166                    announcementsDeliveryImpl.setSms(announcementsDelivery.isSms());
1167                    announcementsDeliveryImpl.setWebsite(announcementsDelivery.isWebsite());
1168    
1169                    return announcementsDeliveryImpl;
1170            }
1171    
1172            /**
1173             * Returns the announcements delivery with the primary key or throws a {@link com.liferay.portal.NoSuchModelException} if it could not be found.
1174             *
1175             * @param primaryKey the primary key of the announcements delivery
1176             * @return the announcements delivery
1177             * @throws com.liferay.portlet.announcements.NoSuchDeliveryException if a announcements delivery with the primary key could not be found
1178             */
1179            @Override
1180            public AnnouncementsDelivery findByPrimaryKey(Serializable primaryKey)
1181                    throws NoSuchDeliveryException {
1182                    AnnouncementsDelivery announcementsDelivery = fetchByPrimaryKey(primaryKey);
1183    
1184                    if (announcementsDelivery == null) {
1185                            if (_log.isWarnEnabled()) {
1186                                    _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
1187                            }
1188    
1189                            throw new NoSuchDeliveryException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
1190                                    primaryKey);
1191                    }
1192    
1193                    return announcementsDelivery;
1194            }
1195    
1196            /**
1197             * Returns the announcements delivery with the primary key or throws a {@link com.liferay.portlet.announcements.NoSuchDeliveryException} if it could not be found.
1198             *
1199             * @param deliveryId the primary key of the announcements delivery
1200             * @return the announcements delivery
1201             * @throws com.liferay.portlet.announcements.NoSuchDeliveryException if a announcements delivery with the primary key could not be found
1202             */
1203            @Override
1204            public AnnouncementsDelivery findByPrimaryKey(long deliveryId)
1205                    throws NoSuchDeliveryException {
1206                    return findByPrimaryKey((Serializable)deliveryId);
1207            }
1208    
1209            /**
1210             * Returns the announcements delivery with the primary key or returns <code>null</code> if it could not be found.
1211             *
1212             * @param primaryKey the primary key of the announcements delivery
1213             * @return the announcements delivery, or <code>null</code> if a announcements delivery with the primary key could not be found
1214             */
1215            @Override
1216            public AnnouncementsDelivery fetchByPrimaryKey(Serializable primaryKey) {
1217                    AnnouncementsDelivery announcementsDelivery = (AnnouncementsDelivery)EntityCacheUtil.getResult(AnnouncementsDeliveryModelImpl.ENTITY_CACHE_ENABLED,
1218                                    AnnouncementsDeliveryImpl.class, primaryKey);
1219    
1220                    if (announcementsDelivery == _nullAnnouncementsDelivery) {
1221                            return null;
1222                    }
1223    
1224                    if (announcementsDelivery == null) {
1225                            Session session = null;
1226    
1227                            try {
1228                                    session = openSession();
1229    
1230                                    announcementsDelivery = (AnnouncementsDelivery)session.get(AnnouncementsDeliveryImpl.class,
1231                                                    primaryKey);
1232    
1233                                    if (announcementsDelivery != null) {
1234                                            cacheResult(announcementsDelivery);
1235                                    }
1236                                    else {
1237                                            EntityCacheUtil.putResult(AnnouncementsDeliveryModelImpl.ENTITY_CACHE_ENABLED,
1238                                                    AnnouncementsDeliveryImpl.class, primaryKey,
1239                                                    _nullAnnouncementsDelivery);
1240                                    }
1241                            }
1242                            catch (Exception e) {
1243                                    EntityCacheUtil.removeResult(AnnouncementsDeliveryModelImpl.ENTITY_CACHE_ENABLED,
1244                                            AnnouncementsDeliveryImpl.class, primaryKey);
1245    
1246                                    throw processException(e);
1247                            }
1248                            finally {
1249                                    closeSession(session);
1250                            }
1251                    }
1252    
1253                    return announcementsDelivery;
1254            }
1255    
1256            /**
1257             * Returns the announcements delivery with the primary key or returns <code>null</code> if it could not be found.
1258             *
1259             * @param deliveryId the primary key of the announcements delivery
1260             * @return the announcements delivery, or <code>null</code> if a announcements delivery with the primary key could not be found
1261             */
1262            @Override
1263            public AnnouncementsDelivery fetchByPrimaryKey(long deliveryId) {
1264                    return fetchByPrimaryKey((Serializable)deliveryId);
1265            }
1266    
1267            @Override
1268            public Map<Serializable, AnnouncementsDelivery> fetchByPrimaryKeys(
1269                    Set<Serializable> primaryKeys) {
1270                    if (primaryKeys.isEmpty()) {
1271                            return Collections.emptyMap();
1272                    }
1273    
1274                    Map<Serializable, AnnouncementsDelivery> map = new HashMap<Serializable, AnnouncementsDelivery>();
1275    
1276                    if (primaryKeys.size() == 1) {
1277                            Iterator<Serializable> iterator = primaryKeys.iterator();
1278    
1279                            Serializable primaryKey = iterator.next();
1280    
1281                            AnnouncementsDelivery announcementsDelivery = fetchByPrimaryKey(primaryKey);
1282    
1283                            if (announcementsDelivery != null) {
1284                                    map.put(primaryKey, announcementsDelivery);
1285                            }
1286    
1287                            return map;
1288                    }
1289    
1290                    Set<Serializable> uncachedPrimaryKeys = null;
1291    
1292                    for (Serializable primaryKey : primaryKeys) {
1293                            AnnouncementsDelivery announcementsDelivery = (AnnouncementsDelivery)EntityCacheUtil.getResult(AnnouncementsDeliveryModelImpl.ENTITY_CACHE_ENABLED,
1294                                            AnnouncementsDeliveryImpl.class, primaryKey);
1295    
1296                            if (announcementsDelivery == null) {
1297                                    if (uncachedPrimaryKeys == null) {
1298                                            uncachedPrimaryKeys = new HashSet<Serializable>();
1299                                    }
1300    
1301                                    uncachedPrimaryKeys.add(primaryKey);
1302                            }
1303                            else {
1304                                    map.put(primaryKey, announcementsDelivery);
1305                            }
1306                    }
1307    
1308                    if (uncachedPrimaryKeys == null) {
1309                            return map;
1310                    }
1311    
1312                    StringBundler query = new StringBundler((uncachedPrimaryKeys.size() * 2) +
1313                                    1);
1314    
1315                    query.append(_SQL_SELECT_ANNOUNCEMENTSDELIVERY_WHERE_PKS_IN);
1316    
1317                    for (Serializable primaryKey : uncachedPrimaryKeys) {
1318                            query.append(String.valueOf(primaryKey));
1319    
1320                            query.append(StringPool.COMMA);
1321                    }
1322    
1323                    query.setIndex(query.index() - 1);
1324    
1325                    query.append(StringPool.CLOSE_PARENTHESIS);
1326    
1327                    String sql = query.toString();
1328    
1329                    Session session = null;
1330    
1331                    try {
1332                            session = openSession();
1333    
1334                            Query q = session.createQuery(sql);
1335    
1336                            for (AnnouncementsDelivery announcementsDelivery : (List<AnnouncementsDelivery>)q.list()) {
1337                                    map.put(announcementsDelivery.getPrimaryKeyObj(),
1338                                            announcementsDelivery);
1339    
1340                                    cacheResult(announcementsDelivery);
1341    
1342                                    uncachedPrimaryKeys.remove(announcementsDelivery.getPrimaryKeyObj());
1343                            }
1344    
1345                            for (Serializable primaryKey : uncachedPrimaryKeys) {
1346                                    EntityCacheUtil.putResult(AnnouncementsDeliveryModelImpl.ENTITY_CACHE_ENABLED,
1347                                            AnnouncementsDeliveryImpl.class, primaryKey,
1348                                            _nullAnnouncementsDelivery);
1349                            }
1350                    }
1351                    catch (Exception e) {
1352                            throw processException(e);
1353                    }
1354                    finally {
1355                            closeSession(session);
1356                    }
1357    
1358                    return map;
1359            }
1360    
1361            /**
1362             * Returns all the announcements deliveries.
1363             *
1364             * @return the announcements deliveries
1365             */
1366            @Override
1367            public List<AnnouncementsDelivery> findAll() {
1368                    return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
1369            }
1370    
1371            /**
1372             * Returns a range of all the announcements deliveries.
1373             *
1374             * <p>
1375             * 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 com.liferay.portlet.announcements.model.impl.AnnouncementsDeliveryModelImpl}. 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.
1376             * </p>
1377             *
1378             * @param start the lower bound of the range of announcements deliveries
1379             * @param end the upper bound of the range of announcements deliveries (not inclusive)
1380             * @return the range of announcements deliveries
1381             */
1382            @Override
1383            public List<AnnouncementsDelivery> findAll(int start, int end) {
1384                    return findAll(start, end, null);
1385            }
1386    
1387            /**
1388             * Returns an ordered range of all the announcements deliveries.
1389             *
1390             * <p>
1391             * 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 com.liferay.portlet.announcements.model.impl.AnnouncementsDeliveryModelImpl}. 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.
1392             * </p>
1393             *
1394             * @param start the lower bound of the range of announcements deliveries
1395             * @param end the upper bound of the range of announcements deliveries (not inclusive)
1396             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
1397             * @return the ordered range of announcements deliveries
1398             */
1399            @Override
1400            public List<AnnouncementsDelivery> findAll(int start, int end,
1401                    OrderByComparator<AnnouncementsDelivery> orderByComparator) {
1402                    boolean pagination = true;
1403                    FinderPath finderPath = null;
1404                    Object[] finderArgs = null;
1405    
1406                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
1407                                    (orderByComparator == null)) {
1408                            pagination = false;
1409                            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL;
1410                            finderArgs = FINDER_ARGS_EMPTY;
1411                    }
1412                    else {
1413                            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_ALL;
1414                            finderArgs = new Object[] { start, end, orderByComparator };
1415                    }
1416    
1417                    List<AnnouncementsDelivery> list = (List<AnnouncementsDelivery>)FinderCacheUtil.getResult(finderPath,
1418                                    finderArgs, this);
1419    
1420                    if (list == null) {
1421                            StringBundler query = null;
1422                            String sql = null;
1423    
1424                            if (orderByComparator != null) {
1425                                    query = new StringBundler(2 +
1426                                                    (orderByComparator.getOrderByFields().length * 3));
1427    
1428                                    query.append(_SQL_SELECT_ANNOUNCEMENTSDELIVERY);
1429    
1430                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
1431                                            orderByComparator);
1432    
1433                                    sql = query.toString();
1434                            }
1435                            else {
1436                                    sql = _SQL_SELECT_ANNOUNCEMENTSDELIVERY;
1437    
1438                                    if (pagination) {
1439                                            sql = sql.concat(AnnouncementsDeliveryModelImpl.ORDER_BY_JPQL);
1440                                    }
1441                            }
1442    
1443                            Session session = null;
1444    
1445                            try {
1446                                    session = openSession();
1447    
1448                                    Query q = session.createQuery(sql);
1449    
1450                                    if (!pagination) {
1451                                            list = (List<AnnouncementsDelivery>)QueryUtil.list(q,
1452                                                            getDialect(), start, end, false);
1453    
1454                                            Collections.sort(list);
1455    
1456                                            list = Collections.unmodifiableList(list);
1457                                    }
1458                                    else {
1459                                            list = (List<AnnouncementsDelivery>)QueryUtil.list(q,
1460                                                            getDialect(), start, end);
1461                                    }
1462    
1463                                    cacheResult(list);
1464    
1465                                    FinderCacheUtil.putResult(finderPath, finderArgs, list);
1466                            }
1467                            catch (Exception e) {
1468                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
1469    
1470                                    throw processException(e);
1471                            }
1472                            finally {
1473                                    closeSession(session);
1474                            }
1475                    }
1476    
1477                    return list;
1478            }
1479    
1480            /**
1481             * Removes all the announcements deliveries from the database.
1482             *
1483             */
1484            @Override
1485            public void removeAll() {
1486                    for (AnnouncementsDelivery announcementsDelivery : findAll()) {
1487                            remove(announcementsDelivery);
1488                    }
1489            }
1490    
1491            /**
1492             * Returns the number of announcements deliveries.
1493             *
1494             * @return the number of announcements deliveries
1495             */
1496            @Override
1497            public int countAll() {
1498                    Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
1499                                    FINDER_ARGS_EMPTY, this);
1500    
1501                    if (count == null) {
1502                            Session session = null;
1503    
1504                            try {
1505                                    session = openSession();
1506    
1507                                    Query q = session.createQuery(_SQL_COUNT_ANNOUNCEMENTSDELIVERY);
1508    
1509                                    count = (Long)q.uniqueResult();
1510    
1511                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL,
1512                                            FINDER_ARGS_EMPTY, count);
1513                            }
1514                            catch (Exception e) {
1515                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_ALL,
1516                                            FINDER_ARGS_EMPTY);
1517    
1518                                    throw processException(e);
1519                            }
1520                            finally {
1521                                    closeSession(session);
1522                            }
1523                    }
1524    
1525                    return count.intValue();
1526            }
1527    
1528            @Override
1529            protected Set<String> getBadColumnNames() {
1530                    return _badColumnNames;
1531            }
1532    
1533            /**
1534             * Initializes the announcements delivery persistence.
1535             */
1536            public void afterPropertiesSet() {
1537            }
1538    
1539            public void destroy() {
1540                    EntityCacheUtil.removeCache(AnnouncementsDeliveryImpl.class.getName());
1541                    FinderCacheUtil.removeCache(FINDER_CLASS_NAME_ENTITY);
1542                    FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
1543                    FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
1544            }
1545    
1546            private static final String _SQL_SELECT_ANNOUNCEMENTSDELIVERY = "SELECT announcementsDelivery FROM AnnouncementsDelivery announcementsDelivery";
1547            private static final String _SQL_SELECT_ANNOUNCEMENTSDELIVERY_WHERE_PKS_IN = "SELECT announcementsDelivery FROM AnnouncementsDelivery announcementsDelivery WHERE deliveryId IN (";
1548            private static final String _SQL_SELECT_ANNOUNCEMENTSDELIVERY_WHERE = "SELECT announcementsDelivery FROM AnnouncementsDelivery announcementsDelivery WHERE ";
1549            private static final String _SQL_COUNT_ANNOUNCEMENTSDELIVERY = "SELECT COUNT(announcementsDelivery) FROM AnnouncementsDelivery announcementsDelivery";
1550            private static final String _SQL_COUNT_ANNOUNCEMENTSDELIVERY_WHERE = "SELECT COUNT(announcementsDelivery) FROM AnnouncementsDelivery announcementsDelivery WHERE ";
1551            private static final String _ORDER_BY_ENTITY_ALIAS = "announcementsDelivery.";
1552            private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No AnnouncementsDelivery exists with the primary key ";
1553            private static final String _NO_SUCH_ENTITY_WITH_KEY = "No AnnouncementsDelivery exists with the key {";
1554            private static final boolean _HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE = com.liferay.portal.util.PropsValues.HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE;
1555            private static final Log _log = LogFactoryUtil.getLog(AnnouncementsDeliveryPersistenceImpl.class);
1556            private static final Set<String> _badColumnNames = SetUtil.fromArray(new String[] {
1557                                    "type"
1558                            });
1559            private static final AnnouncementsDelivery _nullAnnouncementsDelivery = new AnnouncementsDeliveryImpl() {
1560                            @Override
1561                            public Object clone() {
1562                                    return this;
1563                            }
1564    
1565                            @Override
1566                            public CacheModel<AnnouncementsDelivery> toCacheModel() {
1567                                    return _nullAnnouncementsDeliveryCacheModel;
1568                            }
1569                    };
1570    
1571            private static final CacheModel<AnnouncementsDelivery> _nullAnnouncementsDeliveryCacheModel =
1572                    new CacheModel<AnnouncementsDelivery>() {
1573                            @Override
1574                            public AnnouncementsDelivery toEntityModel() {
1575                                    return _nullAnnouncementsDelivery;
1576                            }
1577                    };
1578    }