Author: julien(a)jboss.com
Date: 2008-03-02 17:24:18 -0500 (Sun, 02 Mar 2008)
New Revision: 10174
Added:
modules/portlet/trunk/samples/src/main/java/org/jboss/portal/portlet/samples/shoppingcart/CartEvent.java
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/Catalog.java
modules/portlet/trunk/samples/src/main/java/org/jboss/portal/portlet/samples/shoppingcart/CatalogItem.java
modules/portlet/trunk/samples/src/main/java/org/jboss/portal/portlet/samples/shoppingcart/CatalogPortlet.java
Log:
adding shopping cart portelts
Added:
modules/portlet/trunk/samples/src/main/java/org/jboss/portal/portlet/samples/shoppingcart/CartEvent.java
===================================================================
---
modules/portlet/trunk/samples/src/main/java/org/jboss/portal/portlet/samples/shoppingcart/CartEvent.java
(rev 0)
+++
modules/portlet/trunk/samples/src/main/java/org/jboss/portal/portlet/samples/shoppingcart/CartEvent.java 2008-03-02
22:24:18 UTC (rev 10174)
@@ -0,0 +1,50 @@
+/******************************************************************************
+ * JBoss, a division of Red Hat *
+ * Copyright 2008, Red Hat Middleware, LLC, and individual *
+ * contributors as indicated by the @authors tag. See the *
+ * copyright.txt in the distribution for a full listing of *
+ * individual contributors. *
+ * *
+ * This is free software; you can redistribute it and/or modify it *
+ * under the terms of the GNU Lesser General Public License as *
+ * published by the Free Software Foundation; either version 2.1 of *
+ * the License, or (at your option) any later version. *
+ * *
+ * This software is distributed in the hope that it will be useful, *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
+ * Lesser General Public License for more details. *
+ * *
+ * You should have received a copy of the GNU Lesser General Public *
+ * License along with this software; if not, write to the Free *
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org. *
+ ******************************************************************************/
+
+package org.jboss.portal.portlet.samples.shoppingcart;
+
+import javax.xml.bind.annotation.XmlRootElement;
+import javax.xml.namespace.QName;
+import java.io.Serializable;
+
+/**
+ * @author <a href="mailto:chris.laprun@jboss.com">Chris
Laprun</a>
+ * @version $Revision: 9912 $
+ */
+@XmlRootElement
+public class CartEvent implements Serializable
+{
+ private String id;
+
+ public static final QName QNAME = new
QName("urn:jboss:portal:samples:event", "CartEvent");
+
+ public CartEvent(String id)
+ {
+ this.id = id;
+ }
+
+ public String getId()
+ {
+ return id;
+ }
+}
Added:
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
(rev 0)
+++
modules/portlet/trunk/samples/src/main/java/org/jboss/portal/portlet/samples/shoppingcart/CartPortlet.java 2008-03-02
22:24:18 UTC (rev 10174)
@@ -0,0 +1,83 @@
+/******************************************************************************
+ * JBoss, a division of Red Hat *
+ * Copyright 2008, Red Hat Middleware, LLC, and individual *
+ * contributors as indicated by the @authors tag. See the *
+ * copyright.txt in the distribution for a full listing of *
+ * individual contributors. *
+ * *
+ * This is free software; you can redistribute it and/or modify it *
+ * under the terms of the GNU Lesser General Public License as *
+ * published by the Free Software Foundation; either version 2.1 of *
+ * the License, or (at your option) any later version. *
+ * *
+ * This software is distributed in the hope that it will be useful, *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
+ * Lesser General Public License for more details. *
+ * *
+ * You should have received a copy of the GNU Lesser General Public *
+ * License along with this software; if not, write to the Free *
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org. *
+ ******************************************************************************/
+
+package org.jboss.portal.portlet.samples.shoppingcart;
+
+import javax.portlet.Event;
+import javax.portlet.EventRequest;
+import javax.portlet.EventResponse;
+import javax.portlet.GenericPortlet;
+import javax.portlet.PortletException;
+import javax.portlet.RenderRequest;
+import javax.portlet.RenderResponse;
+import java.io.IOException;
+import java.io.Writer;
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * @author <a href="mailto:chris.laprun@jboss.com">Chris
Laprun</a>
+ * @version $Revision: 9932 $
+ */
+public class CartPortlet extends GenericPortlet
+{
+ List<CatalogItem> items = new ArrayList<CatalogItem>(7);
+
+ @Override
+ protected void doView(RenderRequest renderRequest, RenderResponse renderResponse)
throws PortletException, IOException
+ {
+ renderResponse.setContentType("text/html");
+ Writer writer = renderResponse.getWriter();
+
+
+ if (!items.isEmpty())
+ {
+ writer.append("<table width='95%'
id=\"cart-table\"><thead><tr align='left'><th
width='66%'>Description</th><th
align='right'>Price</th></tr></thead><tbody>");
+ int total = 0;
+ for (CatalogItem item : items)
+ {
+ int price = item.getPrice();
+ total += price;
+
writer.append("<tr><td>").append(item.getDescription()).append("</td><td
align='right'>$").append("" + price)
+ .append("</td></tr>");
+ }
+
writer.append("<tr><td><b>Total:</b></td><td
align='right' style='border-top: 2px solid #000;'>")
+ .append("$" +
total).append("</td></tr></tbody></table>");
+ }
+ else
+ {
+ writer.append("Cart is empty.");
+ }
+ }
+
+ @Override
+ public void processEvent(EventRequest eventRequest, EventResponse eventResponse)
throws PortletException, IOException
+ {
+ Event event = eventRequest.getEvent();
+ if (event.getName().equals("CartEvent"))
+ {
+ CartEvent cartEvent = (CartEvent)event.getValue();
+ items.add(Catalog.get(cartEvent.getId()));
+ }
+ }
+}
Added:
modules/portlet/trunk/samples/src/main/java/org/jboss/portal/portlet/samples/shoppingcart/Catalog.java
===================================================================
---
modules/portlet/trunk/samples/src/main/java/org/jboss/portal/portlet/samples/shoppingcart/Catalog.java
(rev 0)
+++
modules/portlet/trunk/samples/src/main/java/org/jboss/portal/portlet/samples/shoppingcart/Catalog.java 2008-03-02
22:24:18 UTC (rev 10174)
@@ -0,0 +1,63 @@
+/******************************************************************************
+ * JBoss, a division of Red Hat *
+ * Copyright 2008, Red Hat Middleware, LLC, and individual *
+ * contributors as indicated by the @authors tag. See the *
+ * copyright.txt in the distribution for a full listing of *
+ * individual contributors. *
+ * *
+ * This is free software; you can redistribute it and/or modify it *
+ * under the terms of the GNU Lesser General Public License as *
+ * published by the Free Software Foundation; either version 2.1 of *
+ * the License, or (at your option) any later version. *
+ * *
+ * This software is distributed in the hope that it will be useful, *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
+ * Lesser General Public License for more details. *
+ * *
+ * You should have received a copy of the GNU Lesser General Public *
+ * License along with this software; if not, write to the Free *
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org. *
+ ******************************************************************************/
+
+package org.jboss.portal.portlet.samples.shoppingcart;
+
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * @author <a href="mailto:chris.laprun@jboss.com">Chris
Laprun</a>
+ * @version $Revision: 9912 $
+ */
+public class Catalog
+{
+ private static Map<String, CatalogItem> items;
+
+ static
+ {
+ items = new HashMap<String, CatalogItem>(7);
+ items.put("1", new CatalogItem("1", 800, "Bike"));
+ items.put("2", new CatalogItem("2", 450,
"Snowboard"));
+ items.put("3", new CatalogItem("3", 225, "Tent"));
+ items.put("4", new CatalogItem("4", 75,
"Backpack"));
+ items.put("5", new CatalogItem("5", 119,
"Skateboard"));
+ items.put("6", new CatalogItem("6", 333,
"Surfboard"));
+ items.put("7", new CatalogItem("7", 90,
"Sneakers"));
+ }
+
+ private Catalog()
+ {
+ }
+
+ public static CatalogItem get(String id)
+ {
+ return items.get(id);
+ }
+
+ public static Collection<CatalogItem> getAll()
+ {
+ return items.values();
+ }
+}
Added:
modules/portlet/trunk/samples/src/main/java/org/jboss/portal/portlet/samples/shoppingcart/CatalogItem.java
===================================================================
---
modules/portlet/trunk/samples/src/main/java/org/jboss/portal/portlet/samples/shoppingcart/CatalogItem.java
(rev 0)
+++
modules/portlet/trunk/samples/src/main/java/org/jboss/portal/portlet/samples/shoppingcart/CatalogItem.java 2008-03-02
22:24:18 UTC (rev 10174)
@@ -0,0 +1,57 @@
+/******************************************************************************
+ * JBoss, a division of Red Hat *
+ * Copyright 2008, Red Hat Middleware, LLC, and individual *
+ * contributors as indicated by the @authors tag. See the *
+ * copyright.txt in the distribution for a full listing of *
+ * individual contributors. *
+ * *
+ * This is free software; you can redistribute it and/or modify it *
+ * under the terms of the GNU Lesser General Public License as *
+ * published by the Free Software Foundation; either version 2.1 of *
+ * the License, or (at your option) any later version. *
+ * *
+ * This software is distributed in the hope that it will be useful, *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
+ * Lesser General Public License for more details. *
+ * *
+ * You should have received a copy of the GNU Lesser General Public *
+ * License along with this software; if not, write to the Free *
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org. *
+ ******************************************************************************/
+
+package org.jboss.portal.portlet.samples.shoppingcart;
+
+/**
+ * @author <a href="mailto:chris.laprun@jboss.com">Chris
Laprun</a>
+ * @version $Revision: 9912 $
+ */
+public class CatalogItem
+{
+ private String id;
+ private int price;
+ private String description;
+
+ public CatalogItem(String id, int price, String description)
+ {
+ this.id = id;
+ this.price = price;
+ this.description = description;
+ }
+
+ public String getId()
+ {
+ return id;
+ }
+
+ public int getPrice()
+ {
+ return price;
+ }
+
+ public String getDescription()
+ {
+ return description;
+ }
+}
Added:
modules/portlet/trunk/samples/src/main/java/org/jboss/portal/portlet/samples/shoppingcart/CatalogPortlet.java
===================================================================
---
modules/portlet/trunk/samples/src/main/java/org/jboss/portal/portlet/samples/shoppingcart/CatalogPortlet.java
(rev 0)
+++
modules/portlet/trunk/samples/src/main/java/org/jboss/portal/portlet/samples/shoppingcart/CatalogPortlet.java 2008-03-02
22:24:18 UTC (rev 10174)
@@ -0,0 +1,71 @@
+/******************************************************************************
+ * JBoss, a division of Red Hat *
+ * Copyright 2008, Red Hat Middleware, LLC, and individual *
+ * contributors as indicated by the @authors tag. See the *
+ * copyright.txt in the distribution for a full listing of *
+ * individual contributors. *
+ * *
+ * This is free software; you can redistribute it and/or modify it *
+ * under the terms of the GNU Lesser General Public License as *
+ * published by the Free Software Foundation; either version 2.1 of *
+ * the License, or (at your option) any later version. *
+ * *
+ * This software is distributed in the hope that it will be useful, *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
+ * Lesser General Public License for more details. *
+ * *
+ * You should have received a copy of the GNU Lesser General Public *
+ * License along with this software; if not, write to the Free *
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org. *
+ ******************************************************************************/
+
+package org.jboss.portal.portlet.samples.shoppingcart;
+
+import javax.portlet.ActionRequest;
+import javax.portlet.ActionResponse;
+import javax.portlet.GenericPortlet;
+import javax.portlet.PortletException;
+import javax.portlet.PortletURL;
+import javax.portlet.RenderRequest;
+import javax.portlet.RenderResponse;
+import java.io.IOException;
+import java.io.Writer;
+import java.util.Collection;
+
+/**
+ * @author <a href="mailto:chris.laprun@jboss.com">Chris
Laprun</a>
+ * @version $Revision: 9932 $
+ */
+public class CatalogPortlet extends GenericPortlet
+{
+ @Override
+ protected void doView(RenderRequest renderRequest, RenderResponse renderResponse)
throws PortletException, IOException
+ {
+ renderResponse.setContentType("text/html");
+ Writer writer = renderResponse.getWriter();
+
+ writer.append("<table width='95%'><thead><tr
align='left'><th width='50%'>Description</th><th
width='25%'>Price</th><th>Actions</th></tr></thead><tbody>");
+
+ PortletURL addURL = renderResponse.createActionURL();
+ addURL.setParameter("op", "add");
+
+ Collection<CatalogItem> items = Catalog.getAll();
+ for (CatalogItem item : items)
+ {
+ addURL.setParameter("id", item.getId());
+
writer.append("<tr><td>").append(item.getDescription()).append("</td><td>$").append(""
+ item.getPrice())
+ .append("</td><td><a
href='").append(addURL.toString()).append("' onclick=\"new
Effect.Highlight(document.getElementById('cart-table'))\">Add to
cart</a></td></tr>");
+ }
+
+ writer.append("</tbody></table>");
+ }
+
+ @Override
+ public void processAction(ActionRequest actionRequest, ActionResponse actionResponse)
throws PortletException, IOException
+ {
+ String value = actionRequest.getParameter("id");
+ actionResponse.setEvent(CartEvent.QNAME, new CartEvent(value));
+ }
+}