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.dao.orm.DynamicQuery;
27  import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
28  import com.liferay.portal.kernel.dao.orm.Query;
29  import com.liferay.portal.kernel.dao.orm.QueryPos;
30  import com.liferay.portal.kernel.dao.orm.QueryUtil;
31  import com.liferay.portal.kernel.dao.orm.Session;
32  import com.liferay.portal.kernel.util.GetterUtil;
33  import com.liferay.portal.kernel.util.ListUtil;
34  import com.liferay.portal.kernel.util.OrderByComparator;
35  import com.liferay.portal.kernel.util.StringPool;
36  import com.liferay.portal.kernel.util.StringUtil;
37  import com.liferay.portal.kernel.util.Validator;
38  import com.liferay.portal.kernel.uuid.PortalUUIDUtil;
39  import com.liferay.portal.model.ModelListener;
40  import com.liferay.portal.service.persistence.BatchSessionUtil;
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 {
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             if (BatchSessionUtil.isEnabled()) {
137                 Object staleObject = session.get(PollsChoiceImpl.class,
138                         pollsChoice.getPrimaryKeyObj());
139 
140                 if (staleObject != null) {
141                     session.evict(staleObject);
142                 }
143             }
144 
145             session.delete(pollsChoice);
146 
147             session.flush();
148 
149             return pollsChoice;
150         }
151         catch (Exception e) {
152             throw processException(e);
153         }
154         finally {
155             closeSession(session);
156 
157             FinderCacheUtil.clearCache(PollsChoice.class.getName());
158         }
159     }
160 
161     /**
162      * @deprecated Use <code>update(PollsChoice pollsChoice, boolean merge)</code>.
163      */
164     public PollsChoice update(PollsChoice pollsChoice)
165         throws SystemException {
166         if (_log.isWarnEnabled()) {
167             _log.warn(
168                 "Using the deprecated update(PollsChoice pollsChoice) method. Use update(PollsChoice pollsChoice, boolean merge) instead.");
169         }
170 
171         return update(pollsChoice, false);
172     }
173 
174     /**
175      * Add, update, or merge, the entity. This method also calls the model
176      * listeners to trigger the proper events associated with adding, deleting,
177      * or updating an entity.
178      *
179      * @param        pollsChoice the entity to add, update, or merge
180      * @param        merge boolean value for whether to merge the entity. The
181      *                default value is false. Setting merge to true is more
182      *                expensive and should only be true when pollsChoice is
183      *                transient. See LEP-5473 for a detailed discussion of this
184      *                method.
185      * @return        true if the portlet can be displayed via Ajax
186      */
187     public PollsChoice update(PollsChoice pollsChoice, boolean merge)
188         throws SystemException {
189         boolean isNew = pollsChoice.isNew();
190 
191         if (_listeners.length > 0) {
192             for (ModelListener listener : _listeners) {
193                 if (isNew) {
194                     listener.onBeforeCreate(pollsChoice);
195                 }
196                 else {
197                     listener.onBeforeUpdate(pollsChoice);
198                 }
199             }
200         }
201 
202         pollsChoice = updateImpl(pollsChoice, merge);
203 
204         if (_listeners.length > 0) {
205             for (ModelListener listener : _listeners) {
206                 if (isNew) {
207                     listener.onAfterCreate(pollsChoice);
208                 }
209                 else {
210                     listener.onAfterUpdate(pollsChoice);
211                 }
212             }
213         }
214 
215         return pollsChoice;
216     }
217 
218     public PollsChoice updateImpl(
219         com.liferay.portlet.polls.model.PollsChoice pollsChoice, boolean merge)
220         throws SystemException {
221         if (Validator.isNull(pollsChoice.getUuid())) {
222             String uuid = PortalUUIDUtil.generate();
223 
224             pollsChoice.setUuid(uuid);
225         }
226 
227         Session session = null;
228 
229         try {
230             session = openSession();
231 
232             BatchSessionUtil.update(session, pollsChoice, merge);
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 = null;
1000
1001                if (obc == null) {
1002                    list = (List<PollsChoice>)QueryUtil.list(q, getDialect(),
1003                            start, end, false);
1004
1005                    Collections.sort(list);
1006                }
1007                else {
1008                    list = (List<PollsChoice>)QueryUtil.list(q, getDialect(),
1009                            start, end);
1010                }
1011
1012                FinderCacheUtil.putResult(finderClassNameCacheEnabled,
1013                    finderClassName, finderMethodName, finderParams,
1014                    finderArgs, list);
1015
1016                return list;
1017            }
1018            catch (Exception e) {
1019                throw processException(e);
1020            }
1021            finally {
1022                closeSession(session);
1023            }
1024        }
1025        else {
1026            return (List<PollsChoice>)result;
1027        }
1028    }
1029
1030    public void removeByUuid(String uuid) throws SystemException {
1031        for (PollsChoice pollsChoice : findByUuid(uuid)) {
1032            remove(pollsChoice);
1033        }
1034    }
1035
1036    public void removeByQuestionId(long questionId) throws SystemException {
1037        for (PollsChoice pollsChoice : findByQuestionId(questionId)) {
1038            remove(pollsChoice);
1039        }
1040    }
1041
1042    public void removeByQ_N(long questionId, String name)
1043        throws NoSuchChoiceException, SystemException {
1044        PollsChoice pollsChoice = findByQ_N(questionId, name);
1045
1046        remove(pollsChoice);
1047    }
1048
1049    public void removeAll() throws SystemException {
1050        for (PollsChoice pollsChoice : findAll()) {
1051            remove(pollsChoice);
1052        }
1053    }
1054
1055    public int countByUuid(String uuid) throws SystemException {
1056        boolean finderClassNameCacheEnabled = PollsChoiceModelImpl.CACHE_ENABLED;
1057        String finderClassName = PollsChoice.class.getName();
1058        String finderMethodName = "countByUuid";
1059        String[] finderParams = new String[] { String.class.getName() };
1060        Object[] finderArgs = new Object[] { uuid };
1061
1062        Object result = null;
1063
1064        if (finderClassNameCacheEnabled) {
1065            result = FinderCacheUtil.getResult(finderClassName,
1066                    finderMethodName, finderParams, finderArgs, this);
1067        }
1068
1069        if (result == null) {
1070            Session session = null;
1071
1072            try {
1073                session = openSession();
1074
1075                StringBuilder query = new StringBuilder();
1076
1077                query.append("SELECT COUNT(*) ");
1078                query.append(
1079                    "FROM com.liferay.portlet.polls.model.PollsChoice WHERE ");
1080
1081                if (uuid == null) {
1082                    query.append("uuid_ IS NULL");
1083                }
1084                else {
1085                    query.append("uuid_ = ?");
1086                }
1087
1088                query.append(" ");
1089
1090                Query q = session.createQuery(query.toString());
1091
1092                QueryPos qPos = QueryPos.getInstance(q);
1093
1094                if (uuid != null) {
1095                    qPos.add(uuid);
1096                }
1097
1098                Long count = null;
1099
1100                Iterator<Long> itr = q.list().iterator();
1101
1102                if (itr.hasNext()) {
1103                    count = itr.next();
1104                }
1105
1106                if (count == null) {
1107                    count = new Long(0);
1108                }
1109
1110                FinderCacheUtil.putResult(finderClassNameCacheEnabled,
1111                    finderClassName, finderMethodName, finderParams,
1112                    finderArgs, count);
1113
1114                return count.intValue();
1115            }
1116            catch (Exception e) {
1117                throw processException(e);
1118            }
1119            finally {
1120                closeSession(session);
1121            }
1122        }
1123        else {
1124            return ((Long)result).intValue();
1125        }
1126    }
1127
1128    public int countByQuestionId(long questionId) throws SystemException {
1129        boolean finderClassNameCacheEnabled = PollsChoiceModelImpl.CACHE_ENABLED;
1130        String finderClassName = PollsChoice.class.getName();
1131        String finderMethodName = "countByQuestionId";
1132        String[] finderParams = new String[] { Long.class.getName() };
1133        Object[] finderArgs = new Object[] { new Long(questionId) };
1134
1135        Object result = null;
1136
1137        if (finderClassNameCacheEnabled) {
1138            result = FinderCacheUtil.getResult(finderClassName,
1139                    finderMethodName, finderParams, finderArgs, this);
1140        }
1141
1142        if (result == null) {
1143            Session session = null;
1144
1145            try {
1146                session = openSession();
1147
1148                StringBuilder query = new StringBuilder();
1149
1150                query.append("SELECT COUNT(*) ");
1151                query.append(
1152                    "FROM com.liferay.portlet.polls.model.PollsChoice WHERE ");
1153
1154                query.append("questionId = ?");
1155
1156                query.append(" ");
1157
1158                Query q = session.createQuery(query.toString());
1159
1160                QueryPos qPos = QueryPos.getInstance(q);
1161
1162                qPos.add(questionId);
1163
1164                Long count = null;
1165
1166                Iterator<Long> itr = q.list().iterator();
1167
1168                if (itr.hasNext()) {
1169                    count = itr.next();
1170                }
1171
1172                if (count == null) {
1173                    count = new Long(0);
1174                }
1175
1176                FinderCacheUtil.putResult(finderClassNameCacheEnabled,
1177                    finderClassName, finderMethodName, finderParams,
1178                    finderArgs, count);
1179
1180                return count.intValue();
1181            }
1182            catch (Exception e) {
1183                throw processException(e);
1184            }
1185            finally {
1186                closeSession(session);
1187            }
1188        }
1189        else {
1190            return ((Long)result).intValue();
1191        }
1192    }
1193
1194    public int countByQ_N(long questionId, String name)
1195        throws SystemException {
1196        boolean finderClassNameCacheEnabled = PollsChoiceModelImpl.CACHE_ENABLED;
1197        String finderClassName = PollsChoice.class.getName();
1198        String finderMethodName = "countByQ_N";
1199        String[] finderParams = new String[] {
1200                Long.class.getName(), String.class.getName()
1201            };
1202        Object[] finderArgs = new Object[] { new Long(questionId), name };
1203
1204        Object result = null;
1205
1206        if (finderClassNameCacheEnabled) {
1207            result = FinderCacheUtil.getResult(finderClassName,
1208                    finderMethodName, finderParams, finderArgs, this);
1209        }
1210
1211        if (result == null) {
1212            Session session = null;
1213
1214            try {
1215                session = openSession();
1216
1217                StringBuilder query = new StringBuilder();
1218
1219                query.append("SELECT COUNT(*) ");
1220                query.append(
1221                    "FROM com.liferay.portlet.polls.model.PollsChoice WHERE ");
1222
1223                query.append("questionId = ?");
1224
1225                query.append(" AND ");
1226
1227                if (name == null) {
1228                    query.append("name IS NULL");
1229                }
1230                else {
1231                    query.append("name = ?");
1232                }
1233
1234                query.append(" ");
1235
1236                Query q = session.createQuery(query.toString());
1237
1238                QueryPos qPos = QueryPos.getInstance(q);
1239
1240                qPos.add(questionId);
1241
1242                if (name != null) {
1243                    qPos.add(name);
1244                }
1245
1246                Long count = null;
1247
1248                Iterator<Long> itr = q.list().iterator();
1249
1250                if (itr.hasNext()) {
1251                    count = itr.next();
1252                }
1253
1254                if (count == null) {
1255                    count = new Long(0);
1256                }
1257
1258                FinderCacheUtil.putResult(finderClassNameCacheEnabled,
1259                    finderClassName, finderMethodName, finderParams,
1260                    finderArgs, count);
1261
1262                return count.intValue();
1263            }
1264            catch (Exception e) {
1265                throw processException(e);
1266            }
1267            finally {
1268                closeSession(session);
1269            }
1270        }
1271        else {
1272            return ((Long)result).intValue();
1273        }
1274    }
1275
1276    public int countAll() throws SystemException {
1277        boolean finderClassNameCacheEnabled = PollsChoiceModelImpl.CACHE_ENABLED;
1278        String finderClassName = PollsChoice.class.getName();
1279        String finderMethodName = "countAll";
1280        String[] finderParams = new String[] {  };
1281        Object[] finderArgs = new Object[] {  };
1282
1283        Object result = null;
1284
1285        if (finderClassNameCacheEnabled) {
1286            result = FinderCacheUtil.getResult(finderClassName,
1287                    finderMethodName, finderParams, finderArgs, this);
1288        }
1289
1290        if (result == null) {
1291            Session session = null;
1292
1293            try {
1294                session = openSession();
1295
1296                Query q = session.createQuery(
1297                        "SELECT COUNT(*) FROM com.liferay.portlet.polls.model.PollsChoice");
1298
1299                Long count = null;
1300
1301                Iterator<Long> itr = q.list().iterator();
1302
1303                if (itr.hasNext()) {
1304                    count = itr.next();
1305                }
1306
1307                if (count == null) {
1308                    count = new Long(0);
1309                }
1310
1311                FinderCacheUtil.putResult(finderClassNameCacheEnabled,
1312                    finderClassName, finderMethodName, finderParams,
1313                    finderArgs, count);
1314
1315                return count.intValue();
1316            }
1317            catch (Exception e) {
1318                throw processException(e);
1319            }
1320            finally {
1321                closeSession(session);
1322            }
1323        }
1324        else {
1325            return ((Long)result).intValue();
1326        }
1327    }
1328
1329    public void registerListener(ModelListener listener) {
1330        List<ModelListener> listeners = ListUtil.fromArray(_listeners);
1331
1332        listeners.add(listener);
1333
1334        _listeners = listeners.toArray(new ModelListener[listeners.size()]);
1335    }
1336
1337    public void unregisterListener(ModelListener listener) {
1338        List<ModelListener> listeners = ListUtil.fromArray(_listeners);
1339
1340        listeners.remove(listener);
1341
1342        _listeners = listeners.toArray(new ModelListener[listeners.size()]);
1343    }
1344
1345    public void afterPropertiesSet() {
1346        String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
1347                    com.liferay.portal.util.PropsUtil.get(
1348                        "value.object.listener.com.liferay.portlet.polls.model.PollsChoice")));
1349
1350        if (listenerClassNames.length > 0) {
1351            try {
1352                List<ModelListener> listeners = new ArrayList<ModelListener>();
1353
1354                for (String listenerClassName : listenerClassNames) {
1355                    listeners.add((ModelListener)Class.forName(
1356                            listenerClassName).newInstance());
1357                }
1358
1359                _listeners = listeners.toArray(new ModelListener[listeners.size()]);
1360            }
1361            catch (Exception e) {
1362                _log.error(e);
1363            }
1364        }
1365    }
1366
1367    private static Log _log = LogFactory.getLog(PollsChoicePersistenceImpl.class);
1368    private ModelListener[] _listeners = new ModelListener[0];
1369}