1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.shopping.service.persistence;
24  
25  import com.liferay.portal.SystemException;
26  import com.liferay.portal.kernel.annotation.BeanReference;
27  import com.liferay.portal.kernel.cache.CacheRegistry;
28  import com.liferay.portal.kernel.dao.orm.DynamicQuery;
29  import com.liferay.portal.kernel.dao.orm.EntityCacheUtil;
30  import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
31  import com.liferay.portal.kernel.dao.orm.FinderPath;
32  import com.liferay.portal.kernel.dao.orm.Query;
33  import com.liferay.portal.kernel.dao.orm.QueryPos;
34  import com.liferay.portal.kernel.dao.orm.QueryUtil;
35  import com.liferay.portal.kernel.dao.orm.Session;
36  import com.liferay.portal.kernel.log.Log;
37  import com.liferay.portal.kernel.log.LogFactoryUtil;
38  import com.liferay.portal.kernel.util.GetterUtil;
39  import com.liferay.portal.kernel.util.OrderByComparator;
40  import com.liferay.portal.kernel.util.StringPool;
41  import com.liferay.portal.kernel.util.StringUtil;
42  import com.liferay.portal.model.ModelListener;
43  import com.liferay.portal.service.persistence.BatchSessionUtil;
44  import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
45  
46  import com.liferay.portlet.shopping.NoSuchCartException;
47  import com.liferay.portlet.shopping.model.ShoppingCart;
48  import com.liferay.portlet.shopping.model.impl.ShoppingCartImpl;
49  import com.liferay.portlet.shopping.model.impl.ShoppingCartModelImpl;
50  
51  import java.util.ArrayList;
52  import java.util.Collections;
53  import java.util.List;
54  
55  /**
56   * <a href="ShoppingCartPersistenceImpl.java.html"><b><i>View Source</i></b></a>
57   *
58   * <p>
59   * ServiceBuilder generated this class. Modifications in this class will be
60   * overwritten the next time is generated.
61   * </p>
62   *
63   * @author    Brian Wing Shun Chan
64   * @see       ShoppingCartPersistence
65   * @see       ShoppingCartUtil
66   * @generated
67   */
68  public class ShoppingCartPersistenceImpl extends BasePersistenceImpl
69      implements ShoppingCartPersistence {
70      public static final String FINDER_CLASS_NAME_ENTITY = ShoppingCartImpl.class.getName();
71      public static final String FINDER_CLASS_NAME_LIST = FINDER_CLASS_NAME_ENTITY +
72          ".List";
73      public static final FinderPath FINDER_PATH_FIND_BY_GROUPID = new FinderPath(ShoppingCartModelImpl.ENTITY_CACHE_ENABLED,
74              ShoppingCartModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
75              "findByGroupId", new String[] { Long.class.getName() });
76      public static final FinderPath FINDER_PATH_FIND_BY_OBC_GROUPID = new FinderPath(ShoppingCartModelImpl.ENTITY_CACHE_ENABLED,
77              ShoppingCartModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
78              "findByGroupId",
79              new String[] {
80                  Long.class.getName(),
81                  
82              "java.lang.Integer", "java.lang.Integer",
83                  "com.liferay.portal.kernel.util.OrderByComparator"
84              });
85      public static final FinderPath FINDER_PATH_COUNT_BY_GROUPID = new FinderPath(ShoppingCartModelImpl.ENTITY_CACHE_ENABLED,
86              ShoppingCartModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
87              "countByGroupId", new String[] { Long.class.getName() });
88      public static final FinderPath FINDER_PATH_FIND_BY_USERID = new FinderPath(ShoppingCartModelImpl.ENTITY_CACHE_ENABLED,
89              ShoppingCartModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
90              "findByUserId", new String[] { Long.class.getName() });
91      public static final FinderPath FINDER_PATH_FIND_BY_OBC_USERID = new FinderPath(ShoppingCartModelImpl.ENTITY_CACHE_ENABLED,
92              ShoppingCartModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
93              "findByUserId",
94              new String[] {
95                  Long.class.getName(),
96                  
97              "java.lang.Integer", "java.lang.Integer",
98                  "com.liferay.portal.kernel.util.OrderByComparator"
99              });
100     public static final FinderPath FINDER_PATH_COUNT_BY_USERID = new FinderPath(ShoppingCartModelImpl.ENTITY_CACHE_ENABLED,
101             ShoppingCartModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
102             "countByUserId", new String[] { Long.class.getName() });
103     public static final FinderPath FINDER_PATH_FETCH_BY_G_U = new FinderPath(ShoppingCartModelImpl.ENTITY_CACHE_ENABLED,
104             ShoppingCartModelImpl.FINDER_CACHE_ENABLED,
105             FINDER_CLASS_NAME_ENTITY, "fetchByG_U",
106             new String[] { Long.class.getName(), Long.class.getName() });
107     public static final FinderPath FINDER_PATH_COUNT_BY_G_U = new FinderPath(ShoppingCartModelImpl.ENTITY_CACHE_ENABLED,
108             ShoppingCartModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
109             "countByG_U",
110             new String[] { Long.class.getName(), Long.class.getName() });
111     public static final FinderPath FINDER_PATH_FIND_ALL = new FinderPath(ShoppingCartModelImpl.ENTITY_CACHE_ENABLED,
112             ShoppingCartModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
113             "findAll", new String[0]);
114     public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(ShoppingCartModelImpl.ENTITY_CACHE_ENABLED,
115             ShoppingCartModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
116             "countAll", new String[0]);
117 
118     public void cacheResult(ShoppingCart shoppingCart) {
119         EntityCacheUtil.putResult(ShoppingCartModelImpl.ENTITY_CACHE_ENABLED,
120             ShoppingCartImpl.class, shoppingCart.getPrimaryKey(), shoppingCart);
121 
122         FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_G_U,
123             new Object[] {
124                 new Long(shoppingCart.getGroupId()),
125                 new Long(shoppingCart.getUserId())
126             }, shoppingCart);
127     }
128 
129     public void cacheResult(List<ShoppingCart> shoppingCarts) {
130         for (ShoppingCart shoppingCart : shoppingCarts) {
131             if (EntityCacheUtil.getResult(
132                         ShoppingCartModelImpl.ENTITY_CACHE_ENABLED,
133                         ShoppingCartImpl.class, shoppingCart.getPrimaryKey(),
134                         this) == null) {
135                 cacheResult(shoppingCart);
136             }
137         }
138     }
139 
140     public void clearCache() {
141         CacheRegistry.clear(ShoppingCartImpl.class.getName());
142         EntityCacheUtil.clearCache(ShoppingCartImpl.class.getName());
143         FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
144         FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
145     }
146 
147     public ShoppingCart create(long cartId) {
148         ShoppingCart shoppingCart = new ShoppingCartImpl();
149 
150         shoppingCart.setNew(true);
151         shoppingCart.setPrimaryKey(cartId);
152 
153         return shoppingCart;
154     }
155 
156     public ShoppingCart remove(long cartId)
157         throws NoSuchCartException, SystemException {
158         Session session = null;
159 
160         try {
161             session = openSession();
162 
163             ShoppingCart shoppingCart = (ShoppingCart)session.get(ShoppingCartImpl.class,
164                     new Long(cartId));
165 
166             if (shoppingCart == null) {
167                 if (_log.isWarnEnabled()) {
168                     _log.warn("No ShoppingCart exists with the primary key " +
169                         cartId);
170                 }
171 
172                 throw new NoSuchCartException(
173                     "No ShoppingCart exists with the primary key " + cartId);
174             }
175 
176             return remove(shoppingCart);
177         }
178         catch (NoSuchCartException nsee) {
179             throw nsee;
180         }
181         catch (Exception e) {
182             throw processException(e);
183         }
184         finally {
185             closeSession(session);
186         }
187     }
188 
189     public ShoppingCart remove(ShoppingCart shoppingCart)
190         throws SystemException {
191         for (ModelListener<ShoppingCart> listener : listeners) {
192             listener.onBeforeRemove(shoppingCart);
193         }
194 
195         shoppingCart = removeImpl(shoppingCart);
196 
197         for (ModelListener<ShoppingCart> listener : listeners) {
198             listener.onAfterRemove(shoppingCart);
199         }
200 
201         return shoppingCart;
202     }
203 
204     protected ShoppingCart removeImpl(ShoppingCart shoppingCart)
205         throws SystemException {
206         shoppingCart = toUnwrappedModel(shoppingCart);
207 
208         Session session = null;
209 
210         try {
211             session = openSession();
212 
213             if (shoppingCart.isCachedModel() || BatchSessionUtil.isEnabled()) {
214                 Object staleObject = session.get(ShoppingCartImpl.class,
215                         shoppingCart.getPrimaryKeyObj());
216 
217                 if (staleObject != null) {
218                     session.evict(staleObject);
219                 }
220             }
221 
222             session.delete(shoppingCart);
223 
224             session.flush();
225         }
226         catch (Exception e) {
227             throw processException(e);
228         }
229         finally {
230             closeSession(session);
231         }
232 
233         FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
234 
235         ShoppingCartModelImpl shoppingCartModelImpl = (ShoppingCartModelImpl)shoppingCart;
236 
237         FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_G_U,
238             new Object[] {
239                 new Long(shoppingCartModelImpl.getOriginalGroupId()),
240                 new Long(shoppingCartModelImpl.getOriginalUserId())
241             });
242 
243         EntityCacheUtil.removeResult(ShoppingCartModelImpl.ENTITY_CACHE_ENABLED,
244             ShoppingCartImpl.class, shoppingCart.getPrimaryKey());
245 
246         return shoppingCart;
247     }
248 
249     /**
250      * @deprecated Use {@link #update(ShoppingCart, boolean merge)}.
251      */
252     public ShoppingCart update(ShoppingCart shoppingCart)
253         throws SystemException {
254         if (_log.isWarnEnabled()) {
255             _log.warn(
256                 "Using the deprecated update(ShoppingCart shoppingCart) method. Use update(ShoppingCart shoppingCart, boolean merge) instead.");
257         }
258 
259         return update(shoppingCart, false);
260     }
261 
262     /**
263      * Add, update, or merge, the entity. This method also calls the model
264      * listeners to trigger the proper events associated with adding, deleting,
265      * or updating an entity.
266      *
267      * @param  shoppingCart the entity to add, update, or merge
268      * @param  merge boolean value for whether to merge the entity. The default
269      *         value is false. Setting merge to true is more expensive and
270      *         should only be true when shoppingCart is transient. See
271      *         LEP-5473 for a detailed discussion of this method.
272      * @return the entity that was added, updated, or merged
273      */
274     public ShoppingCart update(ShoppingCart shoppingCart, boolean merge)
275         throws SystemException {
276         boolean isNew = shoppingCart.isNew();
277 
278         for (ModelListener<ShoppingCart> listener : listeners) {
279             if (isNew) {
280                 listener.onBeforeCreate(shoppingCart);
281             }
282             else {
283                 listener.onBeforeUpdate(shoppingCart);
284             }
285         }
286 
287         shoppingCart = updateImpl(shoppingCart, merge);
288 
289         for (ModelListener<ShoppingCart> listener : listeners) {
290             if (isNew) {
291                 listener.onAfterCreate(shoppingCart);
292             }
293             else {
294                 listener.onAfterUpdate(shoppingCart);
295             }
296         }
297 
298         return shoppingCart;
299     }
300 
301     public ShoppingCart updateImpl(
302         com.liferay.portlet.shopping.model.ShoppingCart shoppingCart,
303         boolean merge) throws SystemException {
304         shoppingCart = toUnwrappedModel(shoppingCart);
305 
306         boolean isNew = shoppingCart.isNew();
307 
308         ShoppingCartModelImpl shoppingCartModelImpl = (ShoppingCartModelImpl)shoppingCart;
309 
310         Session session = null;
311 
312         try {
313             session = openSession();
314 
315             BatchSessionUtil.update(session, shoppingCart, merge);
316 
317             shoppingCart.setNew(false);
318         }
319         catch (Exception e) {
320             throw processException(e);
321         }
322         finally {
323             closeSession(session);
324         }
325 
326         FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
327 
328         EntityCacheUtil.putResult(ShoppingCartModelImpl.ENTITY_CACHE_ENABLED,
329             ShoppingCartImpl.class, shoppingCart.getPrimaryKey(), shoppingCart);
330 
331         if (!isNew &&
332                 ((shoppingCart.getGroupId() != shoppingCartModelImpl.getOriginalGroupId()) ||
333                 (shoppingCart.getUserId() != shoppingCartModelImpl.getOriginalUserId()))) {
334             FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_G_U,
335                 new Object[] {
336                     new Long(shoppingCartModelImpl.getOriginalGroupId()),
337                     new Long(shoppingCartModelImpl.getOriginalUserId())
338                 });
339         }
340 
341         if (isNew ||
342                 ((shoppingCart.getGroupId() != shoppingCartModelImpl.getOriginalGroupId()) ||
343                 (shoppingCart.getUserId() != shoppingCartModelImpl.getOriginalUserId()))) {
344             FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_G_U,
345                 new Object[] {
346                     new Long(shoppingCart.getGroupId()),
347                     new Long(shoppingCart.getUserId())
348                 }, shoppingCart);
349         }
350 
351         return shoppingCart;
352     }
353 
354     protected ShoppingCart toUnwrappedModel(ShoppingCart shoppingCart) {
355         if (shoppingCart instanceof ShoppingCartImpl) {
356             return shoppingCart;
357         }
358 
359         ShoppingCartImpl shoppingCartImpl = new ShoppingCartImpl();
360 
361         shoppingCartImpl.setNew(shoppingCart.isNew());
362         shoppingCartImpl.setPrimaryKey(shoppingCart.getPrimaryKey());
363 
364         shoppingCartImpl.setCartId(shoppingCart.getCartId());
365         shoppingCartImpl.setGroupId(shoppingCart.getGroupId());
366         shoppingCartImpl.setCompanyId(shoppingCart.getCompanyId());
367         shoppingCartImpl.setUserId(shoppingCart.getUserId());
368         shoppingCartImpl.setUserName(shoppingCart.getUserName());
369         shoppingCartImpl.setCreateDate(shoppingCart.getCreateDate());
370         shoppingCartImpl.setModifiedDate(shoppingCart.getModifiedDate());
371         shoppingCartImpl.setItemIds(shoppingCart.getItemIds());
372         shoppingCartImpl.setCouponCodes(shoppingCart.getCouponCodes());
373         shoppingCartImpl.setAltShipping(shoppingCart.getAltShipping());
374         shoppingCartImpl.setInsure(shoppingCart.isInsure());
375 
376         return shoppingCartImpl;
377     }
378 
379     public ShoppingCart findByPrimaryKey(long cartId)
380         throws NoSuchCartException, SystemException {
381         ShoppingCart shoppingCart = fetchByPrimaryKey(cartId);
382 
383         if (shoppingCart == null) {
384             if (_log.isWarnEnabled()) {
385                 _log.warn("No ShoppingCart exists with the primary key " +
386                     cartId);
387             }
388 
389             throw new NoSuchCartException(
390                 "No ShoppingCart exists with the primary key " + cartId);
391         }
392 
393         return shoppingCart;
394     }
395 
396     public ShoppingCart fetchByPrimaryKey(long cartId)
397         throws SystemException {
398         ShoppingCart shoppingCart = (ShoppingCart)EntityCacheUtil.getResult(ShoppingCartModelImpl.ENTITY_CACHE_ENABLED,
399                 ShoppingCartImpl.class, cartId, this);
400 
401         if (shoppingCart == null) {
402             Session session = null;
403 
404             try {
405                 session = openSession();
406 
407                 shoppingCart = (ShoppingCart)session.get(ShoppingCartImpl.class,
408                         new Long(cartId));
409             }
410             catch (Exception e) {
411                 throw processException(e);
412             }
413             finally {
414                 if (shoppingCart != null) {
415                     cacheResult(shoppingCart);
416                 }
417 
418                 closeSession(session);
419             }
420         }
421 
422         return shoppingCart;
423     }
424 
425     public List<ShoppingCart> findByGroupId(long groupId)
426         throws SystemException {
427         Object[] finderArgs = new Object[] { new Long(groupId) };
428 
429         List<ShoppingCart> list = (List<ShoppingCart>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_GROUPID,
430                 finderArgs, this);
431 
432         if (list == null) {
433             Session session = null;
434 
435             try {
436                 session = openSession();
437 
438                 StringBuilder query = new StringBuilder();
439 
440                 query.append(
441                     "SELECT shoppingCart FROM ShoppingCart shoppingCart WHERE ");
442 
443                 query.append("shoppingCart.groupId = ?");
444 
445                 query.append(" ");
446 
447                 Query q = session.createQuery(query.toString());
448 
449                 QueryPos qPos = QueryPos.getInstance(q);
450 
451                 qPos.add(groupId);
452 
453                 list = q.list();
454             }
455             catch (Exception e) {
456                 throw processException(e);
457             }
458             finally {
459                 if (list == null) {
460                     list = new ArrayList<ShoppingCart>();
461                 }
462 
463                 cacheResult(list);
464 
465                 FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_GROUPID,
466                     finderArgs, list);
467 
468                 closeSession(session);
469             }
470         }
471 
472         return list;
473     }
474 
475     public List<ShoppingCart> findByGroupId(long groupId, int start, int end)
476         throws SystemException {
477         return findByGroupId(groupId, start, end, null);
478     }
479 
480     public List<ShoppingCart> findByGroupId(long groupId, int start, int end,
481         OrderByComparator obc) throws SystemException {
482         Object[] finderArgs = new Object[] {
483                 new Long(groupId),
484                 
485                 String.valueOf(start), String.valueOf(end), String.valueOf(obc)
486             };
487 
488         List<ShoppingCart> list = (List<ShoppingCart>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_OBC_GROUPID,
489                 finderArgs, this);
490 
491         if (list == null) {
492             Session session = null;
493 
494             try {
495                 session = openSession();
496 
497                 StringBuilder query = new StringBuilder();
498 
499                 query.append(
500                     "SELECT shoppingCart FROM ShoppingCart shoppingCart WHERE ");
501 
502                 query.append("shoppingCart.groupId = ?");
503 
504                 query.append(" ");
505 
506                 if (obc != null) {
507                     query.append("ORDER BY ");
508 
509                     String[] orderByFields = obc.getOrderByFields();
510 
511                     for (int i = 0; i < orderByFields.length; i++) {
512                         query.append("shoppingCart.");
513                         query.append(orderByFields[i]);
514 
515                         if (obc.isAscending()) {
516                             query.append(" ASC");
517                         }
518                         else {
519                             query.append(" DESC");
520                         }
521 
522                         if ((i + 1) < orderByFields.length) {
523                             query.append(", ");
524                         }
525                     }
526                 }
527 
528                 Query q = session.createQuery(query.toString());
529 
530                 QueryPos qPos = QueryPos.getInstance(q);
531 
532                 qPos.add(groupId);
533 
534                 list = (List<ShoppingCart>)QueryUtil.list(q, getDialect(),
535                         start, end);
536             }
537             catch (Exception e) {
538                 throw processException(e);
539             }
540             finally {
541                 if (list == null) {
542                     list = new ArrayList<ShoppingCart>();
543                 }
544 
545                 cacheResult(list);
546 
547                 FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_OBC_GROUPID,
548                     finderArgs, list);
549 
550                 closeSession(session);
551             }
552         }
553 
554         return list;
555     }
556 
557     public ShoppingCart findByGroupId_First(long groupId, OrderByComparator obc)
558         throws NoSuchCartException, SystemException {
559         List<ShoppingCart> list = findByGroupId(groupId, 0, 1, obc);
560 
561         if (list.isEmpty()) {
562             StringBuilder msg = new StringBuilder();
563 
564             msg.append("No ShoppingCart exists with the key {");
565 
566             msg.append("groupId=" + groupId);
567 
568             msg.append(StringPool.CLOSE_CURLY_BRACE);
569 
570             throw new NoSuchCartException(msg.toString());
571         }
572         else {
573             return list.get(0);
574         }
575     }
576 
577     public ShoppingCart findByGroupId_Last(long groupId, OrderByComparator obc)
578         throws NoSuchCartException, SystemException {
579         int count = countByGroupId(groupId);
580 
581         List<ShoppingCart> list = findByGroupId(groupId, count - 1, count, obc);
582 
583         if (list.isEmpty()) {
584             StringBuilder msg = new StringBuilder();
585 
586             msg.append("No ShoppingCart exists with the key {");
587 
588             msg.append("groupId=" + groupId);
589 
590             msg.append(StringPool.CLOSE_CURLY_BRACE);
591 
592             throw new NoSuchCartException(msg.toString());
593         }
594         else {
595             return list.get(0);
596         }
597     }
598 
599     public ShoppingCart[] findByGroupId_PrevAndNext(long cartId, long groupId,
600         OrderByComparator obc) throws NoSuchCartException, SystemException {
601         ShoppingCart shoppingCart = findByPrimaryKey(cartId);
602 
603         int count = countByGroupId(groupId);
604 
605         Session session = null;
606 
607         try {
608             session = openSession();
609 
610             StringBuilder query = new StringBuilder();
611 
612             query.append(
613                 "SELECT shoppingCart FROM ShoppingCart shoppingCart WHERE ");
614 
615             query.append("shoppingCart.groupId = ?");
616 
617             query.append(" ");
618 
619             if (obc != null) {
620                 query.append("ORDER BY ");
621 
622                 String[] orderByFields = obc.getOrderByFields();
623 
624                 for (int i = 0; i < orderByFields.length; i++) {
625                     query.append("shoppingCart.");
626                     query.append(orderByFields[i]);
627 
628                     if (obc.isAscending()) {
629                         query.append(" ASC");
630                     }
631                     else {
632                         query.append(" DESC");
633                     }
634 
635                     if ((i + 1) < orderByFields.length) {
636                         query.append(", ");
637                     }
638                 }
639             }
640 
641             Query q = session.createQuery(query.toString());
642 
643             QueryPos qPos = QueryPos.getInstance(q);
644 
645             qPos.add(groupId);
646 
647             Object[] objArray = QueryUtil.getPrevAndNext(q, count, obc,
648                     shoppingCart);
649 
650             ShoppingCart[] array = new ShoppingCartImpl[3];
651 
652             array[0] = (ShoppingCart)objArray[0];
653             array[1] = (ShoppingCart)objArray[1];
654             array[2] = (ShoppingCart)objArray[2];
655 
656             return array;
657         }
658         catch (Exception e) {
659             throw processException(e);
660         }
661         finally {
662             closeSession(session);
663         }
664     }
665 
666     public List<ShoppingCart> findByUserId(long userId)
667         throws SystemException {
668         Object[] finderArgs = new Object[] { new Long(userId) };
669 
670         List<ShoppingCart> list = (List<ShoppingCart>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_USERID,
671                 finderArgs, this);
672 
673         if (list == null) {
674             Session session = null;
675 
676             try {
677                 session = openSession();
678 
679                 StringBuilder query = new StringBuilder();
680 
681                 query.append(
682                     "SELECT shoppingCart FROM ShoppingCart shoppingCart WHERE ");
683 
684                 query.append("shoppingCart.userId = ?");
685 
686                 query.append(" ");
687 
688                 Query q = session.createQuery(query.toString());
689 
690                 QueryPos qPos = QueryPos.getInstance(q);
691 
692                 qPos.add(userId);
693 
694                 list = q.list();
695             }
696             catch (Exception e) {
697                 throw processException(e);
698             }
699             finally {
700                 if (list == null) {
701                     list = new ArrayList<ShoppingCart>();
702                 }
703 
704                 cacheResult(list);
705 
706                 FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_USERID,
707                     finderArgs, list);
708 
709                 closeSession(session);
710             }
711         }
712 
713         return list;
714     }
715 
716     public List<ShoppingCart> findByUserId(long userId, int start, int end)
717         throws SystemException {
718         return findByUserId(userId, start, end, null);
719     }
720 
721     public List<ShoppingCart> findByUserId(long userId, int start, int end,
722         OrderByComparator obc) throws SystemException {
723         Object[] finderArgs = new Object[] {
724                 new Long(userId),
725                 
726                 String.valueOf(start), String.valueOf(end), String.valueOf(obc)
727             };
728 
729         List<ShoppingCart> list = (List<ShoppingCart>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_OBC_USERID,
730                 finderArgs, this);
731 
732         if (list == null) {
733             Session session = null;
734 
735             try {
736                 session = openSession();
737 
738                 StringBuilder query = new StringBuilder();
739 
740                 query.append(
741                     "SELECT shoppingCart FROM ShoppingCart shoppingCart WHERE ");
742 
743                 query.append("shoppingCart.userId = ?");
744 
745                 query.append(" ");
746 
747                 if (obc != null) {
748                     query.append("ORDER BY ");
749 
750                     String[] orderByFields = obc.getOrderByFields();
751 
752                     for (int i = 0; i < orderByFields.length; i++) {
753                         query.append("shoppingCart.");
754                         query.append(orderByFields[i]);
755 
756                         if (obc.isAscending()) {
757                             query.append(" ASC");
758                         }
759                         else {
760                             query.append(" DESC");
761                         }
762 
763                         if ((i + 1) < orderByFields.length) {
764                             query.append(", ");
765                         }
766                     }
767                 }
768 
769                 Query q = session.createQuery(query.toString());
770 
771                 QueryPos qPos = QueryPos.getInstance(q);
772 
773                 qPos.add(userId);
774 
775                 list = (List<ShoppingCart>)QueryUtil.list(q, getDialect(),
776                         start, end);
777             }
778             catch (Exception e) {
779                 throw processException(e);
780             }
781             finally {
782                 if (list == null) {
783                     list = new ArrayList<ShoppingCart>();
784                 }
785 
786                 cacheResult(list);
787 
788                 FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_OBC_USERID,
789                     finderArgs, list);
790 
791                 closeSession(session);
792             }
793         }
794 
795         return list;
796     }
797 
798     public ShoppingCart findByUserId_First(long userId, OrderByComparator obc)
799         throws NoSuchCartException, SystemException {
800         List<ShoppingCart> list = findByUserId(userId, 0, 1, obc);
801 
802         if (list.isEmpty()) {
803             StringBuilder msg = new StringBuilder();
804 
805             msg.append("No ShoppingCart exists with the key {");
806 
807             msg.append("userId=" + userId);
808 
809             msg.append(StringPool.CLOSE_CURLY_BRACE);
810 
811             throw new NoSuchCartException(msg.toString());
812         }
813         else {
814             return list.get(0);
815         }
816     }
817 
818     public ShoppingCart findByUserId_Last(long userId, OrderByComparator obc)
819         throws NoSuchCartException, SystemException {
820         int count = countByUserId(userId);
821 
822         List<ShoppingCart> list = findByUserId(userId, count - 1, count, obc);
823 
824         if (list.isEmpty()) {
825             StringBuilder msg = new StringBuilder();
826 
827             msg.append("No ShoppingCart exists with the key {");
828 
829             msg.append("userId=" + userId);
830 
831             msg.append(StringPool.CLOSE_CURLY_BRACE);
832 
833             throw new NoSuchCartException(msg.toString());
834         }
835         else {
836             return list.get(0);
837         }
838     }
839 
840     public ShoppingCart[] findByUserId_PrevAndNext(long cartId, long userId,
841         OrderByComparator obc) throws NoSuchCartException, SystemException {
842         ShoppingCart shoppingCart = findByPrimaryKey(cartId);
843 
844         int count = countByUserId(userId);
845 
846         Session session = null;
847 
848         try {
849             session = openSession();
850 
851             StringBuilder query = new StringBuilder();
852 
853             query.append(
854                 "SELECT shoppingCart FROM ShoppingCart shoppingCart WHERE ");
855 
856             query.append("shoppingCart.userId = ?");
857 
858             query.append(" ");
859 
860             if (obc != null) {
861                 query.append("ORDER BY ");
862 
863                 String[] orderByFields = obc.getOrderByFields();
864 
865                 for (int i = 0; i < orderByFields.length; i++) {
866                     query.append("shoppingCart.");
867                     query.append(orderByFields[i]);
868 
869                     if (obc.isAscending()) {
870                         query.append(" ASC");
871                     }
872                     else {
873                         query.append(" DESC");
874                     }
875 
876                     if ((i + 1) < orderByFields.length) {
877                         query.append(", ");
878                     }
879                 }
880             }
881 
882             Query q = session.createQuery(query.toString());
883 
884             QueryPos qPos = QueryPos.getInstance(q);
885 
886             qPos.add(userId);
887 
888             Object[] objArray = QueryUtil.getPrevAndNext(q, count, obc,
889                     shoppingCart);
890 
891             ShoppingCart[] array = new ShoppingCartImpl[3];
892 
893             array[0] = (ShoppingCart)objArray[0];
894             array[1] = (ShoppingCart)objArray[1];
895             array[2] = (ShoppingCart)objArray[2];
896 
897             return array;
898         }
899         catch (Exception e) {
900             throw processException(e);
901         }
902         finally {
903             closeSession(session);
904         }
905     }
906 
907     public ShoppingCart findByG_U(long groupId, long userId)
908         throws NoSuchCartException, SystemException {
909         ShoppingCart shoppingCart = fetchByG_U(groupId, userId);
910 
911         if (shoppingCart == null) {
912             StringBuilder msg = new StringBuilder();
913 
914             msg.append("No ShoppingCart exists with the key {");
915 
916             msg.append("groupId=" + groupId);
917 
918             msg.append(", ");
919             msg.append("userId=" + userId);
920 
921             msg.append(StringPool.CLOSE_CURLY_BRACE);
922 
923             if (_log.isWarnEnabled()) {
924                 _log.warn(msg.toString());
925             }
926 
927             throw new NoSuchCartException(msg.toString());
928         }
929 
930         return shoppingCart;
931     }
932 
933     public ShoppingCart fetchByG_U(long groupId, long userId)
934         throws SystemException {
935         return fetchByG_U(groupId, userId, true);
936     }
937 
938     public ShoppingCart fetchByG_U(long groupId, long userId,
939         boolean retrieveFromCache) throws SystemException {
940         Object[] finderArgs = new Object[] { new Long(groupId), new Long(userId) };
941 
942         Object result = null;
943 
944         if (retrieveFromCache) {
945             result = FinderCacheUtil.getResult(FINDER_PATH_FETCH_BY_G_U,
946                     finderArgs, this);
947         }
948 
949         if (result == null) {
950             Session session = null;
951 
952             try {
953                 session = openSession();
954 
955                 StringBuilder query = new StringBuilder();
956 
957                 query.append(
958                     "SELECT shoppingCart FROM ShoppingCart shoppingCart WHERE ");
959 
960                 query.append("shoppingCart.groupId = ?");
961 
962                 query.append(" AND ");
963 
964                 query.append("shoppingCart.userId = ?");
965 
966                 query.append(" ");
967 
968                 Query q = session.createQuery(query.toString());
969 
970                 QueryPos qPos = QueryPos.getInstance(q);
971 
972                 qPos.add(groupId);
973 
974                 qPos.add(userId);
975 
976                 List<ShoppingCart> list = q.list();
977 
978                 result = list;
979 
980                 ShoppingCart shoppingCart = null;
981 
982                 if (list.isEmpty()) {
983                     FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_G_U,
984                         finderArgs, list);
985                 }
986                 else {
987                     shoppingCart = list.get(0);
988 
989                     cacheResult(shoppingCart);
990 
991                     if ((shoppingCart.getGroupId() != groupId) ||
992                             (shoppingCart.getUserId() != userId)) {
993                         FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_G_U,
994                             finderArgs, shoppingCart);
995                     }
996                 }
997 
998                 return shoppingCart;
999             }
1000            catch (Exception e) {
1001                throw processException(e);
1002            }
1003            finally {
1004                if (result == null) {
1005                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_G_U,
1006                        finderArgs, new ArrayList<ShoppingCart>());
1007                }
1008
1009                closeSession(session);
1010            }
1011        }
1012        else {
1013            if (result instanceof List<?>) {
1014                return null;
1015            }
1016            else {
1017                return (ShoppingCart)result;
1018            }
1019        }
1020    }
1021
1022    public List<Object> findWithDynamicQuery(DynamicQuery dynamicQuery)
1023        throws SystemException {
1024        Session session = null;
1025
1026        try {
1027            session = openSession();
1028
1029            dynamicQuery.compile(session);
1030
1031            return dynamicQuery.list();
1032        }
1033        catch (Exception e) {
1034            throw processException(e);
1035        }
1036        finally {
1037            closeSession(session);
1038        }
1039    }
1040
1041    public List<Object> findWithDynamicQuery(DynamicQuery dynamicQuery,
1042        int start, int end) throws SystemException {
1043        Session session = null;
1044
1045        try {
1046            session = openSession();
1047
1048            dynamicQuery.setLimit(start, end);
1049
1050            dynamicQuery.compile(session);
1051
1052            return dynamicQuery.list();
1053        }
1054        catch (Exception e) {
1055            throw processException(e);
1056        }
1057        finally {
1058            closeSession(session);
1059        }
1060    }
1061
1062    public List<ShoppingCart> findAll() throws SystemException {
1063        return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
1064    }
1065
1066    public List<ShoppingCart> findAll(int start, int end)
1067        throws SystemException {
1068        return findAll(start, end, null);
1069    }
1070
1071    public List<ShoppingCart> findAll(int start, int end, OrderByComparator obc)
1072        throws SystemException {
1073        Object[] finderArgs = new Object[] {
1074                String.valueOf(start), String.valueOf(end), String.valueOf(obc)
1075            };
1076
1077        List<ShoppingCart> list = (List<ShoppingCart>)FinderCacheUtil.getResult(FINDER_PATH_FIND_ALL,
1078                finderArgs, this);
1079
1080        if (list == null) {
1081            Session session = null;
1082
1083            try {
1084                session = openSession();
1085
1086                StringBuilder query = new StringBuilder();
1087
1088                query.append(
1089                    "SELECT shoppingCart FROM ShoppingCart shoppingCart ");
1090
1091                if (obc != null) {
1092                    query.append("ORDER BY ");
1093
1094                    String[] orderByFields = obc.getOrderByFields();
1095
1096                    for (int i = 0; i < orderByFields.length; i++) {
1097                        query.append("shoppingCart.");
1098                        query.append(orderByFields[i]);
1099
1100                        if (obc.isAscending()) {
1101                            query.append(" ASC");
1102                        }
1103                        else {
1104                            query.append(" DESC");
1105                        }
1106
1107                        if ((i + 1) < orderByFields.length) {
1108                            query.append(", ");
1109                        }
1110                    }
1111                }
1112
1113                Query q = session.createQuery(query.toString());
1114
1115                if (obc == null) {
1116                    list = (List<ShoppingCart>)QueryUtil.list(q, getDialect(),
1117                            start, end, false);
1118
1119                    Collections.sort(list);
1120                }
1121                else {
1122                    list = (List<ShoppingCart>)QueryUtil.list(q, getDialect(),
1123                            start, end);
1124                }
1125            }
1126            catch (Exception e) {
1127                throw processException(e);
1128            }
1129            finally {
1130                if (list == null) {
1131                    list = new ArrayList<ShoppingCart>();
1132                }
1133
1134                cacheResult(list);
1135
1136                FinderCacheUtil.putResult(FINDER_PATH_FIND_ALL, finderArgs, list);
1137
1138                closeSession(session);
1139            }
1140        }
1141
1142        return list;
1143    }
1144
1145    public void removeByGroupId(long groupId) throws SystemException {
1146        for (ShoppingCart shoppingCart : findByGroupId(groupId)) {
1147            remove(shoppingCart);
1148        }
1149    }
1150
1151    public void removeByUserId(long userId) throws SystemException {
1152        for (ShoppingCart shoppingCart : findByUserId(userId)) {
1153            remove(shoppingCart);
1154        }
1155    }
1156
1157    public void removeByG_U(long groupId, long userId)
1158        throws NoSuchCartException, SystemException {
1159        ShoppingCart shoppingCart = findByG_U(groupId, userId);
1160
1161        remove(shoppingCart);
1162    }
1163
1164    public void removeAll() throws SystemException {
1165        for (ShoppingCart shoppingCart : findAll()) {
1166            remove(shoppingCart);
1167        }
1168    }
1169
1170    public int countByGroupId(long groupId) throws SystemException {
1171        Object[] finderArgs = new Object[] { new Long(groupId) };
1172
1173        Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_GROUPID,
1174                finderArgs, this);
1175
1176        if (count == null) {
1177            Session session = null;
1178
1179            try {
1180                session = openSession();
1181
1182                StringBuilder query = new StringBuilder();
1183
1184                query.append("SELECT COUNT(shoppingCart) ");
1185                query.append("FROM ShoppingCart shoppingCart WHERE ");
1186
1187                query.append("shoppingCart.groupId = ?");
1188
1189                query.append(" ");
1190
1191                Query q = session.createQuery(query.toString());
1192
1193                QueryPos qPos = QueryPos.getInstance(q);
1194
1195                qPos.add(groupId);
1196
1197                count = (Long)q.uniqueResult();
1198            }
1199            catch (Exception e) {
1200                throw processException(e);
1201            }
1202            finally {
1203                if (count == null) {
1204                    count = Long.valueOf(0);
1205                }
1206
1207                FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_GROUPID,
1208                    finderArgs, count);
1209
1210                closeSession(session);
1211            }
1212        }
1213
1214        return count.intValue();
1215    }
1216
1217    public int countByUserId(long userId) throws SystemException {
1218        Object[] finderArgs = new Object[] { new Long(userId) };
1219
1220        Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_USERID,
1221                finderArgs, this);
1222
1223        if (count == null) {
1224            Session session = null;
1225
1226            try {
1227                session = openSession();
1228
1229                StringBuilder query = new StringBuilder();
1230
1231                query.append("SELECT COUNT(shoppingCart) ");
1232                query.append("FROM ShoppingCart shoppingCart WHERE ");
1233
1234                query.append("shoppingCart.userId = ?");
1235
1236                query.append(" ");
1237
1238                Query q = session.createQuery(query.toString());
1239
1240                QueryPos qPos = QueryPos.getInstance(q);
1241
1242                qPos.add(userId);
1243
1244                count = (Long)q.uniqueResult();
1245            }
1246            catch (Exception e) {
1247                throw processException(e);
1248            }
1249            finally {
1250                if (count == null) {
1251                    count = Long.valueOf(0);
1252                }
1253
1254                FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_USERID,
1255                    finderArgs, count);
1256
1257                closeSession(session);
1258            }
1259        }
1260
1261        return count.intValue();
1262    }
1263
1264    public int countByG_U(long groupId, long userId) throws SystemException {
1265        Object[] finderArgs = new Object[] { new Long(groupId), new Long(userId) };
1266
1267        Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_G_U,
1268                finderArgs, this);
1269
1270        if (count == null) {
1271            Session session = null;
1272
1273            try {
1274                session = openSession();
1275
1276                StringBuilder query = new StringBuilder();
1277
1278                query.append("SELECT COUNT(shoppingCart) ");
1279                query.append("FROM ShoppingCart shoppingCart WHERE ");
1280
1281                query.append("shoppingCart.groupId = ?");
1282
1283                query.append(" AND ");
1284
1285                query.append("shoppingCart.userId = ?");
1286
1287                query.append(" ");
1288
1289                Query q = session.createQuery(query.toString());
1290
1291                QueryPos qPos = QueryPos.getInstance(q);
1292
1293                qPos.add(groupId);
1294
1295                qPos.add(userId);
1296
1297                count = (Long)q.uniqueResult();
1298            }
1299            catch (Exception e) {
1300                throw processException(e);
1301            }
1302            finally {
1303                if (count == null) {
1304                    count = Long.valueOf(0);
1305                }
1306
1307                FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_G_U, finderArgs,
1308                    count);
1309
1310                closeSession(session);
1311            }
1312        }
1313
1314        return count.intValue();
1315    }
1316
1317    public int countAll() throws SystemException {
1318        Object[] finderArgs = new Object[0];
1319
1320        Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
1321                finderArgs, this);
1322
1323        if (count == null) {
1324            Session session = null;
1325
1326            try {
1327                session = openSession();
1328
1329                Query q = session.createQuery(
1330                        "SELECT COUNT(shoppingCart) FROM ShoppingCart shoppingCart");
1331
1332                count = (Long)q.uniqueResult();
1333            }
1334            catch (Exception e) {
1335                throw processException(e);
1336            }
1337            finally {
1338                if (count == null) {
1339                    count = Long.valueOf(0);
1340                }
1341
1342                FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL, finderArgs,
1343                    count);
1344
1345                closeSession(session);
1346            }
1347        }
1348
1349        return count.intValue();
1350    }
1351
1352    public void afterPropertiesSet() {
1353        String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
1354                    com.liferay.portal.util.PropsUtil.get(
1355                        "value.object.listener.com.liferay.portlet.shopping.model.ShoppingCart")));
1356
1357        if (listenerClassNames.length > 0) {
1358            try {
1359                List<ModelListener<ShoppingCart>> listenersList = new ArrayList<ModelListener<ShoppingCart>>();
1360
1361                for (String listenerClassName : listenerClassNames) {
1362                    listenersList.add((ModelListener<ShoppingCart>)Class.forName(
1363                            listenerClassName).newInstance());
1364                }
1365
1366                listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
1367            }
1368            catch (Exception e) {
1369                _log.error(e);
1370            }
1371        }
1372    }
1373
1374    @BeanReference(name = "com.liferay.portlet.shopping.service.persistence.ShoppingCartPersistence.impl")
1375    protected com.liferay.portlet.shopping.service.persistence.ShoppingCartPersistence shoppingCartPersistence;
1376    @BeanReference(name = "com.liferay.portlet.shopping.service.persistence.ShoppingCategoryPersistence.impl")
1377    protected com.liferay.portlet.shopping.service.persistence.ShoppingCategoryPersistence shoppingCategoryPersistence;
1378    @BeanReference(name = "com.liferay.portlet.shopping.service.persistence.ShoppingCouponPersistence.impl")
1379    protected com.liferay.portlet.shopping.service.persistence.ShoppingCouponPersistence shoppingCouponPersistence;
1380    @BeanReference(name = "com.liferay.portlet.shopping.service.persistence.ShoppingItemPersistence.impl")
1381    protected com.liferay.portlet.shopping.service.persistence.ShoppingItemPersistence shoppingItemPersistence;
1382    @BeanReference(name = "com.liferay.portlet.shopping.service.persistence.ShoppingItemFieldPersistence.impl")
1383    protected com.liferay.portlet.shopping.service.persistence.ShoppingItemFieldPersistence shoppingItemFieldPersistence;
1384    @BeanReference(name = "com.liferay.portlet.shopping.service.persistence.ShoppingItemPricePersistence.impl")
1385    protected com.liferay.portlet.shopping.service.persistence.ShoppingItemPricePersistence shoppingItemPricePersistence;
1386    @BeanReference(name = "com.liferay.portlet.shopping.service.persistence.ShoppingOrderPersistence.impl")
1387    protected com.liferay.portlet.shopping.service.persistence.ShoppingOrderPersistence shoppingOrderPersistence;
1388    @BeanReference(name = "com.liferay.portlet.shopping.service.persistence.ShoppingOrderItemPersistence.impl")
1389    protected com.liferay.portlet.shopping.service.persistence.ShoppingOrderItemPersistence shoppingOrderItemPersistence;
1390    @BeanReference(name = "com.liferay.portal.service.persistence.ResourcePersistence.impl")
1391    protected com.liferay.portal.service.persistence.ResourcePersistence resourcePersistence;
1392    @BeanReference(name = "com.liferay.portal.service.persistence.UserPersistence.impl")
1393    protected com.liferay.portal.service.persistence.UserPersistence userPersistence;
1394    private static Log _log = LogFactoryUtil.getLog(ShoppingCartPersistenceImpl.class);
1395}