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