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