001    /**
002     * Copyright (c) 2000-2013 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.impl;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.model.User;
020    import com.liferay.portal.security.auth.PrincipalException;
021    import com.liferay.portal.security.permission.ActionKeys;
022    import com.liferay.portal.service.ServiceContext;
023    import com.liferay.portlet.polls.model.PollsVote;
024    import com.liferay.portlet.polls.service.base.PollsVoteServiceBaseImpl;
025    import com.liferay.portlet.polls.service.permission.PollsQuestionPermission;
026    
027    /**
028     * @author Brian Wing Shun Chan
029     */
030    public class PollsVoteServiceImpl extends PollsVoteServiceBaseImpl {
031    
032            @Override
033            public PollsVote addVote(
034                            long questionId, long choiceId, ServiceContext serviceContext)
035                    throws PortalException, SystemException {
036    
037                    long userId = 0;
038    
039                    try {
040                            userId = getUserId();
041    
042                            User user = userService.getUserById(userId);
043    
044                            if (user.isDefaultUser()) {
045                                    userId = counterLocalService.increment();
046                            }
047                    }
048                    catch (PrincipalException pe) {
049                            userId = counterLocalService.increment();
050                    }
051    
052                    PollsQuestionPermission.check(
053                            getPermissionChecker(), questionId, ActionKeys.ADD_VOTE);
054    
055                    return pollsVoteLocalService.addVote(
056                            userId, questionId, choiceId, serviceContext);
057            }
058    
059    }