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