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