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