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