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.kernel.util.Validator;
037 import com.liferay.portal.model.CacheModel;
038 import com.liferay.portal.model.ModelListener;
039 import com.liferay.portal.service.persistence.BatchSessionUtil;
040 import com.liferay.portal.service.persistence.ResourcePersistence;
041 import com.liferay.portal.service.persistence.UserPersistence;
042 import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
043
044 import com.liferay.portlet.shopping.NoSuchCouponException;
045 import com.liferay.portlet.shopping.model.ShoppingCoupon;
046 import com.liferay.portlet.shopping.model.impl.ShoppingCouponImpl;
047 import com.liferay.portlet.shopping.model.impl.ShoppingCouponModelImpl;
048
049 import java.io.Serializable;
050
051 import java.util.ArrayList;
052 import java.util.Collections;
053 import java.util.List;
054
055
067 public class ShoppingCouponPersistenceImpl extends BasePersistenceImpl<ShoppingCoupon>
068 implements ShoppingCouponPersistence {
069
074 public static final String FINDER_CLASS_NAME_ENTITY = ShoppingCouponImpl.class.getName();
075 public static final String FINDER_CLASS_NAME_LIST_WITH_PAGINATION = FINDER_CLASS_NAME_ENTITY +
076 ".List1";
077 public static final String FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION = FINDER_CLASS_NAME_ENTITY +
078 ".List2";
079 public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_GROUPID = new FinderPath(ShoppingCouponModelImpl.ENTITY_CACHE_ENABLED,
080 ShoppingCouponModelImpl.FINDER_CACHE_ENABLED,
081 ShoppingCouponImpl.class, FINDER_CLASS_NAME_LIST_WITH_PAGINATION,
082 "findByGroupId",
083 new String[] {
084 Long.class.getName(),
085
086 "java.lang.Integer", "java.lang.Integer",
087 "com.liferay.portal.kernel.util.OrderByComparator"
088 });
089 public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_GROUPID =
090 new FinderPath(ShoppingCouponModelImpl.ENTITY_CACHE_ENABLED,
091 ShoppingCouponModelImpl.FINDER_CACHE_ENABLED,
092 ShoppingCouponImpl.class,
093 FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findByGroupId",
094 new String[] { Long.class.getName() },
095 ShoppingCouponModelImpl.GROUPID_COLUMN_BITMASK);
096 public static final FinderPath FINDER_PATH_COUNT_BY_GROUPID = new FinderPath(ShoppingCouponModelImpl.ENTITY_CACHE_ENABLED,
097 ShoppingCouponModelImpl.FINDER_CACHE_ENABLED, Long.class,
098 FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByGroupId",
099 new String[] { Long.class.getName() });
100 public static final FinderPath FINDER_PATH_FETCH_BY_CODE = new FinderPath(ShoppingCouponModelImpl.ENTITY_CACHE_ENABLED,
101 ShoppingCouponModelImpl.FINDER_CACHE_ENABLED,
102 ShoppingCouponImpl.class, FINDER_CLASS_NAME_ENTITY, "fetchByCode",
103 new String[] { String.class.getName() },
104 ShoppingCouponModelImpl.CODE_COLUMN_BITMASK);
105 public static final FinderPath FINDER_PATH_COUNT_BY_CODE = new FinderPath(ShoppingCouponModelImpl.ENTITY_CACHE_ENABLED,
106 ShoppingCouponModelImpl.FINDER_CACHE_ENABLED, Long.class,
107 FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByCode",
108 new String[] { String.class.getName() });
109 public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_ALL = new FinderPath(ShoppingCouponModelImpl.ENTITY_CACHE_ENABLED,
110 ShoppingCouponModelImpl.FINDER_CACHE_ENABLED,
111 ShoppingCouponImpl.class, FINDER_CLASS_NAME_LIST_WITH_PAGINATION,
112 "findAll", new String[0]);
113 public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL = new FinderPath(ShoppingCouponModelImpl.ENTITY_CACHE_ENABLED,
114 ShoppingCouponModelImpl.FINDER_CACHE_ENABLED,
115 ShoppingCouponImpl.class,
116 FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findAll", new String[0]);
117 public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(ShoppingCouponModelImpl.ENTITY_CACHE_ENABLED,
118 ShoppingCouponModelImpl.FINDER_CACHE_ENABLED, Long.class,
119 FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countAll", new String[0]);
120
121
126 public void cacheResult(ShoppingCoupon shoppingCoupon) {
127 EntityCacheUtil.putResult(ShoppingCouponModelImpl.ENTITY_CACHE_ENABLED,
128 ShoppingCouponImpl.class, shoppingCoupon.getPrimaryKey(),
129 shoppingCoupon);
130
131 FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_CODE,
132 new Object[] { shoppingCoupon.getCode() }, shoppingCoupon);
133
134 shoppingCoupon.resetOriginalValues();
135 }
136
137
142 public void cacheResult(List<ShoppingCoupon> shoppingCoupons) {
143 for (ShoppingCoupon shoppingCoupon : shoppingCoupons) {
144 if (EntityCacheUtil.getResult(
145 ShoppingCouponModelImpl.ENTITY_CACHE_ENABLED,
146 ShoppingCouponImpl.class, shoppingCoupon.getPrimaryKey()) == null) {
147 cacheResult(shoppingCoupon);
148 }
149 else {
150 shoppingCoupon.resetOriginalValues();
151 }
152 }
153 }
154
155
162 @Override
163 public void clearCache() {
164 if (_HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE) {
165 CacheRegistryUtil.clear(ShoppingCouponImpl.class.getName());
166 }
167
168 EntityCacheUtil.clearCache(ShoppingCouponImpl.class.getName());
169
170 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
171 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
172 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
173 }
174
175
182 @Override
183 public void clearCache(ShoppingCoupon shoppingCoupon) {
184 EntityCacheUtil.removeResult(ShoppingCouponModelImpl.ENTITY_CACHE_ENABLED,
185 ShoppingCouponImpl.class, shoppingCoupon.getPrimaryKey());
186
187 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
188 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
189
190 clearUniqueFindersCache(shoppingCoupon);
191 }
192
193 @Override
194 public void clearCache(List<ShoppingCoupon> shoppingCoupons) {
195 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
196 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
197
198 for (ShoppingCoupon shoppingCoupon : shoppingCoupons) {
199 EntityCacheUtil.removeResult(ShoppingCouponModelImpl.ENTITY_CACHE_ENABLED,
200 ShoppingCouponImpl.class, shoppingCoupon.getPrimaryKey());
201
202 clearUniqueFindersCache(shoppingCoupon);
203 }
204 }
205
206 protected void clearUniqueFindersCache(ShoppingCoupon shoppingCoupon) {
207 FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_CODE,
208 new Object[] { shoppingCoupon.getCode() });
209 }
210
211
217 public ShoppingCoupon create(long couponId) {
218 ShoppingCoupon shoppingCoupon = new ShoppingCouponImpl();
219
220 shoppingCoupon.setNew(true);
221 shoppingCoupon.setPrimaryKey(couponId);
222
223 return shoppingCoupon;
224 }
225
226
234 public ShoppingCoupon remove(long couponId)
235 throws NoSuchCouponException, SystemException {
236 return remove(Long.valueOf(couponId));
237 }
238
239
247 @Override
248 public ShoppingCoupon remove(Serializable primaryKey)
249 throws NoSuchCouponException, SystemException {
250 Session session = null;
251
252 try {
253 session = openSession();
254
255 ShoppingCoupon shoppingCoupon = (ShoppingCoupon)session.get(ShoppingCouponImpl.class,
256 primaryKey);
257
258 if (shoppingCoupon == null) {
259 if (_log.isWarnEnabled()) {
260 _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
261 }
262
263 throw new NoSuchCouponException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
264 primaryKey);
265 }
266
267 return remove(shoppingCoupon);
268 }
269 catch (NoSuchCouponException nsee) {
270 throw nsee;
271 }
272 catch (Exception e) {
273 throw processException(e);
274 }
275 finally {
276 closeSession(session);
277 }
278 }
279
280 @Override
281 protected ShoppingCoupon removeImpl(ShoppingCoupon shoppingCoupon)
282 throws SystemException {
283 shoppingCoupon = toUnwrappedModel(shoppingCoupon);
284
285 Session session = null;
286
287 try {
288 session = openSession();
289
290 BatchSessionUtil.delete(session, shoppingCoupon);
291 }
292 catch (Exception e) {
293 throw processException(e);
294 }
295 finally {
296 closeSession(session);
297 }
298
299 clearCache(shoppingCoupon);
300
301 return shoppingCoupon;
302 }
303
304 @Override
305 public ShoppingCoupon updateImpl(
306 com.liferay.portlet.shopping.model.ShoppingCoupon shoppingCoupon,
307 boolean merge) throws SystemException {
308 shoppingCoupon = toUnwrappedModel(shoppingCoupon);
309
310 boolean isNew = shoppingCoupon.isNew();
311
312 ShoppingCouponModelImpl shoppingCouponModelImpl = (ShoppingCouponModelImpl)shoppingCoupon;
313
314 Session session = null;
315
316 try {
317 session = openSession();
318
319 BatchSessionUtil.update(session, shoppingCoupon, merge);
320
321 shoppingCoupon.setNew(false);
322 }
323 catch (Exception e) {
324 throw processException(e);
325 }
326 finally {
327 closeSession(session);
328 }
329
330 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
331
332 if (isNew || !ShoppingCouponModelImpl.COLUMN_BITMASK_ENABLED) {
333 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
334 }
335
336 else {
337 if ((shoppingCouponModelImpl.getColumnBitmask() &
338 FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_GROUPID.getColumnBitmask()) != 0) {
339 Object[] args = new Object[] {
340 Long.valueOf(shoppingCouponModelImpl.getOriginalGroupId())
341 };
342
343 FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_GROUPID, args);
344 FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_GROUPID,
345 args);
346
347 args = new Object[] {
348 Long.valueOf(shoppingCouponModelImpl.getGroupId())
349 };
350
351 FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_GROUPID, args);
352 FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_GROUPID,
353 args);
354 }
355 }
356
357 EntityCacheUtil.putResult(ShoppingCouponModelImpl.ENTITY_CACHE_ENABLED,
358 ShoppingCouponImpl.class, shoppingCoupon.getPrimaryKey(),
359 shoppingCoupon);
360
361 if (isNew) {
362 FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_CODE,
363 new Object[] { shoppingCoupon.getCode() }, shoppingCoupon);
364 }
365 else {
366 if ((shoppingCouponModelImpl.getColumnBitmask() &
367 FINDER_PATH_FETCH_BY_CODE.getColumnBitmask()) != 0) {
368 Object[] args = new Object[] {
369 shoppingCouponModelImpl.getOriginalCode()
370 };
371
372 FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_CODE, args);
373
374 FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_CODE, args);
375
376 FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_CODE,
377 new Object[] { shoppingCoupon.getCode() }, shoppingCoupon);
378 }
379 }
380
381 return shoppingCoupon;
382 }
383
384 protected ShoppingCoupon toUnwrappedModel(ShoppingCoupon shoppingCoupon) {
385 if (shoppingCoupon instanceof ShoppingCouponImpl) {
386 return shoppingCoupon;
387 }
388
389 ShoppingCouponImpl shoppingCouponImpl = new ShoppingCouponImpl();
390
391 shoppingCouponImpl.setNew(shoppingCoupon.isNew());
392 shoppingCouponImpl.setPrimaryKey(shoppingCoupon.getPrimaryKey());
393
394 shoppingCouponImpl.setCouponId(shoppingCoupon.getCouponId());
395 shoppingCouponImpl.setGroupId(shoppingCoupon.getGroupId());
396 shoppingCouponImpl.setCompanyId(shoppingCoupon.getCompanyId());
397 shoppingCouponImpl.setUserId(shoppingCoupon.getUserId());
398 shoppingCouponImpl.setUserName(shoppingCoupon.getUserName());
399 shoppingCouponImpl.setCreateDate(shoppingCoupon.getCreateDate());
400 shoppingCouponImpl.setModifiedDate(shoppingCoupon.getModifiedDate());
401 shoppingCouponImpl.setCode(shoppingCoupon.getCode());
402 shoppingCouponImpl.setName(shoppingCoupon.getName());
403 shoppingCouponImpl.setDescription(shoppingCoupon.getDescription());
404 shoppingCouponImpl.setStartDate(shoppingCoupon.getStartDate());
405 shoppingCouponImpl.setEndDate(shoppingCoupon.getEndDate());
406 shoppingCouponImpl.setActive(shoppingCoupon.isActive());
407 shoppingCouponImpl.setLimitCategories(shoppingCoupon.getLimitCategories());
408 shoppingCouponImpl.setLimitSkus(shoppingCoupon.getLimitSkus());
409 shoppingCouponImpl.setMinOrder(shoppingCoupon.getMinOrder());
410 shoppingCouponImpl.setDiscount(shoppingCoupon.getDiscount());
411 shoppingCouponImpl.setDiscountType(shoppingCoupon.getDiscountType());
412
413 return shoppingCouponImpl;
414 }
415
416
424 @Override
425 public ShoppingCoupon findByPrimaryKey(Serializable primaryKey)
426 throws NoSuchModelException, SystemException {
427 return findByPrimaryKey(((Long)primaryKey).longValue());
428 }
429
430
438 public ShoppingCoupon findByPrimaryKey(long couponId)
439 throws NoSuchCouponException, SystemException {
440 ShoppingCoupon shoppingCoupon = fetchByPrimaryKey(couponId);
441
442 if (shoppingCoupon == null) {
443 if (_log.isWarnEnabled()) {
444 _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + couponId);
445 }
446
447 throw new NoSuchCouponException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
448 couponId);
449 }
450
451 return shoppingCoupon;
452 }
453
454
461 @Override
462 public ShoppingCoupon fetchByPrimaryKey(Serializable primaryKey)
463 throws SystemException {
464 return fetchByPrimaryKey(((Long)primaryKey).longValue());
465 }
466
467
474 public ShoppingCoupon fetchByPrimaryKey(long couponId)
475 throws SystemException {
476 ShoppingCoupon shoppingCoupon = (ShoppingCoupon)EntityCacheUtil.getResult(ShoppingCouponModelImpl.ENTITY_CACHE_ENABLED,
477 ShoppingCouponImpl.class, couponId);
478
479 if (shoppingCoupon == _nullShoppingCoupon) {
480 return null;
481 }
482
483 if (shoppingCoupon == null) {
484 Session session = null;
485
486 boolean hasException = false;
487
488 try {
489 session = openSession();
490
491 shoppingCoupon = (ShoppingCoupon)session.get(ShoppingCouponImpl.class,
492 Long.valueOf(couponId));
493 }
494 catch (Exception e) {
495 hasException = true;
496
497 throw processException(e);
498 }
499 finally {
500 if (shoppingCoupon != null) {
501 cacheResult(shoppingCoupon);
502 }
503 else if (!hasException) {
504 EntityCacheUtil.putResult(ShoppingCouponModelImpl.ENTITY_CACHE_ENABLED,
505 ShoppingCouponImpl.class, couponId, _nullShoppingCoupon);
506 }
507
508 closeSession(session);
509 }
510 }
511
512 return shoppingCoupon;
513 }
514
515
522 public List<ShoppingCoupon> findByGroupId(long groupId)
523 throws SystemException {
524 return findByGroupId(groupId, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
525 }
526
527
540 public List<ShoppingCoupon> findByGroupId(long groupId, int start, int end)
541 throws SystemException {
542 return findByGroupId(groupId, start, end, null);
543 }
544
545
559 public List<ShoppingCoupon> findByGroupId(long groupId, int start, int end,
560 OrderByComparator orderByComparator) throws SystemException {
561 FinderPath finderPath = null;
562 Object[] finderArgs = null;
563
564 if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
565 (orderByComparator == null)) {
566 finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_GROUPID;
567 finderArgs = new Object[] { groupId };
568 }
569 else {
570 finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_GROUPID;
571 finderArgs = new Object[] { groupId, start, end, orderByComparator };
572 }
573
574 List<ShoppingCoupon> list = (List<ShoppingCoupon>)FinderCacheUtil.getResult(finderPath,
575 finderArgs, this);
576
577 if ((list != null) && !list.isEmpty()) {
578 for (ShoppingCoupon shoppingCoupon : list) {
579 if ((groupId != shoppingCoupon.getGroupId())) {
580 list = null;
581
582 break;
583 }
584 }
585 }
586
587 if (list == null) {
588 StringBundler query = null;
589
590 if (orderByComparator != null) {
591 query = new StringBundler(3 +
592 (orderByComparator.getOrderByFields().length * 3));
593 }
594 else {
595 query = new StringBundler(3);
596 }
597
598 query.append(_SQL_SELECT_SHOPPINGCOUPON_WHERE);
599
600 query.append(_FINDER_COLUMN_GROUPID_GROUPID_2);
601
602 if (orderByComparator != null) {
603 appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
604 orderByComparator);
605 }
606
607 else {
608 query.append(ShoppingCouponModelImpl.ORDER_BY_JPQL);
609 }
610
611 String sql = query.toString();
612
613 Session session = null;
614
615 try {
616 session = openSession();
617
618 Query q = session.createQuery(sql);
619
620 QueryPos qPos = QueryPos.getInstance(q);
621
622 qPos.add(groupId);
623
624 list = (List<ShoppingCoupon>)QueryUtil.list(q, getDialect(),
625 start, end);
626 }
627 catch (Exception e) {
628 throw processException(e);
629 }
630 finally {
631 if (list == null) {
632 FinderCacheUtil.removeResult(finderPath, finderArgs);
633 }
634 else {
635 cacheResult(list);
636
637 FinderCacheUtil.putResult(finderPath, finderArgs, list);
638 }
639
640 closeSession(session);
641 }
642 }
643
644 return list;
645 }
646
647
656 public ShoppingCoupon findByGroupId_First(long groupId,
657 OrderByComparator orderByComparator)
658 throws NoSuchCouponException, SystemException {
659 ShoppingCoupon shoppingCoupon = fetchByGroupId_First(groupId,
660 orderByComparator);
661
662 if (shoppingCoupon != null) {
663 return shoppingCoupon;
664 }
665
666 StringBundler msg = new StringBundler(4);
667
668 msg.append(_NO_SUCH_ENTITY_WITH_KEY);
669
670 msg.append("groupId=");
671 msg.append(groupId);
672
673 msg.append(StringPool.CLOSE_CURLY_BRACE);
674
675 throw new NoSuchCouponException(msg.toString());
676 }
677
678
686 public ShoppingCoupon fetchByGroupId_First(long groupId,
687 OrderByComparator orderByComparator) throws SystemException {
688 List<ShoppingCoupon> list = findByGroupId(groupId, 0, 1,
689 orderByComparator);
690
691 if (!list.isEmpty()) {
692 return list.get(0);
693 }
694
695 return null;
696 }
697
698
707 public ShoppingCoupon findByGroupId_Last(long groupId,
708 OrderByComparator orderByComparator)
709 throws NoSuchCouponException, SystemException {
710 ShoppingCoupon shoppingCoupon = fetchByGroupId_Last(groupId,
711 orderByComparator);
712
713 if (shoppingCoupon != null) {
714 return shoppingCoupon;
715 }
716
717 StringBundler msg = new StringBundler(4);
718
719 msg.append(_NO_SUCH_ENTITY_WITH_KEY);
720
721 msg.append("groupId=");
722 msg.append(groupId);
723
724 msg.append(StringPool.CLOSE_CURLY_BRACE);
725
726 throw new NoSuchCouponException(msg.toString());
727 }
728
729
737 public ShoppingCoupon fetchByGroupId_Last(long groupId,
738 OrderByComparator orderByComparator) throws SystemException {
739 int count = countByGroupId(groupId);
740
741 List<ShoppingCoupon> list = findByGroupId(groupId, count - 1, count,
742 orderByComparator);
743
744 if (!list.isEmpty()) {
745 return list.get(0);
746 }
747
748 return null;
749 }
750
751
761 public ShoppingCoupon[] findByGroupId_PrevAndNext(long couponId,
762 long groupId, OrderByComparator orderByComparator)
763 throws NoSuchCouponException, SystemException {
764 ShoppingCoupon shoppingCoupon = findByPrimaryKey(couponId);
765
766 Session session = null;
767
768 try {
769 session = openSession();
770
771 ShoppingCoupon[] array = new ShoppingCouponImpl[3];
772
773 array[0] = getByGroupId_PrevAndNext(session, shoppingCoupon,
774 groupId, orderByComparator, true);
775
776 array[1] = shoppingCoupon;
777
778 array[2] = getByGroupId_PrevAndNext(session, shoppingCoupon,
779 groupId, orderByComparator, false);
780
781 return array;
782 }
783 catch (Exception e) {
784 throw processException(e);
785 }
786 finally {
787 closeSession(session);
788 }
789 }
790
791 protected ShoppingCoupon getByGroupId_PrevAndNext(Session session,
792 ShoppingCoupon shoppingCoupon, long groupId,
793 OrderByComparator orderByComparator, boolean previous) {
794 StringBundler query = null;
795
796 if (orderByComparator != null) {
797 query = new StringBundler(6 +
798 (orderByComparator.getOrderByFields().length * 6));
799 }
800 else {
801 query = new StringBundler(3);
802 }
803
804 query.append(_SQL_SELECT_SHOPPINGCOUPON_WHERE);
805
806 query.append(_FINDER_COLUMN_GROUPID_GROUPID_2);
807
808 if (orderByComparator != null) {
809 String[] orderByConditionFields = orderByComparator.getOrderByConditionFields();
810
811 if (orderByConditionFields.length > 0) {
812 query.append(WHERE_AND);
813 }
814
815 for (int i = 0; i < orderByConditionFields.length; i++) {
816 query.append(_ORDER_BY_ENTITY_ALIAS);
817 query.append(orderByConditionFields[i]);
818
819 if ((i + 1) < orderByConditionFields.length) {
820 if (orderByComparator.isAscending() ^ previous) {
821 query.append(WHERE_GREATER_THAN_HAS_NEXT);
822 }
823 else {
824 query.append(WHERE_LESSER_THAN_HAS_NEXT);
825 }
826 }
827 else {
828 if (orderByComparator.isAscending() ^ previous) {
829 query.append(WHERE_GREATER_THAN);
830 }
831 else {
832 query.append(WHERE_LESSER_THAN);
833 }
834 }
835 }
836
837 query.append(ORDER_BY_CLAUSE);
838
839 String[] orderByFields = orderByComparator.getOrderByFields();
840
841 for (int i = 0; i < orderByFields.length; i++) {
842 query.append(_ORDER_BY_ENTITY_ALIAS);
843 query.append(orderByFields[i]);
844
845 if ((i + 1) < orderByFields.length) {
846 if (orderByComparator.isAscending() ^ previous) {
847 query.append(ORDER_BY_ASC_HAS_NEXT);
848 }
849 else {
850 query.append(ORDER_BY_DESC_HAS_NEXT);
851 }
852 }
853 else {
854 if (orderByComparator.isAscending() ^ previous) {
855 query.append(ORDER_BY_ASC);
856 }
857 else {
858 query.append(ORDER_BY_DESC);
859 }
860 }
861 }
862 }
863
864 else {
865 query.append(ShoppingCouponModelImpl.ORDER_BY_JPQL);
866 }
867
868 String sql = query.toString();
869
870 Query q = session.createQuery(sql);
871
872 q.setFirstResult(0);
873 q.setMaxResults(2);
874
875 QueryPos qPos = QueryPos.getInstance(q);
876
877 qPos.add(groupId);
878
879 if (orderByComparator != null) {
880 Object[] values = orderByComparator.getOrderByConditionValues(shoppingCoupon);
881
882 for (Object value : values) {
883 qPos.add(value);
884 }
885 }
886
887 List<ShoppingCoupon> list = q.list();
888
889 if (list.size() == 2) {
890 return list.get(1);
891 }
892 else {
893 return null;
894 }
895 }
896
897
905 public ShoppingCoupon findByCode(String code)
906 throws NoSuchCouponException, SystemException {
907 ShoppingCoupon shoppingCoupon = fetchByCode(code);
908
909 if (shoppingCoupon == null) {
910 StringBundler msg = new StringBundler(4);
911
912 msg.append(_NO_SUCH_ENTITY_WITH_KEY);
913
914 msg.append("code=");
915 msg.append(code);
916
917 msg.append(StringPool.CLOSE_CURLY_BRACE);
918
919 if (_log.isWarnEnabled()) {
920 _log.warn(msg.toString());
921 }
922
923 throw new NoSuchCouponException(msg.toString());
924 }
925
926 return shoppingCoupon;
927 }
928
929
936 public ShoppingCoupon fetchByCode(String code) throws SystemException {
937 return fetchByCode(code, true);
938 }
939
940
948 public ShoppingCoupon fetchByCode(String code, boolean retrieveFromCache)
949 throws SystemException {
950 Object[] finderArgs = new Object[] { code };
951
952 Object result = null;
953
954 if (retrieveFromCache) {
955 result = FinderCacheUtil.getResult(FINDER_PATH_FETCH_BY_CODE,
956 finderArgs, this);
957 }
958
959 if (result instanceof ShoppingCoupon) {
960 ShoppingCoupon shoppingCoupon = (ShoppingCoupon)result;
961
962 if (!Validator.equals(code, shoppingCoupon.getCode())) {
963 result = null;
964 }
965 }
966
967 if (result == null) {
968 StringBundler query = new StringBundler(3);
969
970 query.append(_SQL_SELECT_SHOPPINGCOUPON_WHERE);
971
972 if (code == null) {
973 query.append(_FINDER_COLUMN_CODE_CODE_1);
974 }
975 else {
976 if (code.equals(StringPool.BLANK)) {
977 query.append(_FINDER_COLUMN_CODE_CODE_3);
978 }
979 else {
980 query.append(_FINDER_COLUMN_CODE_CODE_2);
981 }
982 }
983
984 query.append(ShoppingCouponModelImpl.ORDER_BY_JPQL);
985
986 String sql = query.toString();
987
988 Session session = null;
989
990 try {
991 session = openSession();
992
993 Query q = session.createQuery(sql);
994
995 QueryPos qPos = QueryPos.getInstance(q);
996
997 if (code != null) {
998 qPos.add(code);
999 }
1000
1001 List<ShoppingCoupon> list = q.list();
1002
1003 result = list;
1004
1005 ShoppingCoupon shoppingCoupon = null;
1006
1007 if (list.isEmpty()) {
1008 FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_CODE,
1009 finderArgs, list);
1010 }
1011 else {
1012 shoppingCoupon = list.get(0);
1013
1014 cacheResult(shoppingCoupon);
1015
1016 if ((shoppingCoupon.getCode() == null) ||
1017 !shoppingCoupon.getCode().equals(code)) {
1018 FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_CODE,
1019 finderArgs, shoppingCoupon);
1020 }
1021 }
1022
1023 return shoppingCoupon;
1024 }
1025 catch (Exception e) {
1026 throw processException(e);
1027 }
1028 finally {
1029 if (result == null) {
1030 FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_CODE,
1031 finderArgs);
1032 }
1033
1034 closeSession(session);
1035 }
1036 }
1037 else {
1038 if (result instanceof List<?>) {
1039 return null;
1040 }
1041 else {
1042 return (ShoppingCoupon)result;
1043 }
1044 }
1045 }
1046
1047
1053 public List<ShoppingCoupon> findAll() throws SystemException {
1054 return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
1055 }
1056
1057
1069 public List<ShoppingCoupon> findAll(int start, int end)
1070 throws SystemException {
1071 return findAll(start, end, null);
1072 }
1073
1074
1087 public List<ShoppingCoupon> findAll(int start, int end,
1088 OrderByComparator orderByComparator) throws SystemException {
1089 FinderPath finderPath = null;
1090 Object[] finderArgs = new Object[] { start, end, orderByComparator };
1091
1092 if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
1093 (orderByComparator == null)) {
1094 finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL;
1095 finderArgs = FINDER_ARGS_EMPTY;
1096 }
1097 else {
1098 finderPath = FINDER_PATH_WITH_PAGINATION_FIND_ALL;
1099 finderArgs = new Object[] { start, end, orderByComparator };
1100 }
1101
1102 List<ShoppingCoupon> list = (List<ShoppingCoupon>)FinderCacheUtil.getResult(finderPath,
1103 finderArgs, this);
1104
1105 if (list == null) {
1106 StringBundler query = null;
1107 String sql = null;
1108
1109 if (orderByComparator != null) {
1110 query = new StringBundler(2 +
1111 (orderByComparator.getOrderByFields().length * 3));
1112
1113 query.append(_SQL_SELECT_SHOPPINGCOUPON);
1114
1115 appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
1116 orderByComparator);
1117
1118 sql = query.toString();
1119 }
1120 else {
1121 sql = _SQL_SELECT_SHOPPINGCOUPON.concat(ShoppingCouponModelImpl.ORDER_BY_JPQL);
1122 }
1123
1124 Session session = null;
1125
1126 try {
1127 session = openSession();
1128
1129 Query q = session.createQuery(sql);
1130
1131 if (orderByComparator == null) {
1132 list = (List<ShoppingCoupon>)QueryUtil.list(q,
1133 getDialect(), start, end, false);
1134
1135 Collections.sort(list);
1136 }
1137 else {
1138 list = (List<ShoppingCoupon>)QueryUtil.list(q,
1139 getDialect(), start, end);
1140 }
1141 }
1142 catch (Exception e) {
1143 throw processException(e);
1144 }
1145 finally {
1146 if (list == null) {
1147 FinderCacheUtil.removeResult(finderPath, finderArgs);
1148 }
1149 else {
1150 cacheResult(list);
1151
1152 FinderCacheUtil.putResult(finderPath, finderArgs, list);
1153 }
1154
1155 closeSession(session);
1156 }
1157 }
1158
1159 return list;
1160 }
1161
1162
1168 public void removeByGroupId(long groupId) throws SystemException {
1169 for (ShoppingCoupon shoppingCoupon : findByGroupId(groupId)) {
1170 remove(shoppingCoupon);
1171 }
1172 }
1173
1174
1181 public ShoppingCoupon removeByCode(String code)
1182 throws NoSuchCouponException, SystemException {
1183 ShoppingCoupon shoppingCoupon = findByCode(code);
1184
1185 return remove(shoppingCoupon);
1186 }
1187
1188
1193 public void removeAll() throws SystemException {
1194 for (ShoppingCoupon shoppingCoupon : findAll()) {
1195 remove(shoppingCoupon);
1196 }
1197 }
1198
1199
1206 public int countByGroupId(long groupId) throws SystemException {
1207 Object[] finderArgs = new Object[] { groupId };
1208
1209 Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_GROUPID,
1210 finderArgs, this);
1211
1212 if (count == null) {
1213 StringBundler query = new StringBundler(2);
1214
1215 query.append(_SQL_COUNT_SHOPPINGCOUPON_WHERE);
1216
1217 query.append(_FINDER_COLUMN_GROUPID_GROUPID_2);
1218
1219 String sql = query.toString();
1220
1221 Session session = null;
1222
1223 try {
1224 session = openSession();
1225
1226 Query q = session.createQuery(sql);
1227
1228 QueryPos qPos = QueryPos.getInstance(q);
1229
1230 qPos.add(groupId);
1231
1232 count = (Long)q.uniqueResult();
1233 }
1234 catch (Exception e) {
1235 throw processException(e);
1236 }
1237 finally {
1238 if (count == null) {
1239 count = Long.valueOf(0);
1240 }
1241
1242 FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_GROUPID,
1243 finderArgs, count);
1244
1245 closeSession(session);
1246 }
1247 }
1248
1249 return count.intValue();
1250 }
1251
1252
1259 public int countByCode(String code) throws SystemException {
1260 Object[] finderArgs = new Object[] { code };
1261
1262 Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_CODE,
1263 finderArgs, this);
1264
1265 if (count == null) {
1266 StringBundler query = new StringBundler(2);
1267
1268 query.append(_SQL_COUNT_SHOPPINGCOUPON_WHERE);
1269
1270 if (code == null) {
1271 query.append(_FINDER_COLUMN_CODE_CODE_1);
1272 }
1273 else {
1274 if (code.equals(StringPool.BLANK)) {
1275 query.append(_FINDER_COLUMN_CODE_CODE_3);
1276 }
1277 else {
1278 query.append(_FINDER_COLUMN_CODE_CODE_2);
1279 }
1280 }
1281
1282 String sql = query.toString();
1283
1284 Session session = null;
1285
1286 try {
1287 session = openSession();
1288
1289 Query q = session.createQuery(sql);
1290
1291 QueryPos qPos = QueryPos.getInstance(q);
1292
1293 if (code != null) {
1294 qPos.add(code);
1295 }
1296
1297 count = (Long)q.uniqueResult();
1298 }
1299 catch (Exception e) {
1300 throw processException(e);
1301 }
1302 finally {
1303 if (count == null) {
1304 count = Long.valueOf(0);
1305 }
1306
1307 FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_CODE,
1308 finderArgs, count);
1309
1310 closeSession(session);
1311 }
1312 }
1313
1314 return count.intValue();
1315 }
1316
1317
1323 public int countAll() throws SystemException {
1324 Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
1325 FINDER_ARGS_EMPTY, this);
1326
1327 if (count == null) {
1328 Session session = null;
1329
1330 try {
1331 session = openSession();
1332
1333 Query q = session.createQuery(_SQL_COUNT_SHOPPINGCOUPON);
1334
1335 count = (Long)q.uniqueResult();
1336 }
1337 catch (Exception e) {
1338 throw processException(e);
1339 }
1340 finally {
1341 if (count == null) {
1342 count = Long.valueOf(0);
1343 }
1344
1345 FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL,
1346 FINDER_ARGS_EMPTY, count);
1347
1348 closeSession(session);
1349 }
1350 }
1351
1352 return count.intValue();
1353 }
1354
1355
1358 public void afterPropertiesSet() {
1359 String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
1360 com.liferay.portal.util.PropsUtil.get(
1361 "value.object.listener.com.liferay.portlet.shopping.model.ShoppingCoupon")));
1362
1363 if (listenerClassNames.length > 0) {
1364 try {
1365 List<ModelListener<ShoppingCoupon>> listenersList = new ArrayList<ModelListener<ShoppingCoupon>>();
1366
1367 for (String listenerClassName : listenerClassNames) {
1368 listenersList.add((ModelListener<ShoppingCoupon>)InstanceFactory.newInstance(
1369 listenerClassName));
1370 }
1371
1372 listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
1373 }
1374 catch (Exception e) {
1375 _log.error(e);
1376 }
1377 }
1378 }
1379
1380 public void destroy() {
1381 EntityCacheUtil.removeCache(ShoppingCouponImpl.class.getName());
1382 FinderCacheUtil.removeCache(FINDER_CLASS_NAME_ENTITY);
1383 FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
1384 }
1385
1386 @BeanReference(type = ShoppingCartPersistence.class)
1387 protected ShoppingCartPersistence shoppingCartPersistence;
1388 @BeanReference(type = ShoppingCategoryPersistence.class)
1389 protected ShoppingCategoryPersistence shoppingCategoryPersistence;
1390 @BeanReference(type = ShoppingCouponPersistence.class)
1391 protected ShoppingCouponPersistence shoppingCouponPersistence;
1392 @BeanReference(type = ShoppingItemPersistence.class)
1393 protected ShoppingItemPersistence shoppingItemPersistence;
1394 @BeanReference(type = ShoppingItemFieldPersistence.class)
1395 protected ShoppingItemFieldPersistence shoppingItemFieldPersistence;
1396 @BeanReference(type = ShoppingItemPricePersistence.class)
1397 protected ShoppingItemPricePersistence shoppingItemPricePersistence;
1398 @BeanReference(type = ShoppingOrderPersistence.class)
1399 protected ShoppingOrderPersistence shoppingOrderPersistence;
1400 @BeanReference(type = ShoppingOrderItemPersistence.class)
1401 protected ShoppingOrderItemPersistence shoppingOrderItemPersistence;
1402 @BeanReference(type = ResourcePersistence.class)
1403 protected ResourcePersistence resourcePersistence;
1404 @BeanReference(type = UserPersistence.class)
1405 protected UserPersistence userPersistence;
1406 private static final String _SQL_SELECT_SHOPPINGCOUPON = "SELECT shoppingCoupon FROM ShoppingCoupon shoppingCoupon";
1407 private static final String _SQL_SELECT_SHOPPINGCOUPON_WHERE = "SELECT shoppingCoupon FROM ShoppingCoupon shoppingCoupon WHERE ";
1408 private static final String _SQL_COUNT_SHOPPINGCOUPON = "SELECT COUNT(shoppingCoupon) FROM ShoppingCoupon shoppingCoupon";
1409 private static final String _SQL_COUNT_SHOPPINGCOUPON_WHERE = "SELECT COUNT(shoppingCoupon) FROM ShoppingCoupon shoppingCoupon WHERE ";
1410 private static final String _FINDER_COLUMN_GROUPID_GROUPID_2 = "shoppingCoupon.groupId = ?";
1411 private static final String _FINDER_COLUMN_CODE_CODE_1 = "shoppingCoupon.code IS NULL";
1412 private static final String _FINDER_COLUMN_CODE_CODE_2 = "shoppingCoupon.code = ?";
1413 private static final String _FINDER_COLUMN_CODE_CODE_3 = "(shoppingCoupon.code IS NULL OR shoppingCoupon.code = ?)";
1414 private static final String _ORDER_BY_ENTITY_ALIAS = "shoppingCoupon.";
1415 private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No ShoppingCoupon exists with the primary key ";
1416 private static final String _NO_SUCH_ENTITY_WITH_KEY = "No ShoppingCoupon exists with the key {";
1417 private static final boolean _HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE = com.liferay.portal.util.PropsValues.HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE;
1418 private static Log _log = LogFactoryUtil.getLog(ShoppingCouponPersistenceImpl.class);
1419 private static ShoppingCoupon _nullShoppingCoupon = new ShoppingCouponImpl() {
1420 @Override
1421 public Object clone() {
1422 return this;
1423 }
1424
1425 @Override
1426 public CacheModel<ShoppingCoupon> toCacheModel() {
1427 return _nullShoppingCouponCacheModel;
1428 }
1429 };
1430
1431 private static CacheModel<ShoppingCoupon> _nullShoppingCouponCacheModel = new CacheModel<ShoppingCoupon>() {
1432 public ShoppingCoupon toEntityModel() {
1433 return _nullShoppingCoupon;
1434 }
1435 };
1436 }