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.pollsdisplay.action;
016    
017    import com.liferay.portal.kernel.servlet.SessionErrors;
018    import com.liferay.portal.kernel.util.GetterUtil;
019    import com.liferay.portal.kernel.util.StringPool;
020    import com.liferay.portal.struts.PortletAction;
021    import com.liferay.portal.util.WebKeys;
022    import com.liferay.portlet.polls.NoSuchQuestionException;
023    import com.liferay.portlet.polls.model.PollsQuestion;
024    import com.liferay.portlet.polls.service.PollsQuestionServiceUtil;
025    
026    import javax.portlet.PortletConfig;
027    import javax.portlet.PortletPreferences;
028    import javax.portlet.RenderRequest;
029    import javax.portlet.RenderResponse;
030    
031    import org.apache.struts.action.ActionForm;
032    import org.apache.struts.action.ActionForward;
033    import org.apache.struts.action.ActionMapping;
034    
035    /**
036     * @author Brian Wing Shun Chan
037     */
038    public class ViewAction extends PortletAction {
039    
040            @Override
041            public ActionForward render(
042                            ActionMapping actionMapping, ActionForm actionForm,
043                            PortletConfig portletConfig, RenderRequest renderRequest,
044                            RenderResponse renderResponse)
045                    throws Exception {
046    
047                    try {
048                            PortletPreferences preferences = renderRequest.getPreferences();
049    
050                            long questionId = GetterUtil.getLong(
051                                    preferences.getValue("questionId", StringPool.BLANK));
052    
053                            if (questionId > 0) {
054                                    PollsQuestion question = PollsQuestionServiceUtil.getQuestion(
055                                            questionId);
056    
057                                    renderRequest.setAttribute(WebKeys.POLLS_QUESTION, question);
058                            }
059                    }
060                    catch (Exception e) {
061                            if (!(e instanceof NoSuchQuestionException)) {
062                                    SessionErrors.add(renderRequest, e.getClass());
063    
064                                    return actionMapping.findForward("portlet.polls_display.error");
065                            }
066                    }
067    
068                    return actionMapping.findForward("portlet.polls_display.view");
069            }
070    
071    }