1   /**
2    * Copyright (c) 2000-2007 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.OrderByComparator;
29  import com.liferay.portal.kernel.util.StringMaker;
30  import com.liferay.portal.kernel.util.StringPool;
31  import com.liferay.portal.service.persistence.BasePersistence;
32  import com.liferay.portal.spring.hibernate.FinderCache;
33  import com.liferay.portal.spring.hibernate.HibernateUtil;
34  
35  import com.liferay.portlet.shopping.NoSuchCouponException;
36  import com.liferay.portlet.shopping.model.ShoppingCoupon;
37  import com.liferay.portlet.shopping.model.impl.ShoppingCouponImpl;
38  
39  import com.liferay.util.dao.hibernate.QueryUtil;
40  
41  import org.apache.commons.logging.Log;
42  import org.apache.commons.logging.LogFactory;
43  
44  import org.hibernate.Query;
45  import org.hibernate.Session;
46  
47  import java.util.Collections;
48  import java.util.Iterator;
49  import java.util.List;
50  
51  /**
52   * <a href="ShoppingCouponPersistenceImpl.java.html"><b><i>View Source</i></b></a>
53   *
54   * @author Brian Wing Shun Chan
55   *
56   */
57  public class ShoppingCouponPersistenceImpl extends BasePersistence
58      implements ShoppingCouponPersistence {
59      public ShoppingCoupon create(long couponId) {
60          ShoppingCoupon shoppingCoupon = new ShoppingCouponImpl();
61          shoppingCoupon.setNew(true);
62          shoppingCoupon.setPrimaryKey(couponId);
63  
64          return shoppingCoupon;
65      }
66  
67      public ShoppingCoupon remove(long couponId)
68          throws NoSuchCouponException, SystemException {
69          Session session = null;
70  
71          try {
72              session = openSession();
73  
74              ShoppingCoupon shoppingCoupon = (ShoppingCoupon)session.get(ShoppingCouponImpl.class,
75                      new Long(couponId));
76  
77              if (shoppingCoupon == null) {
78                  if (_log.isWarnEnabled()) {
79                      _log.warn("No ShoppingCoupon exists with the primary key " +
80                          couponId);
81                  }
82  
83                  throw new NoSuchCouponException(
84                      "No ShoppingCoupon exists with the primary key " +
85                      couponId);
86              }
87  
88              return remove(shoppingCoupon);
89          }
90          catch (NoSuchCouponException nsee) {
91              throw nsee;
92          }
93          catch (Exception e) {
94              throw HibernateUtil.processException(e);
95          }
96          finally {
97              closeSession(session);
98          }
99      }
100 
101     public ShoppingCoupon remove(ShoppingCoupon shoppingCoupon)
102         throws SystemException {
103         Session session = null;
104 
105         try {
106             session = openSession();
107             session.delete(shoppingCoupon);
108             session.flush();
109 
110             return shoppingCoupon;
111         }
112         catch (Exception e) {
113             throw HibernateUtil.processException(e);
114         }
115         finally {
116             closeSession(session);
117             FinderCache.clearCache(ShoppingCoupon.class.getName());
118         }
119     }
120 
121     public ShoppingCoupon update(
122         com.liferay.portlet.shopping.model.ShoppingCoupon shoppingCoupon)
123         throws SystemException {
124         return update(shoppingCoupon, false);
125     }
126 
127     public ShoppingCoupon update(
128         com.liferay.portlet.shopping.model.ShoppingCoupon shoppingCoupon,
129         boolean merge) throws SystemException {
130         Session session = null;
131 
132         try {
133             session = openSession();
134 
135             if (merge) {
136                 session.merge(shoppingCoupon);
137             }
138             else {
139                 if (shoppingCoupon.isNew()) {
140                     session.save(shoppingCoupon);
141                 }
142             }
143 
144             session.flush();
145             shoppingCoupon.setNew(false);
146 
147             return shoppingCoupon;
148         }
149         catch (Exception e) {
150             throw HibernateUtil.processException(e);
151         }
152         finally {
153             closeSession(session);
154             FinderCache.clearCache(ShoppingCoupon.class.getName());
155         }
156     }
157 
158     public ShoppingCoupon findByPrimaryKey(long couponId)
159         throws NoSuchCouponException, SystemException {
160         ShoppingCoupon shoppingCoupon = fetchByPrimaryKey(couponId);
161 
162         if (shoppingCoupon == null) {
163             if (_log.isWarnEnabled()) {
164                 _log.warn("No ShoppingCoupon exists with the primary key " +
165                     couponId);
166             }
167 
168             throw new NoSuchCouponException(
169                 "No ShoppingCoupon exists with the primary key " + couponId);
170         }
171 
172         return shoppingCoupon;
173     }
174 
175     public ShoppingCoupon fetchByPrimaryKey(long couponId)
176         throws SystemException {
177         Session session = null;
178 
179         try {
180             session = openSession();
181 
182             return (ShoppingCoupon)session.get(ShoppingCouponImpl.class,
183                 new Long(couponId));
184         }
185         catch (Exception e) {
186             throw HibernateUtil.processException(e);
187         }
188         finally {
189             closeSession(session);
190         }
191     }
192 
193     public List findByGroupId(long groupId) throws SystemException {
194         String finderClassName = ShoppingCoupon.class.getName();
195         String finderMethodName = "findByGroupId";
196         String[] finderParams = new String[] { Long.class.getName() };
197         Object[] finderArgs = new Object[] { new Long(groupId) };
198         Object result = FinderCache.getResult(finderClassName,
199                 finderMethodName, finderParams, finderArgs, getSessionFactory());
200 
201         if (result == null) {
202             Session session = null;
203 
204             try {
205                 session = openSession();
206 
207                 StringMaker query = new StringMaker();
208                 query.append(
209                     "FROM com.liferay.portlet.shopping.model.ShoppingCoupon WHERE ");
210                 query.append("groupId = ?");
211                 query.append(" ");
212                 query.append("ORDER BY ");
213                 query.append("createDate ASC");
214 
215                 Query q = session.createQuery(query.toString());
216                 int queryPos = 0;
217                 q.setLong(queryPos++, groupId);
218 
219                 List list = q.list();
220                 FinderCache.putResult(finderClassName, finderMethodName,
221                     finderParams, finderArgs, list);
222 
223                 return list;
224             }
225             catch (Exception e) {
226                 throw HibernateUtil.processException(e);
227             }
228             finally {
229                 closeSession(session);
230             }
231         }
232         else {
233             return (List)result;
234         }
235     }
236 
237     public List findByGroupId(long groupId, int begin, int end)
238         throws SystemException {
239         return findByGroupId(groupId, begin, end, null);
240     }
241 
242     public List findByGroupId(long groupId, int begin, int end,
243         OrderByComparator obc) throws SystemException {
244         String finderClassName = ShoppingCoupon.class.getName();
245         String finderMethodName = "findByGroupId";
246         String[] finderParams = new String[] {
247                 Long.class.getName(), "java.lang.Integer", "java.lang.Integer",
248                 "com.liferay.portal.kernel.util.OrderByComparator"
249             };
250         Object[] finderArgs = new Object[] {
251                 new Long(groupId), String.valueOf(begin), String.valueOf(end),
252                 String.valueOf(obc)
253             };
254         Object result = FinderCache.getResult(finderClassName,
255                 finderMethodName, finderParams, finderArgs, getSessionFactory());
256 
257         if (result == null) {
258             Session session = null;
259 
260             try {
261                 session = openSession();
262 
263                 StringMaker query = new StringMaker();
264                 query.append(
265                     "FROM com.liferay.portlet.shopping.model.ShoppingCoupon WHERE ");
266                 query.append("groupId = ?");
267                 query.append(" ");
268 
269                 if (obc != null) {
270                     query.append("ORDER BY ");
271                     query.append(obc.getOrderBy());
272                 }
273                 else {
274                     query.append("ORDER BY ");
275                     query.append("createDate ASC");
276                 }
277 
278                 Query q = session.createQuery(query.toString());
279                 int queryPos = 0;
280                 q.setLong(queryPos++, groupId);
281 
282                 List list = QueryUtil.list(q, getDialect(), begin, end);
283                 FinderCache.putResult(finderClassName, finderMethodName,
284                     finderParams, finderArgs, list);
285 
286                 return list;
287             }
288             catch (Exception e) {
289                 throw HibernateUtil.processException(e);
290             }
291             finally {
292                 closeSession(session);
293             }
294         }
295         else {
296             return (List)result;
297         }
298     }
299 
300     public ShoppingCoupon findByGroupId_First(long groupId,
301         OrderByComparator obc) throws NoSuchCouponException, SystemException {
302         List list = findByGroupId(groupId, 0, 1, obc);
303 
304         if (list.size() == 0) {
305             StringMaker msg = new StringMaker();
306             msg.append("No ShoppingCoupon exists with the key ");
307             msg.append(StringPool.OPEN_CURLY_BRACE);
308             msg.append("groupId=");
309             msg.append(groupId);
310             msg.append(StringPool.CLOSE_CURLY_BRACE);
311             throw new NoSuchCouponException(msg.toString());
312         }
313         else {
314             return (ShoppingCoupon)list.get(0);
315         }
316     }
317 
318     public ShoppingCoupon findByGroupId_Last(long groupId, OrderByComparator obc)
319         throws NoSuchCouponException, SystemException {
320         int count = countByGroupId(groupId);
321         List list = findByGroupId(groupId, count - 1, count, obc);
322 
323         if (list.size() == 0) {
324             StringMaker msg = new StringMaker();
325             msg.append("No ShoppingCoupon exists with the key ");
326             msg.append(StringPool.OPEN_CURLY_BRACE);
327             msg.append("groupId=");
328             msg.append(groupId);
329             msg.append(StringPool.CLOSE_CURLY_BRACE);
330             throw new NoSuchCouponException(msg.toString());
331         }
332         else {
333             return (ShoppingCoupon)list.get(0);
334         }
335     }
336 
337     public ShoppingCoupon[] findByGroupId_PrevAndNext(long couponId,
338         long groupId, OrderByComparator obc)
339         throws NoSuchCouponException, SystemException {
340         ShoppingCoupon shoppingCoupon = findByPrimaryKey(couponId);
341         int count = countByGroupId(groupId);
342         Session session = null;
343 
344         try {
345             session = openSession();
346 
347             StringMaker query = new StringMaker();
348             query.append(
349                 "FROM com.liferay.portlet.shopping.model.ShoppingCoupon WHERE ");
350             query.append("groupId = ?");
351             query.append(" ");
352 
353             if (obc != null) {
354                 query.append("ORDER BY ");
355                 query.append(obc.getOrderBy());
356             }
357             else {
358                 query.append("ORDER BY ");
359                 query.append("createDate ASC");
360             }
361 
362             Query q = session.createQuery(query.toString());
363             int queryPos = 0;
364             q.setLong(queryPos++, groupId);
365 
366             Object[] objArray = QueryUtil.getPrevAndNext(q, count, obc,
367                     shoppingCoupon);
368             ShoppingCoupon[] array = new ShoppingCouponImpl[3];
369             array[0] = (ShoppingCoupon)objArray[0];
370             array[1] = (ShoppingCoupon)objArray[1];
371             array[2] = (ShoppingCoupon)objArray[2];
372 
373             return array;
374         }
375         catch (Exception e) {
376             throw HibernateUtil.processException(e);
377         }
378         finally {
379             closeSession(session);
380         }
381     }
382 
383     public ShoppingCoupon findByCode(String code)
384         throws NoSuchCouponException, SystemException {
385         ShoppingCoupon shoppingCoupon = fetchByCode(code);
386 
387         if (shoppingCoupon == null) {
388             StringMaker msg = new StringMaker();
389             msg.append("No ShoppingCoupon exists with the key ");
390             msg.append(StringPool.OPEN_CURLY_BRACE);
391             msg.append("code=");
392             msg.append(code);
393             msg.append(StringPool.CLOSE_CURLY_BRACE);
394 
395             if (_log.isWarnEnabled()) {
396                 _log.warn(msg.toString());
397             }
398 
399             throw new NoSuchCouponException(msg.toString());
400         }
401 
402         return shoppingCoupon;
403     }
404 
405     public ShoppingCoupon fetchByCode(String code) throws SystemException {
406         String finderClassName = ShoppingCoupon.class.getName();
407         String finderMethodName = "fetchByCode";
408         String[] finderParams = new String[] { String.class.getName() };
409         Object[] finderArgs = new Object[] { code };
410         Object result = FinderCache.getResult(finderClassName,
411                 finderMethodName, finderParams, finderArgs, getSessionFactory());
412 
413         if (result == null) {
414             Session session = null;
415 
416             try {
417                 session = openSession();
418 
419                 StringMaker query = new StringMaker();
420                 query.append(
421                     "FROM com.liferay.portlet.shopping.model.ShoppingCoupon WHERE ");
422 
423                 if (code == null) {
424                     query.append("code_ IS NULL");
425                 }
426                 else {
427                     query.append("code_ = ?");
428                 }
429 
430                 query.append(" ");
431                 query.append("ORDER BY ");
432                 query.append("createDate ASC");
433 
434                 Query q = session.createQuery(query.toString());
435                 int queryPos = 0;
436 
437                 if (code != null) {
438                     q.setString(queryPos++, code);
439                 }
440 
441                 List list = q.list();
442                 FinderCache.putResult(finderClassName, finderMethodName,
443                     finderParams, finderArgs, list);
444 
445                 if (list.size() == 0) {
446                     return null;
447                 }
448                 else {
449                     return (ShoppingCoupon)list.get(0);
450                 }
451             }
452             catch (Exception e) {
453                 throw HibernateUtil.processException(e);
454             }
455             finally {
456                 closeSession(session);
457             }
458         }
459         else {
460             List list = (List)result;
461 
462             if (list.size() == 0) {
463                 return null;
464             }
465             else {
466                 return (ShoppingCoupon)list.get(0);
467             }
468         }
469     }
470 
471     public List findWithDynamicQuery(DynamicQueryInitializer queryInitializer)
472         throws SystemException {
473         Session session = null;
474 
475         try {
476             session = openSession();
477 
478             DynamicQuery query = queryInitializer.initialize(session);
479 
480             return query.list();
481         }
482         catch (Exception e) {
483             throw HibernateUtil.processException(e);
484         }
485         finally {
486             closeSession(session);
487         }
488     }
489 
490     public List findWithDynamicQuery(DynamicQueryInitializer queryInitializer,
491         int begin, int end) throws SystemException {
492         Session session = null;
493 
494         try {
495             session = openSession();
496 
497             DynamicQuery query = queryInitializer.initialize(session);
498             query.setLimit(begin, end);
499 
500             return query.list();
501         }
502         catch (Exception e) {
503             throw HibernateUtil.processException(e);
504         }
505         finally {
506             closeSession(session);
507         }
508     }
509 
510     public List findAll() throws SystemException {
511         return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
512     }
513 
514     public List findAll(int begin, int end) throws SystemException {
515         return findAll(begin, end, null);
516     }
517 
518     public List findAll(int begin, int end, OrderByComparator obc)
519         throws SystemException {
520         String finderClassName = ShoppingCoupon.class.getName();
521         String finderMethodName = "findAll";
522         String[] finderParams = new String[] {
523                 "java.lang.Integer", "java.lang.Integer",
524                 "com.liferay.portal.kernel.util.OrderByComparator"
525             };
526         Object[] finderArgs = new Object[] {
527                 String.valueOf(begin), String.valueOf(end), String.valueOf(obc)
528             };
529         Object result = FinderCache.getResult(finderClassName,
530                 finderMethodName, finderParams, finderArgs, getSessionFactory());
531 
532         if (result == null) {
533             Session session = null;
534 
535             try {
536                 session = openSession();
537 
538                 StringMaker query = new StringMaker();
539                 query.append(
540                     "FROM com.liferay.portlet.shopping.model.ShoppingCoupon ");
541 
542                 if (obc != null) {
543                     query.append("ORDER BY ");
544                     query.append(obc.getOrderBy());
545                 }
546                 else {
547                     query.append("ORDER BY ");
548                     query.append("createDate ASC");
549                 }
550 
551                 Query q = session.createQuery(query.toString());
552                 List list = QueryUtil.list(q, getDialect(), begin, end);
553 
554                 if (obc == null) {
555                     Collections.sort(list);
556                 }
557 
558                 FinderCache.putResult(finderClassName, finderMethodName,
559                     finderParams, finderArgs, list);
560 
561                 return list;
562             }
563             catch (Exception e) {
564                 throw HibernateUtil.processException(e);
565             }
566             finally {
567                 closeSession(session);
568             }
569         }
570         else {
571             return (List)result;
572         }
573     }
574 
575     public void removeByGroupId(long groupId) throws SystemException {
576         Iterator itr = findByGroupId(groupId).iterator();
577 
578         while (itr.hasNext()) {
579             ShoppingCoupon shoppingCoupon = (ShoppingCoupon)itr.next();
580             remove(shoppingCoupon);
581         }
582     }
583 
584     public void removeByCode(String code)
585         throws NoSuchCouponException, SystemException {
586         ShoppingCoupon shoppingCoupon = findByCode(code);
587         remove(shoppingCoupon);
588     }
589 
590     public void removeAll() throws SystemException {
591         Iterator itr = findAll().iterator();
592 
593         while (itr.hasNext()) {
594             remove((ShoppingCoupon)itr.next());
595         }
596     }
597 
598     public int countByGroupId(long groupId) throws SystemException {
599         String finderClassName = ShoppingCoupon.class.getName();
600         String finderMethodName = "countByGroupId";
601         String[] finderParams = new String[] { Long.class.getName() };
602         Object[] finderArgs = new Object[] { new Long(groupId) };
603         Object result = FinderCache.getResult(finderClassName,
604                 finderMethodName, finderParams, finderArgs, getSessionFactory());
605 
606         if (result == null) {
607             Session session = null;
608 
609             try {
610                 session = openSession();
611 
612                 StringMaker query = new StringMaker();
613                 query.append("SELECT COUNT(*) ");
614                 query.append(
615                     "FROM com.liferay.portlet.shopping.model.ShoppingCoupon WHERE ");
616                 query.append("groupId = ?");
617                 query.append(" ");
618 
619                 Query q = session.createQuery(query.toString());
620                 int queryPos = 0;
621                 q.setLong(queryPos++, groupId);
622 
623                 Long count = null;
624                 Iterator itr = q.list().iterator();
625 
626                 if (itr.hasNext()) {
627                     count = (Long)itr.next();
628                 }
629 
630                 if (count == null) {
631                     count = new Long(0);
632                 }
633 
634                 FinderCache.putResult(finderClassName, finderMethodName,
635                     finderParams, finderArgs, count);
636 
637                 return count.intValue();
638             }
639             catch (Exception e) {
640                 throw HibernateUtil.processException(e);
641             }
642             finally {
643                 closeSession(session);
644             }
645         }
646         else {
647             return ((Long)result).intValue();
648         }
649     }
650 
651     public int countByCode(String code) throws SystemException {
652         String finderClassName = ShoppingCoupon.class.getName();
653         String finderMethodName = "countByCode";
654         String[] finderParams = new String[] { String.class.getName() };
655         Object[] finderArgs = new Object[] { code };
656         Object result = FinderCache.getResult(finderClassName,
657                 finderMethodName, finderParams, finderArgs, getSessionFactory());
658 
659         if (result == null) {
660             Session session = null;
661 
662             try {
663                 session = openSession();
664 
665                 StringMaker query = new StringMaker();
666                 query.append("SELECT COUNT(*) ");
667                 query.append(
668                     "FROM com.liferay.portlet.shopping.model.ShoppingCoupon WHERE ");
669 
670                 if (code == null) {
671                     query.append("code_ IS NULL");
672                 }
673                 else {
674                     query.append("code_ = ?");
675                 }
676 
677                 query.append(" ");
678 
679                 Query q = session.createQuery(query.toString());
680                 int queryPos = 0;
681 
682                 if (code != null) {
683                     q.setString(queryPos++, code);
684                 }
685 
686                 Long count = null;
687                 Iterator itr = q.list().iterator();
688 
689                 if (itr.hasNext()) {
690                     count = (Long)itr.next();
691                 }
692 
693                 if (count == null) {
694                     count = new Long(0);
695                 }
696 
697                 FinderCache.putResult(finderClassName, finderMethodName,
698                     finderParams, finderArgs, count);
699 
700                 return count.intValue();
701             }
702             catch (Exception e) {
703                 throw HibernateUtil.processException(e);
704             }
705             finally {
706                 closeSession(session);
707             }
708         }
709         else {
710             return ((Long)result).intValue();
711         }
712     }
713 
714     public int countAll() throws SystemException {
715         String finderClassName = ShoppingCoupon.class.getName();
716         String finderMethodName = "countAll";
717         String[] finderParams = new String[] {  };
718         Object[] finderArgs = new Object[] {  };
719         Object result = FinderCache.getResult(finderClassName,
720                 finderMethodName, finderParams, finderArgs, getSessionFactory());
721 
722         if (result == null) {
723             Session session = null;
724 
725             try {
726                 session = openSession();
727 
728                 StringMaker query = new StringMaker();
729                 query.append("SELECT COUNT(*) ");
730                 query.append(
731                     "FROM com.liferay.portlet.shopping.model.ShoppingCoupon");
732 
733                 Query q = session.createQuery(query.toString());
734                 Long count = null;
735                 Iterator itr = q.list().iterator();
736 
737                 if (itr.hasNext()) {
738                     count = (Long)itr.next();
739                 }
740 
741                 if (count == null) {
742                     count = new Long(0);
743                 }
744 
745                 FinderCache.putResult(finderClassName, finderMethodName,
746                     finderParams, finderArgs, count);
747 
748                 return count.intValue();
749             }
750             catch (Exception e) {
751                 throw HibernateUtil.processException(e);
752             }
753             finally {
754                 closeSession(session);
755             }
756         }
757         else {
758             return ((Long)result).intValue();
759         }
760     }
761 
762     protected void initDao() {
763     }
764 
765     private static Log _log = LogFactory.getLog(ShoppingCouponPersistenceImpl.class);
766 }