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.shopping.action;
016    
017    import com.liferay.portal.kernel.servlet.SessionErrors;
018    import com.liferay.portal.kernel.util.CharPool;
019    import com.liferay.portal.kernel.util.ParamUtil;
020    import com.liferay.portal.kernel.util.StringUtil;
021    import com.liferay.portal.security.auth.PrincipalException;
022    import com.liferay.portal.struts.PortletAction;
023    import com.liferay.portal.theme.ThemeDisplay;
024    import com.liferay.portal.util.WebKeys;
025    import com.liferay.portlet.shopping.AmazonException;
026    import com.liferay.portlet.shopping.service.ShoppingItemServiceUtil;
027    
028    import javax.portlet.ActionRequest;
029    import javax.portlet.ActionResponse;
030    import javax.portlet.PortletConfig;
031    import javax.portlet.RenderRequest;
032    import javax.portlet.RenderResponse;
033    
034    import org.apache.struts.action.ActionForm;
035    import org.apache.struts.action.ActionForward;
036    import org.apache.struts.action.ActionMapping;
037    
038    /**
039     * @author Brian Wing Shun Chan
040     */
041    public class AddBookItemsAction extends PortletAction {
042    
043            @Override
044            public void processAction(
045                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
046                            ActionRequest actionRequest, ActionResponse actionResponse)
047                    throws Exception {
048    
049                    try {
050                            quickAddItems(actionRequest);
051    
052                            sendRedirect(actionRequest, actionResponse);
053                    }
054                    catch (Exception e) {
055                            if (e instanceof PrincipalException) {
056                                    SessionErrors.add(actionRequest, e.getClass().getName());
057    
058                                    setForward(actionRequest, "portlet.shopping.error");
059                            }
060                            else {
061                                    throw e;
062                            }
063                    }
064            }
065    
066            @Override
067            public ActionForward render(
068                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
069                            RenderRequest renderRequest, RenderResponse renderResponse)
070                    throws Exception {
071    
072                    return mapping.findForward(
073                            getForward(renderRequest, "portlet.shopping.add_book_items"));
074            }
075    
076            protected void quickAddItems(ActionRequest actionRequest) throws Exception {
077                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
078                            WebKeys.THEME_DISPLAY);
079    
080                    long groupId = themeDisplay.getScopeGroupId();
081                    long categoryId = ParamUtil.getLong(actionRequest, "categoryId");
082                    String[] isbns = StringUtil.split(
083                            ParamUtil.getString(actionRequest, "isbns").toUpperCase(),
084                            CharPool.SPACE);
085    
086                    try {
087                            ShoppingItemServiceUtil.addBookItems(groupId, categoryId, isbns);
088                    }
089                    catch (AmazonException ae) {
090                             SessionErrors.add(actionRequest, ae.getClass().getName());
091                    }
092            }
093    
094    }