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