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