Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Updates for 1.1 release

...

Next you download the latest release distribution. Launch your browser and enter one of the
following URL's.
Latest Release - http://cwiki.apache.org/TUSCANY/sca-java-releases.html
Download both the bin zip as well as the src zip to the folder that you created on your disk. Once you completed the download you should see the following on your disk.

...

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 Entryvoid put(String id, Entry entry) throws NotFoundException {
		entry.setUpdated(new Date());
		cart.put(id, entry);
		return 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);
	}
}

...

The Text editor will open on the new created html file. Replace the content of the editor by copy-paste
of the following html snippet.

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

	<script type="text/javascript" src="binding-atomstore.js"></script>
	<script type="text/javascript" src="binding-jsonrpc.js"></script>
	<script language="JavaScript">

	//Reference@Reference
	var catalog = (new JSONRpcClientReference("../Catalogcatalog")).Catalog;
	
	//Reference@Reference
	var shoppingCart = new AtomClientReference("../ShoppingCartshoppingCart");


	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;
			if (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><title>cart-item</title>' +
							'<content type=title><content type="text">'+items[i].value+'</content>' +
							'<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="/ufs/store.html">'+
					document.getElementById('shoppingCart').innerHTML+
					'<br>'+
					document.getElementById('total').innerHTML+
					'<br>'+
					'<br>'+
					'<input type="submit" value="Continue Shopping">'+ 
				'</form>';
		shoppingCart.deletedel("", null);
	}
	function deleteCart() {
		shoppingCart.deletedel("", null);
		document.getElementById('shoppingCart').innerHTML = "";
		document.getElementById('total').innerHTML = "";	
	}
	window.onload = function() {	

			catalog.get(catalog_getResponse);
			shoppingCart.get("", shoppingCart_getResponse);
		}
	</script>
	<link rel="stylesheet" type="text/css" href="style.css" />
	</head>
	<body>
	
</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>
		<img src="http://projects.apache.org/images/blue-icon.png" border="0">
		</a>
	</form>
	</div>
	</body>
</html>

Next select the "ufservices" package again. Right click to get the context menu, select New, and
then File. In the New File dialog enter "binding-jsonrpc.js" for the File name, and then select
Finish to complete the dialog.

The Text editor will open on the new created javascript file. Replace the content of the editor by
copy-paste of the javascript snippet you find here:

http://svn.apache.org/repos/asf/incubator/tuscany/java/sca/modules/binding-jsonrpc/src/main/resources/org/apache/tuscany/sca/binding/jsonrpc/jsonrpc.js

Next select the "ufservices" package again. Right click to get the context menu, select New, and
then File. In the New File dialog enter "binding-atom.js" for the File name, and then select Finish
to complete the dialog.

The Text editor will open on the new created javascript file. Replace the content of the editor by
copy-paste of the following javascript snippet.

Code Block

function AtomClient(uri) {
	this.uri=uri;
	this.get = function(id, responseFunction) {
		var xhr = this.createXMLHttpRequest();
		xhr.onreadystatechange = function() {
			if (xhr.readyState == 4) {
				if (xhr.status == 200) {
					if (responseFunction != null)
						responseFunction(xhr.responseXML);
				} else {
				alert("get - Error getting data from the server");
				}
			}
		}
		xhr.open("GET", uri + id, true);
		xhr.send(null);
	}
	this.post = function (entry, responseFunction) {
	var xhr = this.createXMLHttpRequest();
	xhr.onreadystatechange = function() {
		if (xhr.readyState == 4) {
			if (xhr.status == 201) {
				if (responseFunction != null)
					responseFunction(xhr.responseXML);
				} else {
					alert("post - Error getting data from the server");
				}
			}
		}
		xhr.open("POST", uri, true);
		xhr.setRequestHeader("Content-Type", "application/atom+xml");
		xhr.send(entry);
	}
	this.put = function (id, entry, responseFunction) {
		var xhr = this.createXMLHttpRequest();
		xhr.onreadystatechange = function() {
			if (xhr.readyState == 4) {
				if (xhr.status == 200) {
					if (responseFunction != null)
						responseFunction(xhr.responseXML);
				} else {
					alert("put - Error getting data from the server");
				}
			}
		}
		xhr.open("PUT", uri + id, true);
		xhr.setRequestHeader("Content-Type", "application/atom+xml");
		xhr.send(entry);
	}
	this.delete = function (id, responseFunction) {
		var xhr = this.createXMLHttpRequest();
		xhr.onreadystatechange = function() {
			if (xhr.readyState == 4) {
				if (xhr.status == 200) {
					if (responseFunction != null) responseFunction();
				} else {
					alert("delete - Error getting data from the server");
				}
			}
		}
		xhr.open("DELETE", uri + id, true);
		xhr.send(null);
	}
	this.createXMLHttpRequest = function () {
		try {return new XMLHttpRequest();} catch(e) {}
		try {return new ActiveXObject("Msxml2.XMLHTTP");} catch(e) {}
		try {return new ActiveXObject("Microsoft.XMLHTTP");} catch(e) {}
		alert("XML http request not supported");
		return null;
	}
}

...

</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="http://store"
	name="store">
	<component name="ufs="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:implementation.resource location="ufservices"/>binding.jsonrpc/>
		 </reference>
		<service <reference name="shoppingCart" target="ResourceShoppingCart">
		 	<t:binding.httpatom/>
		 </service>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="Collection">
			<t:binding.atom/>
		</service>
	</component>
	<component name="CurrencyConverter">
		<implementation.java class="services.CurrencyConverterImpl"/>
	</component>
</composite>

...

 Next Launch your Web browser and enter the following address:

http://localhost:8080/ufsstore/store.html 

 You get to the Store user facing service of the composite service application.

...