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