001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.polls.service.persistence;
016    
017    import com.liferay.portal.NoSuchModelException;
018    import com.liferay.portal.kernel.annotation.BeanReference;
019    import com.liferay.portal.kernel.cache.CacheRegistryUtil;
020    import com.liferay.portal.kernel.dao.orm.EntityCacheUtil;
021    import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
022    import com.liferay.portal.kernel.dao.orm.FinderPath;
023    import com.liferay.portal.kernel.dao.orm.Query;
024    import com.liferay.portal.kernel.dao.orm.QueryPos;
025    import com.liferay.portal.kernel.dao.orm.QueryUtil;
026    import com.liferay.portal.kernel.dao.orm.Session;
027    import com.liferay.portal.kernel.exception.SystemException;
028    import com.liferay.portal.kernel.log.Log;
029    import com.liferay.portal.kernel.log.LogFactoryUtil;
030    import com.liferay.portal.kernel.util.GetterUtil;
031    import com.liferay.portal.kernel.util.InstanceFactory;
032    import com.liferay.portal.kernel.util.OrderByComparator;
033    import com.liferay.portal.kernel.util.StringBundler;
034    import com.liferay.portal.kernel.util.StringPool;
035    import com.liferay.portal.kernel.util.StringUtil;
036    import com.liferay.portal.model.ModelListener;
037    import com.liferay.portal.service.persistence.BatchSessionUtil;
038    import com.liferay.portal.service.persistence.ResourcePersistence;
039    import com.liferay.portal.service.persistence.UserPersistence;
040    import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
041    
042    import com.liferay.portlet.polls.NoSuchVoteException;
043    import com.liferay.portlet.polls.model.PollsVote;
044    import com.liferay.portlet.polls.model.impl.PollsVoteImpl;
045    import com.liferay.portlet.polls.model.impl.PollsVoteModelImpl;
046    
047    import java.io.Serializable;
048    
049    import java.util.ArrayList;
050    import java.util.Collections;
051    import java.util.List;
052    
053    /**
054     * @author    Brian Wing Shun Chan
055     * @see       PollsVotePersistence
056     * @see       PollsVoteUtil
057     * @generated
058     */
059    public class PollsVotePersistenceImpl extends BasePersistenceImpl<PollsVote>
060            implements PollsVotePersistence {
061            public static final String FINDER_CLASS_NAME_ENTITY = PollsVoteImpl.class.getName();
062            public static final String FINDER_CLASS_NAME_LIST = FINDER_CLASS_NAME_ENTITY +
063                    ".List";
064            public static final FinderPath FINDER_PATH_FIND_BY_QUESTIONID = new FinderPath(PollsVoteModelImpl.ENTITY_CACHE_ENABLED,
065                            PollsVoteModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
066                            "findByQuestionId",
067                            new String[] {
068                                    Long.class.getName(),
069                                    
070                            "java.lang.Integer", "java.lang.Integer",
071                                    "com.liferay.portal.kernel.util.OrderByComparator"
072                            });
073            public static final FinderPath FINDER_PATH_COUNT_BY_QUESTIONID = new FinderPath(PollsVoteModelImpl.ENTITY_CACHE_ENABLED,
074                            PollsVoteModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
075                            "countByQuestionId", new String[] { Long.class.getName() });
076            public static final FinderPath FINDER_PATH_FIND_BY_CHOICEID = new FinderPath(PollsVoteModelImpl.ENTITY_CACHE_ENABLED,
077                            PollsVoteModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
078                            "findByChoiceId",
079                            new String[] {
080                                    Long.class.getName(),
081                                    
082                            "java.lang.Integer", "java.lang.Integer",
083                                    "com.liferay.portal.kernel.util.OrderByComparator"
084                            });
085            public static final FinderPath FINDER_PATH_COUNT_BY_CHOICEID = new FinderPath(PollsVoteModelImpl.ENTITY_CACHE_ENABLED,
086                            PollsVoteModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
087                            "countByChoiceId", new String[] { Long.class.getName() });
088            public static final FinderPath FINDER_PATH_FETCH_BY_Q_U = new FinderPath(PollsVoteModelImpl.ENTITY_CACHE_ENABLED,
089                            PollsVoteModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_ENTITY,
090                            "fetchByQ_U",
091                            new String[] { Long.class.getName(), Long.class.getName() });
092            public static final FinderPath FINDER_PATH_COUNT_BY_Q_U = new FinderPath(PollsVoteModelImpl.ENTITY_CACHE_ENABLED,
093                            PollsVoteModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
094                            "countByQ_U",
095                            new String[] { Long.class.getName(), Long.class.getName() });
096            public static final FinderPath FINDER_PATH_FIND_ALL = new FinderPath(PollsVoteModelImpl.ENTITY_CACHE_ENABLED,
097                            PollsVoteModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
098                            "findAll", new String[0]);
099            public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(PollsVoteModelImpl.ENTITY_CACHE_ENABLED,
100                            PollsVoteModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
101                            "countAll", new String[0]);
102    
103            public void cacheResult(PollsVote pollsVote) {
104                    EntityCacheUtil.putResult(PollsVoteModelImpl.ENTITY_CACHE_ENABLED,
105                            PollsVoteImpl.class, pollsVote.getPrimaryKey(), pollsVote);
106    
107                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_Q_U,
108                            new Object[] {
109                                    new Long(pollsVote.getQuestionId()),
110                                    new Long(pollsVote.getUserId())
111                            }, pollsVote);
112            }
113    
114            public void cacheResult(List<PollsVote> pollsVotes) {
115                    for (PollsVote pollsVote : pollsVotes) {
116                            if (EntityCacheUtil.getResult(
117                                                    PollsVoteModelImpl.ENTITY_CACHE_ENABLED,
118                                                    PollsVoteImpl.class, pollsVote.getPrimaryKey(), this) == null) {
119                                    cacheResult(pollsVote);
120                            }
121                    }
122            }
123    
124            public void clearCache() {
125                    CacheRegistryUtil.clear(PollsVoteImpl.class.getName());
126                    EntityCacheUtil.clearCache(PollsVoteImpl.class.getName());
127                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
128                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
129            }
130    
131            public void clearCache(PollsVote pollsVote) {
132                    EntityCacheUtil.removeResult(PollsVoteModelImpl.ENTITY_CACHE_ENABLED,
133                            PollsVoteImpl.class, pollsVote.getPrimaryKey());
134    
135                    FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_Q_U,
136                            new Object[] {
137                                    new Long(pollsVote.getQuestionId()),
138                                    new Long(pollsVote.getUserId())
139                            });
140            }
141    
142            public PollsVote create(long voteId) {
143                    PollsVote pollsVote = new PollsVoteImpl();
144    
145                    pollsVote.setNew(true);
146                    pollsVote.setPrimaryKey(voteId);
147    
148                    return pollsVote;
149            }
150    
151            public PollsVote remove(Serializable primaryKey)
152                    throws NoSuchModelException, SystemException {
153                    return remove(((Long)primaryKey).longValue());
154            }
155    
156            public PollsVote remove(long voteId)
157                    throws NoSuchVoteException, SystemException {
158                    Session session = null;
159    
160                    try {
161                            session = openSession();
162    
163                            PollsVote pollsVote = (PollsVote)session.get(PollsVoteImpl.class,
164                                            new Long(voteId));
165    
166                            if (pollsVote == null) {
167                                    if (_log.isWarnEnabled()) {
168                                            _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + voteId);
169                                    }
170    
171                                    throw new NoSuchVoteException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
172                                            voteId);
173                            }
174    
175                            return remove(pollsVote);
176                    }
177                    catch (NoSuchVoteException nsee) {
178                            throw nsee;
179                    }
180                    catch (Exception e) {
181                            throw processException(e);
182                    }
183                    finally {
184                            closeSession(session);
185                    }
186            }
187    
188            protected PollsVote removeImpl(PollsVote pollsVote)
189                    throws SystemException {
190                    pollsVote = toUnwrappedModel(pollsVote);
191    
192                    Session session = null;
193    
194                    try {
195                            session = openSession();
196    
197                            if (pollsVote.isCachedModel() || BatchSessionUtil.isEnabled()) {
198                                    Object staleObject = session.get(PollsVoteImpl.class,
199                                                    pollsVote.getPrimaryKeyObj());
200    
201                                    if (staleObject != null) {
202                                            session.evict(staleObject);
203                                    }
204                            }
205    
206                            session.delete(pollsVote);
207    
208                            session.flush();
209                    }
210                    catch (Exception e) {
211                            throw processException(e);
212                    }
213                    finally {
214                            closeSession(session);
215                    }
216    
217                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
218    
219                    PollsVoteModelImpl pollsVoteModelImpl = (PollsVoteModelImpl)pollsVote;
220    
221                    FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_Q_U,
222                            new Object[] {
223                                    new Long(pollsVoteModelImpl.getOriginalQuestionId()),
224                                    new Long(pollsVoteModelImpl.getOriginalUserId())
225                            });
226    
227                    EntityCacheUtil.removeResult(PollsVoteModelImpl.ENTITY_CACHE_ENABLED,
228                            PollsVoteImpl.class, pollsVote.getPrimaryKey());
229    
230                    return pollsVote;
231            }
232    
233            public PollsVote updateImpl(
234                    com.liferay.portlet.polls.model.PollsVote pollsVote, boolean merge)
235                    throws SystemException {
236                    pollsVote = toUnwrappedModel(pollsVote);
237    
238                    boolean isNew = pollsVote.isNew();
239    
240                    PollsVoteModelImpl pollsVoteModelImpl = (PollsVoteModelImpl)pollsVote;
241    
242                    Session session = null;
243    
244                    try {
245                            session = openSession();
246    
247                            BatchSessionUtil.update(session, pollsVote, merge);
248    
249                            pollsVote.setNew(false);
250                    }
251                    catch (Exception e) {
252                            throw processException(e);
253                    }
254                    finally {
255                            closeSession(session);
256                    }
257    
258                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
259    
260                    EntityCacheUtil.putResult(PollsVoteModelImpl.ENTITY_CACHE_ENABLED,
261                            PollsVoteImpl.class, pollsVote.getPrimaryKey(), pollsVote);
262    
263                    if (!isNew &&
264                                    ((pollsVote.getQuestionId() != pollsVoteModelImpl.getOriginalQuestionId()) ||
265                                    (pollsVote.getUserId() != pollsVoteModelImpl.getOriginalUserId()))) {
266                            FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_Q_U,
267                                    new Object[] {
268                                            new Long(pollsVoteModelImpl.getOriginalQuestionId()),
269                                            new Long(pollsVoteModelImpl.getOriginalUserId())
270                                    });
271                    }
272    
273                    if (isNew ||
274                                    ((pollsVote.getQuestionId() != pollsVoteModelImpl.getOriginalQuestionId()) ||
275                                    (pollsVote.getUserId() != pollsVoteModelImpl.getOriginalUserId()))) {
276                            FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_Q_U,
277                                    new Object[] {
278                                            new Long(pollsVote.getQuestionId()),
279                                            new Long(pollsVote.getUserId())
280                                    }, pollsVote);
281                    }
282    
283                    return pollsVote;
284            }
285    
286            protected PollsVote toUnwrappedModel(PollsVote pollsVote) {
287                    if (pollsVote instanceof PollsVoteImpl) {
288                            return pollsVote;
289                    }
290    
291                    PollsVoteImpl pollsVoteImpl = new PollsVoteImpl();
292    
293                    pollsVoteImpl.setNew(pollsVote.isNew());
294                    pollsVoteImpl.setPrimaryKey(pollsVote.getPrimaryKey());
295    
296                    pollsVoteImpl.setVoteId(pollsVote.getVoteId());
297                    pollsVoteImpl.setUserId(pollsVote.getUserId());
298                    pollsVoteImpl.setQuestionId(pollsVote.getQuestionId());
299                    pollsVoteImpl.setChoiceId(pollsVote.getChoiceId());
300                    pollsVoteImpl.setVoteDate(pollsVote.getVoteDate());
301    
302                    return pollsVoteImpl;
303            }
304    
305            public PollsVote findByPrimaryKey(Serializable primaryKey)
306                    throws NoSuchModelException, SystemException {
307                    return findByPrimaryKey(((Long)primaryKey).longValue());
308            }
309    
310            public PollsVote findByPrimaryKey(long voteId)
311                    throws NoSuchVoteException, SystemException {
312                    PollsVote pollsVote = fetchByPrimaryKey(voteId);
313    
314                    if (pollsVote == null) {
315                            if (_log.isWarnEnabled()) {
316                                    _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + voteId);
317                            }
318    
319                            throw new NoSuchVoteException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
320                                    voteId);
321                    }
322    
323                    return pollsVote;
324            }
325    
326            public PollsVote fetchByPrimaryKey(Serializable primaryKey)
327                    throws SystemException {
328                    return fetchByPrimaryKey(((Long)primaryKey).longValue());
329            }
330    
331            public PollsVote fetchByPrimaryKey(long voteId) throws SystemException {
332                    PollsVote pollsVote = (PollsVote)EntityCacheUtil.getResult(PollsVoteModelImpl.ENTITY_CACHE_ENABLED,
333                                    PollsVoteImpl.class, voteId, this);
334    
335                    if (pollsVote == null) {
336                            Session session = null;
337    
338                            try {
339                                    session = openSession();
340    
341                                    pollsVote = (PollsVote)session.get(PollsVoteImpl.class,
342                                                    new Long(voteId));
343                            }
344                            catch (Exception e) {
345                                    throw processException(e);
346                            }
347                            finally {
348                                    if (pollsVote != null) {
349                                            cacheResult(pollsVote);
350                                    }
351    
352                                    closeSession(session);
353                            }
354                    }
355    
356                    return pollsVote;
357            }
358    
359            public List<PollsVote> findByQuestionId(long questionId)
360                    throws SystemException {
361                    return findByQuestionId(questionId, QueryUtil.ALL_POS,
362                            QueryUtil.ALL_POS, null);
363            }
364    
365            public List<PollsVote> findByQuestionId(long questionId, int start, int end)
366                    throws SystemException {
367                    return findByQuestionId(questionId, start, end, null);
368            }
369    
370            public List<PollsVote> findByQuestionId(long questionId, int start,
371                    int end, OrderByComparator orderByComparator) throws SystemException {
372                    Object[] finderArgs = new Object[] {
373                                    questionId,
374                                    
375                                    String.valueOf(start), String.valueOf(end),
376                                    String.valueOf(orderByComparator)
377                            };
378    
379                    List<PollsVote> list = (List<PollsVote>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_QUESTIONID,
380                                    finderArgs, this);
381    
382                    if (list == null) {
383                            Session session = null;
384    
385                            try {
386                                    session = openSession();
387    
388                                    StringBundler query = null;
389    
390                                    if (orderByComparator != null) {
391                                            query = new StringBundler(3 +
392                                                            (orderByComparator.getOrderByFields().length * 3));
393                                    }
394                                    else {
395                                            query = new StringBundler(2);
396                                    }
397    
398                                    query.append(_SQL_SELECT_POLLSVOTE_WHERE);
399    
400                                    query.append(_FINDER_COLUMN_QUESTIONID_QUESTIONID_2);
401    
402                                    if (orderByComparator != null) {
403                                            appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
404                                                    orderByComparator);
405                                    }
406    
407                                    String sql = query.toString();
408    
409                                    Query q = session.createQuery(sql);
410    
411                                    QueryPos qPos = QueryPos.getInstance(q);
412    
413                                    qPos.add(questionId);
414    
415                                    list = (List<PollsVote>)QueryUtil.list(q, getDialect(), start,
416                                                    end);
417                            }
418                            catch (Exception e) {
419                                    throw processException(e);
420                            }
421                            finally {
422                                    if (list == null) {
423                                            list = new ArrayList<PollsVote>();
424                                    }
425    
426                                    cacheResult(list);
427    
428                                    FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_QUESTIONID,
429                                            finderArgs, list);
430    
431                                    closeSession(session);
432                            }
433                    }
434    
435                    return list;
436            }
437    
438            public PollsVote findByQuestionId_First(long questionId,
439                    OrderByComparator orderByComparator)
440                    throws NoSuchVoteException, SystemException {
441                    List<PollsVote> list = findByQuestionId(questionId, 0, 1,
442                                    orderByComparator);
443    
444                    if (list.isEmpty()) {
445                            StringBundler msg = new StringBundler(4);
446    
447                            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
448    
449                            msg.append("questionId=");
450                            msg.append(questionId);
451    
452                            msg.append(StringPool.CLOSE_CURLY_BRACE);
453    
454                            throw new NoSuchVoteException(msg.toString());
455                    }
456                    else {
457                            return list.get(0);
458                    }
459            }
460    
461            public PollsVote findByQuestionId_Last(long questionId,
462                    OrderByComparator orderByComparator)
463                    throws NoSuchVoteException, SystemException {
464                    int count = countByQuestionId(questionId);
465    
466                    List<PollsVote> list = findByQuestionId(questionId, count - 1, count,
467                                    orderByComparator);
468    
469                    if (list.isEmpty()) {
470                            StringBundler msg = new StringBundler(4);
471    
472                            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
473    
474                            msg.append("questionId=");
475                            msg.append(questionId);
476    
477                            msg.append(StringPool.CLOSE_CURLY_BRACE);
478    
479                            throw new NoSuchVoteException(msg.toString());
480                    }
481                    else {
482                            return list.get(0);
483                    }
484            }
485    
486            public PollsVote[] findByQuestionId_PrevAndNext(long voteId,
487                    long questionId, OrderByComparator orderByComparator)
488                    throws NoSuchVoteException, SystemException {
489                    PollsVote pollsVote = findByPrimaryKey(voteId);
490    
491                    Session session = null;
492    
493                    try {
494                            session = openSession();
495    
496                            PollsVote[] array = new PollsVoteImpl[3];
497    
498                            array[0] = getByQuestionId_PrevAndNext(session, pollsVote,
499                                            questionId, orderByComparator, true);
500    
501                            array[1] = pollsVote;
502    
503                            array[2] = getByQuestionId_PrevAndNext(session, pollsVote,
504                                            questionId, orderByComparator, false);
505    
506                            return array;
507                    }
508                    catch (Exception e) {
509                            throw processException(e);
510                    }
511                    finally {
512                            closeSession(session);
513                    }
514            }
515    
516            protected PollsVote getByQuestionId_PrevAndNext(Session session,
517                    PollsVote pollsVote, long questionId,
518                    OrderByComparator orderByComparator, boolean previous) {
519                    StringBundler query = null;
520    
521                    if (orderByComparator != null) {
522                            query = new StringBundler(6 +
523                                            (orderByComparator.getOrderByFields().length * 6));
524                    }
525                    else {
526                            query = new StringBundler(3);
527                    }
528    
529                    query.append(_SQL_SELECT_POLLSVOTE_WHERE);
530    
531                    query.append(_FINDER_COLUMN_QUESTIONID_QUESTIONID_2);
532    
533                    if (orderByComparator != null) {
534                            String[] orderByFields = orderByComparator.getOrderByFields();
535    
536                            if (orderByFields.length > 0) {
537                                    query.append(WHERE_AND);
538                            }
539    
540                            for (int i = 0; i < orderByFields.length; i++) {
541                                    query.append(_ORDER_BY_ENTITY_ALIAS);
542                                    query.append(orderByFields[i]);
543    
544                                    if ((i + 1) < orderByFields.length) {
545                                            if (orderByComparator.isAscending() ^ previous) {
546                                                    query.append(WHERE_GREATER_THAN_HAS_NEXT);
547                                            }
548                                            else {
549                                                    query.append(WHERE_LESSER_THAN_HAS_NEXT);
550                                            }
551                                    }
552                                    else {
553                                            if (orderByComparator.isAscending() ^ previous) {
554                                                    query.append(WHERE_GREATER_THAN);
555                                            }
556                                            else {
557                                                    query.append(WHERE_LESSER_THAN);
558                                            }
559                                    }
560                            }
561    
562                            query.append(ORDER_BY_CLAUSE);
563    
564                            for (int i = 0; i < orderByFields.length; i++) {
565                                    query.append(_ORDER_BY_ENTITY_ALIAS);
566                                    query.append(orderByFields[i]);
567    
568                                    if ((i + 1) < orderByFields.length) {
569                                            if (orderByComparator.isAscending() ^ previous) {
570                                                    query.append(ORDER_BY_ASC_HAS_NEXT);
571                                            }
572                                            else {
573                                                    query.append(ORDER_BY_DESC_HAS_NEXT);
574                                            }
575                                    }
576                                    else {
577                                            if (orderByComparator.isAscending() ^ previous) {
578                                                    query.append(ORDER_BY_ASC);
579                                            }
580                                            else {
581                                                    query.append(ORDER_BY_DESC);
582                                            }
583                                    }
584                            }
585                    }
586    
587                    String sql = query.toString();
588    
589                    Query q = session.createQuery(sql);
590    
591                    q.setFirstResult(0);
592                    q.setMaxResults(2);
593    
594                    QueryPos qPos = QueryPos.getInstance(q);
595    
596                    qPos.add(questionId);
597    
598                    if (orderByComparator != null) {
599                            Object[] values = orderByComparator.getOrderByValues(pollsVote);
600    
601                            for (Object value : values) {
602                                    qPos.add(value);
603                            }
604                    }
605    
606                    List<PollsVote> list = q.list();
607    
608                    if (list.size() == 2) {
609                            return list.get(1);
610                    }
611                    else {
612                            return null;
613                    }
614            }
615    
616            public List<PollsVote> findByChoiceId(long choiceId)
617                    throws SystemException {
618                    return findByChoiceId(choiceId, QueryUtil.ALL_POS, QueryUtil.ALL_POS,
619                            null);
620            }
621    
622            public List<PollsVote> findByChoiceId(long choiceId, int start, int end)
623                    throws SystemException {
624                    return findByChoiceId(choiceId, start, end, null);
625            }
626    
627            public List<PollsVote> findByChoiceId(long choiceId, int start, int end,
628                    OrderByComparator orderByComparator) throws SystemException {
629                    Object[] finderArgs = new Object[] {
630                                    choiceId,
631                                    
632                                    String.valueOf(start), String.valueOf(end),
633                                    String.valueOf(orderByComparator)
634                            };
635    
636                    List<PollsVote> list = (List<PollsVote>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_CHOICEID,
637                                    finderArgs, this);
638    
639                    if (list == null) {
640                            Session session = null;
641    
642                            try {
643                                    session = openSession();
644    
645                                    StringBundler query = null;
646    
647                                    if (orderByComparator != null) {
648                                            query = new StringBundler(3 +
649                                                            (orderByComparator.getOrderByFields().length * 3));
650                                    }
651                                    else {
652                                            query = new StringBundler(2);
653                                    }
654    
655                                    query.append(_SQL_SELECT_POLLSVOTE_WHERE);
656    
657                                    query.append(_FINDER_COLUMN_CHOICEID_CHOICEID_2);
658    
659                                    if (orderByComparator != null) {
660                                            appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
661                                                    orderByComparator);
662                                    }
663    
664                                    String sql = query.toString();
665    
666                                    Query q = session.createQuery(sql);
667    
668                                    QueryPos qPos = QueryPos.getInstance(q);
669    
670                                    qPos.add(choiceId);
671    
672                                    list = (List<PollsVote>)QueryUtil.list(q, getDialect(), start,
673                                                    end);
674                            }
675                            catch (Exception e) {
676                                    throw processException(e);
677                            }
678                            finally {
679                                    if (list == null) {
680                                            list = new ArrayList<PollsVote>();
681                                    }
682    
683                                    cacheResult(list);
684    
685                                    FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_CHOICEID,
686                                            finderArgs, list);
687    
688                                    closeSession(session);
689                            }
690                    }
691    
692                    return list;
693            }
694    
695            public PollsVote findByChoiceId_First(long choiceId,
696                    OrderByComparator orderByComparator)
697                    throws NoSuchVoteException, SystemException {
698                    List<PollsVote> list = findByChoiceId(choiceId, 0, 1, orderByComparator);
699    
700                    if (list.isEmpty()) {
701                            StringBundler msg = new StringBundler(4);
702    
703                            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
704    
705                            msg.append("choiceId=");
706                            msg.append(choiceId);
707    
708                            msg.append(StringPool.CLOSE_CURLY_BRACE);
709    
710                            throw new NoSuchVoteException(msg.toString());
711                    }
712                    else {
713                            return list.get(0);
714                    }
715            }
716    
717            public PollsVote findByChoiceId_Last(long choiceId,
718                    OrderByComparator orderByComparator)
719                    throws NoSuchVoteException, SystemException {
720                    int count = countByChoiceId(choiceId);
721    
722                    List<PollsVote> list = findByChoiceId(choiceId, count - 1, count,
723                                    orderByComparator);
724    
725                    if (list.isEmpty()) {
726                            StringBundler msg = new StringBundler(4);
727    
728                            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
729    
730                            msg.append("choiceId=");
731                            msg.append(choiceId);
732    
733                            msg.append(StringPool.CLOSE_CURLY_BRACE);
734    
735                            throw new NoSuchVoteException(msg.toString());
736                    }
737                    else {
738                            return list.get(0);
739                    }
740            }
741    
742            public PollsVote[] findByChoiceId_PrevAndNext(long voteId, long choiceId,
743                    OrderByComparator orderByComparator)
744                    throws NoSuchVoteException, SystemException {
745                    PollsVote pollsVote = findByPrimaryKey(voteId);
746    
747                    Session session = null;
748    
749                    try {
750                            session = openSession();
751    
752                            PollsVote[] array = new PollsVoteImpl[3];
753    
754                            array[0] = getByChoiceId_PrevAndNext(session, pollsVote, choiceId,
755                                            orderByComparator, true);
756    
757                            array[1] = pollsVote;
758    
759                            array[2] = getByChoiceId_PrevAndNext(session, pollsVote, choiceId,
760                                            orderByComparator, false);
761    
762                            return array;
763                    }
764                    catch (Exception e) {
765                            throw processException(e);
766                    }
767                    finally {
768                            closeSession(session);
769                    }
770            }
771    
772            protected PollsVote getByChoiceId_PrevAndNext(Session session,
773                    PollsVote pollsVote, long choiceId,
774                    OrderByComparator orderByComparator, boolean previous) {
775                    StringBundler query = null;
776    
777                    if (orderByComparator != null) {
778                            query = new StringBundler(6 +
779                                            (orderByComparator.getOrderByFields().length * 6));
780                    }
781                    else {
782                            query = new StringBundler(3);
783                    }
784    
785                    query.append(_SQL_SELECT_POLLSVOTE_WHERE);
786    
787                    query.append(_FINDER_COLUMN_CHOICEID_CHOICEID_2);
788    
789                    if (orderByComparator != null) {
790                            String[] orderByFields = orderByComparator.getOrderByFields();
791    
792                            if (orderByFields.length > 0) {
793                                    query.append(WHERE_AND);
794                            }
795    
796                            for (int i = 0; i < orderByFields.length; i++) {
797                                    query.append(_ORDER_BY_ENTITY_ALIAS);
798                                    query.append(orderByFields[i]);
799    
800                                    if ((i + 1) < orderByFields.length) {
801                                            if (orderByComparator.isAscending() ^ previous) {
802                                                    query.append(WHERE_GREATER_THAN_HAS_NEXT);
803                                            }
804                                            else {
805                                                    query.append(WHERE_LESSER_THAN_HAS_NEXT);
806                                            }
807                                    }
808                                    else {
809                                            if (orderByComparator.isAscending() ^ previous) {
810                                                    query.append(WHERE_GREATER_THAN);
811                                            }
812                                            else {
813                                                    query.append(WHERE_LESSER_THAN);
814                                            }
815                                    }
816                            }
817    
818                            query.append(ORDER_BY_CLAUSE);
819    
820                            for (int i = 0; i < orderByFields.length; i++) {
821                                    query.append(_ORDER_BY_ENTITY_ALIAS);
822                                    query.append(orderByFields[i]);
823    
824                                    if ((i + 1) < orderByFields.length) {
825                                            if (orderByComparator.isAscending() ^ previous) {
826                                                    query.append(ORDER_BY_ASC_HAS_NEXT);
827                                            }
828                                            else {
829                                                    query.append(ORDER_BY_DESC_HAS_NEXT);
830                                            }
831                                    }
832                                    else {
833                                            if (orderByComparator.isAscending() ^ previous) {
834                                                    query.append(ORDER_BY_ASC);
835                                            }
836                                            else {
837                                                    query.append(ORDER_BY_DESC);
838                                            }
839                                    }
840                            }
841                    }
842    
843                    String sql = query.toString();
844    
845                    Query q = session.createQuery(sql);
846    
847                    q.setFirstResult(0);
848                    q.setMaxResults(2);
849    
850                    QueryPos qPos = QueryPos.getInstance(q);
851    
852                    qPos.add(choiceId);
853    
854                    if (orderByComparator != null) {
855                            Object[] values = orderByComparator.getOrderByValues(pollsVote);
856    
857                            for (Object value : values) {
858                                    qPos.add(value);
859                            }
860                    }
861    
862                    List<PollsVote> list = q.list();
863    
864                    if (list.size() == 2) {
865                            return list.get(1);
866                    }
867                    else {
868                            return null;
869                    }
870            }
871    
872            public PollsVote findByQ_U(long questionId, long userId)
873                    throws NoSuchVoteException, SystemException {
874                    PollsVote pollsVote = fetchByQ_U(questionId, userId);
875    
876                    if (pollsVote == null) {
877                            StringBundler msg = new StringBundler(6);
878    
879                            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
880    
881                            msg.append("questionId=");
882                            msg.append(questionId);
883    
884                            msg.append(", userId=");
885                            msg.append(userId);
886    
887                            msg.append(StringPool.CLOSE_CURLY_BRACE);
888    
889                            if (_log.isWarnEnabled()) {
890                                    _log.warn(msg.toString());
891                            }
892    
893                            throw new NoSuchVoteException(msg.toString());
894                    }
895    
896                    return pollsVote;
897            }
898    
899            public PollsVote fetchByQ_U(long questionId, long userId)
900                    throws SystemException {
901                    return fetchByQ_U(questionId, userId, true);
902            }
903    
904            public PollsVote fetchByQ_U(long questionId, long userId,
905                    boolean retrieveFromCache) throws SystemException {
906                    Object[] finderArgs = new Object[] { questionId, userId };
907    
908                    Object result = null;
909    
910                    if (retrieveFromCache) {
911                            result = FinderCacheUtil.getResult(FINDER_PATH_FETCH_BY_Q_U,
912                                            finderArgs, this);
913                    }
914    
915                    if (result == null) {
916                            Session session = null;
917    
918                            try {
919                                    session = openSession();
920    
921                                    StringBundler query = new StringBundler(3);
922    
923                                    query.append(_SQL_SELECT_POLLSVOTE_WHERE);
924    
925                                    query.append(_FINDER_COLUMN_Q_U_QUESTIONID_2);
926    
927                                    query.append(_FINDER_COLUMN_Q_U_USERID_2);
928    
929                                    String sql = query.toString();
930    
931                                    Query q = session.createQuery(sql);
932    
933                                    QueryPos qPos = QueryPos.getInstance(q);
934    
935                                    qPos.add(questionId);
936    
937                                    qPos.add(userId);
938    
939                                    List<PollsVote> list = q.list();
940    
941                                    result = list;
942    
943                                    PollsVote pollsVote = null;
944    
945                                    if (list.isEmpty()) {
946                                            FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_Q_U,
947                                                    finderArgs, list);
948                                    }
949                                    else {
950                                            pollsVote = list.get(0);
951    
952                                            cacheResult(pollsVote);
953    
954                                            if ((pollsVote.getQuestionId() != questionId) ||
955                                                            (pollsVote.getUserId() != userId)) {
956                                                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_Q_U,
957                                                            finderArgs, pollsVote);
958                                            }
959                                    }
960    
961                                    return pollsVote;
962                            }
963                            catch (Exception e) {
964                                    throw processException(e);
965                            }
966                            finally {
967                                    if (result == null) {
968                                            FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_Q_U,
969                                                    finderArgs, new ArrayList<PollsVote>());
970                                    }
971    
972                                    closeSession(session);
973                            }
974                    }
975                    else {
976                            if (result instanceof List<?>) {
977                                    return null;
978                            }
979                            else {
980                                    return (PollsVote)result;
981                            }
982                    }
983            }
984    
985            public List<PollsVote> findAll() throws SystemException {
986                    return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
987            }
988    
989            public List<PollsVote> findAll(int start, int end)
990                    throws SystemException {
991                    return findAll(start, end, null);
992            }
993    
994            public List<PollsVote> findAll(int start, int end,
995                    OrderByComparator orderByComparator) throws SystemException {
996                    Object[] finderArgs = new Object[] {
997                                    String.valueOf(start), String.valueOf(end),
998                                    String.valueOf(orderByComparator)
999                            };
1000    
1001                    List<PollsVote> list = (List<PollsVote>)FinderCacheUtil.getResult(FINDER_PATH_FIND_ALL,
1002                                    finderArgs, this);
1003    
1004                    if (list == null) {
1005                            Session session = null;
1006    
1007                            try {
1008                                    session = openSession();
1009    
1010                                    StringBundler query = null;
1011                                    String sql = null;
1012    
1013                                    if (orderByComparator != null) {
1014                                            query = new StringBundler(2 +
1015                                                            (orderByComparator.getOrderByFields().length * 3));
1016    
1017                                            query.append(_SQL_SELECT_POLLSVOTE);
1018    
1019                                            appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
1020                                                    orderByComparator);
1021    
1022                                            sql = query.toString();
1023                                    }
1024                                    else {
1025                                            sql = _SQL_SELECT_POLLSVOTE;
1026                                    }
1027    
1028                                    Query q = session.createQuery(sql);
1029    
1030                                    if (orderByComparator == null) {
1031                                            list = (List<PollsVote>)QueryUtil.list(q, getDialect(),
1032                                                            start, end, false);
1033    
1034                                            Collections.sort(list);
1035                                    }
1036                                    else {
1037                                            list = (List<PollsVote>)QueryUtil.list(q, getDialect(),
1038                                                            start, end);
1039                                    }
1040                            }
1041                            catch (Exception e) {
1042                                    throw processException(e);
1043                            }
1044                            finally {
1045                                    if (list == null) {
1046                                            list = new ArrayList<PollsVote>();
1047                                    }
1048    
1049                                    cacheResult(list);
1050    
1051                                    FinderCacheUtil.putResult(FINDER_PATH_FIND_ALL, finderArgs, list);
1052    
1053                                    closeSession(session);
1054                            }
1055                    }
1056    
1057                    return list;
1058            }
1059    
1060            public void removeByQuestionId(long questionId) throws SystemException {
1061                    for (PollsVote pollsVote : findByQuestionId(questionId)) {
1062                            remove(pollsVote);
1063                    }
1064            }
1065    
1066            public void removeByChoiceId(long choiceId) throws SystemException {
1067                    for (PollsVote pollsVote : findByChoiceId(choiceId)) {
1068                            remove(pollsVote);
1069                    }
1070            }
1071    
1072            public void removeByQ_U(long questionId, long userId)
1073                    throws NoSuchVoteException, SystemException {
1074                    PollsVote pollsVote = findByQ_U(questionId, userId);
1075    
1076                    remove(pollsVote);
1077            }
1078    
1079            public void removeAll() throws SystemException {
1080                    for (PollsVote pollsVote : findAll()) {
1081                            remove(pollsVote);
1082                    }
1083            }
1084    
1085            public int countByQuestionId(long questionId) throws SystemException {
1086                    Object[] finderArgs = new Object[] { questionId };
1087    
1088                    Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_QUESTIONID,
1089                                    finderArgs, this);
1090    
1091                    if (count == null) {
1092                            Session session = null;
1093    
1094                            try {
1095                                    session = openSession();
1096    
1097                                    StringBundler query = new StringBundler(2);
1098    
1099                                    query.append(_SQL_COUNT_POLLSVOTE_WHERE);
1100    
1101                                    query.append(_FINDER_COLUMN_QUESTIONID_QUESTIONID_2);
1102    
1103                                    String sql = query.toString();
1104    
1105                                    Query q = session.createQuery(sql);
1106    
1107                                    QueryPos qPos = QueryPos.getInstance(q);
1108    
1109                                    qPos.add(questionId);
1110    
1111                                    count = (Long)q.uniqueResult();
1112                            }
1113                            catch (Exception e) {
1114                                    throw processException(e);
1115                            }
1116                            finally {
1117                                    if (count == null) {
1118                                            count = Long.valueOf(0);
1119                                    }
1120    
1121                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_QUESTIONID,
1122                                            finderArgs, count);
1123    
1124                                    closeSession(session);
1125                            }
1126                    }
1127    
1128                    return count.intValue();
1129            }
1130    
1131            public int countByChoiceId(long choiceId) throws SystemException {
1132                    Object[] finderArgs = new Object[] { choiceId };
1133    
1134                    Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_CHOICEID,
1135                                    finderArgs, this);
1136    
1137                    if (count == null) {
1138                            Session session = null;
1139    
1140                            try {
1141                                    session = openSession();
1142    
1143                                    StringBundler query = new StringBundler(2);
1144    
1145                                    query.append(_SQL_COUNT_POLLSVOTE_WHERE);
1146    
1147                                    query.append(_FINDER_COLUMN_CHOICEID_CHOICEID_2);
1148    
1149                                    String sql = query.toString();
1150    
1151                                    Query q = session.createQuery(sql);
1152    
1153                                    QueryPos qPos = QueryPos.getInstance(q);
1154    
1155                                    qPos.add(choiceId);
1156    
1157                                    count = (Long)q.uniqueResult();
1158                            }
1159                            catch (Exception e) {
1160                                    throw processException(e);
1161                            }
1162                            finally {
1163                                    if (count == null) {
1164                                            count = Long.valueOf(0);
1165                                    }
1166    
1167                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_CHOICEID,
1168                                            finderArgs, count);
1169    
1170                                    closeSession(session);
1171                            }
1172                    }
1173    
1174                    return count.intValue();
1175            }
1176    
1177            public int countByQ_U(long questionId, long userId)
1178                    throws SystemException {
1179                    Object[] finderArgs = new Object[] { questionId, userId };
1180    
1181                    Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_Q_U,
1182                                    finderArgs, this);
1183    
1184                    if (count == null) {
1185                            Session session = null;
1186    
1187                            try {
1188                                    session = openSession();
1189    
1190                                    StringBundler query = new StringBundler(3);
1191    
1192                                    query.append(_SQL_COUNT_POLLSVOTE_WHERE);
1193    
1194                                    query.append(_FINDER_COLUMN_Q_U_QUESTIONID_2);
1195    
1196                                    query.append(_FINDER_COLUMN_Q_U_USERID_2);
1197    
1198                                    String sql = query.toString();
1199    
1200                                    Query q = session.createQuery(sql);
1201    
1202                                    QueryPos qPos = QueryPos.getInstance(q);
1203    
1204                                    qPos.add(questionId);
1205    
1206                                    qPos.add(userId);
1207    
1208                                    count = (Long)q.uniqueResult();
1209                            }
1210                            catch (Exception e) {
1211                                    throw processException(e);
1212                            }
1213                            finally {
1214                                    if (count == null) {
1215                                            count = Long.valueOf(0);
1216                                    }
1217    
1218                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_Q_U, finderArgs,
1219                                            count);
1220    
1221                                    closeSession(session);
1222                            }
1223                    }
1224    
1225                    return count.intValue();
1226            }
1227    
1228            public int countAll() throws SystemException {
1229                    Object[] finderArgs = new Object[0];
1230    
1231                    Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
1232                                    finderArgs, this);
1233    
1234                    if (count == null) {
1235                            Session session = null;
1236    
1237                            try {
1238                                    session = openSession();
1239    
1240                                    Query q = session.createQuery(_SQL_COUNT_POLLSVOTE);
1241    
1242                                    count = (Long)q.uniqueResult();
1243                            }
1244                            catch (Exception e) {
1245                                    throw processException(e);
1246                            }
1247                            finally {
1248                                    if (count == null) {
1249                                            count = Long.valueOf(0);
1250                                    }
1251    
1252                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL, finderArgs,
1253                                            count);
1254    
1255                                    closeSession(session);
1256                            }
1257                    }
1258    
1259                    return count.intValue();
1260            }
1261    
1262            public void afterPropertiesSet() {
1263                    String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
1264                                            com.liferay.portal.util.PropsUtil.get(
1265                                                    "value.object.listener.com.liferay.portlet.polls.model.PollsVote")));
1266    
1267                    if (listenerClassNames.length > 0) {
1268                            try {
1269                                    List<ModelListener<PollsVote>> listenersList = new ArrayList<ModelListener<PollsVote>>();
1270    
1271                                    for (String listenerClassName : listenerClassNames) {
1272                                            listenersList.add((ModelListener<PollsVote>)InstanceFactory.newInstance(
1273                                                            listenerClassName));
1274                                    }
1275    
1276                                    listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
1277                            }
1278                            catch (Exception e) {
1279                                    _log.error(e);
1280                            }
1281                    }
1282            }
1283    
1284            @BeanReference(type = PollsChoicePersistence.class)
1285            protected PollsChoicePersistence pollsChoicePersistence;
1286            @BeanReference(type = PollsQuestionPersistence.class)
1287            protected PollsQuestionPersistence pollsQuestionPersistence;
1288            @BeanReference(type = PollsVotePersistence.class)
1289            protected PollsVotePersistence pollsVotePersistence;
1290            @BeanReference(type = ResourcePersistence.class)
1291            protected ResourcePersistence resourcePersistence;
1292            @BeanReference(type = UserPersistence.class)
1293            protected UserPersistence userPersistence;
1294            private static final String _SQL_SELECT_POLLSVOTE = "SELECT pollsVote FROM PollsVote pollsVote";
1295            private static final String _SQL_SELECT_POLLSVOTE_WHERE = "SELECT pollsVote FROM PollsVote pollsVote WHERE ";
1296            private static final String _SQL_COUNT_POLLSVOTE = "SELECT COUNT(pollsVote) FROM PollsVote pollsVote";
1297            private static final String _SQL_COUNT_POLLSVOTE_WHERE = "SELECT COUNT(pollsVote) FROM PollsVote pollsVote WHERE ";
1298            private static final String _FINDER_COLUMN_QUESTIONID_QUESTIONID_2 = "pollsVote.questionId = ?";
1299            private static final String _FINDER_COLUMN_CHOICEID_CHOICEID_2 = "pollsVote.choiceId = ?";
1300            private static final String _FINDER_COLUMN_Q_U_QUESTIONID_2 = "pollsVote.questionId = ? AND ";
1301            private static final String _FINDER_COLUMN_Q_U_USERID_2 = "pollsVote.userId = ?";
1302            private static final String _ORDER_BY_ENTITY_ALIAS = "pollsVote.";
1303            private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No PollsVote exists with the primary key ";
1304            private static final String _NO_SUCH_ENTITY_WITH_KEY = "No PollsVote exists with the key {";
1305            private static Log _log = LogFactoryUtil.getLog(PollsVotePersistenceImpl.class);
1306    }