001    /**
002     * Copyright (c) 2000-2012 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 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().getName());
062    
063                                    return mapping.findForward("portlet.polls_display.error");
064                            }
065                    }
066    
067                    return mapping.findForward("portlet.polls_display.view");
068            }
069    
070    }