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