001
014
015 package com.liferay.portlet.shopping.service.persistence;
016
017 import com.liferay.portal.NoSuchModelException;
018 import com.liferay.portal.kernel.annotation.BeanReference;
019 import com.liferay.portal.kernel.cache.CacheRegistryUtil;
020 import com.liferay.portal.kernel.dao.orm.EntityCacheUtil;
021 import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
022 import com.liferay.portal.kernel.dao.orm.FinderPath;
023 import com.liferay.portal.kernel.dao.orm.Query;
024 import com.liferay.portal.kernel.dao.orm.QueryPos;
025 import com.liferay.portal.kernel.dao.orm.QueryUtil;
026 import com.liferay.portal.kernel.dao.orm.Session;
027 import com.liferay.portal.kernel.exception.SystemException;
028 import com.liferay.portal.kernel.log.Log;
029 import com.liferay.portal.kernel.log.LogFactoryUtil;
030 import com.liferay.portal.kernel.util.GetterUtil;
031 import com.liferay.portal.kernel.util.InstanceFactory;
032 import com.liferay.portal.kernel.util.OrderByComparator;
033 import com.liferay.portal.kernel.util.StringBundler;
034 import com.liferay.portal.kernel.util.StringPool;
035 import com.liferay.portal.kernel.util.StringUtil;
036 import com.liferay.portal.kernel.util.Validator;
037 import com.liferay.portal.model.ModelListener;
038 import com.liferay.portal.service.persistence.BatchSessionUtil;
039 import com.liferay.portal.service.persistence.ResourcePersistence;
040 import com.liferay.portal.service.persistence.UserPersistence;
041 import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
042
043 import com.liferay.portlet.shopping.NoSuchCouponException;
044 import com.liferay.portlet.shopping.model.ShoppingCoupon;
045 import com.liferay.portlet.shopping.model.impl.ShoppingCouponImpl;
046 import com.liferay.portlet.shopping.model.impl.ShoppingCouponModelImpl;
047
048 import java.io.Serializable;
049
050 import java.util.ArrayList;
051 import java.util.Collections;
052 import java.util.List;
053
054
070 public class ShoppingCouponPersistenceImpl extends BasePersistenceImpl<ShoppingCoupon>
071 implements ShoppingCouponPersistence {
072 public static final String FINDER_CLASS_NAME_ENTITY = ShoppingCouponImpl.class.getName();
073 public static final String FINDER_CLASS_NAME_LIST = FINDER_CLASS_NAME_ENTITY +
074 ".List";
075 public static final FinderPath FINDER_PATH_FIND_BY_GROUPID = new FinderPath(ShoppingCouponModelImpl.ENTITY_CACHE_ENABLED,
076 ShoppingCouponModelImpl.FINDER_CACHE_ENABLED,
077 FINDER_CLASS_NAME_LIST, "findByGroupId",
078 new String[] {
079 Long.class.getName(),
080
081 "java.lang.Integer", "java.lang.Integer",
082 "com.liferay.portal.kernel.util.OrderByComparator"
083 });
084 public static final FinderPath FINDER_PATH_COUNT_BY_GROUPID = new FinderPath(ShoppingCouponModelImpl.ENTITY_CACHE_ENABLED,
085 ShoppingCouponModelImpl.FINDER_CACHE_ENABLED,
086 FINDER_CLASS_NAME_LIST, "countByGroupId",
087 new String[] { Long.class.getName() });
088 public static final FinderPath FINDER_PATH_FETCH_BY_CODE = new FinderPath(ShoppingCouponModelImpl.ENTITY_CACHE_ENABLED,
089 ShoppingCouponModelImpl.FINDER_CACHE_ENABLED,
090 FINDER_CLASS_NAME_ENTITY, "fetchByCode",
091 new String[] { String.class.getName() });
092 public static final FinderPath FINDER_PATH_COUNT_BY_CODE = new FinderPath(ShoppingCouponModelImpl.ENTITY_CACHE_ENABLED,
093 ShoppingCouponModelImpl.FINDER_CACHE_ENABLED,
094 FINDER_CLASS_NAME_LIST, "countByCode",
095 new String[] { String.class.getName() });
096 public static final FinderPath FINDER_PATH_FIND_ALL = new FinderPath(ShoppingCouponModelImpl.ENTITY_CACHE_ENABLED,
097 ShoppingCouponModelImpl.FINDER_CACHE_ENABLED,
098 FINDER_CLASS_NAME_LIST, "findAll", new String[0]);
099 public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(ShoppingCouponModelImpl.ENTITY_CACHE_ENABLED,
100 ShoppingCouponModelImpl.FINDER_CACHE_ENABLED,
101 FINDER_CLASS_NAME_LIST, "countAll", new String[0]);
102
103
108 public void cacheResult(ShoppingCoupon shoppingCoupon) {
109 EntityCacheUtil.putResult(ShoppingCouponModelImpl.ENTITY_CACHE_ENABLED,
110 ShoppingCouponImpl.class, shoppingCoupon.getPrimaryKey(),
111 shoppingCoupon);
112
113 FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_CODE,
114 new Object[] { shoppingCoupon.getCode() }, shoppingCoupon);
115 }
116
117
122 public void cacheResult(List<ShoppingCoupon> shoppingCoupons) {
123 for (ShoppingCoupon shoppingCoupon : shoppingCoupons) {
124 if (EntityCacheUtil.getResult(
125 ShoppingCouponModelImpl.ENTITY_CACHE_ENABLED,
126 ShoppingCouponImpl.class,
127 shoppingCoupon.getPrimaryKey(), this) == null) {
128 cacheResult(shoppingCoupon);
129 }
130 }
131 }
132
133
140 public void clearCache() {
141 CacheRegistryUtil.clear(ShoppingCouponImpl.class.getName());
142 EntityCacheUtil.clearCache(ShoppingCouponImpl.class.getName());
143 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
144 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
145 }
146
147
154 public void clearCache(ShoppingCoupon shoppingCoupon) {
155 EntityCacheUtil.removeResult(ShoppingCouponModelImpl.ENTITY_CACHE_ENABLED,
156 ShoppingCouponImpl.class, shoppingCoupon.getPrimaryKey());
157
158 FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_CODE,
159 new Object[] { shoppingCoupon.getCode() });
160 }
161
162
168 public ShoppingCoupon create(long couponId) {
169 ShoppingCoupon shoppingCoupon = new ShoppingCouponImpl();
170
171 shoppingCoupon.setNew(true);
172 shoppingCoupon.setPrimaryKey(couponId);
173
174 return shoppingCoupon;
175 }
176
177
185 public ShoppingCoupon remove(Serializable primaryKey)
186 throws NoSuchModelException, SystemException {
187 return remove(((Long)primaryKey).longValue());
188 }
189
190
198 public ShoppingCoupon remove(long couponId)
199 throws NoSuchCouponException, SystemException {
200 Session session = null;
201
202 try {
203 session = openSession();
204
205 ShoppingCoupon shoppingCoupon = (ShoppingCoupon)session.get(ShoppingCouponImpl.class,
206 new Long(couponId));
207
208 if (shoppingCoupon == null) {
209 if (_log.isWarnEnabled()) {
210 _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + couponId);
211 }
212
213 throw new NoSuchCouponException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
214 couponId);
215 }
216
217 return remove(shoppingCoupon);
218 }
219 catch (NoSuchCouponException nsee) {
220 throw nsee;
221 }
222 catch (Exception e) {
223 throw processException(e);
224 }
225 finally {
226 closeSession(session);
227 }
228 }
229
230 protected ShoppingCoupon removeImpl(ShoppingCoupon shoppingCoupon)
231 throws SystemException {
232 shoppingCoupon = toUnwrappedModel(shoppingCoupon);
233
234 Session session = null;
235
236 try {
237 session = openSession();
238
239 BatchSessionUtil.delete(session, shoppingCoupon);
240 }
241 catch (Exception e) {
242 throw processException(e);
243 }
244 finally {
245 closeSession(session);
246 }
247
248 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
249
250 ShoppingCouponModelImpl shoppingCouponModelImpl = (ShoppingCouponModelImpl)shoppingCoupon;
251
252 FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_CODE,
253 new Object[] { shoppingCouponModelImpl.getOriginalCode() });
254
255 EntityCacheUtil.removeResult(ShoppingCouponModelImpl.ENTITY_CACHE_ENABLED,
256 ShoppingCouponImpl.class, shoppingCoupon.getPrimaryKey());
257
258 return shoppingCoupon;
259 }
260
261 public ShoppingCoupon updateImpl(
262 com.liferay.portlet.shopping.model.ShoppingCoupon shoppingCoupon,
263 boolean merge) throws SystemException {
264 shoppingCoupon = toUnwrappedModel(shoppingCoupon);
265
266 boolean isNew = shoppingCoupon.isNew();
267
268 ShoppingCouponModelImpl shoppingCouponModelImpl = (ShoppingCouponModelImpl)shoppingCoupon;
269
270 Session session = null;
271
272 try {
273 session = openSession();
274
275 BatchSessionUtil.update(session, shoppingCoupon, merge);
276
277 shoppingCoupon.setNew(false);
278 }
279 catch (Exception e) {
280 throw processException(e);
281 }
282 finally {
283 closeSession(session);
284 }
285
286 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
287
288 EntityCacheUtil.putResult(ShoppingCouponModelImpl.ENTITY_CACHE_ENABLED,
289 ShoppingCouponImpl.class, shoppingCoupon.getPrimaryKey(),
290 shoppingCoupon);
291
292 if (!isNew &&
293 (!Validator.equals(shoppingCoupon.getCode(),
294 shoppingCouponModelImpl.getOriginalCode()))) {
295 FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_CODE,
296 new Object[] { shoppingCouponModelImpl.getOriginalCode() });
297 }
298
299 if (isNew ||
300 (!Validator.equals(shoppingCoupon.getCode(),
301 shoppingCouponModelImpl.getOriginalCode()))) {
302 FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_CODE,
303 new Object[] { shoppingCoupon.getCode() }, shoppingCoupon);
304 }
305
306 return shoppingCoupon;
307 }
308
309 protected ShoppingCoupon toUnwrappedModel(ShoppingCoupon shoppingCoupon) {
310 if (shoppingCoupon instanceof ShoppingCouponImpl) {
311 return shoppingCoupon;
312 }
313
314 ShoppingCouponImpl shoppingCouponImpl = new ShoppingCouponImpl();
315
316 shoppingCouponImpl.setNew(shoppingCoupon.isNew());
317 shoppingCouponImpl.setPrimaryKey(shoppingCoupon.getPrimaryKey());
318
319 shoppingCouponImpl.setCouponId(shoppingCoupon.getCouponId());
320 shoppingCouponImpl.setGroupId(shoppingCoupon.getGroupId());
321 shoppingCouponImpl.setCompanyId(shoppingCoupon.getCompanyId());
322 shoppingCouponImpl.setUserId(shoppingCoupon.getUserId());
323 shoppingCouponImpl.setUserName(shoppingCoupon.getUserName());
324 shoppingCouponImpl.setCreateDate(shoppingCoupon.getCreateDate());
325 shoppingCouponImpl.setModifiedDate(shoppingCoupon.getModifiedDate());
326 shoppingCouponImpl.setCode(shoppingCoupon.getCode());
327 shoppingCouponImpl.setName(shoppingCoupon.getName());
328 shoppingCouponImpl.setDescription(shoppingCoupon.getDescription());
329 shoppingCouponImpl.setStartDate(shoppingCoupon.getStartDate());
330 shoppingCouponImpl.setEndDate(shoppingCoupon.getEndDate());
331 shoppingCouponImpl.setActive(shoppingCoupon.isActive());
332 shoppingCouponImpl.setLimitCategories(shoppingCoupon.getLimitCategories());
333 shoppingCouponImpl.setLimitSkus(shoppingCoupon.getLimitSkus());
334 shoppingCouponImpl.setMinOrder(shoppingCoupon.getMinOrder());
335 shoppingCouponImpl.setDiscount(shoppingCoupon.getDiscount());
336 shoppingCouponImpl.setDiscountType(shoppingCoupon.getDiscountType());
337
338 return shoppingCouponImpl;
339 }
340
341
349 public ShoppingCoupon findByPrimaryKey(Serializable primaryKey)
350 throws NoSuchModelException, SystemException {
351 return findByPrimaryKey(((Long)primaryKey).longValue());
352 }
353
354
362 public ShoppingCoupon findByPrimaryKey(long couponId)
363 throws NoSuchCouponException, SystemException {
364 ShoppingCoupon shoppingCoupon = fetchByPrimaryKey(couponId);
365
366 if (shoppingCoupon == null) {
367 if (_log.isWarnEnabled()) {
368 _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + couponId);
369 }
370
371 throw new NoSuchCouponException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
372 couponId);
373 }
374
375 return shoppingCoupon;
376 }
377
378
385 public ShoppingCoupon fetchByPrimaryKey(Serializable primaryKey)
386 throws SystemException {
387 return fetchByPrimaryKey(((Long)primaryKey).longValue());
388 }
389
390
397 public ShoppingCoupon fetchByPrimaryKey(long couponId)
398 throws SystemException {
399 ShoppingCoupon shoppingCoupon = (ShoppingCoupon)EntityCacheUtil.getResult(ShoppingCouponModelImpl.ENTITY_CACHE_ENABLED,
400 ShoppingCouponImpl.class, couponId, this);
401
402 if (shoppingCoupon == null) {
403 Session session = null;
404
405 try {
406 session = openSession();
407
408 shoppingCoupon = (ShoppingCoupon)session.get(ShoppingCouponImpl.class,
409 new Long(couponId));
410 }
411 catch (Exception e) {
412 throw processException(e);
413 }
414 finally {
415 if (shoppingCoupon != null) {
416 cacheResult(shoppingCoupon);
417 }
418
419 closeSession(session);
420 }
421 }
422
423 return shoppingCoupon;
424 }
425
426
433 public List<ShoppingCoupon> findByGroupId(long groupId)
434 throws SystemException {
435 return findByGroupId(groupId, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
436 }
437
438
451 public List<ShoppingCoupon> findByGroupId(long groupId, int start, int end)
452 throws SystemException {
453 return findByGroupId(groupId, start, end, null);
454 }
455
456
470 public List<ShoppingCoupon> findByGroupId(long groupId, int start, int end,
471 OrderByComparator orderByComparator) throws SystemException {
472 Object[] finderArgs = new Object[] {
473 groupId,
474
475 String.valueOf(start), String.valueOf(end),
476 String.valueOf(orderByComparator)
477 };
478
479 List<ShoppingCoupon> list = (List<ShoppingCoupon>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_GROUPID,
480 finderArgs, this);
481
482 if (list == null) {
483 Session session = null;
484
485 try {
486 session = openSession();
487
488 StringBundler query = null;
489
490 if (orderByComparator != null) {
491 query = new StringBundler(3 +
492 (orderByComparator.getOrderByFields().length * 3));
493 }
494 else {
495 query = new StringBundler(3);
496 }
497
498 query.append(_SQL_SELECT_SHOPPINGCOUPON_WHERE);
499
500 query.append(_FINDER_COLUMN_GROUPID_GROUPID_2);
501
502 if (orderByComparator != null) {
503 appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
504 orderByComparator);
505 }
506
507 else {
508 query.append(ShoppingCouponModelImpl.ORDER_BY_JPQL);
509 }
510
511 String sql = query.toString();
512
513 Query q = session.createQuery(sql);
514
515 QueryPos qPos = QueryPos.getInstance(q);
516
517 qPos.add(groupId);
518
519 list = (List<ShoppingCoupon>)QueryUtil.list(q, getDialect(),
520 start, end);
521 }
522 catch (Exception e) {
523 throw processException(e);
524 }
525 finally {
526 if (list == null) {
527 list = new ArrayList<ShoppingCoupon>();
528 }
529
530 cacheResult(list);
531
532 FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_GROUPID,
533 finderArgs, list);
534
535 closeSession(session);
536 }
537 }
538
539 return list;
540 }
541
542
555 public ShoppingCoupon findByGroupId_First(long groupId,
556 OrderByComparator orderByComparator)
557 throws NoSuchCouponException, SystemException {
558 List<ShoppingCoupon> list = findByGroupId(groupId, 0, 1,
559 orderByComparator);
560
561 if (list.isEmpty()) {
562 StringBundler msg = new StringBundler(4);
563
564 msg.append(_NO_SUCH_ENTITY_WITH_KEY);
565
566 msg.append("groupId=");
567 msg.append(groupId);
568
569 msg.append(StringPool.CLOSE_CURLY_BRACE);
570
571 throw new NoSuchCouponException(msg.toString());
572 }
573 else {
574 return list.get(0);
575 }
576 }
577
578
591 public ShoppingCoupon findByGroupId_Last(long groupId,
592 OrderByComparator orderByComparator)
593 throws NoSuchCouponException, SystemException {
594 int count = countByGroupId(groupId);
595
596 List<ShoppingCoupon> list = findByGroupId(groupId, count - 1, count,
597 orderByComparator);
598
599 if (list.isEmpty()) {
600 StringBundler msg = new StringBundler(4);
601
602 msg.append(_NO_SUCH_ENTITY_WITH_KEY);
603
604 msg.append("groupId=");
605 msg.append(groupId);
606
607 msg.append(StringPool.CLOSE_CURLY_BRACE);
608
609 throw new NoSuchCouponException(msg.toString());
610 }
611 else {
612 return list.get(0);
613 }
614 }
615
616
630 public ShoppingCoupon[] findByGroupId_PrevAndNext(long couponId,
631 long groupId, OrderByComparator orderByComparator)
632 throws NoSuchCouponException, SystemException {
633 ShoppingCoupon shoppingCoupon = findByPrimaryKey(couponId);
634
635 Session session = null;
636
637 try {
638 session = openSession();
639
640 ShoppingCoupon[] array = new ShoppingCouponImpl[3];
641
642 array[0] = getByGroupId_PrevAndNext(session, shoppingCoupon,
643 groupId, orderByComparator, true);
644
645 array[1] = shoppingCoupon;
646
647 array[2] = getByGroupId_PrevAndNext(session, shoppingCoupon,
648 groupId, orderByComparator, false);
649
650 return array;
651 }
652 catch (Exception e) {
653 throw processException(e);
654 }
655 finally {
656 closeSession(session);
657 }
658 }
659
660 protected ShoppingCoupon getByGroupId_PrevAndNext(Session session,
661 ShoppingCoupon shoppingCoupon, long groupId,
662 OrderByComparator orderByComparator, boolean previous) {
663 StringBundler query = null;
664
665 if (orderByComparator != null) {
666 query = new StringBundler(6 +
667 (orderByComparator.getOrderByFields().length * 6));
668 }
669 else {
670 query = new StringBundler(3);
671 }
672
673 query.append(_SQL_SELECT_SHOPPINGCOUPON_WHERE);
674
675 query.append(_FINDER_COLUMN_GROUPID_GROUPID_2);
676
677 if (orderByComparator != null) {
678 String[] orderByFields = orderByComparator.getOrderByFields();
679
680 if (orderByFields.length > 0) {
681 query.append(WHERE_AND);
682 }
683
684 for (int i = 0; i < orderByFields.length; i++) {
685 query.append(_ORDER_BY_ENTITY_ALIAS);
686 query.append(orderByFields[i]);
687
688 if ((i + 1) < orderByFields.length) {
689 if (orderByComparator.isAscending() ^ previous) {
690 query.append(WHERE_GREATER_THAN_HAS_NEXT);
691 }
692 else {
693 query.append(WHERE_LESSER_THAN_HAS_NEXT);
694 }
695 }
696 else {
697 if (orderByComparator.isAscending() ^ previous) {
698 query.append(WHERE_GREATER_THAN);
699 }
700 else {
701 query.append(WHERE_LESSER_THAN);
702 }
703 }
704 }
705
706 query.append(ORDER_BY_CLAUSE);
707
708 for (int i = 0; i < orderByFields.length; i++) {
709 query.append(_ORDER_BY_ENTITY_ALIAS);
710 query.append(orderByFields[i]);
711
712 if ((i + 1) < orderByFields.length) {
713 if (orderByComparator.isAscending() ^ previous) {
714 query.append(ORDER_BY_ASC_HAS_NEXT);
715 }
716 else {
717 query.append(ORDER_BY_DESC_HAS_NEXT);
718 }
719 }
720 else {
721 if (orderByComparator.isAscending() ^ previous) {
722 query.append(ORDER_BY_ASC);
723 }
724 else {
725 query.append(ORDER_BY_DESC);
726 }
727 }
728 }
729 }
730
731 else {
732 query.append(ShoppingCouponModelImpl.ORDER_BY_JPQL);
733 }
734
735 String sql = query.toString();
736
737 Query q = session.createQuery(sql);
738
739 q.setFirstResult(0);
740 q.setMaxResults(2);
741
742 QueryPos qPos = QueryPos.getInstance(q);
743
744 qPos.add(groupId);
745
746 if (orderByComparator != null) {
747 Object[] values = orderByComparator.getOrderByValues(shoppingCoupon);
748
749 for (Object value : values) {
750 qPos.add(value);
751 }
752 }
753
754 List<ShoppingCoupon> list = q.list();
755
756 if (list.size() == 2) {
757 return list.get(1);
758 }
759 else {
760 return null;
761 }
762 }
763
764
772 public ShoppingCoupon findByCode(String code)
773 throws NoSuchCouponException, SystemException {
774 ShoppingCoupon shoppingCoupon = fetchByCode(code);
775
776 if (shoppingCoupon == null) {
777 StringBundler msg = new StringBundler(4);
778
779 msg.append(_NO_SUCH_ENTITY_WITH_KEY);
780
781 msg.append("code=");
782 msg.append(code);
783
784 msg.append(StringPool.CLOSE_CURLY_BRACE);
785
786 if (_log.isWarnEnabled()) {
787 _log.warn(msg.toString());
788 }
789
790 throw new NoSuchCouponException(msg.toString());
791 }
792
793 return shoppingCoupon;
794 }
795
796
803 public ShoppingCoupon fetchByCode(String code) throws SystemException {
804 return fetchByCode(code, true);
805 }
806
807
814 public ShoppingCoupon fetchByCode(String code, boolean retrieveFromCache)
815 throws SystemException {
816 Object[] finderArgs = new Object[] { code };
817
818 Object result = null;
819
820 if (retrieveFromCache) {
821 result = FinderCacheUtil.getResult(FINDER_PATH_FETCH_BY_CODE,
822 finderArgs, this);
823 }
824
825 if (result == null) {
826 Session session = null;
827
828 try {
829 session = openSession();
830
831 StringBundler query = new StringBundler(3);
832
833 query.append(_SQL_SELECT_SHOPPINGCOUPON_WHERE);
834
835 if (code == null) {
836 query.append(_FINDER_COLUMN_CODE_CODE_1);
837 }
838 else {
839 if (code.equals(StringPool.BLANK)) {
840 query.append(_FINDER_COLUMN_CODE_CODE_3);
841 }
842 else {
843 query.append(_FINDER_COLUMN_CODE_CODE_2);
844 }
845 }
846
847 query.append(ShoppingCouponModelImpl.ORDER_BY_JPQL);
848
849 String sql = query.toString();
850
851 Query q = session.createQuery(sql);
852
853 QueryPos qPos = QueryPos.getInstance(q);
854
855 if (code != null) {
856 qPos.add(code);
857 }
858
859 List<ShoppingCoupon> list = q.list();
860
861 result = list;
862
863 ShoppingCoupon shoppingCoupon = null;
864
865 if (list.isEmpty()) {
866 FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_CODE,
867 finderArgs, list);
868 }
869 else {
870 shoppingCoupon = list.get(0);
871
872 cacheResult(shoppingCoupon);
873
874 if ((shoppingCoupon.getCode() == null) ||
875 !shoppingCoupon.getCode().equals(code)) {
876 FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_CODE,
877 finderArgs, shoppingCoupon);
878 }
879 }
880
881 return shoppingCoupon;
882 }
883 catch (Exception e) {
884 throw processException(e);
885 }
886 finally {
887 if (result == null) {
888 FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_CODE,
889 finderArgs, new ArrayList<ShoppingCoupon>());
890 }
891
892 closeSession(session);
893 }
894 }
895 else {
896 if (result instanceof List<?>) {
897 return null;
898 }
899 else {
900 return (ShoppingCoupon)result;
901 }
902 }
903 }
904
905
911 public List<ShoppingCoupon> findAll() throws SystemException {
912 return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
913 }
914
915
927 public List<ShoppingCoupon> findAll(int start, int end)
928 throws SystemException {
929 return findAll(start, end, null);
930 }
931
932
945 public List<ShoppingCoupon> findAll(int start, int end,
946 OrderByComparator orderByComparator) throws SystemException {
947 Object[] finderArgs = new Object[] {
948 String.valueOf(start), String.valueOf(end),
949 String.valueOf(orderByComparator)
950 };
951
952 List<ShoppingCoupon> list = (List<ShoppingCoupon>)FinderCacheUtil.getResult(FINDER_PATH_FIND_ALL,
953 finderArgs, this);
954
955 if (list == null) {
956 Session session = null;
957
958 try {
959 session = openSession();
960
961 StringBundler query = null;
962 String sql = null;
963
964 if (orderByComparator != null) {
965 query = new StringBundler(2 +
966 (orderByComparator.getOrderByFields().length * 3));
967
968 query.append(_SQL_SELECT_SHOPPINGCOUPON);
969
970 appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
971 orderByComparator);
972
973 sql = query.toString();
974 }
975 else {
976 sql = _SQL_SELECT_SHOPPINGCOUPON.concat(ShoppingCouponModelImpl.ORDER_BY_JPQL);
977 }
978
979 Query q = session.createQuery(sql);
980
981 if (orderByComparator == null) {
982 list = (List<ShoppingCoupon>)QueryUtil.list(q,
983 getDialect(), start, end, false);
984
985 Collections.sort(list);
986 }
987 else {
988 list = (List<ShoppingCoupon>)QueryUtil.list(q,
989 getDialect(), start, end);
990 }
991 }
992 catch (Exception e) {
993 throw processException(e);
994 }
995 finally {
996 if (list == null) {
997 list = new ArrayList<ShoppingCoupon>();
998 }
999
1000 cacheResult(list);
1001
1002 FinderCacheUtil.putResult(FINDER_PATH_FIND_ALL, finderArgs, list);
1003
1004 closeSession(session);
1005 }
1006 }
1007
1008 return list;
1009 }
1010
1011
1017 public void removeByGroupId(long groupId) throws SystemException {
1018 for (ShoppingCoupon shoppingCoupon : findByGroupId(groupId)) {
1019 remove(shoppingCoupon);
1020 }
1021 }
1022
1023
1029 public void removeByCode(String code)
1030 throws NoSuchCouponException, SystemException {
1031 ShoppingCoupon shoppingCoupon = findByCode(code);
1032
1033 remove(shoppingCoupon);
1034 }
1035
1036
1041 public void removeAll() throws SystemException {
1042 for (ShoppingCoupon shoppingCoupon : findAll()) {
1043 remove(shoppingCoupon);
1044 }
1045 }
1046
1047
1054 public int countByGroupId(long groupId) throws SystemException {
1055 Object[] finderArgs = new Object[] { groupId };
1056
1057 Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_GROUPID,
1058 finderArgs, this);
1059
1060 if (count == null) {
1061 Session session = null;
1062
1063 try {
1064 session = openSession();
1065
1066 StringBundler query = new StringBundler(2);
1067
1068 query.append(_SQL_COUNT_SHOPPINGCOUPON_WHERE);
1069
1070 query.append(_FINDER_COLUMN_GROUPID_GROUPID_2);
1071
1072 String sql = query.toString();
1073
1074 Query q = session.createQuery(sql);
1075
1076 QueryPos qPos = QueryPos.getInstance(q);
1077
1078 qPos.add(groupId);
1079
1080 count = (Long)q.uniqueResult();
1081 }
1082 catch (Exception e) {
1083 throw processException(e);
1084 }
1085 finally {
1086 if (count == null) {
1087 count = Long.valueOf(0);
1088 }
1089
1090 FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_GROUPID,
1091 finderArgs, count);
1092
1093 closeSession(session);
1094 }
1095 }
1096
1097 return count.intValue();
1098 }
1099
1100
1107 public int countByCode(String code) throws SystemException {
1108 Object[] finderArgs = new Object[] { code };
1109
1110 Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_CODE,
1111 finderArgs, this);
1112
1113 if (count == null) {
1114 Session session = null;
1115
1116 try {
1117 session = openSession();
1118
1119 StringBundler query = new StringBundler(2);
1120
1121 query.append(_SQL_COUNT_SHOPPINGCOUPON_WHERE);
1122
1123 if (code == null) {
1124 query.append(_FINDER_COLUMN_CODE_CODE_1);
1125 }
1126 else {
1127 if (code.equals(StringPool.BLANK)) {
1128 query.append(_FINDER_COLUMN_CODE_CODE_3);
1129 }
1130 else {
1131 query.append(_FINDER_COLUMN_CODE_CODE_2);
1132 }
1133 }
1134
1135 String sql = query.toString();
1136
1137 Query q = session.createQuery(sql);
1138
1139 QueryPos qPos = QueryPos.getInstance(q);
1140
1141 if (code != null) {
1142 qPos.add(code);
1143 }
1144
1145 count = (Long)q.uniqueResult();
1146 }
1147 catch (Exception e) {
1148 throw processException(e);
1149 }
1150 finally {
1151 if (count == null) {
1152 count = Long.valueOf(0);
1153 }
1154
1155 FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_CODE,
1156 finderArgs, count);
1157
1158 closeSession(session);
1159 }
1160 }
1161
1162 return count.intValue();
1163 }
1164
1165
1171 public int countAll() throws SystemException {
1172 Object[] finderArgs = new Object[0];
1173
1174 Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
1175 finderArgs, this);
1176
1177 if (count == null) {
1178 Session session = null;
1179
1180 try {
1181 session = openSession();
1182
1183 Query q = session.createQuery(_SQL_COUNT_SHOPPINGCOUPON);
1184
1185 count = (Long)q.uniqueResult();
1186 }
1187 catch (Exception e) {
1188 throw processException(e);
1189 }
1190 finally {
1191 if (count == null) {
1192 count = Long.valueOf(0);
1193 }
1194
1195 FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL, finderArgs,
1196 count);
1197
1198 closeSession(session);
1199 }
1200 }
1201
1202 return count.intValue();
1203 }
1204
1205
1208 public void afterPropertiesSet() {
1209 String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
1210 com.liferay.portal.util.PropsUtil.get(
1211 "value.object.listener.com.liferay.portlet.shopping.model.ShoppingCoupon")));
1212
1213 if (listenerClassNames.length > 0) {
1214 try {
1215 List<ModelListener<ShoppingCoupon>> listenersList = new ArrayList<ModelListener<ShoppingCoupon>>();
1216
1217 for (String listenerClassName : listenerClassNames) {
1218 listenersList.add((ModelListener<ShoppingCoupon>)InstanceFactory.newInstance(
1219 listenerClassName));
1220 }
1221
1222 listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
1223 }
1224 catch (Exception e) {
1225 _log.error(e);
1226 }
1227 }
1228 }
1229
1230 public void destroy() {
1231 EntityCacheUtil.removeCache(ShoppingCouponImpl.class.getName());
1232 FinderCacheUtil.removeCache(FINDER_CLASS_NAME_ENTITY);
1233 FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST);
1234 }
1235
1236 @BeanReference(type = ShoppingCartPersistence.class)
1237 protected ShoppingCartPersistence shoppingCartPersistence;
1238 @BeanReference(type = ShoppingCategoryPersistence.class)
1239 protected ShoppingCategoryPersistence shoppingCategoryPersistence;
1240 @BeanReference(type = ShoppingCouponPersistence.class)
1241 protected ShoppingCouponPersistence shoppingCouponPersistence;
1242 @BeanReference(type = ShoppingItemPersistence.class)
1243 protected ShoppingItemPersistence shoppingItemPersistence;
1244 @BeanReference(type = ShoppingItemFieldPersistence.class)
1245 protected ShoppingItemFieldPersistence shoppingItemFieldPersistence;
1246 @BeanReference(type = ShoppingItemPricePersistence.class)
1247 protected ShoppingItemPricePersistence shoppingItemPricePersistence;
1248 @BeanReference(type = ShoppingOrderPersistence.class)
1249 protected ShoppingOrderPersistence shoppingOrderPersistence;
1250 @BeanReference(type = ShoppingOrderItemPersistence.class)
1251 protected ShoppingOrderItemPersistence shoppingOrderItemPersistence;
1252 @BeanReference(type = ResourcePersistence.class)
1253 protected ResourcePersistence resourcePersistence;
1254 @BeanReference(type = UserPersistence.class)
1255 protected UserPersistence userPersistence;
1256 private static final String _SQL_SELECT_SHOPPINGCOUPON = "SELECT shoppingCoupon FROM ShoppingCoupon shoppingCoupon";
1257 private static final String _SQL_SELECT_SHOPPINGCOUPON_WHERE = "SELECT shoppingCoupon FROM ShoppingCoupon shoppingCoupon WHERE ";
1258 private static final String _SQL_COUNT_SHOPPINGCOUPON = "SELECT COUNT(shoppingCoupon) FROM ShoppingCoupon shoppingCoupon";
1259 private static final String _SQL_COUNT_SHOPPINGCOUPON_WHERE = "SELECT COUNT(shoppingCoupon) FROM ShoppingCoupon shoppingCoupon WHERE ";
1260 private static final String _FINDER_COLUMN_GROUPID_GROUPID_2 = "shoppingCoupon.groupId = ?";
1261 private static final String _FINDER_COLUMN_CODE_CODE_1 = "shoppingCoupon.code IS NULL";
1262 private static final String _FINDER_COLUMN_CODE_CODE_2 = "shoppingCoupon.code = ?";
1263 private static final String _FINDER_COLUMN_CODE_CODE_3 = "(shoppingCoupon.code IS NULL OR shoppingCoupon.code = ?)";
1264 private static final String _ORDER_BY_ENTITY_ALIAS = "shoppingCoupon.";
1265 private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No ShoppingCoupon exists with the primary key ";
1266 private static final String _NO_SUCH_ENTITY_WITH_KEY = "No ShoppingCoupon exists with the key {";
1267 private static Log _log = LogFactoryUtil.getLog(ShoppingCouponPersistenceImpl.class);
1268 }