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