001    /**
002     * Copyright (c) 2000-2010 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.shopping.action;
016    
017    import com.liferay.portal.kernel.servlet.SessionErrors;
018    import com.liferay.portal.kernel.util.ParamUtil;
019    import com.liferay.portal.security.auth.PrincipalException;
020    import com.liferay.portal.struts.PortletAction;
021    import com.liferay.portal.util.WebKeys;
022    import com.liferay.portlet.shopping.NoSuchCouponException;
023    import com.liferay.portlet.shopping.model.ShoppingCoupon;
024    import com.liferay.portlet.shopping.service.ShoppingCouponLocalServiceUtil;
025    
026    import javax.portlet.PortletConfig;
027    import javax.portlet.RenderRequest;
028    import javax.portlet.RenderResponse;
029    
030    import org.apache.struts.action.ActionForm;
031    import org.apache.struts.action.ActionForward;
032    import org.apache.struts.action.ActionMapping;
033    
034    /**
035     * @author Brian Wing Shun Chan
036     */
037    public class ViewCouponAction extends PortletAction {
038    
039            public ActionForward render(
040                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
041                            RenderRequest renderRequest, RenderResponse renderResponse)
042                    throws Exception {
043    
044                    try {
045                            long couponId = ParamUtil.getLong(renderRequest, "couponId");
046    
047                            String code = ParamUtil.getString(renderRequest, "code");
048    
049                            ShoppingCoupon coupon = null;
050    
051                            if (couponId > 0) {
052                                    coupon = ShoppingCouponLocalServiceUtil.getCoupon(couponId);
053                            }
054                            else {
055                                    coupon = ShoppingCouponLocalServiceUtil.getCoupon(code);
056                            }
057    
058                            renderRequest.setAttribute(WebKeys.SHOPPING_COUPON, coupon);
059                    }
060                    catch (Exception e) {
061                            if (e instanceof NoSuchCouponException ||
062                                    e instanceof PrincipalException) {
063    
064                                    SessionErrors.add(renderRequest, e.getClass().getName());
065    
066                                    return mapping.findForward("portlet.shopping.error");
067                            }
068                            else {
069                                    throw e;
070                            }
071                    }
072    
073                    return mapping.findForward("portlet.shopping.view_coupon");
074            }
075    
076    }