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