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