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