1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
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.portlet.shopping.service.ShoppingItemServiceUtil;
24  
25  import javax.portlet.ActionRequest;
26  import javax.portlet.ActionResponse;
27  import javax.portlet.PortletConfig;
28  import javax.portlet.RenderRequest;
29  import javax.portlet.RenderResponse;
30  
31  import org.apache.struts.action.ActionForm;
32  import org.apache.struts.action.ActionForward;
33  import org.apache.struts.action.ActionMapping;
34  
35  /**
36   * <a href="AddBookItemsAction.java.html"><b><i>View Source</i></b></a>
37   *
38   * @author Brian Wing Shun Chan
39   */
40  public class AddBookItemsAction extends PortletAction {
41  
42      public void processAction(
43              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
44              ActionRequest actionRequest, ActionResponse actionResponse)
45          throws Exception {
46  
47          try {
48              quickAddItems(actionRequest);
49  
50              sendRedirect(actionRequest, actionResponse);
51          }
52          catch (Exception e) {
53              if (e instanceof PrincipalException) {
54                  SessionErrors.add(actionRequest, e.getClass().getName());
55  
56                  setForward(actionRequest, "portlet.shopping.error");
57              }
58              else {
59                  throw e;
60              }
61          }
62      }
63  
64      public ActionForward render(
65              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
66              RenderRequest renderRequest, RenderResponse renderResponse)
67          throws Exception {
68  
69          return mapping.findForward(
70              getForward(renderRequest, "portlet.shopping.add_book_items"));
71      }
72  
73      protected void quickAddItems(ActionRequest actionRequest) throws Exception {
74          long categoryId = ParamUtil.getLong(actionRequest, "categoryId");
75          String[] isbns = StringUtil.split(
76              ParamUtil.getString(actionRequest, "isbns").toUpperCase(),
77              StringPool.SPACE);
78  
79          ShoppingItemServiceUtil.addBookItems(categoryId, isbns);
80      }
81  
82  }