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.dao.orm.DynamicQuery;
27  import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
28  import com.liferay.portal.kernel.dao.orm.Query;
29  import com.liferay.portal.kernel.dao.orm.QueryPos;
30  import com.liferay.portal.kernel.dao.orm.QueryUtil;
31  import com.liferay.portal.kernel.dao.orm.Session;
32  import com.liferay.portal.kernel.util.GetterUtil;
33  import com.liferay.portal.kernel.util.ListUtil;
34  import com.liferay.portal.kernel.util.OrderByComparator;
35  import com.liferay.portal.kernel.util.StringPool;
36  import com.liferay.portal.kernel.util.StringUtil;
37  import com.liferay.portal.model.ModelListener;
38  import com.liferay.portal.service.persistence.BatchSessionUtil;
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 {
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             if (BatchSessionUtil.isEnabled()) {
133                 Object staleObject = session.get(AnnouncementsDeliveryImpl.class,
134                         announcementsDelivery.getPrimaryKeyObj());
135 
136                 if (staleObject != null) {
137                     session.evict(staleObject);
138                 }
139             }
140 
141             session.delete(announcementsDelivery);
142 
143             session.flush();
144 
145             return announcementsDelivery;
146         }
147         catch (Exception e) {
148             throw processException(e);
149         }
150         finally {
151             closeSession(session);
152 
153             FinderCacheUtil.clearCache(AnnouncementsDelivery.class.getName());
154         }
155     }
156 
157     /**
158      * @deprecated Use <code>update(AnnouncementsDelivery announcementsDelivery, boolean merge)</code>.
159      */
160     public AnnouncementsDelivery update(
161         AnnouncementsDelivery announcementsDelivery) throws SystemException {
162         if (_log.isWarnEnabled()) {
163             _log.warn(
164                 "Using the deprecated update(AnnouncementsDelivery announcementsDelivery) method. Use update(AnnouncementsDelivery announcementsDelivery, boolean merge) instead.");
165         }
166 
167         return update(announcementsDelivery, false);
168     }
169 
170     /**
171      * Add, update, or merge, the entity. This method also calls the model
172      * listeners to trigger the proper events associated with adding, deleting,
173      * or updating an entity.
174      *
175      * @param        announcementsDelivery the entity to add, update, or merge
176      * @param        merge boolean value for whether to merge the entity. The
177      *                default value is false. Setting merge to true is more
178      *                expensive and should only be true when announcementsDelivery is
179      *                transient. See LEP-5473 for a detailed discussion of this
180      *                method.
181      * @return        true if the portlet can be displayed via Ajax
182      */
183     public AnnouncementsDelivery update(
184         AnnouncementsDelivery announcementsDelivery, boolean merge)
185         throws SystemException {
186         boolean isNew = announcementsDelivery.isNew();
187 
188         if (_listeners.length > 0) {
189             for (ModelListener listener : _listeners) {
190                 if (isNew) {
191                     listener.onBeforeCreate(announcementsDelivery);
192                 }
193                 else {
194                     listener.onBeforeUpdate(announcementsDelivery);
195                 }
196             }
197         }
198 
199         announcementsDelivery = updateImpl(announcementsDelivery, merge);
200 
201         if (_listeners.length > 0) {
202             for (ModelListener listener : _listeners) {
203                 if (isNew) {
204                     listener.onAfterCreate(announcementsDelivery);
205                 }
206                 else {
207                     listener.onAfterUpdate(announcementsDelivery);
208                 }
209             }
210         }
211 
212         return announcementsDelivery;
213     }
214 
215     public AnnouncementsDelivery updateImpl(
216         com.liferay.portlet.announcements.model.AnnouncementsDelivery announcementsDelivery,
217         boolean merge) throws SystemException {
218         Session session = null;
219 
220         try {
221             session = openSession();
222 
223             BatchSessionUtil.update(session, announcementsDelivery, merge);
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 = null;
699 
700                 if (obc == null) {
701                     list = (List<AnnouncementsDelivery>)QueryUtil.list(q,
702                             getDialect(), start, end, false);
703 
704                     Collections.sort(list);
705                 }
706                 else {
707                     list = (List<AnnouncementsDelivery>)QueryUtil.list(q,
708                             getDialect(), start, end);
709                 }
710 
711                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
712                     finderClassName, finderMethodName, finderParams,
713                     finderArgs, list);
714 
715                 return list;
716             }
717             catch (Exception e) {
718                 throw processException(e);
719             }
720             finally {
721                 closeSession(session);
722             }
723         }
724         else {
725             return (List<AnnouncementsDelivery>)result;
726         }
727     }
728 
729     public void removeByUserId(long userId) throws SystemException {
730         for (AnnouncementsDelivery announcementsDelivery : findByUserId(userId)) {
731             remove(announcementsDelivery);
732         }
733     }
734 
735     public void removeByU_T(long userId, String type)
736         throws NoSuchDeliveryException, SystemException {
737         AnnouncementsDelivery announcementsDelivery = findByU_T(userId, type);
738 
739         remove(announcementsDelivery);
740     }
741 
742     public void removeAll() throws SystemException {
743         for (AnnouncementsDelivery announcementsDelivery : findAll()) {
744             remove(announcementsDelivery);
745         }
746     }
747 
748     public int countByUserId(long userId) throws SystemException {
749         boolean finderClassNameCacheEnabled = AnnouncementsDeliveryModelImpl.CACHE_ENABLED;
750         String finderClassName = AnnouncementsDelivery.class.getName();
751         String finderMethodName = "countByUserId";
752         String[] finderParams = new String[] { Long.class.getName() };
753         Object[] finderArgs = new Object[] { new Long(userId) };
754 
755         Object result = null;
756 
757         if (finderClassNameCacheEnabled) {
758             result = FinderCacheUtil.getResult(finderClassName,
759                     finderMethodName, finderParams, finderArgs, this);
760         }
761 
762         if (result == null) {
763             Session session = null;
764 
765             try {
766                 session = openSession();
767 
768                 StringBuilder query = new StringBuilder();
769 
770                 query.append("SELECT COUNT(*) ");
771                 query.append(
772                     "FROM com.liferay.portlet.announcements.model.AnnouncementsDelivery WHERE ");
773 
774                 query.append("userId = ?");
775 
776                 query.append(" ");
777 
778                 Query q = session.createQuery(query.toString());
779 
780                 QueryPos qPos = QueryPos.getInstance(q);
781 
782                 qPos.add(userId);
783 
784                 Long count = null;
785 
786                 Iterator<Long> itr = q.list().iterator();
787 
788                 if (itr.hasNext()) {
789                     count = itr.next();
790                 }
791 
792                 if (count == null) {
793                     count = new Long(0);
794                 }
795 
796                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
797                     finderClassName, finderMethodName, finderParams,
798                     finderArgs, count);
799 
800                 return count.intValue();
801             }
802             catch (Exception e) {
803                 throw processException(e);
804             }
805             finally {
806                 closeSession(session);
807             }
808         }
809         else {
810             return ((Long)result).intValue();
811         }
812     }
813 
814     public int countByU_T(long userId, String type) throws SystemException {
815         boolean finderClassNameCacheEnabled = AnnouncementsDeliveryModelImpl.CACHE_ENABLED;
816         String finderClassName = AnnouncementsDelivery.class.getName();
817         String finderMethodName = "countByU_T";
818         String[] finderParams = new String[] {
819                 Long.class.getName(), String.class.getName()
820             };
821         Object[] finderArgs = new Object[] { new Long(userId), type };
822 
823         Object result = null;
824 
825         if (finderClassNameCacheEnabled) {
826             result = FinderCacheUtil.getResult(finderClassName,
827                     finderMethodName, finderParams, finderArgs, this);
828         }
829 
830         if (result == null) {
831             Session session = null;
832 
833             try {
834                 session = openSession();
835 
836                 StringBuilder query = new StringBuilder();
837 
838                 query.append("SELECT COUNT(*) ");
839                 query.append(
840                     "FROM com.liferay.portlet.announcements.model.AnnouncementsDelivery WHERE ");
841 
842                 query.append("userId = ?");
843 
844                 query.append(" AND ");
845 
846                 if (type == null) {
847                     query.append("type_ IS NULL");
848                 }
849                 else {
850                     query.append("type_ = ?");
851                 }
852 
853                 query.append(" ");
854 
855                 Query q = session.createQuery(query.toString());
856 
857                 QueryPos qPos = QueryPos.getInstance(q);
858 
859                 qPos.add(userId);
860 
861                 if (type != null) {
862                     qPos.add(type);
863                 }
864 
865                 Long count = null;
866 
867                 Iterator<Long> itr = q.list().iterator();
868 
869                 if (itr.hasNext()) {
870                     count = itr.next();
871                 }
872 
873                 if (count == null) {
874                     count = new Long(0);
875                 }
876 
877                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
878                     finderClassName, finderMethodName, finderParams,
879                     finderArgs, count);
880 
881                 return count.intValue();
882             }
883             catch (Exception e) {
884                 throw processException(e);
885             }
886             finally {
887                 closeSession(session);
888             }
889         }
890         else {
891             return ((Long)result).intValue();
892         }
893     }
894 
895     public int countAll() throws SystemException {
896         boolean finderClassNameCacheEnabled = AnnouncementsDeliveryModelImpl.CACHE_ENABLED;
897         String finderClassName = AnnouncementsDelivery.class.getName();
898         String finderMethodName = "countAll";
899         String[] finderParams = new String[] {  };
900         Object[] finderArgs = new Object[] {  };
901 
902         Object result = null;
903 
904         if (finderClassNameCacheEnabled) {
905             result = FinderCacheUtil.getResult(finderClassName,
906                     finderMethodName, finderParams, finderArgs, this);
907         }
908 
909         if (result == null) {
910             Session session = null;
911 
912             try {
913                 session = openSession();
914 
915                 Query q = session.createQuery(
916                         "SELECT COUNT(*) FROM com.liferay.portlet.announcements.model.AnnouncementsDelivery");
917 
918                 Long count = null;
919 
920                 Iterator<Long> itr = q.list().iterator();
921 
922                 if (itr.hasNext()) {
923                     count = itr.next();
924                 }
925 
926                 if (count == null) {
927                     count = new Long(0);
928                 }
929 
930                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
931                     finderClassName, finderMethodName, finderParams,
932                     finderArgs, count);
933 
934                 return count.intValue();
935             }
936             catch (Exception e) {
937                 throw processException(e);
938             }
939             finally {
940                 closeSession(session);
941             }
942         }
943         else {
944             return ((Long)result).intValue();
945         }
946     }
947 
948     public void registerListener(ModelListener listener) {
949         List<ModelListener> listeners = ListUtil.fromArray(_listeners);
950 
951         listeners.add(listener);
952 
953         _listeners = listeners.toArray(new ModelListener[listeners.size()]);
954     }
955 
956     public void unregisterListener(ModelListener listener) {
957         List<ModelListener> listeners = ListUtil.fromArray(_listeners);
958 
959         listeners.remove(listener);
960 
961         _listeners = listeners.toArray(new ModelListener[listeners.size()]);
962     }
963 
964     public void afterPropertiesSet() {
965         String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
966                     com.liferay.portal.util.PropsUtil.get(
967                         "value.object.listener.com.liferay.portlet.announcements.model.AnnouncementsDelivery")));
968 
969         if (listenerClassNames.length > 0) {
970             try {
971                 List<ModelListener> listeners = new ArrayList<ModelListener>();
972 
973                 for (String listenerClassName : listenerClassNames) {
974                     listeners.add((ModelListener)Class.forName(
975                             listenerClassName).newInstance());
976                 }
977 
978                 _listeners = listeners.toArray(new ModelListener[listeners.size()]);
979             }
980             catch (Exception e) {
981                 _log.error(e);
982             }
983         }
984     }
985 
986     private static Log _log = LogFactory.getLog(AnnouncementsDeliveryPersistenceImpl.class);
987     private ModelListener[] _listeners = new ModelListener[0];
988 }