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