001
014
015 package com.liferay.portlet.shopping.service.persistence;
016
017 import com.liferay.portal.NoSuchModelException;
018 import com.liferay.portal.kernel.bean.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.model.CacheModel;
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
066 public class ShoppingCouponPersistenceImpl extends BasePersistenceImpl<ShoppingCoupon>
067 implements ShoppingCouponPersistence {
068
073 public static final String FINDER_CLASS_NAME_ENTITY = ShoppingCouponImpl.class.getName();
074 public static final String FINDER_CLASS_NAME_LIST_WITH_PAGINATION = FINDER_CLASS_NAME_ENTITY +
075 ".List1";
076 public static final String FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION = FINDER_CLASS_NAME_ENTITY +
077 ".List2";
078 public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_GROUPID = new FinderPath(ShoppingCouponModelImpl.ENTITY_CACHE_ENABLED,
079 ShoppingCouponModelImpl.FINDER_CACHE_ENABLED,
080 ShoppingCouponImpl.class, FINDER_CLASS_NAME_LIST_WITH_PAGINATION,
081 "findByGroupId",
082 new String[] {
083 Long.class.getName(),
084
085 "java.lang.Integer", "java.lang.Integer",
086 "com.liferay.portal.kernel.util.OrderByComparator"
087 });
088 public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_GROUPID =
089 new FinderPath(ShoppingCouponModelImpl.ENTITY_CACHE_ENABLED,
090 ShoppingCouponModelImpl.FINDER_CACHE_ENABLED,
091 ShoppingCouponImpl.class,
092 FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findByGroupId",
093 new String[] { Long.class.getName() },
094 ShoppingCouponModelImpl.GROUPID_COLUMN_BITMASK);
095 public static final FinderPath FINDER_PATH_COUNT_BY_GROUPID = new FinderPath(ShoppingCouponModelImpl.ENTITY_CACHE_ENABLED,
096 ShoppingCouponModelImpl.FINDER_CACHE_ENABLED, Long.class,
097 FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByGroupId",
098 new String[] { Long.class.getName() });
099 public static final FinderPath FINDER_PATH_FETCH_BY_CODE = new FinderPath(ShoppingCouponModelImpl.ENTITY_CACHE_ENABLED,
100 ShoppingCouponModelImpl.FINDER_CACHE_ENABLED,
101 ShoppingCouponImpl.class, FINDER_CLASS_NAME_ENTITY, "fetchByCode",
102 new String[] { String.class.getName() },
103 ShoppingCouponModelImpl.CODE_COLUMN_BITMASK);
104 public static final FinderPath FINDER_PATH_COUNT_BY_CODE = new FinderPath(ShoppingCouponModelImpl.ENTITY_CACHE_ENABLED,
105 ShoppingCouponModelImpl.FINDER_CACHE_ENABLED, Long.class,
106 FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByCode",
107 new String[] { String.class.getName() });
108 public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_ALL = new FinderPath(ShoppingCouponModelImpl.ENTITY_CACHE_ENABLED,
109 ShoppingCouponModelImpl.FINDER_CACHE_ENABLED,
110 ShoppingCouponImpl.class,
111 FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findAll", new String[0]);
112 public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL = new FinderPath(ShoppingCouponModelImpl.ENTITY_CACHE_ENABLED,
113 ShoppingCouponModelImpl.FINDER_CACHE_ENABLED,
114 ShoppingCouponImpl.class, FINDER_CLASS_NAME_LIST_WITH_PAGINATION,
115 "findAll", new String[0]);
116 public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(ShoppingCouponModelImpl.ENTITY_CACHE_ENABLED,
117 ShoppingCouponModelImpl.FINDER_CACHE_ENABLED, Long.class,
118 FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countAll", new String[0]);
119
120
125 public void cacheResult(ShoppingCoupon shoppingCoupon) {
126 EntityCacheUtil.putResult(ShoppingCouponModelImpl.ENTITY_CACHE_ENABLED,
127 ShoppingCouponImpl.class, shoppingCoupon.getPrimaryKey(),
128 shoppingCoupon);
129
130 FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_CODE,
131 new Object[] { shoppingCoupon.getCode() }, shoppingCoupon);
132
133 shoppingCoupon.resetOriginalValues();
134 }
135
136
141 public void cacheResult(List<ShoppingCoupon> shoppingCoupons) {
142 for (ShoppingCoupon shoppingCoupon : shoppingCoupons) {
143 if (EntityCacheUtil.getResult(
144 ShoppingCouponModelImpl.ENTITY_CACHE_ENABLED,
145 ShoppingCouponImpl.class, shoppingCoupon.getPrimaryKey()) == null) {
146 cacheResult(shoppingCoupon);
147 }
148 else {
149 shoppingCoupon.resetOriginalValues();
150 }
151 }
152 }
153
154
161 @Override
162 public void clearCache() {
163 if (_HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE) {
164 CacheRegistryUtil.clear(ShoppingCouponImpl.class.getName());
165 }
166
167 EntityCacheUtil.clearCache(ShoppingCouponImpl.class.getName());
168
169 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
170 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
171 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
172 }
173
174
181 @Override
182 public void clearCache(ShoppingCoupon shoppingCoupon) {
183 EntityCacheUtil.removeResult(ShoppingCouponModelImpl.ENTITY_CACHE_ENABLED,
184 ShoppingCouponImpl.class, shoppingCoupon.getPrimaryKey());
185
186 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
187 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
188
189 clearUniqueFindersCache(shoppingCoupon);
190 }
191
192 @Override
193 public void clearCache(List<ShoppingCoupon> shoppingCoupons) {
194 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
195 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
196
197 for (ShoppingCoupon shoppingCoupon : shoppingCoupons) {
198 EntityCacheUtil.removeResult(ShoppingCouponModelImpl.ENTITY_CACHE_ENABLED,
199 ShoppingCouponImpl.class, shoppingCoupon.getPrimaryKey());
200
201 clearUniqueFindersCache(shoppingCoupon);
202 }
203 }
204
205 protected void clearUniqueFindersCache(ShoppingCoupon shoppingCoupon) {
206 FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_CODE,
207 new Object[] { shoppingCoupon.getCode() });
208 }
209
210
216 public ShoppingCoupon create(long couponId) {
217 ShoppingCoupon shoppingCoupon = new ShoppingCouponImpl();
218
219 shoppingCoupon.setNew(true);
220 shoppingCoupon.setPrimaryKey(couponId);
221
222 return shoppingCoupon;
223 }
224
225
233 public ShoppingCoupon remove(long couponId)
234 throws NoSuchCouponException, SystemException {
235 return remove(Long.valueOf(couponId));
236 }
237
238
246 @Override
247 public ShoppingCoupon remove(Serializable primaryKey)
248 throws NoSuchCouponException, SystemException {
249 Session session = null;
250
251 try {
252 session = openSession();
253
254 ShoppingCoupon shoppingCoupon = (ShoppingCoupon)session.get(ShoppingCouponImpl.class,
255 primaryKey);
256
257 if (shoppingCoupon == null) {
258 if (_log.isWarnEnabled()) {
259 _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
260 }
261
262 throw new NoSuchCouponException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
263 primaryKey);
264 }
265
266 return remove(shoppingCoupon);
267 }
268 catch (NoSuchCouponException nsee) {
269 throw nsee;
270 }
271 catch (Exception e) {
272 throw processException(e);
273 }
274 finally {
275 closeSession(session);
276 }
277 }
278
279 @Override
280 protected ShoppingCoupon removeImpl(ShoppingCoupon shoppingCoupon)
281 throws SystemException {
282 shoppingCoupon = toUnwrappedModel(shoppingCoupon);
283
284 Session session = null;
285
286 try {
287 session = openSession();
288
289 BatchSessionUtil.delete(session, shoppingCoupon);
290 }
291 catch (Exception e) {
292 throw processException(e);
293 }
294 finally {
295 closeSession(session);
296 }
297
298 clearCache(shoppingCoupon);
299
300 return shoppingCoupon;
301 }
302
303 @Override
304 public ShoppingCoupon updateImpl(
305 com.liferay.portlet.shopping.model.ShoppingCoupon shoppingCoupon,
306 boolean merge) throws SystemException {
307 shoppingCoupon = toUnwrappedModel(shoppingCoupon);
308
309 boolean isNew = shoppingCoupon.isNew();
310
311 ShoppingCouponModelImpl shoppingCouponModelImpl = (ShoppingCouponModelImpl)shoppingCoupon;
312
313 Session session = null;
314
315 try {
316 session = openSession();
317
318 BatchSessionUtil.update(session, shoppingCoupon, merge);
319
320 shoppingCoupon.setNew(false);
321 }
322 catch (Exception e) {
323 throw processException(e);
324 }
325 finally {
326 closeSession(session);
327 }
328
329 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
330
331 if (isNew || !ShoppingCouponModelImpl.COLUMN_BITMASK_ENABLED) {
332 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
333 }
334
335 else {
336 if ((shoppingCouponModelImpl.getColumnBitmask() &
337 FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_GROUPID.getColumnBitmask()) != 0) {
338 Object[] args = new Object[] {
339 Long.valueOf(shoppingCouponModelImpl.getOriginalGroupId())
340 };
341
342 FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_GROUPID, args);
343 FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_GROUPID,
344 args);
345
346 args = new Object[] {
347 Long.valueOf(shoppingCouponModelImpl.getGroupId())
348 };
349
350 FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_GROUPID, args);
351 FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_GROUPID,
352 args);
353 }
354 }
355
356 EntityCacheUtil.putResult(ShoppingCouponModelImpl.ENTITY_CACHE_ENABLED,
357 ShoppingCouponImpl.class, shoppingCoupon.getPrimaryKey(),
358 shoppingCoupon);
359
360 if (isNew) {
361 FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_CODE,
362 new Object[] { shoppingCoupon.getCode() }, shoppingCoupon);
363 }
364 else {
365 if ((shoppingCouponModelImpl.getColumnBitmask() &
366 FINDER_PATH_FETCH_BY_CODE.getColumnBitmask()) != 0) {
367 Object[] args = new Object[] {
368 shoppingCouponModelImpl.getOriginalCode()
369 };
370
371 FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_CODE, args);
372 FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_CODE, args);
373
374 FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_CODE,
375 new Object[] { shoppingCoupon.getCode() }, shoppingCoupon);
376 }
377 }
378
379 return shoppingCoupon;
380 }
381
382 protected ShoppingCoupon toUnwrappedModel(ShoppingCoupon shoppingCoupon) {
383 if (shoppingCoupon instanceof ShoppingCouponImpl) {
384 return shoppingCoupon;
385 }
386
387 ShoppingCouponImpl shoppingCouponImpl = new ShoppingCouponImpl();
388
389 shoppingCouponImpl.setNew(shoppingCoupon.isNew());
390 shoppingCouponImpl.setPrimaryKey(shoppingCoupon.getPrimaryKey());
391
392 shoppingCouponImpl.setCouponId(shoppingCoupon.getCouponId());
393 shoppingCouponImpl.setGroupId(shoppingCoupon.getGroupId());
394 shoppingCouponImpl.setCompanyId(shoppingCoupon.getCompanyId());
395 shoppingCouponImpl.setUserId(shoppingCoupon.getUserId());
396 shoppingCouponImpl.setUserName(shoppingCoupon.getUserName());
397 shoppingCouponImpl.setCreateDate(shoppingCoupon.getCreateDate());
398 shoppingCouponImpl.setModifiedDate(shoppingCoupon.getModifiedDate());
399 shoppingCouponImpl.setCode(shoppingCoupon.getCode());
400 shoppingCouponImpl.setName(shoppingCoupon.getName());
401 shoppingCouponImpl.setDescription(shoppingCoupon.getDescription());
402 shoppingCouponImpl.setStartDate(shoppingCoupon.getStartDate());
403 shoppingCouponImpl.setEndDate(shoppingCoupon.getEndDate());
404 shoppingCouponImpl.setActive(shoppingCoupon.isActive());
405 shoppingCouponImpl.setLimitCategories(shoppingCoupon.getLimitCategories());
406 shoppingCouponImpl.setLimitSkus(shoppingCoupon.getLimitSkus());
407 shoppingCouponImpl.setMinOrder(shoppingCoupon.getMinOrder());
408 shoppingCouponImpl.setDiscount(shoppingCoupon.getDiscount());
409 shoppingCouponImpl.setDiscountType(shoppingCoupon.getDiscountType());
410
411 return shoppingCouponImpl;
412 }
413
414
422 @Override
423 public ShoppingCoupon findByPrimaryKey(Serializable primaryKey)
424 throws NoSuchModelException, SystemException {
425 return findByPrimaryKey(((Long)primaryKey).longValue());
426 }
427
428
436 public ShoppingCoupon findByPrimaryKey(long couponId)
437 throws NoSuchCouponException, SystemException {
438 ShoppingCoupon shoppingCoupon = fetchByPrimaryKey(couponId);
439
440 if (shoppingCoupon == null) {
441 if (_log.isWarnEnabled()) {
442 _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + couponId);
443 }
444
445 throw new NoSuchCouponException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
446 couponId);
447 }
448
449 return shoppingCoupon;
450 }
451
452
459 @Override
460 public ShoppingCoupon fetchByPrimaryKey(Serializable primaryKey)
461 throws SystemException {
462 return fetchByPrimaryKey(((Long)primaryKey).longValue());
463 }
464
465
472 public ShoppingCoupon fetchByPrimaryKey(long couponId)
473 throws SystemException {
474 ShoppingCoupon shoppingCoupon = (ShoppingCoupon)EntityCacheUtil.getResult(ShoppingCouponModelImpl.ENTITY_CACHE_ENABLED,
475 ShoppingCouponImpl.class, couponId);
476
477 if (shoppingCoupon == _nullShoppingCoupon) {
478 return null;
479 }
480
481 if (shoppingCoupon == null) {
482 Session session = null;
483
484 boolean hasException = false;
485
486 try {
487 session = openSession();
488
489 shoppingCoupon = (ShoppingCoupon)session.get(ShoppingCouponImpl.class,
490 Long.valueOf(couponId));
491 }
492 catch (Exception e) {
493 hasException = true;
494
495 throw processException(e);
496 }
497 finally {
498 if (shoppingCoupon != null) {
499 cacheResult(shoppingCoupon);
500 }
501 else if (!hasException) {
502 EntityCacheUtil.putResult(ShoppingCouponModelImpl.ENTITY_CACHE_ENABLED,
503 ShoppingCouponImpl.class, couponId, _nullShoppingCoupon);
504 }
505
506 closeSession(session);
507 }
508 }
509
510 return shoppingCoupon;
511 }
512
513
520 public List<ShoppingCoupon> findByGroupId(long groupId)
521 throws SystemException {
522 return findByGroupId(groupId, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
523 }
524
525
538 public List<ShoppingCoupon> findByGroupId(long groupId, int start, int end)
539 throws SystemException {
540 return findByGroupId(groupId, start, end, null);
541 }
542
543
557 public List<ShoppingCoupon> findByGroupId(long groupId, int start, int end,
558 OrderByComparator orderByComparator) throws SystemException {
559 FinderPath finderPath = null;
560 Object[] finderArgs = null;
561
562 if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
563 (orderByComparator == null)) {
564 finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_GROUPID;
565 finderArgs = new Object[] { groupId };
566 }
567 else {
568 finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_GROUPID;
569 finderArgs = new Object[] { groupId, start, end, orderByComparator };
570 }
571
572 List<ShoppingCoupon> list = (List<ShoppingCoupon>)FinderCacheUtil.getResult(finderPath,
573 finderArgs, this);
574
575 if (list == null) {
576 StringBundler query = null;
577
578 if (orderByComparator != null) {
579 query = new StringBundler(3 +
580 (orderByComparator.getOrderByFields().length * 3));
581 }
582 else {
583 query = new StringBundler(3);
584 }
585
586 query.append(_SQL_SELECT_SHOPPINGCOUPON_WHERE);
587
588 query.append(_FINDER_COLUMN_GROUPID_GROUPID_2);
589
590 if (orderByComparator != null) {
591 appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
592 orderByComparator);
593 }
594
595 else {
596 query.append(ShoppingCouponModelImpl.ORDER_BY_JPQL);
597 }
598
599 String sql = query.toString();
600
601 Session session = null;
602
603 try {
604 session = openSession();
605
606 Query q = session.createQuery(sql);
607
608 QueryPos qPos = QueryPos.getInstance(q);
609
610 qPos.add(groupId);
611
612 list = (List<ShoppingCoupon>)QueryUtil.list(q, getDialect(),
613 start, end);
614 }
615 catch (Exception e) {
616 throw processException(e);
617 }
618 finally {
619 if (list == null) {
620 FinderCacheUtil.removeResult(finderPath, finderArgs);
621 }
622 else {
623 cacheResult(list);
624
625 FinderCacheUtil.putResult(finderPath, finderArgs, list);
626 }
627
628 closeSession(session);
629 }
630 }
631
632 return list;
633 }
634
635
648 public ShoppingCoupon findByGroupId_First(long groupId,
649 OrderByComparator orderByComparator)
650 throws NoSuchCouponException, SystemException {
651 List<ShoppingCoupon> list = findByGroupId(groupId, 0, 1,
652 orderByComparator);
653
654 if (list.isEmpty()) {
655 StringBundler msg = new StringBundler(4);
656
657 msg.append(_NO_SUCH_ENTITY_WITH_KEY);
658
659 msg.append("groupId=");
660 msg.append(groupId);
661
662 msg.append(StringPool.CLOSE_CURLY_BRACE);
663
664 throw new NoSuchCouponException(msg.toString());
665 }
666 else {
667 return list.get(0);
668 }
669 }
670
671
684 public ShoppingCoupon findByGroupId_Last(long groupId,
685 OrderByComparator orderByComparator)
686 throws NoSuchCouponException, SystemException {
687 int count = countByGroupId(groupId);
688
689 List<ShoppingCoupon> list = findByGroupId(groupId, count - 1, count,
690 orderByComparator);
691
692 if (list.isEmpty()) {
693 StringBundler msg = new StringBundler(4);
694
695 msg.append(_NO_SUCH_ENTITY_WITH_KEY);
696
697 msg.append("groupId=");
698 msg.append(groupId);
699
700 msg.append(StringPool.CLOSE_CURLY_BRACE);
701
702 throw new NoSuchCouponException(msg.toString());
703 }
704 else {
705 return list.get(0);
706 }
707 }
708
709
723 public ShoppingCoupon[] findByGroupId_PrevAndNext(long couponId,
724 long groupId, OrderByComparator orderByComparator)
725 throws NoSuchCouponException, SystemException {
726 ShoppingCoupon shoppingCoupon = findByPrimaryKey(couponId);
727
728 Session session = null;
729
730 try {
731 session = openSession();
732
733 ShoppingCoupon[] array = new ShoppingCouponImpl[3];
734
735 array[0] = getByGroupId_PrevAndNext(session, shoppingCoupon,
736 groupId, orderByComparator, true);
737
738 array[1] = shoppingCoupon;
739
740 array[2] = getByGroupId_PrevAndNext(session, shoppingCoupon,
741 groupId, orderByComparator, false);
742
743 return array;
744 }
745 catch (Exception e) {
746 throw processException(e);
747 }
748 finally {
749 closeSession(session);
750 }
751 }
752
753 protected ShoppingCoupon getByGroupId_PrevAndNext(Session session,
754 ShoppingCoupon shoppingCoupon, long groupId,
755 OrderByComparator orderByComparator, boolean previous) {
756 StringBundler query = null;
757
758 if (orderByComparator != null) {
759 query = new StringBundler(6 +
760 (orderByComparator.getOrderByFields().length * 6));
761 }
762 else {
763 query = new StringBundler(3);
764 }
765
766 query.append(_SQL_SELECT_SHOPPINGCOUPON_WHERE);
767
768 query.append(_FINDER_COLUMN_GROUPID_GROUPID_2);
769
770 if (orderByComparator != null) {
771 String[] orderByConditionFields = orderByComparator.getOrderByConditionFields();
772
773 if (orderByConditionFields.length > 0) {
774 query.append(WHERE_AND);
775 }
776
777 for (int i = 0; i < orderByConditionFields.length; i++) {
778 query.append(_ORDER_BY_ENTITY_ALIAS);
779 query.append(orderByConditionFields[i]);
780
781 if ((i + 1) < orderByConditionFields.length) {
782 if (orderByComparator.isAscending() ^ previous) {
783 query.append(WHERE_GREATER_THAN_HAS_NEXT);
784 }
785 else {
786 query.append(WHERE_LESSER_THAN_HAS_NEXT);
787 }
788 }
789 else {
790 if (orderByComparator.isAscending() ^ previous) {
791 query.append(WHERE_GREATER_THAN);
792 }
793 else {
794 query.append(WHERE_LESSER_THAN);
795 }
796 }
797 }
798
799 query.append(ORDER_BY_CLAUSE);
800
801 String[] orderByFields = orderByComparator.getOrderByFields();
802
803 for (int i = 0; i < orderByFields.length; i++) {
804 query.append(_ORDER_BY_ENTITY_ALIAS);
805 query.append(orderByFields[i]);
806
807 if ((i + 1) < orderByFields.length) {
808 if (orderByComparator.isAscending() ^ previous) {
809 query.append(ORDER_BY_ASC_HAS_NEXT);
810 }
811 else {
812 query.append(ORDER_BY_DESC_HAS_NEXT);
813 }
814 }
815 else {
816 if (orderByComparator.isAscending() ^ previous) {
817 query.append(ORDER_BY_ASC);
818 }
819 else {
820 query.append(ORDER_BY_DESC);
821 }
822 }
823 }
824 }
825
826 else {
827 query.append(ShoppingCouponModelImpl.ORDER_BY_JPQL);
828 }
829
830 String sql = query.toString();
831
832 Query q = session.createQuery(sql);
833
834 q.setFirstResult(0);
835 q.setMaxResults(2);
836
837 QueryPos qPos = QueryPos.getInstance(q);
838
839 qPos.add(groupId);
840
841 if (orderByComparator != null) {
842 Object[] values = orderByComparator.getOrderByConditionValues(shoppingCoupon);
843
844 for (Object value : values) {
845 qPos.add(value);
846 }
847 }
848
849 List<ShoppingCoupon> list = q.list();
850
851 if (list.size() == 2) {
852 return list.get(1);
853 }
854 else {
855 return null;
856 }
857 }
858
859
867 public ShoppingCoupon findByCode(String code)
868 throws NoSuchCouponException, SystemException {
869 ShoppingCoupon shoppingCoupon = fetchByCode(code);
870
871 if (shoppingCoupon == null) {
872 StringBundler msg = new StringBundler(4);
873
874 msg.append(_NO_SUCH_ENTITY_WITH_KEY);
875
876 msg.append("code=");
877 msg.append(code);
878
879 msg.append(StringPool.CLOSE_CURLY_BRACE);
880
881 if (_log.isWarnEnabled()) {
882 _log.warn(msg.toString());
883 }
884
885 throw new NoSuchCouponException(msg.toString());
886 }
887
888 return shoppingCoupon;
889 }
890
891
898 public ShoppingCoupon fetchByCode(String code) throws SystemException {
899 return fetchByCode(code, true);
900 }
901
902
910 public ShoppingCoupon fetchByCode(String code, boolean retrieveFromCache)
911 throws SystemException {
912 Object[] finderArgs = new Object[] { code };
913
914 Object result = null;
915
916 if (retrieveFromCache) {
917 result = FinderCacheUtil.getResult(FINDER_PATH_FETCH_BY_CODE,
918 finderArgs, this);
919 }
920
921 if (result == null) {
922 StringBundler query = new StringBundler(3);
923
924 query.append(_SQL_SELECT_SHOPPINGCOUPON_WHERE);
925
926 if (code == null) {
927 query.append(_FINDER_COLUMN_CODE_CODE_1);
928 }
929 else {
930 if (code.equals(StringPool.BLANK)) {
931 query.append(_FINDER_COLUMN_CODE_CODE_3);
932 }
933 else {
934 query.append(_FINDER_COLUMN_CODE_CODE_2);
935 }
936 }
937
938 query.append(ShoppingCouponModelImpl.ORDER_BY_JPQL);
939
940 String sql = query.toString();
941
942 Session session = null;
943
944 try {
945 session = openSession();
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 List<ShoppingCoupon> list = q.list();
956
957 result = list;
958
959 ShoppingCoupon shoppingCoupon = null;
960
961 if (list.isEmpty()) {
962 FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_CODE,
963 finderArgs, list);
964 }
965 else {
966 shoppingCoupon = list.get(0);
967
968 cacheResult(shoppingCoupon);
969
970 if ((shoppingCoupon.getCode() == null) ||
971 !shoppingCoupon.getCode().equals(code)) {
972 FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_CODE,
973 finderArgs, shoppingCoupon);
974 }
975 }
976
977 return shoppingCoupon;
978 }
979 catch (Exception e) {
980 throw processException(e);
981 }
982 finally {
983 if (result == null) {
984 FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_CODE,
985 finderArgs);
986 }
987
988 closeSession(session);
989 }
990 }
991 else {
992 if (result instanceof List<?>) {
993 return null;
994 }
995 else {
996 return (ShoppingCoupon)result;
997 }
998 }
999 }
1000
1001
1007 public List<ShoppingCoupon> findAll() throws SystemException {
1008 return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
1009 }
1010
1011
1023 public List<ShoppingCoupon> findAll(int start, int end)
1024 throws SystemException {
1025 return findAll(start, end, null);
1026 }
1027
1028
1041 public List<ShoppingCoupon> findAll(int start, int end,
1042 OrderByComparator orderByComparator) throws SystemException {
1043 FinderPath finderPath = null;
1044 Object[] finderArgs = new Object[] { start, end, orderByComparator };
1045
1046 if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
1047 (orderByComparator == null)) {
1048 finderPath = FINDER_PATH_WITH_PAGINATION_FIND_ALL;
1049 finderArgs = FINDER_ARGS_EMPTY;
1050 }
1051 else {
1052 finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL;
1053 finderArgs = new Object[] { start, end, orderByComparator };
1054 }
1055
1056 List<ShoppingCoupon> list = (List<ShoppingCoupon>)FinderCacheUtil.getResult(finderPath,
1057 finderArgs, this);
1058
1059 if (list == null) {
1060 StringBundler query = null;
1061 String sql = null;
1062
1063 if (orderByComparator != null) {
1064 query = new StringBundler(2 +
1065 (orderByComparator.getOrderByFields().length * 3));
1066
1067 query.append(_SQL_SELECT_SHOPPINGCOUPON);
1068
1069 appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
1070 orderByComparator);
1071
1072 sql = query.toString();
1073 }
1074 else {
1075 sql = _SQL_SELECT_SHOPPINGCOUPON.concat(ShoppingCouponModelImpl.ORDER_BY_JPQL);
1076 }
1077
1078 Session session = null;
1079
1080 try {
1081 session = openSession();
1082
1083 Query q = session.createQuery(sql);
1084
1085 if (orderByComparator == null) {
1086 list = (List<ShoppingCoupon>)QueryUtil.list(q,
1087 getDialect(), start, end, false);
1088
1089 Collections.sort(list);
1090 }
1091 else {
1092 list = (List<ShoppingCoupon>)QueryUtil.list(q,
1093 getDialect(), start, end);
1094 }
1095 }
1096 catch (Exception e) {
1097 throw processException(e);
1098 }
1099 finally {
1100 if (list == null) {
1101 FinderCacheUtil.removeResult(finderPath, finderArgs);
1102 }
1103 else {
1104 cacheResult(list);
1105
1106 FinderCacheUtil.putResult(finderPath, finderArgs, list);
1107 }
1108
1109 closeSession(session);
1110 }
1111 }
1112
1113 return list;
1114 }
1115
1116
1122 public void removeByGroupId(long groupId) throws SystemException {
1123 for (ShoppingCoupon shoppingCoupon : findByGroupId(groupId)) {
1124 remove(shoppingCoupon);
1125 }
1126 }
1127
1128
1134 public void removeByCode(String code)
1135 throws NoSuchCouponException, SystemException {
1136 ShoppingCoupon shoppingCoupon = findByCode(code);
1137
1138 remove(shoppingCoupon);
1139 }
1140
1141
1146 public void removeAll() throws SystemException {
1147 for (ShoppingCoupon shoppingCoupon : findAll()) {
1148 remove(shoppingCoupon);
1149 }
1150 }
1151
1152
1159 public int countByGroupId(long groupId) throws SystemException {
1160 Object[] finderArgs = new Object[] { groupId };
1161
1162 Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_GROUPID,
1163 finderArgs, this);
1164
1165 if (count == null) {
1166 StringBundler query = new StringBundler(2);
1167
1168 query.append(_SQL_COUNT_SHOPPINGCOUPON_WHERE);
1169
1170 query.append(_FINDER_COLUMN_GROUPID_GROUPID_2);
1171
1172 String sql = query.toString();
1173
1174 Session session = null;
1175
1176 try {
1177 session = openSession();
1178
1179 Query q = session.createQuery(sql);
1180
1181 QueryPos qPos = QueryPos.getInstance(q);
1182
1183 qPos.add(groupId);
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_BY_GROUPID,
1196 finderArgs, count);
1197
1198 closeSession(session);
1199 }
1200 }
1201
1202 return count.intValue();
1203 }
1204
1205
1212 public int countByCode(String code) throws SystemException {
1213 Object[] finderArgs = new Object[] { code };
1214
1215 Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_CODE,
1216 finderArgs, this);
1217
1218 if (count == null) {
1219 StringBundler query = new StringBundler(2);
1220
1221 query.append(_SQL_COUNT_SHOPPINGCOUPON_WHERE);
1222
1223 if (code == null) {
1224 query.append(_FINDER_COLUMN_CODE_CODE_1);
1225 }
1226 else {
1227 if (code.equals(StringPool.BLANK)) {
1228 query.append(_FINDER_COLUMN_CODE_CODE_3);
1229 }
1230 else {
1231 query.append(_FINDER_COLUMN_CODE_CODE_2);
1232 }
1233 }
1234
1235 String sql = query.toString();
1236
1237 Session session = null;
1238
1239 try {
1240 session = openSession();
1241
1242 Query q = session.createQuery(sql);
1243
1244 QueryPos qPos = QueryPos.getInstance(q);
1245
1246 if (code != null) {
1247 qPos.add(code);
1248 }
1249
1250 count = (Long)q.uniqueResult();
1251 }
1252 catch (Exception e) {
1253 throw processException(e);
1254 }
1255 finally {
1256 if (count == null) {
1257 count = Long.valueOf(0);
1258 }
1259
1260 FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_CODE,
1261 finderArgs, count);
1262
1263 closeSession(session);
1264 }
1265 }
1266
1267 return count.intValue();
1268 }
1269
1270
1276 public int countAll() throws SystemException {
1277 Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
1278 FINDER_ARGS_EMPTY, this);
1279
1280 if (count == null) {
1281 Session session = null;
1282
1283 try {
1284 session = openSession();
1285
1286 Query q = session.createQuery(_SQL_COUNT_SHOPPINGCOUPON);
1287
1288 count = (Long)q.uniqueResult();
1289 }
1290 catch (Exception e) {
1291 throw processException(e);
1292 }
1293 finally {
1294 if (count == null) {
1295 count = Long.valueOf(0);
1296 }
1297
1298 FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL,
1299 FINDER_ARGS_EMPTY, count);
1300
1301 closeSession(session);
1302 }
1303 }
1304
1305 return count.intValue();
1306 }
1307
1308
1311 public void afterPropertiesSet() {
1312 String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
1313 com.liferay.portal.util.PropsUtil.get(
1314 "value.object.listener.com.liferay.portlet.shopping.model.ShoppingCoupon")));
1315
1316 if (listenerClassNames.length > 0) {
1317 try {
1318 List<ModelListener<ShoppingCoupon>> listenersList = new ArrayList<ModelListener<ShoppingCoupon>>();
1319
1320 for (String listenerClassName : listenerClassNames) {
1321 listenersList.add((ModelListener<ShoppingCoupon>)InstanceFactory.newInstance(
1322 listenerClassName));
1323 }
1324
1325 listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
1326 }
1327 catch (Exception e) {
1328 _log.error(e);
1329 }
1330 }
1331 }
1332
1333 public void destroy() {
1334 EntityCacheUtil.removeCache(ShoppingCouponImpl.class.getName());
1335 FinderCacheUtil.removeCache(FINDER_CLASS_NAME_ENTITY);
1336 FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
1337 }
1338
1339 @BeanReference(type = ShoppingCartPersistence.class)
1340 protected ShoppingCartPersistence shoppingCartPersistence;
1341 @BeanReference(type = ShoppingCategoryPersistence.class)
1342 protected ShoppingCategoryPersistence shoppingCategoryPersistence;
1343 @BeanReference(type = ShoppingCouponPersistence.class)
1344 protected ShoppingCouponPersistence shoppingCouponPersistence;
1345 @BeanReference(type = ShoppingItemPersistence.class)
1346 protected ShoppingItemPersistence shoppingItemPersistence;
1347 @BeanReference(type = ShoppingItemFieldPersistence.class)
1348 protected ShoppingItemFieldPersistence shoppingItemFieldPersistence;
1349 @BeanReference(type = ShoppingItemPricePersistence.class)
1350 protected ShoppingItemPricePersistence shoppingItemPricePersistence;
1351 @BeanReference(type = ShoppingOrderPersistence.class)
1352 protected ShoppingOrderPersistence shoppingOrderPersistence;
1353 @BeanReference(type = ShoppingOrderItemPersistence.class)
1354 protected ShoppingOrderItemPersistence shoppingOrderItemPersistence;
1355 @BeanReference(type = ResourcePersistence.class)
1356 protected ResourcePersistence resourcePersistence;
1357 @BeanReference(type = UserPersistence.class)
1358 protected UserPersistence userPersistence;
1359 private static final String _SQL_SELECT_SHOPPINGCOUPON = "SELECT shoppingCoupon FROM ShoppingCoupon shoppingCoupon";
1360 private static final String _SQL_SELECT_SHOPPINGCOUPON_WHERE = "SELECT shoppingCoupon FROM ShoppingCoupon shoppingCoupon WHERE ";
1361 private static final String _SQL_COUNT_SHOPPINGCOUPON = "SELECT COUNT(shoppingCoupon) FROM ShoppingCoupon shoppingCoupon";
1362 private static final String _SQL_COUNT_SHOPPINGCOUPON_WHERE = "SELECT COUNT(shoppingCoupon) FROM ShoppingCoupon shoppingCoupon WHERE ";
1363 private static final String _FINDER_COLUMN_GROUPID_GROUPID_2 = "shoppingCoupon.groupId = ?";
1364 private static final String _FINDER_COLUMN_CODE_CODE_1 = "shoppingCoupon.code IS NULL";
1365 private static final String _FINDER_COLUMN_CODE_CODE_2 = "shoppingCoupon.code = ?";
1366 private static final String _FINDER_COLUMN_CODE_CODE_3 = "(shoppingCoupon.code IS NULL OR shoppingCoupon.code = ?)";
1367 private static final String _ORDER_BY_ENTITY_ALIAS = "shoppingCoupon.";
1368 private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No ShoppingCoupon exists with the primary key ";
1369 private static final String _NO_SUCH_ENTITY_WITH_KEY = "No ShoppingCoupon exists with the key {";
1370 private static final boolean _HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE = com.liferay.portal.util.PropsValues.HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE;
1371 private static Log _log = LogFactoryUtil.getLog(ShoppingCouponPersistenceImpl.class);
1372 private static ShoppingCoupon _nullShoppingCoupon = new ShoppingCouponImpl() {
1373 @Override
1374 public Object clone() {
1375 return this;
1376 }
1377
1378 @Override
1379 public CacheModel<ShoppingCoupon> toCacheModel() {
1380 return _nullShoppingCouponCacheModel;
1381 }
1382 };
1383
1384 private static CacheModel<ShoppingCoupon> _nullShoppingCouponCacheModel = new CacheModel<ShoppingCoupon>() {
1385 public ShoppingCoupon toEntityModel() {
1386 return _nullShoppingCoupon;
1387 }
1388 };
1389 }