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