1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.shopping.action;
16  
17  import com.liferay.portal.kernel.servlet.SessionErrors;
18  import com.liferay.portal.kernel.util.ParamUtil;
19  import com.liferay.portal.kernel.util.StringPool;
20  import com.liferay.portal.kernel.util.StringUtil;
21  import com.liferay.portal.security.auth.PrincipalException;
22  import com.liferay.portal.struts.PortletAction;
23  import com.liferay.portal.theme.ThemeDisplay;
24  import com.liferay.portal.util.WebKeys;
25  import com.liferay.portlet.shopping.service.ShoppingItemServiceUtil;
26  
27  import javax.portlet.ActionRequest;
28  import javax.portlet.ActionResponse;
29  import javax.portlet.PortletConfig;
30  import javax.portlet.RenderRequest;
31  import javax.portlet.RenderResponse;
32  
33  import org.apache.struts.action.ActionForm;
34  import org.apache.struts.action.ActionForward;
35  import org.apache.struts.action.ActionMapping;
36  
37  /**
38   * <a href="AddBookItemsAction.java.html"><b><i>View Source</i></b></a>
39   *
40   * @author Brian Wing Shun Chan
41   */
42  public class AddBookItemsAction extends PortletAction {
43  
44      public void processAction(
45              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
46              ActionRequest actionRequest, ActionResponse actionResponse)
47          throws Exception {
48  
49          try {
50              quickAddItems(actionRequest);
51  
52              sendRedirect(actionRequest, actionResponse);
53          }
54          catch (Exception e) {
55              if (e instanceof PrincipalException) {
56                  SessionErrors.add(actionRequest, e.getClass().getName());
57  
58                  setForward(actionRequest, "portlet.shopping.error");
59              }
60              else {
61                  throw e;
62              }
63          }
64      }
65  
66      public ActionForward render(
67              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
68              RenderRequest renderRequest, RenderResponse renderResponse)
69          throws Exception {
70  
71          return mapping.findForward(
72              getForward(renderRequest, "portlet.shopping.add_book_items"));
73      }
74  
75      protected void quickAddItems(ActionRequest actionRequest) throws Exception {
76          ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
77              WebKeys.THEME_DISPLAY);
78  
79          long groupId = themeDisplay.getScopeGroupId();
80          long categoryId = ParamUtil.getLong(actionRequest, "categoryId");
81          String[] isbns = StringUtil.split(
82              ParamUtil.getString(actionRequest, "isbns").toUpperCase(),
83              StringPool.SPACE);
84  
85          ShoppingItemServiceUtil.addBookItems(groupId, categoryId, isbns);
86      }
87  
88  }