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