Author: bdaw
Date: 2008-07-17 18:36:08 -0400 (Thu, 17 Jul 2008)
New Revision: 11487
Modified:
modules/portlet/trunk/samples/src/main/java/org/jboss/portal/portlet/samples/shoppingcart/CartPortlet.java
Log:
make CartPortlet use session to store items
Modified:
modules/portlet/trunk/samples/src/main/java/org/jboss/portal/portlet/samples/shoppingcart/CartPortlet.java
===================================================================
---
modules/portlet/trunk/samples/src/main/java/org/jboss/portal/portlet/samples/shoppingcart/CartPortlet.java 2008-07-17
22:22:14 UTC (rev 11486)
+++
modules/portlet/trunk/samples/src/main/java/org/jboss/portal/portlet/samples/shoppingcart/CartPortlet.java 2008-07-17
22:36:08 UTC (rev 11487)
@@ -30,6 +30,7 @@
import javax.portlet.PortletException;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
+import javax.portlet.PortletSession;
import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
@@ -41,7 +42,7 @@
*/
public class CartPortlet extends GenericPortlet
{
- List<CatalogItem> items = new ArrayList<CatalogItem>(7);
+ public static final String ITEMS = "cart_items";
@Override
protected void doView(RenderRequest renderRequest, RenderResponse renderResponse)
throws PortletException, IOException
@@ -49,6 +50,7 @@
renderResponse.setContentType("text/html");
Writer writer = renderResponse.getWriter();
+ List<CatalogItem> items = getItems(renderRequest.getPortletSession());
if (!items.isEmpty())
{
@@ -73,11 +75,28 @@
@Override
public void processEvent(EventRequest eventRequest, EventResponse eventResponse)
throws PortletException, IOException
{
+ List<CatalogItem> items = getItems(eventRequest.getPortletSession());
+
Event event = eventRequest.getEvent();
if (event.getName().equals("CartEvent"))
{
CartEvent cartEvent = (CartEvent)event.getValue();
items.add(Catalog.get(cartEvent.getId()));
}
+
+ eventRequest.getPortletSession().setAttribute(ITEMS, items);
}
+
+ private List<CatalogItem> getItems(PortletSession session)
+ {
+ List<CatalogItem> items =
(List<CatalogItem>)session.getAttribute(ITEMS);
+ if (items == null)
+ {
+ items = new ArrayList<CatalogItem>(7);
+ }
+ return items;
+ }
+
+
+
}