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