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