1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.announcements.service.persistence;
16  
17  import com.liferay.portal.NoSuchModelException;
18  import com.liferay.portal.kernel.annotation.BeanReference;
19  import com.liferay.portal.kernel.cache.CacheRegistry;
20  import com.liferay.portal.kernel.dao.orm.EntityCacheUtil;
21  import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
22  import com.liferay.portal.kernel.dao.orm.FinderPath;
23  import com.liferay.portal.kernel.dao.orm.Query;
24  import com.liferay.portal.kernel.dao.orm.QueryPos;
25  import com.liferay.portal.kernel.dao.orm.QueryUtil;
26  import com.liferay.portal.kernel.dao.orm.Session;
27  import com.liferay.portal.kernel.exception.SystemException;
28  import com.liferay.portal.kernel.log.Log;
29  import com.liferay.portal.kernel.log.LogFactoryUtil;
30  import com.liferay.portal.kernel.util.GetterUtil;
31  import com.liferay.portal.kernel.util.InstanceFactory;
32  import com.liferay.portal.kernel.util.OrderByComparator;
33  import com.liferay.portal.kernel.util.StringBundler;
34  import com.liferay.portal.kernel.util.StringPool;
35  import com.liferay.portal.kernel.util.StringUtil;
36  import com.liferay.portal.kernel.util.Validator;
37  import com.liferay.portal.model.ModelListener;
38  import com.liferay.portal.service.persistence.BatchSessionUtil;
39  import com.liferay.portal.service.persistence.ResourcePersistence;
40  import com.liferay.portal.service.persistence.UserPersistence;
41  import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
42  
43  import com.liferay.portlet.announcements.NoSuchDeliveryException;
44  import com.liferay.portlet.announcements.model.AnnouncementsDelivery;
45  import com.liferay.portlet.announcements.model.impl.AnnouncementsDeliveryImpl;
46  import com.liferay.portlet.announcements.model.impl.AnnouncementsDeliveryModelImpl;
47  
48  import java.io.Serializable;
49  
50  import java.util.ArrayList;
51  import java.util.Collections;
52  import java.util.List;
53  
54  /**
55   * <a href="AnnouncementsDeliveryPersistenceImpl.java.html"><b><i>View Source</i></b></a>
56   *
57   * <p>
58   * ServiceBuilder generated this class. Modifications in this class will be
59   * overwritten the next time is generated.
60   * </p>
61   *
62   * @author    Brian Wing Shun Chan
63   * @see       AnnouncementsDeliveryPersistence
64   * @see       AnnouncementsDeliveryUtil
65   * @generated
66   */
67  public class AnnouncementsDeliveryPersistenceImpl extends BasePersistenceImpl<AnnouncementsDelivery>
68      implements AnnouncementsDeliveryPersistence {
69      public static final String FINDER_CLASS_NAME_ENTITY = AnnouncementsDeliveryImpl.class.getName();
70      public static final String FINDER_CLASS_NAME_LIST = FINDER_CLASS_NAME_ENTITY +
71          ".List";
72      public static final FinderPath FINDER_PATH_FIND_BY_USERID = new FinderPath(AnnouncementsDeliveryModelImpl.ENTITY_CACHE_ENABLED,
73              AnnouncementsDeliveryModelImpl.FINDER_CACHE_ENABLED,
74              FINDER_CLASS_NAME_LIST, "findByUserId",
75              new String[] {
76                  Long.class.getName(),
77                  
78              "java.lang.Integer", "java.lang.Integer",
79                  "com.liferay.portal.kernel.util.OrderByComparator"
80              });
81      public static final FinderPath FINDER_PATH_COUNT_BY_USERID = new FinderPath(AnnouncementsDeliveryModelImpl.ENTITY_CACHE_ENABLED,
82              AnnouncementsDeliveryModelImpl.FINDER_CACHE_ENABLED,
83              FINDER_CLASS_NAME_LIST, "countByUserId",
84              new String[] { Long.class.getName() });
85      public static final FinderPath FINDER_PATH_FETCH_BY_U_T = new FinderPath(AnnouncementsDeliveryModelImpl.ENTITY_CACHE_ENABLED,
86              AnnouncementsDeliveryModelImpl.FINDER_CACHE_ENABLED,
87              FINDER_CLASS_NAME_ENTITY, "fetchByU_T",
88              new String[] { Long.class.getName(), String.class.getName() });
89      public static final FinderPath FINDER_PATH_COUNT_BY_U_T = new FinderPath(AnnouncementsDeliveryModelImpl.ENTITY_CACHE_ENABLED,
90              AnnouncementsDeliveryModelImpl.FINDER_CACHE_ENABLED,
91              FINDER_CLASS_NAME_LIST, "countByU_T",
92              new String[] { Long.class.getName(), String.class.getName() });
93      public static final FinderPath FINDER_PATH_FIND_ALL = new FinderPath(AnnouncementsDeliveryModelImpl.ENTITY_CACHE_ENABLED,
94              AnnouncementsDeliveryModelImpl.FINDER_CACHE_ENABLED,
95              FINDER_CLASS_NAME_LIST, "findAll", new String[0]);
96      public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(AnnouncementsDeliveryModelImpl.ENTITY_CACHE_ENABLED,
97              AnnouncementsDeliveryModelImpl.FINDER_CACHE_ENABLED,
98              FINDER_CLASS_NAME_LIST, "countAll", new String[0]);
99  
100     public void cacheResult(AnnouncementsDelivery announcementsDelivery) {
101         EntityCacheUtil.putResult(AnnouncementsDeliveryModelImpl.ENTITY_CACHE_ENABLED,
102             AnnouncementsDeliveryImpl.class,
103             announcementsDelivery.getPrimaryKey(), announcementsDelivery);
104 
105         FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_U_T,
106             new Object[] {
107                 new Long(announcementsDelivery.getUserId()),
108                 
109             announcementsDelivery.getType()
110             }, announcementsDelivery);
111     }
112 
113     public void cacheResult(List<AnnouncementsDelivery> announcementsDeliveries) {
114         for (AnnouncementsDelivery announcementsDelivery : announcementsDeliveries) {
115             if (EntityCacheUtil.getResult(
116                         AnnouncementsDeliveryModelImpl.ENTITY_CACHE_ENABLED,
117                         AnnouncementsDeliveryImpl.class,
118                         announcementsDelivery.getPrimaryKey(), this) == null) {
119                 cacheResult(announcementsDelivery);
120             }
121         }
122     }
123 
124     public void clearCache() {
125         CacheRegistry.clear(AnnouncementsDeliveryImpl.class.getName());
126         EntityCacheUtil.clearCache(AnnouncementsDeliveryImpl.class.getName());
127         FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
128         FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
129     }
130 
131     public void clearCache(AnnouncementsDelivery announcementsDelivery) {
132         EntityCacheUtil.removeResult(AnnouncementsDeliveryModelImpl.ENTITY_CACHE_ENABLED,
133             AnnouncementsDeliveryImpl.class,
134             announcementsDelivery.getPrimaryKey());
135 
136         FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_U_T,
137             new Object[] {
138                 new Long(announcementsDelivery.getUserId()),
139                 
140             announcementsDelivery.getType()
141             });
142     }
143 
144     public AnnouncementsDelivery create(long deliveryId) {
145         AnnouncementsDelivery announcementsDelivery = new AnnouncementsDeliveryImpl();
146 
147         announcementsDelivery.setNew(true);
148         announcementsDelivery.setPrimaryKey(deliveryId);
149 
150         return announcementsDelivery;
151     }
152 
153     public AnnouncementsDelivery remove(Serializable primaryKey)
154         throws NoSuchModelException, SystemException {
155         return remove(((Long)primaryKey).longValue());
156     }
157 
158     public AnnouncementsDelivery remove(long deliveryId)
159         throws NoSuchDeliveryException, SystemException {
160         Session session = null;
161 
162         try {
163             session = openSession();
164 
165             AnnouncementsDelivery announcementsDelivery = (AnnouncementsDelivery)session.get(AnnouncementsDeliveryImpl.class,
166                     new Long(deliveryId));
167 
168             if (announcementsDelivery == null) {
169                 if (_log.isWarnEnabled()) {
170                     _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + deliveryId);
171                 }
172 
173                 throw new NoSuchDeliveryException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
174                     deliveryId);
175             }
176 
177             return remove(announcementsDelivery);
178         }
179         catch (NoSuchDeliveryException nsee) {
180             throw nsee;
181         }
182         catch (Exception e) {
183             throw processException(e);
184         }
185         finally {
186             closeSession(session);
187         }
188     }
189 
190     public AnnouncementsDelivery remove(
191         AnnouncementsDelivery announcementsDelivery) throws SystemException {
192         for (ModelListener<AnnouncementsDelivery> listener : listeners) {
193             listener.onBeforeRemove(announcementsDelivery);
194         }
195 
196         announcementsDelivery = removeImpl(announcementsDelivery);
197 
198         for (ModelListener<AnnouncementsDelivery> listener : listeners) {
199             listener.onAfterRemove(announcementsDelivery);
200         }
201 
202         return announcementsDelivery;
203     }
204 
205     protected AnnouncementsDelivery removeImpl(
206         AnnouncementsDelivery announcementsDelivery) throws SystemException {
207         announcementsDelivery = toUnwrappedModel(announcementsDelivery);
208 
209         Session session = null;
210 
211         try {
212             session = openSession();
213 
214             if (announcementsDelivery.isCachedModel() ||
215                     BatchSessionUtil.isEnabled()) {
216                 Object staleObject = session.get(AnnouncementsDeliveryImpl.class,
217                         announcementsDelivery.getPrimaryKeyObj());
218 
219                 if (staleObject != null) {
220                     session.evict(staleObject);
221                 }
222             }
223 
224             session.delete(announcementsDelivery);
225 
226             session.flush();
227         }
228         catch (Exception e) {
229             throw processException(e);
230         }
231         finally {
232             closeSession(session);
233         }
234 
235         FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
236 
237         AnnouncementsDeliveryModelImpl announcementsDeliveryModelImpl = (AnnouncementsDeliveryModelImpl)announcementsDelivery;
238 
239         FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_U_T,
240             new Object[] {
241                 new Long(announcementsDeliveryModelImpl.getOriginalUserId()),
242                 
243             announcementsDeliveryModelImpl.getOriginalType()
244             });
245 
246         EntityCacheUtil.removeResult(AnnouncementsDeliveryModelImpl.ENTITY_CACHE_ENABLED,
247             AnnouncementsDeliveryImpl.class,
248             announcementsDelivery.getPrimaryKey());
249 
250         return announcementsDelivery;
251     }
252 
253     public AnnouncementsDelivery updateImpl(
254         com.liferay.portlet.announcements.model.AnnouncementsDelivery announcementsDelivery,
255         boolean merge) throws SystemException {
256         announcementsDelivery = toUnwrappedModel(announcementsDelivery);
257 
258         boolean isNew = announcementsDelivery.isNew();
259 
260         AnnouncementsDeliveryModelImpl announcementsDeliveryModelImpl = (AnnouncementsDeliveryModelImpl)announcementsDelivery;
261 
262         Session session = null;
263 
264         try {
265             session = openSession();
266 
267             BatchSessionUtil.update(session, announcementsDelivery, merge);
268 
269             announcementsDelivery.setNew(false);
270         }
271         catch (Exception e) {
272             throw processException(e);
273         }
274         finally {
275             closeSession(session);
276         }
277 
278         FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
279 
280         EntityCacheUtil.putResult(AnnouncementsDeliveryModelImpl.ENTITY_CACHE_ENABLED,
281             AnnouncementsDeliveryImpl.class,
282             announcementsDelivery.getPrimaryKey(), announcementsDelivery);
283 
284         if (!isNew &&
285                 ((announcementsDelivery.getUserId() != announcementsDeliveryModelImpl.getOriginalUserId()) ||
286                 !Validator.equals(announcementsDelivery.getType(),
287                     announcementsDeliveryModelImpl.getOriginalType()))) {
288             FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_U_T,
289                 new Object[] {
290                     new Long(announcementsDeliveryModelImpl.getOriginalUserId()),
291                     
292                 announcementsDeliveryModelImpl.getOriginalType()
293                 });
294         }
295 
296         if (isNew ||
297                 ((announcementsDelivery.getUserId() != announcementsDeliveryModelImpl.getOriginalUserId()) ||
298                 !Validator.equals(announcementsDelivery.getType(),
299                     announcementsDeliveryModelImpl.getOriginalType()))) {
300             FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_U_T,
301                 new Object[] {
302                     new Long(announcementsDelivery.getUserId()),
303                     
304                 announcementsDelivery.getType()
305                 }, announcementsDelivery);
306         }
307 
308         return announcementsDelivery;
309     }
310 
311     protected AnnouncementsDelivery toUnwrappedModel(
312         AnnouncementsDelivery announcementsDelivery) {
313         if (announcementsDelivery instanceof AnnouncementsDeliveryImpl) {
314             return announcementsDelivery;
315         }
316 
317         AnnouncementsDeliveryImpl announcementsDeliveryImpl = new AnnouncementsDeliveryImpl();
318 
319         announcementsDeliveryImpl.setNew(announcementsDelivery.isNew());
320         announcementsDeliveryImpl.setPrimaryKey(announcementsDelivery.getPrimaryKey());
321 
322         announcementsDeliveryImpl.setDeliveryId(announcementsDelivery.getDeliveryId());
323         announcementsDeliveryImpl.setCompanyId(announcementsDelivery.getCompanyId());
324         announcementsDeliveryImpl.setUserId(announcementsDelivery.getUserId());
325         announcementsDeliveryImpl.setType(announcementsDelivery.getType());
326         announcementsDeliveryImpl.setEmail(announcementsDelivery.isEmail());
327         announcementsDeliveryImpl.setSms(announcementsDelivery.isSms());
328         announcementsDeliveryImpl.setWebsite(announcementsDelivery.isWebsite());
329 
330         return announcementsDeliveryImpl;
331     }
332 
333     public AnnouncementsDelivery findByPrimaryKey(Serializable primaryKey)
334         throws NoSuchModelException, SystemException {
335         return findByPrimaryKey(((Long)primaryKey).longValue());
336     }
337 
338     public AnnouncementsDelivery findByPrimaryKey(long deliveryId)
339         throws NoSuchDeliveryException, SystemException {
340         AnnouncementsDelivery announcementsDelivery = fetchByPrimaryKey(deliveryId);
341 
342         if (announcementsDelivery == null) {
343             if (_log.isWarnEnabled()) {
344                 _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + deliveryId);
345             }
346 
347             throw new NoSuchDeliveryException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
348                 deliveryId);
349         }
350 
351         return announcementsDelivery;
352     }
353 
354     public AnnouncementsDelivery fetchByPrimaryKey(Serializable primaryKey)
355         throws SystemException {
356         return fetchByPrimaryKey(((Long)primaryKey).longValue());
357     }
358 
359     public AnnouncementsDelivery fetchByPrimaryKey(long deliveryId)
360         throws SystemException {
361         AnnouncementsDelivery announcementsDelivery = (AnnouncementsDelivery)EntityCacheUtil.getResult(AnnouncementsDeliveryModelImpl.ENTITY_CACHE_ENABLED,
362                 AnnouncementsDeliveryImpl.class, deliveryId, this);
363 
364         if (announcementsDelivery == null) {
365             Session session = null;
366 
367             try {
368                 session = openSession();
369 
370                 announcementsDelivery = (AnnouncementsDelivery)session.get(AnnouncementsDeliveryImpl.class,
371                         new Long(deliveryId));
372             }
373             catch (Exception e) {
374                 throw processException(e);
375             }
376             finally {
377                 if (announcementsDelivery != null) {
378                     cacheResult(announcementsDelivery);
379                 }
380 
381                 closeSession(session);
382             }
383         }
384 
385         return announcementsDelivery;
386     }
387 
388     public List<AnnouncementsDelivery> findByUserId(long userId)
389         throws SystemException {
390         return findByUserId(userId, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
391     }
392 
393     public List<AnnouncementsDelivery> findByUserId(long userId, int start,
394         int end) throws SystemException {
395         return findByUserId(userId, start, end, null);
396     }
397 
398     public List<AnnouncementsDelivery> findByUserId(long userId, int start,
399         int end, OrderByComparator orderByComparator) throws SystemException {
400         Object[] finderArgs = new Object[] {
401                 new Long(userId),
402                 
403                 String.valueOf(start), String.valueOf(end),
404                 String.valueOf(orderByComparator)
405             };
406 
407         List<AnnouncementsDelivery> list = (List<AnnouncementsDelivery>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_USERID,
408                 finderArgs, this);
409 
410         if (list == null) {
411             Session session = null;
412 
413             try {
414                 session = openSession();
415 
416                 StringBundler query = null;
417 
418                 if (orderByComparator != null) {
419                     query = new StringBundler(3 +
420                             (orderByComparator.getOrderByFields().length * 3));
421                 }
422                 else {
423                     query = new StringBundler(2);
424                 }
425 
426                 query.append(_SQL_SELECT_ANNOUNCEMENTSDELIVERY_WHERE);
427 
428                 query.append(_FINDER_COLUMN_USERID_USERID_2);
429 
430                 if (orderByComparator != null) {
431                     appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
432                         orderByComparator);
433                 }
434 
435                 String sql = query.toString();
436 
437                 Query q = session.createQuery(sql);
438 
439                 QueryPos qPos = QueryPos.getInstance(q);
440 
441                 qPos.add(userId);
442 
443                 list = (List<AnnouncementsDelivery>)QueryUtil.list(q,
444                         getDialect(), start, end);
445             }
446             catch (Exception e) {
447                 throw processException(e);
448             }
449             finally {
450                 if (list == null) {
451                     list = new ArrayList<AnnouncementsDelivery>();
452                 }
453 
454                 cacheResult(list);
455 
456                 FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_USERID,
457                     finderArgs, list);
458 
459                 closeSession(session);
460             }
461         }
462 
463         return list;
464     }
465 
466     public AnnouncementsDelivery findByUserId_First(long userId,
467         OrderByComparator orderByComparator)
468         throws NoSuchDeliveryException, SystemException {
469         List<AnnouncementsDelivery> list = findByUserId(userId, 0, 1,
470                 orderByComparator);
471 
472         if (list.isEmpty()) {
473             StringBundler msg = new StringBundler(4);
474 
475             msg.append(_NO_SUCH_ENTITY_WITH_KEY);
476 
477             msg.append("userId=");
478             msg.append(userId);
479 
480             msg.append(StringPool.CLOSE_CURLY_BRACE);
481 
482             throw new NoSuchDeliveryException(msg.toString());
483         }
484         else {
485             return list.get(0);
486         }
487     }
488 
489     public AnnouncementsDelivery findByUserId_Last(long userId,
490         OrderByComparator orderByComparator)
491         throws NoSuchDeliveryException, SystemException {
492         int count = countByUserId(userId);
493 
494         List<AnnouncementsDelivery> list = findByUserId(userId, count - 1,
495                 count, orderByComparator);
496 
497         if (list.isEmpty()) {
498             StringBundler msg = new StringBundler(4);
499 
500             msg.append(_NO_SUCH_ENTITY_WITH_KEY);
501 
502             msg.append("userId=");
503             msg.append(userId);
504 
505             msg.append(StringPool.CLOSE_CURLY_BRACE);
506 
507             throw new NoSuchDeliveryException(msg.toString());
508         }
509         else {
510             return list.get(0);
511         }
512     }
513 
514     public AnnouncementsDelivery[] findByUserId_PrevAndNext(long deliveryId,
515         long userId, OrderByComparator orderByComparator)
516         throws NoSuchDeliveryException, SystemException {
517         AnnouncementsDelivery announcementsDelivery = findByPrimaryKey(deliveryId);
518 
519         Session session = null;
520 
521         try {
522             session = openSession();
523 
524             AnnouncementsDelivery[] array = new AnnouncementsDeliveryImpl[3];
525 
526             array[0] = getByUserId_PrevAndNext(session, announcementsDelivery,
527                     userId, orderByComparator, true);
528 
529             array[1] = announcementsDelivery;
530 
531             array[2] = getByUserId_PrevAndNext(session, announcementsDelivery,
532                     userId, orderByComparator, false);
533 
534             return array;
535         }
536         catch (Exception e) {
537             throw processException(e);
538         }
539         finally {
540             closeSession(session);
541         }
542     }
543 
544     protected AnnouncementsDelivery getByUserId_PrevAndNext(Session session,
545         AnnouncementsDelivery announcementsDelivery, long userId,
546         OrderByComparator orderByComparator, boolean previous) {
547         StringBundler query = null;
548 
549         if (orderByComparator != null) {
550             query = new StringBundler(6 +
551                     (orderByComparator.getOrderByFields().length * 6));
552         }
553         else {
554             query = new StringBundler(3);
555         }
556 
557         query.append(_SQL_SELECT_ANNOUNCEMENTSDELIVERY_WHERE);
558 
559         query.append(_FINDER_COLUMN_USERID_USERID_2);
560 
561         if (orderByComparator != null) {
562             String[] orderByFields = orderByComparator.getOrderByFields();
563 
564             if (orderByFields.length > 0) {
565                 query.append(WHERE_AND);
566             }
567 
568             for (int i = 0; i < orderByFields.length; i++) {
569                 query.append(_ORDER_BY_ENTITY_ALIAS);
570                 query.append(orderByFields[i]);
571 
572                 if ((i + 1) < orderByFields.length) {
573                     if (orderByComparator.isAscending() ^ previous) {
574                         query.append(WHERE_GREATER_THAN_HAS_NEXT);
575                     }
576                     else {
577                         query.append(WHERE_LESSER_THAN_HAS_NEXT);
578                     }
579                 }
580                 else {
581                     if (orderByComparator.isAscending() ^ previous) {
582                         query.append(WHERE_GREATER_THAN);
583                     }
584                     else {
585                         query.append(WHERE_LESSER_THAN);
586                     }
587                 }
588             }
589 
590             query.append(ORDER_BY_CLAUSE);
591 
592             for (int i = 0; i < orderByFields.length; i++) {
593                 query.append(_ORDER_BY_ENTITY_ALIAS);
594                 query.append(orderByFields[i]);
595 
596                 if ((i + 1) < orderByFields.length) {
597                     if (orderByComparator.isAscending() ^ previous) {
598                         query.append(ORDER_BY_ASC_HAS_NEXT);
599                     }
600                     else {
601                         query.append(ORDER_BY_DESC_HAS_NEXT);
602                     }
603                 }
604                 else {
605                     if (orderByComparator.isAscending() ^ previous) {
606                         query.append(ORDER_BY_ASC);
607                     }
608                     else {
609                         query.append(ORDER_BY_DESC);
610                     }
611                 }
612             }
613         }
614 
615         String sql = query.toString();
616 
617         Query q = session.createQuery(sql);
618 
619         q.setFirstResult(0);
620         q.setMaxResults(2);
621 
622         QueryPos qPos = QueryPos.getInstance(q);
623 
624         qPos.add(userId);
625 
626         if (orderByComparator != null) {
627             Object[] values = orderByComparator.getOrderByValues(announcementsDelivery);
628 
629             for (Object value : values) {
630                 qPos.add(value);
631             }
632         }
633 
634         List<AnnouncementsDelivery> list = q.list();
635 
636         if (list.size() == 2) {
637             return list.get(1);
638         }
639         else {
640             return null;
641         }
642     }
643 
644     public AnnouncementsDelivery findByU_T(long userId, String type)
645         throws NoSuchDeliveryException, SystemException {
646         AnnouncementsDelivery announcementsDelivery = fetchByU_T(userId, type);
647 
648         if (announcementsDelivery == null) {
649             StringBundler msg = new StringBundler(6);
650 
651             msg.append(_NO_SUCH_ENTITY_WITH_KEY);
652 
653             msg.append("userId=");
654             msg.append(userId);
655 
656             msg.append(", type=");
657             msg.append(type);
658 
659             msg.append(StringPool.CLOSE_CURLY_BRACE);
660 
661             if (_log.isWarnEnabled()) {
662                 _log.warn(msg.toString());
663             }
664 
665             throw new NoSuchDeliveryException(msg.toString());
666         }
667 
668         return announcementsDelivery;
669     }
670 
671     public AnnouncementsDelivery fetchByU_T(long userId, String type)
672         throws SystemException {
673         return fetchByU_T(userId, type, true);
674     }
675 
676     public AnnouncementsDelivery fetchByU_T(long userId, String type,
677         boolean retrieveFromCache) throws SystemException {
678         Object[] finderArgs = new Object[] { new Long(userId), type };
679 
680         Object result = null;
681 
682         if (retrieveFromCache) {
683             result = FinderCacheUtil.getResult(FINDER_PATH_FETCH_BY_U_T,
684                     finderArgs, this);
685         }
686 
687         if (result == null) {
688             Session session = null;
689 
690             try {
691                 session = openSession();
692 
693                 StringBundler query = new StringBundler(3);
694 
695                 query.append(_SQL_SELECT_ANNOUNCEMENTSDELIVERY_WHERE);
696 
697                 query.append(_FINDER_COLUMN_U_T_USERID_2);
698 
699                 if (type == null) {
700                     query.append(_FINDER_COLUMN_U_T_TYPE_1);
701                 }
702                 else {
703                     if (type.equals(StringPool.BLANK)) {
704                         query.append(_FINDER_COLUMN_U_T_TYPE_3);
705                     }
706                     else {
707                         query.append(_FINDER_COLUMN_U_T_TYPE_2);
708                     }
709                 }
710 
711                 String sql = query.toString();
712 
713                 Query q = session.createQuery(sql);
714 
715                 QueryPos qPos = QueryPos.getInstance(q);
716 
717                 qPos.add(userId);
718 
719                 if (type != null) {
720                     qPos.add(type);
721                 }
722 
723                 List<AnnouncementsDelivery> list = q.list();
724 
725                 result = list;
726 
727                 AnnouncementsDelivery announcementsDelivery = null;
728 
729                 if (list.isEmpty()) {
730                     FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_U_T,
731                         finderArgs, list);
732                 }
733                 else {
734                     announcementsDelivery = list.get(0);
735 
736                     cacheResult(announcementsDelivery);
737 
738                     if ((announcementsDelivery.getUserId() != userId) ||
739                             (announcementsDelivery.getType() == null) ||
740                             !announcementsDelivery.getType().equals(type)) {
741                         FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_U_T,
742                             finderArgs, announcementsDelivery);
743                     }
744                 }
745 
746                 return announcementsDelivery;
747             }
748             catch (Exception e) {
749                 throw processException(e);
750             }
751             finally {
752                 if (result == null) {
753                     FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_U_T,
754                         finderArgs, new ArrayList<AnnouncementsDelivery>());
755                 }
756 
757                 closeSession(session);
758             }
759         }
760         else {
761             if (result instanceof List<?>) {
762                 return null;
763             }
764             else {
765                 return (AnnouncementsDelivery)result;
766             }
767         }
768     }
769 
770     public List<AnnouncementsDelivery> findAll() throws SystemException {
771         return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
772     }
773 
774     public List<AnnouncementsDelivery> findAll(int start, int end)
775         throws SystemException {
776         return findAll(start, end, null);
777     }
778 
779     public List<AnnouncementsDelivery> findAll(int start, int end,
780         OrderByComparator orderByComparator) throws SystemException {
781         Object[] finderArgs = new Object[] {
782                 String.valueOf(start), String.valueOf(end),
783                 String.valueOf(orderByComparator)
784             };
785 
786         List<AnnouncementsDelivery> list = (List<AnnouncementsDelivery>)FinderCacheUtil.getResult(FINDER_PATH_FIND_ALL,
787                 finderArgs, this);
788 
789         if (list == null) {
790             Session session = null;
791 
792             try {
793                 session = openSession();
794 
795                 StringBundler query = null;
796                 String sql = null;
797 
798                 if (orderByComparator != null) {
799                     query = new StringBundler(2 +
800                             (orderByComparator.getOrderByFields().length * 3));
801 
802                     query.append(_SQL_SELECT_ANNOUNCEMENTSDELIVERY);
803 
804                     appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
805                         orderByComparator);
806 
807                     sql = query.toString();
808                 }
809 
810                 sql = _SQL_SELECT_ANNOUNCEMENTSDELIVERY;
811 
812                 Query q = session.createQuery(sql);
813 
814                 if (orderByComparator == null) {
815                     list = (List<AnnouncementsDelivery>)QueryUtil.list(q,
816                             getDialect(), start, end, false);
817 
818                     Collections.sort(list);
819                 }
820                 else {
821                     list = (List<AnnouncementsDelivery>)QueryUtil.list(q,
822                             getDialect(), start, end);
823                 }
824             }
825             catch (Exception e) {
826                 throw processException(e);
827             }
828             finally {
829                 if (list == null) {
830                     list = new ArrayList<AnnouncementsDelivery>();
831                 }
832 
833                 cacheResult(list);
834 
835                 FinderCacheUtil.putResult(FINDER_PATH_FIND_ALL, finderArgs, list);
836 
837                 closeSession(session);
838             }
839         }
840 
841         return list;
842     }
843 
844     public void removeByUserId(long userId) throws SystemException {
845         for (AnnouncementsDelivery announcementsDelivery : findByUserId(userId)) {
846             remove(announcementsDelivery);
847         }
848     }
849 
850     public void removeByU_T(long userId, String type)
851         throws NoSuchDeliveryException, SystemException {
852         AnnouncementsDelivery announcementsDelivery = findByU_T(userId, type);
853 
854         remove(announcementsDelivery);
855     }
856 
857     public void removeAll() throws SystemException {
858         for (AnnouncementsDelivery announcementsDelivery : findAll()) {
859             remove(announcementsDelivery);
860         }
861     }
862 
863     public int countByUserId(long userId) throws SystemException {
864         Object[] finderArgs = new Object[] { new Long(userId) };
865 
866         Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_USERID,
867                 finderArgs, this);
868 
869         if (count == null) {
870             Session session = null;
871 
872             try {
873                 session = openSession();
874 
875                 StringBundler query = new StringBundler(2);
876 
877                 query.append(_SQL_COUNT_ANNOUNCEMENTSDELIVERY_WHERE);
878 
879                 query.append(_FINDER_COLUMN_USERID_USERID_2);
880 
881                 String sql = query.toString();
882 
883                 Query q = session.createQuery(sql);
884 
885                 QueryPos qPos = QueryPos.getInstance(q);
886 
887                 qPos.add(userId);
888 
889                 count = (Long)q.uniqueResult();
890             }
891             catch (Exception e) {
892                 throw processException(e);
893             }
894             finally {
895                 if (count == null) {
896                     count = Long.valueOf(0);
897                 }
898 
899                 FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_USERID,
900                     finderArgs, count);
901 
902                 closeSession(session);
903             }
904         }
905 
906         return count.intValue();
907     }
908 
909     public int countByU_T(long userId, String type) throws SystemException {
910         Object[] finderArgs = new Object[] { new Long(userId), type };
911 
912         Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_U_T,
913                 finderArgs, this);
914 
915         if (count == null) {
916             Session session = null;
917 
918             try {
919                 session = openSession();
920 
921                 StringBundler query = new StringBundler(3);
922 
923                 query.append(_SQL_COUNT_ANNOUNCEMENTSDELIVERY_WHERE);
924 
925                 query.append(_FINDER_COLUMN_U_T_USERID_2);
926 
927                 if (type == null) {
928                     query.append(_FINDER_COLUMN_U_T_TYPE_1);
929                 }
930                 else {
931                     if (type.equals(StringPool.BLANK)) {
932                         query.append(_FINDER_COLUMN_U_T_TYPE_3);
933                     }
934                     else {
935                         query.append(_FINDER_COLUMN_U_T_TYPE_2);
936                     }
937                 }
938 
939                 String sql = query.toString();
940 
941                 Query q = session.createQuery(sql);
942 
943                 QueryPos qPos = QueryPos.getInstance(q);
944 
945                 qPos.add(userId);
946 
947                 if (type != null) {
948                     qPos.add(type);
949                 }
950 
951                 count = (Long)q.uniqueResult();
952             }
953             catch (Exception e) {
954                 throw processException(e);
955             }
956             finally {
957                 if (count == null) {
958                     count = Long.valueOf(0);
959                 }
960 
961                 FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_U_T, finderArgs,
962                     count);
963 
964                 closeSession(session);
965             }
966         }
967 
968         return count.intValue();
969     }
970 
971     public int countAll() throws SystemException {
972         Object[] finderArgs = new Object[0];
973 
974         Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
975                 finderArgs, this);
976 
977         if (count == null) {
978             Session session = null;
979 
980             try {
981                 session = openSession();
982 
983                 Query q = session.createQuery(_SQL_COUNT_ANNOUNCEMENTSDELIVERY);
984 
985                 count = (Long)q.uniqueResult();
986             }
987             catch (Exception e) {
988                 throw processException(e);
989             }
990             finally {
991                 if (count == null) {
992                     count = Long.valueOf(0);
993                 }
994 
995                 FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL, finderArgs,
996                     count);
997 
998                 closeSession(session);
999             }
1000        }
1001
1002        return count.intValue();
1003    }
1004
1005    public void afterPropertiesSet() {
1006        String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
1007                    com.liferay.portal.util.PropsUtil.get(
1008                        "value.object.listener.com.liferay.portlet.announcements.model.AnnouncementsDelivery")));
1009
1010        if (listenerClassNames.length > 0) {
1011            try {
1012                List<ModelListener<AnnouncementsDelivery>> listenersList = new ArrayList<ModelListener<AnnouncementsDelivery>>();
1013
1014                for (String listenerClassName : listenerClassNames) {
1015                    listenersList.add((ModelListener<AnnouncementsDelivery>)InstanceFactory.newInstance(
1016                            listenerClassName));
1017                }
1018
1019                listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
1020            }
1021            catch (Exception e) {
1022                _log.error(e);
1023            }
1024        }
1025    }
1026
1027    @BeanReference(type = AnnouncementsDeliveryPersistence.class)
1028    protected AnnouncementsDeliveryPersistence announcementsDeliveryPersistence;
1029    @BeanReference(type = AnnouncementsEntryPersistence.class)
1030    protected AnnouncementsEntryPersistence announcementsEntryPersistence;
1031    @BeanReference(type = AnnouncementsFlagPersistence.class)
1032    protected AnnouncementsFlagPersistence announcementsFlagPersistence;
1033    @BeanReference(type = ResourcePersistence.class)
1034    protected ResourcePersistence resourcePersistence;
1035    @BeanReference(type = UserPersistence.class)
1036    protected UserPersistence userPersistence;
1037    private static final String _SQL_SELECT_ANNOUNCEMENTSDELIVERY = "SELECT announcementsDelivery FROM AnnouncementsDelivery announcementsDelivery";
1038    private static final String _SQL_SELECT_ANNOUNCEMENTSDELIVERY_WHERE = "SELECT announcementsDelivery FROM AnnouncementsDelivery announcementsDelivery WHERE ";
1039    private static final String _SQL_COUNT_ANNOUNCEMENTSDELIVERY = "SELECT COUNT(announcementsDelivery) FROM AnnouncementsDelivery announcementsDelivery";
1040    private static final String _SQL_COUNT_ANNOUNCEMENTSDELIVERY_WHERE = "SELECT COUNT(announcementsDelivery) FROM AnnouncementsDelivery announcementsDelivery WHERE ";
1041    private static final String _FINDER_COLUMN_USERID_USERID_2 = "announcementsDelivery.userId = ?";
1042    private static final String _FINDER_COLUMN_U_T_USERID_2 = "announcementsDelivery.userId = ? AND ";
1043    private static final String _FINDER_COLUMN_U_T_TYPE_1 = "announcementsDelivery.type IS NULL";
1044    private static final String _FINDER_COLUMN_U_T_TYPE_2 = "announcementsDelivery.type = ?";
1045    private static final String _FINDER_COLUMN_U_T_TYPE_3 = "(announcementsDelivery.type IS NULL OR announcementsDelivery.type = ?)";
1046    private static final String _ORDER_BY_ENTITY_ALIAS = "announcementsDelivery.";
1047    private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No AnnouncementsDelivery exists with the primary key ";
1048    private static final String _NO_SUCH_ENTITY_WITH_KEY = "No AnnouncementsDelivery exists with the key {";
1049    private static Log _log = LogFactoryUtil.getLog(AnnouncementsDeliveryPersistenceImpl.class);
1050}