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