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         shoppingCoupon = toUnwrappedModel(shoppingCoupon);
194 
195         Session session = null;
196 
197         try {
198             session = openSession();
199 
200             if (shoppingCoupon.isCachedModel() || BatchSessionUtil.isEnabled()) {
201                 Object staleObject = session.get(ShoppingCouponImpl.class,
202                         shoppingCoupon.getPrimaryKeyObj());
203 
204                 if (staleObject != null) {
205                     session.evict(staleObject);
206                 }
207             }
208 
209             session.delete(shoppingCoupon);
210 
211             session.flush();
212         }
213         catch (Exception e) {
214             throw processException(e);
215         }
216         finally {
217             closeSession(session);
218         }
219 
220         FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
221 
222         ShoppingCouponModelImpl shoppingCouponModelImpl = (ShoppingCouponModelImpl)shoppingCoupon;
223 
224         FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_CODE,
225             new Object[] { shoppingCouponModelImpl.getOriginalCode() });
226 
227         EntityCacheUtil.removeResult(ShoppingCouponModelImpl.ENTITY_CACHE_ENABLED,
228             ShoppingCouponImpl.class, shoppingCoupon.getPrimaryKey());
229 
230         return shoppingCoupon;
231     }
232 
233     /**
234      * @deprecated Use {@link #update(ShoppingCoupon, boolean merge)}.
235      */
236     public ShoppingCoupon update(ShoppingCoupon shoppingCoupon)
237         throws SystemException {
238         if (_log.isWarnEnabled()) {
239             _log.warn(
240                 "Using the deprecated update(ShoppingCoupon shoppingCoupon) method. Use update(ShoppingCoupon shoppingCoupon, boolean merge) instead.");
241         }
242 
243         return update(shoppingCoupon, false);
244     }
245 
246     /**
247      * Add, update, or merge, the entity. This method also calls the model
248      * listeners to trigger the proper events associated with adding, deleting,
249      * or updating an entity.
250      *
251      * @param  shoppingCoupon the entity to add, update, or merge
252      * @param  merge boolean value for whether to merge the entity. The default
253      *         value is false. Setting merge to true is more expensive and
254      *         should only be true when shoppingCoupon is transient. See
255      *         LEP-5473 for a detailed discussion of this method.
256      * @return the entity that was added, updated, or merged
257      */
258     public ShoppingCoupon update(ShoppingCoupon shoppingCoupon, boolean merge)
259         throws SystemException {
260         boolean isNew = shoppingCoupon.isNew();
261 
262         for (ModelListener<ShoppingCoupon> listener : listeners) {
263             if (isNew) {
264                 listener.onBeforeCreate(shoppingCoupon);
265             }
266             else {
267                 listener.onBeforeUpdate(shoppingCoupon);
268             }
269         }
270 
271         shoppingCoupon = updateImpl(shoppingCoupon, merge);
272 
273         for (ModelListener<ShoppingCoupon> listener : listeners) {
274             if (isNew) {
275                 listener.onAfterCreate(shoppingCoupon);
276             }
277             else {
278                 listener.onAfterUpdate(shoppingCoupon);
279             }
280         }
281 
282         return shoppingCoupon;
283     }
284 
285     public ShoppingCoupon updateImpl(
286         com.liferay.portlet.shopping.model.ShoppingCoupon shoppingCoupon,
287         boolean merge) throws SystemException {
288         shoppingCoupon = toUnwrappedModel(shoppingCoupon);
289 
290         boolean isNew = shoppingCoupon.isNew();
291 
292         ShoppingCouponModelImpl shoppingCouponModelImpl = (ShoppingCouponModelImpl)shoppingCoupon;
293 
294         Session session = null;
295 
296         try {
297             session = openSession();
298 
299             BatchSessionUtil.update(session, shoppingCoupon, merge);
300 
301             shoppingCoupon.setNew(false);
302         }
303         catch (Exception e) {
304             throw processException(e);
305         }
306         finally {
307             closeSession(session);
308         }
309 
310         FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
311 
312         EntityCacheUtil.putResult(ShoppingCouponModelImpl.ENTITY_CACHE_ENABLED,
313             ShoppingCouponImpl.class, shoppingCoupon.getPrimaryKey(),
314             shoppingCoupon);
315 
316         if (!isNew &&
317                 (!Validator.equals(shoppingCoupon.getCode(),
318                     shoppingCouponModelImpl.getOriginalCode()))) {
319             FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_CODE,
320                 new Object[] { shoppingCouponModelImpl.getOriginalCode() });
321         }
322 
323         if (isNew ||
324                 (!Validator.equals(shoppingCoupon.getCode(),
325                     shoppingCouponModelImpl.getOriginalCode()))) {
326             FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_CODE,
327                 new Object[] { shoppingCoupon.getCode() }, shoppingCoupon);
328         }
329 
330         return shoppingCoupon;
331     }
332 
333     protected ShoppingCoupon toUnwrappedModel(ShoppingCoupon shoppingCoupon) {
334         if (shoppingCoupon instanceof ShoppingCouponImpl) {
335             return shoppingCoupon;
336         }
337 
338         ShoppingCouponImpl shoppingCouponImpl = new ShoppingCouponImpl();
339 
340         shoppingCouponImpl.setNew(shoppingCoupon.isNew());
341         shoppingCouponImpl.setPrimaryKey(shoppingCoupon.getPrimaryKey());
342 
343         shoppingCouponImpl.setCouponId(shoppingCoupon.getCouponId());
344         shoppingCouponImpl.setGroupId(shoppingCoupon.getGroupId());
345         shoppingCouponImpl.setCompanyId(shoppingCoupon.getCompanyId());
346         shoppingCouponImpl.setUserId(shoppingCoupon.getUserId());
347         shoppingCouponImpl.setUserName(shoppingCoupon.getUserName());
348         shoppingCouponImpl.setCreateDate(shoppingCoupon.getCreateDate());
349         shoppingCouponImpl.setModifiedDate(shoppingCoupon.getModifiedDate());
350         shoppingCouponImpl.setCode(shoppingCoupon.getCode());
351         shoppingCouponImpl.setName(shoppingCoupon.getName());
352         shoppingCouponImpl.setDescription(shoppingCoupon.getDescription());
353         shoppingCouponImpl.setStartDate(shoppingCoupon.getStartDate());
354         shoppingCouponImpl.setEndDate(shoppingCoupon.getEndDate());
355         shoppingCouponImpl.setActive(shoppingCoupon.isActive());
356         shoppingCouponImpl.setLimitCategories(shoppingCoupon.getLimitCategories());
357         shoppingCouponImpl.setLimitSkus(shoppingCoupon.getLimitSkus());
358         shoppingCouponImpl.setMinOrder(shoppingCoupon.getMinOrder());
359         shoppingCouponImpl.setDiscount(shoppingCoupon.getDiscount());
360         shoppingCouponImpl.setDiscountType(shoppingCoupon.getDiscountType());
361 
362         return shoppingCouponImpl;
363     }
364 
365     public ShoppingCoupon findByPrimaryKey(long couponId)
366         throws NoSuchCouponException, SystemException {
367         ShoppingCoupon shoppingCoupon = fetchByPrimaryKey(couponId);
368 
369         if (shoppingCoupon == null) {
370             if (_log.isWarnEnabled()) {
371                 _log.warn("No ShoppingCoupon exists with the primary key " +
372                     couponId);
373             }
374 
375             throw new NoSuchCouponException(
376                 "No ShoppingCoupon exists with the primary key " + couponId);
377         }
378 
379         return shoppingCoupon;
380     }
381 
382     public ShoppingCoupon fetchByPrimaryKey(long couponId)
383         throws SystemException {
384         ShoppingCoupon shoppingCoupon = (ShoppingCoupon)EntityCacheUtil.getResult(ShoppingCouponModelImpl.ENTITY_CACHE_ENABLED,
385                 ShoppingCouponImpl.class, couponId, this);
386 
387         if (shoppingCoupon == null) {
388             Session session = null;
389 
390             try {
391                 session = openSession();
392 
393                 shoppingCoupon = (ShoppingCoupon)session.get(ShoppingCouponImpl.class,
394                         new Long(couponId));
395             }
396             catch (Exception e) {
397                 throw processException(e);
398             }
399             finally {
400                 if (shoppingCoupon != null) {
401                     cacheResult(shoppingCoupon);
402                 }
403 
404                 closeSession(session);
405             }
406         }
407 
408         return shoppingCoupon;
409     }
410 
411     public List<ShoppingCoupon> findByGroupId(long groupId)
412         throws SystemException {
413         Object[] finderArgs = new Object[] { new Long(groupId) };
414 
415         List<ShoppingCoupon> list = (List<ShoppingCoupon>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_GROUPID,
416                 finderArgs, this);
417 
418         if (list == null) {
419             Session session = null;
420 
421             try {
422                 session = openSession();
423 
424                 StringBuilder query = new StringBuilder();
425 
426                 query.append(
427                     "SELECT shoppingCoupon FROM ShoppingCoupon shoppingCoupon WHERE ");
428 
429                 query.append("shoppingCoupon.groupId = ?");
430 
431                 query.append(" ");
432 
433                 query.append("ORDER BY ");
434 
435                 query.append("shoppingCoupon.createDate ASC");
436 
437                 Query q = session.createQuery(query.toString());
438 
439                 QueryPos qPos = QueryPos.getInstance(q);
440 
441                 qPos.add(groupId);
442 
443                 list = q.list();
444             }
445             catch (Exception e) {
446                 throw processException(e);
447             }
448             finally {
449                 if (list == null) {
450                     list = new ArrayList<ShoppingCoupon>();
451                 }
452 
453                 cacheResult(list);
454 
455                 FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_GROUPID,
456                     finderArgs, list);
457 
458                 closeSession(session);
459             }
460         }
461 
462         return list;
463     }
464 
465     public List<ShoppingCoupon> findByGroupId(long groupId, int start, int end)
466         throws SystemException {
467         return findByGroupId(groupId, start, end, null);
468     }
469 
470     public List<ShoppingCoupon> findByGroupId(long groupId, int start, int end,
471         OrderByComparator obc) throws SystemException {
472         Object[] finderArgs = new Object[] {
473                 new Long(groupId),
474                 
475                 String.valueOf(start), String.valueOf(end), String.valueOf(obc)
476             };
477 
478         List<ShoppingCoupon> list = (List<ShoppingCoupon>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_OBC_GROUPID,
479                 finderArgs, this);
480 
481         if (list == null) {
482             Session session = null;
483 
484             try {
485                 session = openSession();
486 
487                 StringBuilder query = new StringBuilder();
488 
489                 query.append(
490                     "SELECT shoppingCoupon FROM ShoppingCoupon shoppingCoupon WHERE ");
491 
492                 query.append("shoppingCoupon.groupId = ?");
493 
494                 query.append(" ");
495 
496                 if (obc != null) {
497                     query.append("ORDER BY ");
498 
499                     String[] orderByFields = obc.getOrderByFields();
500 
501                     for (int i = 0; i < orderByFields.length; i++) {
502                         query.append("shoppingCoupon.");
503                         query.append(orderByFields[i]);
504 
505                         if (obc.isAscending()) {
506                             query.append(" ASC");
507                         }
508                         else {
509                             query.append(" DESC");
510                         }
511 
512                         if ((i + 1) < orderByFields.length) {
513                             query.append(", ");
514                         }
515                     }
516                 }
517 
518                 else {
519                     query.append("ORDER BY ");
520 
521                     query.append("shoppingCoupon.createDate ASC");
522                 }
523 
524                 Query q = session.createQuery(query.toString());
525 
526                 QueryPos qPos = QueryPos.getInstance(q);
527 
528                 qPos.add(groupId);
529 
530                 list = (List<ShoppingCoupon>)QueryUtil.list(q, getDialect(),
531                         start, end);
532             }
533             catch (Exception e) {
534                 throw processException(e);
535             }
536             finally {
537                 if (list == null) {
538                     list = new ArrayList<ShoppingCoupon>();
539                 }
540 
541                 cacheResult(list);
542 
543                 FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_OBC_GROUPID,
544                     finderArgs, list);
545 
546                 closeSession(session);
547             }
548         }
549 
550         return list;
551     }
552 
553     public ShoppingCoupon findByGroupId_First(long groupId,
554         OrderByComparator obc) throws NoSuchCouponException, SystemException {
555         List<ShoppingCoupon> list = findByGroupId(groupId, 0, 1, obc);
556 
557         if (list.isEmpty()) {
558             StringBuilder msg = new StringBuilder();
559 
560             msg.append("No ShoppingCoupon exists with the key {");
561 
562             msg.append("groupId=" + groupId);
563 
564             msg.append(StringPool.CLOSE_CURLY_BRACE);
565 
566             throw new NoSuchCouponException(msg.toString());
567         }
568         else {
569             return list.get(0);
570         }
571     }
572 
573     public ShoppingCoupon findByGroupId_Last(long groupId, OrderByComparator obc)
574         throws NoSuchCouponException, SystemException {
575         int count = countByGroupId(groupId);
576 
577         List<ShoppingCoupon> list = findByGroupId(groupId, count - 1, count, obc);
578 
579         if (list.isEmpty()) {
580             StringBuilder msg = new StringBuilder();
581 
582             msg.append("No ShoppingCoupon exists with the key {");
583 
584             msg.append("groupId=" + groupId);
585 
586             msg.append(StringPool.CLOSE_CURLY_BRACE);
587 
588             throw new NoSuchCouponException(msg.toString());
589         }
590         else {
591             return list.get(0);
592         }
593     }
594 
595     public ShoppingCoupon[] findByGroupId_PrevAndNext(long couponId,
596         long groupId, OrderByComparator obc)
597         throws NoSuchCouponException, SystemException {
598         ShoppingCoupon shoppingCoupon = findByPrimaryKey(couponId);
599 
600         int count = countByGroupId(groupId);
601 
602         Session session = null;
603 
604         try {
605             session = openSession();
606 
607             StringBuilder query = new StringBuilder();
608 
609             query.append(
610                 "SELECT shoppingCoupon FROM ShoppingCoupon shoppingCoupon WHERE ");
611 
612             query.append("shoppingCoupon.groupId = ?");
613 
614             query.append(" ");
615 
616             if (obc != null) {
617                 query.append("ORDER BY ");
618 
619                 String[] orderByFields = obc.getOrderByFields();
620 
621                 for (int i = 0; i < orderByFields.length; i++) {
622                     query.append("shoppingCoupon.");
623                     query.append(orderByFields[i]);
624 
625                     if (obc.isAscending()) {
626                         query.append(" ASC");
627                     }
628                     else {
629                         query.append(" DESC");
630                     }
631 
632                     if ((i + 1) < orderByFields.length) {
633                         query.append(", ");
634                     }
635                 }
636             }
637 
638             else {
639                 query.append("ORDER BY ");
640 
641                 query.append("shoppingCoupon.createDate ASC");
642             }
643 
644             Query q = session.createQuery(query.toString());
645 
646             QueryPos qPos = QueryPos.getInstance(q);
647 
648             qPos.add(groupId);
649 
650             Object[] objArray = QueryUtil.getPrevAndNext(q, count, obc,
651                     shoppingCoupon);
652 
653             ShoppingCoupon[] array = new ShoppingCouponImpl[3];
654 
655             array[0] = (ShoppingCoupon)objArray[0];
656             array[1] = (ShoppingCoupon)objArray[1];
657             array[2] = (ShoppingCoupon)objArray[2];
658 
659             return array;
660         }
661         catch (Exception e) {
662             throw processException(e);
663         }
664         finally {
665             closeSession(session);
666         }
667     }
668 
669     public ShoppingCoupon findByCode(String code)
670         throws NoSuchCouponException, SystemException {
671         ShoppingCoupon shoppingCoupon = fetchByCode(code);
672 
673         if (shoppingCoupon == null) {
674             StringBuilder msg = new StringBuilder();
675 
676             msg.append("No ShoppingCoupon exists with the key {");
677 
678             msg.append("code=" + code);
679 
680             msg.append(StringPool.CLOSE_CURLY_BRACE);
681 
682             if (_log.isWarnEnabled()) {
683                 _log.warn(msg.toString());
684             }
685 
686             throw new NoSuchCouponException(msg.toString());
687         }
688 
689         return shoppingCoupon;
690     }
691 
692     public ShoppingCoupon fetchByCode(String code) throws SystemException {
693         return fetchByCode(code, true);
694     }
695 
696     public ShoppingCoupon fetchByCode(String code, boolean retrieveFromCache)
697         throws SystemException {
698         Object[] finderArgs = new Object[] { code };
699 
700         Object result = null;
701 
702         if (retrieveFromCache) {
703             result = FinderCacheUtil.getResult(FINDER_PATH_FETCH_BY_CODE,
704                     finderArgs, this);
705         }
706 
707         if (result == null) {
708             Session session = null;
709 
710             try {
711                 session = openSession();
712 
713                 StringBuilder query = new StringBuilder();
714 
715                 query.append(
716                     "SELECT shoppingCoupon FROM ShoppingCoupon shoppingCoupon WHERE ");
717 
718                 if (code == null) {
719                     query.append("shoppingCoupon.code IS NULL");
720                 }
721                 else {
722                     query.append("shoppingCoupon.code = ?");
723                 }
724 
725                 query.append(" ");
726 
727                 query.append("ORDER BY ");
728 
729                 query.append("shoppingCoupon.createDate ASC");
730 
731                 Query q = session.createQuery(query.toString());
732 
733                 QueryPos qPos = QueryPos.getInstance(q);
734 
735                 if (code != null) {
736                     qPos.add(code);
737                 }
738 
739                 List<ShoppingCoupon> list = q.list();
740 
741                 result = list;
742 
743                 ShoppingCoupon shoppingCoupon = null;
744 
745                 if (list.isEmpty()) {
746                     FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_CODE,
747                         finderArgs, list);
748                 }
749                 else {
750                     shoppingCoupon = list.get(0);
751 
752                     cacheResult(shoppingCoupon);
753 
754                     if ((shoppingCoupon.getCode() == null) ||
755                             !shoppingCoupon.getCode().equals(code)) {
756                         FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_CODE,
757                             finderArgs, shoppingCoupon);
758                     }
759                 }
760 
761                 return shoppingCoupon;
762             }
763             catch (Exception e) {
764                 throw processException(e);
765             }
766             finally {
767                 if (result == null) {
768                     FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_CODE,
769                         finderArgs, new ArrayList<ShoppingCoupon>());
770                 }
771 
772                 closeSession(session);
773             }
774         }
775         else {
776             if (result instanceof List<?>) {
777                 return null;
778             }
779             else {
780                 return (ShoppingCoupon)result;
781             }
782         }
783     }
784 
785     public List<Object> findWithDynamicQuery(DynamicQuery dynamicQuery)
786         throws SystemException {
787         Session session = null;
788 
789         try {
790             session = openSession();
791 
792             dynamicQuery.compile(session);
793 
794             return dynamicQuery.list();
795         }
796         catch (Exception e) {
797             throw processException(e);
798         }
799         finally {
800             closeSession(session);
801         }
802     }
803 
804     public List<Object> findWithDynamicQuery(DynamicQuery dynamicQuery,
805         int start, int end) throws SystemException {
806         Session session = null;
807 
808         try {
809             session = openSession();
810 
811             dynamicQuery.setLimit(start, end);
812 
813             dynamicQuery.compile(session);
814 
815             return dynamicQuery.list();
816         }
817         catch (Exception e) {
818             throw processException(e);
819         }
820         finally {
821             closeSession(session);
822         }
823     }
824 
825     public List<ShoppingCoupon> findAll() throws SystemException {
826         return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
827     }
828 
829     public List<ShoppingCoupon> findAll(int start, int end)
830         throws SystemException {
831         return findAll(start, end, null);
832     }
833 
834     public List<ShoppingCoupon> findAll(int start, int end,
835         OrderByComparator obc) throws SystemException {
836         Object[] finderArgs = new Object[] {
837                 String.valueOf(start), String.valueOf(end), String.valueOf(obc)
838             };
839 
840         List<ShoppingCoupon> list = (List<ShoppingCoupon>)FinderCacheUtil.getResult(FINDER_PATH_FIND_ALL,
841                 finderArgs, this);
842 
843         if (list == null) {
844             Session session = null;
845 
846             try {
847                 session = openSession();
848 
849                 StringBuilder query = new StringBuilder();
850 
851                 query.append(
852                     "SELECT shoppingCoupon FROM ShoppingCoupon shoppingCoupon ");
853 
854                 if (obc != null) {
855                     query.append("ORDER BY ");
856 
857                     String[] orderByFields = obc.getOrderByFields();
858 
859                     for (int i = 0; i < orderByFields.length; i++) {
860                         query.append("shoppingCoupon.");
861                         query.append(orderByFields[i]);
862 
863                         if (obc.isAscending()) {
864                             query.append(" ASC");
865                         }
866                         else {
867                             query.append(" DESC");
868                         }
869 
870                         if ((i + 1) < orderByFields.length) {
871                             query.append(", ");
872                         }
873                     }
874                 }
875 
876                 else {
877                     query.append("ORDER BY ");
878 
879                     query.append("shoppingCoupon.createDate ASC");
880                 }
881 
882                 Query q = session.createQuery(query.toString());
883 
884                 if (obc == null) {
885                     list = (List<ShoppingCoupon>)QueryUtil.list(q,
886                             getDialect(), start, end, false);
887 
888                     Collections.sort(list);
889                 }
890                 else {
891                     list = (List<ShoppingCoupon>)QueryUtil.list(q,
892                             getDialect(), start, end);
893                 }
894             }
895             catch (Exception e) {
896                 throw processException(e);
897             }
898             finally {
899                 if (list == null) {
900                     list = new ArrayList<ShoppingCoupon>();
901                 }
902 
903                 cacheResult(list);
904 
905                 FinderCacheUtil.putResult(FINDER_PATH_FIND_ALL, finderArgs, list);
906 
907                 closeSession(session);
908             }
909         }
910 
911         return list;
912     }
913 
914     public void removeByGroupId(long groupId) throws SystemException {
915         for (ShoppingCoupon shoppingCoupon : findByGroupId(groupId)) {
916             remove(shoppingCoupon);
917         }
918     }
919 
920     public void removeByCode(String code)
921         throws NoSuchCouponException, SystemException {
922         ShoppingCoupon shoppingCoupon = findByCode(code);
923 
924         remove(shoppingCoupon);
925     }
926 
927     public void removeAll() throws SystemException {
928         for (ShoppingCoupon shoppingCoupon : findAll()) {
929             remove(shoppingCoupon);
930         }
931     }
932 
933     public int countByGroupId(long groupId) throws SystemException {
934         Object[] finderArgs = new Object[] { new Long(groupId) };
935 
936         Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_GROUPID,
937                 finderArgs, this);
938 
939         if (count == null) {
940             Session session = null;
941 
942             try {
943                 session = openSession();
944 
945                 StringBuilder query = new StringBuilder();
946 
947                 query.append("SELECT COUNT(shoppingCoupon) ");
948                 query.append("FROM ShoppingCoupon shoppingCoupon WHERE ");
949 
950                 query.append("shoppingCoupon.groupId = ?");
951 
952                 query.append(" ");
953 
954                 Query q = session.createQuery(query.toString());
955 
956                 QueryPos qPos = QueryPos.getInstance(q);
957 
958                 qPos.add(groupId);
959 
960                 count = (Long)q.uniqueResult();
961             }
962             catch (Exception e) {
963                 throw processException(e);
964             }
965             finally {
966                 if (count == null) {
967                     count = Long.valueOf(0);
968                 }
969 
970                 FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_GROUPID,
971                     finderArgs, count);
972 
973                 closeSession(session);
974             }
975         }
976 
977         return count.intValue();
978     }
979 
980     public int countByCode(String code) throws SystemException {
981         Object[] finderArgs = new Object[] { code };
982 
983         Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_CODE,
984                 finderArgs, this);
985 
986         if (count == null) {
987             Session session = null;
988 
989             try {
990                 session = openSession();
991 
992                 StringBuilder query = new StringBuilder();
993 
994                 query.append("SELECT COUNT(shoppingCoupon) ");
995                 query.append("FROM ShoppingCoupon shoppingCoupon WHERE ");
996 
997                 if (code == null) {
998                     query.append("shoppingCoupon.code IS NULL");
999                 }
1000                else {
1001                    query.append("shoppingCoupon.code = ?");
1002                }
1003
1004                query.append(" ");
1005
1006                Query q = session.createQuery(query.toString());
1007
1008                QueryPos qPos = QueryPos.getInstance(q);
1009
1010                if (code != null) {
1011                    qPos.add(code);
1012                }
1013
1014                count = (Long)q.uniqueResult();
1015            }
1016            catch (Exception e) {
1017                throw processException(e);
1018            }
1019            finally {
1020                if (count == null) {
1021                    count = Long.valueOf(0);
1022                }
1023
1024                FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_CODE,
1025                    finderArgs, count);
1026
1027                closeSession(session);
1028            }
1029        }
1030
1031        return count.intValue();
1032    }
1033
1034    public int countAll() throws SystemException {
1035        Object[] finderArgs = new Object[0];
1036
1037        Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
1038                finderArgs, this);
1039
1040        if (count == null) {
1041            Session session = null;
1042
1043            try {
1044                session = openSession();
1045
1046                Query q = session.createQuery(
1047                        "SELECT COUNT(shoppingCoupon) FROM ShoppingCoupon shoppingCoupon");
1048
1049                count = (Long)q.uniqueResult();
1050            }
1051            catch (Exception e) {
1052                throw processException(e);
1053            }
1054            finally {
1055                if (count == null) {
1056                    count = Long.valueOf(0);
1057                }
1058
1059                FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL, finderArgs,
1060                    count);
1061
1062                closeSession(session);
1063            }
1064        }
1065
1066        return count.intValue();
1067    }
1068
1069    public void afterPropertiesSet() {
1070        String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
1071                    com.liferay.portal.util.PropsUtil.get(
1072                        "value.object.listener.com.liferay.portlet.shopping.model.ShoppingCoupon")));
1073
1074        if (listenerClassNames.length > 0) {
1075            try {
1076                List<ModelListener<ShoppingCoupon>> listenersList = new ArrayList<ModelListener<ShoppingCoupon>>();
1077
1078                for (String listenerClassName : listenerClassNames) {
1079                    listenersList.add((ModelListener<ShoppingCoupon>)Class.forName(
1080                            listenerClassName).newInstance());
1081                }
1082
1083                listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
1084            }
1085            catch (Exception e) {
1086                _log.error(e);
1087            }
1088        }
1089    }
1090
1091    @BeanReference(name = "com.liferay.portlet.shopping.service.persistence.ShoppingCartPersistence.impl")
1092    protected com.liferay.portlet.shopping.service.persistence.ShoppingCartPersistence shoppingCartPersistence;
1093    @BeanReference(name = "com.liferay.portlet.shopping.service.persistence.ShoppingCategoryPersistence.impl")
1094    protected com.liferay.portlet.shopping.service.persistence.ShoppingCategoryPersistence shoppingCategoryPersistence;
1095    @BeanReference(name = "com.liferay.portlet.shopping.service.persistence.ShoppingCouponPersistence.impl")
1096    protected com.liferay.portlet.shopping.service.persistence.ShoppingCouponPersistence shoppingCouponPersistence;
1097    @BeanReference(name = "com.liferay.portlet.shopping.service.persistence.ShoppingItemPersistence.impl")
1098    protected com.liferay.portlet.shopping.service.persistence.ShoppingItemPersistence shoppingItemPersistence;
1099    @BeanReference(name = "com.liferay.portlet.shopping.service.persistence.ShoppingItemFieldPersistence.impl")
1100    protected com.liferay.portlet.shopping.service.persistence.ShoppingItemFieldPersistence shoppingItemFieldPersistence;
1101    @BeanReference(name = "com.liferay.portlet.shopping.service.persistence.ShoppingItemPricePersistence.impl")
1102    protected com.liferay.portlet.shopping.service.persistence.ShoppingItemPricePersistence shoppingItemPricePersistence;
1103    @BeanReference(name = "com.liferay.portlet.shopping.service.persistence.ShoppingOrderPersistence.impl")
1104    protected com.liferay.portlet.shopping.service.persistence.ShoppingOrderPersistence shoppingOrderPersistence;
1105    @BeanReference(name = "com.liferay.portlet.shopping.service.persistence.ShoppingOrderItemPersistence.impl")
1106    protected com.liferay.portlet.shopping.service.persistence.ShoppingOrderItemPersistence shoppingOrderItemPersistence;
1107    @BeanReference(name = "com.liferay.portal.service.persistence.ResourcePersistence.impl")
1108    protected com.liferay.portal.service.persistence.ResourcePersistence resourcePersistence;
1109    @BeanReference(name = "com.liferay.portal.service.persistence.UserPersistence.impl")
1110    protected com.liferay.portal.service.persistence.UserPersistence userPersistence;
1111    private static Log _log = LogFactoryUtil.getLog(ShoppingCouponPersistenceImpl.class);
1112}