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