001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.shopping.service.persistence;
016    
017    import com.liferay.portal.NoSuchModelException;
018    import com.liferay.portal.kernel.annotation.BeanReference;
019    import com.liferay.portal.kernel.cache.CacheRegistryUtil;
020    import com.liferay.portal.kernel.dao.orm.EntityCacheUtil;
021    import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
022    import com.liferay.portal.kernel.dao.orm.FinderPath;
023    import com.liferay.portal.kernel.dao.orm.Query;
024    import com.liferay.portal.kernel.dao.orm.QueryPos;
025    import com.liferay.portal.kernel.dao.orm.QueryUtil;
026    import com.liferay.portal.kernel.dao.orm.Session;
027    import com.liferay.portal.kernel.exception.SystemException;
028    import com.liferay.portal.kernel.log.Log;
029    import com.liferay.portal.kernel.log.LogFactoryUtil;
030    import com.liferay.portal.kernel.util.GetterUtil;
031    import com.liferay.portal.kernel.util.InstanceFactory;
032    import com.liferay.portal.kernel.util.OrderByComparator;
033    import com.liferay.portal.kernel.util.StringBundler;
034    import com.liferay.portal.kernel.util.StringPool;
035    import com.liferay.portal.kernel.util.StringUtil;
036    import com.liferay.portal.kernel.util.Validator;
037    import com.liferay.portal.model.ModelListener;
038    import com.liferay.portal.service.persistence.BatchSessionUtil;
039    import com.liferay.portal.service.persistence.ResourcePersistence;
040    import com.liferay.portal.service.persistence.UserPersistence;
041    import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
042    
043    import com.liferay.portlet.shopping.NoSuchCouponException;
044    import com.liferay.portlet.shopping.model.ShoppingCoupon;
045    import com.liferay.portlet.shopping.model.impl.ShoppingCouponImpl;
046    import com.liferay.portlet.shopping.model.impl.ShoppingCouponModelImpl;
047    
048    import java.io.Serializable;
049    
050    import java.util.ArrayList;
051    import java.util.Collections;
052    import java.util.List;
053    
054    /**
055     * @author    Brian Wing Shun Chan
056     * @see       ShoppingCouponPersistence
057     * @see       ShoppingCouponUtil
058     * @generated
059     */
060    public class ShoppingCouponPersistenceImpl extends BasePersistenceImpl<ShoppingCoupon>
061            implements ShoppingCouponPersistence {
062            public static final String FINDER_CLASS_NAME_ENTITY = ShoppingCouponImpl.class.getName();
063            public static final String FINDER_CLASS_NAME_LIST = FINDER_CLASS_NAME_ENTITY +
064                    ".List";
065            public static final FinderPath FINDER_PATH_FIND_BY_GROUPID = new FinderPath(ShoppingCouponModelImpl.ENTITY_CACHE_ENABLED,
066                            ShoppingCouponModelImpl.FINDER_CACHE_ENABLED,
067                            FINDER_CLASS_NAME_LIST, "findByGroupId",
068                            new String[] {
069                                    Long.class.getName(),
070                                    
071                            "java.lang.Integer", "java.lang.Integer",
072                                    "com.liferay.portal.kernel.util.OrderByComparator"
073                            });
074            public static final FinderPath FINDER_PATH_COUNT_BY_GROUPID = new FinderPath(ShoppingCouponModelImpl.ENTITY_CACHE_ENABLED,
075                            ShoppingCouponModelImpl.FINDER_CACHE_ENABLED,
076                            FINDER_CLASS_NAME_LIST, "countByGroupId",
077                            new String[] { Long.class.getName() });
078            public static final FinderPath FINDER_PATH_FETCH_BY_CODE = new FinderPath(ShoppingCouponModelImpl.ENTITY_CACHE_ENABLED,
079                            ShoppingCouponModelImpl.FINDER_CACHE_ENABLED,
080                            FINDER_CLASS_NAME_ENTITY, "fetchByCode",
081                            new String[] { String.class.getName() });
082            public static final FinderPath FINDER_PATH_COUNT_BY_CODE = new FinderPath(ShoppingCouponModelImpl.ENTITY_CACHE_ENABLED,
083                            ShoppingCouponModelImpl.FINDER_CACHE_ENABLED,
084                            FINDER_CLASS_NAME_LIST, "countByCode",
085                            new String[] { String.class.getName() });
086            public static final FinderPath FINDER_PATH_FIND_ALL = new FinderPath(ShoppingCouponModelImpl.ENTITY_CACHE_ENABLED,
087                            ShoppingCouponModelImpl.FINDER_CACHE_ENABLED,
088                            FINDER_CLASS_NAME_LIST, "findAll", new String[0]);
089            public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(ShoppingCouponModelImpl.ENTITY_CACHE_ENABLED,
090                            ShoppingCouponModelImpl.FINDER_CACHE_ENABLED,
091                            FINDER_CLASS_NAME_LIST, "countAll", new String[0]);
092    
093            public void cacheResult(ShoppingCoupon shoppingCoupon) {
094                    EntityCacheUtil.putResult(ShoppingCouponModelImpl.ENTITY_CACHE_ENABLED,
095                            ShoppingCouponImpl.class, shoppingCoupon.getPrimaryKey(),
096                            shoppingCoupon);
097    
098                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_CODE,
099                            new Object[] { shoppingCoupon.getCode() }, shoppingCoupon);
100            }
101    
102            public void cacheResult(List<ShoppingCoupon> shoppingCoupons) {
103                    for (ShoppingCoupon shoppingCoupon : shoppingCoupons) {
104                            if (EntityCacheUtil.getResult(
105                                                    ShoppingCouponModelImpl.ENTITY_CACHE_ENABLED,
106                                                    ShoppingCouponImpl.class,
107                                                    shoppingCoupon.getPrimaryKey(), this) == null) {
108                                    cacheResult(shoppingCoupon);
109                            }
110                    }
111            }
112    
113            public void clearCache() {
114                    CacheRegistryUtil.clear(ShoppingCouponImpl.class.getName());
115                    EntityCacheUtil.clearCache(ShoppingCouponImpl.class.getName());
116                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
117                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
118            }
119    
120            public void clearCache(ShoppingCoupon shoppingCoupon) {
121                    EntityCacheUtil.removeResult(ShoppingCouponModelImpl.ENTITY_CACHE_ENABLED,
122                            ShoppingCouponImpl.class, shoppingCoupon.getPrimaryKey());
123    
124                    FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_CODE,
125                            new Object[] { shoppingCoupon.getCode() });
126            }
127    
128            public ShoppingCoupon create(long couponId) {
129                    ShoppingCoupon shoppingCoupon = new ShoppingCouponImpl();
130    
131                    shoppingCoupon.setNew(true);
132                    shoppingCoupon.setPrimaryKey(couponId);
133    
134                    return shoppingCoupon;
135            }
136    
137            public ShoppingCoupon remove(Serializable primaryKey)
138                    throws NoSuchModelException, SystemException {
139                    return remove(((Long)primaryKey).longValue());
140            }
141    
142            public ShoppingCoupon remove(long couponId)
143                    throws NoSuchCouponException, SystemException {
144                    Session session = null;
145    
146                    try {
147                            session = openSession();
148    
149                            ShoppingCoupon shoppingCoupon = (ShoppingCoupon)session.get(ShoppingCouponImpl.class,
150                                            new Long(couponId));
151    
152                            if (shoppingCoupon == null) {
153                                    if (_log.isWarnEnabled()) {
154                                            _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + couponId);
155                                    }
156    
157                                    throw new NoSuchCouponException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
158                                            couponId);
159                            }
160    
161                            return remove(shoppingCoupon);
162                    }
163                    catch (NoSuchCouponException nsee) {
164                            throw nsee;
165                    }
166                    catch (Exception e) {
167                            throw processException(e);
168                    }
169                    finally {
170                            closeSession(session);
171                    }
172            }
173    
174            protected ShoppingCoupon removeImpl(ShoppingCoupon shoppingCoupon)
175                    throws SystemException {
176                    shoppingCoupon = toUnwrappedModel(shoppingCoupon);
177    
178                    Session session = null;
179    
180                    try {
181                            session = openSession();
182    
183                            if (shoppingCoupon.isCachedModel() || BatchSessionUtil.isEnabled()) {
184                                    Object staleObject = session.get(ShoppingCouponImpl.class,
185                                                    shoppingCoupon.getPrimaryKeyObj());
186    
187                                    if (staleObject != null) {
188                                            session.evict(staleObject);
189                                    }
190                            }
191    
192                            session.delete(shoppingCoupon);
193    
194                            session.flush();
195                    }
196                    catch (Exception e) {
197                            throw processException(e);
198                    }
199                    finally {
200                            closeSession(session);
201                    }
202    
203                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
204    
205                    ShoppingCouponModelImpl shoppingCouponModelImpl = (ShoppingCouponModelImpl)shoppingCoupon;
206    
207                    FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_CODE,
208                            new Object[] { shoppingCouponModelImpl.getOriginalCode() });
209    
210                    EntityCacheUtil.removeResult(ShoppingCouponModelImpl.ENTITY_CACHE_ENABLED,
211                            ShoppingCouponImpl.class, shoppingCoupon.getPrimaryKey());
212    
213                    return shoppingCoupon;
214            }
215    
216            public ShoppingCoupon updateImpl(
217                    com.liferay.portlet.shopping.model.ShoppingCoupon shoppingCoupon,
218                    boolean merge) throws SystemException {
219                    shoppingCoupon = toUnwrappedModel(shoppingCoupon);
220    
221                    boolean isNew = shoppingCoupon.isNew();
222    
223                    ShoppingCouponModelImpl shoppingCouponModelImpl = (ShoppingCouponModelImpl)shoppingCoupon;
224    
225                    Session session = null;
226    
227                    try {
228                            session = openSession();
229    
230                            BatchSessionUtil.update(session, shoppingCoupon, merge);
231    
232                            shoppingCoupon.setNew(false);
233                    }
234                    catch (Exception e) {
235                            throw processException(e);
236                    }
237                    finally {
238                            closeSession(session);
239                    }
240    
241                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
242    
243                    EntityCacheUtil.putResult(ShoppingCouponModelImpl.ENTITY_CACHE_ENABLED,
244                            ShoppingCouponImpl.class, shoppingCoupon.getPrimaryKey(),
245                            shoppingCoupon);
246    
247                    if (!isNew &&
248                                    (!Validator.equals(shoppingCoupon.getCode(),
249                                            shoppingCouponModelImpl.getOriginalCode()))) {
250                            FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_CODE,
251                                    new Object[] { shoppingCouponModelImpl.getOriginalCode() });
252                    }
253    
254                    if (isNew ||
255                                    (!Validator.equals(shoppingCoupon.getCode(),
256                                            shoppingCouponModelImpl.getOriginalCode()))) {
257                            FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_CODE,
258                                    new Object[] { shoppingCoupon.getCode() }, shoppingCoupon);
259                    }
260    
261                    return shoppingCoupon;
262            }
263    
264            protected ShoppingCoupon toUnwrappedModel(ShoppingCoupon shoppingCoupon) {
265                    if (shoppingCoupon instanceof ShoppingCouponImpl) {
266                            return shoppingCoupon;
267                    }
268    
269                    ShoppingCouponImpl shoppingCouponImpl = new ShoppingCouponImpl();
270    
271                    shoppingCouponImpl.setNew(shoppingCoupon.isNew());
272                    shoppingCouponImpl.setPrimaryKey(shoppingCoupon.getPrimaryKey());
273    
274                    shoppingCouponImpl.setCouponId(shoppingCoupon.getCouponId());
275                    shoppingCouponImpl.setGroupId(shoppingCoupon.getGroupId());
276                    shoppingCouponImpl.setCompanyId(shoppingCoupon.getCompanyId());
277                    shoppingCouponImpl.setUserId(shoppingCoupon.getUserId());
278                    shoppingCouponImpl.setUserName(shoppingCoupon.getUserName());
279                    shoppingCouponImpl.setCreateDate(shoppingCoupon.getCreateDate());
280                    shoppingCouponImpl.setModifiedDate(shoppingCoupon.getModifiedDate());
281                    shoppingCouponImpl.setCode(shoppingCoupon.getCode());
282                    shoppingCouponImpl.setName(shoppingCoupon.getName());
283                    shoppingCouponImpl.setDescription(shoppingCoupon.getDescription());
284                    shoppingCouponImpl.setStartDate(shoppingCoupon.getStartDate());
285                    shoppingCouponImpl.setEndDate(shoppingCoupon.getEndDate());
286                    shoppingCouponImpl.setActive(shoppingCoupon.isActive());
287                    shoppingCouponImpl.setLimitCategories(shoppingCoupon.getLimitCategories());
288                    shoppingCouponImpl.setLimitSkus(shoppingCoupon.getLimitSkus());
289                    shoppingCouponImpl.setMinOrder(shoppingCoupon.getMinOrder());
290                    shoppingCouponImpl.setDiscount(shoppingCoupon.getDiscount());
291                    shoppingCouponImpl.setDiscountType(shoppingCoupon.getDiscountType());
292    
293                    return shoppingCouponImpl;
294            }
295    
296            public ShoppingCoupon findByPrimaryKey(Serializable primaryKey)
297                    throws NoSuchModelException, SystemException {
298                    return findByPrimaryKey(((Long)primaryKey).longValue());
299            }
300    
301            public ShoppingCoupon findByPrimaryKey(long couponId)
302                    throws NoSuchCouponException, SystemException {
303                    ShoppingCoupon shoppingCoupon = fetchByPrimaryKey(couponId);
304    
305                    if (shoppingCoupon == null) {
306                            if (_log.isWarnEnabled()) {
307                                    _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + couponId);
308                            }
309    
310                            throw new NoSuchCouponException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
311                                    couponId);
312                    }
313    
314                    return shoppingCoupon;
315            }
316    
317            public ShoppingCoupon fetchByPrimaryKey(Serializable primaryKey)
318                    throws SystemException {
319                    return fetchByPrimaryKey(((Long)primaryKey).longValue());
320            }
321    
322            public ShoppingCoupon fetchByPrimaryKey(long couponId)
323                    throws SystemException {
324                    ShoppingCoupon shoppingCoupon = (ShoppingCoupon)EntityCacheUtil.getResult(ShoppingCouponModelImpl.ENTITY_CACHE_ENABLED,
325                                    ShoppingCouponImpl.class, couponId, this);
326    
327                    if (shoppingCoupon == null) {
328                            Session session = null;
329    
330                            try {
331                                    session = openSession();
332    
333                                    shoppingCoupon = (ShoppingCoupon)session.get(ShoppingCouponImpl.class,
334                                                    new Long(couponId));
335                            }
336                            catch (Exception e) {
337                                    throw processException(e);
338                            }
339                            finally {
340                                    if (shoppingCoupon != null) {
341                                            cacheResult(shoppingCoupon);
342                                    }
343    
344                                    closeSession(session);
345                            }
346                    }
347    
348                    return shoppingCoupon;
349            }
350    
351            public List<ShoppingCoupon> findByGroupId(long groupId)
352                    throws SystemException {
353                    return findByGroupId(groupId, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
354            }
355    
356            public List<ShoppingCoupon> findByGroupId(long groupId, int start, int end)
357                    throws SystemException {
358                    return findByGroupId(groupId, start, end, null);
359            }
360    
361            public List<ShoppingCoupon> findByGroupId(long groupId, int start, int end,
362                    OrderByComparator orderByComparator) throws SystemException {
363                    Object[] finderArgs = new Object[] {
364                                    groupId,
365                                    
366                                    String.valueOf(start), String.valueOf(end),
367                                    String.valueOf(orderByComparator)
368                            };
369    
370                    List<ShoppingCoupon> list = (List<ShoppingCoupon>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_GROUPID,
371                                    finderArgs, this);
372    
373                    if (list == null) {
374                            Session session = null;
375    
376                            try {
377                                    session = openSession();
378    
379                                    StringBundler query = null;
380    
381                                    if (orderByComparator != null) {
382                                            query = new StringBundler(3 +
383                                                            (orderByComparator.getOrderByFields().length * 3));
384                                    }
385                                    else {
386                                            query = new StringBundler(3);
387                                    }
388    
389                                    query.append(_SQL_SELECT_SHOPPINGCOUPON_WHERE);
390    
391                                    query.append(_FINDER_COLUMN_GROUPID_GROUPID_2);
392    
393                                    if (orderByComparator != null) {
394                                            appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
395                                                    orderByComparator);
396                                    }
397    
398                                    else {
399                                            query.append(ShoppingCouponModelImpl.ORDER_BY_JPQL);
400                                    }
401    
402                                    String sql = query.toString();
403    
404                                    Query q = session.createQuery(sql);
405    
406                                    QueryPos qPos = QueryPos.getInstance(q);
407    
408                                    qPos.add(groupId);
409    
410                                    list = (List<ShoppingCoupon>)QueryUtil.list(q, getDialect(),
411                                                    start, end);
412                            }
413                            catch (Exception e) {
414                                    throw processException(e);
415                            }
416                            finally {
417                                    if (list == null) {
418                                            list = new ArrayList<ShoppingCoupon>();
419                                    }
420    
421                                    cacheResult(list);
422    
423                                    FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_GROUPID,
424                                            finderArgs, list);
425    
426                                    closeSession(session);
427                            }
428                    }
429    
430                    return list;
431            }
432    
433            public ShoppingCoupon findByGroupId_First(long groupId,
434                    OrderByComparator orderByComparator)
435                    throws NoSuchCouponException, SystemException {
436                    List<ShoppingCoupon> list = findByGroupId(groupId, 0, 1,
437                                    orderByComparator);
438    
439                    if (list.isEmpty()) {
440                            StringBundler msg = new StringBundler(4);
441    
442                            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
443    
444                            msg.append("groupId=");
445                            msg.append(groupId);
446    
447                            msg.append(StringPool.CLOSE_CURLY_BRACE);
448    
449                            throw new NoSuchCouponException(msg.toString());
450                    }
451                    else {
452                            return list.get(0);
453                    }
454            }
455    
456            public ShoppingCoupon findByGroupId_Last(long groupId,
457                    OrderByComparator orderByComparator)
458                    throws NoSuchCouponException, SystemException {
459                    int count = countByGroupId(groupId);
460    
461                    List<ShoppingCoupon> list = findByGroupId(groupId, count - 1, count,
462                                    orderByComparator);
463    
464                    if (list.isEmpty()) {
465                            StringBundler msg = new StringBundler(4);
466    
467                            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
468    
469                            msg.append("groupId=");
470                            msg.append(groupId);
471    
472                            msg.append(StringPool.CLOSE_CURLY_BRACE);
473    
474                            throw new NoSuchCouponException(msg.toString());
475                    }
476                    else {
477                            return list.get(0);
478                    }
479            }
480    
481            public ShoppingCoupon[] findByGroupId_PrevAndNext(long couponId,
482                    long groupId, OrderByComparator orderByComparator)
483                    throws NoSuchCouponException, SystemException {
484                    ShoppingCoupon shoppingCoupon = findByPrimaryKey(couponId);
485    
486                    Session session = null;
487    
488                    try {
489                            session = openSession();
490    
491                            ShoppingCoupon[] array = new ShoppingCouponImpl[3];
492    
493                            array[0] = getByGroupId_PrevAndNext(session, shoppingCoupon,
494                                            groupId, orderByComparator, true);
495    
496                            array[1] = shoppingCoupon;
497    
498                            array[2] = getByGroupId_PrevAndNext(session, shoppingCoupon,
499                                            groupId, orderByComparator, false);
500    
501                            return array;
502                    }
503                    catch (Exception e) {
504                            throw processException(e);
505                    }
506                    finally {
507                            closeSession(session);
508                    }
509            }
510    
511            protected ShoppingCoupon getByGroupId_PrevAndNext(Session session,
512                    ShoppingCoupon shoppingCoupon, long groupId,
513                    OrderByComparator orderByComparator, boolean previous) {
514                    StringBundler query = null;
515    
516                    if (orderByComparator != null) {
517                            query = new StringBundler(6 +
518                                            (orderByComparator.getOrderByFields().length * 6));
519                    }
520                    else {
521                            query = new StringBundler(3);
522                    }
523    
524                    query.append(_SQL_SELECT_SHOPPINGCOUPON_WHERE);
525    
526                    query.append(_FINDER_COLUMN_GROUPID_GROUPID_2);
527    
528                    if (orderByComparator != null) {
529                            String[] orderByFields = orderByComparator.getOrderByFields();
530    
531                            if (orderByFields.length > 0) {
532                                    query.append(WHERE_AND);
533                            }
534    
535                            for (int i = 0; i < orderByFields.length; i++) {
536                                    query.append(_ORDER_BY_ENTITY_ALIAS);
537                                    query.append(orderByFields[i]);
538    
539                                    if ((i + 1) < orderByFields.length) {
540                                            if (orderByComparator.isAscending() ^ previous) {
541                                                    query.append(WHERE_GREATER_THAN_HAS_NEXT);
542                                            }
543                                            else {
544                                                    query.append(WHERE_LESSER_THAN_HAS_NEXT);
545                                            }
546                                    }
547                                    else {
548                                            if (orderByComparator.isAscending() ^ previous) {
549                                                    query.append(WHERE_GREATER_THAN);
550                                            }
551                                            else {
552                                                    query.append(WHERE_LESSER_THAN);
553                                            }
554                                    }
555                            }
556    
557                            query.append(ORDER_BY_CLAUSE);
558    
559                            for (int i = 0; i < orderByFields.length; i++) {
560                                    query.append(_ORDER_BY_ENTITY_ALIAS);
561                                    query.append(orderByFields[i]);
562    
563                                    if ((i + 1) < orderByFields.length) {
564                                            if (orderByComparator.isAscending() ^ previous) {
565                                                    query.append(ORDER_BY_ASC_HAS_NEXT);
566                                            }
567                                            else {
568                                                    query.append(ORDER_BY_DESC_HAS_NEXT);
569                                            }
570                                    }
571                                    else {
572                                            if (orderByComparator.isAscending() ^ previous) {
573                                                    query.append(ORDER_BY_ASC);
574                                            }
575                                            else {
576                                                    query.append(ORDER_BY_DESC);
577                                            }
578                                    }
579                            }
580                    }
581    
582                    else {
583                            query.append(ShoppingCouponModelImpl.ORDER_BY_JPQL);
584                    }
585    
586                    String sql = query.toString();
587    
588                    Query q = session.createQuery(sql);
589    
590                    q.setFirstResult(0);
591                    q.setMaxResults(2);
592    
593                    QueryPos qPos = QueryPos.getInstance(q);
594    
595                    qPos.add(groupId);
596    
597                    if (orderByComparator != null) {
598                            Object[] values = orderByComparator.getOrderByValues(shoppingCoupon);
599    
600                            for (Object value : values) {
601                                    qPos.add(value);
602                            }
603                    }
604    
605                    List<ShoppingCoupon> list = q.list();
606    
607                    if (list.size() == 2) {
608                            return list.get(1);
609                    }
610                    else {
611                            return null;
612                    }
613            }
614    
615            public ShoppingCoupon findByCode(String code)
616                    throws NoSuchCouponException, SystemException {
617                    ShoppingCoupon shoppingCoupon = fetchByCode(code);
618    
619                    if (shoppingCoupon == null) {
620                            StringBundler msg = new StringBundler(4);
621    
622                            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
623    
624                            msg.append("code=");
625                            msg.append(code);
626    
627                            msg.append(StringPool.CLOSE_CURLY_BRACE);
628    
629                            if (_log.isWarnEnabled()) {
630                                    _log.warn(msg.toString());
631                            }
632    
633                            throw new NoSuchCouponException(msg.toString());
634                    }
635    
636                    return shoppingCoupon;
637            }
638    
639            public ShoppingCoupon fetchByCode(String code) throws SystemException {
640                    return fetchByCode(code, true);
641            }
642    
643            public ShoppingCoupon fetchByCode(String code, boolean retrieveFromCache)
644                    throws SystemException {
645                    Object[] finderArgs = new Object[] { code };
646    
647                    Object result = null;
648    
649                    if (retrieveFromCache) {
650                            result = FinderCacheUtil.getResult(FINDER_PATH_FETCH_BY_CODE,
651                                            finderArgs, this);
652                    }
653    
654                    if (result == null) {
655                            Session session = null;
656    
657                            try {
658                                    session = openSession();
659    
660                                    StringBundler query = new StringBundler(3);
661    
662                                    query.append(_SQL_SELECT_SHOPPINGCOUPON_WHERE);
663    
664                                    if (code == null) {
665                                            query.append(_FINDER_COLUMN_CODE_CODE_1);
666                                    }
667                                    else {
668                                            if (code.equals(StringPool.BLANK)) {
669                                                    query.append(_FINDER_COLUMN_CODE_CODE_3);
670                                            }
671                                            else {
672                                                    query.append(_FINDER_COLUMN_CODE_CODE_2);
673                                            }
674                                    }
675    
676                                    query.append(ShoppingCouponModelImpl.ORDER_BY_JPQL);
677    
678                                    String sql = query.toString();
679    
680                                    Query q = session.createQuery(sql);
681    
682                                    QueryPos qPos = QueryPos.getInstance(q);
683    
684                                    if (code != null) {
685                                            qPos.add(code);
686                                    }
687    
688                                    List<ShoppingCoupon> list = q.list();
689    
690                                    result = list;
691    
692                                    ShoppingCoupon shoppingCoupon = null;
693    
694                                    if (list.isEmpty()) {
695                                            FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_CODE,
696                                                    finderArgs, list);
697                                    }
698                                    else {
699                                            shoppingCoupon = list.get(0);
700    
701                                            cacheResult(shoppingCoupon);
702    
703                                            if ((shoppingCoupon.getCode() == null) ||
704                                                            !shoppingCoupon.getCode().equals(code)) {
705                                                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_CODE,
706                                                            finderArgs, shoppingCoupon);
707                                            }
708                                    }
709    
710                                    return shoppingCoupon;
711                            }
712                            catch (Exception e) {
713                                    throw processException(e);
714                            }
715                            finally {
716                                    if (result == null) {
717                                            FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_CODE,
718                                                    finderArgs, new ArrayList<ShoppingCoupon>());
719                                    }
720    
721                                    closeSession(session);
722                            }
723                    }
724                    else {
725                            if (result instanceof List<?>) {
726                                    return null;
727                            }
728                            else {
729                                    return (ShoppingCoupon)result;
730                            }
731                    }
732            }
733    
734            public List<ShoppingCoupon> findAll() throws SystemException {
735                    return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
736            }
737    
738            public List<ShoppingCoupon> findAll(int start, int end)
739                    throws SystemException {
740                    return findAll(start, end, null);
741            }
742    
743            public List<ShoppingCoupon> findAll(int start, int end,
744                    OrderByComparator orderByComparator) throws SystemException {
745                    Object[] finderArgs = new Object[] {
746                                    String.valueOf(start), String.valueOf(end),
747                                    String.valueOf(orderByComparator)
748                            };
749    
750                    List<ShoppingCoupon> list = (List<ShoppingCoupon>)FinderCacheUtil.getResult(FINDER_PATH_FIND_ALL,
751                                    finderArgs, this);
752    
753                    if (list == null) {
754                            Session session = null;
755    
756                            try {
757                                    session = openSession();
758    
759                                    StringBundler query = null;
760                                    String sql = null;
761    
762                                    if (orderByComparator != null) {
763                                            query = new StringBundler(2 +
764                                                            (orderByComparator.getOrderByFields().length * 3));
765    
766                                            query.append(_SQL_SELECT_SHOPPINGCOUPON);
767    
768                                            appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
769                                                    orderByComparator);
770    
771                                            sql = query.toString();
772                                    }
773                                    else {
774                                            sql = _SQL_SELECT_SHOPPINGCOUPON.concat(ShoppingCouponModelImpl.ORDER_BY_JPQL);
775                                    }
776    
777                                    Query q = session.createQuery(sql);
778    
779                                    if (orderByComparator == null) {
780                                            list = (List<ShoppingCoupon>)QueryUtil.list(q,
781                                                            getDialect(), start, end, false);
782    
783                                            Collections.sort(list);
784                                    }
785                                    else {
786                                            list = (List<ShoppingCoupon>)QueryUtil.list(q,
787                                                            getDialect(), start, end);
788                                    }
789                            }
790                            catch (Exception e) {
791                                    throw processException(e);
792                            }
793                            finally {
794                                    if (list == null) {
795                                            list = new ArrayList<ShoppingCoupon>();
796                                    }
797    
798                                    cacheResult(list);
799    
800                                    FinderCacheUtil.putResult(FINDER_PATH_FIND_ALL, finderArgs, list);
801    
802                                    closeSession(session);
803                            }
804                    }
805    
806                    return list;
807            }
808    
809            public void removeByGroupId(long groupId) throws SystemException {
810                    for (ShoppingCoupon shoppingCoupon : findByGroupId(groupId)) {
811                            remove(shoppingCoupon);
812                    }
813            }
814    
815            public void removeByCode(String code)
816                    throws NoSuchCouponException, SystemException {
817                    ShoppingCoupon shoppingCoupon = findByCode(code);
818    
819                    remove(shoppingCoupon);
820            }
821    
822            public void removeAll() throws SystemException {
823                    for (ShoppingCoupon shoppingCoupon : findAll()) {
824                            remove(shoppingCoupon);
825                    }
826            }
827    
828            public int countByGroupId(long groupId) throws SystemException {
829                    Object[] finderArgs = new Object[] { groupId };
830    
831                    Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_GROUPID,
832                                    finderArgs, this);
833    
834                    if (count == null) {
835                            Session session = null;
836    
837                            try {
838                                    session = openSession();
839    
840                                    StringBundler query = new StringBundler(2);
841    
842                                    query.append(_SQL_COUNT_SHOPPINGCOUPON_WHERE);
843    
844                                    query.append(_FINDER_COLUMN_GROUPID_GROUPID_2);
845    
846                                    String sql = query.toString();
847    
848                                    Query q = session.createQuery(sql);
849    
850                                    QueryPos qPos = QueryPos.getInstance(q);
851    
852                                    qPos.add(groupId);
853    
854                                    count = (Long)q.uniqueResult();
855                            }
856                            catch (Exception e) {
857                                    throw processException(e);
858                            }
859                            finally {
860                                    if (count == null) {
861                                            count = Long.valueOf(0);
862                                    }
863    
864                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_GROUPID,
865                                            finderArgs, count);
866    
867                                    closeSession(session);
868                            }
869                    }
870    
871                    return count.intValue();
872            }
873    
874            public int countByCode(String code) throws SystemException {
875                    Object[] finderArgs = new Object[] { code };
876    
877                    Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_CODE,
878                                    finderArgs, this);
879    
880                    if (count == null) {
881                            Session session = null;
882    
883                            try {
884                                    session = openSession();
885    
886                                    StringBundler query = new StringBundler(2);
887    
888                                    query.append(_SQL_COUNT_SHOPPINGCOUPON_WHERE);
889    
890                                    if (code == null) {
891                                            query.append(_FINDER_COLUMN_CODE_CODE_1);
892                                    }
893                                    else {
894                                            if (code.equals(StringPool.BLANK)) {
895                                                    query.append(_FINDER_COLUMN_CODE_CODE_3);
896                                            }
897                                            else {
898                                                    query.append(_FINDER_COLUMN_CODE_CODE_2);
899                                            }
900                                    }
901    
902                                    String sql = query.toString();
903    
904                                    Query q = session.createQuery(sql);
905    
906                                    QueryPos qPos = QueryPos.getInstance(q);
907    
908                                    if (code != null) {
909                                            qPos.add(code);
910                                    }
911    
912                                    count = (Long)q.uniqueResult();
913                            }
914                            catch (Exception e) {
915                                    throw processException(e);
916                            }
917                            finally {
918                                    if (count == null) {
919                                            count = Long.valueOf(0);
920                                    }
921    
922                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_CODE,
923                                            finderArgs, count);
924    
925                                    closeSession(session);
926                            }
927                    }
928    
929                    return count.intValue();
930            }
931    
932            public int countAll() throws SystemException {
933                    Object[] finderArgs = new Object[0];
934    
935                    Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
936                                    finderArgs, this);
937    
938                    if (count == null) {
939                            Session session = null;
940    
941                            try {
942                                    session = openSession();
943    
944                                    Query q = session.createQuery(_SQL_COUNT_SHOPPINGCOUPON);
945    
946                                    count = (Long)q.uniqueResult();
947                            }
948                            catch (Exception e) {
949                                    throw processException(e);
950                            }
951                            finally {
952                                    if (count == null) {
953                                            count = Long.valueOf(0);
954                                    }
955    
956                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL, finderArgs,
957                                            count);
958    
959                                    closeSession(session);
960                            }
961                    }
962    
963                    return count.intValue();
964            }
965    
966            public void afterPropertiesSet() {
967                    String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
968                                            com.liferay.portal.util.PropsUtil.get(
969                                                    "value.object.listener.com.liferay.portlet.shopping.model.ShoppingCoupon")));
970    
971                    if (listenerClassNames.length > 0) {
972                            try {
973                                    List<ModelListener<ShoppingCoupon>> listenersList = new ArrayList<ModelListener<ShoppingCoupon>>();
974    
975                                    for (String listenerClassName : listenerClassNames) {
976                                            listenersList.add((ModelListener<ShoppingCoupon>)InstanceFactory.newInstance(
977                                                            listenerClassName));
978                                    }
979    
980                                    listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
981                            }
982                            catch (Exception e) {
983                                    _log.error(e);
984                            }
985                    }
986            }
987    
988            @BeanReference(type = ShoppingCartPersistence.class)
989            protected ShoppingCartPersistence shoppingCartPersistence;
990            @BeanReference(type = ShoppingCategoryPersistence.class)
991            protected ShoppingCategoryPersistence shoppingCategoryPersistence;
992            @BeanReference(type = ShoppingCouponPersistence.class)
993            protected ShoppingCouponPersistence shoppingCouponPersistence;
994            @BeanReference(type = ShoppingItemPersistence.class)
995            protected ShoppingItemPersistence shoppingItemPersistence;
996            @BeanReference(type = ShoppingItemFieldPersistence.class)
997            protected ShoppingItemFieldPersistence shoppingItemFieldPersistence;
998            @BeanReference(type = ShoppingItemPricePersistence.class)
999            protected ShoppingItemPricePersistence shoppingItemPricePersistence;
1000            @BeanReference(type = ShoppingOrderPersistence.class)
1001            protected ShoppingOrderPersistence shoppingOrderPersistence;
1002            @BeanReference(type = ShoppingOrderItemPersistence.class)
1003            protected ShoppingOrderItemPersistence shoppingOrderItemPersistence;
1004            @BeanReference(type = ResourcePersistence.class)
1005            protected ResourcePersistence resourcePersistence;
1006            @BeanReference(type = UserPersistence.class)
1007            protected UserPersistence userPersistence;
1008            private static final String _SQL_SELECT_SHOPPINGCOUPON = "SELECT shoppingCoupon FROM ShoppingCoupon shoppingCoupon";
1009            private static final String _SQL_SELECT_SHOPPINGCOUPON_WHERE = "SELECT shoppingCoupon FROM ShoppingCoupon shoppingCoupon WHERE ";
1010            private static final String _SQL_COUNT_SHOPPINGCOUPON = "SELECT COUNT(shoppingCoupon) FROM ShoppingCoupon shoppingCoupon";
1011            private static final String _SQL_COUNT_SHOPPINGCOUPON_WHERE = "SELECT COUNT(shoppingCoupon) FROM ShoppingCoupon shoppingCoupon WHERE ";
1012            private static final String _FINDER_COLUMN_GROUPID_GROUPID_2 = "shoppingCoupon.groupId = ?";
1013            private static final String _FINDER_COLUMN_CODE_CODE_1 = "shoppingCoupon.code IS NULL";
1014            private static final String _FINDER_COLUMN_CODE_CODE_2 = "shoppingCoupon.code = ?";
1015            private static final String _FINDER_COLUMN_CODE_CODE_3 = "(shoppingCoupon.code IS NULL OR shoppingCoupon.code = ?)";
1016            private static final String _ORDER_BY_ENTITY_ALIAS = "shoppingCoupon.";
1017            private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No ShoppingCoupon exists with the primary key ";
1018            private static final String _NO_SUCH_ENTITY_WITH_KEY = "No ShoppingCoupon exists with the key {";
1019            private static Log _log = LogFactoryUtil.getLog(ShoppingCouponPersistenceImpl.class);
1020    }