001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
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    /**
055     * The persistence implementation for the shopping coupon service.
056     *
057     * <p>
058     * Caching information and settings can be found in <code>portal.properties</code>
059     * </p>
060     *
061     * @author Brian Wing Shun Chan
062     * @see ShoppingCouponPersistence
063     * @see ShoppingCouponUtil
064     * @generated
065     */
066    public class ShoppingCouponPersistenceImpl extends BasePersistenceImpl<ShoppingCoupon>
067            implements ShoppingCouponPersistence {
068            /*
069             * NOTE FOR DEVELOPERS:
070             *
071             * Never modify or reference this class directly. Always use {@link ShoppingCouponUtil} to access the shopping coupon persistence. Modify <code>service.xml</code> and rerun ServiceBuilder to regenerate this class.
072             */
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            /**
121             * Caches the shopping coupon in the entity cache if it is enabled.
122             *
123             * @param shoppingCoupon the shopping coupon
124             */
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            /**
137             * Caches the shopping coupons in the entity cache if it is enabled.
138             *
139             * @param shoppingCoupons the shopping coupons
140             */
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            /**
155             * Clears the cache for all shopping coupons.
156             *
157             * <p>
158             * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
159             * </p>
160             */
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            /**
175             * Clears the cache for the shopping coupon.
176             *
177             * <p>
178             * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
179             * </p>
180             */
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                    FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_CODE,
190                            new Object[] { shoppingCoupon.getCode() });
191            }
192    
193            /**
194             * Creates a new shopping coupon with the primary key. Does not add the shopping coupon to the database.
195             *
196             * @param couponId the primary key for the new shopping coupon
197             * @return the new shopping coupon
198             */
199            public ShoppingCoupon create(long couponId) {
200                    ShoppingCoupon shoppingCoupon = new ShoppingCouponImpl();
201    
202                    shoppingCoupon.setNew(true);
203                    shoppingCoupon.setPrimaryKey(couponId);
204    
205                    return shoppingCoupon;
206            }
207    
208            /**
209             * Removes the shopping coupon with the primary key from the database. Also notifies the appropriate model listeners.
210             *
211             * @param primaryKey the primary key of the shopping coupon
212             * @return the shopping coupon that was removed
213             * @throws com.liferay.portal.NoSuchModelException if a shopping coupon with the primary key could not be found
214             * @throws SystemException if a system exception occurred
215             */
216            @Override
217            public ShoppingCoupon remove(Serializable primaryKey)
218                    throws NoSuchModelException, SystemException {
219                    return remove(((Long)primaryKey).longValue());
220            }
221    
222            /**
223             * Removes the shopping coupon with the primary key from the database. Also notifies the appropriate model listeners.
224             *
225             * @param couponId the primary key of the shopping coupon
226             * @return the shopping coupon that was removed
227             * @throws com.liferay.portlet.shopping.NoSuchCouponException if a shopping coupon with the primary key could not be found
228             * @throws SystemException if a system exception occurred
229             */
230            public ShoppingCoupon remove(long couponId)
231                    throws NoSuchCouponException, SystemException {
232                    Session session = null;
233    
234                    try {
235                            session = openSession();
236    
237                            ShoppingCoupon shoppingCoupon = (ShoppingCoupon)session.get(ShoppingCouponImpl.class,
238                                            Long.valueOf(couponId));
239    
240                            if (shoppingCoupon == null) {
241                                    if (_log.isWarnEnabled()) {
242                                            _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + couponId);
243                                    }
244    
245                                    throw new NoSuchCouponException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
246                                            couponId);
247                            }
248    
249                            return shoppingCouponPersistence.remove(shoppingCoupon);
250                    }
251                    catch (NoSuchCouponException nsee) {
252                            throw nsee;
253                    }
254                    catch (Exception e) {
255                            throw processException(e);
256                    }
257                    finally {
258                            closeSession(session);
259                    }
260            }
261    
262            /**
263             * Removes the shopping coupon from the database. Also notifies the appropriate model listeners.
264             *
265             * @param shoppingCoupon the shopping coupon
266             * @return the shopping coupon that was removed
267             * @throws SystemException if a system exception occurred
268             */
269            @Override
270            public ShoppingCoupon remove(ShoppingCoupon shoppingCoupon)
271                    throws SystemException {
272                    return super.remove(shoppingCoupon);
273            }
274    
275            @Override
276            protected ShoppingCoupon removeImpl(ShoppingCoupon shoppingCoupon)
277                    throws SystemException {
278                    shoppingCoupon = toUnwrappedModel(shoppingCoupon);
279    
280                    Session session = null;
281    
282                    try {
283                            session = openSession();
284    
285                            BatchSessionUtil.delete(session, shoppingCoupon);
286                    }
287                    catch (Exception e) {
288                            throw processException(e);
289                    }
290                    finally {
291                            closeSession(session);
292                    }
293    
294                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
295                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
296    
297                    ShoppingCouponModelImpl shoppingCouponModelImpl = (ShoppingCouponModelImpl)shoppingCoupon;
298    
299                    FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_CODE,
300                            new Object[] { shoppingCouponModelImpl.getCode() });
301    
302                    EntityCacheUtil.removeResult(ShoppingCouponModelImpl.ENTITY_CACHE_ENABLED,
303                            ShoppingCouponImpl.class, shoppingCoupon.getPrimaryKey());
304    
305                    return shoppingCoupon;
306            }
307    
308            @Override
309            public ShoppingCoupon updateImpl(
310                    com.liferay.portlet.shopping.model.ShoppingCoupon shoppingCoupon,
311                    boolean merge) throws SystemException {
312                    shoppingCoupon = toUnwrappedModel(shoppingCoupon);
313    
314                    boolean isNew = shoppingCoupon.isNew();
315    
316                    ShoppingCouponModelImpl shoppingCouponModelImpl = (ShoppingCouponModelImpl)shoppingCoupon;
317    
318                    Session session = null;
319    
320                    try {
321                            session = openSession();
322    
323                            BatchSessionUtil.update(session, shoppingCoupon, merge);
324    
325                            shoppingCoupon.setNew(false);
326                    }
327                    catch (Exception e) {
328                            throw processException(e);
329                    }
330                    finally {
331                            closeSession(session);
332                    }
333    
334                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
335    
336                    if (isNew || !ShoppingCouponModelImpl.COLUMN_BITMASK_ENABLED) {
337                            FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
338                    }
339    
340                    else {
341                            if ((shoppingCouponModelImpl.getColumnBitmask() &
342                                            FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_GROUPID.getColumnBitmask()) != 0) {
343                                    Object[] args = new Object[] {
344                                                    Long.valueOf(shoppingCouponModelImpl.getOriginalGroupId())
345                                            };
346    
347                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_GROUPID, args);
348                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_GROUPID,
349                                            args);
350    
351                                    args = new Object[] {
352                                                    Long.valueOf(shoppingCouponModelImpl.getGroupId())
353                                            };
354    
355                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_GROUPID, args);
356                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_GROUPID,
357                                            args);
358                            }
359                    }
360    
361                    EntityCacheUtil.putResult(ShoppingCouponModelImpl.ENTITY_CACHE_ENABLED,
362                            ShoppingCouponImpl.class, shoppingCoupon.getPrimaryKey(),
363                            shoppingCoupon);
364    
365                    if (isNew) {
366                            FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_CODE,
367                                    new Object[] { shoppingCoupon.getCode() }, shoppingCoupon);
368                    }
369                    else {
370                            if ((shoppingCouponModelImpl.getColumnBitmask() &
371                                            FINDER_PATH_FETCH_BY_CODE.getColumnBitmask()) != 0) {
372                                    Object[] args = new Object[] {
373                                                    shoppingCouponModelImpl.getOriginalCode()
374                                            };
375    
376                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_CODE, args);
377                                    FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_CODE, args);
378    
379                                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_CODE,
380                                            new Object[] { shoppingCoupon.getCode() }, shoppingCoupon);
381                            }
382                    }
383    
384                    return shoppingCoupon;
385            }
386    
387            protected ShoppingCoupon toUnwrappedModel(ShoppingCoupon shoppingCoupon) {
388                    if (shoppingCoupon instanceof ShoppingCouponImpl) {
389                            return shoppingCoupon;
390                    }
391    
392                    ShoppingCouponImpl shoppingCouponImpl = new ShoppingCouponImpl();
393    
394                    shoppingCouponImpl.setNew(shoppingCoupon.isNew());
395                    shoppingCouponImpl.setPrimaryKey(shoppingCoupon.getPrimaryKey());
396    
397                    shoppingCouponImpl.setCouponId(shoppingCoupon.getCouponId());
398                    shoppingCouponImpl.setGroupId(shoppingCoupon.getGroupId());
399                    shoppingCouponImpl.setCompanyId(shoppingCoupon.getCompanyId());
400                    shoppingCouponImpl.setUserId(shoppingCoupon.getUserId());
401                    shoppingCouponImpl.setUserName(shoppingCoupon.getUserName());
402                    shoppingCouponImpl.setCreateDate(shoppingCoupon.getCreateDate());
403                    shoppingCouponImpl.setModifiedDate(shoppingCoupon.getModifiedDate());
404                    shoppingCouponImpl.setCode(shoppingCoupon.getCode());
405                    shoppingCouponImpl.setName(shoppingCoupon.getName());
406                    shoppingCouponImpl.setDescription(shoppingCoupon.getDescription());
407                    shoppingCouponImpl.setStartDate(shoppingCoupon.getStartDate());
408                    shoppingCouponImpl.setEndDate(shoppingCoupon.getEndDate());
409                    shoppingCouponImpl.setActive(shoppingCoupon.isActive());
410                    shoppingCouponImpl.setLimitCategories(shoppingCoupon.getLimitCategories());
411                    shoppingCouponImpl.setLimitSkus(shoppingCoupon.getLimitSkus());
412                    shoppingCouponImpl.setMinOrder(shoppingCoupon.getMinOrder());
413                    shoppingCouponImpl.setDiscount(shoppingCoupon.getDiscount());
414                    shoppingCouponImpl.setDiscountType(shoppingCoupon.getDiscountType());
415    
416                    return shoppingCouponImpl;
417            }
418    
419            /**
420             * Returns the shopping coupon with the primary key or throws a {@link com.liferay.portal.NoSuchModelException} if it could not be found.
421             *
422             * @param primaryKey the primary key of the shopping coupon
423             * @return the shopping coupon
424             * @throws com.liferay.portal.NoSuchModelException if a shopping coupon with the primary key could not be found
425             * @throws SystemException if a system exception occurred
426             */
427            @Override
428            public ShoppingCoupon findByPrimaryKey(Serializable primaryKey)
429                    throws NoSuchModelException, SystemException {
430                    return findByPrimaryKey(((Long)primaryKey).longValue());
431            }
432    
433            /**
434             * Returns the shopping coupon with the primary key or throws a {@link com.liferay.portlet.shopping.NoSuchCouponException} if it could not be found.
435             *
436             * @param couponId the primary key of the shopping coupon
437             * @return the shopping coupon
438             * @throws com.liferay.portlet.shopping.NoSuchCouponException if a shopping coupon with the primary key could not be found
439             * @throws SystemException if a system exception occurred
440             */
441            public ShoppingCoupon findByPrimaryKey(long couponId)
442                    throws NoSuchCouponException, SystemException {
443                    ShoppingCoupon shoppingCoupon = fetchByPrimaryKey(couponId);
444    
445                    if (shoppingCoupon == null) {
446                            if (_log.isWarnEnabled()) {
447                                    _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + couponId);
448                            }
449    
450                            throw new NoSuchCouponException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
451                                    couponId);
452                    }
453    
454                    return shoppingCoupon;
455            }
456    
457            /**
458             * Returns the shopping coupon with the primary key or returns <code>null</code> if it could not be found.
459             *
460             * @param primaryKey the primary key of the shopping coupon
461             * @return the shopping coupon, or <code>null</code> if a shopping coupon with the primary key could not be found
462             * @throws SystemException if a system exception occurred
463             */
464            @Override
465            public ShoppingCoupon fetchByPrimaryKey(Serializable primaryKey)
466                    throws SystemException {
467                    return fetchByPrimaryKey(((Long)primaryKey).longValue());
468            }
469    
470            /**
471             * Returns the shopping coupon with the primary key or returns <code>null</code> if it could not be found.
472             *
473             * @param couponId the primary key of the shopping coupon
474             * @return the shopping coupon, or <code>null</code> if a shopping coupon with the primary key could not be found
475             * @throws SystemException if a system exception occurred
476             */
477            public ShoppingCoupon fetchByPrimaryKey(long couponId)
478                    throws SystemException {
479                    ShoppingCoupon shoppingCoupon = (ShoppingCoupon)EntityCacheUtil.getResult(ShoppingCouponModelImpl.ENTITY_CACHE_ENABLED,
480                                    ShoppingCouponImpl.class, couponId);
481    
482                    if (shoppingCoupon == _nullShoppingCoupon) {
483                            return null;
484                    }
485    
486                    if (shoppingCoupon == null) {
487                            Session session = null;
488    
489                            boolean hasException = false;
490    
491                            try {
492                                    session = openSession();
493    
494                                    shoppingCoupon = (ShoppingCoupon)session.get(ShoppingCouponImpl.class,
495                                                    Long.valueOf(couponId));
496                            }
497                            catch (Exception e) {
498                                    hasException = true;
499    
500                                    throw processException(e);
501                            }
502                            finally {
503                                    if (shoppingCoupon != null) {
504                                            cacheResult(shoppingCoupon);
505                                    }
506                                    else if (!hasException) {
507                                            EntityCacheUtil.putResult(ShoppingCouponModelImpl.ENTITY_CACHE_ENABLED,
508                                                    ShoppingCouponImpl.class, couponId, _nullShoppingCoupon);
509                                    }
510    
511                                    closeSession(session);
512                            }
513                    }
514    
515                    return shoppingCoupon;
516            }
517    
518            /**
519             * Returns all the shopping coupons where groupId = &#63;.
520             *
521             * @param groupId the group ID
522             * @return the matching shopping coupons
523             * @throws SystemException if a system exception occurred
524             */
525            public List<ShoppingCoupon> findByGroupId(long groupId)
526                    throws SystemException {
527                    return findByGroupId(groupId, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
528            }
529    
530            /**
531             * Returns a range of all the shopping coupons where groupId = &#63;.
532             *
533             * <p>
534             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
535             * </p>
536             *
537             * @param groupId the group ID
538             * @param start the lower bound of the range of shopping coupons
539             * @param end the upper bound of the range of shopping coupons (not inclusive)
540             * @return the range of matching shopping coupons
541             * @throws SystemException if a system exception occurred
542             */
543            public List<ShoppingCoupon> findByGroupId(long groupId, int start, int end)
544                    throws SystemException {
545                    return findByGroupId(groupId, start, end, null);
546            }
547    
548            /**
549             * Returns an ordered range of all the shopping coupons where groupId = &#63;.
550             *
551             * <p>
552             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
553             * </p>
554             *
555             * @param groupId the group ID
556             * @param start the lower bound of the range of shopping coupons
557             * @param end the upper bound of the range of shopping coupons (not inclusive)
558             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
559             * @return the ordered range of matching shopping coupons
560             * @throws SystemException if a system exception occurred
561             */
562            public List<ShoppingCoupon> findByGroupId(long groupId, int start, int end,
563                    OrderByComparator orderByComparator) throws SystemException {
564                    FinderPath finderPath = null;
565                    Object[] finderArgs = null;
566    
567                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
568                                    (orderByComparator == null)) {
569                            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_GROUPID;
570                            finderArgs = new Object[] { groupId };
571                    }
572                    else {
573                            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_GROUPID;
574                            finderArgs = new Object[] { groupId, start, end, orderByComparator };
575                    }
576    
577                    List<ShoppingCoupon> list = (List<ShoppingCoupon>)FinderCacheUtil.getResult(finderPath,
578                                    finderArgs, this);
579    
580                    if (list == null) {
581                            StringBundler query = null;
582    
583                            if (orderByComparator != null) {
584                                    query = new StringBundler(3 +
585                                                    (orderByComparator.getOrderByFields().length * 3));
586                            }
587                            else {
588                                    query = new StringBundler(3);
589                            }
590    
591                            query.append(_SQL_SELECT_SHOPPINGCOUPON_WHERE);
592    
593                            query.append(_FINDER_COLUMN_GROUPID_GROUPID_2);
594    
595                            if (orderByComparator != null) {
596                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
597                                            orderByComparator);
598                            }
599    
600                            else {
601                                    query.append(ShoppingCouponModelImpl.ORDER_BY_JPQL);
602                            }
603    
604                            String sql = query.toString();
605    
606                            Session session = null;
607    
608                            try {
609                                    session = openSession();
610    
611                                    Query q = session.createQuery(sql);
612    
613                                    QueryPos qPos = QueryPos.getInstance(q);
614    
615                                    qPos.add(groupId);
616    
617                                    list = (List<ShoppingCoupon>)QueryUtil.list(q, getDialect(),
618                                                    start, end);
619                            }
620                            catch (Exception e) {
621                                    throw processException(e);
622                            }
623                            finally {
624                                    if (list == null) {
625                                            FinderCacheUtil.removeResult(finderPath, finderArgs);
626                                    }
627                                    else {
628                                            cacheResult(list);
629    
630                                            FinderCacheUtil.putResult(finderPath, finderArgs, list);
631                                    }
632    
633                                    closeSession(session);
634                            }
635                    }
636    
637                    return list;
638            }
639    
640            /**
641             * Returns the first shopping coupon in the ordered set where groupId = &#63;.
642             *
643             * <p>
644             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
645             * </p>
646             *
647             * @param groupId the group ID
648             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
649             * @return the first matching shopping coupon
650             * @throws com.liferay.portlet.shopping.NoSuchCouponException if a matching shopping coupon could not be found
651             * @throws SystemException if a system exception occurred
652             */
653            public ShoppingCoupon findByGroupId_First(long groupId,
654                    OrderByComparator orderByComparator)
655                    throws NoSuchCouponException, SystemException {
656                    List<ShoppingCoupon> list = findByGroupId(groupId, 0, 1,
657                                    orderByComparator);
658    
659                    if (list.isEmpty()) {
660                            StringBundler msg = new StringBundler(4);
661    
662                            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
663    
664                            msg.append("groupId=");
665                            msg.append(groupId);
666    
667                            msg.append(StringPool.CLOSE_CURLY_BRACE);
668    
669                            throw new NoSuchCouponException(msg.toString());
670                    }
671                    else {
672                            return list.get(0);
673                    }
674            }
675    
676            /**
677             * Returns the last shopping coupon in the ordered set where groupId = &#63;.
678             *
679             * <p>
680             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
681             * </p>
682             *
683             * @param groupId the group ID
684             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
685             * @return the last matching shopping coupon
686             * @throws com.liferay.portlet.shopping.NoSuchCouponException if a matching shopping coupon could not be found
687             * @throws SystemException if a system exception occurred
688             */
689            public ShoppingCoupon findByGroupId_Last(long groupId,
690                    OrderByComparator orderByComparator)
691                    throws NoSuchCouponException, SystemException {
692                    int count = countByGroupId(groupId);
693    
694                    List<ShoppingCoupon> list = findByGroupId(groupId, count - 1, count,
695                                    orderByComparator);
696    
697                    if (list.isEmpty()) {
698                            StringBundler msg = new StringBundler(4);
699    
700                            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
701    
702                            msg.append("groupId=");
703                            msg.append(groupId);
704    
705                            msg.append(StringPool.CLOSE_CURLY_BRACE);
706    
707                            throw new NoSuchCouponException(msg.toString());
708                    }
709                    else {
710                            return list.get(0);
711                    }
712            }
713    
714            /**
715             * Returns the shopping coupons before and after the current shopping coupon in the ordered set where groupId = &#63;.
716             *
717             * <p>
718             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
719             * </p>
720             *
721             * @param couponId the primary key of the current shopping coupon
722             * @param groupId the group ID
723             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
724             * @return the previous, current, and next shopping coupon
725             * @throws com.liferay.portlet.shopping.NoSuchCouponException if a shopping coupon with the primary key could not be found
726             * @throws SystemException if a system exception occurred
727             */
728            public ShoppingCoupon[] findByGroupId_PrevAndNext(long couponId,
729                    long groupId, OrderByComparator orderByComparator)
730                    throws NoSuchCouponException, SystemException {
731                    ShoppingCoupon shoppingCoupon = findByPrimaryKey(couponId);
732    
733                    Session session = null;
734    
735                    try {
736                            session = openSession();
737    
738                            ShoppingCoupon[] array = new ShoppingCouponImpl[3];
739    
740                            array[0] = getByGroupId_PrevAndNext(session, shoppingCoupon,
741                                            groupId, orderByComparator, true);
742    
743                            array[1] = shoppingCoupon;
744    
745                            array[2] = getByGroupId_PrevAndNext(session, shoppingCoupon,
746                                            groupId, orderByComparator, false);
747    
748                            return array;
749                    }
750                    catch (Exception e) {
751                            throw processException(e);
752                    }
753                    finally {
754                            closeSession(session);
755                    }
756            }
757    
758            protected ShoppingCoupon getByGroupId_PrevAndNext(Session session,
759                    ShoppingCoupon shoppingCoupon, long groupId,
760                    OrderByComparator orderByComparator, boolean previous) {
761                    StringBundler query = null;
762    
763                    if (orderByComparator != null) {
764                            query = new StringBundler(6 +
765                                            (orderByComparator.getOrderByFields().length * 6));
766                    }
767                    else {
768                            query = new StringBundler(3);
769                    }
770    
771                    query.append(_SQL_SELECT_SHOPPINGCOUPON_WHERE);
772    
773                    query.append(_FINDER_COLUMN_GROUPID_GROUPID_2);
774    
775                    if (orderByComparator != null) {
776                            String[] orderByConditionFields = orderByComparator.getOrderByConditionFields();
777    
778                            if (orderByConditionFields.length > 0) {
779                                    query.append(WHERE_AND);
780                            }
781    
782                            for (int i = 0; i < orderByConditionFields.length; i++) {
783                                    query.append(_ORDER_BY_ENTITY_ALIAS);
784                                    query.append(orderByConditionFields[i]);
785    
786                                    if ((i + 1) < orderByConditionFields.length) {
787                                            if (orderByComparator.isAscending() ^ previous) {
788                                                    query.append(WHERE_GREATER_THAN_HAS_NEXT);
789                                            }
790                                            else {
791                                                    query.append(WHERE_LESSER_THAN_HAS_NEXT);
792                                            }
793                                    }
794                                    else {
795                                            if (orderByComparator.isAscending() ^ previous) {
796                                                    query.append(WHERE_GREATER_THAN);
797                                            }
798                                            else {
799                                                    query.append(WHERE_LESSER_THAN);
800                                            }
801                                    }
802                            }
803    
804                            query.append(ORDER_BY_CLAUSE);
805    
806                            String[] orderByFields = orderByComparator.getOrderByFields();
807    
808                            for (int i = 0; i < orderByFields.length; i++) {
809                                    query.append(_ORDER_BY_ENTITY_ALIAS);
810                                    query.append(orderByFields[i]);
811    
812                                    if ((i + 1) < orderByFields.length) {
813                                            if (orderByComparator.isAscending() ^ previous) {
814                                                    query.append(ORDER_BY_ASC_HAS_NEXT);
815                                            }
816                                            else {
817                                                    query.append(ORDER_BY_DESC_HAS_NEXT);
818                                            }
819                                    }
820                                    else {
821                                            if (orderByComparator.isAscending() ^ previous) {
822                                                    query.append(ORDER_BY_ASC);
823                                            }
824                                            else {
825                                                    query.append(ORDER_BY_DESC);
826                                            }
827                                    }
828                            }
829                    }
830    
831                    else {
832                            query.append(ShoppingCouponModelImpl.ORDER_BY_JPQL);
833                    }
834    
835                    String sql = query.toString();
836    
837                    Query q = session.createQuery(sql);
838    
839                    q.setFirstResult(0);
840                    q.setMaxResults(2);
841    
842                    QueryPos qPos = QueryPos.getInstance(q);
843    
844                    qPos.add(groupId);
845    
846                    if (orderByComparator != null) {
847                            Object[] values = orderByComparator.getOrderByConditionValues(shoppingCoupon);
848    
849                            for (Object value : values) {
850                                    qPos.add(value);
851                            }
852                    }
853    
854                    List<ShoppingCoupon> list = q.list();
855    
856                    if (list.size() == 2) {
857                            return list.get(1);
858                    }
859                    else {
860                            return null;
861                    }
862            }
863    
864            /**
865             * Returns the shopping coupon where code = &#63; or throws a {@link com.liferay.portlet.shopping.NoSuchCouponException} if it could not be found.
866             *
867             * @param code the code
868             * @return the matching shopping coupon
869             * @throws com.liferay.portlet.shopping.NoSuchCouponException if a matching shopping coupon could not be found
870             * @throws SystemException if a system exception occurred
871             */
872            public ShoppingCoupon findByCode(String code)
873                    throws NoSuchCouponException, SystemException {
874                    ShoppingCoupon shoppingCoupon = fetchByCode(code);
875    
876                    if (shoppingCoupon == null) {
877                            StringBundler msg = new StringBundler(4);
878    
879                            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
880    
881                            msg.append("code=");
882                            msg.append(code);
883    
884                            msg.append(StringPool.CLOSE_CURLY_BRACE);
885    
886                            if (_log.isWarnEnabled()) {
887                                    _log.warn(msg.toString());
888                            }
889    
890                            throw new NoSuchCouponException(msg.toString());
891                    }
892    
893                    return shoppingCoupon;
894            }
895    
896            /**
897             * Returns the shopping coupon where code = &#63; or returns <code>null</code> if it could not be found. Uses the finder cache.
898             *
899             * @param code the code
900             * @return the matching shopping coupon, or <code>null</code> if a matching shopping coupon could not be found
901             * @throws SystemException if a system exception occurred
902             */
903            public ShoppingCoupon fetchByCode(String code) throws SystemException {
904                    return fetchByCode(code, true);
905            }
906    
907            /**
908             * Returns the shopping coupon where code = &#63; or returns <code>null</code> if it could not be found, optionally using the finder cache.
909             *
910             * @param code the code
911             * @param retrieveFromCache whether to use the finder cache
912             * @return the matching shopping coupon, or <code>null</code> if a matching shopping coupon could not be found
913             * @throws SystemException if a system exception occurred
914             */
915            public ShoppingCoupon fetchByCode(String code, boolean retrieveFromCache)
916                    throws SystemException {
917                    Object[] finderArgs = new Object[] { code };
918    
919                    Object result = null;
920    
921                    if (retrieveFromCache) {
922                            result = FinderCacheUtil.getResult(FINDER_PATH_FETCH_BY_CODE,
923                                            finderArgs, this);
924                    }
925    
926                    if (result == null) {
927                            StringBundler query = new StringBundler(3);
928    
929                            query.append(_SQL_SELECT_SHOPPINGCOUPON_WHERE);
930    
931                            if (code == null) {
932                                    query.append(_FINDER_COLUMN_CODE_CODE_1);
933                            }
934                            else {
935                                    if (code.equals(StringPool.BLANK)) {
936                                            query.append(_FINDER_COLUMN_CODE_CODE_3);
937                                    }
938                                    else {
939                                            query.append(_FINDER_COLUMN_CODE_CODE_2);
940                                    }
941                            }
942    
943                            query.append(ShoppingCouponModelImpl.ORDER_BY_JPQL);
944    
945                            String sql = query.toString();
946    
947                            Session session = null;
948    
949                            try {
950                                    session = openSession();
951    
952                                    Query q = session.createQuery(sql);
953    
954                                    QueryPos qPos = QueryPos.getInstance(q);
955    
956                                    if (code != null) {
957                                            qPos.add(code);
958                                    }
959    
960                                    List<ShoppingCoupon> list = q.list();
961    
962                                    result = list;
963    
964                                    ShoppingCoupon shoppingCoupon = null;
965    
966                                    if (list.isEmpty()) {
967                                            FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_CODE,
968                                                    finderArgs, list);
969                                    }
970                                    else {
971                                            shoppingCoupon = list.get(0);
972    
973                                            cacheResult(shoppingCoupon);
974    
975                                            if ((shoppingCoupon.getCode() == null) ||
976                                                            !shoppingCoupon.getCode().equals(code)) {
977                                                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_CODE,
978                                                            finderArgs, shoppingCoupon);
979                                            }
980                                    }
981    
982                                    return shoppingCoupon;
983                            }
984                            catch (Exception e) {
985                                    throw processException(e);
986                            }
987                            finally {
988                                    if (result == null) {
989                                            FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_CODE,
990                                                    finderArgs);
991                                    }
992    
993                                    closeSession(session);
994                            }
995                    }
996                    else {
997                            if (result instanceof List<?>) {
998                                    return null;
999                            }
1000                            else {
1001                                    return (ShoppingCoupon)result;
1002                            }
1003                    }
1004            }
1005    
1006            /**
1007             * Returns all the shopping coupons.
1008             *
1009             * @return the shopping coupons
1010             * @throws SystemException if a system exception occurred
1011             */
1012            public List<ShoppingCoupon> findAll() throws SystemException {
1013                    return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
1014            }
1015    
1016            /**
1017             * Returns a range of all the shopping coupons.
1018             *
1019             * <p>
1020             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
1021             * </p>
1022             *
1023             * @param start the lower bound of the range of shopping coupons
1024             * @param end the upper bound of the range of shopping coupons (not inclusive)
1025             * @return the range of shopping coupons
1026             * @throws SystemException if a system exception occurred
1027             */
1028            public List<ShoppingCoupon> findAll(int start, int end)
1029                    throws SystemException {
1030                    return findAll(start, end, null);
1031            }
1032    
1033            /**
1034             * Returns an ordered range of all the shopping coupons.
1035             *
1036             * <p>
1037             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
1038             * </p>
1039             *
1040             * @param start the lower bound of the range of shopping coupons
1041             * @param end the upper bound of the range of shopping coupons (not inclusive)
1042             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
1043             * @return the ordered range of shopping coupons
1044             * @throws SystemException if a system exception occurred
1045             */
1046            public List<ShoppingCoupon> findAll(int start, int end,
1047                    OrderByComparator orderByComparator) throws SystemException {
1048                    FinderPath finderPath = null;
1049                    Object[] finderArgs = new Object[] { start, end, orderByComparator };
1050    
1051                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
1052                                    (orderByComparator == null)) {
1053                            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_ALL;
1054                            finderArgs = FINDER_ARGS_EMPTY;
1055                    }
1056                    else {
1057                            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL;
1058                            finderArgs = new Object[] { start, end, orderByComparator };
1059                    }
1060    
1061                    List<ShoppingCoupon> list = (List<ShoppingCoupon>)FinderCacheUtil.getResult(finderPath,
1062                                    finderArgs, this);
1063    
1064                    if (list == null) {
1065                            StringBundler query = null;
1066                            String sql = null;
1067    
1068                            if (orderByComparator != null) {
1069                                    query = new StringBundler(2 +
1070                                                    (orderByComparator.getOrderByFields().length * 3));
1071    
1072                                    query.append(_SQL_SELECT_SHOPPINGCOUPON);
1073    
1074                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
1075                                            orderByComparator);
1076    
1077                                    sql = query.toString();
1078                            }
1079                            else {
1080                                    sql = _SQL_SELECT_SHOPPINGCOUPON.concat(ShoppingCouponModelImpl.ORDER_BY_JPQL);
1081                            }
1082    
1083                            Session session = null;
1084    
1085                            try {
1086                                    session = openSession();
1087    
1088                                    Query q = session.createQuery(sql);
1089    
1090                                    if (orderByComparator == null) {
1091                                            list = (List<ShoppingCoupon>)QueryUtil.list(q,
1092                                                            getDialect(), start, end, false);
1093    
1094                                            Collections.sort(list);
1095                                    }
1096                                    else {
1097                                            list = (List<ShoppingCoupon>)QueryUtil.list(q,
1098                                                            getDialect(), start, end);
1099                                    }
1100                            }
1101                            catch (Exception e) {
1102                                    throw processException(e);
1103                            }
1104                            finally {
1105                                    if (list == null) {
1106                                            FinderCacheUtil.removeResult(finderPath, finderArgs);
1107                                    }
1108                                    else {
1109                                            cacheResult(list);
1110    
1111                                            FinderCacheUtil.putResult(finderPath, finderArgs, list);
1112                                    }
1113    
1114                                    closeSession(session);
1115                            }
1116                    }
1117    
1118                    return list;
1119            }
1120    
1121            /**
1122             * Removes all the shopping coupons where groupId = &#63; from the database.
1123             *
1124             * @param groupId the group ID
1125             * @throws SystemException if a system exception occurred
1126             */
1127            public void removeByGroupId(long groupId) throws SystemException {
1128                    for (ShoppingCoupon shoppingCoupon : findByGroupId(groupId)) {
1129                            shoppingCouponPersistence.remove(shoppingCoupon);
1130                    }
1131            }
1132    
1133            /**
1134             * Removes the shopping coupon where code = &#63; from the database.
1135             *
1136             * @param code the code
1137             * @throws SystemException if a system exception occurred
1138             */
1139            public void removeByCode(String code)
1140                    throws NoSuchCouponException, SystemException {
1141                    ShoppingCoupon shoppingCoupon = findByCode(code);
1142    
1143                    shoppingCouponPersistence.remove(shoppingCoupon);
1144            }
1145    
1146            /**
1147             * Removes all the shopping coupons from the database.
1148             *
1149             * @throws SystemException if a system exception occurred
1150             */
1151            public void removeAll() throws SystemException {
1152                    for (ShoppingCoupon shoppingCoupon : findAll()) {
1153                            shoppingCouponPersistence.remove(shoppingCoupon);
1154                    }
1155            }
1156    
1157            /**
1158             * Returns the number of shopping coupons where groupId = &#63;.
1159             *
1160             * @param groupId the group ID
1161             * @return the number of matching shopping coupons
1162             * @throws SystemException if a system exception occurred
1163             */
1164            public int countByGroupId(long groupId) throws SystemException {
1165                    Object[] finderArgs = new Object[] { groupId };
1166    
1167                    Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_GROUPID,
1168                                    finderArgs, this);
1169    
1170                    if (count == null) {
1171                            StringBundler query = new StringBundler(2);
1172    
1173                            query.append(_SQL_COUNT_SHOPPINGCOUPON_WHERE);
1174    
1175                            query.append(_FINDER_COLUMN_GROUPID_GROUPID_2);
1176    
1177                            String sql = query.toString();
1178    
1179                            Session session = null;
1180    
1181                            try {
1182                                    session = openSession();
1183    
1184                                    Query q = session.createQuery(sql);
1185    
1186                                    QueryPos qPos = QueryPos.getInstance(q);
1187    
1188                                    qPos.add(groupId);
1189    
1190                                    count = (Long)q.uniqueResult();
1191                            }
1192                            catch (Exception e) {
1193                                    throw processException(e);
1194                            }
1195                            finally {
1196                                    if (count == null) {
1197                                            count = Long.valueOf(0);
1198                                    }
1199    
1200                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_GROUPID,
1201                                            finderArgs, count);
1202    
1203                                    closeSession(session);
1204                            }
1205                    }
1206    
1207                    return count.intValue();
1208            }
1209    
1210            /**
1211             * Returns the number of shopping coupons where code = &#63;.
1212             *
1213             * @param code the code
1214             * @return the number of matching shopping coupons
1215             * @throws SystemException if a system exception occurred
1216             */
1217            public int countByCode(String code) throws SystemException {
1218                    Object[] finderArgs = new Object[] { code };
1219    
1220                    Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_CODE,
1221                                    finderArgs, this);
1222    
1223                    if (count == null) {
1224                            StringBundler query = new StringBundler(2);
1225    
1226                            query.append(_SQL_COUNT_SHOPPINGCOUPON_WHERE);
1227    
1228                            if (code == null) {
1229                                    query.append(_FINDER_COLUMN_CODE_CODE_1);
1230                            }
1231                            else {
1232                                    if (code.equals(StringPool.BLANK)) {
1233                                            query.append(_FINDER_COLUMN_CODE_CODE_3);
1234                                    }
1235                                    else {
1236                                            query.append(_FINDER_COLUMN_CODE_CODE_2);
1237                                    }
1238                            }
1239    
1240                            String sql = query.toString();
1241    
1242                            Session session = null;
1243    
1244                            try {
1245                                    session = openSession();
1246    
1247                                    Query q = session.createQuery(sql);
1248    
1249                                    QueryPos qPos = QueryPos.getInstance(q);
1250    
1251                                    if (code != null) {
1252                                            qPos.add(code);
1253                                    }
1254    
1255                                    count = (Long)q.uniqueResult();
1256                            }
1257                            catch (Exception e) {
1258                                    throw processException(e);
1259                            }
1260                            finally {
1261                                    if (count == null) {
1262                                            count = Long.valueOf(0);
1263                                    }
1264    
1265                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_CODE,
1266                                            finderArgs, count);
1267    
1268                                    closeSession(session);
1269                            }
1270                    }
1271    
1272                    return count.intValue();
1273            }
1274    
1275            /**
1276             * Returns the number of shopping coupons.
1277             *
1278             * @return the number of shopping coupons
1279             * @throws SystemException if a system exception occurred
1280             */
1281            public int countAll() throws SystemException {
1282                    Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
1283                                    FINDER_ARGS_EMPTY, this);
1284    
1285                    if (count == null) {
1286                            Session session = null;
1287    
1288                            try {
1289                                    session = openSession();
1290    
1291                                    Query q = session.createQuery(_SQL_COUNT_SHOPPINGCOUPON);
1292    
1293                                    count = (Long)q.uniqueResult();
1294                            }
1295                            catch (Exception e) {
1296                                    throw processException(e);
1297                            }
1298                            finally {
1299                                    if (count == null) {
1300                                            count = Long.valueOf(0);
1301                                    }
1302    
1303                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL,
1304                                            FINDER_ARGS_EMPTY, count);
1305    
1306                                    closeSession(session);
1307                            }
1308                    }
1309    
1310                    return count.intValue();
1311            }
1312    
1313            /**
1314             * Initializes the shopping coupon persistence.
1315             */
1316            public void afterPropertiesSet() {
1317                    String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
1318                                            com.liferay.portal.util.PropsUtil.get(
1319                                                    "value.object.listener.com.liferay.portlet.shopping.model.ShoppingCoupon")));
1320    
1321                    if (listenerClassNames.length > 0) {
1322                            try {
1323                                    List<ModelListener<ShoppingCoupon>> listenersList = new ArrayList<ModelListener<ShoppingCoupon>>();
1324    
1325                                    for (String listenerClassName : listenerClassNames) {
1326                                            listenersList.add((ModelListener<ShoppingCoupon>)InstanceFactory.newInstance(
1327                                                            listenerClassName));
1328                                    }
1329    
1330                                    listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
1331                            }
1332                            catch (Exception e) {
1333                                    _log.error(e);
1334                            }
1335                    }
1336            }
1337    
1338            public void destroy() {
1339                    EntityCacheUtil.removeCache(ShoppingCouponImpl.class.getName());
1340                    FinderCacheUtil.removeCache(FINDER_CLASS_NAME_ENTITY);
1341                    FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
1342            }
1343    
1344            @BeanReference(type = ShoppingCartPersistence.class)
1345            protected ShoppingCartPersistence shoppingCartPersistence;
1346            @BeanReference(type = ShoppingCategoryPersistence.class)
1347            protected ShoppingCategoryPersistence shoppingCategoryPersistence;
1348            @BeanReference(type = ShoppingCouponPersistence.class)
1349            protected ShoppingCouponPersistence shoppingCouponPersistence;
1350            @BeanReference(type = ShoppingItemPersistence.class)
1351            protected ShoppingItemPersistence shoppingItemPersistence;
1352            @BeanReference(type = ShoppingItemFieldPersistence.class)
1353            protected ShoppingItemFieldPersistence shoppingItemFieldPersistence;
1354            @BeanReference(type = ShoppingItemPricePersistence.class)
1355            protected ShoppingItemPricePersistence shoppingItemPricePersistence;
1356            @BeanReference(type = ShoppingOrderPersistence.class)
1357            protected ShoppingOrderPersistence shoppingOrderPersistence;
1358            @BeanReference(type = ShoppingOrderItemPersistence.class)
1359            protected ShoppingOrderItemPersistence shoppingOrderItemPersistence;
1360            @BeanReference(type = ResourcePersistence.class)
1361            protected ResourcePersistence resourcePersistence;
1362            @BeanReference(type = UserPersistence.class)
1363            protected UserPersistence userPersistence;
1364            private static final String _SQL_SELECT_SHOPPINGCOUPON = "SELECT shoppingCoupon FROM ShoppingCoupon shoppingCoupon";
1365            private static final String _SQL_SELECT_SHOPPINGCOUPON_WHERE = "SELECT shoppingCoupon FROM ShoppingCoupon shoppingCoupon WHERE ";
1366            private static final String _SQL_COUNT_SHOPPINGCOUPON = "SELECT COUNT(shoppingCoupon) FROM ShoppingCoupon shoppingCoupon";
1367            private static final String _SQL_COUNT_SHOPPINGCOUPON_WHERE = "SELECT COUNT(shoppingCoupon) FROM ShoppingCoupon shoppingCoupon WHERE ";
1368            private static final String _FINDER_COLUMN_GROUPID_GROUPID_2 = "shoppingCoupon.groupId = ?";
1369            private static final String _FINDER_COLUMN_CODE_CODE_1 = "shoppingCoupon.code IS NULL";
1370            private static final String _FINDER_COLUMN_CODE_CODE_2 = "shoppingCoupon.code = ?";
1371            private static final String _FINDER_COLUMN_CODE_CODE_3 = "(shoppingCoupon.code IS NULL OR shoppingCoupon.code = ?)";
1372            private static final String _ORDER_BY_ENTITY_ALIAS = "shoppingCoupon.";
1373            private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No ShoppingCoupon exists with the primary key ";
1374            private static final String _NO_SUCH_ENTITY_WITH_KEY = "No ShoppingCoupon exists with the key {";
1375            private static final boolean _HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE = com.liferay.portal.util.PropsValues.HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE;
1376            private static Log _log = LogFactoryUtil.getLog(ShoppingCouponPersistenceImpl.class);
1377            private static ShoppingCoupon _nullShoppingCoupon = new ShoppingCouponImpl() {
1378                            @Override
1379                            public Object clone() {
1380                                    return this;
1381                            }
1382    
1383                            @Override
1384                            public CacheModel<ShoppingCoupon> toCacheModel() {
1385                                    return _nullShoppingCouponCacheModel;
1386                            }
1387                    };
1388    
1389            private static CacheModel<ShoppingCoupon> _nullShoppingCouponCacheModel = new CacheModel<ShoppingCoupon>() {
1390                            public ShoppingCoupon toEntityModel() {
1391                                    return _nullShoppingCoupon;
1392                            }
1393                    };
1394    }