001    /**
002     * Copyright (c) 2000-2013 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.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    }