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