1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.shopping.service.persistence;
16  
17  import com.liferay.portal.NoSuchModelException;
18  import com.liferay.portal.kernel.annotation.BeanReference;
19  import com.liferay.portal.kernel.cache.CacheRegistry;
20  import com.liferay.portal.kernel.dao.orm.EntityCacheUtil;
21  import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
22  import com.liferay.portal.kernel.dao.orm.FinderPath;
23  import com.liferay.portal.kernel.dao.orm.Query;
24  import com.liferay.portal.kernel.dao.orm.QueryPos;
25  import com.liferay.portal.kernel.dao.orm.QueryUtil;
26  import com.liferay.portal.kernel.dao.orm.Session;
27  import com.liferay.portal.kernel.exception.SystemException;
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     public ShoppingCoupon updateImpl(
234         com.liferay.portlet.shopping.model.ShoppingCoupon shoppingCoupon,
235         boolean merge) throws SystemException {
236         shoppingCoupon = toUnwrappedModel(shoppingCoupon);
237 
238         boolean isNew = shoppingCoupon.isNew();
239 
240         ShoppingCouponModelImpl shoppingCouponModelImpl = (ShoppingCouponModelImpl)shoppingCoupon;
241 
242         Session session = null;
243 
244         try {
245             session = openSession();
246 
247             BatchSessionUtil.update(session, shoppingCoupon, merge);
248 
249             shoppingCoupon.setNew(false);
250         }
251         catch (Exception e) {
252             throw processException(e);
253         }
254         finally {
255             closeSession(session);
256         }
257 
258         FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
259 
260         EntityCacheUtil.putResult(ShoppingCouponModelImpl.ENTITY_CACHE_ENABLED,
261             ShoppingCouponImpl.class, shoppingCoupon.getPrimaryKey(),
262             shoppingCoupon);
263 
264         if (!isNew &&
265                 (!Validator.equals(shoppingCoupon.getCode(),
266                     shoppingCouponModelImpl.getOriginalCode()))) {
267             FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_CODE,
268                 new Object[] { shoppingCouponModelImpl.getOriginalCode() });
269         }
270 
271         if (isNew ||
272                 (!Validator.equals(shoppingCoupon.getCode(),
273                     shoppingCouponModelImpl.getOriginalCode()))) {
274             FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_CODE,
275                 new Object[] { shoppingCoupon.getCode() }, shoppingCoupon);
276         }
277 
278         return shoppingCoupon;
279     }
280 
281     protected ShoppingCoupon toUnwrappedModel(ShoppingCoupon shoppingCoupon) {
282         if (shoppingCoupon instanceof ShoppingCouponImpl) {
283             return shoppingCoupon;
284         }
285 
286         ShoppingCouponImpl shoppingCouponImpl = new ShoppingCouponImpl();
287 
288         shoppingCouponImpl.setNew(shoppingCoupon.isNew());
289         shoppingCouponImpl.setPrimaryKey(shoppingCoupon.getPrimaryKey());
290 
291         shoppingCouponImpl.setCouponId(shoppingCoupon.getCouponId());
292         shoppingCouponImpl.setGroupId(shoppingCoupon.getGroupId());
293         shoppingCouponImpl.setCompanyId(shoppingCoupon.getCompanyId());
294         shoppingCouponImpl.setUserId(shoppingCoupon.getUserId());
295         shoppingCouponImpl.setUserName(shoppingCoupon.getUserName());
296         shoppingCouponImpl.setCreateDate(shoppingCoupon.getCreateDate());
297         shoppingCouponImpl.setModifiedDate(shoppingCoupon.getModifiedDate());
298         shoppingCouponImpl.setCode(shoppingCoupon.getCode());
299         shoppingCouponImpl.setName(shoppingCoupon.getName());
300         shoppingCouponImpl.setDescription(shoppingCoupon.getDescription());
301         shoppingCouponImpl.setStartDate(shoppingCoupon.getStartDate());
302         shoppingCouponImpl.setEndDate(shoppingCoupon.getEndDate());
303         shoppingCouponImpl.setActive(shoppingCoupon.isActive());
304         shoppingCouponImpl.setLimitCategories(shoppingCoupon.getLimitCategories());
305         shoppingCouponImpl.setLimitSkus(shoppingCoupon.getLimitSkus());
306         shoppingCouponImpl.setMinOrder(shoppingCoupon.getMinOrder());
307         shoppingCouponImpl.setDiscount(shoppingCoupon.getDiscount());
308         shoppingCouponImpl.setDiscountType(shoppingCoupon.getDiscountType());
309 
310         return shoppingCouponImpl;
311     }
312 
313     public ShoppingCoupon findByPrimaryKey(Serializable primaryKey)
314         throws NoSuchModelException, SystemException {
315         return findByPrimaryKey(((Long)primaryKey).longValue());
316     }
317 
318     public ShoppingCoupon findByPrimaryKey(long couponId)
319         throws NoSuchCouponException, SystemException {
320         ShoppingCoupon shoppingCoupon = fetchByPrimaryKey(couponId);
321 
322         if (shoppingCoupon == null) {
323             if (_log.isWarnEnabled()) {
324                 _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + couponId);
325             }
326 
327             throw new NoSuchCouponException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
328                 couponId);
329         }
330 
331         return shoppingCoupon;
332     }
333 
334     public ShoppingCoupon fetchByPrimaryKey(Serializable primaryKey)
335         throws SystemException {
336         return fetchByPrimaryKey(((Long)primaryKey).longValue());
337     }
338 
339     public ShoppingCoupon fetchByPrimaryKey(long couponId)
340         throws SystemException {
341         ShoppingCoupon shoppingCoupon = (ShoppingCoupon)EntityCacheUtil.getResult(ShoppingCouponModelImpl.ENTITY_CACHE_ENABLED,
342                 ShoppingCouponImpl.class, couponId, this);
343 
344         if (shoppingCoupon == null) {
345             Session session = null;
346 
347             try {
348                 session = openSession();
349 
350                 shoppingCoupon = (ShoppingCoupon)session.get(ShoppingCouponImpl.class,
351                         new Long(couponId));
352             }
353             catch (Exception e) {
354                 throw processException(e);
355             }
356             finally {
357                 if (shoppingCoupon != null) {
358                     cacheResult(shoppingCoupon);
359                 }
360 
361                 closeSession(session);
362             }
363         }
364 
365         return shoppingCoupon;
366     }
367 
368     public List<ShoppingCoupon> findByGroupId(long groupId)
369         throws SystemException {
370         Object[] finderArgs = new Object[] { new Long(groupId) };
371 
372         List<ShoppingCoupon> list = (List<ShoppingCoupon>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_GROUPID,
373                 finderArgs, this);
374 
375         if (list == null) {
376             Session session = null;
377 
378             try {
379                 session = openSession();
380 
381                 StringBundler query = new StringBundler(3);
382 
383                 query.append(_SQL_SELECT_SHOPPINGCOUPON_WHERE);
384 
385                 query.append(_FINDER_COLUMN_GROUPID_GROUPID_2);
386 
387                 query.append(ShoppingCouponModelImpl.ORDER_BY_JPQL);
388 
389                 String sql = query.toString();
390 
391                 Query q = session.createQuery(sql);
392 
393                 QueryPos qPos = QueryPos.getInstance(q);
394 
395                 qPos.add(groupId);
396 
397                 list = q.list();
398             }
399             catch (Exception e) {
400                 throw processException(e);
401             }
402             finally {
403                 if (list == null) {
404                     list = new ArrayList<ShoppingCoupon>();
405                 }
406 
407                 cacheResult(list);
408 
409                 FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_GROUPID,
410                     finderArgs, list);
411 
412                 closeSession(session);
413             }
414         }
415 
416         return list;
417     }
418 
419     public List<ShoppingCoupon> findByGroupId(long groupId, int start, int end)
420         throws SystemException {
421         return findByGroupId(groupId, start, end, null);
422     }
423 
424     public List<ShoppingCoupon> findByGroupId(long groupId, int start, int end,
425         OrderByComparator orderByComparator) throws SystemException {
426         Object[] finderArgs = new Object[] {
427                 new Long(groupId),
428                 
429                 String.valueOf(start), String.valueOf(end),
430                 String.valueOf(orderByComparator)
431             };
432 
433         List<ShoppingCoupon> list = (List<ShoppingCoupon>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_OBC_GROUPID,
434                 finderArgs, this);
435 
436         if (list == null) {
437             Session session = null;
438 
439             try {
440                 session = openSession();
441 
442                 StringBundler query = null;
443 
444                 if (orderByComparator != null) {
445                     query = new StringBundler(3 +
446                             (orderByComparator.getOrderByFields().length * 3));
447                 }
448                 else {
449                     query = new StringBundler(3);
450                 }
451 
452                 query.append(_SQL_SELECT_SHOPPINGCOUPON_WHERE);
453 
454                 query.append(_FINDER_COLUMN_GROUPID_GROUPID_2);
455 
456                 if (orderByComparator != null) {
457                     appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
458                         orderByComparator);
459                 }
460 
461                 else {
462                     query.append(ShoppingCouponModelImpl.ORDER_BY_JPQL);
463                 }
464 
465                 String sql = query.toString();
466 
467                 Query q = session.createQuery(sql);
468 
469                 QueryPos qPos = QueryPos.getInstance(q);
470 
471                 qPos.add(groupId);
472 
473                 list = (List<ShoppingCoupon>)QueryUtil.list(q, getDialect(),
474                         start, end);
475             }
476             catch (Exception e) {
477                 throw processException(e);
478             }
479             finally {
480                 if (list == null) {
481                     list = new ArrayList<ShoppingCoupon>();
482                 }
483 
484                 cacheResult(list);
485 
486                 FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_OBC_GROUPID,
487                     finderArgs, list);
488 
489                 closeSession(session);
490             }
491         }
492 
493         return list;
494     }
495 
496     public ShoppingCoupon findByGroupId_First(long groupId,
497         OrderByComparator orderByComparator)
498         throws NoSuchCouponException, SystemException {
499         List<ShoppingCoupon> list = findByGroupId(groupId, 0, 1,
500                 orderByComparator);
501 
502         if (list.isEmpty()) {
503             StringBundler msg = new StringBundler(4);
504 
505             msg.append(_NO_SUCH_ENTITY_WITH_KEY);
506 
507             msg.append("groupId=");
508             msg.append(groupId);
509 
510             msg.append(StringPool.CLOSE_CURLY_BRACE);
511 
512             throw new NoSuchCouponException(msg.toString());
513         }
514         else {
515             return list.get(0);
516         }
517     }
518 
519     public ShoppingCoupon findByGroupId_Last(long groupId,
520         OrderByComparator orderByComparator)
521         throws NoSuchCouponException, SystemException {
522         int count = countByGroupId(groupId);
523 
524         List<ShoppingCoupon> list = findByGroupId(groupId, count - 1, count,
525                 orderByComparator);
526 
527         if (list.isEmpty()) {
528             StringBundler msg = new StringBundler(4);
529 
530             msg.append(_NO_SUCH_ENTITY_WITH_KEY);
531 
532             msg.append("groupId=");
533             msg.append(groupId);
534 
535             msg.append(StringPool.CLOSE_CURLY_BRACE);
536 
537             throw new NoSuchCouponException(msg.toString());
538         }
539         else {
540             return list.get(0);
541         }
542     }
543 
544     public ShoppingCoupon[] findByGroupId_PrevAndNext(long couponId,
545         long groupId, OrderByComparator orderByComparator)
546         throws NoSuchCouponException, SystemException {
547         ShoppingCoupon shoppingCoupon = findByPrimaryKey(couponId);
548 
549         int count = countByGroupId(groupId);
550 
551         Session session = null;
552 
553         try {
554             session = openSession();
555 
556             StringBundler query = null;
557 
558             if (orderByComparator != null) {
559                 query = new StringBundler(3 +
560                         (orderByComparator.getOrderByFields().length * 3));
561             }
562             else {
563                 query = new StringBundler(3);
564             }
565 
566             query.append(_SQL_SELECT_SHOPPINGCOUPON_WHERE);
567 
568             query.append(_FINDER_COLUMN_GROUPID_GROUPID_2);
569 
570             if (orderByComparator != null) {
571                 appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
572                     orderByComparator);
573             }
574 
575             else {
576                 query.append(ShoppingCouponModelImpl.ORDER_BY_JPQL);
577             }
578 
579             String sql = query.toString();
580 
581             Query q = session.createQuery(sql);
582 
583             QueryPos qPos = QueryPos.getInstance(q);
584 
585             qPos.add(groupId);
586 
587             Object[] objArray = QueryUtil.getPrevAndNext(q, count,
588                     orderByComparator, shoppingCoupon);
589 
590             ShoppingCoupon[] array = new ShoppingCouponImpl[3];
591 
592             array[0] = (ShoppingCoupon)objArray[0];
593             array[1] = (ShoppingCoupon)objArray[1];
594             array[2] = (ShoppingCoupon)objArray[2];
595 
596             return array;
597         }
598         catch (Exception e) {
599             throw processException(e);
600         }
601         finally {
602             closeSession(session);
603         }
604     }
605 
606     public ShoppingCoupon findByCode(String code)
607         throws NoSuchCouponException, SystemException {
608         ShoppingCoupon shoppingCoupon = fetchByCode(code);
609 
610         if (shoppingCoupon == null) {
611             StringBundler msg = new StringBundler(4);
612 
613             msg.append(_NO_SUCH_ENTITY_WITH_KEY);
614 
615             msg.append("code=");
616             msg.append(code);
617 
618             msg.append(StringPool.CLOSE_CURLY_BRACE);
619 
620             if (_log.isWarnEnabled()) {
621                 _log.warn(msg.toString());
622             }
623 
624             throw new NoSuchCouponException(msg.toString());
625         }
626 
627         return shoppingCoupon;
628     }
629 
630     public ShoppingCoupon fetchByCode(String code) throws SystemException {
631         return fetchByCode(code, true);
632     }
633 
634     public ShoppingCoupon fetchByCode(String code, boolean retrieveFromCache)
635         throws SystemException {
636         Object[] finderArgs = new Object[] { code };
637 
638         Object result = null;
639 
640         if (retrieveFromCache) {
641             result = FinderCacheUtil.getResult(FINDER_PATH_FETCH_BY_CODE,
642                     finderArgs, this);
643         }
644 
645         if (result == null) {
646             Session session = null;
647 
648             try {
649                 session = openSession();
650 
651                 StringBundler query = new StringBundler(3);
652 
653                 query.append(_SQL_SELECT_SHOPPINGCOUPON_WHERE);
654 
655                 if (code == null) {
656                     query.append(_FINDER_COLUMN_CODE_CODE_1);
657                 }
658                 else {
659                     if (code.equals(StringPool.BLANK)) {
660                         query.append(_FINDER_COLUMN_CODE_CODE_3);
661                     }
662                     else {
663                         query.append(_FINDER_COLUMN_CODE_CODE_2);
664                     }
665                 }
666 
667                 query.append(ShoppingCouponModelImpl.ORDER_BY_JPQL);
668 
669                 String sql = query.toString();
670 
671                 Query q = session.createQuery(sql);
672 
673                 QueryPos qPos = QueryPos.getInstance(q);
674 
675                 if (code != null) {
676                     qPos.add(code);
677                 }
678 
679                 List<ShoppingCoupon> list = q.list();
680 
681                 result = list;
682 
683                 ShoppingCoupon shoppingCoupon = null;
684 
685                 if (list.isEmpty()) {
686                     FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_CODE,
687                         finderArgs, list);
688                 }
689                 else {
690                     shoppingCoupon = list.get(0);
691 
692                     cacheResult(shoppingCoupon);
693 
694                     if ((shoppingCoupon.getCode() == null) ||
695                             !shoppingCoupon.getCode().equals(code)) {
696                         FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_CODE,
697                             finderArgs, shoppingCoupon);
698                     }
699                 }
700 
701                 return shoppingCoupon;
702             }
703             catch (Exception e) {
704                 throw processException(e);
705             }
706             finally {
707                 if (result == null) {
708                     FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_CODE,
709                         finderArgs, new ArrayList<ShoppingCoupon>());
710                 }
711 
712                 closeSession(session);
713             }
714         }
715         else {
716             if (result instanceof List<?>) {
717                 return null;
718             }
719             else {
720                 return (ShoppingCoupon)result;
721             }
722         }
723     }
724 
725     public List<ShoppingCoupon> findAll() throws SystemException {
726         return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
727     }
728 
729     public List<ShoppingCoupon> findAll(int start, int end)
730         throws SystemException {
731         return findAll(start, end, null);
732     }
733 
734     public List<ShoppingCoupon> findAll(int start, int end,
735         OrderByComparator orderByComparator) throws SystemException {
736         Object[] finderArgs = new Object[] {
737                 String.valueOf(start), String.valueOf(end),
738                 String.valueOf(orderByComparator)
739             };
740 
741         List<ShoppingCoupon> list = (List<ShoppingCoupon>)FinderCacheUtil.getResult(FINDER_PATH_FIND_ALL,
742                 finderArgs, this);
743 
744         if (list == null) {
745             Session session = null;
746 
747             try {
748                 session = openSession();
749 
750                 StringBundler query = null;
751                 String sql = null;
752 
753                 if (orderByComparator != null) {
754                     query = new StringBundler(2 +
755                             (orderByComparator.getOrderByFields().length * 3));
756 
757                     query.append(_SQL_SELECT_SHOPPINGCOUPON);
758 
759                     appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
760                         orderByComparator);
761 
762                     sql = query.toString();
763                 }
764 
765                 else {
766                     sql = _SQL_SELECT_SHOPPINGCOUPON.concat(ShoppingCouponModelImpl.ORDER_BY_JPQL);
767                 }
768 
769                 Query q = session.createQuery(sql);
770 
771                 if (orderByComparator == null) {
772                     list = (List<ShoppingCoupon>)QueryUtil.list(q,
773                             getDialect(), start, end, false);
774 
775                     Collections.sort(list);
776                 }
777                 else {
778                     list = (List<ShoppingCoupon>)QueryUtil.list(q,
779                             getDialect(), start, end);
780                 }
781             }
782             catch (Exception e) {
783                 throw processException(e);
784             }
785             finally {
786                 if (list == null) {
787                     list = new ArrayList<ShoppingCoupon>();
788                 }
789 
790                 cacheResult(list);
791 
792                 FinderCacheUtil.putResult(FINDER_PATH_FIND_ALL, finderArgs, list);
793 
794                 closeSession(session);
795             }
796         }
797 
798         return list;
799     }
800 
801     public void removeByGroupId(long groupId) throws SystemException {
802         for (ShoppingCoupon shoppingCoupon : findByGroupId(groupId)) {
803             remove(shoppingCoupon);
804         }
805     }
806 
807     public void removeByCode(String code)
808         throws NoSuchCouponException, SystemException {
809         ShoppingCoupon shoppingCoupon = findByCode(code);
810 
811         remove(shoppingCoupon);
812     }
813 
814     public void removeAll() throws SystemException {
815         for (ShoppingCoupon shoppingCoupon : findAll()) {
816             remove(shoppingCoupon);
817         }
818     }
819 
820     public int countByGroupId(long groupId) throws SystemException {
821         Object[] finderArgs = new Object[] { new Long(groupId) };
822 
823         Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_GROUPID,
824                 finderArgs, this);
825 
826         if (count == null) {
827             Session session = null;
828 
829             try {
830                 session = openSession();
831 
832                 StringBundler query = new StringBundler(2);
833 
834                 query.append(_SQL_COUNT_SHOPPINGCOUPON_WHERE);
835 
836                 query.append(_FINDER_COLUMN_GROUPID_GROUPID_2);
837 
838                 String sql = query.toString();
839 
840                 Query q = session.createQuery(sql);
841 
842                 QueryPos qPos = QueryPos.getInstance(q);
843 
844                 qPos.add(groupId);
845 
846                 count = (Long)q.uniqueResult();
847             }
848             catch (Exception e) {
849                 throw processException(e);
850             }
851             finally {
852                 if (count == null) {
853                     count = Long.valueOf(0);
854                 }
855 
856                 FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_GROUPID,
857                     finderArgs, count);
858 
859                 closeSession(session);
860             }
861         }
862 
863         return count.intValue();
864     }
865 
866     public int countByCode(String code) throws SystemException {
867         Object[] finderArgs = new Object[] { code };
868 
869         Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_CODE,
870                 finderArgs, this);
871 
872         if (count == null) {
873             Session session = null;
874 
875             try {
876                 session = openSession();
877 
878                 StringBundler query = new StringBundler(2);
879 
880                 query.append(_SQL_COUNT_SHOPPINGCOUPON_WHERE);
881 
882                 if (code == null) {
883                     query.append(_FINDER_COLUMN_CODE_CODE_1);
884                 }
885                 else {
886                     if (code.equals(StringPool.BLANK)) {
887                         query.append(_FINDER_COLUMN_CODE_CODE_3);
888                     }
889                     else {
890                         query.append(_FINDER_COLUMN_CODE_CODE_2);
891                     }
892                 }
893 
894                 String sql = query.toString();
895 
896                 Query q = session.createQuery(sql);
897 
898                 QueryPos qPos = QueryPos.getInstance(q);
899 
900                 if (code != null) {
901                     qPos.add(code);
902                 }
903 
904                 count = (Long)q.uniqueResult();
905             }
906             catch (Exception e) {
907                 throw processException(e);
908             }
909             finally {
910                 if (count == null) {
911                     count = Long.valueOf(0);
912                 }
913 
914                 FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_CODE,
915                     finderArgs, count);
916 
917                 closeSession(session);
918             }
919         }
920 
921         return count.intValue();
922     }
923 
924     public int countAll() throws SystemException {
925         Object[] finderArgs = new Object[0];
926 
927         Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
928                 finderArgs, this);
929 
930         if (count == null) {
931             Session session = null;
932 
933             try {
934                 session = openSession();
935 
936                 Query q = session.createQuery(_SQL_COUNT_SHOPPINGCOUPON);
937 
938                 count = (Long)q.uniqueResult();
939             }
940             catch (Exception e) {
941                 throw processException(e);
942             }
943             finally {
944                 if (count == null) {
945                     count = Long.valueOf(0);
946                 }
947 
948                 FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL, finderArgs,
949                     count);
950 
951                 closeSession(session);
952             }
953         }
954 
955         return count.intValue();
956     }
957 
958     public void afterPropertiesSet() {
959         String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
960                     com.liferay.portal.util.PropsUtil.get(
961                         "value.object.listener.com.liferay.portlet.shopping.model.ShoppingCoupon")));
962 
963         if (listenerClassNames.length > 0) {
964             try {
965                 List<ModelListener<ShoppingCoupon>> listenersList = new ArrayList<ModelListener<ShoppingCoupon>>();
966 
967                 for (String listenerClassName : listenerClassNames) {
968                     listenersList.add((ModelListener<ShoppingCoupon>)Class.forName(
969                             listenerClassName).newInstance());
970                 }
971 
972                 listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
973             }
974             catch (Exception e) {
975                 _log.error(e);
976             }
977         }
978     }
979 
980     @BeanReference(type = ShoppingCartPersistence.class)
981     protected ShoppingCartPersistence shoppingCartPersistence;
982     @BeanReference(type = ShoppingCategoryPersistence.class)
983     protected ShoppingCategoryPersistence shoppingCategoryPersistence;
984     @BeanReference(type = ShoppingCouponPersistence.class)
985     protected ShoppingCouponPersistence shoppingCouponPersistence;
986     @BeanReference(type = ShoppingItemPersistence.class)
987     protected ShoppingItemPersistence shoppingItemPersistence;
988     @BeanReference(type = ShoppingItemFieldPersistence.class)
989     protected ShoppingItemFieldPersistence shoppingItemFieldPersistence;
990     @BeanReference(type = ShoppingItemPricePersistence.class)
991     protected ShoppingItemPricePersistence shoppingItemPricePersistence;
992     @BeanReference(type = ShoppingOrderPersistence.class)
993     protected ShoppingOrderPersistence shoppingOrderPersistence;
994     @BeanReference(type = ShoppingOrderItemPersistence.class)
995     protected ShoppingOrderItemPersistence shoppingOrderItemPersistence;
996     @BeanReference(type = ResourcePersistence.class)
997     protected ResourcePersistence resourcePersistence;
998     @BeanReference(type = UserPersistence.class)
999     protected UserPersistence userPersistence;
1000    private static final String _SQL_SELECT_SHOPPINGCOUPON = "SELECT shoppingCoupon FROM ShoppingCoupon shoppingCoupon";
1001    private static final String _SQL_SELECT_SHOPPINGCOUPON_WHERE = "SELECT shoppingCoupon FROM ShoppingCoupon shoppingCoupon WHERE ";
1002    private static final String _SQL_COUNT_SHOPPINGCOUPON = "SELECT COUNT(shoppingCoupon) FROM ShoppingCoupon shoppingCoupon";
1003    private static final String _SQL_COUNT_SHOPPINGCOUPON_WHERE = "SELECT COUNT(shoppingCoupon) FROM ShoppingCoupon shoppingCoupon WHERE ";
1004    private static final String _FINDER_COLUMN_GROUPID_GROUPID_2 = "shoppingCoupon.groupId = ?";
1005    private static final String _FINDER_COLUMN_CODE_CODE_1 = "shoppingCoupon.code IS NULL";
1006    private static final String _FINDER_COLUMN_CODE_CODE_2 = "shoppingCoupon.code = ?";
1007    private static final String _FINDER_COLUMN_CODE_CODE_3 = "(shoppingCoupon.code IS NULL OR shoppingCoupon.code = ?)";
1008    private static final String _ORDER_BY_ENTITY_ALIAS = "shoppingCoupon.";
1009    private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No ShoppingCoupon exists with the primary key ";
1010    private static final String _NO_SUCH_ENTITY_WITH_KEY = "No ShoppingCoupon exists with the key {";
1011    private static Log _log = LogFactoryUtil.getLog(ShoppingCouponPersistenceImpl.class);
1012}