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