summaryrefslogtreecommitdiffstats
path: root/sandbox/sca-cloud-tutorial/store-appengine-webapp/src/services
diff options
context:
space:
mode:
authorlresende <lresende@13f79535-47bb-0310-9956-ffa450edef68>2009-10-26 06:27:26 +0000
committerlresende <lresende@13f79535-47bb-0310-9956-ffa450edef68>2009-10-26 06:27:26 +0000
commite40410d3e5a1491abda7cec2094e3749614dc84a (patch)
tree5373cb0e20b492be7aace2059785c6c948848e12 /sandbox/sca-cloud-tutorial/store-appengine-webapp/src/services
parentaf2857365e659d99dda12bee356681ab7b789822 (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/src/services')
-rw-r--r--sandbox/sca-cloud-tutorial/store-appengine-webapp/src/services/ShoppingCart.java27
-rw-r--r--sandbox/sca-cloud-tutorial/store-appengine-webapp/src/services/ShoppingCartImpl.java10
-rw-r--r--sandbox/sca-cloud-tutorial/store-appengine-webapp/src/services/ShoppingCartManager.java117
3 files changed, 146 insertions, 8 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;
+ }
+}