Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3

Ready, Set, Go - Getting started with Tuscany

Info

If you are using Tuscany SCA Java 1.1, please see version 1.1 of the guide

(star) "Get Started with Store Demo in Eclipse video"
This guide shows you how to get started with Tuscany in Eclipse by installing the Eclipse plugins and creating a web store shopping cart with SCA technology.

Include Page
Tools - Install the Latest Tuscany Eclipse Plugin
Tools - Install the Latest Tuscany Eclipse Plugin
Include Page
TUSCANY:Tools - Install Tuscany Eclipse Plugins 1.2TUSCANY:Tools - Install Tuscany Eclipse Plugins 1.2

Create your 1st Composite Service Application

...

Code Block
package services;

import org.osoa.sca.annotations.Remotable;

@Remotable
public interface Catalog {
    String[] 	Item[] get();
}

Select the "services" package again. Select the New Java Class button . In the dialog enter
"CatalogImpl" as the Name of the class, add "Catalog" as the interface this class implements, and
then select Finish to complete the dialog.

...

Code Block
package services;

import java.util.ArrayList;
import java.util.List;

import org.osoa.sca.annotations.Init;
import org.osoa.sca.annotations.Property;
import org.osoa.sca.annotations.Reference;

public class CatalogImpl implements Catalog {
	@Property
	public String currencyCode = "USD";
	@Reference
	public CurrencyConverter currencyConverter;

	private List<String>List<Item> catalog = new ArrayList<String>ArrayList<Item>();

	@Init
	public void init() {
		String currencySymbol = currencyConverter
				.getCurrencySymbol(currencyCode);
		catalog.add(new Item("Apple - ", + currencySymbol +
				+ currencyConverter.getConversion("USD", currencyCode, 2.99f99)));
		catalog.add(new Item("Orange - ", + currencySymbol +
				+ currencyConverter.getConversion("USD", currencyCode, 3.55f55)));
		catalog.add(new Item("Pear - ", + currencySymbol +
				+ currencyConverter.getConversion("USD", currencyCode, 1.55f55)));
	}

	public StringItem[] get() {
		StringItem[] catalogArray = new StringItem[catalog.size()];
		catalog.toArray(catalogArray);
		return catalogArray;
	}
}

...

Code Block
package services;

import org.osoa.sca.annotations.Remotable;

@Remotable
public interface CurrencyConverter {
	    public floatdouble getConversion(String fromCurrenycCode,
	 String toCurrencyCode, floatdouble amount);

	    public String getCurrencySymbol(String currencyCode);
}

...

Code Block
package services;

public class CurrencyConverterImpl implements CurrencyConverter {
	    public floatdouble getConversion(String fromCurrencyCode,
		String toCurrencyCode, floatdouble amount) {
		        if (toCurrencyCode.equals("USD"))
			            return amount;
		else 			if         else if (toCurrencyCode.equals("EUR"))
				return amount*0.7256f;
		            return ((double)Math.round(amount * 0.7256 * 100)) /100;
        return 0;
	    }

	    public String getCurrencySymbol(String currencyCode) {
		if (currencyCode.        if (currencyCode.equals("USD"))
			            return "$";
		else
			        else if (currencyCode.equals("EUR"))
				return "€";
		            return "E"; //"€";
        return "?";
	    }
}

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

...

Code Block
package services;

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> {

}

...

Code Block
package services;

import org.osoa.sca.annotations.Remotable;

@Remotable
public interface Total {
    String getTotal	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
	public    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            key ="cart= "cart-" + UUID.randomUUID().toString();
        }
        		}
		cart.put(key, item);
        		return key;
    	}

    	public void put(String key, Item item) throws NotFoundException {
        		if (!cart.containsKey(key)) {
			throw            throw new NotFoundException(key);
        }
        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	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               Item item = e.getValue();
                = 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            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.

...

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

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

<script language="JavaScript">

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


	function catalog_getResponse(items) {	//@Reference
		var catalogshoppingTotal = new tuscany.sca.Reference("shoppingTotal");
	
	var catalogItems;

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


	function addToCart(	list += name + ' - ' + price + ' <br>';
			}
			document.getElementById("shoppingCart").innerHTML = list;

			if (entries.length != 0) {			
				shoppingTotal.getTotal(shoppingTotal_getTotalResponse);
			}
		}
	}
	
	function shoppingTotal_getTotalResponse(total) {
		var items  = document.catalogForm.itemsdocument.getElementById('total').innerHTML = total;
	}
	
	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+/Atom"><title>item</title><content type="text/xml">' +
                	'<Item xmlns="http://services/">' +
                	'<name xmlns="">' + catalogItems[i].name + '</name>' + '<price xmlns="">' + catalogItems[i].price + '</price>' +
                 	'</Item>' + '</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 = "";	
	}	

	function init() {
		catalog.get(catalog_getResponse);
		shoppingCart.get("", shoppingCart_getResponse);
	}
	
</script>

</head>

<body><body onload="init()">
<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/Cart/">(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="http://store"
         
	targetNamespace="http://store" 
	name="store">
	
	<component name="store">
		<t:implementation.widget location="ufservices/store.html" />
		<service name="Widget">
			<t:binding.http uri="http://localhost:8080/store" />
		</service>
                		<reference name="catalog" target="Catalog">
		 	<t:binding.jsonrpc />
		 </reference>
		 <reference name="shoppingCart" target="ShoppingCart/Cart">
		 	<t:binding.atom />
		 </reference>
		 <reference name="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:binding.jsonrpc uri="http://localhost:8080/Catalog"/>
		</service>
		<reference name="currencyConverter" target="CurrencyConverter" />
	</component>
	
	<component name="ShoppingCart">
		<implementation.java class="services.ShoppingCartImpl" />
		<service name="Cart">
			<t:binding.atom uri="http://localhost:8080/ShoppingCart/Cart" />
		</service>    	
		<service name="Total">
			<t:binding.jsonrpc uri="http://localhost:8080/Total"/>
		</service>    	
	</component>

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

...

First select the "store.composite" file. Right click to get the context menu, select Run As, and then Tuscany.
The Tuscany runtime will start up adding the store composition to its domain.

...

 Next Launch your Web browser and enter the following address:

http://localhost:81008080/store/store.html 

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

...

You can select items from the Catalog and add them to your Shopping Cart.Note: When adding items for the first time you will be asked for userid and password by the
browser. Enter "admin" for both.

Since the ShoppingCart service is bound using the ATOM binding, you can also look at the
shopping card content in ATOM feed form by clicking on the feed icon . You get the browsers default rendering for ATOM feeds.

...