Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

In this step you create the Item model object, the Cart and Total service interfaces and the ShoppingCart service implementation.
You follow the same steps that you learned previously to create the interface and implementation.

Create a Java class in the "services" package named "ShoppingCartImplItem" and copy-paste the
following Java class code snippet into it.

Code Block
package services;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import org.apache.tuscany.sca.binding.feed.collection.Collection;
import org.apache.tuscany.sca.binding.feed.collection.NotFoundException;
import com.sun.syndication.feed.atom.Content;
import com.sun.syndication.feed.atom.Entry;
import com.sun.syndication.feed.atom.Feed;
import com.sun.syndication.feed.atom.Link;
public class ShoppingCartImpl implements Collection {
	// needs to change to instance var once conversation scope works
	private static Map<String, Entry> cart = new HashMap<String, Entry>();
	public Feed getFeed() {
		Feed feed = new Feed();
		feed.setTitle("shopping cart");
		Content subtitle = new Content();
		subtitle.setValue("Total : " + getTotal());
		feed.setSubtitle(subtitle);
		feed.getEntries().addAll(cart.values());
		return feed;
	}
	public Entry get(String id) throws NotFoundException {
		return cart.get(id);
	}
	public Entry post(Entry entry) {
		String id = "cart-" + UUID.randomUUID().toString();
		entry.setId(id);
		Link link = new Link();
		link.setRel("edit");
		link.setHref("" + id);
		entry.getOtherLinks().add(link);
		link = new Link();
		link.setRel("alternate");
		link.setHref("" + id);
		entry.getAlternateLinks().add(link);
		entry.setCreated(new Date());
		cart.put(id, entry);
		return entry;
	}
		public void put(String id, Entry entry) throws NotFoundException {
		entry.setUpdated(new Date());
		cart.put(id, entry);
	}
	public void delete(String id) throws NotFoundException {
		if (id.equals(""))
			cart.clear();
		else
			cart.remove(id);
	} 	private String getTotal() {
		float total = 0;
		String symbol = "";
		if (!cart.isEmpty()) {
			Entry entry = cart.values().iterator().next();
			String item = ((Content)entry.getContents().get(0)).getValue();
			symbol = item.substring(item.indexOf("-")+2, item.indexOf("-")+3);
		}
		for (Entry entry : cart.values()) {
			String item = ((Content)entry.getContents().get(0)).getValue();
			total += Float.valueOf(item.substring(item.indexOf("-")+3));
		}
		return symbol + String.valueOf(total);
	}
}

Note: Since the Tuscany conversational support is not ready yet the cart is realized through a hack.
The cart field is defined as static.


public class Item {
    private String name;
    private String price;
    
    public Item() {
    }
    public Item(String name, String price) {
        this.name = name;
        this.price = price;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getPrice() {
        return price;
    }   
    public void setPrice(String price) {
        this.price = price;
    }
}

Create a Java interface in the "services" package named "Cart" and copy-paste the
following code snippet into it.

Code Block

package services;

import org.apache.tuscany.sca.implementation.data.collection.Collection;
import org.osoa.sca.annotations.Remotable;

@Remotable
public interface Cart extends Collection<String, Item> {

}

Create a Java interface in the "services" package named "Total" and copy-paste the
following code snippet into it.

Code Block

package services;

import org.osoa.sca.annotations.Remotable;

@Remotable
public interface Total {
    String getTotal();
}

Create a Java class in the "services" package named "ShoppingCartImpl" and copy-paste the
following Java class code snippet into it.

Code Block

package services;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.apache.tuscany.sca.implementation.data.collection.Entry;
import org.apache.tuscany.sca.implementation.data.collection.NotFoundException;
import org.osoa.sca.annotations.Init;
import org.osoa.sca.annotations.Scope;

@Scope("COMPOSITE")
public class ShoppingCartImpl implements Cart, Total {
    
    private Map<String, Item> cart;
    
    @Init
    protected void init() {
        cart = new HashMap<String, Item>();
    }

    public Entry<String, Item>[] getAll() {
        Entry<String, Item>[] entries = new Entry[cart.size()];
        int i = 0;
        for (Map.Entry<String, Item> e: cart.entrySet()) {
            entries[i++] = new Entry<String, Item>(e.getKey(), e.getValue());
        }
        return entries;
    }

    public Item get(String key) throws NotFoundException {
        Item item = cart.get(key);
        if (item == null) {
            throw new NotFoundException(key);
        } else {
            return item;
        }
    }

    public String post(String key, Item item) {
        if (key == null) {
            key ="cart-" + UUID.randomUUID().toString();
        }
        cart.put(key, item);
        return key;
    }

    public void put(String key, Item item) throws NotFoundException {
        if (!cart.containsKey(key)) {
            throw new NotFoundException(key);
        }
        cart.put(key, item);
    }
    
    public void delete(String key) throws NotFoundException {
        if (key == null || key.equals("")) {
            cart.clear();
        } else {
            Item item = cart.remove(key);
            if (item == null)
                throw new NotFoundException(key);
        }
    }

    public Entry<String, Item>[] query(String queryString) {
        List<Entry<String, Item>> entries = new ArrayList<Entry<String,Item>>();
        if (queryString.startsWith("name=")) {
            String name = queryString.substring(5);
            for (Map.Entry<String, Item> e: cart.entrySet()) {
                Item item = e.getValue();
                if (item.getName().equals(name)) {
                    entries.add(new Entry<String, Item>(e.getKey(), e.getValue()));
                }
            }
        }
        return entries.toArray(new Entry[entries.size()]);
    }
    
    public String getTotal() {
        double total = 0;
        String currencySymbol = "";
        if (!cart.isEmpty()) {
            Item item = cart.values().iterator().next();
            currencySymbol = item.getPrice().substring(0, 1);
        }
        for (Item item : cart.values()) {
            total += Double.valueOf(item.getPrice().substring(1));
        }
        return currencySymbol + String.valueOf(total);
    }
}

Note: Since the Tuscany conversational support is not ready yet the cart is realized through a hack.
The cart field is defined as static.

After completing these steps the content of the "store" project will look as follows.

...

Code Block
<html>
<head>
<title>Store</TITLE>

<script type="text/javascript" src="store.js"></script>

<script language="JavaScript">

	//@Reference
	var catalog = new Reference("catalog");
	
	//@Reference
	var shoppingCart = new Reference("shoppingCart");


	function catalog_getResponse(items) {
		var catalog = "";
		for (var i=0; i<items.length; i++)
			catalog += '<input name="items" type="checkbox" value="' + 
						items[i] + '">' + items[i]+ ' <br>';
		document.getElementById('catalog').innerHTML=catalog;
	}
	
	function shoppingCart_getResponse(feed) {
		if (feed != null) {
			var entries = feed.getElementsByTagName("entry");              
			var list = "";
			for (var i=0; i<entries.length; i++) {
				var item = entries[i].getElementsByTagName("content")[0].firstChild.nodeValue;
				list += item + ' <br>';
			}
			document.getElementById("shoppingCart").innerHTML = list;
			document.getElementById('total').innerHTML = feed.getElementsByTagName("subtitle")[0].firstChild.nodeValue;
		}
	}
	function shoppingCart_postResponse(entry) {
		shoppingCart.get("", shoppingCart_getResponse);
	}				


	function addToCart() {
		var items  = document.catalogForm.items;
		var j = 0;
		for (var i=0; i<items.length; i++)
			if (items[i].checked) {
				var entry = '<entry xmlns="http://www.w3.org/2005/Atom"><title>cart-item</title><content type="text">'+items[i].value+'</content></entry>'
				shoppingCart.post(entry, shoppingCart_postResponse);
				items[i].checked = false;
			}
	}
	function checkoutCart() {
		document.getElementById('store').innerHTML='<h2>' +
				'Thanks for Shopping With Us!</h2>'+
				'<h2>Your Order</h2>'+
				'<form name="orderForm" action="store.html">'+
					document.getElementById('shoppingCart').innerHTML+
					'<br>'+
					document.getElementById('total').innerHTML+
					'<br>'+
					'<br>'+
					'<input type="submit" value="Continue Shopping">'+ 
				'</form>';
		shoppingCart.del("", null);
	}
	function deleteCart() {
		shoppingCart.del("", null);
		document.getElementById('shoppingCart').innerHTML = "";
		document.getElementById('total').innerHTML = "";	
	}	

	catalog.get(catalog_getResponse);
	shoppingCart.get("", shoppingCart_getResponse);
</script>

</head>

<body>
<h1>Store</h1>
  <div id="store">
   	<h2>Catalog</h2>
   	<form name="catalogForm">
		<div id="catalog" ></div>
		<br>
		<input type="button" onClick="addToCart()"  value="Add to Cart">
   	</form>
 
 	<br>
  
   	<h2>Your Shopping Cart</h2>
   	<form name="shoppingCartForm">
		<div id="shoppingCart"></div>
		<br>
		<div id="total"></div>
		<br>		
		<input type="button" onClick="checkoutCart()" value="Checkout"> 
		<input type="button" onClick="deleteCart()" value="Empty">     
	   	<a href="../ShoppingCart/">(feed)</a>
	</form>    
  </div>
</body>
</html>

After completing these steps the content of the "store" project will look as follows.

...

Code Block
<?xml version="1.0" encoding="UTF-8"?>
<composite xmlns="http://www.osoa.org/xmlns/sca/1.0"
	xmlns:t="http://tuscany.apache.org/xmlns/sca/1.0"
	xmlns:s:s="http://store"
        targetNamespace="http://store"
	name="store">
	<component name="store">
		<t:implementation.widget location="ufservices/store.html"/>
		<service name="Widget">
			<t:binding.http/>
		</service>
                <reference name="catalog" target="Catalog">
		 	<t:binding.jsonrpc="ufservices/store.html"/>
		 </reference>
		 <reference <service name="shoppingCart" target="ShoppingCart"Widget">
		 	<t:binding.atomhttp uri="http://localhost:8080/"/>
		 </reference>		
	</component>
	<component name="Catalog">
		<implementation.java class="services.CatalogImpl"/>
		<propertyservice>
                <reference name="currencyCode">USD</property>
		<service namecatalog" target="Catalog">
		 	<t:binding.jsonrpc/>
		 </service>reference>
		 <reference name="currencyConvertershoppingCart" target="CurrencyConverter"/>
	</component>
	<component name="ShoppingCart">
		<implementation.java class="services.ShoppingCartImpl"/>
		<service name="Collection="ShoppingCart/Cart">
		 	<t:binding.atom/>
		 </service>reference>
	</component>
	<component	 <reference name="CurrencyConverter">
		<implementation.java class="services.CurrencyConverterImpl"/>
	</component>
</composite>

After completing these steps the content of the "store" project will look as follows.

Image Removed
 

Launch Services

In this step you create the code to launch the Tuscany runtime with the new store composite
service you created.

Select the "store" project and click on the New Java Package button Image Removed  in the toolbar to start the

package creation dialog. Use the dialog to create a new package named "launch". 
Select the "launch" package. Select the New Java Class button Image Removed  . In the dialog enter "Launch"
as the Name of the class, check the checkbox for creating a main method stub, and then select
Finish to complete the dialog.

The Java editor will open on the new created Java class. Replace the content of the editor by
copy-paste of the following Java class code snippet.

Code Block

package launch;
import org.apache.tuscany.sca.host.embedded.SCADomain;
public class Launch {
	public static void main(String[] args) throws Exception {
		System.out.println("Starting ...");
		SCADomain scaDomain = SCADomain.newInstance("store.composite");
		System.out.println("store.composite ready for big business !!!");
		System.out.println();
		System.in.read();
		scaDomain.close();
	}
}
shoppingTotal" target="ShoppingCart/Total">
		 	<t:binding.jsonrpc/>
		 </reference>
	</component>
	
	<component name="Catalog">
		<implementation.java class="services.CatalogImpl"/>
		<property name="currencyCode">USD</property>
		<service name="Catalog">
			<t:binding.jsonrpc/>
		</service>
		<reference name="currencyConverter" target="CurrencyConverter"/>
	</component>
	
	<component name="ShoppingCart">
		<implementation.java class="services.ShoppingCartImpl"/>
		<service name="Cart">
			<t:binding.atom uri="/ShoppingCart/Cart"/>
		</service>    	
		<service name="Total">
			<t:binding.jsonrpc/>
		</service>    	
	</component>

	<component name="CurrencyConverter">
		<implementation.java class="services.CurrencyConverterImpl"/>
	</component>
</composite>

After  After completing these steps the content of the "store" project will look as follows.

Image RemovedImage Added
 

Congratulations you completed your 1st composite service applications, now its time to take it into
action.

...