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