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