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