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