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                     "SELECT shoppingCoupon FROM ShoppingCoupon shoppingCoupon WHERE ");
386 
387                 query.append("shoppingCoupon.groupId = ?");
388 
389                 query.append(" ");
390 
391                 query.append("ORDER BY ");
392 
393                 query.append("shoppingCoupon.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                     "SELECT shoppingCoupon FROM ShoppingCoupon shoppingCoupon WHERE ");
449 
450                 query.append("shoppingCoupon.groupId = ?");
451 
452                 query.append(" ");
453 
454                 if (obc != null) {
455                     query.append("ORDER BY ");
456 
457                     String[] orderByFields = obc.getOrderByFields();
458 
459                     for (int i = 0; i < orderByFields.length; i++) {
460                         query.append("shoppingCoupon.");
461                         query.append(orderByFields[i]);
462 
463                         if (obc.isAscending()) {
464                             query.append(" ASC");
465                         }
466                         else {
467                             query.append(" DESC");
468                         }
469 
470                         if ((i + 1) < orderByFields.length) {
471                             query.append(", ");
472                         }
473                     }
474                 }
475 
476                 else {
477                     query.append("ORDER BY ");
478 
479                     query.append("shoppingCoupon.createDate ASC");
480                 }
481 
482                 Query q = session.createQuery(query.toString());
483 
484                 QueryPos qPos = QueryPos.getInstance(q);
485 
486                 qPos.add(groupId);
487 
488                 list = (List<ShoppingCoupon>)QueryUtil.list(q, getDialect(),
489                         start, end);
490             }
491             catch (Exception e) {
492                 throw processException(e);
493             }
494             finally {
495                 if (list == null) {
496                     list = new ArrayList<ShoppingCoupon>();
497                 }
498 
499                 cacheResult(list);
500 
501                 FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_OBC_GROUPID,
502                     finderArgs, list);
503 
504                 closeSession(session);
505             }
506         }
507 
508         return list;
509     }
510 
511     public ShoppingCoupon findByGroupId_First(long groupId,
512         OrderByComparator obc) throws NoSuchCouponException, SystemException {
513         List<ShoppingCoupon> list = findByGroupId(groupId, 0, 1, obc);
514 
515         if (list.isEmpty()) {
516             StringBuilder msg = new StringBuilder();
517 
518             msg.append("No ShoppingCoupon exists with the key {");
519 
520             msg.append("groupId=" + groupId);
521 
522             msg.append(StringPool.CLOSE_CURLY_BRACE);
523 
524             throw new NoSuchCouponException(msg.toString());
525         }
526         else {
527             return list.get(0);
528         }
529     }
530 
531     public ShoppingCoupon findByGroupId_Last(long groupId, OrderByComparator obc)
532         throws NoSuchCouponException, SystemException {
533         int count = countByGroupId(groupId);
534 
535         List<ShoppingCoupon> list = findByGroupId(groupId, count - 1, count, obc);
536 
537         if (list.isEmpty()) {
538             StringBuilder msg = new StringBuilder();
539 
540             msg.append("No ShoppingCoupon exists with the key {");
541 
542             msg.append("groupId=" + groupId);
543 
544             msg.append(StringPool.CLOSE_CURLY_BRACE);
545 
546             throw new NoSuchCouponException(msg.toString());
547         }
548         else {
549             return list.get(0);
550         }
551     }
552 
553     public ShoppingCoupon[] findByGroupId_PrevAndNext(long couponId,
554         long groupId, OrderByComparator obc)
555         throws NoSuchCouponException, SystemException {
556         ShoppingCoupon shoppingCoupon = findByPrimaryKey(couponId);
557 
558         int count = countByGroupId(groupId);
559 
560         Session session = null;
561 
562         try {
563             session = openSession();
564 
565             StringBuilder query = new StringBuilder();
566 
567             query.append(
568                 "SELECT shoppingCoupon FROM ShoppingCoupon shoppingCoupon WHERE ");
569 
570             query.append("shoppingCoupon.groupId = ?");
571 
572             query.append(" ");
573 
574             if (obc != null) {
575                 query.append("ORDER BY ");
576 
577                 String[] orderByFields = obc.getOrderByFields();
578 
579                 for (int i = 0; i < orderByFields.length; i++) {
580                     query.append("shoppingCoupon.");
581                     query.append(orderByFields[i]);
582 
583                     if (obc.isAscending()) {
584                         query.append(" ASC");
585                     }
586                     else {
587                         query.append(" DESC");
588                     }
589 
590                     if ((i + 1) < orderByFields.length) {
591                         query.append(", ");
592                     }
593                 }
594             }
595 
596             else {
597                 query.append("ORDER BY ");
598 
599                 query.append("shoppingCoupon.createDate ASC");
600             }
601 
602             Query q = session.createQuery(query.toString());
603 
604             QueryPos qPos = QueryPos.getInstance(q);
605 
606             qPos.add(groupId);
607 
608             Object[] objArray = QueryUtil.getPrevAndNext(q, count, obc,
609                     shoppingCoupon);
610 
611             ShoppingCoupon[] array = new ShoppingCouponImpl[3];
612 
613             array[0] = (ShoppingCoupon)objArray[0];
614             array[1] = (ShoppingCoupon)objArray[1];
615             array[2] = (ShoppingCoupon)objArray[2];
616 
617             return array;
618         }
619         catch (Exception e) {
620             throw processException(e);
621         }
622         finally {
623             closeSession(session);
624         }
625     }
626 
627     public ShoppingCoupon findByCode(String code)
628         throws NoSuchCouponException, SystemException {
629         ShoppingCoupon shoppingCoupon = fetchByCode(code);
630 
631         if (shoppingCoupon == null) {
632             StringBuilder msg = new StringBuilder();
633 
634             msg.append("No ShoppingCoupon exists with the key {");
635 
636             msg.append("code=" + code);
637 
638             msg.append(StringPool.CLOSE_CURLY_BRACE);
639 
640             if (_log.isWarnEnabled()) {
641                 _log.warn(msg.toString());
642             }
643 
644             throw new NoSuchCouponException(msg.toString());
645         }
646 
647         return shoppingCoupon;
648     }
649 
650     public ShoppingCoupon fetchByCode(String code) throws SystemException {
651         return fetchByCode(code, true);
652     }
653 
654     public ShoppingCoupon fetchByCode(String code, boolean retrieveFromCache)
655         throws SystemException {
656         Object[] finderArgs = new Object[] { code };
657 
658         Object result = null;
659 
660         if (retrieveFromCache) {
661             result = FinderCacheUtil.getResult(FINDER_PATH_FETCH_BY_CODE,
662                     finderArgs, this);
663         }
664 
665         if (result == null) {
666             Session session = null;
667 
668             try {
669                 session = openSession();
670 
671                 StringBuilder query = new StringBuilder();
672 
673                 query.append(
674                     "SELECT shoppingCoupon FROM ShoppingCoupon shoppingCoupon WHERE ");
675 
676                 if (code == null) {
677                     query.append("shoppingCoupon.code IS NULL");
678                 }
679                 else {
680                     query.append("shoppingCoupon.code = ?");
681                 }
682 
683                 query.append(" ");
684 
685                 query.append("ORDER BY ");
686 
687                 query.append("shoppingCoupon.createDate ASC");
688 
689                 Query q = session.createQuery(query.toString());
690 
691                 QueryPos qPos = QueryPos.getInstance(q);
692 
693                 if (code != null) {
694                     qPos.add(code);
695                 }
696 
697                 List<ShoppingCoupon> list = q.list();
698 
699                 result = list;
700 
701                 ShoppingCoupon shoppingCoupon = null;
702 
703                 if (list.isEmpty()) {
704                     FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_CODE,
705                         finderArgs, list);
706                 }
707                 else {
708                     shoppingCoupon = list.get(0);
709 
710                     cacheResult(shoppingCoupon);
711 
712                     if ((shoppingCoupon.getCode() == null) ||
713                             !shoppingCoupon.getCode().equals(code)) {
714                         FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_CODE,
715                             finderArgs, shoppingCoupon);
716                     }
717                 }
718 
719                 return shoppingCoupon;
720             }
721             catch (Exception e) {
722                 throw processException(e);
723             }
724             finally {
725                 if (result == null) {
726                     FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_CODE,
727                         finderArgs, new ArrayList<ShoppingCoupon>());
728                 }
729 
730                 closeSession(session);
731             }
732         }
733         else {
734             if (result instanceof List) {
735                 return null;
736             }
737             else {
738                 return (ShoppingCoupon)result;
739             }
740         }
741     }
742 
743     public List<Object> findWithDynamicQuery(DynamicQuery dynamicQuery)
744         throws SystemException {
745         Session session = null;
746 
747         try {
748             session = openSession();
749 
750             dynamicQuery.compile(session);
751 
752             return dynamicQuery.list();
753         }
754         catch (Exception e) {
755             throw processException(e);
756         }
757         finally {
758             closeSession(session);
759         }
760     }
761 
762     public List<Object> findWithDynamicQuery(DynamicQuery dynamicQuery,
763         int start, int end) throws SystemException {
764         Session session = null;
765 
766         try {
767             session = openSession();
768 
769             dynamicQuery.setLimit(start, end);
770 
771             dynamicQuery.compile(session);
772 
773             return dynamicQuery.list();
774         }
775         catch (Exception e) {
776             throw processException(e);
777         }
778         finally {
779             closeSession(session);
780         }
781     }
782 
783     public List<ShoppingCoupon> findAll() throws SystemException {
784         return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
785     }
786 
787     public List<ShoppingCoupon> findAll(int start, int end)
788         throws SystemException {
789         return findAll(start, end, null);
790     }
791 
792     public List<ShoppingCoupon> findAll(int start, int end,
793         OrderByComparator obc) throws SystemException {
794         Object[] finderArgs = new Object[] {
795                 String.valueOf(start), String.valueOf(end), String.valueOf(obc)
796             };
797 
798         List<ShoppingCoupon> list = (List<ShoppingCoupon>)FinderCacheUtil.getResult(FINDER_PATH_FIND_ALL,
799                 finderArgs, this);
800 
801         if (list == null) {
802             Session session = null;
803 
804             try {
805                 session = openSession();
806 
807                 StringBuilder query = new StringBuilder();
808 
809                 query.append(
810                     "SELECT shoppingCoupon FROM ShoppingCoupon shoppingCoupon ");
811 
812                 if (obc != null) {
813                     query.append("ORDER BY ");
814 
815                     String[] orderByFields = obc.getOrderByFields();
816 
817                     for (int i = 0; i < orderByFields.length; i++) {
818                         query.append("shoppingCoupon.");
819                         query.append(orderByFields[i]);
820 
821                         if (obc.isAscending()) {
822                             query.append(" ASC");
823                         }
824                         else {
825                             query.append(" DESC");
826                         }
827 
828                         if ((i + 1) < orderByFields.length) {
829                             query.append(", ");
830                         }
831                     }
832                 }
833 
834                 else {
835                     query.append("ORDER BY ");
836 
837                     query.append("shoppingCoupon.createDate ASC");
838                 }
839 
840                 Query q = session.createQuery(query.toString());
841 
842                 if (obc == null) {
843                     list = (List<ShoppingCoupon>)QueryUtil.list(q,
844                             getDialect(), start, end, false);
845 
846                     Collections.sort(list);
847                 }
848                 else {
849                     list = (List<ShoppingCoupon>)QueryUtil.list(q,
850                             getDialect(), start, end);
851                 }
852             }
853             catch (Exception e) {
854                 throw processException(e);
855             }
856             finally {
857                 if (list == null) {
858                     list = new ArrayList<ShoppingCoupon>();
859                 }
860 
861                 cacheResult(list);
862 
863                 FinderCacheUtil.putResult(FINDER_PATH_FIND_ALL, finderArgs, list);
864 
865                 closeSession(session);
866             }
867         }
868 
869         return list;
870     }
871 
872     public void removeByGroupId(long groupId) throws SystemException {
873         for (ShoppingCoupon shoppingCoupon : findByGroupId(groupId)) {
874             remove(shoppingCoupon);
875         }
876     }
877 
878     public void removeByCode(String code)
879         throws NoSuchCouponException, SystemException {
880         ShoppingCoupon shoppingCoupon = findByCode(code);
881 
882         remove(shoppingCoupon);
883     }
884 
885     public void removeAll() throws SystemException {
886         for (ShoppingCoupon shoppingCoupon : findAll()) {
887             remove(shoppingCoupon);
888         }
889     }
890 
891     public int countByGroupId(long groupId) throws SystemException {
892         Object[] finderArgs = new Object[] { new Long(groupId) };
893 
894         Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_GROUPID,
895                 finderArgs, this);
896 
897         if (count == null) {
898             Session session = null;
899 
900             try {
901                 session = openSession();
902 
903                 StringBuilder query = new StringBuilder();
904 
905                 query.append("SELECT COUNT(shoppingCoupon) ");
906                 query.append("FROM ShoppingCoupon shoppingCoupon WHERE ");
907 
908                 query.append("shoppingCoupon.groupId = ?");
909 
910                 query.append(" ");
911 
912                 Query q = session.createQuery(query.toString());
913 
914                 QueryPos qPos = QueryPos.getInstance(q);
915 
916                 qPos.add(groupId);
917 
918                 count = (Long)q.uniqueResult();
919             }
920             catch (Exception e) {
921                 throw processException(e);
922             }
923             finally {
924                 if (count == null) {
925                     count = Long.valueOf(0);
926                 }
927 
928                 FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_GROUPID,
929                     finderArgs, count);
930 
931                 closeSession(session);
932             }
933         }
934 
935         return count.intValue();
936     }
937 
938     public int countByCode(String code) throws SystemException {
939         Object[] finderArgs = new Object[] { code };
940 
941         Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_CODE,
942                 finderArgs, this);
943 
944         if (count == null) {
945             Session session = null;
946 
947             try {
948                 session = openSession();
949 
950                 StringBuilder query = new StringBuilder();
951 
952                 query.append("SELECT COUNT(shoppingCoupon) ");
953                 query.append("FROM ShoppingCoupon shoppingCoupon WHERE ");
954 
955                 if (code == null) {
956                     query.append("shoppingCoupon.code IS NULL");
957                 }
958                 else {
959                     query.append("shoppingCoupon.code = ?");
960                 }
961 
962                 query.append(" ");
963 
964                 Query q = session.createQuery(query.toString());
965 
966                 QueryPos qPos = QueryPos.getInstance(q);
967 
968                 if (code != null) {
969                     qPos.add(code);
970                 }
971 
972                 count = (Long)q.uniqueResult();
973             }
974             catch (Exception e) {
975                 throw processException(e);
976             }
977             finally {
978                 if (count == null) {
979                     count = Long.valueOf(0);
980                 }
981 
982                 FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_CODE,
983                     finderArgs, count);
984 
985                 closeSession(session);
986             }
987         }
988 
989         return count.intValue();
990     }
991 
992     public int countAll() throws SystemException {
993         Object[] finderArgs = new Object[0];
994 
995         Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
996                 finderArgs, this);
997 
998         if (count == null) {
999             Session session = null;
1000
1001            try {
1002                session = openSession();
1003
1004                Query q = session.createQuery(
1005                        "SELECT COUNT(shoppingCoupon) FROM ShoppingCoupon shoppingCoupon");
1006
1007                count = (Long)q.uniqueResult();
1008            }
1009            catch (Exception e) {
1010                throw processException(e);
1011            }
1012            finally {
1013                if (count == null) {
1014                    count = Long.valueOf(0);
1015                }
1016
1017                FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL, finderArgs,
1018                    count);
1019
1020                closeSession(session);
1021            }
1022        }
1023
1024        return count.intValue();
1025    }
1026
1027    public void afterPropertiesSet() {
1028        String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
1029                    com.liferay.portal.util.PropsUtil.get(
1030                        "value.object.listener.com.liferay.portlet.shopping.model.ShoppingCoupon")));
1031
1032        if (listenerClassNames.length > 0) {
1033            try {
1034                List<ModelListener<ShoppingCoupon>> listenersList = new ArrayList<ModelListener<ShoppingCoupon>>();
1035
1036                for (String listenerClassName : listenerClassNames) {
1037                    listenersList.add((ModelListener<ShoppingCoupon>)Class.forName(
1038                            listenerClassName).newInstance());
1039                }
1040
1041                listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
1042            }
1043            catch (Exception e) {
1044                _log.error(e);
1045            }
1046        }
1047    }
1048
1049    @BeanReference(name = "com.liferay.portlet.shopping.service.persistence.ShoppingCartPersistence.impl")
1050    protected com.liferay.portlet.shopping.service.persistence.ShoppingCartPersistence shoppingCartPersistence;
1051    @BeanReference(name = "com.liferay.portlet.shopping.service.persistence.ShoppingCategoryPersistence.impl")
1052    protected com.liferay.portlet.shopping.service.persistence.ShoppingCategoryPersistence shoppingCategoryPersistence;
1053    @BeanReference(name = "com.liferay.portlet.shopping.service.persistence.ShoppingCouponPersistence.impl")
1054    protected com.liferay.portlet.shopping.service.persistence.ShoppingCouponPersistence shoppingCouponPersistence;
1055    @BeanReference(name = "com.liferay.portlet.shopping.service.persistence.ShoppingItemPersistence.impl")
1056    protected com.liferay.portlet.shopping.service.persistence.ShoppingItemPersistence shoppingItemPersistence;
1057    @BeanReference(name = "com.liferay.portlet.shopping.service.persistence.ShoppingItemFieldPersistence.impl")
1058    protected com.liferay.portlet.shopping.service.persistence.ShoppingItemFieldPersistence shoppingItemFieldPersistence;
1059    @BeanReference(name = "com.liferay.portlet.shopping.service.persistence.ShoppingItemPricePersistence.impl")
1060    protected com.liferay.portlet.shopping.service.persistence.ShoppingItemPricePersistence shoppingItemPricePersistence;
1061    @BeanReference(name = "com.liferay.portlet.shopping.service.persistence.ShoppingOrderPersistence.impl")
1062    protected com.liferay.portlet.shopping.service.persistence.ShoppingOrderPersistence shoppingOrderPersistence;
1063    @BeanReference(name = "com.liferay.portlet.shopping.service.persistence.ShoppingOrderItemPersistence.impl")
1064    protected com.liferay.portlet.shopping.service.persistence.ShoppingOrderItemPersistence shoppingOrderItemPersistence;
1065    @BeanReference(name = "com.liferay.portal.service.persistence.UserPersistence.impl")
1066    protected com.liferay.portal.service.persistence.UserPersistence userPersistence;
1067    private static Log _log = LogFactoryUtil.getLog(ShoppingCouponPersistenceImpl.class);
1068}