001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.polls.service.persistence;
016    
017    import com.liferay.portal.NoSuchModelException;
018    import com.liferay.portal.kernel.annotation.BeanReference;
019    import com.liferay.portal.kernel.cache.CacheRegistryUtil;
020    import com.liferay.portal.kernel.dao.orm.EntityCacheUtil;
021    import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
022    import com.liferay.portal.kernel.dao.orm.FinderPath;
023    import com.liferay.portal.kernel.dao.orm.Query;
024    import com.liferay.portal.kernel.dao.orm.QueryPos;
025    import com.liferay.portal.kernel.dao.orm.QueryUtil;
026    import com.liferay.portal.kernel.dao.orm.Session;
027    import com.liferay.portal.kernel.exception.SystemException;
028    import com.liferay.portal.kernel.log.Log;
029    import com.liferay.portal.kernel.log.LogFactoryUtil;
030    import com.liferay.portal.kernel.util.GetterUtil;
031    import com.liferay.portal.kernel.util.InstanceFactory;
032    import com.liferay.portal.kernel.util.OrderByComparator;
033    import com.liferay.portal.kernel.util.StringBundler;
034    import com.liferay.portal.kernel.util.StringPool;
035    import com.liferay.portal.kernel.util.StringUtil;
036    import com.liferay.portal.kernel.util.Validator;
037    import com.liferay.portal.kernel.uuid.PortalUUIDUtil;
038    import com.liferay.portal.model.ModelListener;
039    import com.liferay.portal.service.persistence.BatchSessionUtil;
040    import com.liferay.portal.service.persistence.ResourcePersistence;
041    import com.liferay.portal.service.persistence.UserPersistence;
042    import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
043    
044    import com.liferay.portlet.polls.NoSuchChoiceException;
045    import com.liferay.portlet.polls.model.PollsChoice;
046    import com.liferay.portlet.polls.model.impl.PollsChoiceImpl;
047    import com.liferay.portlet.polls.model.impl.PollsChoiceModelImpl;
048    
049    import java.io.Serializable;
050    
051    import java.util.ArrayList;
052    import java.util.Collections;
053    import java.util.List;
054    
055    /**
056     * The persistence implementation for the polls choice service.
057     *
058     * <p>
059     * Never modify or reference this class directly. Always use {@link PollsChoiceUtil} to access the polls choice persistence. Modify <code>service.xml</code> and rerun ServiceBuilder to regenerate this class.
060     * </p>
061     *
062     * <p>
063     * Caching information and settings can be found in <code>portal.properties</code>
064     * </p>
065     *
066     * @author Brian Wing Shun Chan
067     * @see PollsChoicePersistence
068     * @see PollsChoiceUtil
069     * @generated
070     */
071    public class PollsChoicePersistenceImpl extends BasePersistenceImpl<PollsChoice>
072            implements PollsChoicePersistence {
073            public static final String FINDER_CLASS_NAME_ENTITY = PollsChoiceImpl.class.getName();
074            public static final String FINDER_CLASS_NAME_LIST = FINDER_CLASS_NAME_ENTITY +
075                    ".List";
076            public static final FinderPath FINDER_PATH_FIND_BY_UUID = new FinderPath(PollsChoiceModelImpl.ENTITY_CACHE_ENABLED,
077                            PollsChoiceModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
078                            "findByUuid",
079                            new String[] {
080                                    String.class.getName(),
081                                    
082                            "java.lang.Integer", "java.lang.Integer",
083                                    "com.liferay.portal.kernel.util.OrderByComparator"
084                            });
085            public static final FinderPath FINDER_PATH_COUNT_BY_UUID = new FinderPath(PollsChoiceModelImpl.ENTITY_CACHE_ENABLED,
086                            PollsChoiceModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
087                            "countByUuid", new String[] { String.class.getName() });
088            public static final FinderPath FINDER_PATH_FIND_BY_QUESTIONID = new FinderPath(PollsChoiceModelImpl.ENTITY_CACHE_ENABLED,
089                            PollsChoiceModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
090                            "findByQuestionId",
091                            new String[] {
092                                    Long.class.getName(),
093                                    
094                            "java.lang.Integer", "java.lang.Integer",
095                                    "com.liferay.portal.kernel.util.OrderByComparator"
096                            });
097            public static final FinderPath FINDER_PATH_COUNT_BY_QUESTIONID = new FinderPath(PollsChoiceModelImpl.ENTITY_CACHE_ENABLED,
098                            PollsChoiceModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
099                            "countByQuestionId", new String[] { Long.class.getName() });
100            public static final FinderPath FINDER_PATH_FETCH_BY_Q_N = new FinderPath(PollsChoiceModelImpl.ENTITY_CACHE_ENABLED,
101                            PollsChoiceModelImpl.FINDER_CACHE_ENABLED,
102                            FINDER_CLASS_NAME_ENTITY, "fetchByQ_N",
103                            new String[] { Long.class.getName(), String.class.getName() });
104            public static final FinderPath FINDER_PATH_COUNT_BY_Q_N = new FinderPath(PollsChoiceModelImpl.ENTITY_CACHE_ENABLED,
105                            PollsChoiceModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
106                            "countByQ_N",
107                            new String[] { Long.class.getName(), String.class.getName() });
108            public static final FinderPath FINDER_PATH_FIND_ALL = new FinderPath(PollsChoiceModelImpl.ENTITY_CACHE_ENABLED,
109                            PollsChoiceModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
110                            "findAll", new String[0]);
111            public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(PollsChoiceModelImpl.ENTITY_CACHE_ENABLED,
112                            PollsChoiceModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
113                            "countAll", new String[0]);
114    
115            /**
116             * Caches the polls choice in the entity cache if it is enabled.
117             *
118             * @param pollsChoice the polls choice to cache
119             */
120            public void cacheResult(PollsChoice pollsChoice) {
121                    EntityCacheUtil.putResult(PollsChoiceModelImpl.ENTITY_CACHE_ENABLED,
122                            PollsChoiceImpl.class, pollsChoice.getPrimaryKey(), pollsChoice);
123    
124                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_Q_N,
125                            new Object[] {
126                                    new Long(pollsChoice.getQuestionId()),
127                                    
128                            pollsChoice.getName()
129                            }, pollsChoice);
130            }
131    
132            /**
133             * Caches the polls choices in the entity cache if it is enabled.
134             *
135             * @param pollsChoices the polls choices to cache
136             */
137            public void cacheResult(List<PollsChoice> pollsChoices) {
138                    for (PollsChoice pollsChoice : pollsChoices) {
139                            if (EntityCacheUtil.getResult(
140                                                    PollsChoiceModelImpl.ENTITY_CACHE_ENABLED,
141                                                    PollsChoiceImpl.class, pollsChoice.getPrimaryKey(), this) == null) {
142                                    cacheResult(pollsChoice);
143                            }
144                    }
145            }
146    
147            /**
148             * Clears the cache for all polls choices.
149             *
150             * <p>
151             * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
152             * </p>
153             */
154            public void clearCache() {
155                    CacheRegistryUtil.clear(PollsChoiceImpl.class.getName());
156                    EntityCacheUtil.clearCache(PollsChoiceImpl.class.getName());
157                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
158                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
159            }
160    
161            /**
162             * Clears the cache for the polls choice.
163             *
164             * <p>
165             * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
166             * </p>
167             */
168            public void clearCache(PollsChoice pollsChoice) {
169                    EntityCacheUtil.removeResult(PollsChoiceModelImpl.ENTITY_CACHE_ENABLED,
170                            PollsChoiceImpl.class, pollsChoice.getPrimaryKey());
171    
172                    FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_Q_N,
173                            new Object[] {
174                                    new Long(pollsChoice.getQuestionId()),
175                                    
176                            pollsChoice.getName()
177                            });
178            }
179    
180            /**
181             * Creates a new polls choice with the primary key. Does not add the polls choice to the database.
182             *
183             * @param choiceId the primary key for the new polls choice
184             * @return the new polls choice
185             */
186            public PollsChoice create(long choiceId) {
187                    PollsChoice pollsChoice = new PollsChoiceImpl();
188    
189                    pollsChoice.setNew(true);
190                    pollsChoice.setPrimaryKey(choiceId);
191    
192                    String uuid = PortalUUIDUtil.generate();
193    
194                    pollsChoice.setUuid(uuid);
195    
196                    return pollsChoice;
197            }
198    
199            /**
200             * Removes the polls choice with the primary key from the database. Also notifies the appropriate model listeners.
201             *
202             * @param primaryKey the primary key of the polls choice to remove
203             * @return the polls choice that was removed
204             * @throws com.liferay.portal.NoSuchModelException if a polls choice with the primary key could not be found
205             * @throws SystemException if a system exception occurred
206             */
207            public PollsChoice remove(Serializable primaryKey)
208                    throws NoSuchModelException, SystemException {
209                    return remove(((Long)primaryKey).longValue());
210            }
211    
212            /**
213             * Removes the polls choice with the primary key from the database. Also notifies the appropriate model listeners.
214             *
215             * @param choiceId the primary key of the polls choice to remove
216             * @return the polls choice that was removed
217             * @throws com.liferay.portlet.polls.NoSuchChoiceException if a polls choice with the primary key could not be found
218             * @throws SystemException if a system exception occurred
219             */
220            public PollsChoice remove(long choiceId)
221                    throws NoSuchChoiceException, SystemException {
222                    Session session = null;
223    
224                    try {
225                            session = openSession();
226    
227                            PollsChoice pollsChoice = (PollsChoice)session.get(PollsChoiceImpl.class,
228                                            new Long(choiceId));
229    
230                            if (pollsChoice == null) {
231                                    if (_log.isWarnEnabled()) {
232                                            _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + choiceId);
233                                    }
234    
235                                    throw new NoSuchChoiceException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
236                                            choiceId);
237                            }
238    
239                            return remove(pollsChoice);
240                    }
241                    catch (NoSuchChoiceException nsee) {
242                            throw nsee;
243                    }
244                    catch (Exception e) {
245                            throw processException(e);
246                    }
247                    finally {
248                            closeSession(session);
249                    }
250            }
251    
252            protected PollsChoice removeImpl(PollsChoice pollsChoice)
253                    throws SystemException {
254                    pollsChoice = toUnwrappedModel(pollsChoice);
255    
256                    Session session = null;
257    
258                    try {
259                            session = openSession();
260    
261                            BatchSessionUtil.delete(session, pollsChoice);
262                    }
263                    catch (Exception e) {
264                            throw processException(e);
265                    }
266                    finally {
267                            closeSession(session);
268                    }
269    
270                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
271    
272                    PollsChoiceModelImpl pollsChoiceModelImpl = (PollsChoiceModelImpl)pollsChoice;
273    
274                    FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_Q_N,
275                            new Object[] {
276                                    new Long(pollsChoiceModelImpl.getQuestionId()),
277                                    
278                            pollsChoiceModelImpl.getName()
279                            });
280    
281                    EntityCacheUtil.removeResult(PollsChoiceModelImpl.ENTITY_CACHE_ENABLED,
282                            PollsChoiceImpl.class, pollsChoice.getPrimaryKey());
283    
284                    return pollsChoice;
285            }
286    
287            public PollsChoice updateImpl(
288                    com.liferay.portlet.polls.model.PollsChoice pollsChoice, boolean merge)
289                    throws SystemException {
290                    pollsChoice = toUnwrappedModel(pollsChoice);
291    
292                    boolean isNew = pollsChoice.isNew();
293    
294                    PollsChoiceModelImpl pollsChoiceModelImpl = (PollsChoiceModelImpl)pollsChoice;
295    
296                    if (Validator.isNull(pollsChoice.getUuid())) {
297                            String uuid = PortalUUIDUtil.generate();
298    
299                            pollsChoice.setUuid(uuid);
300                    }
301    
302                    Session session = null;
303    
304                    try {
305                            session = openSession();
306    
307                            BatchSessionUtil.update(session, pollsChoice, merge);
308    
309                            pollsChoice.setNew(false);
310                    }
311                    catch (Exception e) {
312                            throw processException(e);
313                    }
314                    finally {
315                            closeSession(session);
316                    }
317    
318                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
319    
320                    EntityCacheUtil.putResult(PollsChoiceModelImpl.ENTITY_CACHE_ENABLED,
321                            PollsChoiceImpl.class, pollsChoice.getPrimaryKey(), pollsChoice);
322    
323                    if (!isNew &&
324                                    ((pollsChoice.getQuestionId() != pollsChoiceModelImpl.getOriginalQuestionId()) ||
325                                    !Validator.equals(pollsChoice.getName(),
326                                            pollsChoiceModelImpl.getOriginalName()))) {
327                            FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_Q_N,
328                                    new Object[] {
329                                            new Long(pollsChoiceModelImpl.getOriginalQuestionId()),
330                                            
331                                    pollsChoiceModelImpl.getOriginalName()
332                                    });
333                    }
334    
335                    if (isNew ||
336                                    ((pollsChoice.getQuestionId() != pollsChoiceModelImpl.getOriginalQuestionId()) ||
337                                    !Validator.equals(pollsChoice.getName(),
338                                            pollsChoiceModelImpl.getOriginalName()))) {
339                            FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_Q_N,
340                                    new Object[] {
341                                            new Long(pollsChoice.getQuestionId()),
342                                            
343                                    pollsChoice.getName()
344                                    }, pollsChoice);
345                    }
346    
347                    return pollsChoice;
348            }
349    
350            protected PollsChoice toUnwrappedModel(PollsChoice pollsChoice) {
351                    if (pollsChoice instanceof PollsChoiceImpl) {
352                            return pollsChoice;
353                    }
354    
355                    PollsChoiceImpl pollsChoiceImpl = new PollsChoiceImpl();
356    
357                    pollsChoiceImpl.setNew(pollsChoice.isNew());
358                    pollsChoiceImpl.setPrimaryKey(pollsChoice.getPrimaryKey());
359    
360                    pollsChoiceImpl.setUuid(pollsChoice.getUuid());
361                    pollsChoiceImpl.setChoiceId(pollsChoice.getChoiceId());
362                    pollsChoiceImpl.setQuestionId(pollsChoice.getQuestionId());
363                    pollsChoiceImpl.setName(pollsChoice.getName());
364                    pollsChoiceImpl.setDescription(pollsChoice.getDescription());
365    
366                    return pollsChoiceImpl;
367            }
368    
369            /**
370             * Finds the polls choice with the primary key or throws a {@link com.liferay.portal.NoSuchModelException} if it could not be found.
371             *
372             * @param primaryKey the primary key of the polls choice to find
373             * @return the polls choice
374             * @throws com.liferay.portal.NoSuchModelException if a polls choice with the primary key could not be found
375             * @throws SystemException if a system exception occurred
376             */
377            public PollsChoice findByPrimaryKey(Serializable primaryKey)
378                    throws NoSuchModelException, SystemException {
379                    return findByPrimaryKey(((Long)primaryKey).longValue());
380            }
381    
382            /**
383             * Finds the polls choice with the primary key or throws a {@link com.liferay.portlet.polls.NoSuchChoiceException} if it could not be found.
384             *
385             * @param choiceId the primary key of the polls choice to find
386             * @return the polls choice
387             * @throws com.liferay.portlet.polls.NoSuchChoiceException if a polls choice with the primary key could not be found
388             * @throws SystemException if a system exception occurred
389             */
390            public PollsChoice findByPrimaryKey(long choiceId)
391                    throws NoSuchChoiceException, SystemException {
392                    PollsChoice pollsChoice = fetchByPrimaryKey(choiceId);
393    
394                    if (pollsChoice == null) {
395                            if (_log.isWarnEnabled()) {
396                                    _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + choiceId);
397                            }
398    
399                            throw new NoSuchChoiceException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
400                                    choiceId);
401                    }
402    
403                    return pollsChoice;
404            }
405    
406            /**
407             * Finds the polls choice with the primary key or returns <code>null</code> if it could not be found.
408             *
409             * @param primaryKey the primary key of the polls choice to find
410             * @return the polls choice, or <code>null</code> if a polls choice with the primary key could not be found
411             * @throws SystemException if a system exception occurred
412             */
413            public PollsChoice fetchByPrimaryKey(Serializable primaryKey)
414                    throws SystemException {
415                    return fetchByPrimaryKey(((Long)primaryKey).longValue());
416            }
417    
418            /**
419             * Finds the polls choice with the primary key or returns <code>null</code> if it could not be found.
420             *
421             * @param choiceId the primary key of the polls choice to find
422             * @return the polls choice, or <code>null</code> if a polls choice with the primary key could not be found
423             * @throws SystemException if a system exception occurred
424             */
425            public PollsChoice fetchByPrimaryKey(long choiceId)
426                    throws SystemException {
427                    PollsChoice pollsChoice = (PollsChoice)EntityCacheUtil.getResult(PollsChoiceModelImpl.ENTITY_CACHE_ENABLED,
428                                    PollsChoiceImpl.class, choiceId, this);
429    
430                    if (pollsChoice == null) {
431                            Session session = null;
432    
433                            try {
434                                    session = openSession();
435    
436                                    pollsChoice = (PollsChoice)session.get(PollsChoiceImpl.class,
437                                                    new Long(choiceId));
438                            }
439                            catch (Exception e) {
440                                    throw processException(e);
441                            }
442                            finally {
443                                    if (pollsChoice != null) {
444                                            cacheResult(pollsChoice);
445                                    }
446    
447                                    closeSession(session);
448                            }
449                    }
450    
451                    return pollsChoice;
452            }
453    
454            /**
455             * Finds all the polls choices where uuid = &#63;.
456             *
457             * @param uuid the uuid to search with
458             * @return the matching polls choices
459             * @throws SystemException if a system exception occurred
460             */
461            public List<PollsChoice> findByUuid(String uuid) throws SystemException {
462                    return findByUuid(uuid, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
463            }
464    
465            /**
466             * Finds a range of all the polls choices where uuid = &#63;.
467             *
468             * <p>
469             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
470             * </p>
471             *
472             * @param uuid the uuid to search with
473             * @param start the lower bound of the range of polls choices to return
474             * @param end the upper bound of the range of polls choices to return (not inclusive)
475             * @return the range of matching polls choices
476             * @throws SystemException if a system exception occurred
477             */
478            public List<PollsChoice> findByUuid(String uuid, int start, int end)
479                    throws SystemException {
480                    return findByUuid(uuid, start, end, null);
481            }
482    
483            /**
484             * Finds an ordered range of all the polls choices where uuid = &#63;.
485             *
486             * <p>
487             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
488             * </p>
489             *
490             * @param uuid the uuid to search with
491             * @param start the lower bound of the range of polls choices to return
492             * @param end the upper bound of the range of polls choices to return (not inclusive)
493             * @param orderByComparator the comparator to order the results by
494             * @return the ordered range of matching polls choices
495             * @throws SystemException if a system exception occurred
496             */
497            public List<PollsChoice> findByUuid(String uuid, int start, int end,
498                    OrderByComparator orderByComparator) throws SystemException {
499                    Object[] finderArgs = new Object[] {
500                                    uuid,
501                                    
502                                    String.valueOf(start), String.valueOf(end),
503                                    String.valueOf(orderByComparator)
504                            };
505    
506                    List<PollsChoice> list = (List<PollsChoice>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_UUID,
507                                    finderArgs, this);
508    
509                    if (list == null) {
510                            StringBundler query = null;
511    
512                            if (orderByComparator != null) {
513                                    query = new StringBundler(3 +
514                                                    (orderByComparator.getOrderByFields().length * 3));
515                            }
516                            else {
517                                    query = new StringBundler(3);
518                            }
519    
520                            query.append(_SQL_SELECT_POLLSCHOICE_WHERE);
521    
522                            if (uuid == null) {
523                                    query.append(_FINDER_COLUMN_UUID_UUID_1);
524                            }
525                            else {
526                                    if (uuid.equals(StringPool.BLANK)) {
527                                            query.append(_FINDER_COLUMN_UUID_UUID_3);
528                                    }
529                                    else {
530                                            query.append(_FINDER_COLUMN_UUID_UUID_2);
531                                    }
532                            }
533    
534                            if (orderByComparator != null) {
535                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
536                                            orderByComparator);
537                            }
538    
539                            else {
540                                    query.append(PollsChoiceModelImpl.ORDER_BY_JPQL);
541                            }
542    
543                            String sql = query.toString();
544    
545                            Session session = null;
546    
547                            try {
548                                    session = openSession();
549    
550                                    Query q = session.createQuery(sql);
551    
552                                    QueryPos qPos = QueryPos.getInstance(q);
553    
554                                    if (uuid != null) {
555                                            qPos.add(uuid);
556                                    }
557    
558                                    list = (List<PollsChoice>)QueryUtil.list(q, getDialect(),
559                                                    start, end);
560                            }
561                            catch (Exception e) {
562                                    throw processException(e);
563                            }
564                            finally {
565                                    if (list == null) {
566                                            FinderCacheUtil.removeResult(FINDER_PATH_FIND_BY_UUID,
567                                                    finderArgs);
568                                    }
569                                    else {
570                                            cacheResult(list);
571    
572                                            FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_UUID,
573                                                    finderArgs, list);
574                                    }
575    
576                                    closeSession(session);
577                            }
578                    }
579    
580                    return list;
581            }
582    
583            /**
584             * Finds the first polls choice in the ordered set where uuid = &#63;.
585             *
586             * <p>
587             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
588             * </p>
589             *
590             * @param uuid the uuid to search with
591             * @param orderByComparator the comparator to order the set by
592             * @return the first matching polls choice
593             * @throws com.liferay.portlet.polls.NoSuchChoiceException if a matching polls choice could not be found
594             * @throws SystemException if a system exception occurred
595             */
596            public PollsChoice findByUuid_First(String uuid,
597                    OrderByComparator orderByComparator)
598                    throws NoSuchChoiceException, SystemException {
599                    List<PollsChoice> list = findByUuid(uuid, 0, 1, orderByComparator);
600    
601                    if (list.isEmpty()) {
602                            StringBundler msg = new StringBundler(4);
603    
604                            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
605    
606                            msg.append("uuid=");
607                            msg.append(uuid);
608    
609                            msg.append(StringPool.CLOSE_CURLY_BRACE);
610    
611                            throw new NoSuchChoiceException(msg.toString());
612                    }
613                    else {
614                            return list.get(0);
615                    }
616            }
617    
618            /**
619             * Finds the last polls choice in the ordered set where uuid = &#63;.
620             *
621             * <p>
622             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
623             * </p>
624             *
625             * @param uuid the uuid to search with
626             * @param orderByComparator the comparator to order the set by
627             * @return the last matching polls choice
628             * @throws com.liferay.portlet.polls.NoSuchChoiceException if a matching polls choice could not be found
629             * @throws SystemException if a system exception occurred
630             */
631            public PollsChoice findByUuid_Last(String uuid,
632                    OrderByComparator orderByComparator)
633                    throws NoSuchChoiceException, SystemException {
634                    int count = countByUuid(uuid);
635    
636                    List<PollsChoice> list = findByUuid(uuid, count - 1, count,
637                                    orderByComparator);
638    
639                    if (list.isEmpty()) {
640                            StringBundler msg = new StringBundler(4);
641    
642                            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
643    
644                            msg.append("uuid=");
645                            msg.append(uuid);
646    
647                            msg.append(StringPool.CLOSE_CURLY_BRACE);
648    
649                            throw new NoSuchChoiceException(msg.toString());
650                    }
651                    else {
652                            return list.get(0);
653                    }
654            }
655    
656            /**
657             * Finds the polls choices before and after the current polls choice in the ordered set where uuid = &#63;.
658             *
659             * <p>
660             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
661             * </p>
662             *
663             * @param choiceId the primary key of the current polls choice
664             * @param uuid the uuid to search with
665             * @param orderByComparator the comparator to order the set by
666             * @return the previous, current, and next polls choice
667             * @throws com.liferay.portlet.polls.NoSuchChoiceException if a polls choice with the primary key could not be found
668             * @throws SystemException if a system exception occurred
669             */
670            public PollsChoice[] findByUuid_PrevAndNext(long choiceId, String uuid,
671                    OrderByComparator orderByComparator)
672                    throws NoSuchChoiceException, SystemException {
673                    PollsChoice pollsChoice = findByPrimaryKey(choiceId);
674    
675                    Session session = null;
676    
677                    try {
678                            session = openSession();
679    
680                            PollsChoice[] array = new PollsChoiceImpl[3];
681    
682                            array[0] = getByUuid_PrevAndNext(session, pollsChoice, uuid,
683                                            orderByComparator, true);
684    
685                            array[1] = pollsChoice;
686    
687                            array[2] = getByUuid_PrevAndNext(session, pollsChoice, uuid,
688                                            orderByComparator, false);
689    
690                            return array;
691                    }
692                    catch (Exception e) {
693                            throw processException(e);
694                    }
695                    finally {
696                            closeSession(session);
697                    }
698            }
699    
700            protected PollsChoice getByUuid_PrevAndNext(Session session,
701                    PollsChoice pollsChoice, String uuid,
702                    OrderByComparator orderByComparator, boolean previous) {
703                    StringBundler query = null;
704    
705                    if (orderByComparator != null) {
706                            query = new StringBundler(6 +
707                                            (orderByComparator.getOrderByFields().length * 6));
708                    }
709                    else {
710                            query = new StringBundler(3);
711                    }
712    
713                    query.append(_SQL_SELECT_POLLSCHOICE_WHERE);
714    
715                    if (uuid == null) {
716                            query.append(_FINDER_COLUMN_UUID_UUID_1);
717                    }
718                    else {
719                            if (uuid.equals(StringPool.BLANK)) {
720                                    query.append(_FINDER_COLUMN_UUID_UUID_3);
721                            }
722                            else {
723                                    query.append(_FINDER_COLUMN_UUID_UUID_2);
724                            }
725                    }
726    
727                    if (orderByComparator != null) {
728                            String[] orderByFields = orderByComparator.getOrderByFields();
729    
730                            if (orderByFields.length > 0) {
731                                    query.append(WHERE_AND);
732                            }
733    
734                            for (int i = 0; i < orderByFields.length; i++) {
735                                    query.append(_ORDER_BY_ENTITY_ALIAS);
736                                    query.append(orderByFields[i]);
737    
738                                    if ((i + 1) < orderByFields.length) {
739                                            if (orderByComparator.isAscending() ^ previous) {
740                                                    query.append(WHERE_GREATER_THAN_HAS_NEXT);
741                                            }
742                                            else {
743                                                    query.append(WHERE_LESSER_THAN_HAS_NEXT);
744                                            }
745                                    }
746                                    else {
747                                            if (orderByComparator.isAscending() ^ previous) {
748                                                    query.append(WHERE_GREATER_THAN);
749                                            }
750                                            else {
751                                                    query.append(WHERE_LESSER_THAN);
752                                            }
753                                    }
754                            }
755    
756                            query.append(ORDER_BY_CLAUSE);
757    
758                            for (int i = 0; i < orderByFields.length; i++) {
759                                    query.append(_ORDER_BY_ENTITY_ALIAS);
760                                    query.append(orderByFields[i]);
761    
762                                    if ((i + 1) < orderByFields.length) {
763                                            if (orderByComparator.isAscending() ^ previous) {
764                                                    query.append(ORDER_BY_ASC_HAS_NEXT);
765                                            }
766                                            else {
767                                                    query.append(ORDER_BY_DESC_HAS_NEXT);
768                                            }
769                                    }
770                                    else {
771                                            if (orderByComparator.isAscending() ^ previous) {
772                                                    query.append(ORDER_BY_ASC);
773                                            }
774                                            else {
775                                                    query.append(ORDER_BY_DESC);
776                                            }
777                                    }
778                            }
779                    }
780    
781                    else {
782                            query.append(PollsChoiceModelImpl.ORDER_BY_JPQL);
783                    }
784    
785                    String sql = query.toString();
786    
787                    Query q = session.createQuery(sql);
788    
789                    q.setFirstResult(0);
790                    q.setMaxResults(2);
791    
792                    QueryPos qPos = QueryPos.getInstance(q);
793    
794                    if (uuid != null) {
795                            qPos.add(uuid);
796                    }
797    
798                    if (orderByComparator != null) {
799                            Object[] values = orderByComparator.getOrderByValues(pollsChoice);
800    
801                            for (Object value : values) {
802                                    qPos.add(value);
803                            }
804                    }
805    
806                    List<PollsChoice> list = q.list();
807    
808                    if (list.size() == 2) {
809                            return list.get(1);
810                    }
811                    else {
812                            return null;
813                    }
814            }
815    
816            /**
817             * Finds all the polls choices where questionId = &#63;.
818             *
819             * @param questionId the question id to search with
820             * @return the matching polls choices
821             * @throws SystemException if a system exception occurred
822             */
823            public List<PollsChoice> findByQuestionId(long questionId)
824                    throws SystemException {
825                    return findByQuestionId(questionId, QueryUtil.ALL_POS,
826                            QueryUtil.ALL_POS, null);
827            }
828    
829            /**
830             * Finds a range of all the polls choices where questionId = &#63;.
831             *
832             * <p>
833             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
834             * </p>
835             *
836             * @param questionId the question id to search with
837             * @param start the lower bound of the range of polls choices to return
838             * @param end the upper bound of the range of polls choices to return (not inclusive)
839             * @return the range of matching polls choices
840             * @throws SystemException if a system exception occurred
841             */
842            public List<PollsChoice> findByQuestionId(long questionId, int start,
843                    int end) throws SystemException {
844                    return findByQuestionId(questionId, start, end, null);
845            }
846    
847            /**
848             * Finds an ordered range of all the polls choices where questionId = &#63;.
849             *
850             * <p>
851             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
852             * </p>
853             *
854             * @param questionId the question id to search with
855             * @param start the lower bound of the range of polls choices to return
856             * @param end the upper bound of the range of polls choices to return (not inclusive)
857             * @param orderByComparator the comparator to order the results by
858             * @return the ordered range of matching polls choices
859             * @throws SystemException if a system exception occurred
860             */
861            public List<PollsChoice> findByQuestionId(long questionId, int start,
862                    int end, OrderByComparator orderByComparator) throws SystemException {
863                    Object[] finderArgs = new Object[] {
864                                    questionId,
865                                    
866                                    String.valueOf(start), String.valueOf(end),
867                                    String.valueOf(orderByComparator)
868                            };
869    
870                    List<PollsChoice> list = (List<PollsChoice>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_QUESTIONID,
871                                    finderArgs, this);
872    
873                    if (list == null) {
874                            StringBundler query = null;
875    
876                            if (orderByComparator != null) {
877                                    query = new StringBundler(3 +
878                                                    (orderByComparator.getOrderByFields().length * 3));
879                            }
880                            else {
881                                    query = new StringBundler(3);
882                            }
883    
884                            query.append(_SQL_SELECT_POLLSCHOICE_WHERE);
885    
886                            query.append(_FINDER_COLUMN_QUESTIONID_QUESTIONID_2);
887    
888                            if (orderByComparator != null) {
889                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
890                                            orderByComparator);
891                            }
892    
893                            else {
894                                    query.append(PollsChoiceModelImpl.ORDER_BY_JPQL);
895                            }
896    
897                            String sql = query.toString();
898    
899                            Session session = null;
900    
901                            try {
902                                    session = openSession();
903    
904                                    Query q = session.createQuery(sql);
905    
906                                    QueryPos qPos = QueryPos.getInstance(q);
907    
908                                    qPos.add(questionId);
909    
910                                    list = (List<PollsChoice>)QueryUtil.list(q, getDialect(),
911                                                    start, end);
912                            }
913                            catch (Exception e) {
914                                    throw processException(e);
915                            }
916                            finally {
917                                    if (list == null) {
918                                            FinderCacheUtil.removeResult(FINDER_PATH_FIND_BY_QUESTIONID,
919                                                    finderArgs);
920                                    }
921                                    else {
922                                            cacheResult(list);
923    
924                                            FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_QUESTIONID,
925                                                    finderArgs, list);
926                                    }
927    
928                                    closeSession(session);
929                            }
930                    }
931    
932                    return list;
933            }
934    
935            /**
936             * Finds the first polls choice in the ordered set where questionId = &#63;.
937             *
938             * <p>
939             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
940             * </p>
941             *
942             * @param questionId the question id to search with
943             * @param orderByComparator the comparator to order the set by
944             * @return the first matching polls choice
945             * @throws com.liferay.portlet.polls.NoSuchChoiceException if a matching polls choice could not be found
946             * @throws SystemException if a system exception occurred
947             */
948            public PollsChoice findByQuestionId_First(long questionId,
949                    OrderByComparator orderByComparator)
950                    throws NoSuchChoiceException, SystemException {
951                    List<PollsChoice> list = findByQuestionId(questionId, 0, 1,
952                                    orderByComparator);
953    
954                    if (list.isEmpty()) {
955                            StringBundler msg = new StringBundler(4);
956    
957                            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
958    
959                            msg.append("questionId=");
960                            msg.append(questionId);
961    
962                            msg.append(StringPool.CLOSE_CURLY_BRACE);
963    
964                            throw new NoSuchChoiceException(msg.toString());
965                    }
966                    else {
967                            return list.get(0);
968                    }
969            }
970    
971            /**
972             * Finds the last polls choice in the ordered set where questionId = &#63;.
973             *
974             * <p>
975             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
976             * </p>
977             *
978             * @param questionId the question id to search with
979             * @param orderByComparator the comparator to order the set by
980             * @return the last matching polls choice
981             * @throws com.liferay.portlet.polls.NoSuchChoiceException if a matching polls choice could not be found
982             * @throws SystemException if a system exception occurred
983             */
984            public PollsChoice findByQuestionId_Last(long questionId,
985                    OrderByComparator orderByComparator)
986                    throws NoSuchChoiceException, SystemException {
987                    int count = countByQuestionId(questionId);
988    
989                    List<PollsChoice> list = findByQuestionId(questionId, count - 1, count,
990                                    orderByComparator);
991    
992                    if (list.isEmpty()) {
993                            StringBundler msg = new StringBundler(4);
994    
995                            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
996    
997                            msg.append("questionId=");
998                            msg.append(questionId);
999    
1000                            msg.append(StringPool.CLOSE_CURLY_BRACE);
1001    
1002                            throw new NoSuchChoiceException(msg.toString());
1003                    }
1004                    else {
1005                            return list.get(0);
1006                    }
1007            }
1008    
1009            /**
1010             * Finds the polls choices before and after the current polls choice in the ordered set where questionId = &#63;.
1011             *
1012             * <p>
1013             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
1014             * </p>
1015             *
1016             * @param choiceId the primary key of the current polls choice
1017             * @param questionId the question id to search with
1018             * @param orderByComparator the comparator to order the set by
1019             * @return the previous, current, and next polls choice
1020             * @throws com.liferay.portlet.polls.NoSuchChoiceException if a polls choice with the primary key could not be found
1021             * @throws SystemException if a system exception occurred
1022             */
1023            public PollsChoice[] findByQuestionId_PrevAndNext(long choiceId,
1024                    long questionId, OrderByComparator orderByComparator)
1025                    throws NoSuchChoiceException, SystemException {
1026                    PollsChoice pollsChoice = findByPrimaryKey(choiceId);
1027    
1028                    Session session = null;
1029    
1030                    try {
1031                            session = openSession();
1032    
1033                            PollsChoice[] array = new PollsChoiceImpl[3];
1034    
1035                            array[0] = getByQuestionId_PrevAndNext(session, pollsChoice,
1036                                            questionId, orderByComparator, true);
1037    
1038                            array[1] = pollsChoice;
1039    
1040                            array[2] = getByQuestionId_PrevAndNext(session, pollsChoice,
1041                                            questionId, orderByComparator, false);
1042    
1043                            return array;
1044                    }
1045                    catch (Exception e) {
1046                            throw processException(e);
1047                    }
1048                    finally {
1049                            closeSession(session);
1050                    }
1051            }
1052    
1053            protected PollsChoice getByQuestionId_PrevAndNext(Session session,
1054                    PollsChoice pollsChoice, long questionId,
1055                    OrderByComparator orderByComparator, boolean previous) {
1056                    StringBundler query = null;
1057    
1058                    if (orderByComparator != null) {
1059                            query = new StringBundler(6 +
1060                                            (orderByComparator.getOrderByFields().length * 6));
1061                    }
1062                    else {
1063                            query = new StringBundler(3);
1064                    }
1065    
1066                    query.append(_SQL_SELECT_POLLSCHOICE_WHERE);
1067    
1068                    query.append(_FINDER_COLUMN_QUESTIONID_QUESTIONID_2);
1069    
1070                    if (orderByComparator != null) {
1071                            String[] orderByFields = orderByComparator.getOrderByFields();
1072    
1073                            if (orderByFields.length > 0) {
1074                                    query.append(WHERE_AND);
1075                            }
1076    
1077                            for (int i = 0; i < orderByFields.length; i++) {
1078                                    query.append(_ORDER_BY_ENTITY_ALIAS);
1079                                    query.append(orderByFields[i]);
1080    
1081                                    if ((i + 1) < orderByFields.length) {
1082                                            if (orderByComparator.isAscending() ^ previous) {
1083                                                    query.append(WHERE_GREATER_THAN_HAS_NEXT);
1084                                            }
1085                                            else {
1086                                                    query.append(WHERE_LESSER_THAN_HAS_NEXT);
1087                                            }
1088                                    }
1089                                    else {
1090                                            if (orderByComparator.isAscending() ^ previous) {
1091                                                    query.append(WHERE_GREATER_THAN);
1092                                            }
1093                                            else {
1094                                                    query.append(WHERE_LESSER_THAN);
1095                                            }
1096                                    }
1097                            }
1098    
1099                            query.append(ORDER_BY_CLAUSE);
1100    
1101                            for (int i = 0; i < orderByFields.length; i++) {
1102                                    query.append(_ORDER_BY_ENTITY_ALIAS);
1103                                    query.append(orderByFields[i]);
1104    
1105                                    if ((i + 1) < orderByFields.length) {
1106                                            if (orderByComparator.isAscending() ^ previous) {
1107                                                    query.append(ORDER_BY_ASC_HAS_NEXT);
1108                                            }
1109                                            else {
1110                                                    query.append(ORDER_BY_DESC_HAS_NEXT);
1111                                            }
1112                                    }
1113                                    else {
1114                                            if (orderByComparator.isAscending() ^ previous) {
1115                                                    query.append(ORDER_BY_ASC);
1116                                            }
1117                                            else {
1118                                                    query.append(ORDER_BY_DESC);
1119                                            }
1120                                    }
1121                            }
1122                    }
1123    
1124                    else {
1125                            query.append(PollsChoiceModelImpl.ORDER_BY_JPQL);
1126                    }
1127    
1128                    String sql = query.toString();
1129    
1130                    Query q = session.createQuery(sql);
1131    
1132                    q.setFirstResult(0);
1133                    q.setMaxResults(2);
1134    
1135                    QueryPos qPos = QueryPos.getInstance(q);
1136    
1137                    qPos.add(questionId);
1138    
1139                    if (orderByComparator != null) {
1140                            Object[] values = orderByComparator.getOrderByValues(pollsChoice);
1141    
1142                            for (Object value : values) {
1143                                    qPos.add(value);
1144                            }
1145                    }
1146    
1147                    List<PollsChoice> list = q.list();
1148    
1149                    if (list.size() == 2) {
1150                            return list.get(1);
1151                    }
1152                    else {
1153                            return null;
1154                    }
1155            }
1156    
1157            /**
1158             * Finds the polls choice where questionId = &#63; and name = &#63; or throws a {@link com.liferay.portlet.polls.NoSuchChoiceException} if it could not be found.
1159             *
1160             * @param questionId the question id to search with
1161             * @param name the name to search with
1162             * @return the matching polls choice
1163             * @throws com.liferay.portlet.polls.NoSuchChoiceException if a matching polls choice could not be found
1164             * @throws SystemException if a system exception occurred
1165             */
1166            public PollsChoice findByQ_N(long questionId, String name)
1167                    throws NoSuchChoiceException, SystemException {
1168                    PollsChoice pollsChoice = fetchByQ_N(questionId, name);
1169    
1170                    if (pollsChoice == null) {
1171                            StringBundler msg = new StringBundler(6);
1172    
1173                            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
1174    
1175                            msg.append("questionId=");
1176                            msg.append(questionId);
1177    
1178                            msg.append(", name=");
1179                            msg.append(name);
1180    
1181                            msg.append(StringPool.CLOSE_CURLY_BRACE);
1182    
1183                            if (_log.isWarnEnabled()) {
1184                                    _log.warn(msg.toString());
1185                            }
1186    
1187                            throw new NoSuchChoiceException(msg.toString());
1188                    }
1189    
1190                    return pollsChoice;
1191            }
1192    
1193            /**
1194             * Finds the polls choice where questionId = &#63; and name = &#63; or returns <code>null</code> if it could not be found. Uses the finder cache.
1195             *
1196             * @param questionId the question id to search with
1197             * @param name the name to search with
1198             * @return the matching polls choice, or <code>null</code> if a matching polls choice could not be found
1199             * @throws SystemException if a system exception occurred
1200             */
1201            public PollsChoice fetchByQ_N(long questionId, String name)
1202                    throws SystemException {
1203                    return fetchByQ_N(questionId, name, true);
1204            }
1205    
1206            /**
1207             * Finds the polls choice where questionId = &#63; and name = &#63; or returns <code>null</code> if it could not be found, optionally using the finder cache.
1208             *
1209             * @param questionId the question id to search with
1210             * @param name the name to search with
1211             * @return the matching polls choice, or <code>null</code> if a matching polls choice could not be found
1212             * @throws SystemException if a system exception occurred
1213             */
1214            public PollsChoice fetchByQ_N(long questionId, String name,
1215                    boolean retrieveFromCache) throws SystemException {
1216                    Object[] finderArgs = new Object[] { questionId, name };
1217    
1218                    Object result = null;
1219    
1220                    if (retrieveFromCache) {
1221                            result = FinderCacheUtil.getResult(FINDER_PATH_FETCH_BY_Q_N,
1222                                            finderArgs, this);
1223                    }
1224    
1225                    if (result == null) {
1226                            StringBundler query = new StringBundler(4);
1227    
1228                            query.append(_SQL_SELECT_POLLSCHOICE_WHERE);
1229    
1230                            query.append(_FINDER_COLUMN_Q_N_QUESTIONID_2);
1231    
1232                            if (name == null) {
1233                                    query.append(_FINDER_COLUMN_Q_N_NAME_1);
1234                            }
1235                            else {
1236                                    if (name.equals(StringPool.BLANK)) {
1237                                            query.append(_FINDER_COLUMN_Q_N_NAME_3);
1238                                    }
1239                                    else {
1240                                            query.append(_FINDER_COLUMN_Q_N_NAME_2);
1241                                    }
1242                            }
1243    
1244                            query.append(PollsChoiceModelImpl.ORDER_BY_JPQL);
1245    
1246                            String sql = query.toString();
1247    
1248                            Session session = null;
1249    
1250                            try {
1251                                    session = openSession();
1252    
1253                                    Query q = session.createQuery(sql);
1254    
1255                                    QueryPos qPos = QueryPos.getInstance(q);
1256    
1257                                    qPos.add(questionId);
1258    
1259                                    if (name != null) {
1260                                            qPos.add(name);
1261                                    }
1262    
1263                                    List<PollsChoice> list = q.list();
1264    
1265                                    result = list;
1266    
1267                                    PollsChoice pollsChoice = null;
1268    
1269                                    if (list.isEmpty()) {
1270                                            FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_Q_N,
1271                                                    finderArgs, list);
1272                                    }
1273                                    else {
1274                                            pollsChoice = list.get(0);
1275    
1276                                            cacheResult(pollsChoice);
1277    
1278                                            if ((pollsChoice.getQuestionId() != questionId) ||
1279                                                            (pollsChoice.getName() == null) ||
1280                                                            !pollsChoice.getName().equals(name)) {
1281                                                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_Q_N,
1282                                                            finderArgs, pollsChoice);
1283                                            }
1284                                    }
1285    
1286                                    return pollsChoice;
1287                            }
1288                            catch (Exception e) {
1289                                    throw processException(e);
1290                            }
1291                            finally {
1292                                    if (result == null) {
1293                                            FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_Q_N,
1294                                                    finderArgs);
1295                                    }
1296    
1297                                    closeSession(session);
1298                            }
1299                    }
1300                    else {
1301                            if (result instanceof List<?>) {
1302                                    return null;
1303                            }
1304                            else {
1305                                    return (PollsChoice)result;
1306                            }
1307                    }
1308            }
1309    
1310            /**
1311             * Finds all the polls choices.
1312             *
1313             * @return the polls choices
1314             * @throws SystemException if a system exception occurred
1315             */
1316            public List<PollsChoice> findAll() throws SystemException {
1317                    return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
1318            }
1319    
1320            /**
1321             * Finds a range of all the polls choices.
1322             *
1323             * <p>
1324             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
1325             * </p>
1326             *
1327             * @param start the lower bound of the range of polls choices to return
1328             * @param end the upper bound of the range of polls choices to return (not inclusive)
1329             * @return the range of polls choices
1330             * @throws SystemException if a system exception occurred
1331             */
1332            public List<PollsChoice> findAll(int start, int end)
1333                    throws SystemException {
1334                    return findAll(start, end, null);
1335            }
1336    
1337            /**
1338             * Finds an ordered range of all the polls choices.
1339             *
1340             * <p>
1341             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
1342             * </p>
1343             *
1344             * @param start the lower bound of the range of polls choices to return
1345             * @param end the upper bound of the range of polls choices to return (not inclusive)
1346             * @param orderByComparator the comparator to order the results by
1347             * @return the ordered range of polls choices
1348             * @throws SystemException if a system exception occurred
1349             */
1350            public List<PollsChoice> findAll(int start, int end,
1351                    OrderByComparator orderByComparator) throws SystemException {
1352                    Object[] finderArgs = new Object[] {
1353                                    String.valueOf(start), String.valueOf(end),
1354                                    String.valueOf(orderByComparator)
1355                            };
1356    
1357                    List<PollsChoice> list = (List<PollsChoice>)FinderCacheUtil.getResult(FINDER_PATH_FIND_ALL,
1358                                    finderArgs, this);
1359    
1360                    if (list == null) {
1361                            StringBundler query = null;
1362                            String sql = null;
1363    
1364                            if (orderByComparator != null) {
1365                                    query = new StringBundler(2 +
1366                                                    (orderByComparator.getOrderByFields().length * 3));
1367    
1368                                    query.append(_SQL_SELECT_POLLSCHOICE);
1369    
1370                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
1371                                            orderByComparator);
1372    
1373                                    sql = query.toString();
1374                            }
1375                            else {
1376                                    sql = _SQL_SELECT_POLLSCHOICE.concat(PollsChoiceModelImpl.ORDER_BY_JPQL);
1377                            }
1378    
1379                            Session session = null;
1380    
1381                            try {
1382                                    session = openSession();
1383    
1384                                    Query q = session.createQuery(sql);
1385    
1386                                    if (orderByComparator == null) {
1387                                            list = (List<PollsChoice>)QueryUtil.list(q, getDialect(),
1388                                                            start, end, false);
1389    
1390                                            Collections.sort(list);
1391                                    }
1392                                    else {
1393                                            list = (List<PollsChoice>)QueryUtil.list(q, getDialect(),
1394                                                            start, end);
1395                                    }
1396                            }
1397                            catch (Exception e) {
1398                                    throw processException(e);
1399                            }
1400                            finally {
1401                                    if (list == null) {
1402                                            FinderCacheUtil.removeResult(FINDER_PATH_FIND_ALL,
1403                                                    finderArgs);
1404                                    }
1405                                    else {
1406                                            cacheResult(list);
1407    
1408                                            FinderCacheUtil.putResult(FINDER_PATH_FIND_ALL, finderArgs,
1409                                                    list);
1410                                    }
1411    
1412                                    closeSession(session);
1413                            }
1414                    }
1415    
1416                    return list;
1417            }
1418    
1419            /**
1420             * Removes all the polls choices where uuid = &#63; from the database.
1421             *
1422             * @param uuid the uuid to search with
1423             * @throws SystemException if a system exception occurred
1424             */
1425            public void removeByUuid(String uuid) throws SystemException {
1426                    for (PollsChoice pollsChoice : findByUuid(uuid)) {
1427                            remove(pollsChoice);
1428                    }
1429            }
1430    
1431            /**
1432             * Removes all the polls choices where questionId = &#63; from the database.
1433             *
1434             * @param questionId the question id to search with
1435             * @throws SystemException if a system exception occurred
1436             */
1437            public void removeByQuestionId(long questionId) throws SystemException {
1438                    for (PollsChoice pollsChoice : findByQuestionId(questionId)) {
1439                            remove(pollsChoice);
1440                    }
1441            }
1442    
1443            /**
1444             * Removes the polls choice where questionId = &#63; and name = &#63; from the database.
1445             *
1446             * @param questionId the question id to search with
1447             * @param name the name to search with
1448             * @throws SystemException if a system exception occurred
1449             */
1450            public void removeByQ_N(long questionId, String name)
1451                    throws NoSuchChoiceException, SystemException {
1452                    PollsChoice pollsChoice = findByQ_N(questionId, name);
1453    
1454                    remove(pollsChoice);
1455            }
1456    
1457            /**
1458             * Removes all the polls choices from the database.
1459             *
1460             * @throws SystemException if a system exception occurred
1461             */
1462            public void removeAll() throws SystemException {
1463                    for (PollsChoice pollsChoice : findAll()) {
1464                            remove(pollsChoice);
1465                    }
1466            }
1467    
1468            /**
1469             * Counts all the polls choices where uuid = &#63;.
1470             *
1471             * @param uuid the uuid to search with
1472             * @return the number of matching polls choices
1473             * @throws SystemException if a system exception occurred
1474             */
1475            public int countByUuid(String uuid) throws SystemException {
1476                    Object[] finderArgs = new Object[] { uuid };
1477    
1478                    Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_UUID,
1479                                    finderArgs, this);
1480    
1481                    if (count == null) {
1482                            StringBundler query = new StringBundler(2);
1483    
1484                            query.append(_SQL_COUNT_POLLSCHOICE_WHERE);
1485    
1486                            if (uuid == null) {
1487                                    query.append(_FINDER_COLUMN_UUID_UUID_1);
1488                            }
1489                            else {
1490                                    if (uuid.equals(StringPool.BLANK)) {
1491                                            query.append(_FINDER_COLUMN_UUID_UUID_3);
1492                                    }
1493                                    else {
1494                                            query.append(_FINDER_COLUMN_UUID_UUID_2);
1495                                    }
1496                            }
1497    
1498                            String sql = query.toString();
1499    
1500                            Session session = null;
1501    
1502                            try {
1503                                    session = openSession();
1504    
1505                                    Query q = session.createQuery(sql);
1506    
1507                                    QueryPos qPos = QueryPos.getInstance(q);
1508    
1509                                    if (uuid != null) {
1510                                            qPos.add(uuid);
1511                                    }
1512    
1513                                    count = (Long)q.uniqueResult();
1514                            }
1515                            catch (Exception e) {
1516                                    throw processException(e);
1517                            }
1518                            finally {
1519                                    if (count == null) {
1520                                            count = Long.valueOf(0);
1521                                    }
1522    
1523                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_UUID,
1524                                            finderArgs, count);
1525    
1526                                    closeSession(session);
1527                            }
1528                    }
1529    
1530                    return count.intValue();
1531            }
1532    
1533            /**
1534             * Counts all the polls choices where questionId = &#63;.
1535             *
1536             * @param questionId the question id to search with
1537             * @return the number of matching polls choices
1538             * @throws SystemException if a system exception occurred
1539             */
1540            public int countByQuestionId(long questionId) throws SystemException {
1541                    Object[] finderArgs = new Object[] { questionId };
1542    
1543                    Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_QUESTIONID,
1544                                    finderArgs, this);
1545    
1546                    if (count == null) {
1547                            StringBundler query = new StringBundler(2);
1548    
1549                            query.append(_SQL_COUNT_POLLSCHOICE_WHERE);
1550    
1551                            query.append(_FINDER_COLUMN_QUESTIONID_QUESTIONID_2);
1552    
1553                            String sql = query.toString();
1554    
1555                            Session session = null;
1556    
1557                            try {
1558                                    session = openSession();
1559    
1560                                    Query q = session.createQuery(sql);
1561    
1562                                    QueryPos qPos = QueryPos.getInstance(q);
1563    
1564                                    qPos.add(questionId);
1565    
1566                                    count = (Long)q.uniqueResult();
1567                            }
1568                            catch (Exception e) {
1569                                    throw processException(e);
1570                            }
1571                            finally {
1572                                    if (count == null) {
1573                                            count = Long.valueOf(0);
1574                                    }
1575    
1576                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_QUESTIONID,
1577                                            finderArgs, count);
1578    
1579                                    closeSession(session);
1580                            }
1581                    }
1582    
1583                    return count.intValue();
1584            }
1585    
1586            /**
1587             * Counts all the polls choices where questionId = &#63; and name = &#63;.
1588             *
1589             * @param questionId the question id to search with
1590             * @param name the name to search with
1591             * @return the number of matching polls choices
1592             * @throws SystemException if a system exception occurred
1593             */
1594            public int countByQ_N(long questionId, String name)
1595                    throws SystemException {
1596                    Object[] finderArgs = new Object[] { questionId, name };
1597    
1598                    Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_Q_N,
1599                                    finderArgs, this);
1600    
1601                    if (count == null) {
1602                            StringBundler query = new StringBundler(3);
1603    
1604                            query.append(_SQL_COUNT_POLLSCHOICE_WHERE);
1605    
1606                            query.append(_FINDER_COLUMN_Q_N_QUESTIONID_2);
1607    
1608                            if (name == null) {
1609                                    query.append(_FINDER_COLUMN_Q_N_NAME_1);
1610                            }
1611                            else {
1612                                    if (name.equals(StringPool.BLANK)) {
1613                                            query.append(_FINDER_COLUMN_Q_N_NAME_3);
1614                                    }
1615                                    else {
1616                                            query.append(_FINDER_COLUMN_Q_N_NAME_2);
1617                                    }
1618                            }
1619    
1620                            String sql = query.toString();
1621    
1622                            Session session = null;
1623    
1624                            try {
1625                                    session = openSession();
1626    
1627                                    Query q = session.createQuery(sql);
1628    
1629                                    QueryPos qPos = QueryPos.getInstance(q);
1630    
1631                                    qPos.add(questionId);
1632    
1633                                    if (name != null) {
1634                                            qPos.add(name);
1635                                    }
1636    
1637                                    count = (Long)q.uniqueResult();
1638                            }
1639                            catch (Exception e) {
1640                                    throw processException(e);
1641                            }
1642                            finally {
1643                                    if (count == null) {
1644                                            count = Long.valueOf(0);
1645                                    }
1646    
1647                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_Q_N, finderArgs,
1648                                            count);
1649    
1650                                    closeSession(session);
1651                            }
1652                    }
1653    
1654                    return count.intValue();
1655            }
1656    
1657            /**
1658             * Counts all the polls choices.
1659             *
1660             * @return the number of polls choices
1661             * @throws SystemException if a system exception occurred
1662             */
1663            public int countAll() throws SystemException {
1664                    Object[] finderArgs = new Object[0];
1665    
1666                    Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
1667                                    finderArgs, this);
1668    
1669                    if (count == null) {
1670                            Session session = null;
1671    
1672                            try {
1673                                    session = openSession();
1674    
1675                                    Query q = session.createQuery(_SQL_COUNT_POLLSCHOICE);
1676    
1677                                    count = (Long)q.uniqueResult();
1678                            }
1679                            catch (Exception e) {
1680                                    throw processException(e);
1681                            }
1682                            finally {
1683                                    if (count == null) {
1684                                            count = Long.valueOf(0);
1685                                    }
1686    
1687                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL, finderArgs,
1688                                            count);
1689    
1690                                    closeSession(session);
1691                            }
1692                    }
1693    
1694                    return count.intValue();
1695            }
1696    
1697            /**
1698             * Initializes the polls choice persistence.
1699             */
1700            public void afterPropertiesSet() {
1701                    String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
1702                                            com.liferay.portal.util.PropsUtil.get(
1703                                                    "value.object.listener.com.liferay.portlet.polls.model.PollsChoice")));
1704    
1705                    if (listenerClassNames.length > 0) {
1706                            try {
1707                                    List<ModelListener<PollsChoice>> listenersList = new ArrayList<ModelListener<PollsChoice>>();
1708    
1709                                    for (String listenerClassName : listenerClassNames) {
1710                                            listenersList.add((ModelListener<PollsChoice>)InstanceFactory.newInstance(
1711                                                            listenerClassName));
1712                                    }
1713    
1714                                    listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
1715                            }
1716                            catch (Exception e) {
1717                                    _log.error(e);
1718                            }
1719                    }
1720            }
1721    
1722            public void destroy() {
1723                    EntityCacheUtil.removeCache(PollsChoiceImpl.class.getName());
1724                    FinderCacheUtil.removeCache(FINDER_CLASS_NAME_ENTITY);
1725                    FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST);
1726            }
1727    
1728            @BeanReference(type = PollsChoicePersistence.class)
1729            protected PollsChoicePersistence pollsChoicePersistence;
1730            @BeanReference(type = PollsQuestionPersistence.class)
1731            protected PollsQuestionPersistence pollsQuestionPersistence;
1732            @BeanReference(type = PollsVotePersistence.class)
1733            protected PollsVotePersistence pollsVotePersistence;
1734            @BeanReference(type = ResourcePersistence.class)
1735            protected ResourcePersistence resourcePersistence;
1736            @BeanReference(type = UserPersistence.class)
1737            protected UserPersistence userPersistence;
1738            private static final String _SQL_SELECT_POLLSCHOICE = "SELECT pollsChoice FROM PollsChoice pollsChoice";
1739            private static final String _SQL_SELECT_POLLSCHOICE_WHERE = "SELECT pollsChoice FROM PollsChoice pollsChoice WHERE ";
1740            private static final String _SQL_COUNT_POLLSCHOICE = "SELECT COUNT(pollsChoice) FROM PollsChoice pollsChoice";
1741            private static final String _SQL_COUNT_POLLSCHOICE_WHERE = "SELECT COUNT(pollsChoice) FROM PollsChoice pollsChoice WHERE ";
1742            private static final String _FINDER_COLUMN_UUID_UUID_1 = "pollsChoice.uuid IS NULL";
1743            private static final String _FINDER_COLUMN_UUID_UUID_2 = "pollsChoice.uuid = ?";
1744            private static final String _FINDER_COLUMN_UUID_UUID_3 = "(pollsChoice.uuid IS NULL OR pollsChoice.uuid = ?)";
1745            private static final String _FINDER_COLUMN_QUESTIONID_QUESTIONID_2 = "pollsChoice.questionId = ?";
1746            private static final String _FINDER_COLUMN_Q_N_QUESTIONID_2 = "pollsChoice.questionId = ? AND ";
1747            private static final String _FINDER_COLUMN_Q_N_NAME_1 = "pollsChoice.name IS NULL";
1748            private static final String _FINDER_COLUMN_Q_N_NAME_2 = "pollsChoice.name = ?";
1749            private static final String _FINDER_COLUMN_Q_N_NAME_3 = "(pollsChoice.name IS NULL OR pollsChoice.name = ?)";
1750            private static final String _ORDER_BY_ENTITY_ALIAS = "pollsChoice.";
1751            private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No PollsChoice exists with the primary key ";
1752            private static final String _NO_SUCH_ENTITY_WITH_KEY = "No PollsChoice exists with the key {";
1753            private static Log _log = LogFactoryUtil.getLog(PollsChoicePersistenceImpl.class);
1754    }