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