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 if (shoppingCoupon.isCachedModel() || BatchSessionUtil.isEnabled()) {
240 Object staleObject = session.get(ShoppingCouponImpl.class,
241 shoppingCoupon.getPrimaryKeyObj());
242
243 if (staleObject != null) {
244 session.evict(staleObject);
245 }
246 }
247
248 session.delete(shoppingCoupon);
249
250 session.flush();
251 }
252 catch (Exception e) {
253 throw processException(e);
254 }
255 finally {
256 closeSession(session);
257 }
258
259 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
260
261 ShoppingCouponModelImpl shoppingCouponModelImpl = (ShoppingCouponModelImpl)shoppingCoupon;
262
263 FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_CODE,
264 new Object[] { shoppingCouponModelImpl.getOriginalCode() });
265
266 EntityCacheUtil.removeResult(ShoppingCouponModelImpl.ENTITY_CACHE_ENABLED,
267 ShoppingCouponImpl.class, shoppingCoupon.getPrimaryKey());
268
269 return shoppingCoupon;
270 }
271
272 public ShoppingCoupon updateImpl(
273 com.liferay.portlet.shopping.model.ShoppingCoupon shoppingCoupon,
274 boolean merge) throws SystemException {
275 shoppingCoupon = toUnwrappedModel(shoppingCoupon);
276
277 boolean isNew = shoppingCoupon.isNew();
278
279 ShoppingCouponModelImpl shoppingCouponModelImpl = (ShoppingCouponModelImpl)shoppingCoupon;
280
281 Session session = null;
282
283 try {
284 session = openSession();
285
286 BatchSessionUtil.update(session, shoppingCoupon, merge);
287
288 shoppingCoupon.setNew(false);
289 }
290 catch (Exception e) {
291 throw processException(e);
292 }
293 finally {
294 closeSession(session);
295 }
296
297 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
298
299 EntityCacheUtil.putResult(ShoppingCouponModelImpl.ENTITY_CACHE_ENABLED,
300 ShoppingCouponImpl.class, shoppingCoupon.getPrimaryKey(),
301 shoppingCoupon);
302
303 if (!isNew &&
304 (!Validator.equals(shoppingCoupon.getCode(),
305 shoppingCouponModelImpl.getOriginalCode()))) {
306 FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_CODE,
307 new Object[] { shoppingCouponModelImpl.getOriginalCode() });
308 }
309
310 if (isNew ||
311 (!Validator.equals(shoppingCoupon.getCode(),
312 shoppingCouponModelImpl.getOriginalCode()))) {
313 FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_CODE,
314 new Object[] { shoppingCoupon.getCode() }, shoppingCoupon);
315 }
316
317 return shoppingCoupon;
318 }
319
320 protected ShoppingCoupon toUnwrappedModel(ShoppingCoupon shoppingCoupon) {
321 if (shoppingCoupon instanceof ShoppingCouponImpl) {
322 return shoppingCoupon;
323 }
324
325 ShoppingCouponImpl shoppingCouponImpl = new ShoppingCouponImpl();
326
327 shoppingCouponImpl.setNew(shoppingCoupon.isNew());
328 shoppingCouponImpl.setPrimaryKey(shoppingCoupon.getPrimaryKey());
329
330 shoppingCouponImpl.setCouponId(shoppingCoupon.getCouponId());
331 shoppingCouponImpl.setGroupId(shoppingCoupon.getGroupId());
332 shoppingCouponImpl.setCompanyId(shoppingCoupon.getCompanyId());
333 shoppingCouponImpl.setUserId(shoppingCoupon.getUserId());
334 shoppingCouponImpl.setUserName(shoppingCoupon.getUserName());
335 shoppingCouponImpl.setCreateDate(shoppingCoupon.getCreateDate());
336 shoppingCouponImpl.setModifiedDate(shoppingCoupon.getModifiedDate());
337 shoppingCouponImpl.setCode(shoppingCoupon.getCode());
338 shoppingCouponImpl.setName(shoppingCoupon.getName());
339 shoppingCouponImpl.setDescription(shoppingCoupon.getDescription());
340 shoppingCouponImpl.setStartDate(shoppingCoupon.getStartDate());
341 shoppingCouponImpl.setEndDate(shoppingCoupon.getEndDate());
342 shoppingCouponImpl.setActive(shoppingCoupon.isActive());
343 shoppingCouponImpl.setLimitCategories(shoppingCoupon.getLimitCategories());
344 shoppingCouponImpl.setLimitSkus(shoppingCoupon.getLimitSkus());
345 shoppingCouponImpl.setMinOrder(shoppingCoupon.getMinOrder());
346 shoppingCouponImpl.setDiscount(shoppingCoupon.getDiscount());
347 shoppingCouponImpl.setDiscountType(shoppingCoupon.getDiscountType());
348
349 return shoppingCouponImpl;
350 }
351
352
360 public ShoppingCoupon findByPrimaryKey(Serializable primaryKey)
361 throws NoSuchModelException, SystemException {
362 return findByPrimaryKey(((Long)primaryKey).longValue());
363 }
364
365
373 public ShoppingCoupon findByPrimaryKey(long couponId)
374 throws NoSuchCouponException, SystemException {
375 ShoppingCoupon shoppingCoupon = fetchByPrimaryKey(couponId);
376
377 if (shoppingCoupon == null) {
378 if (_log.isWarnEnabled()) {
379 _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + couponId);
380 }
381
382 throw new NoSuchCouponException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
383 couponId);
384 }
385
386 return shoppingCoupon;
387 }
388
389
396 public ShoppingCoupon fetchByPrimaryKey(Serializable primaryKey)
397 throws SystemException {
398 return fetchByPrimaryKey(((Long)primaryKey).longValue());
399 }
400
401
408 public ShoppingCoupon fetchByPrimaryKey(long couponId)
409 throws SystemException {
410 ShoppingCoupon shoppingCoupon = (ShoppingCoupon)EntityCacheUtil.getResult(ShoppingCouponModelImpl.ENTITY_CACHE_ENABLED,
411 ShoppingCouponImpl.class, couponId, this);
412
413 if (shoppingCoupon == null) {
414 Session session = null;
415
416 try {
417 session = openSession();
418
419 shoppingCoupon = (ShoppingCoupon)session.get(ShoppingCouponImpl.class,
420 new Long(couponId));
421 }
422 catch (Exception e) {
423 throw processException(e);
424 }
425 finally {
426 if (shoppingCoupon != null) {
427 cacheResult(shoppingCoupon);
428 }
429
430 closeSession(session);
431 }
432 }
433
434 return shoppingCoupon;
435 }
436
437
444 public List<ShoppingCoupon> findByGroupId(long groupId)
445 throws SystemException {
446 return findByGroupId(groupId, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
447 }
448
449
462 public List<ShoppingCoupon> findByGroupId(long groupId, int start, int end)
463 throws SystemException {
464 return findByGroupId(groupId, start, end, null);
465 }
466
467
481 public List<ShoppingCoupon> findByGroupId(long groupId, int start, int end,
482 OrderByComparator orderByComparator) throws SystemException {
483 Object[] finderArgs = new Object[] {
484 groupId,
485
486 String.valueOf(start), String.valueOf(end),
487 String.valueOf(orderByComparator)
488 };
489
490 List<ShoppingCoupon> list = (List<ShoppingCoupon>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_GROUPID,
491 finderArgs, this);
492
493 if (list == null) {
494 Session session = null;
495
496 try {
497 session = openSession();
498
499 StringBundler query = null;
500
501 if (orderByComparator != null) {
502 query = new StringBundler(3 +
503 (orderByComparator.getOrderByFields().length * 3));
504 }
505 else {
506 query = new StringBundler(3);
507 }
508
509 query.append(_SQL_SELECT_SHOPPINGCOUPON_WHERE);
510
511 query.append(_FINDER_COLUMN_GROUPID_GROUPID_2);
512
513 if (orderByComparator != null) {
514 appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
515 orderByComparator);
516 }
517
518 else {
519 query.append(ShoppingCouponModelImpl.ORDER_BY_JPQL);
520 }
521
522 String sql = query.toString();
523
524 Query q = session.createQuery(sql);
525
526 QueryPos qPos = QueryPos.getInstance(q);
527
528 qPos.add(groupId);
529
530 list = (List<ShoppingCoupon>)QueryUtil.list(q, getDialect(),
531 start, end);
532 }
533 catch (Exception e) {
534 throw processException(e);
535 }
536 finally {
537 if (list == null) {
538 list = new ArrayList<ShoppingCoupon>();
539 }
540
541 cacheResult(list);
542
543 FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_GROUPID,
544 finderArgs, list);
545
546 closeSession(session);
547 }
548 }
549
550 return list;
551 }
552
553
566 public ShoppingCoupon findByGroupId_First(long groupId,
567 OrderByComparator orderByComparator)
568 throws NoSuchCouponException, SystemException {
569 List<ShoppingCoupon> list = findByGroupId(groupId, 0, 1,
570 orderByComparator);
571
572 if (list.isEmpty()) {
573 StringBundler msg = new StringBundler(4);
574
575 msg.append(_NO_SUCH_ENTITY_WITH_KEY);
576
577 msg.append("groupId=");
578 msg.append(groupId);
579
580 msg.append(StringPool.CLOSE_CURLY_BRACE);
581
582 throw new NoSuchCouponException(msg.toString());
583 }
584 else {
585 return list.get(0);
586 }
587 }
588
589
602 public ShoppingCoupon findByGroupId_Last(long groupId,
603 OrderByComparator orderByComparator)
604 throws NoSuchCouponException, SystemException {
605 int count = countByGroupId(groupId);
606
607 List<ShoppingCoupon> list = findByGroupId(groupId, count - 1, count,
608 orderByComparator);
609
610 if (list.isEmpty()) {
611 StringBundler msg = new StringBundler(4);
612
613 msg.append(_NO_SUCH_ENTITY_WITH_KEY);
614
615 msg.append("groupId=");
616 msg.append(groupId);
617
618 msg.append(StringPool.CLOSE_CURLY_BRACE);
619
620 throw new NoSuchCouponException(msg.toString());
621 }
622 else {
623 return list.get(0);
624 }
625 }
626
627
641 public ShoppingCoupon[] findByGroupId_PrevAndNext(long couponId,
642 long groupId, OrderByComparator orderByComparator)
643 throws NoSuchCouponException, SystemException {
644 ShoppingCoupon shoppingCoupon = findByPrimaryKey(couponId);
645
646 Session session = null;
647
648 try {
649 session = openSession();
650
651 ShoppingCoupon[] array = new ShoppingCouponImpl[3];
652
653 array[0] = getByGroupId_PrevAndNext(session, shoppingCoupon,
654 groupId, orderByComparator, true);
655
656 array[1] = shoppingCoupon;
657
658 array[2] = getByGroupId_PrevAndNext(session, shoppingCoupon,
659 groupId, orderByComparator, false);
660
661 return array;
662 }
663 catch (Exception e) {
664 throw processException(e);
665 }
666 finally {
667 closeSession(session);
668 }
669 }
670
671 protected ShoppingCoupon getByGroupId_PrevAndNext(Session session,
672 ShoppingCoupon shoppingCoupon, long groupId,
673 OrderByComparator orderByComparator, boolean previous) {
674 StringBundler query = null;
675
676 if (orderByComparator != null) {
677 query = new StringBundler(6 +
678 (orderByComparator.getOrderByFields().length * 6));
679 }
680 else {
681 query = new StringBundler(3);
682 }
683
684 query.append(_SQL_SELECT_SHOPPINGCOUPON_WHERE);
685
686 query.append(_FINDER_COLUMN_GROUPID_GROUPID_2);
687
688 if (orderByComparator != null) {
689 String[] orderByFields = orderByComparator.getOrderByFields();
690
691 if (orderByFields.length > 0) {
692 query.append(WHERE_AND);
693 }
694
695 for (int i = 0; i < orderByFields.length; i++) {
696 query.append(_ORDER_BY_ENTITY_ALIAS);
697 query.append(orderByFields[i]);
698
699 if ((i + 1) < orderByFields.length) {
700 if (orderByComparator.isAscending() ^ previous) {
701 query.append(WHERE_GREATER_THAN_HAS_NEXT);
702 }
703 else {
704 query.append(WHERE_LESSER_THAN_HAS_NEXT);
705 }
706 }
707 else {
708 if (orderByComparator.isAscending() ^ previous) {
709 query.append(WHERE_GREATER_THAN);
710 }
711 else {
712 query.append(WHERE_LESSER_THAN);
713 }
714 }
715 }
716
717 query.append(ORDER_BY_CLAUSE);
718
719 for (int i = 0; i < orderByFields.length; i++) {
720 query.append(_ORDER_BY_ENTITY_ALIAS);
721 query.append(orderByFields[i]);
722
723 if ((i + 1) < orderByFields.length) {
724 if (orderByComparator.isAscending() ^ previous) {
725 query.append(ORDER_BY_ASC_HAS_NEXT);
726 }
727 else {
728 query.append(ORDER_BY_DESC_HAS_NEXT);
729 }
730 }
731 else {
732 if (orderByComparator.isAscending() ^ previous) {
733 query.append(ORDER_BY_ASC);
734 }
735 else {
736 query.append(ORDER_BY_DESC);
737 }
738 }
739 }
740 }
741
742 else {
743 query.append(ShoppingCouponModelImpl.ORDER_BY_JPQL);
744 }
745
746 String sql = query.toString();
747
748 Query q = session.createQuery(sql);
749
750 q.setFirstResult(0);
751 q.setMaxResults(2);
752
753 QueryPos qPos = QueryPos.getInstance(q);
754
755 qPos.add(groupId);
756
757 if (orderByComparator != null) {
758 Object[] values = orderByComparator.getOrderByValues(shoppingCoupon);
759
760 for (Object value : values) {
761 qPos.add(value);
762 }
763 }
764
765 List<ShoppingCoupon> list = q.list();
766
767 if (list.size() == 2) {
768 return list.get(1);
769 }
770 else {
771 return null;
772 }
773 }
774
775
783 public ShoppingCoupon findByCode(String code)
784 throws NoSuchCouponException, SystemException {
785 ShoppingCoupon shoppingCoupon = fetchByCode(code);
786
787 if (shoppingCoupon == null) {
788 StringBundler msg = new StringBundler(4);
789
790 msg.append(_NO_SUCH_ENTITY_WITH_KEY);
791
792 msg.append("code=");
793 msg.append(code);
794
795 msg.append(StringPool.CLOSE_CURLY_BRACE);
796
797 if (_log.isWarnEnabled()) {
798 _log.warn(msg.toString());
799 }
800
801 throw new NoSuchCouponException(msg.toString());
802 }
803
804 return shoppingCoupon;
805 }
806
807
814 public ShoppingCoupon fetchByCode(String code) throws SystemException {
815 return fetchByCode(code, true);
816 }
817
818
825 public ShoppingCoupon fetchByCode(String code, boolean retrieveFromCache)
826 throws SystemException {
827 Object[] finderArgs = new Object[] { code };
828
829 Object result = null;
830
831 if (retrieveFromCache) {
832 result = FinderCacheUtil.getResult(FINDER_PATH_FETCH_BY_CODE,
833 finderArgs, this);
834 }
835
836 if (result == null) {
837 Session session = null;
838
839 try {
840 session = openSession();
841
842 StringBundler query = new StringBundler(3);
843
844 query.append(_SQL_SELECT_SHOPPINGCOUPON_WHERE);
845
846 if (code == null) {
847 query.append(_FINDER_COLUMN_CODE_CODE_1);
848 }
849 else {
850 if (code.equals(StringPool.BLANK)) {
851 query.append(_FINDER_COLUMN_CODE_CODE_3);
852 }
853 else {
854 query.append(_FINDER_COLUMN_CODE_CODE_2);
855 }
856 }
857
858 query.append(ShoppingCouponModelImpl.ORDER_BY_JPQL);
859
860 String sql = query.toString();
861
862 Query q = session.createQuery(sql);
863
864 QueryPos qPos = QueryPos.getInstance(q);
865
866 if (code != null) {
867 qPos.add(code);
868 }
869
870 List<ShoppingCoupon> list = q.list();
871
872 result = list;
873
874 ShoppingCoupon shoppingCoupon = null;
875
876 if (list.isEmpty()) {
877 FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_CODE,
878 finderArgs, list);
879 }
880 else {
881 shoppingCoupon = list.get(0);
882
883 cacheResult(shoppingCoupon);
884
885 if ((shoppingCoupon.getCode() == null) ||
886 !shoppingCoupon.getCode().equals(code)) {
887 FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_CODE,
888 finderArgs, shoppingCoupon);
889 }
890 }
891
892 return shoppingCoupon;
893 }
894 catch (Exception e) {
895 throw processException(e);
896 }
897 finally {
898 if (result == null) {
899 FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_CODE,
900 finderArgs, new ArrayList<ShoppingCoupon>());
901 }
902
903 closeSession(session);
904 }
905 }
906 else {
907 if (result instanceof List<?>) {
908 return null;
909 }
910 else {
911 return (ShoppingCoupon)result;
912 }
913 }
914 }
915
916
922 public List<ShoppingCoupon> findAll() throws SystemException {
923 return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
924 }
925
926
938 public List<ShoppingCoupon> findAll(int start, int end)
939 throws SystemException {
940 return findAll(start, end, null);
941 }
942
943
956 public List<ShoppingCoupon> findAll(int start, int end,
957 OrderByComparator orderByComparator) throws SystemException {
958 Object[] finderArgs = new Object[] {
959 String.valueOf(start), String.valueOf(end),
960 String.valueOf(orderByComparator)
961 };
962
963 List<ShoppingCoupon> list = (List<ShoppingCoupon>)FinderCacheUtil.getResult(FINDER_PATH_FIND_ALL,
964 finderArgs, this);
965
966 if (list == null) {
967 Session session = null;
968
969 try {
970 session = openSession();
971
972 StringBundler query = null;
973 String sql = null;
974
975 if (orderByComparator != null) {
976 query = new StringBundler(2 +
977 (orderByComparator.getOrderByFields().length * 3));
978
979 query.append(_SQL_SELECT_SHOPPINGCOUPON);
980
981 appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
982 orderByComparator);
983
984 sql = query.toString();
985 }
986 else {
987 sql = _SQL_SELECT_SHOPPINGCOUPON.concat(ShoppingCouponModelImpl.ORDER_BY_JPQL);
988 }
989
990 Query q = session.createQuery(sql);
991
992 if (orderByComparator == null) {
993 list = (List<ShoppingCoupon>)QueryUtil.list(q,
994 getDialect(), start, end, false);
995
996 Collections.sort(list);
997 }
998 else {
999 list = (List<ShoppingCoupon>)QueryUtil.list(q,
1000 getDialect(), start, end);
1001 }
1002 }
1003 catch (Exception e) {
1004 throw processException(e);
1005 }
1006 finally {
1007 if (list == null) {
1008 list = new ArrayList<ShoppingCoupon>();
1009 }
1010
1011 cacheResult(list);
1012
1013 FinderCacheUtil.putResult(FINDER_PATH_FIND_ALL, finderArgs, list);
1014
1015 closeSession(session);
1016 }
1017 }
1018
1019 return list;
1020 }
1021
1022
1028 public void removeByGroupId(long groupId) throws SystemException {
1029 for (ShoppingCoupon shoppingCoupon : findByGroupId(groupId)) {
1030 remove(shoppingCoupon);
1031 }
1032 }
1033
1034
1040 public void removeByCode(String code)
1041 throws NoSuchCouponException, SystemException {
1042 ShoppingCoupon shoppingCoupon = findByCode(code);
1043
1044 remove(shoppingCoupon);
1045 }
1046
1047
1052 public void removeAll() throws SystemException {
1053 for (ShoppingCoupon shoppingCoupon : findAll()) {
1054 remove(shoppingCoupon);
1055 }
1056 }
1057
1058
1065 public int countByGroupId(long groupId) throws SystemException {
1066 Object[] finderArgs = new Object[] { groupId };
1067
1068 Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_GROUPID,
1069 finderArgs, this);
1070
1071 if (count == null) {
1072 Session session = null;
1073
1074 try {
1075 session = openSession();
1076
1077 StringBundler query = new StringBundler(2);
1078
1079 query.append(_SQL_COUNT_SHOPPINGCOUPON_WHERE);
1080
1081 query.append(_FINDER_COLUMN_GROUPID_GROUPID_2);
1082
1083 String sql = query.toString();
1084
1085 Query q = session.createQuery(sql);
1086
1087 QueryPos qPos = QueryPos.getInstance(q);
1088
1089 qPos.add(groupId);
1090
1091 count = (Long)q.uniqueResult();
1092 }
1093 catch (Exception e) {
1094 throw processException(e);
1095 }
1096 finally {
1097 if (count == null) {
1098 count = Long.valueOf(0);
1099 }
1100
1101 FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_GROUPID,
1102 finderArgs, count);
1103
1104 closeSession(session);
1105 }
1106 }
1107
1108 return count.intValue();
1109 }
1110
1111
1118 public int countByCode(String code) throws SystemException {
1119 Object[] finderArgs = new Object[] { code };
1120
1121 Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_CODE,
1122 finderArgs, this);
1123
1124 if (count == null) {
1125 Session session = null;
1126
1127 try {
1128 session = openSession();
1129
1130 StringBundler query = new StringBundler(2);
1131
1132 query.append(_SQL_COUNT_SHOPPINGCOUPON_WHERE);
1133
1134 if (code == null) {
1135 query.append(_FINDER_COLUMN_CODE_CODE_1);
1136 }
1137 else {
1138 if (code.equals(StringPool.BLANK)) {
1139 query.append(_FINDER_COLUMN_CODE_CODE_3);
1140 }
1141 else {
1142 query.append(_FINDER_COLUMN_CODE_CODE_2);
1143 }
1144 }
1145
1146 String sql = query.toString();
1147
1148 Query q = session.createQuery(sql);
1149
1150 QueryPos qPos = QueryPos.getInstance(q);
1151
1152 if (code != null) {
1153 qPos.add(code);
1154 }
1155
1156 count = (Long)q.uniqueResult();
1157 }
1158 catch (Exception e) {
1159 throw processException(e);
1160 }
1161 finally {
1162 if (count == null) {
1163 count = Long.valueOf(0);
1164 }
1165
1166 FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_CODE,
1167 finderArgs, count);
1168
1169 closeSession(session);
1170 }
1171 }
1172
1173 return count.intValue();
1174 }
1175
1176
1182 public int countAll() throws SystemException {
1183 Object[] finderArgs = new Object[0];
1184
1185 Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
1186 finderArgs, this);
1187
1188 if (count == null) {
1189 Session session = null;
1190
1191 try {
1192 session = openSession();
1193
1194 Query q = session.createQuery(_SQL_COUNT_SHOPPINGCOUPON);
1195
1196 count = (Long)q.uniqueResult();
1197 }
1198 catch (Exception e) {
1199 throw processException(e);
1200 }
1201 finally {
1202 if (count == null) {
1203 count = Long.valueOf(0);
1204 }
1205
1206 FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL, finderArgs,
1207 count);
1208
1209 closeSession(session);
1210 }
1211 }
1212
1213 return count.intValue();
1214 }
1215
1216
1219 public void afterPropertiesSet() {
1220 String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
1221 com.liferay.portal.util.PropsUtil.get(
1222 "value.object.listener.com.liferay.portlet.shopping.model.ShoppingCoupon")));
1223
1224 if (listenerClassNames.length > 0) {
1225 try {
1226 List<ModelListener<ShoppingCoupon>> listenersList = new ArrayList<ModelListener<ShoppingCoupon>>();
1227
1228 for (String listenerClassName : listenerClassNames) {
1229 listenersList.add((ModelListener<ShoppingCoupon>)InstanceFactory.newInstance(
1230 listenerClassName));
1231 }
1232
1233 listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
1234 }
1235 catch (Exception e) {
1236 _log.error(e);
1237 }
1238 }
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 }