diff options
Diffstat (limited to 'sandbox/sca-cloud-tutorial/store-assets/src/main/java')
12 files changed, 617 insertions, 0 deletions
diff --git a/sandbox/sca-cloud-tutorial/store-assets/src/main/java/services/Cart.java b/sandbox/sca-cloud-tutorial/store-assets/src/main/java/services/Cart.java new file mode 100644 index 0000000000..3fb5439bc3 --- /dev/null +++ b/sandbox/sca-cloud-tutorial/store-assets/src/main/java/services/Cart.java @@ -0,0 +1,28 @@ +/* + * 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.apache.tuscany.sca.data.collection.Collection; +import org.oasisopen.sca.annotation.Remotable; + +@Remotable +public interface Cart extends Collection<String, Item> { + +} diff --git a/sandbox/sca-cloud-tutorial/store-assets/src/main/java/services/Catalog.java b/sandbox/sca-cloud-tutorial/store-assets/src/main/java/services/Catalog.java new file mode 100644 index 0000000000..b8630370ad --- /dev/null +++ b/sandbox/sca-cloud-tutorial/store-assets/src/main/java/services/Catalog.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 Catalog { + Item[] get(); +} diff --git a/sandbox/sca-cloud-tutorial/store-assets/src/main/java/services/CatalogAggregatorImpl.java b/sandbox/sca-cloud-tutorial/store-assets/src/main/java/services/CatalogAggregatorImpl.java new file mode 100644 index 0000000000..09cae14100 --- /dev/null +++ b/sandbox/sca-cloud-tutorial/store-assets/src/main/java/services/CatalogAggregatorImpl.java @@ -0,0 +1,66 @@ +/* + * 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.Property; +import org.oasisopen.sca.annotation.Reference; +import org.oasisopen.sca.annotation.Scope; +import org.oasisopen.sca.annotation.Service; + +@Service(Catalog.class) +@Scope("COMPOSITE") +public class CatalogAggregatorImpl implements Catalog { + + @Property + public String currencyCode = "USD"; + + @Reference + public CurrencyConverter currencyConverter; + + @Reference + public Catalog fruitsCatalog; + + @Reference + public Catalog vegetablesCatalog; + + public Item[] get() { + String currencySymbol = currencyConverter.getCurrencySymbol(currencyCode); + + Item[] fruits = fruitsCatalog.get(); + Item[] vegetables = vegetablesCatalog.get(); + + Item[] catalog = new Item[fruits.length + vegetables.length]; + int i =0; + for (Item item: fruits) { + double price = Double.valueOf(item.getPrice().substring(1)); + price = currencyConverter.getConversion("USD", currencyCode, price); + catalog[i++] = new Item(item.getName(), currencySymbol + price); + } + + for (Item item: vegetables) { + double price = Double.valueOf(item.getPrice().substring(1)); + price = currencyConverter.getConversion("USD", currencyCode, price); + catalog[i++] = new Item(item.getName(), currencySymbol + price); + } + + return catalog; + } + +} diff --git a/sandbox/sca-cloud-tutorial/store-assets/src/main/java/services/CurrencyConverter.java b/sandbox/sca-cloud-tutorial/store-assets/src/main/java/services/CurrencyConverter.java new file mode 100644 index 0000000000..59435d9099 --- /dev/null +++ b/sandbox/sca-cloud-tutorial/store-assets/src/main/java/services/CurrencyConverter.java @@ -0,0 +1,31 @@ +/* + * 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 CurrencyConverter { + public double getConversion(String fromCurrenycCode, + String toCurrencyCode, + double amount); + + public String getCurrencySymbol(String currencyCode); +} diff --git a/sandbox/sca-cloud-tutorial/store-assets/src/main/java/services/CurrencyConverterImpl.java b/sandbox/sca-cloud-tutorial/store-assets/src/main/java/services/CurrencyConverterImpl.java new file mode 100644 index 0000000000..d185a2fe83 --- /dev/null +++ b/sandbox/sca-cloud-tutorial/store-assets/src/main/java/services/CurrencyConverterImpl.java @@ -0,0 +1,43 @@ +/* + * 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.Service; + +@Service(CurrencyConverter.class) +public class CurrencyConverterImpl implements CurrencyConverter { + public double getConversion(String fromCurrencyCode, + String toCurrencyCode, + double amount) { + if (toCurrencyCode.equals("USD")) + return amount; + else if (toCurrencyCode.equals("EUR")) + return ((double)Math.round(amount * 0.7256 * 100)) /100; + return 0; + } + + public String getCurrencySymbol(String currencyCode) { + if (currencyCode.equals("USD")) + return "$"; + else if (currencyCode.equals("EUR")) + return "E"; //"€"; + return "?"; + } +} diff --git a/sandbox/sca-cloud-tutorial/store-assets/src/main/java/services/FruitsCatalogImpl.java b/sandbox/sca-cloud-tutorial/store-assets/src/main/java/services/FruitsCatalogImpl.java new file mode 100644 index 0000000000..5290623487 --- /dev/null +++ b/sandbox/sca-cloud-tutorial/store-assets/src/main/java/services/FruitsCatalogImpl.java @@ -0,0 +1,38 @@ +package services; + +import java.util.ArrayList; +import java.util.List; + +import org.oasisopen.sca.annotation.Init; +import org.oasisopen.sca.annotation.Property; +import org.oasisopen.sca.annotation.Reference; +import org.oasisopen.sca.annotation.Scope; +import org.oasisopen.sca.annotation.Service; + +@Service(Catalog.class) +@Scope("COMPOSITE") +public class FruitsCatalogImpl implements Catalog { + @Property + public String currencyCode = "USD"; + @Reference + public CurrencyConverter currencyConverter; + + private List<Item> catalog = new ArrayList<Item>(); + + @Init + public void init() { + String currencySymbol = currencyConverter.getCurrencySymbol(currencyCode); + catalog.add(new Item("Apple", currencySymbol + + currencyConverter.getConversion("USD", currencyCode, 2.99))); + catalog.add(new Item("Orange", currencySymbol + + currencyConverter.getConversion("USD", currencyCode, 3.55))); + catalog.add(new Item("Pear", currencySymbol + + currencyConverter.getConversion("USD", currencyCode, 1.55))); + } + + public Item[] get() { + Item[] catalogArray = new Item[catalog.size()]; + catalog.toArray(catalogArray); + return catalogArray; + } +} diff --git a/sandbox/sca-cloud-tutorial/store-assets/src/main/java/services/Item.java b/sandbox/sca-cloud-tutorial/store-assets/src/main/java/services/Item.java new file mode 100644 index 0000000000..74e7239a20 --- /dev/null +++ b/sandbox/sca-cloud-tutorial/store-assets/src/main/java/services/Item.java @@ -0,0 +1,53 @@ +/* + * 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.io.Serializable; + +public class Item implements Serializable { + private static final long serialVersionUID = -5847326138627338217L; + + 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; + } +} diff --git a/sandbox/sca-cloud-tutorial/store-assets/src/main/java/services/ShoppingCart.java b/sandbox/sca-cloud-tutorial/store-assets/src/main/java/services/ShoppingCart.java new file mode 100644 index 0000000000..e4d590484c --- /dev/null +++ b/sandbox/sca-cloud-tutorial/store-assets/src/main/java/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-assets/src/main/java/services/ShoppingCartImpl.java b/sandbox/sca-cloud-tutorial/store-assets/src/main/java/services/ShoppingCartImpl.java new file mode 100644 index 0000000000..000e205948 --- /dev/null +++ b/sandbox/sca-cloud-tutorial/store-assets/src/main/java/services/ShoppingCartImpl.java @@ -0,0 +1,109 @@ +/* + * 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.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.UUID; +import java.util.logging.Logger; + +import org.apache.tuscany.sca.data.collection.Entry; +import org.apache.tuscany.sca.data.collection.NotFoundException; +import org.oasisopen.sca.annotation.Scope; + +@Scope("COMPOSITE") +public class ShoppingCartImpl implements ShoppingCart { + private static final Logger log = Logger.getLogger(ShoppingCartImpl.class.getName()); + + //private DatastoreDocumentServiceImpl cart = new DatastoreDocumentServiceImpl(); + private Map<String, Item> 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.length() == 0) { + key ="cart-" + UUID.randomUUID().toString(); + } + cart.put(key, item); + return key; + } + + public void put(String key, Item item) throws NotFoundException { + if (!cart.containsKey(key)) { + throw 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, 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 = 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 += Double.valueOf(item.getPrice().substring(1)); + } + return currencySymbol + String.valueOf(total); + } +} diff --git a/sandbox/sca-cloud-tutorial/store-assets/src/main/java/services/ShoppingCartManager.java b/sandbox/sca-cloud-tutorial/store-assets/src/main/java/services/ShoppingCartManager.java new file mode 100644 index 0000000000..be2c9216c8 --- /dev/null +++ b/sandbox/sca-cloud-tutorial/store-assets/src/main/java/services/ShoppingCartManager.java @@ -0,0 +1,114 @@ +/* + * 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; + } + return userId; + } + private String getCartKey() { + String cartKey = "cart-" + this.getUserId(); + return cartKey; + } + private String generateItemKey() { + String itemKey = getCartKey() + "-item-" + UUID.randomUUID().toString(); + return itemKey; + } +} diff --git a/sandbox/sca-cloud-tutorial/store-assets/src/main/java/services/Total.java b/sandbox/sca-cloud-tutorial/store-assets/src/main/java/services/Total.java new file mode 100644 index 0000000000..b77cc1c7ac --- /dev/null +++ b/sandbox/sca-cloud-tutorial/store-assets/src/main/java/services/Total.java @@ -0,0 +1,29 @@ +/* + * 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 Total { + + String getTotal(); + +} diff --git a/sandbox/sca-cloud-tutorial/store-assets/src/main/java/services/VegetablesCatalogImpl.java b/sandbox/sca-cloud-tutorial/store-assets/src/main/java/services/VegetablesCatalogImpl.java new file mode 100644 index 0000000000..6ca311b42e --- /dev/null +++ b/sandbox/sca-cloud-tutorial/store-assets/src/main/java/services/VegetablesCatalogImpl.java @@ -0,0 +1,52 @@ +/* + * 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.ArrayList; +import java.util.List; + +import org.oasisopen.sca.annotation.Init; +import org.oasisopen.sca.annotation.Property; +import org.oasisopen.sca.annotation.Reference; + +public class VegetablesCatalogImpl implements Catalog { + + @Property + public String currencyCode = "USD"; + + @Reference + public CurrencyConverter currencyConverter; + + private List<Item> catalog = new ArrayList<Item>(); + + @Init + public void init() { + String currencySymbol = currencyConverter.getCurrencySymbol(currencyCode); + catalog.add(new Item("Broccoli", currencySymbol + currencyConverter.getConversion("USD", currencyCode, 2.99))); + catalog.add(new Item("Asparagus", currencySymbol + currencyConverter.getConversion("USD", currencyCode, 3.55))); + catalog.add(new Item("Cauliflower", currencySymbol + currencyConverter.getConversion("USD", currencyCode, 1.55))); + } + + public Item[] get() { + Item[] catalogArray = new Item[catalog.size()]; + catalog.toArray(catalogArray); + return catalogArray; + } +} |