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