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