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