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