001    /**
002     * Copyright (c) 2000-2012 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 mapping, ActionForm form, PortletConfig portletConfig,
043                            RenderRequest renderRequest, RenderResponse renderResponse)
044                    throws Exception {
045    
046                    try {
047                            PortletPreferences preferences = renderRequest.getPreferences();
048    
049                            long questionId = GetterUtil.getLong(
050                                    preferences.getValue("questionId", StringPool.BLANK));
051    
052                            if (questionId > 0) {
053                                    PollsQuestion question = PollsQuestionServiceUtil.getQuestion(
054                                            questionId);
055    
056                                    renderRequest.setAttribute(WebKeys.POLLS_QUESTION, question);
057                            }
058                    }
059                    catch (Exception e) {
060                            if (!(e instanceof NoSuchQuestionException)) {
061                                    SessionErrors.add(renderRequest, e.getClass());
062    
063                                    return mapping.findForward("portlet.polls_display.error");
064                            }
065                    }
066    
067                    return mapping.findForward("portlet.polls_display.view");
068            }
069    
070    }