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