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