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                     "FROM com.liferay.portlet.announcements.model.AnnouncementsDelivery WHERE ");
410 
411                 query.append("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                     "FROM com.liferay.portlet.announcements.model.AnnouncementsDelivery WHERE ");
469 
470                 query.append("userId = ?");
471 
472                 query.append(" ");
473 
474                 if (obc != null) {
475                     query.append("ORDER BY ");
476                     query.append(obc.getOrderBy());
477                 }
478 
479                 Query q = session.createQuery(query.toString());
480 
481                 QueryPos qPos = QueryPos.getInstance(q);
482 
483                 qPos.add(userId);
484 
485                 list = (List<AnnouncementsDelivery>)QueryUtil.list(q,
486                         getDialect(), start, end);
487             }
488             catch (Exception e) {
489                 throw processException(e);
490             }
491             finally {
492                 if (list == null) {
493                     list = new ArrayList<AnnouncementsDelivery>();
494                 }
495 
496                 cacheResult(list);
497 
498                 FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_OBC_USERID,
499                     finderArgs, list);
500 
501                 closeSession(session);
502             }
503         }
504 
505         return list;
506     }
507 
508     public AnnouncementsDelivery findByUserId_First(long userId,
509         OrderByComparator obc) throws NoSuchDeliveryException, SystemException {
510         List<AnnouncementsDelivery> list = findByUserId(userId, 0, 1, obc);
511 
512         if (list.isEmpty()) {
513             StringBuilder msg = new StringBuilder();
514 
515             msg.append("No AnnouncementsDelivery exists with the key {");
516 
517             msg.append("userId=" + userId);
518 
519             msg.append(StringPool.CLOSE_CURLY_BRACE);
520 
521             throw new NoSuchDeliveryException(msg.toString());
522         }
523         else {
524             return list.get(0);
525         }
526     }
527 
528     public AnnouncementsDelivery findByUserId_Last(long userId,
529         OrderByComparator obc) throws NoSuchDeliveryException, SystemException {
530         int count = countByUserId(userId);
531 
532         List<AnnouncementsDelivery> list = findByUserId(userId, count - 1,
533                 count, obc);
534 
535         if (list.isEmpty()) {
536             StringBuilder msg = new StringBuilder();
537 
538             msg.append("No AnnouncementsDelivery exists with the key {");
539 
540             msg.append("userId=" + userId);
541 
542             msg.append(StringPool.CLOSE_CURLY_BRACE);
543 
544             throw new NoSuchDeliveryException(msg.toString());
545         }
546         else {
547             return list.get(0);
548         }
549     }
550 
551     public AnnouncementsDelivery[] findByUserId_PrevAndNext(long deliveryId,
552         long userId, OrderByComparator obc)
553         throws NoSuchDeliveryException, SystemException {
554         AnnouncementsDelivery announcementsDelivery = findByPrimaryKey(deliveryId);
555 
556         int count = countByUserId(userId);
557 
558         Session session = null;
559 
560         try {
561             session = openSession();
562 
563             StringBuilder query = new StringBuilder();
564 
565             query.append(
566                 "FROM com.liferay.portlet.announcements.model.AnnouncementsDelivery WHERE ");
567 
568             query.append("userId = ?");
569 
570             query.append(" ");
571 
572             if (obc != null) {
573                 query.append("ORDER BY ");
574                 query.append(obc.getOrderBy());
575             }
576 
577             Query q = session.createQuery(query.toString());
578 
579             QueryPos qPos = QueryPos.getInstance(q);
580 
581             qPos.add(userId);
582 
583             Object[] objArray = QueryUtil.getPrevAndNext(q, count, obc,
584                     announcementsDelivery);
585 
586             AnnouncementsDelivery[] array = new AnnouncementsDeliveryImpl[3];
587 
588             array[0] = (AnnouncementsDelivery)objArray[0];
589             array[1] = (AnnouncementsDelivery)objArray[1];
590             array[2] = (AnnouncementsDelivery)objArray[2];
591 
592             return array;
593         }
594         catch (Exception e) {
595             throw processException(e);
596         }
597         finally {
598             closeSession(session);
599         }
600     }
601 
602     public AnnouncementsDelivery findByU_T(long userId, String type)
603         throws NoSuchDeliveryException, SystemException {
604         AnnouncementsDelivery announcementsDelivery = fetchByU_T(userId, type);
605 
606         if (announcementsDelivery == null) {
607             StringBuilder msg = new StringBuilder();
608 
609             msg.append("No AnnouncementsDelivery exists with the key {");
610 
611             msg.append("userId=" + userId);
612 
613             msg.append(", ");
614             msg.append("type=" + type);
615 
616             msg.append(StringPool.CLOSE_CURLY_BRACE);
617 
618             if (_log.isWarnEnabled()) {
619                 _log.warn(msg.toString());
620             }
621 
622             throw new NoSuchDeliveryException(msg.toString());
623         }
624 
625         return announcementsDelivery;
626     }
627 
628     public AnnouncementsDelivery fetchByU_T(long userId, String type)
629         throws SystemException {
630         return fetchByU_T(userId, type, true);
631     }
632 
633     public AnnouncementsDelivery fetchByU_T(long userId, String type,
634         boolean retrieveFromCache) throws SystemException {
635         Object[] finderArgs = new Object[] { new Long(userId), type };
636 
637         Object result = null;
638 
639         if (retrieveFromCache) {
640             result = FinderCacheUtil.getResult(FINDER_PATH_FETCH_BY_U_T,
641                     finderArgs, this);
642         }
643 
644         if (result == null) {
645             Session session = null;
646 
647             try {
648                 session = openSession();
649 
650                 StringBuilder query = new StringBuilder();
651 
652                 query.append(
653                     "FROM com.liferay.portlet.announcements.model.AnnouncementsDelivery WHERE ");
654 
655                 query.append("userId = ?");
656 
657                 query.append(" AND ");
658 
659                 if (type == null) {
660                     query.append("type_ IS NULL");
661                 }
662                 else {
663                     query.append("type_ = ?");
664                 }
665 
666                 query.append(" ");
667 
668                 Query q = session.createQuery(query.toString());
669 
670                 QueryPos qPos = QueryPos.getInstance(q);
671 
672                 qPos.add(userId);
673 
674                 if (type != null) {
675                     qPos.add(type);
676                 }
677 
678                 List<AnnouncementsDelivery> list = q.list();
679 
680                 result = list;
681 
682                 AnnouncementsDelivery announcementsDelivery = null;
683 
684                 if (list.isEmpty()) {
685                     FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_U_T,
686                         finderArgs, list);
687                 }
688                 else {
689                     announcementsDelivery = list.get(0);
690 
691                     cacheResult(announcementsDelivery);
692 
693                     if ((announcementsDelivery.getUserId() != userId) ||
694                             (announcementsDelivery.getType() == null) ||
695                             !announcementsDelivery.getType().equals(type)) {
696                         FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_U_T,
697                             finderArgs, announcementsDelivery);
698                     }
699                 }
700 
701                 return announcementsDelivery;
702             }
703             catch (Exception e) {
704                 throw processException(e);
705             }
706             finally {
707                 if (result == null) {
708                     FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_U_T,
709                         finderArgs, new ArrayList<AnnouncementsDelivery>());
710                 }
711 
712                 closeSession(session);
713             }
714         }
715         else {
716             if (result instanceof List) {
717                 return null;
718             }
719             else {
720                 return (AnnouncementsDelivery)result;
721             }
722         }
723     }
724 
725     public List<Object> findWithDynamicQuery(DynamicQuery dynamicQuery)
726         throws SystemException {
727         Session session = null;
728 
729         try {
730             session = openSession();
731 
732             dynamicQuery.compile(session);
733 
734             return dynamicQuery.list();
735         }
736         catch (Exception e) {
737             throw processException(e);
738         }
739         finally {
740             closeSession(session);
741         }
742     }
743 
744     public List<Object> findWithDynamicQuery(DynamicQuery dynamicQuery,
745         int start, int end) throws SystemException {
746         Session session = null;
747 
748         try {
749             session = openSession();
750 
751             dynamicQuery.setLimit(start, end);
752 
753             dynamicQuery.compile(session);
754 
755             return dynamicQuery.list();
756         }
757         catch (Exception e) {
758             throw processException(e);
759         }
760         finally {
761             closeSession(session);
762         }
763     }
764 
765     public List<AnnouncementsDelivery> findAll() throws SystemException {
766         return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
767     }
768 
769     public List<AnnouncementsDelivery> findAll(int start, int end)
770         throws SystemException {
771         return findAll(start, end, null);
772     }
773 
774     public List<AnnouncementsDelivery> findAll(int start, int end,
775         OrderByComparator obc) throws SystemException {
776         Object[] finderArgs = new Object[] {
777                 String.valueOf(start), String.valueOf(end), String.valueOf(obc)
778             };
779 
780         List<AnnouncementsDelivery> list = (List<AnnouncementsDelivery>)FinderCacheUtil.getResult(FINDER_PATH_FIND_ALL,
781                 finderArgs, this);
782 
783         if (list == null) {
784             Session session = null;
785 
786             try {
787                 session = openSession();
788 
789                 StringBuilder query = new StringBuilder();
790 
791                 query.append(
792                     "FROM com.liferay.portlet.announcements.model.AnnouncementsDelivery ");
793 
794                 if (obc != null) {
795                     query.append("ORDER BY ");
796                     query.append(obc.getOrderBy());
797                 }
798 
799                 Query q = session.createQuery(query.toString());
800 
801                 if (obc == 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[] { new Long(userId) };
852 
853         Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_USERID,
854                 finderArgs, this);
855 
856         if (count == null) {
857             Session session = null;
858 
859             try {
860                 session = openSession();
861 
862                 StringBuilder query = new StringBuilder();
863 
864                 query.append("SELECT COUNT(*) ");
865                 query.append(
866                     "FROM com.liferay.portlet.announcements.model.AnnouncementsDelivery WHERE ");
867 
868                 query.append("userId = ?");
869 
870                 query.append(" ");
871 
872                 Query q = session.createQuery(query.toString());
873 
874                 QueryPos qPos = QueryPos.getInstance(q);
875 
876                 qPos.add(userId);
877 
878                 count = (Long)q.uniqueResult();
879             }
880             catch (Exception e) {
881                 throw processException(e);
882             }
883             finally {
884                 if (count == null) {
885                     count = Long.valueOf(0);
886                 }
887 
888                 FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_USERID,
889                     finderArgs, count);
890 
891                 closeSession(session);
892             }
893         }
894 
895         return count.intValue();
896     }
897 
898     public int countByU_T(long userId, String type) throws SystemException {
899         Object[] finderArgs = new Object[] { new Long(userId), type };
900 
901         Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_U_T,
902                 finderArgs, this);
903 
904         if (count == null) {
905             Session session = null;
906 
907             try {
908                 session = openSession();
909 
910                 StringBuilder query = new StringBuilder();
911 
912                 query.append("SELECT COUNT(*) ");
913                 query.append(
914                     "FROM com.liferay.portlet.announcements.model.AnnouncementsDelivery WHERE ");
915 
916                 query.append("userId = ?");
917 
918                 query.append(" AND ");
919 
920                 if (type == null) {
921                     query.append("type_ IS NULL");
922                 }
923                 else {
924                     query.append("type_ = ?");
925                 }
926 
927                 query.append(" ");
928 
929                 Query q = session.createQuery(query.toString());
930 
931                 QueryPos qPos = QueryPos.getInstance(q);
932 
933                 qPos.add(userId);
934 
935                 if (type != null) {
936                     qPos.add(type);
937                 }
938 
939                 count = (Long)q.uniqueResult();
940             }
941             catch (Exception e) {
942                 throw processException(e);
943             }
944             finally {
945                 if (count == null) {
946                     count = Long.valueOf(0);
947                 }
948 
949                 FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_U_T, finderArgs,
950                     count);
951 
952                 closeSession(session);
953             }
954         }
955 
956         return count.intValue();
957     }
958 
959     public int countAll() throws SystemException {
960         Object[] finderArgs = new Object[0];
961 
962         Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
963                 finderArgs, this);
964 
965         if (count == null) {
966             Session session = null;
967 
968             try {
969                 session = openSession();
970 
971                 Query q = session.createQuery(
972                         "SELECT COUNT(*) FROM com.liferay.portlet.announcements.model.AnnouncementsDelivery");
973 
974                 count = (Long)q.uniqueResult();
975             }
976             catch (Exception e) {
977                 throw processException(e);
978             }
979             finally {
980                 if (count == null) {
981                     count = Long.valueOf(0);
982                 }
983 
984                 FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL, finderArgs,
985                     count);
986 
987                 closeSession(session);
988             }
989         }
990 
991         return count.intValue();
992     }
993 
994     public void afterPropertiesSet() {
995         String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
996                     com.liferay.portal.util.PropsUtil.get(
997                         "value.object.listener.com.liferay.portlet.announcements.model.AnnouncementsDelivery")));
998 
999         if (listenerClassNames.length > 0) {
1000            try {
1001                List<ModelListener<AnnouncementsDelivery>> listenersList = new ArrayList<ModelListener<AnnouncementsDelivery>>();
1002
1003                for (String listenerClassName : listenerClassNames) {
1004                    listenersList.add((ModelListener<AnnouncementsDelivery>)Class.forName(
1005                            listenerClassName).newInstance());
1006                }
1007
1008                listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
1009            }
1010            catch (Exception e) {
1011                _log.error(e);
1012            }
1013        }
1014    }
1015
1016    @BeanReference(name = "com.liferay.portlet.announcements.service.persistence.AnnouncementsDeliveryPersistence.impl")
1017    protected com.liferay.portlet.announcements.service.persistence.AnnouncementsDeliveryPersistence announcementsDeliveryPersistence;
1018    @BeanReference(name = "com.liferay.portlet.announcements.service.persistence.AnnouncementsEntryPersistence.impl")
1019    protected com.liferay.portlet.announcements.service.persistence.AnnouncementsEntryPersistence announcementsEntryPersistence;
1020    @BeanReference(name = "com.liferay.portlet.announcements.service.persistence.AnnouncementsFlagPersistence.impl")
1021    protected com.liferay.portlet.announcements.service.persistence.AnnouncementsFlagPersistence announcementsFlagPersistence;
1022    @BeanReference(name = "com.liferay.portal.service.persistence.UserPersistence.impl")
1023    protected com.liferay.portal.service.persistence.UserPersistence userPersistence;
1024    private static Log _log = LogFactoryUtil.getLog(AnnouncementsDeliveryPersistenceImpl.class);
1025}