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.BatchSessionUtil;
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 {
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             if (BatchSessionUtil.isEnabled()) {
132                 Object staleObject = session.get(ShoppingCouponImpl.class,
133                         shoppingCoupon.getPrimaryKeyObj());
134 
135                 if (staleObject != null) {
136                     session.evict(staleObject);
137                 }
138             }
139 
140             session.delete(shoppingCoupon);
141 
142             session.flush();
143 
144             return shoppingCoupon;
145         }
146         catch (Exception e) {
147             throw processException(e);
148         }
149         finally {
150             closeSession(session);
151 
152             FinderCacheUtil.clearCache(ShoppingCoupon.class.getName());
153         }
154     }
155 
156     /**
157      * @deprecated Use <code>update(ShoppingCoupon shoppingCoupon, boolean merge)</code>.
158      */
159     public ShoppingCoupon update(ShoppingCoupon shoppingCoupon)
160         throws SystemException {
161         if (_log.isWarnEnabled()) {
162             _log.warn(
163                 "Using the deprecated update(ShoppingCoupon shoppingCoupon) method. Use update(ShoppingCoupon shoppingCoupon, boolean merge) instead.");
164         }
165 
166         return update(shoppingCoupon, false);
167     }
168 
169     /**
170      * Add, update, or merge, the entity. This method also calls the model
171      * listeners to trigger the proper events associated with adding, deleting,
172      * or updating an entity.
173      *
174      * @param        shoppingCoupon the entity to add, update, or merge
175      * @param        merge boolean value for whether to merge the entity. The
176      *                default value is false. Setting merge to true is more
177      *                expensive and should only be true when shoppingCoupon is
178      *                transient. See LEP-5473 for a detailed discussion of this
179      *                method.
180      * @return        true if the portlet can be displayed via Ajax
181      */
182     public ShoppingCoupon update(ShoppingCoupon shoppingCoupon, boolean merge)
183         throws SystemException {
184         boolean isNew = shoppingCoupon.isNew();
185 
186         if (_listeners.length > 0) {
187             for (ModelListener listener : _listeners) {
188                 if (isNew) {
189                     listener.onBeforeCreate(shoppingCoupon);
190                 }
191                 else {
192                     listener.onBeforeUpdate(shoppingCoupon);
193                 }
194             }
195         }
196 
197         shoppingCoupon = updateImpl(shoppingCoupon, merge);
198 
199         if (_listeners.length > 0) {
200             for (ModelListener listener : _listeners) {
201                 if (isNew) {
202                     listener.onAfterCreate(shoppingCoupon);
203                 }
204                 else {
205                     listener.onAfterUpdate(shoppingCoupon);
206                 }
207             }
208         }
209 
210         return shoppingCoupon;
211     }
212 
213     public ShoppingCoupon updateImpl(
214         com.liferay.portlet.shopping.model.ShoppingCoupon shoppingCoupon,
215         boolean merge) throws SystemException {
216         Session session = null;
217 
218         try {
219             session = openSession();
220 
221             BatchSessionUtil.update(session, shoppingCoupon, merge);
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 = null;
708 
709                 if (obc == null) {
710                     list = (List<ShoppingCoupon>)QueryUtil.list(q,
711                             getDialect(), start, end, false);
712 
713                     Collections.sort(list);
714                 }
715                 else {
716                     list = (List<ShoppingCoupon>)QueryUtil.list(q,
717                             getDialect(), start, end);
718                 }
719 
720                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
721                     finderClassName, finderMethodName, finderParams,
722                     finderArgs, list);
723 
724                 return list;
725             }
726             catch (Exception e) {
727                 throw processException(e);
728             }
729             finally {
730                 closeSession(session);
731             }
732         }
733         else {
734             return (List<ShoppingCoupon>)result;
735         }
736     }
737 
738     public void removeByGroupId(long groupId) throws SystemException {
739         for (ShoppingCoupon shoppingCoupon : findByGroupId(groupId)) {
740             remove(shoppingCoupon);
741         }
742     }
743 
744     public void removeByCode(String code)
745         throws NoSuchCouponException, SystemException {
746         ShoppingCoupon shoppingCoupon = findByCode(code);
747 
748         remove(shoppingCoupon);
749     }
750 
751     public void removeAll() throws SystemException {
752         for (ShoppingCoupon shoppingCoupon : findAll()) {
753             remove(shoppingCoupon);
754         }
755     }
756 
757     public int countByGroupId(long groupId) throws SystemException {
758         boolean finderClassNameCacheEnabled = ShoppingCouponModelImpl.CACHE_ENABLED;
759         String finderClassName = ShoppingCoupon.class.getName();
760         String finderMethodName = "countByGroupId";
761         String[] finderParams = new String[] { Long.class.getName() };
762         Object[] finderArgs = new Object[] { new Long(groupId) };
763 
764         Object result = null;
765 
766         if (finderClassNameCacheEnabled) {
767             result = FinderCacheUtil.getResult(finderClassName,
768                     finderMethodName, finderParams, finderArgs, this);
769         }
770 
771         if (result == null) {
772             Session session = null;
773 
774             try {
775                 session = openSession();
776 
777                 StringBuilder query = new StringBuilder();
778 
779                 query.append("SELECT COUNT(*) ");
780                 query.append(
781                     "FROM com.liferay.portlet.shopping.model.ShoppingCoupon WHERE ");
782 
783                 query.append("groupId = ?");
784 
785                 query.append(" ");
786 
787                 Query q = session.createQuery(query.toString());
788 
789                 QueryPos qPos = QueryPos.getInstance(q);
790 
791                 qPos.add(groupId);
792 
793                 Long count = null;
794 
795                 Iterator<Long> itr = q.list().iterator();
796 
797                 if (itr.hasNext()) {
798                     count = itr.next();
799                 }
800 
801                 if (count == null) {
802                     count = new Long(0);
803                 }
804 
805                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
806                     finderClassName, finderMethodName, finderParams,
807                     finderArgs, count);
808 
809                 return count.intValue();
810             }
811             catch (Exception e) {
812                 throw processException(e);
813             }
814             finally {
815                 closeSession(session);
816             }
817         }
818         else {
819             return ((Long)result).intValue();
820         }
821     }
822 
823     public int countByCode(String code) throws SystemException {
824         boolean finderClassNameCacheEnabled = ShoppingCouponModelImpl.CACHE_ENABLED;
825         String finderClassName = ShoppingCoupon.class.getName();
826         String finderMethodName = "countByCode";
827         String[] finderParams = new String[] { String.class.getName() };
828         Object[] finderArgs = new Object[] { code };
829 
830         Object result = null;
831 
832         if (finderClassNameCacheEnabled) {
833             result = FinderCacheUtil.getResult(finderClassName,
834                     finderMethodName, finderParams, finderArgs, this);
835         }
836 
837         if (result == null) {
838             Session session = null;
839 
840             try {
841                 session = openSession();
842 
843                 StringBuilder query = new StringBuilder();
844 
845                 query.append("SELECT COUNT(*) ");
846                 query.append(
847                     "FROM com.liferay.portlet.shopping.model.ShoppingCoupon WHERE ");
848 
849                 if (code == null) {
850                     query.append("code_ IS NULL");
851                 }
852                 else {
853                     query.append("code_ = ?");
854                 }
855 
856                 query.append(" ");
857 
858                 Query q = session.createQuery(query.toString());
859 
860                 QueryPos qPos = QueryPos.getInstance(q);
861 
862                 if (code != null) {
863                     qPos.add(code);
864                 }
865 
866                 Long count = null;
867 
868                 Iterator<Long> itr = q.list().iterator();
869 
870                 if (itr.hasNext()) {
871                     count = itr.next();
872                 }
873 
874                 if (count == null) {
875                     count = new Long(0);
876                 }
877 
878                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
879                     finderClassName, finderMethodName, finderParams,
880                     finderArgs, count);
881 
882                 return count.intValue();
883             }
884             catch (Exception e) {
885                 throw processException(e);
886             }
887             finally {
888                 closeSession(session);
889             }
890         }
891         else {
892             return ((Long)result).intValue();
893         }
894     }
895 
896     public int countAll() throws SystemException {
897         boolean finderClassNameCacheEnabled = ShoppingCouponModelImpl.CACHE_ENABLED;
898         String finderClassName = ShoppingCoupon.class.getName();
899         String finderMethodName = "countAll";
900         String[] finderParams = new String[] {  };
901         Object[] finderArgs = new Object[] {  };
902 
903         Object result = null;
904 
905         if (finderClassNameCacheEnabled) {
906             result = FinderCacheUtil.getResult(finderClassName,
907                     finderMethodName, finderParams, finderArgs, this);
908         }
909 
910         if (result == null) {
911             Session session = null;
912 
913             try {
914                 session = openSession();
915 
916                 Query q = session.createQuery(
917                         "SELECT COUNT(*) FROM com.liferay.portlet.shopping.model.ShoppingCoupon");
918 
919                 Long count = null;
920 
921                 Iterator<Long> itr = q.list().iterator();
922 
923                 if (itr.hasNext()) {
924                     count = itr.next();
925                 }
926 
927                 if (count == null) {
928                     count = new Long(0);
929                 }
930 
931                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
932                     finderClassName, finderMethodName, finderParams,
933                     finderArgs, count);
934 
935                 return count.intValue();
936             }
937             catch (Exception e) {
938                 throw processException(e);
939             }
940             finally {
941                 closeSession(session);
942             }
943         }
944         else {
945             return ((Long)result).intValue();
946         }
947     }
948 
949     public void registerListener(ModelListener listener) {
950         List<ModelListener> listeners = ListUtil.fromArray(_listeners);
951 
952         listeners.add(listener);
953 
954         _listeners = listeners.toArray(new ModelListener[listeners.size()]);
955     }
956 
957     public void unregisterListener(ModelListener listener) {
958         List<ModelListener> listeners = ListUtil.fromArray(_listeners);
959 
960         listeners.remove(listener);
961 
962         _listeners = listeners.toArray(new ModelListener[listeners.size()]);
963     }
964 
965     public void afterPropertiesSet() {
966         String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
967                     com.liferay.portal.util.PropsUtil.get(
968                         "value.object.listener.com.liferay.portlet.shopping.model.ShoppingCoupon")));
969 
970         if (listenerClassNames.length > 0) {
971             try {
972                 List<ModelListener> listeners = new ArrayList<ModelListener>();
973 
974                 for (String listenerClassName : listenerClassNames) {
975                     listeners.add((ModelListener)Class.forName(
976                             listenerClassName).newInstance());
977                 }
978 
979                 _listeners = listeners.toArray(new ModelListener[listeners.size()]);
980             }
981             catch (Exception e) {
982                 _log.error(e);
983             }
984         }
985     }
986 
987     private static Log _log = LogFactory.getLog(ShoppingCouponPersistenceImpl.class);
988     private ModelListener[] _listeners = new ModelListener[0];
989 }