1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portlet.announcements.service.persistence;
16  
17  import com.liferay.portal.NoSuchModelException;
18  import com.liferay.portal.SystemException;
19  import com.liferay.portal.kernel.annotation.BeanReference;
20  import com.liferay.portal.kernel.cache.CacheRegistry;
21  import com.liferay.portal.kernel.dao.orm.EntityCacheUtil;
22  import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
23  import com.liferay.portal.kernel.dao.orm.FinderPath;
24  import com.liferay.portal.kernel.dao.orm.Query;
25  import com.liferay.portal.kernel.dao.orm.QueryPos;
26  import com.liferay.portal.kernel.dao.orm.QueryUtil;
27  import com.liferay.portal.kernel.dao.orm.Session;
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     protected AnnouncementsDelivery removeImpl(
191         AnnouncementsDelivery announcementsDelivery) throws SystemException {
192         announcementsDelivery = toUnwrappedModel(announcementsDelivery);
193 
194         Session session = null;
195 
196         try {
197             session = openSession();
198 
199             BatchSessionUtil.delete(session, announcementsDelivery);
200         }
201         catch (Exception e) {
202             throw processException(e);
203         }
204         finally {
205             closeSession(session);
206         }
207 
208         FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
209 
210         AnnouncementsDeliveryModelImpl announcementsDeliveryModelImpl = (AnnouncementsDeliveryModelImpl)announcementsDelivery;
211 
212         FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_U_T,
213             new Object[] {
214                 new Long(announcementsDeliveryModelImpl.getOriginalUserId()),
215                 
216             announcementsDeliveryModelImpl.getOriginalType()
217             });
218 
219         EntityCacheUtil.removeResult(AnnouncementsDeliveryModelImpl.ENTITY_CACHE_ENABLED,
220             AnnouncementsDeliveryImpl.class,
221             announcementsDelivery.getPrimaryKey());
222 
223         return announcementsDelivery;
224     }
225 
226     /**
227      * @deprecated Use {@link BasePersistence#update(com.liferay.portal.model.BaseModel, boolean)}.
228      */
229     public AnnouncementsDelivery update(
230         AnnouncementsDelivery announcementsDelivery) throws SystemException {
231         if (_log.isWarnEnabled()) {
232             _log.warn(
233                 "Using the deprecated update(AnnouncementsDelivery announcementsDelivery) method. Use update(AnnouncementsDelivery announcementsDelivery, boolean merge) instead.");
234         }
235 
236         return update(announcementsDelivery, false);
237     }
238 
239     public AnnouncementsDelivery updateImpl(
240         com.liferay.portlet.announcements.model.AnnouncementsDelivery announcementsDelivery,
241         boolean merge) throws SystemException {
242         announcementsDelivery = toUnwrappedModel(announcementsDelivery);
243 
244         boolean isNew = announcementsDelivery.isNew();
245 
246         AnnouncementsDeliveryModelImpl announcementsDeliveryModelImpl = (AnnouncementsDeliveryModelImpl)announcementsDelivery;
247 
248         Session session = null;
249 
250         try {
251             session = openSession();
252 
253             BatchSessionUtil.update(session, announcementsDelivery, merge);
254 
255             announcementsDelivery.setNew(false);
256         }
257         catch (Exception e) {
258             throw processException(e);
259         }
260         finally {
261             closeSession(session);
262         }
263 
264         FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
265 
266         EntityCacheUtil.putResult(AnnouncementsDeliveryModelImpl.ENTITY_CACHE_ENABLED,
267             AnnouncementsDeliveryImpl.class,
268             announcementsDelivery.getPrimaryKey(), announcementsDelivery);
269 
270         if (!isNew &&
271                 ((announcementsDelivery.getUserId() != announcementsDeliveryModelImpl.getOriginalUserId()) ||
272                 !Validator.equals(announcementsDelivery.getType(),
273                     announcementsDeliveryModelImpl.getOriginalType()))) {
274             FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_U_T,
275                 new Object[] {
276                     new Long(announcementsDeliveryModelImpl.getOriginalUserId()),
277                     
278                 announcementsDeliveryModelImpl.getOriginalType()
279                 });
280         }
281 
282         if (isNew ||
283                 ((announcementsDelivery.getUserId() != announcementsDeliveryModelImpl.getOriginalUserId()) ||
284                 !Validator.equals(announcementsDelivery.getType(),
285                     announcementsDeliveryModelImpl.getOriginalType()))) {
286             FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_U_T,
287                 new Object[] {
288                     new Long(announcementsDelivery.getUserId()),
289                     
290                 announcementsDelivery.getType()
291                 }, announcementsDelivery);
292         }
293 
294         return announcementsDelivery;
295     }
296 
297     protected AnnouncementsDelivery toUnwrappedModel(
298         AnnouncementsDelivery announcementsDelivery) {
299         if (announcementsDelivery instanceof AnnouncementsDeliveryImpl) {
300             return announcementsDelivery;
301         }
302 
303         AnnouncementsDeliveryImpl announcementsDeliveryImpl = new AnnouncementsDeliveryImpl();
304 
305         announcementsDeliveryImpl.setNew(announcementsDelivery.isNew());
306         announcementsDeliveryImpl.setPrimaryKey(announcementsDelivery.getPrimaryKey());
307 
308         announcementsDeliveryImpl.setDeliveryId(announcementsDelivery.getDeliveryId());
309         announcementsDeliveryImpl.setCompanyId(announcementsDelivery.getCompanyId());
310         announcementsDeliveryImpl.setUserId(announcementsDelivery.getUserId());
311         announcementsDeliveryImpl.setType(announcementsDelivery.getType());
312         announcementsDeliveryImpl.setEmail(announcementsDelivery.isEmail());
313         announcementsDeliveryImpl.setSms(announcementsDelivery.isSms());
314         announcementsDeliveryImpl.setWebsite(announcementsDelivery.isWebsite());
315 
316         return announcementsDeliveryImpl;
317     }
318 
319     public AnnouncementsDelivery findByPrimaryKey(Serializable primaryKey)
320         throws NoSuchModelException, SystemException {
321         return findByPrimaryKey(((Long)primaryKey).longValue());
322     }
323 
324     public AnnouncementsDelivery findByPrimaryKey(long deliveryId)
325         throws NoSuchDeliveryException, SystemException {
326         AnnouncementsDelivery announcementsDelivery = fetchByPrimaryKey(deliveryId);
327 
328         if (announcementsDelivery == null) {
329             if (_log.isWarnEnabled()) {
330                 _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + deliveryId);
331             }
332 
333             throw new NoSuchDeliveryException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
334                 deliveryId);
335         }
336 
337         return announcementsDelivery;
338     }
339 
340     public AnnouncementsDelivery fetchByPrimaryKey(Serializable primaryKey)
341         throws SystemException {
342         return fetchByPrimaryKey(((Long)primaryKey).longValue());
343     }
344 
345     public AnnouncementsDelivery fetchByPrimaryKey(long deliveryId)
346         throws SystemException {
347         AnnouncementsDelivery announcementsDelivery = (AnnouncementsDelivery)EntityCacheUtil.getResult(AnnouncementsDeliveryModelImpl.ENTITY_CACHE_ENABLED,
348                 AnnouncementsDeliveryImpl.class, deliveryId, this);
349 
350         if (announcementsDelivery == null) {
351             Session session = null;
352 
353             try {
354                 session = openSession();
355 
356                 announcementsDelivery = (AnnouncementsDelivery)session.get(AnnouncementsDeliveryImpl.class,
357                         new Long(deliveryId));
358             }
359             catch (Exception e) {
360                 throw processException(e);
361             }
362             finally {
363                 if (announcementsDelivery != null) {
364                     cacheResult(announcementsDelivery);
365                 }
366 
367                 closeSession(session);
368             }
369         }
370 
371         return announcementsDelivery;
372     }
373 
374     public List<AnnouncementsDelivery> findByUserId(long userId)
375         throws SystemException {
376         return findByUserId(userId, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
377     }
378 
379     public List<AnnouncementsDelivery> findByUserId(long userId, int start,
380         int end) throws SystemException {
381         return findByUserId(userId, start, end, null);
382     }
383 
384     public List<AnnouncementsDelivery> findByUserId(long userId, int start,
385         int end, OrderByComparator orderByComparator) throws SystemException {
386         Object[] finderArgs = new Object[] {
387                 userId,
388                 
389                 String.valueOf(start), String.valueOf(end),
390                 String.valueOf(orderByComparator)
391             };
392 
393         List<AnnouncementsDelivery> list = (List<AnnouncementsDelivery>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_USERID,
394                 finderArgs, this);
395 
396         if (list == null) {
397             StringBundler query = null;
398 
399             if (orderByComparator != null) {
400                 query = new StringBundler(3 +
401                         (orderByComparator.getOrderByFields().length * 3));
402             }
403             else {
404                 query = new StringBundler(2);
405             }
406 
407             query.append(_SQL_SELECT_ANNOUNCEMENTSDELIVERY_WHERE);
408 
409             query.append(_FINDER_COLUMN_USERID_USERID_2);
410 
411             if (orderByComparator != null) {
412                 appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
413                     orderByComparator);
414             }
415 
416             String sql = query.toString();
417 
418             Session session = null;
419 
420             try {
421                 session = openSession();
422 
423                 Query q = session.createQuery(sql);
424 
425                 QueryPos qPos = QueryPos.getInstance(q);
426 
427                 qPos.add(userId);
428 
429                 list = (List<AnnouncementsDelivery>)QueryUtil.list(q,
430                         getDialect(), start, end);
431             }
432             catch (Exception e) {
433                 throw processException(e);
434             }
435             finally {
436                 if (list == null) {
437                     list = new ArrayList<AnnouncementsDelivery>();
438                 }
439 
440                 cacheResult(list);
441 
442                 FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_USERID,
443                     finderArgs, list);
444 
445                 closeSession(session);
446             }
447         }
448 
449         return list;
450     }
451 
452     public AnnouncementsDelivery findByUserId_First(long userId,
453         OrderByComparator orderByComparator)
454         throws NoSuchDeliveryException, SystemException {
455         List<AnnouncementsDelivery> list = findByUserId(userId, 0, 1,
456                 orderByComparator);
457 
458         if (list.isEmpty()) {
459             StringBundler msg = new StringBundler(4);
460 
461             msg.append(_NO_SUCH_ENTITY_WITH_KEY);
462 
463             msg.append("userId=");
464             msg.append(userId);
465 
466             msg.append(StringPool.CLOSE_CURLY_BRACE);
467 
468             throw new NoSuchDeliveryException(msg.toString());
469         }
470         else {
471             return list.get(0);
472         }
473     }
474 
475     public AnnouncementsDelivery findByUserId_Last(long userId,
476         OrderByComparator orderByComparator)
477         throws NoSuchDeliveryException, SystemException {
478         int count = countByUserId(userId);
479 
480         List<AnnouncementsDelivery> list = findByUserId(userId, count - 1,
481                 count, orderByComparator);
482 
483         if (list.isEmpty()) {
484             StringBundler msg = new StringBundler(4);
485 
486             msg.append(_NO_SUCH_ENTITY_WITH_KEY);
487 
488             msg.append("userId=");
489             msg.append(userId);
490 
491             msg.append(StringPool.CLOSE_CURLY_BRACE);
492 
493             throw new NoSuchDeliveryException(msg.toString());
494         }
495         else {
496             return list.get(0);
497         }
498     }
499 
500     public AnnouncementsDelivery[] findByUserId_PrevAndNext(long deliveryId,
501         long userId, OrderByComparator orderByComparator)
502         throws NoSuchDeliveryException, SystemException {
503         AnnouncementsDelivery announcementsDelivery = findByPrimaryKey(deliveryId);
504 
505         Session session = null;
506 
507         try {
508             session = openSession();
509 
510             AnnouncementsDelivery[] array = new AnnouncementsDeliveryImpl[3];
511 
512             array[0] = getByUserId_PrevAndNext(session, announcementsDelivery,
513                     userId, orderByComparator, true);
514 
515             array[1] = announcementsDelivery;
516 
517             array[2] = getByUserId_PrevAndNext(session, announcementsDelivery,
518                     userId, orderByComparator, false);
519 
520             return array;
521         }
522         catch (Exception e) {
523             throw processException(e);
524         }
525         finally {
526             closeSession(session);
527         }
528     }
529 
530     protected AnnouncementsDelivery getByUserId_PrevAndNext(Session session,
531         AnnouncementsDelivery announcementsDelivery, long userId,
532         OrderByComparator orderByComparator, boolean previous) {
533         StringBundler query = null;
534 
535         if (orderByComparator != null) {
536             query = new StringBundler(6 +
537                     (orderByComparator.getOrderByFields().length * 6));
538         }
539         else {
540             query = new StringBundler(3);
541         }
542 
543         query.append(_SQL_SELECT_ANNOUNCEMENTSDELIVERY_WHERE);
544 
545         query.append(_FINDER_COLUMN_USERID_USERID_2);
546 
547         if (orderByComparator != null) {
548             String[] orderByFields = orderByComparator.getOrderByFields();
549 
550             if (orderByFields.length > 0) {
551                 query.append(WHERE_AND);
552             }
553 
554             for (int i = 0; i < orderByFields.length; i++) {
555                 query.append(_ORDER_BY_ENTITY_ALIAS);
556                 query.append(orderByFields[i]);
557 
558                 if ((i + 1) < orderByFields.length) {
559                     if (orderByComparator.isAscending() ^ previous) {
560                         query.append(WHERE_GREATER_THAN_HAS_NEXT);
561                     }
562                     else {
563                         query.append(WHERE_LESSER_THAN_HAS_NEXT);
564                     }
565                 }
566                 else {
567                     if (orderByComparator.isAscending() ^ previous) {
568                         query.append(WHERE_GREATER_THAN);
569                     }
570                     else {
571                         query.append(WHERE_LESSER_THAN);
572                     }
573                 }
574             }
575 
576             query.append(ORDER_BY_CLAUSE);
577 
578             for (int i = 0; i < orderByFields.length; i++) {
579                 query.append(_ORDER_BY_ENTITY_ALIAS);
580                 query.append(orderByFields[i]);
581 
582                 if ((i + 1) < orderByFields.length) {
583                     if (orderByComparator.isAscending() ^ previous) {
584                         query.append(ORDER_BY_ASC_HAS_NEXT);
585                     }
586                     else {
587                         query.append(ORDER_BY_DESC_HAS_NEXT);
588                     }
589                 }
590                 else {
591                     if (orderByComparator.isAscending() ^ previous) {
592                         query.append(ORDER_BY_ASC);
593                     }
594                     else {
595                         query.append(ORDER_BY_DESC);
596                     }
597                 }
598             }
599         }
600 
601         String sql = query.toString();
602 
603         Query q = session.createQuery(sql);
604 
605         q.setFirstResult(0);
606         q.setMaxResults(2);
607 
608         QueryPos qPos = QueryPos.getInstance(q);
609 
610         qPos.add(userId);
611 
612         if (orderByComparator != null) {
613             Object[] values = orderByComparator.getOrderByValues(announcementsDelivery);
614 
615             for (Object value : values) {
616                 qPos.add(value);
617             }
618         }
619 
620         List<AnnouncementsDelivery> list = q.list();
621 
622         if (list.size() == 2) {
623             return list.get(1);
624         }
625         else {
626             return null;
627         }
628     }
629 
630     public AnnouncementsDelivery findByU_T(long userId, String type)
631         throws NoSuchDeliveryException, SystemException {
632         AnnouncementsDelivery announcementsDelivery = fetchByU_T(userId, type);
633 
634         if (announcementsDelivery == null) {
635             StringBundler msg = new StringBundler(6);
636 
637             msg.append(_NO_SUCH_ENTITY_WITH_KEY);
638 
639             msg.append("userId=");
640             msg.append(userId);
641 
642             msg.append(", type=");
643             msg.append(type);
644 
645             msg.append(StringPool.CLOSE_CURLY_BRACE);
646 
647             if (_log.isWarnEnabled()) {
648                 _log.warn(msg.toString());
649             }
650 
651             throw new NoSuchDeliveryException(msg.toString());
652         }
653 
654         return announcementsDelivery;
655     }
656 
657     public AnnouncementsDelivery fetchByU_T(long userId, String type)
658         throws SystemException {
659         return fetchByU_T(userId, type, true);
660     }
661 
662     public AnnouncementsDelivery fetchByU_T(long userId, String type,
663         boolean retrieveFromCache) throws SystemException {
664         Object[] finderArgs = new Object[] { userId, type };
665 
666         Object result = null;
667 
668         if (retrieveFromCache) {
669             result = FinderCacheUtil.getResult(FINDER_PATH_FETCH_BY_U_T,
670                     finderArgs, this);
671         }
672 
673         if (result == null) {
674             StringBundler query = new StringBundler(3);
675 
676             query.append(_SQL_SELECT_ANNOUNCEMENTSDELIVERY_WHERE);
677 
678             query.append(_FINDER_COLUMN_U_T_USERID_2);
679 
680             if (type == null) {
681                 query.append(_FINDER_COLUMN_U_T_TYPE_1);
682             }
683             else {
684                 if (type.equals(StringPool.BLANK)) {
685                     query.append(_FINDER_COLUMN_U_T_TYPE_3);
686                 }
687                 else {
688                     query.append(_FINDER_COLUMN_U_T_TYPE_2);
689                 }
690             }
691 
692             String sql = query.toString();
693 
694             Session session = null;
695 
696             try {
697                 session = openSession();
698 
699                 Query q = session.createQuery(sql);
700 
701                 QueryPos qPos = QueryPos.getInstance(q);
702 
703                 qPos.add(userId);
704 
705                 if (type != null) {
706                     qPos.add(type);
707                 }
708 
709                 List<AnnouncementsDelivery> list = q.list();
710 
711                 result = list;
712 
713                 AnnouncementsDelivery announcementsDelivery = null;
714 
715                 if (list.isEmpty()) {
716                     FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_U_T,
717                         finderArgs, list);
718                 }
719                 else {
720                     announcementsDelivery = list.get(0);
721 
722                     cacheResult(announcementsDelivery);
723 
724                     if ((announcementsDelivery.getUserId() != userId) ||
725                             (announcementsDelivery.getType() == null) ||
726                             !announcementsDelivery.getType().equals(type)) {
727                         FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_U_T,
728                             finderArgs, announcementsDelivery);
729                     }
730                 }
731 
732                 return announcementsDelivery;
733             }
734             catch (Exception e) {
735                 throw processException(e);
736             }
737             finally {
738                 if (result == null) {
739                     FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_U_T,
740                         finderArgs, new ArrayList<AnnouncementsDelivery>());
741                 }
742 
743                 closeSession(session);
744             }
745         }
746         else {
747             if (result instanceof List<?>) {
748                 return null;
749             }
750             else {
751                 return (AnnouncementsDelivery)result;
752             }
753         }
754     }
755 
756     public List<AnnouncementsDelivery> findAll() throws SystemException {
757         return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
758     }
759 
760     public List<AnnouncementsDelivery> findAll(int start, int end)
761         throws SystemException {
762         return findAll(start, end, null);
763     }
764 
765     public List<AnnouncementsDelivery> findAll(int start, int end,
766         OrderByComparator orderByComparator) throws SystemException {
767         Object[] finderArgs = new Object[] {
768                 String.valueOf(start), String.valueOf(end),
769                 String.valueOf(orderByComparator)
770             };
771 
772         List<AnnouncementsDelivery> list = (List<AnnouncementsDelivery>)FinderCacheUtil.getResult(FINDER_PATH_FIND_ALL,
773                 finderArgs, this);
774 
775         if (list == null) {
776             StringBundler query = null;
777             String sql = null;
778 
779             if (orderByComparator != null) {
780                 query = new StringBundler(2 +
781                         (orderByComparator.getOrderByFields().length * 3));
782 
783                 query.append(_SQL_SELECT_ANNOUNCEMENTSDELIVERY);
784 
785                 appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
786                     orderByComparator);
787 
788                 sql = query.toString();
789             }
790             else {
791                 sql = _SQL_SELECT_ANNOUNCEMENTSDELIVERY;
792             }
793 
794             Session session = null;
795 
796             try {
797                 session = openSession();
798 
799                 Query q = session.createQuery(sql);
800 
801                 if (orderByComparator == null) {
802                     list = (List<AnnouncementsDelivery>)QueryUtil.list(q,
803                             getDialect(), start, end, false);
804 
805                     Collections.sort(list);
806                 }
807                 else {
808                     list = (List<AnnouncementsDelivery>)QueryUtil.list(q,
809                             getDialect(), start, end);
810                 }
811             }
812             catch (Exception e) {
813                 throw processException(e);
814             }
815             finally {
816                 if (list == null) {
817                     list = new ArrayList<AnnouncementsDelivery>();
818                 }
819 
820                 cacheResult(list);
821 
822                 FinderCacheUtil.putResult(FINDER_PATH_FIND_ALL, finderArgs, list);
823 
824                 closeSession(session);
825             }
826         }
827 
828         return list;
829     }
830 
831     public void removeByUserId(long userId) throws SystemException {
832         for (AnnouncementsDelivery announcementsDelivery : findByUserId(userId)) {
833             remove(announcementsDelivery);
834         }
835     }
836 
837     public void removeByU_T(long userId, String type)
838         throws NoSuchDeliveryException, SystemException {
839         AnnouncementsDelivery announcementsDelivery = findByU_T(userId, type);
840 
841         remove(announcementsDelivery);
842     }
843 
844     public void removeAll() throws SystemException {
845         for (AnnouncementsDelivery announcementsDelivery : findAll()) {
846             remove(announcementsDelivery);
847         }
848     }
849 
850     public int countByUserId(long userId) throws SystemException {
851         Object[] finderArgs = new Object[] { userId };
852 
853         Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_USERID,
854                 finderArgs, this);
855 
856         if (count == null) {
857             StringBundler query = new StringBundler(2);
858 
859             query.append(_SQL_COUNT_ANNOUNCEMENTSDELIVERY_WHERE);
860 
861             query.append(_FINDER_COLUMN_USERID_USERID_2);
862 
863             String sql = query.toString();
864 
865             Session session = null;
866 
867             try {
868                 session = openSession();
869 
870                 Query q = session.createQuery(sql);
871 
872                 QueryPos qPos = QueryPos.getInstance(q);
873 
874                 qPos.add(userId);
875 
876                 count = (Long)q.uniqueResult();
877             }
878             catch (Exception e) {
879                 throw processException(e);
880             }
881             finally {
882                 if (count == null) {
883                     count = Long.valueOf(0);
884                 }
885 
886                 FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_USERID,
887                     finderArgs, count);
888 
889                 closeSession(session);
890             }
891         }
892 
893         return count.intValue();
894     }
895 
896     public int countByU_T(long userId, String type) throws SystemException {
897         Object[] finderArgs = new Object[] { userId, type };
898 
899         Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_U_T,
900                 finderArgs, this);
901 
902         if (count == null) {
903             StringBundler query = new StringBundler(3);
904 
905             query.append(_SQL_COUNT_ANNOUNCEMENTSDELIVERY_WHERE);
906 
907             query.append(_FINDER_COLUMN_U_T_USERID_2);
908 
909             if (type == null) {
910                 query.append(_FINDER_COLUMN_U_T_TYPE_1);
911             }
912             else {
913                 if (type.equals(StringPool.BLANK)) {
914                     query.append(_FINDER_COLUMN_U_T_TYPE_3);
915                 }
916                 else {
917                     query.append(_FINDER_COLUMN_U_T_TYPE_2);
918                 }
919             }
920 
921             String sql = query.toString();
922 
923             Session session = null;
924 
925             try {
926                 session = openSession();
927 
928                 Query q = session.createQuery(sql);
929 
930                 QueryPos qPos = QueryPos.getInstance(q);
931 
932                 qPos.add(userId);
933 
934                 if (type != null) {
935                     qPos.add(type);
936                 }
937 
938                 count = (Long)q.uniqueResult();
939             }
940             catch (Exception e) {
941                 throw processException(e);
942             }
943             finally {
944                 if (count == null) {
945                     count = Long.valueOf(0);
946                 }
947 
948                 FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_U_T, finderArgs,
949                     count);
950 
951                 closeSession(session);
952             }
953         }
954 
955         return count.intValue();
956     }
957 
958     public int countAll() throws SystemException {
959         Object[] finderArgs = new Object[0];
960 
961         Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
962                 finderArgs, this);
963 
964         if (count == null) {
965             Session session = null;
966 
967             try {
968                 session = openSession();
969 
970                 Query q = session.createQuery(_SQL_COUNT_ANNOUNCEMENTSDELIVERY);
971 
972                 count = (Long)q.uniqueResult();
973             }
974             catch (Exception e) {
975                 throw processException(e);
976             }
977             finally {
978                 if (count == null) {
979                     count = Long.valueOf(0);
980                 }
981 
982                 FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL, finderArgs,
983                     count);
984 
985                 closeSession(session);
986             }
987         }
988 
989         return count.intValue();
990     }
991 
992     public void afterPropertiesSet() {
993         String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
994                     com.liferay.portal.util.PropsUtil.get(
995                         "value.object.listener.com.liferay.portlet.announcements.model.AnnouncementsDelivery")));
996 
997         if (listenerClassNames.length > 0) {
998             try {
999                 List<ModelListener<AnnouncementsDelivery>> listenersList = new ArrayList<ModelListener<AnnouncementsDelivery>>();
1000
1001                for (String listenerClassName : listenerClassNames) {
1002                    listenersList.add((ModelListener<AnnouncementsDelivery>)InstanceFactory.newInstance(
1003                            listenerClassName));
1004                }
1005
1006                listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
1007            }
1008            catch (Exception e) {
1009                _log.error(e);
1010            }
1011        }
1012    }
1013
1014    public void destroy() {
1015        EntityCacheUtil.removeCache(AnnouncementsDeliveryImpl.class.getName());
1016        FinderCacheUtil.removeCache(FINDER_CLASS_NAME_ENTITY);
1017        FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST);
1018    }
1019
1020    @BeanReference(type = AnnouncementsDeliveryPersistence.class)
1021    protected AnnouncementsDeliveryPersistence announcementsDeliveryPersistence;
1022    @BeanReference(type = AnnouncementsEntryPersistence.class)
1023    protected AnnouncementsEntryPersistence announcementsEntryPersistence;
1024    @BeanReference(type = AnnouncementsFlagPersistence.class)
1025    protected AnnouncementsFlagPersistence announcementsFlagPersistence;
1026    @BeanReference(type = ResourcePersistence.class)
1027    protected ResourcePersistence resourcePersistence;
1028    @BeanReference(type = UserPersistence.class)
1029    protected UserPersistence userPersistence;
1030    private static final String _SQL_SELECT_ANNOUNCEMENTSDELIVERY = "SELECT announcementsDelivery FROM AnnouncementsDelivery announcementsDelivery";
1031    private static final String _SQL_SELECT_ANNOUNCEMENTSDELIVERY_WHERE = "SELECT announcementsDelivery FROM AnnouncementsDelivery announcementsDelivery WHERE ";
1032    private static final String _SQL_COUNT_ANNOUNCEMENTSDELIVERY = "SELECT COUNT(announcementsDelivery) FROM AnnouncementsDelivery announcementsDelivery";
1033    private static final String _SQL_COUNT_ANNOUNCEMENTSDELIVERY_WHERE = "SELECT COUNT(announcementsDelivery) FROM AnnouncementsDelivery announcementsDelivery WHERE ";
1034    private static final String _FINDER_COLUMN_USERID_USERID_2 = "announcementsDelivery.userId = ?";
1035    private static final String _FINDER_COLUMN_U_T_USERID_2 = "announcementsDelivery.userId = ? AND ";
1036    private static final String _FINDER_COLUMN_U_T_TYPE_1 = "announcementsDelivery.type IS NULL";
1037    private static final String _FINDER_COLUMN_U_T_TYPE_2 = "announcementsDelivery.type = ?";
1038    private static final String _FINDER_COLUMN_U_T_TYPE_3 = "(announcementsDelivery.type IS NULL OR announcementsDelivery.type = ?)";
1039    private static final String _ORDER_BY_ENTITY_ALIAS = "announcementsDelivery.";
1040    private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No AnnouncementsDelivery exists with the primary key ";
1041    private static final String _NO_SUCH_ENTITY_WITH_KEY = "No AnnouncementsDelivery exists with the key {";
1042    private static Log _log = LogFactoryUtil.getLog(AnnouncementsDeliveryPersistenceImpl.class);
1043}