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