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.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 actionMapping, ActionForm actionForm,
046                            PortletConfig portletConfig, ActionRequest actionRequest,
047                            ActionResponse actionResponse)
048                    throws Exception {
049    
050                    try {
051                            quickAddItems(actionRequest);
052    
053                            sendRedirect(actionRequest, actionResponse);
054                    }
055                    catch (Exception e) {
056                            if (e instanceof PrincipalException) {
057                                    SessionErrors.add(actionRequest, e.getClass());
058    
059                                    setForward(actionRequest, "portlet.shopping.error");
060                            }
061                            else {
062                                    throw e;
063                            }
064                    }
065            }
066    
067            @Override
068            public ActionForward render(
069                            ActionMapping actionMapping, ActionForm actionForm,
070                            PortletConfig portletConfig, RenderRequest renderRequest,
071                            RenderResponse renderResponse)
072                    throws Exception {
073    
074                    return actionMapping.findForward(
075                            getForward(renderRequest, "portlet.shopping.add_book_items"));
076            }
077    
078            protected void quickAddItems(ActionRequest actionRequest) throws Exception {
079                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
080                            WebKeys.THEME_DISPLAY);
081    
082                    long groupId = themeDisplay.getScopeGroupId();
083                    long categoryId = ParamUtil.getLong(actionRequest, "categoryId");
084                    String[] isbns = StringUtil.split(
085                            ParamUtil.getString(actionRequest, "isbns").toUpperCase(),
086                            CharPool.SPACE);
087    
088                    try {
089                            ShoppingItemServiceUtil.addBookItems(groupId, categoryId, isbns);
090                    }
091                    catch (AmazonException ae) {
092                            SessionErrors.add(actionRequest, ae.getClass());
093                    }
094            }
095    
096    }