diff options
author | lresende <lresende@13f79535-47bb-0310-9956-ffa450edef68> | 2009-10-26 06:27:26 +0000 |
---|---|---|
committer | lresende <lresende@13f79535-47bb-0310-9956-ffa450edef68> | 2009-10-26 06:27:26 +0000 |
commit | e40410d3e5a1491abda7cec2094e3749614dc84a (patch) | |
tree | 5373cb0e20b492be7aace2059785c6c948848e12 /sandbox/sca-cloud-tutorial/store-appengine-webapp | |
parent | af2857365e659d99dda12bee356681ab7b789822 (diff) |
Adding support for multi-user shoppingCart
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@829709 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'sandbox/sca-cloud-tutorial/store-appengine-webapp')
6 files changed, 180 insertions, 21 deletions
diff --git a/sandbox/sca-cloud-tutorial/store-appengine-webapp/src/services/ShoppingCart.java b/sandbox/sca-cloud-tutorial/store-appengine-webapp/src/services/ShoppingCart.java new file mode 100644 index 0000000000..e4d590484c --- /dev/null +++ b/sandbox/sca-cloud-tutorial/store-appengine-webapp/src/services/ShoppingCart.java @@ -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{ + +} diff --git a/sandbox/sca-cloud-tutorial/store-appengine-webapp/src/services/ShoppingCartImpl.java b/sandbox/sca-cloud-tutorial/store-appengine-webapp/src/services/ShoppingCartImpl.java index d06ee5b61b..cfa9dd7435 100644 --- a/sandbox/sca-cloud-tutorial/store-appengine-webapp/src/services/ShoppingCartImpl.java +++ b/sandbox/sca-cloud-tutorial/store-appengine-webapp/src/services/ShoppingCartImpl.java @@ -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()]; diff --git a/sandbox/sca-cloud-tutorial/store-appengine-webapp/src/services/ShoppingCartManager.java b/sandbox/sca-cloud-tutorial/store-appengine-webapp/src/services/ShoppingCartManager.java new file mode 100644 index 0000000000..90f25d214c --- /dev/null +++ b/sandbox/sca-cloud-tutorial/store-appengine-webapp/src/services/ShoppingCartManager.java @@ -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; + } +} diff --git a/sandbox/sca-cloud-tutorial/store-appengine-webapp/src/store.composite b/sandbox/sca-cloud-tutorial/store-appengine-webapp/src/store.composite index 255857ffd4..4fac764518 100644 --- a/sandbox/sca-cloud-tutorial/store-appengine-webapp/src/store.composite +++ b/sandbox/sca-cloud-tutorial/store-appengine-webapp/src/store.composite @@ -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> diff --git a/sandbox/sca-cloud-tutorial/store-appengine-webapp/war/store.css b/sandbox/sca-cloud-tutorial/store-appengine-webapp/war/store.css new file mode 100644 index 0000000000..0b1d541bea --- /dev/null +++ b/sandbox/sca-cloud-tutorial/store-appengine-webapp/war/store.css @@ -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>
\ No newline at end of file diff --git a/sandbox/sca-cloud-tutorial/store-appengine-webapp/war/store.html b/sandbox/sca-cloud-tutorial/store-appengine-webapp/war/store.html index c28e417913..050d451899 100644 --- a/sandbox/sca-cloud-tutorial/store-appengine-webapp/war/store.html +++ b/sandbox/sca-cloud-tutorial/store-appengine-webapp/war/store.html @@ -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; |