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.getCode() });
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 StringBundler query = null;
484
485 if (orderByComparator != null) {
486 query = new StringBundler(3 +
487 (orderByComparator.getOrderByFields().length * 3));
488 }
489 else {
490 query = new StringBundler(3);
491 }
492
493 query.append(_SQL_SELECT_SHOPPINGCOUPON_WHERE);
494
495 query.append(_FINDER_COLUMN_GROUPID_GROUPID_2);
496
497 if (orderByComparator != null) {
498 appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
499 orderByComparator);
500 }
501
502 else {
503 query.append(ShoppingCouponModelImpl.ORDER_BY_JPQL);
504 }
505
506 String sql = query.toString();
507
508 Session session = null;
509
510 try {
511 session = openSession();
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 FinderCacheUtil.removeResult(FINDER_PATH_FIND_BY_GROUPID,
528 finderArgs);
529 }
530 else {
531 cacheResult(list);
532
533 FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_GROUPID,
534 finderArgs, list);
535 }
536
537 closeSession(session);
538 }
539 }
540
541 return list;
542 }
543
544
557 public ShoppingCoupon findByGroupId_First(long groupId,
558 OrderByComparator orderByComparator)
559 throws NoSuchCouponException, SystemException {
560 List<ShoppingCoupon> list = findByGroupId(groupId, 0, 1,
561 orderByComparator);
562
563 if (list.isEmpty()) {
564 StringBundler msg = new StringBundler(4);
565
566 msg.append(_NO_SUCH_ENTITY_WITH_KEY);
567
568 msg.append("groupId=");
569 msg.append(groupId);
570
571 msg.append(StringPool.CLOSE_CURLY_BRACE);
572
573 throw new NoSuchCouponException(msg.toString());
574 }
575 else {
576 return list.get(0);
577 }
578 }
579
580
593 public ShoppingCoupon findByGroupId_Last(long groupId,
594 OrderByComparator orderByComparator)
595 throws NoSuchCouponException, SystemException {
596 int count = countByGroupId(groupId);
597
598 List<ShoppingCoupon> list = findByGroupId(groupId, count - 1, count,
599 orderByComparator);
600
601 if (list.isEmpty()) {
602 StringBundler msg = new StringBundler(4);
603
604 msg.append(_NO_SUCH_ENTITY_WITH_KEY);
605
606 msg.append("groupId=");
607 msg.append(groupId);
608
609 msg.append(StringPool.CLOSE_CURLY_BRACE);
610
611 throw new NoSuchCouponException(msg.toString());
612 }
613 else {
614 return list.get(0);
615 }
616 }
617
618
632 public ShoppingCoupon[] findByGroupId_PrevAndNext(long couponId,
633 long groupId, OrderByComparator orderByComparator)
634 throws NoSuchCouponException, SystemException {
635 ShoppingCoupon shoppingCoupon = findByPrimaryKey(couponId);
636
637 Session session = null;
638
639 try {
640 session = openSession();
641
642 ShoppingCoupon[] array = new ShoppingCouponImpl[3];
643
644 array[0] = getByGroupId_PrevAndNext(session, shoppingCoupon,
645 groupId, orderByComparator, true);
646
647 array[1] = shoppingCoupon;
648
649 array[2] = getByGroupId_PrevAndNext(session, shoppingCoupon,
650 groupId, orderByComparator, false);
651
652 return array;
653 }
654 catch (Exception e) {
655 throw processException(e);
656 }
657 finally {
658 closeSession(session);
659 }
660 }
661
662 protected ShoppingCoupon getByGroupId_PrevAndNext(Session session,
663 ShoppingCoupon shoppingCoupon, long groupId,
664 OrderByComparator orderByComparator, boolean previous) {
665 StringBundler query = null;
666
667 if (orderByComparator != null) {
668 query = new StringBundler(6 +
669 (orderByComparator.getOrderByFields().length * 6));
670 }
671 else {
672 query = new StringBundler(3);
673 }
674
675 query.append(_SQL_SELECT_SHOPPINGCOUPON_WHERE);
676
677 query.append(_FINDER_COLUMN_GROUPID_GROUPID_2);
678
679 if (orderByComparator != null) {
680 String[] orderByFields = orderByComparator.getOrderByFields();
681
682 if (orderByFields.length > 0) {
683 query.append(WHERE_AND);
684 }
685
686 for (int i = 0; i < orderByFields.length; i++) {
687 query.append(_ORDER_BY_ENTITY_ALIAS);
688 query.append(orderByFields[i]);
689
690 if ((i + 1) < orderByFields.length) {
691 if (orderByComparator.isAscending() ^ previous) {
692 query.append(WHERE_GREATER_THAN_HAS_NEXT);
693 }
694 else {
695 query.append(WHERE_LESSER_THAN_HAS_NEXT);
696 }
697 }
698 else {
699 if (orderByComparator.isAscending() ^ previous) {
700 query.append(WHERE_GREATER_THAN);
701 }
702 else {
703 query.append(WHERE_LESSER_THAN);
704 }
705 }
706 }
707
708 query.append(ORDER_BY_CLAUSE);
709
710 for (int i = 0; i < orderByFields.length; i++) {
711 query.append(_ORDER_BY_ENTITY_ALIAS);
712 query.append(orderByFields[i]);
713
714 if ((i + 1) < orderByFields.length) {
715 if (orderByComparator.isAscending() ^ previous) {
716 query.append(ORDER_BY_ASC_HAS_NEXT);
717 }
718 else {
719 query.append(ORDER_BY_DESC_HAS_NEXT);
720 }
721 }
722 else {
723 if (orderByComparator.isAscending() ^ previous) {
724 query.append(ORDER_BY_ASC);
725 }
726 else {
727 query.append(ORDER_BY_DESC);
728 }
729 }
730 }
731 }
732
733 else {
734 query.append(ShoppingCouponModelImpl.ORDER_BY_JPQL);
735 }
736
737 String sql = query.toString();
738
739 Query q = session.createQuery(sql);
740
741 q.setFirstResult(0);
742 q.setMaxResults(2);
743
744 QueryPos qPos = QueryPos.getInstance(q);
745
746 qPos.add(groupId);
747
748 if (orderByComparator != null) {
749 Object[] values = orderByComparator.getOrderByValues(shoppingCoupon);
750
751 for (Object value : values) {
752 qPos.add(value);
753 }
754 }
755
756 List<ShoppingCoupon> list = q.list();
757
758 if (list.size() == 2) {
759 return list.get(1);
760 }
761 else {
762 return null;
763 }
764 }
765
766
774 public ShoppingCoupon findByCode(String code)
775 throws NoSuchCouponException, SystemException {
776 ShoppingCoupon shoppingCoupon = fetchByCode(code);
777
778 if (shoppingCoupon == null) {
779 StringBundler msg = new StringBundler(4);
780
781 msg.append(_NO_SUCH_ENTITY_WITH_KEY);
782
783 msg.append("code=");
784 msg.append(code);
785
786 msg.append(StringPool.CLOSE_CURLY_BRACE);
787
788 if (_log.isWarnEnabled()) {
789 _log.warn(msg.toString());
790 }
791
792 throw new NoSuchCouponException(msg.toString());
793 }
794
795 return shoppingCoupon;
796 }
797
798
805 public ShoppingCoupon fetchByCode(String code) throws SystemException {
806 return fetchByCode(code, true);
807 }
808
809
816 public ShoppingCoupon fetchByCode(String code, boolean retrieveFromCache)
817 throws SystemException {
818 Object[] finderArgs = new Object[] { code };
819
820 Object result = null;
821
822 if (retrieveFromCache) {
823 result = FinderCacheUtil.getResult(FINDER_PATH_FETCH_BY_CODE,
824 finderArgs, this);
825 }
826
827 if (result == null) {
828 StringBundler query = new StringBundler(3);
829
830 query.append(_SQL_SELECT_SHOPPINGCOUPON_WHERE);
831
832 if (code == null) {
833 query.append(_FINDER_COLUMN_CODE_CODE_1);
834 }
835 else {
836 if (code.equals(StringPool.BLANK)) {
837 query.append(_FINDER_COLUMN_CODE_CODE_3);
838 }
839 else {
840 query.append(_FINDER_COLUMN_CODE_CODE_2);
841 }
842 }
843
844 query.append(ShoppingCouponModelImpl.ORDER_BY_JPQL);
845
846 String sql = query.toString();
847
848 Session session = null;
849
850 try {
851 session = openSession();
852
853 Query q = session.createQuery(sql);
854
855 QueryPos qPos = QueryPos.getInstance(q);
856
857 if (code != null) {
858 qPos.add(code);
859 }
860
861 List<ShoppingCoupon> list = q.list();
862
863 result = list;
864
865 ShoppingCoupon shoppingCoupon = null;
866
867 if (list.isEmpty()) {
868 FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_CODE,
869 finderArgs, list);
870 }
871 else {
872 shoppingCoupon = list.get(0);
873
874 cacheResult(shoppingCoupon);
875
876 if ((shoppingCoupon.getCode() == null) ||
877 !shoppingCoupon.getCode().equals(code)) {
878 FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_CODE,
879 finderArgs, shoppingCoupon);
880 }
881 }
882
883 return shoppingCoupon;
884 }
885 catch (Exception e) {
886 throw processException(e);
887 }
888 finally {
889 if (result == null) {
890 FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_CODE,
891 finderArgs);
892 }
893
894 closeSession(session);
895 }
896 }
897 else {
898 if (result instanceof List<?>) {
899 return null;
900 }
901 else {
902 return (ShoppingCoupon)result;
903 }
904 }
905 }
906
907
913 public List<ShoppingCoupon> findAll() throws SystemException {
914 return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
915 }
916
917
929 public List<ShoppingCoupon> findAll(int start, int end)
930 throws SystemException {
931 return findAll(start, end, null);
932 }
933
934
947 public List<ShoppingCoupon> findAll(int start, int end,
948 OrderByComparator orderByComparator) throws SystemException {
949 Object[] finderArgs = new Object[] {
950 String.valueOf(start), String.valueOf(end),
951 String.valueOf(orderByComparator)
952 };
953
954 List<ShoppingCoupon> list = (List<ShoppingCoupon>)FinderCacheUtil.getResult(FINDER_PATH_FIND_ALL,
955 finderArgs, this);
956
957 if (list == null) {
958 StringBundler query = null;
959 String sql = null;
960
961 if (orderByComparator != null) {
962 query = new StringBundler(2 +
963 (orderByComparator.getOrderByFields().length * 3));
964
965 query.append(_SQL_SELECT_SHOPPINGCOUPON);
966
967 appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
968 orderByComparator);
969
970 sql = query.toString();
971 }
972 else {
973 sql = _SQL_SELECT_SHOPPINGCOUPON.concat(ShoppingCouponModelImpl.ORDER_BY_JPQL);
974 }
975
976 Session session = null;
977
978 try {
979 session = openSession();
980
981 Query q = session.createQuery(sql);
982
983 if (orderByComparator == null) {
984 list = (List<ShoppingCoupon>)QueryUtil.list(q,
985 getDialect(), start, end, false);
986
987 Collections.sort(list);
988 }
989 else {
990 list = (List<ShoppingCoupon>)QueryUtil.list(q,
991 getDialect(), start, end);
992 }
993 }
994 catch (Exception e) {
995 throw processException(e);
996 }
997 finally {
998 if (list == null) {
999 FinderCacheUtil.removeResult(FINDER_PATH_FIND_ALL,
1000 finderArgs);
1001 }
1002 else {
1003 cacheResult(list);
1004
1005 FinderCacheUtil.putResult(FINDER_PATH_FIND_ALL, finderArgs,
1006 list);
1007 }
1008
1009 closeSession(session);
1010 }
1011 }
1012
1013 return list;
1014 }
1015
1016
1022 public void removeByGroupId(long groupId) throws SystemException {
1023 for (ShoppingCoupon shoppingCoupon : findByGroupId(groupId)) {
1024 remove(shoppingCoupon);
1025 }
1026 }
1027
1028
1034 public void removeByCode(String code)
1035 throws NoSuchCouponException, SystemException {
1036 ShoppingCoupon shoppingCoupon = findByCode(code);
1037
1038 remove(shoppingCoupon);
1039 }
1040
1041
1046 public void removeAll() throws SystemException {
1047 for (ShoppingCoupon shoppingCoupon : findAll()) {
1048 remove(shoppingCoupon);
1049 }
1050 }
1051
1052
1059 public int countByGroupId(long groupId) throws SystemException {
1060 Object[] finderArgs = new Object[] { groupId };
1061
1062 Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_GROUPID,
1063 finderArgs, this);
1064
1065 if (count == null) {
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 Session session = null;
1075
1076 try {
1077 session = openSession();
1078
1079 Query q = session.createQuery(sql);
1080
1081 QueryPos qPos = QueryPos.getInstance(q);
1082
1083 qPos.add(groupId);
1084
1085 count = (Long)q.uniqueResult();
1086 }
1087 catch (Exception e) {
1088 throw processException(e);
1089 }
1090 finally {
1091 if (count == null) {
1092 count = Long.valueOf(0);
1093 }
1094
1095 FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_GROUPID,
1096 finderArgs, count);
1097
1098 closeSession(session);
1099 }
1100 }
1101
1102 return count.intValue();
1103 }
1104
1105
1112 public int countByCode(String code) throws SystemException {
1113 Object[] finderArgs = new Object[] { code };
1114
1115 Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_CODE,
1116 finderArgs, this);
1117
1118 if (count == null) {
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 Session session = null;
1138
1139 try {
1140 session = openSession();
1141
1142 Query q = session.createQuery(sql);
1143
1144 QueryPos qPos = QueryPos.getInstance(q);
1145
1146 if (code != null) {
1147 qPos.add(code);
1148 }
1149
1150 count = (Long)q.uniqueResult();
1151 }
1152 catch (Exception e) {
1153 throw processException(e);
1154 }
1155 finally {
1156 if (count == null) {
1157 count = Long.valueOf(0);
1158 }
1159
1160 FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_CODE,
1161 finderArgs, count);
1162
1163 closeSession(session);
1164 }
1165 }
1166
1167 return count.intValue();
1168 }
1169
1170
1176 public int countAll() throws SystemException {
1177 Object[] finderArgs = new Object[0];
1178
1179 Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
1180 finderArgs, this);
1181
1182 if (count == null) {
1183 Session session = null;
1184
1185 try {
1186 session = openSession();
1187
1188 Query q = session.createQuery(_SQL_COUNT_SHOPPINGCOUPON);
1189
1190 count = (Long)q.uniqueResult();
1191 }
1192 catch (Exception e) {
1193 throw processException(e);
1194 }
1195 finally {
1196 if (count == null) {
1197 count = Long.valueOf(0);
1198 }
1199
1200 FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL, finderArgs,
1201 count);
1202
1203 closeSession(session);
1204 }
1205 }
1206
1207 return count.intValue();
1208 }
1209
1210
1213 public void afterPropertiesSet() {
1214 String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
1215 com.liferay.portal.util.PropsUtil.get(
1216 "value.object.listener.com.liferay.portlet.shopping.model.ShoppingCoupon")));
1217
1218 if (listenerClassNames.length > 0) {
1219 try {
1220 List<ModelListener<ShoppingCoupon>> listenersList = new ArrayList<ModelListener<ShoppingCoupon>>();
1221
1222 for (String listenerClassName : listenerClassNames) {
1223 listenersList.add((ModelListener<ShoppingCoupon>)InstanceFactory.newInstance(
1224 listenerClassName));
1225 }
1226
1227 listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
1228 }
1229 catch (Exception e) {
1230 _log.error(e);
1231 }
1232 }
1233 }
1234
1235 public void destroy() {
1236 EntityCacheUtil.removeCache(ShoppingCouponImpl.class.getName());
1237 FinderCacheUtil.removeCache(FINDER_CLASS_NAME_ENTITY);
1238 FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST);
1239 }
1240
1241 @BeanReference(type = ShoppingCartPersistence.class)
1242 protected ShoppingCartPersistence shoppingCartPersistence;
1243 @BeanReference(type = ShoppingCategoryPersistence.class)
1244 protected ShoppingCategoryPersistence shoppingCategoryPersistence;
1245 @BeanReference(type = ShoppingCouponPersistence.class)
1246 protected ShoppingCouponPersistence shoppingCouponPersistence;
1247 @BeanReference(type = ShoppingItemPersistence.class)
1248 protected ShoppingItemPersistence shoppingItemPersistence;
1249 @BeanReference(type = ShoppingItemFieldPersistence.class)
1250 protected ShoppingItemFieldPersistence shoppingItemFieldPersistence;
1251 @BeanReference(type = ShoppingItemPricePersistence.class)
1252 protected ShoppingItemPricePersistence shoppingItemPricePersistence;
1253 @BeanReference(type = ShoppingOrderPersistence.class)
1254 protected ShoppingOrderPersistence shoppingOrderPersistence;
1255 @BeanReference(type = ShoppingOrderItemPersistence.class)
1256 protected ShoppingOrderItemPersistence shoppingOrderItemPersistence;
1257 @BeanReference(type = ResourcePersistence.class)
1258 protected ResourcePersistence resourcePersistence;
1259 @BeanReference(type = UserPersistence.class)
1260 protected UserPersistence userPersistence;
1261 private static final String _SQL_SELECT_SHOPPINGCOUPON = "SELECT shoppingCoupon FROM ShoppingCoupon shoppingCoupon";
1262 private static final String _SQL_SELECT_SHOPPINGCOUPON_WHERE = "SELECT shoppingCoupon FROM ShoppingCoupon shoppingCoupon WHERE ";
1263 private static final String _SQL_COUNT_SHOPPINGCOUPON = "SELECT COUNT(shoppingCoupon) FROM ShoppingCoupon shoppingCoupon";
1264 private static final String _SQL_COUNT_SHOPPINGCOUPON_WHERE = "SELECT COUNT(shoppingCoupon) FROM ShoppingCoupon shoppingCoupon WHERE ";
1265 private static final String _FINDER_COLUMN_GROUPID_GROUPID_2 = "shoppingCoupon.groupId = ?";
1266 private static final String _FINDER_COLUMN_CODE_CODE_1 = "shoppingCoupon.code IS NULL";
1267 private static final String _FINDER_COLUMN_CODE_CODE_2 = "shoppingCoupon.code = ?";
1268 private static final String _FINDER_COLUMN_CODE_CODE_3 = "(shoppingCoupon.code IS NULL OR shoppingCoupon.code = ?)";
1269 private static final String _ORDER_BY_ENTITY_ALIAS = "shoppingCoupon.";
1270 private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No ShoppingCoupon exists with the primary key ";
1271 private static final String _NO_SUCH_ENTITY_WITH_KEY = "No ShoppingCoupon exists with the key {";
1272 private static Log _log = LogFactoryUtil.getLog(ShoppingCouponPersistenceImpl.class);
1273 }