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