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            /**
824             * Caches the shopping coupon in the entity cache if it is enabled.
825             *
826             * @param shoppingCoupon the shopping coupon
827             */
828            @Override
829            public void cacheResult(ShoppingCoupon shoppingCoupon) {
830                    EntityCacheUtil.putResult(ShoppingCouponModelImpl.ENTITY_CACHE_ENABLED,
831                            ShoppingCouponImpl.class, shoppingCoupon.getPrimaryKey(),
832                            shoppingCoupon);
833    
834                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_CODE,
835                            new Object[] { shoppingCoupon.getCode() }, shoppingCoupon);
836    
837                    shoppingCoupon.resetOriginalValues();
838            }
839    
840            /**
841             * Caches the shopping coupons in the entity cache if it is enabled.
842             *
843             * @param shoppingCoupons the shopping coupons
844             */
845            @Override
846            public void cacheResult(List<ShoppingCoupon> shoppingCoupons) {
847                    for (ShoppingCoupon shoppingCoupon : shoppingCoupons) {
848                            if (EntityCacheUtil.getResult(
849                                                    ShoppingCouponModelImpl.ENTITY_CACHE_ENABLED,
850                                                    ShoppingCouponImpl.class, shoppingCoupon.getPrimaryKey()) == null) {
851                                    cacheResult(shoppingCoupon);
852                            }
853                            else {
854                                    shoppingCoupon.resetOriginalValues();
855                            }
856                    }
857            }
858    
859            /**
860             * Clears the cache for all shopping coupons.
861             *
862             * <p>
863             * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
864             * </p>
865             */
866            @Override
867            public void clearCache() {
868                    if (_HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE) {
869                            CacheRegistryUtil.clear(ShoppingCouponImpl.class.getName());
870                    }
871    
872                    EntityCacheUtil.clearCache(ShoppingCouponImpl.class.getName());
873    
874                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
875                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
876                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
877            }
878    
879            /**
880             * Clears the cache for the shopping coupon.
881             *
882             * <p>
883             * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
884             * </p>
885             */
886            @Override
887            public void clearCache(ShoppingCoupon shoppingCoupon) {
888                    EntityCacheUtil.removeResult(ShoppingCouponModelImpl.ENTITY_CACHE_ENABLED,
889                            ShoppingCouponImpl.class, shoppingCoupon.getPrimaryKey());
890    
891                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
892                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
893    
894                    clearUniqueFindersCache(shoppingCoupon);
895            }
896    
897            @Override
898            public void clearCache(List<ShoppingCoupon> shoppingCoupons) {
899                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
900                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
901    
902                    for (ShoppingCoupon shoppingCoupon : shoppingCoupons) {
903                            EntityCacheUtil.removeResult(ShoppingCouponModelImpl.ENTITY_CACHE_ENABLED,
904                                    ShoppingCouponImpl.class, shoppingCoupon.getPrimaryKey());
905    
906                            clearUniqueFindersCache(shoppingCoupon);
907                    }
908            }
909    
910            protected void cacheUniqueFindersCache(ShoppingCoupon shoppingCoupon) {
911                    if (shoppingCoupon.isNew()) {
912                            Object[] args = new Object[] { shoppingCoupon.getCode() };
913    
914                            FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_CODE, args,
915                                    Long.valueOf(1));
916                            FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_CODE, args,
917                                    shoppingCoupon);
918                    }
919                    else {
920                            ShoppingCouponModelImpl shoppingCouponModelImpl = (ShoppingCouponModelImpl)shoppingCoupon;
921    
922                            if ((shoppingCouponModelImpl.getColumnBitmask() &
923                                            FINDER_PATH_FETCH_BY_CODE.getColumnBitmask()) != 0) {
924                                    Object[] args = new Object[] { shoppingCoupon.getCode() };
925    
926                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_CODE, args,
927                                            Long.valueOf(1));
928                                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_CODE, args,
929                                            shoppingCoupon);
930                            }
931                    }
932            }
933    
934            protected void clearUniqueFindersCache(ShoppingCoupon shoppingCoupon) {
935                    ShoppingCouponModelImpl shoppingCouponModelImpl = (ShoppingCouponModelImpl)shoppingCoupon;
936    
937                    Object[] args = new Object[] { shoppingCoupon.getCode() };
938    
939                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_CODE, args);
940                    FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_CODE, args);
941    
942                    if ((shoppingCouponModelImpl.getColumnBitmask() &
943                                    FINDER_PATH_FETCH_BY_CODE.getColumnBitmask()) != 0) {
944                            args = new Object[] { shoppingCouponModelImpl.getOriginalCode() };
945    
946                            FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_CODE, args);
947                            FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_CODE, args);
948                    }
949            }
950    
951            /**
952             * Creates a new shopping coupon with the primary key. Does not add the shopping coupon to the database.
953             *
954             * @param couponId the primary key for the new shopping coupon
955             * @return the new shopping coupon
956             */
957            @Override
958            public ShoppingCoupon create(long couponId) {
959                    ShoppingCoupon shoppingCoupon = new ShoppingCouponImpl();
960    
961                    shoppingCoupon.setNew(true);
962                    shoppingCoupon.setPrimaryKey(couponId);
963    
964                    return shoppingCoupon;
965            }
966    
967            /**
968             * Removes the shopping coupon with the primary key from the database. Also notifies the appropriate model listeners.
969             *
970             * @param couponId the primary key of the shopping coupon
971             * @return the shopping coupon that was removed
972             * @throws com.liferay.portlet.shopping.NoSuchCouponException if a shopping coupon with the primary key could not be found
973             * @throws SystemException if a system exception occurred
974             */
975            @Override
976            public ShoppingCoupon remove(long couponId)
977                    throws NoSuchCouponException, SystemException {
978                    return remove((Serializable)couponId);
979            }
980    
981            /**
982             * Removes the shopping coupon with the primary key from the database. Also notifies the appropriate model listeners.
983             *
984             * @param primaryKey the primary key of the shopping coupon
985             * @return the shopping coupon that was removed
986             * @throws com.liferay.portlet.shopping.NoSuchCouponException if a shopping coupon with the primary key could not be found
987             * @throws SystemException if a system exception occurred
988             */
989            @Override
990            public ShoppingCoupon remove(Serializable primaryKey)
991                    throws NoSuchCouponException, SystemException {
992                    Session session = null;
993    
994                    try {
995                            session = openSession();
996    
997                            ShoppingCoupon shoppingCoupon = (ShoppingCoupon)session.get(ShoppingCouponImpl.class,
998                                            primaryKey);
999    
1000                            if (shoppingCoupon == null) {
1001                                    if (_log.isWarnEnabled()) {
1002                                            _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
1003                                    }
1004    
1005                                    throw new NoSuchCouponException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
1006                                            primaryKey);
1007                            }
1008    
1009                            return remove(shoppingCoupon);
1010                    }
1011                    catch (NoSuchCouponException nsee) {
1012                            throw nsee;
1013                    }
1014                    catch (Exception e) {
1015                            throw processException(e);
1016                    }
1017                    finally {
1018                            closeSession(session);
1019                    }
1020            }
1021    
1022            @Override
1023            protected ShoppingCoupon removeImpl(ShoppingCoupon shoppingCoupon)
1024                    throws SystemException {
1025                    shoppingCoupon = toUnwrappedModel(shoppingCoupon);
1026    
1027                    Session session = null;
1028    
1029                    try {
1030                            session = openSession();
1031    
1032                            if (!session.contains(shoppingCoupon)) {
1033                                    shoppingCoupon = (ShoppingCoupon)session.get(ShoppingCouponImpl.class,
1034                                                    shoppingCoupon.getPrimaryKeyObj());
1035                            }
1036    
1037                            if (shoppingCoupon != null) {
1038                                    session.delete(shoppingCoupon);
1039                            }
1040                    }
1041                    catch (Exception e) {
1042                            throw processException(e);
1043                    }
1044                    finally {
1045                            closeSession(session);
1046                    }
1047    
1048                    if (shoppingCoupon != null) {
1049                            clearCache(shoppingCoupon);
1050                    }
1051    
1052                    return shoppingCoupon;
1053            }
1054    
1055            @Override
1056            public ShoppingCoupon updateImpl(
1057                    com.liferay.portlet.shopping.model.ShoppingCoupon shoppingCoupon)
1058                    throws SystemException {
1059                    shoppingCoupon = toUnwrappedModel(shoppingCoupon);
1060    
1061                    boolean isNew = shoppingCoupon.isNew();
1062    
1063                    ShoppingCouponModelImpl shoppingCouponModelImpl = (ShoppingCouponModelImpl)shoppingCoupon;
1064    
1065                    Session session = null;
1066    
1067                    try {
1068                            session = openSession();
1069    
1070                            if (shoppingCoupon.isNew()) {
1071                                    session.save(shoppingCoupon);
1072    
1073                                    shoppingCoupon.setNew(false);
1074                            }
1075                            else {
1076                                    session.merge(shoppingCoupon);
1077                            }
1078                    }
1079                    catch (Exception e) {
1080                            throw processException(e);
1081                    }
1082                    finally {
1083                            closeSession(session);
1084                    }
1085    
1086                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
1087    
1088                    if (isNew || !ShoppingCouponModelImpl.COLUMN_BITMASK_ENABLED) {
1089                            FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
1090                    }
1091    
1092                    else {
1093                            if ((shoppingCouponModelImpl.getColumnBitmask() &
1094                                            FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_GROUPID.getColumnBitmask()) != 0) {
1095                                    Object[] args = new Object[] {
1096                                                    shoppingCouponModelImpl.getOriginalGroupId()
1097                                            };
1098    
1099                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_GROUPID, args);
1100                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_GROUPID,
1101                                            args);
1102    
1103                                    args = new Object[] { shoppingCouponModelImpl.getGroupId() };
1104    
1105                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_GROUPID, args);
1106                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_GROUPID,
1107                                            args);
1108                            }
1109                    }
1110    
1111                    EntityCacheUtil.putResult(ShoppingCouponModelImpl.ENTITY_CACHE_ENABLED,
1112                            ShoppingCouponImpl.class, shoppingCoupon.getPrimaryKey(),
1113                            shoppingCoupon);
1114    
1115                    clearUniqueFindersCache(shoppingCoupon);
1116                    cacheUniqueFindersCache(shoppingCoupon);
1117    
1118                    return shoppingCoupon;
1119            }
1120    
1121            protected ShoppingCoupon toUnwrappedModel(ShoppingCoupon shoppingCoupon) {
1122                    if (shoppingCoupon instanceof ShoppingCouponImpl) {
1123                            return shoppingCoupon;
1124                    }
1125    
1126                    ShoppingCouponImpl shoppingCouponImpl = new ShoppingCouponImpl();
1127    
1128                    shoppingCouponImpl.setNew(shoppingCoupon.isNew());
1129                    shoppingCouponImpl.setPrimaryKey(shoppingCoupon.getPrimaryKey());
1130    
1131                    shoppingCouponImpl.setCouponId(shoppingCoupon.getCouponId());
1132                    shoppingCouponImpl.setGroupId(shoppingCoupon.getGroupId());
1133                    shoppingCouponImpl.setCompanyId(shoppingCoupon.getCompanyId());
1134                    shoppingCouponImpl.setUserId(shoppingCoupon.getUserId());
1135                    shoppingCouponImpl.setUserName(shoppingCoupon.getUserName());
1136                    shoppingCouponImpl.setCreateDate(shoppingCoupon.getCreateDate());
1137                    shoppingCouponImpl.setModifiedDate(shoppingCoupon.getModifiedDate());
1138                    shoppingCouponImpl.setCode(shoppingCoupon.getCode());
1139                    shoppingCouponImpl.setName(shoppingCoupon.getName());
1140                    shoppingCouponImpl.setDescription(shoppingCoupon.getDescription());
1141                    shoppingCouponImpl.setStartDate(shoppingCoupon.getStartDate());
1142                    shoppingCouponImpl.setEndDate(shoppingCoupon.getEndDate());
1143                    shoppingCouponImpl.setActive(shoppingCoupon.isActive());
1144                    shoppingCouponImpl.setLimitCategories(shoppingCoupon.getLimitCategories());
1145                    shoppingCouponImpl.setLimitSkus(shoppingCoupon.getLimitSkus());
1146                    shoppingCouponImpl.setMinOrder(shoppingCoupon.getMinOrder());
1147                    shoppingCouponImpl.setDiscount(shoppingCoupon.getDiscount());
1148                    shoppingCouponImpl.setDiscountType(shoppingCoupon.getDiscountType());
1149    
1150                    return shoppingCouponImpl;
1151            }
1152    
1153            /**
1154             * Returns the shopping coupon with the primary key or throws a {@link com.liferay.portal.NoSuchModelException} if it could not be found.
1155             *
1156             * @param primaryKey the primary key of the shopping coupon
1157             * @return the shopping coupon
1158             * @throws com.liferay.portlet.shopping.NoSuchCouponException if a shopping coupon with the primary key could not be found
1159             * @throws SystemException if a system exception occurred
1160             */
1161            @Override
1162            public ShoppingCoupon findByPrimaryKey(Serializable primaryKey)
1163                    throws NoSuchCouponException, SystemException {
1164                    ShoppingCoupon shoppingCoupon = fetchByPrimaryKey(primaryKey);
1165    
1166                    if (shoppingCoupon == null) {
1167                            if (_log.isWarnEnabled()) {
1168                                    _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
1169                            }
1170    
1171                            throw new NoSuchCouponException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
1172                                    primaryKey);
1173                    }
1174    
1175                    return shoppingCoupon;
1176            }
1177    
1178            /**
1179             * Returns the shopping coupon with the primary key or throws a {@link com.liferay.portlet.shopping.NoSuchCouponException} if it could not be found.
1180             *
1181             * @param couponId the primary key of the shopping coupon
1182             * @return the shopping coupon
1183             * @throws com.liferay.portlet.shopping.NoSuchCouponException if a shopping coupon with the primary key could not be found
1184             * @throws SystemException if a system exception occurred
1185             */
1186            @Override
1187            public ShoppingCoupon findByPrimaryKey(long couponId)
1188                    throws NoSuchCouponException, SystemException {
1189                    return findByPrimaryKey((Serializable)couponId);
1190            }
1191    
1192            /**
1193             * Returns the shopping coupon with the primary key or returns <code>null</code> if it could not be found.
1194             *
1195             * @param primaryKey the primary key of the shopping coupon
1196             * @return the shopping coupon, or <code>null</code> if a shopping coupon with the primary key could not be found
1197             * @throws SystemException if a system exception occurred
1198             */
1199            @Override
1200            public ShoppingCoupon fetchByPrimaryKey(Serializable primaryKey)
1201                    throws SystemException {
1202                    ShoppingCoupon shoppingCoupon = (ShoppingCoupon)EntityCacheUtil.getResult(ShoppingCouponModelImpl.ENTITY_CACHE_ENABLED,
1203                                    ShoppingCouponImpl.class, primaryKey);
1204    
1205                    if (shoppingCoupon == _nullShoppingCoupon) {
1206                            return null;
1207                    }
1208    
1209                    if (shoppingCoupon == null) {
1210                            Session session = null;
1211    
1212                            try {
1213                                    session = openSession();
1214    
1215                                    shoppingCoupon = (ShoppingCoupon)session.get(ShoppingCouponImpl.class,
1216                                                    primaryKey);
1217    
1218                                    if (shoppingCoupon != null) {
1219                                            cacheResult(shoppingCoupon);
1220                                    }
1221                                    else {
1222                                            EntityCacheUtil.putResult(ShoppingCouponModelImpl.ENTITY_CACHE_ENABLED,
1223                                                    ShoppingCouponImpl.class, primaryKey,
1224                                                    _nullShoppingCoupon);
1225                                    }
1226                            }
1227                            catch (Exception e) {
1228                                    EntityCacheUtil.removeResult(ShoppingCouponModelImpl.ENTITY_CACHE_ENABLED,
1229                                            ShoppingCouponImpl.class, primaryKey);
1230    
1231                                    throw processException(e);
1232                            }
1233                            finally {
1234                                    closeSession(session);
1235                            }
1236                    }
1237    
1238                    return shoppingCoupon;
1239            }
1240    
1241            /**
1242             * Returns the shopping coupon with the primary key or returns <code>null</code> if it could not be found.
1243             *
1244             * @param couponId the primary key of the shopping coupon
1245             * @return the shopping coupon, or <code>null</code> if a shopping coupon with the primary key could not be found
1246             * @throws SystemException if a system exception occurred
1247             */
1248            @Override
1249            public ShoppingCoupon fetchByPrimaryKey(long couponId)
1250                    throws SystemException {
1251                    return fetchByPrimaryKey((Serializable)couponId);
1252            }
1253    
1254            /**
1255             * Returns all the shopping coupons.
1256             *
1257             * @return the shopping coupons
1258             * @throws SystemException if a system exception occurred
1259             */
1260            @Override
1261            public List<ShoppingCoupon> findAll() throws SystemException {
1262                    return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
1263            }
1264    
1265            /**
1266             * Returns a range of all the shopping coupons.
1267             *
1268             * <p>
1269             * 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.
1270             * </p>
1271             *
1272             * @param start the lower bound of the range of shopping coupons
1273             * @param end the upper bound of the range of shopping coupons (not inclusive)
1274             * @return the range of shopping coupons
1275             * @throws SystemException if a system exception occurred
1276             */
1277            @Override
1278            public List<ShoppingCoupon> findAll(int start, int end)
1279                    throws SystemException {
1280                    return findAll(start, end, null);
1281            }
1282    
1283            /**
1284             * Returns an ordered range of all the shopping coupons.
1285             *
1286             * <p>
1287             * 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.
1288             * </p>
1289             *
1290             * @param start the lower bound of the range of shopping coupons
1291             * @param end the upper bound of the range of shopping coupons (not inclusive)
1292             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
1293             * @return the ordered range of shopping coupons
1294             * @throws SystemException if a system exception occurred
1295             */
1296            @Override
1297            public List<ShoppingCoupon> findAll(int start, int end,
1298                    OrderByComparator orderByComparator) throws SystemException {
1299                    boolean pagination = true;
1300                    FinderPath finderPath = null;
1301                    Object[] finderArgs = null;
1302    
1303                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
1304                                    (orderByComparator == null)) {
1305                            pagination = false;
1306                            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL;
1307                            finderArgs = FINDER_ARGS_EMPTY;
1308                    }
1309                    else {
1310                            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_ALL;
1311                            finderArgs = new Object[] { start, end, orderByComparator };
1312                    }
1313    
1314                    List<ShoppingCoupon> list = (List<ShoppingCoupon>)FinderCacheUtil.getResult(finderPath,
1315                                    finderArgs, this);
1316    
1317                    if (list == null) {
1318                            StringBundler query = null;
1319                            String sql = null;
1320    
1321                            if (orderByComparator != null) {
1322                                    query = new StringBundler(2 +
1323                                                    (orderByComparator.getOrderByFields().length * 3));
1324    
1325                                    query.append(_SQL_SELECT_SHOPPINGCOUPON);
1326    
1327                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
1328                                            orderByComparator);
1329    
1330                                    sql = query.toString();
1331                            }
1332                            else {
1333                                    sql = _SQL_SELECT_SHOPPINGCOUPON;
1334    
1335                                    if (pagination) {
1336                                            sql = sql.concat(ShoppingCouponModelImpl.ORDER_BY_JPQL);
1337                                    }
1338                            }
1339    
1340                            Session session = null;
1341    
1342                            try {
1343                                    session = openSession();
1344    
1345                                    Query q = session.createQuery(sql);
1346    
1347                                    if (!pagination) {
1348                                            list = (List<ShoppingCoupon>)QueryUtil.list(q,
1349                                                            getDialect(), start, end, false);
1350    
1351                                            Collections.sort(list);
1352    
1353                                            list = new UnmodifiableList<ShoppingCoupon>(list);
1354                                    }
1355                                    else {
1356                                            list = (List<ShoppingCoupon>)QueryUtil.list(q,
1357                                                            getDialect(), start, end);
1358                                    }
1359    
1360                                    cacheResult(list);
1361    
1362                                    FinderCacheUtil.putResult(finderPath, finderArgs, list);
1363                            }
1364                            catch (Exception e) {
1365                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
1366    
1367                                    throw processException(e);
1368                            }
1369                            finally {
1370                                    closeSession(session);
1371                            }
1372                    }
1373    
1374                    return list;
1375            }
1376    
1377            /**
1378             * Removes all the shopping coupons from the database.
1379             *
1380             * @throws SystemException if a system exception occurred
1381             */
1382            @Override
1383            public void removeAll() throws SystemException {
1384                    for (ShoppingCoupon shoppingCoupon : findAll()) {
1385                            remove(shoppingCoupon);
1386                    }
1387            }
1388    
1389            /**
1390             * Returns the number of shopping coupons.
1391             *
1392             * @return the number of shopping coupons
1393             * @throws SystemException if a system exception occurred
1394             */
1395            @Override
1396            public int countAll() throws SystemException {
1397                    Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
1398                                    FINDER_ARGS_EMPTY, this);
1399    
1400                    if (count == null) {
1401                            Session session = null;
1402    
1403                            try {
1404                                    session = openSession();
1405    
1406                                    Query q = session.createQuery(_SQL_COUNT_SHOPPINGCOUPON);
1407    
1408                                    count = (Long)q.uniqueResult();
1409    
1410                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL,
1411                                            FINDER_ARGS_EMPTY, count);
1412                            }
1413                            catch (Exception e) {
1414                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_ALL,
1415                                            FINDER_ARGS_EMPTY);
1416    
1417                                    throw processException(e);
1418                            }
1419                            finally {
1420                                    closeSession(session);
1421                            }
1422                    }
1423    
1424                    return count.intValue();
1425            }
1426    
1427            @Override
1428            protected Set<String> getBadColumnNames() {
1429                    return _badColumnNames;
1430            }
1431    
1432            /**
1433             * Initializes the shopping coupon persistence.
1434             */
1435            public void afterPropertiesSet() {
1436                    String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
1437                                            com.liferay.portal.util.PropsUtil.get(
1438                                                    "value.object.listener.com.liferay.portlet.shopping.model.ShoppingCoupon")));
1439    
1440                    if (listenerClassNames.length > 0) {
1441                            try {
1442                                    List<ModelListener<ShoppingCoupon>> listenersList = new ArrayList<ModelListener<ShoppingCoupon>>();
1443    
1444                                    for (String listenerClassName : listenerClassNames) {
1445                                            listenersList.add((ModelListener<ShoppingCoupon>)InstanceFactory.newInstance(
1446                                                            getClassLoader(), listenerClassName));
1447                                    }
1448    
1449                                    listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
1450                            }
1451                            catch (Exception e) {
1452                                    _log.error(e);
1453                            }
1454                    }
1455            }
1456    
1457            public void destroy() {
1458                    EntityCacheUtil.removeCache(ShoppingCouponImpl.class.getName());
1459                    FinderCacheUtil.removeCache(FINDER_CLASS_NAME_ENTITY);
1460                    FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
1461                    FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
1462            }
1463    
1464            private static final String _SQL_SELECT_SHOPPINGCOUPON = "SELECT shoppingCoupon FROM ShoppingCoupon shoppingCoupon";
1465            private static final String _SQL_SELECT_SHOPPINGCOUPON_WHERE = "SELECT shoppingCoupon FROM ShoppingCoupon shoppingCoupon WHERE ";
1466            private static final String _SQL_COUNT_SHOPPINGCOUPON = "SELECT COUNT(shoppingCoupon) FROM ShoppingCoupon shoppingCoupon";
1467            private static final String _SQL_COUNT_SHOPPINGCOUPON_WHERE = "SELECT COUNT(shoppingCoupon) FROM ShoppingCoupon shoppingCoupon WHERE ";
1468            private static final String _ORDER_BY_ENTITY_ALIAS = "shoppingCoupon.";
1469            private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No ShoppingCoupon exists with the primary key ";
1470            private static final String _NO_SUCH_ENTITY_WITH_KEY = "No ShoppingCoupon exists with the key {";
1471            private static final boolean _HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE = com.liferay.portal.util.PropsValues.HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE;
1472            private static Log _log = LogFactoryUtil.getLog(ShoppingCouponPersistenceImpl.class);
1473            private static Set<String> _badColumnNames = SetUtil.fromArray(new String[] {
1474                                    "code", "active"
1475                            });
1476            private static ShoppingCoupon _nullShoppingCoupon = new ShoppingCouponImpl() {
1477                            @Override
1478                            public Object clone() {
1479                                    return this;
1480                            }
1481    
1482                            @Override
1483                            public CacheModel<ShoppingCoupon> toCacheModel() {
1484                                    return _nullShoppingCouponCacheModel;
1485                            }
1486                    };
1487    
1488            private static CacheModel<ShoppingCoupon> _nullShoppingCouponCacheModel = new CacheModel<ShoppingCoupon>() {
1489                            @Override
1490                            public ShoppingCoupon toEntityModel() {
1491                                    return _nullShoppingCoupon;
1492                            }
1493                    };
1494    }