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