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