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