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