Adding support for multi-user shoppingCart

git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@829709 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
lresende 2009-10-26 06:27:26 +00:00
commit e40410d3e5
6 changed files with 180 additions and 21 deletions

View file

@ -0,0 +1,27 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package services;
import org.oasisopen.sca.annotation.Remotable;
@Remotable
public interface ShoppingCart extends Cart, Total{
}

View file

@ -27,18 +27,12 @@ import java.util.UUID;
import org.apache.tuscany.sca.data.collection.Entry;
import org.apache.tuscany.sca.data.collection.NotFoundException;
import org.oasisopen.sca.annotation.Init;
import org.oasisopen.sca.annotation.Scope;
@Scope("COMPOSITE")
public class ShoppingCartImpl implements Cart, Total {
public class ShoppingCartImpl implements ShoppingCart {
private Map<String, Item> cart;
@Init
public void init() {
cart = new HashMap<String, Item>();
}
private Map<String, Item> cart = new HashMap<String, Item>();
public Entry<String, Item>[] getAll() {
Entry<String, Item>[] entries = new Entry[cart.size()];

View file

@ -0,0 +1,117 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package services;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.logging.Logger;
import org.apache.tuscany.sca.cloud.user.User;
import org.apache.tuscany.sca.cloud.user.UserService;
import org.apache.tuscany.sca.data.collection.Entry;
import org.apache.tuscany.sca.data.collection.NotFoundException;
import org.oasisopen.sca.annotation.Reference;
import org.oasisopen.sca.annotation.Scope;
@Scope("COMPOSITE")
public class ShoppingCartManager implements ShoppingCart {
private static final Logger log = Logger.getLogger(ShoppingCartManager.class.getName());
private static String ANONYMOUS = "anonymous";
private Map<String, ShoppingCart> carts = new HashMap<String, ShoppingCart>();
@Reference
private UserService userService;
public Entry<String, Item>[] getAll() {
return getUserShoppingCart().getAll();
}
public Item get(String key) throws NotFoundException {
return getUserShoppingCart().get(key);
}
public String post(String key, Item item) {
if (key == null || key.length() == 0) {
key = this.generateItemKey();
}
return getUserShoppingCart().post(key, item);
}
public void put(String key, Item item) throws NotFoundException {
getUserShoppingCart().put(key,item);
}
public Entry<String, Item>[] query(String queryString) {
return getUserShoppingCart().query(queryString);
}
public void delete(String key) throws NotFoundException {
this.getUserShoppingCart().delete(key);
}
public String getTotal() {
return this.getUserShoppingCart().getTotal();
}
/**
* Utility functions
*/
private ShoppingCart getUserShoppingCart() {
String key = getCartKey();
ShoppingCart userCart = carts.get(key);
if(userCart == null) {
userCart = new ShoppingCartImpl();
carts.put(key, userCart);
}
return userCart;
}
private String getUserId() {
String userId = null;
if(userService != null) {
try {
User user = userService.getCurrentUser();
userId = user.getUserId();
} catch(Exception e) {
//ignore
e.printStackTrace();
}
}
if(userId == null || userId.length() == 0) {
userId = ANONYMOUS;
}
log.warning("Using userId:" + userId);
return userId;
}
private String getCartKey() {
String cartKey = "cart-" + this.getUserId();
log.warning("Using cartKey:" + cartKey);
return cartKey;
}
private String generateItemKey() {
String itemKey = getCartKey() + "-item-" + UUID.randomUUID().toString();
log.warning("Using itemKey:" + itemKey);
return itemKey;
}
}

View file

@ -48,19 +48,20 @@
</service>
</component>
<component name="ShoppingCart">
<implementation.java class="services.ShoppingCartImpl"/>
<service name="Cart">
<tuscany:binding.jsonrpc uri="/ShoppingCart/Cart"/>
</service>
<service name="Total">
<tuscany:binding.jsonrpc uri="/ShoppingCart/Total"/>
</service>
<component name="ShoppingCartManager">
<implementation.java class="services.ShoppingCartManager"/>
<service name="ShoppingCart">
<tuscany:binding.jsonrpc uri="/ShoppingCart"/>
</service>
<reference name="userService" target="UserService">
<binding.sca/>
</reference>
</component>
<component name="UserService">
<implementation.java class="org.apache.tuscany.sca.cloud.user.impl.GoogleUserService"/>
<service name="UserService">
<binding.sca/>
<tuscany:binding.jsonrpc uri="/User"/>
</service>
</component>

View file

@ -0,0 +1,22 @@
<style type="text/css">
html, body {
height:100%;
margin:0;
overflow:hidden;
width:100%;
}
.header {
font-size:13px;
text-align:right;
color:#CCCCCC;
padding:5px 5px 0;
padding-right:8px;
padding-top:0px !important;
padding-bottom: 0px;
border-bottom: 1px solid black;
}
</style>

View file

@ -35,9 +35,7 @@
var catalog = new dojo.rpc.JsonService("/CatalogAggregator?smd");
var shoppingCart = new dojo.rpc.JsonService("/ShoppingCart/Cart?smd");
var shoppingTotal = new dojo.rpc.JsonService("/ShoppingCart/Total?smd");
var shoppingCart = new dojo.rpc.JsonService("/ShoppingCart?smd");
var userContext;
@ -93,7 +91,7 @@
if (items.length != 0) {
try {
shoppingTotal.getTotal().addCallback(shoppingTotal_getTotalResponse);
shoppingCart.getTotal().addCallback(shoppingCart_getTotalResponse);
}
catch(e){
alert(e);
@ -101,7 +99,7 @@
}
}
function shoppingTotal_getTotalResponse(total,exception) {
function shoppingCart_getTotalResponse(total,exception) {
if(exception) {
alert(exception.message);
return;