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