From 6d685c8e138af8d18bc71181ec630ccb9a17bdd9 Mon Sep 17 00:00:00 2001 From: nash Date: Wed, 6 Apr 2011 22:03:35 +0000 Subject: Tag for 1.6.2-RC1 git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@1089647 13f79535-47bb-0310-9956-ffa450edef68 --- .../tutorials/store/assets/services/Cart.java | 28 ++++ .../tutorials/store/assets/services/Catalog.java | 27 ++++ .../store/assets/services/CurrencyConverter.java | 29 ++++ .../assets/services/CurrencyConverterImpl.java | 38 +++++ .../store/assets/services/FruitsCatalogImpl.java | 52 ++++++ .../tutorials/store/assets/services/Item.java | 66 ++++++++ .../tutorials/store/assets/services/Order.java | 40 +++++ .../store/assets/services/ShoppingCartImpl.java | 129 +++++++++++++++ .../tutorials/store/assets/services/Total.java | 30 ++++ .../assets/services/VegetablesCatalogImpl.java | 42 +++++ .../tutorials/store/assets/services/Warehouse.java | 28 ++++ .../assets/services/db/ShoppingCartTableImpl.java | 175 +++++++++++++++++++++ .../tutorials/store/assets/services/db/cart.sql | 27 ++++ .../assets/services/map/FruitsCatalogImpl.java | 56 +++++++ .../assets/services/market/MarketCatalogImpl.java | 66 ++++++++ .../assets/services/merger/MergedCatalogImpl.java | 66 ++++++++ 16 files changed, 899 insertions(+) create mode 100644 sca-java-1.x/tags/1.6.2-RC1/tutorials/store/assets/services/Cart.java create mode 100644 sca-java-1.x/tags/1.6.2-RC1/tutorials/store/assets/services/Catalog.java create mode 100644 sca-java-1.x/tags/1.6.2-RC1/tutorials/store/assets/services/CurrencyConverter.java create mode 100644 sca-java-1.x/tags/1.6.2-RC1/tutorials/store/assets/services/CurrencyConverterImpl.java create mode 100644 sca-java-1.x/tags/1.6.2-RC1/tutorials/store/assets/services/FruitsCatalogImpl.java create mode 100644 sca-java-1.x/tags/1.6.2-RC1/tutorials/store/assets/services/Item.java create mode 100644 sca-java-1.x/tags/1.6.2-RC1/tutorials/store/assets/services/Order.java create mode 100644 sca-java-1.x/tags/1.6.2-RC1/tutorials/store/assets/services/ShoppingCartImpl.java create mode 100644 sca-java-1.x/tags/1.6.2-RC1/tutorials/store/assets/services/Total.java create mode 100644 sca-java-1.x/tags/1.6.2-RC1/tutorials/store/assets/services/VegetablesCatalogImpl.java create mode 100644 sca-java-1.x/tags/1.6.2-RC1/tutorials/store/assets/services/Warehouse.java create mode 100644 sca-java-1.x/tags/1.6.2-RC1/tutorials/store/assets/services/db/ShoppingCartTableImpl.java create mode 100644 sca-java-1.x/tags/1.6.2-RC1/tutorials/store/assets/services/db/cart.sql create mode 100644 sca-java-1.x/tags/1.6.2-RC1/tutorials/store/assets/services/map/FruitsCatalogImpl.java create mode 100644 sca-java-1.x/tags/1.6.2-RC1/tutorials/store/assets/services/market/MarketCatalogImpl.java create mode 100644 sca-java-1.x/tags/1.6.2-RC1/tutorials/store/assets/services/merger/MergedCatalogImpl.java (limited to 'sca-java-1.x/tags/1.6.2-RC1/tutorials/store/assets/services') diff --git a/sca-java-1.x/tags/1.6.2-RC1/tutorials/store/assets/services/Cart.java b/sca-java-1.x/tags/1.6.2-RC1/tutorials/store/assets/services/Cart.java new file mode 100644 index 0000000000..9e6226d963 --- /dev/null +++ b/sca-java-1.x/tags/1.6.2-RC1/tutorials/store/assets/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.osoa.sca.annotations.Remotable; + +@Remotable +public interface Cart extends Collection { + +} diff --git a/sca-java-1.x/tags/1.6.2-RC1/tutorials/store/assets/services/Catalog.java b/sca-java-1.x/tags/1.6.2-RC1/tutorials/store/assets/services/Catalog.java new file mode 100644 index 0000000000..2c3b19f579 --- /dev/null +++ b/sca-java-1.x/tags/1.6.2-RC1/tutorials/store/assets/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.osoa.sca.annotations.Remotable; + +@Remotable +public interface Catalog { + Item[] get(); +} diff --git a/sca-java-1.x/tags/1.6.2-RC1/tutorials/store/assets/services/CurrencyConverter.java b/sca-java-1.x/tags/1.6.2-RC1/tutorials/store/assets/services/CurrencyConverter.java new file mode 100644 index 0000000000..e104a0423a --- /dev/null +++ b/sca-java-1.x/tags/1.6.2-RC1/tutorials/store/assets/services/CurrencyConverter.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.osoa.sca.annotations.Remotable; + +@Remotable +public interface CurrencyConverter { + public double getConversion(String fromCurrenycCode, String toCurrencyCode, double amount); + + public String getCurrencySymbol(String currencyCode); +} diff --git a/sca-java-1.x/tags/1.6.2-RC1/tutorials/store/assets/services/CurrencyConverterImpl.java b/sca-java-1.x/tags/1.6.2-RC1/tutorials/store/assets/services/CurrencyConverterImpl.java new file mode 100644 index 0000000000..c354aed447 --- /dev/null +++ b/sca-java-1.x/tags/1.6.2-RC1/tutorials/store/assets/services/CurrencyConverterImpl.java @@ -0,0 +1,38 @@ +/* + * 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; + +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/sca-java-1.x/tags/1.6.2-RC1/tutorials/store/assets/services/FruitsCatalogImpl.java b/sca-java-1.x/tags/1.6.2-RC1/tutorials/store/assets/services/FruitsCatalogImpl.java new file mode 100644 index 0000000000..377b3d7e59 --- /dev/null +++ b/sca-java-1.x/tags/1.6.2-RC1/tutorials/store/assets/services/FruitsCatalogImpl.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.osoa.sca.annotations.Init; +import org.osoa.sca.annotations.Property; +import org.osoa.sca.annotations.Reference; + +public class FruitsCatalogImpl implements Catalog { + + @Property + public String currencyCode = "USD"; + + @Reference + public CurrencyConverter currencyConverter; + + private List catalog = new ArrayList(); + + @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/sca-java-1.x/tags/1.6.2-RC1/tutorials/store/assets/services/Item.java b/sca-java-1.x/tags/1.6.2-RC1/tutorials/store/assets/services/Item.java new file mode 100644 index 0000000000..81cefcdbef --- /dev/null +++ b/sca-java-1.x/tags/1.6.2-RC1/tutorials/store/assets/services/Item.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; + + +public class Item { + private String name; + private String price; + private String origin; + + public Item() { + } + + public Item(String name, String price, String origin) { + this.name = name; + this.price = price; + this.origin = origin; + } + + 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; + } + + public String getOrigin() { + return origin; + } + + public void setOrigin(String origin) { + this.origin = origin; + } + +} diff --git a/sca-java-1.x/tags/1.6.2-RC1/tutorials/store/assets/services/Order.java b/sca-java-1.x/tags/1.6.2-RC1/tutorials/store/assets/services/Order.java new file mode 100644 index 0000000000..07a9f9d0b0 --- /dev/null +++ b/sca-java-1.x/tags/1.6.2-RC1/tutorials/store/assets/services/Order.java @@ -0,0 +1,40 @@ +/* + * 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; + + +public class Order { + private Item[] items; + + public Order() { + } + + public Order(Item[] items) { + this.items = items; + } + + public Item[] getItems() { + return items; + } + + public void setItems(Item[] items) { + this.items = items; + } +} diff --git a/sca-java-1.x/tags/1.6.2-RC1/tutorials/store/assets/services/ShoppingCartImpl.java b/sca-java-1.x/tags/1.6.2-RC1/tutorials/store/assets/services/ShoppingCartImpl.java new file mode 100644 index 0000000000..d297a8ccf1 --- /dev/null +++ b/sca-java-1.x/tags/1.6.2-RC1/tutorials/store/assets/services/ShoppingCartImpl.java @@ -0,0 +1,129 @@ +/* + * 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 org.apache.tuscany.sca.data.collection.Entry; +import org.apache.tuscany.sca.data.collection.NotFoundException; +import org.osoa.sca.annotations.Init; +import org.osoa.sca.annotations.Reference; +import org.osoa.sca.annotations.Scope; + +@Scope("COMPOSITE") +public class ShoppingCartImpl implements Cart, Total { + + private Map cart; + + @Reference(required=false) + protected Warehouse warehouse; + + @Init + public void init() { + cart = new HashMap(); + } + + public Entry[] getAll() { + Entry[] entries = new Entry[cart.size()]; + int i = 0; + for (Map.Entry e: cart.entrySet()) { + entries[i++] = new Entry(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 ="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[] query(String queryString) { + List> entries = new ArrayList>(); + if (queryString.startsWith("name=")) { + String name = queryString.substring(5); + for (Map.Entry e: cart.entrySet()) { + Item item = e.getValue(); + if (item.getName().equals(name)) { + entries.add(new Entry(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); + } + + public void confirmTotal() { + if (warehouse != null){ + System.out.println("Confirm total"); + try { + Order order = new Order(cart.values().toArray(new Item[cart.values().size()])); + warehouse.addOrder(order); + } catch (Exception ex){ + ex.printStackTrace(); + } + } + cart.clear(); + } +} diff --git a/sca-java-1.x/tags/1.6.2-RC1/tutorials/store/assets/services/Total.java b/sca-java-1.x/tags/1.6.2-RC1/tutorials/store/assets/services/Total.java new file mode 100644 index 0000000000..b29b321e25 --- /dev/null +++ b/sca-java-1.x/tags/1.6.2-RC1/tutorials/store/assets/services/Total.java @@ -0,0 +1,30 @@ +/* + * 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.osoa.sca.annotations.Remotable; + +@Remotable +public interface Total { + + String getTotal(); + void confirmTotal(); + +} diff --git a/sca-java-1.x/tags/1.6.2-RC1/tutorials/store/assets/services/VegetablesCatalogImpl.java b/sca-java-1.x/tags/1.6.2-RC1/tutorials/store/assets/services/VegetablesCatalogImpl.java new file mode 100644 index 0000000000..8bfbc757db --- /dev/null +++ b/sca-java-1.x/tags/1.6.2-RC1/tutorials/store/assets/services/VegetablesCatalogImpl.java @@ -0,0 +1,42 @@ +/* + * 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.osoa.sca.annotations.Init; + +public class VegetablesCatalogImpl implements Catalog { + private List catalog = new ArrayList(); + + @Init + public void init() { + catalog.add(new Item("Broccoli", "$2.99")); + catalog.add(new Item("Asparagus", "$3.55")); + catalog.add(new Item("Cauliflower", "$1.55")); + } + + public Item[] get() { + Item[] catalogArray = new Item[catalog.size()]; + catalog.toArray(catalogArray); + return catalogArray; + } +} diff --git a/sca-java-1.x/tags/1.6.2-RC1/tutorials/store/assets/services/Warehouse.java b/sca-java-1.x/tags/1.6.2-RC1/tutorials/store/assets/services/Warehouse.java new file mode 100644 index 0000000000..f04b0dd52d --- /dev/null +++ b/sca-java-1.x/tags/1.6.2-RC1/tutorials/store/assets/services/Warehouse.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.osoa.sca.annotations.Remotable; + +@Remotable +public interface Warehouse { + void addOrder(Order order); + Order[] getOrders(); +} diff --git a/sca-java-1.x/tags/1.6.2-RC1/tutorials/store/assets/services/db/ShoppingCartTableImpl.java b/sca-java-1.x/tags/1.6.2-RC1/tutorials/store/assets/services/db/ShoppingCartTableImpl.java new file mode 100644 index 0000000000..24eca045ab --- /dev/null +++ b/sca-java-1.x/tags/1.6.2-RC1/tutorials/store/assets/services/db/ShoppingCartTableImpl.java @@ -0,0 +1,175 @@ +/* + * 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.db; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; +import java.util.ArrayList; +import java.util.List; +import java.util.UUID; + +import org.apache.tuscany.sca.data.collection.Entry; +import org.apache.tuscany.sca.data.collection.NotFoundException; +import org.osoa.sca.ServiceRuntimeException; +import org.osoa.sca.annotations.Destroy; +import org.osoa.sca.annotations.Init; +import org.osoa.sca.annotations.Property; + +import services.Cart; +import services.Item; +import services.Total; + +public class ShoppingCartTableImpl implements Cart, Total { + + @Property + public String database; + + private Connection connection; + + @Init + public void init() throws Exception { + Class.forName("org.apache.derby.jdbc.EmbeddedDriver", true, Thread.currentThread().getContextClassLoader()); + String baseDir = System.getProperty("basedir"); + String url = "jdbc:derby:directory:" + (baseDir != null? baseDir + "/" + database : database); + System.out.println("Connecting to database: " + url); + connection = DriverManager.getConnection(url, "", ""); + } + + @Destroy + public void shutdown() throws Exception { + if(connection != null) { + connection.close(); + connection = null; + } + } + + public Entry[] getAll() { + try { + Statement statement = connection.createStatement(); + ResultSet results = statement.executeQuery("select * from Cart"); + List> entries = new ArrayList>(); + while (results.next()) { + Item item = new Item(results.getString("name"), results.getString("price")); + entries.add(new Entry(results.getString("id"), item)); + } + return entries.toArray(new Entry[entries.size()]); + } catch (SQLException e) { + throw new ServiceRuntimeException(e); + } + } + + public Item get(String key) throws NotFoundException { + try { + Statement statement = connection.createStatement(); + ResultSet results = statement.executeQuery("select * from Cart where id = '" + key + "'"); + if (results.next()) { + return new Item(results.getString("name"), results.getString("price")); + } else { + throw new NotFoundException(key); + } + } catch (SQLException e) { + throw new ServiceRuntimeException(e); + } + } + + public String post(String key, Item item) { + if (key == null) { + key = "cart-" + UUID.randomUUID().toString(); + } + try { + Statement statement = connection.createStatement(); + String query = "insert into Cart values ('" + key + "', '" + item.getName() + "', '" + item.getPrice() + "')"; + System.out.println(query); + statement.executeUpdate(query); + } catch (SQLException e) { + throw new ServiceRuntimeException(e); + } + return key; + } + + public void put(String key, Item item) throws NotFoundException { + try { + Statement statement = connection.createStatement(); + String query = "update into Cart set name = '" + item.getName() + "', price = '" + item.getPrice() + "' where id = '" + key + "'"; + System.out.println(query); + int count = statement.executeUpdate(query); + if (count == 0) + throw new NotFoundException(key); + } catch (SQLException e) { + throw new ServiceRuntimeException(e); + } + } + + public void delete(String key) throws NotFoundException { + try { + Statement statement = connection.createStatement(); + if (key == null || key.equals("")) { + String query = "delete from Cart"; + System.out.println(query); + statement.executeUpdate(query); + } else { + String query = "delete from Cart where id = '" + key + "'"; + System.out.println(query); + int count = statement.executeUpdate(query); + if (count == 0) + throw new NotFoundException(key); + } + } catch (SQLException e) { + throw new ServiceRuntimeException(e); + } + } + + public Entry[] query(String queryString) { + try { + Statement statement = connection.createStatement(); + ResultSet results = statement.executeQuery("select * from Cart where " + queryString); + List> entries = new ArrayList>(); + while (results.next()) { + Item item = new Item(results.getString("name"), results.getString("price")); + entries.add(new Entry(results.getString("id"), item)); + } + return entries.toArray(new Entry[entries.size()]); + } catch (SQLException e) { + throw new ServiceRuntimeException(e); + } + } + + public String getTotal() { + Entry[] entries = getAll(); + double total = 0; + String currencySymbol = ""; + if (entries.length > 0) { + Item item = entries[0].getData(); + currencySymbol = item.getPrice().substring(0, 1); + } + for (Entry entry : entries) { + Item item = entry.getData(); + total += Double.valueOf(item.getPrice().substring(1)); + } + return currencySymbol + total; + } + + public void confirmTotal() { + + } +} diff --git a/sca-java-1.x/tags/1.6.2-RC1/tutorials/store/assets/services/db/cart.sql b/sca-java-1.x/tags/1.6.2-RC1/tutorials/store/assets/services/db/cart.sql new file mode 100644 index 0000000000..750e23ebde --- /dev/null +++ b/sca-java-1.x/tags/1.6.2-RC1/tutorials/store/assets/services/db/cart.sql @@ -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. +-- + +DROP TABLE CART; + +CREATE TABLE CART( + id VARCHAR(50) NOT NULL, + name VARCHAR(50), + price VARCHAR(10), + primary key (id) +); diff --git a/sca-java-1.x/tags/1.6.2-RC1/tutorials/store/assets/services/map/FruitsCatalogImpl.java b/sca-java-1.x/tags/1.6.2-RC1/tutorials/store/assets/services/map/FruitsCatalogImpl.java new file mode 100644 index 0000000000..b160ba7be0 --- /dev/null +++ b/sca-java-1.x/tags/1.6.2-RC1/tutorials/store/assets/services/map/FruitsCatalogImpl.java @@ -0,0 +1,56 @@ +/* + * 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.map; + +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; + +import services.Catalog; +import services.CurrencyConverter; +import services.Item; + +public class FruitsCatalogImpl implements Catalog { + + @Property + public String currencyCode = "USD"; + + @Reference + public CurrencyConverter currencyConverter; + + private List catalog = new ArrayList(); + + @Init + public void init() { + String currencySymbol = currencyConverter.getCurrencySymbol(currencyCode); + catalog.add(new Item("Apple", currencySymbol + currencyConverter.getConversion("USD", currencyCode, 2.99), "34.425744,-119.711151")); + catalog.add(new Item("Orange", currencySymbol + currencyConverter.getConversion("USD", currencyCode, 3.55), "25.811018,-80.130844")); + catalog.add(new Item("Pear", currencySymbol + currencyConverter.getConversion("USD", currencyCode, 1.55), "36.596649,-121.8964")); + } + + public Item[] get() { + Item[] catalogArray = new Item[catalog.size()]; + catalog.toArray(catalogArray); + return catalogArray; + } +} diff --git a/sca-java-1.x/tags/1.6.2-RC1/tutorials/store/assets/services/market/MarketCatalogImpl.java b/sca-java-1.x/tags/1.6.2-RC1/tutorials/store/assets/services/market/MarketCatalogImpl.java new file mode 100644 index 0000000000..bd2c7d76a2 --- /dev/null +++ b/sca-java-1.x/tags/1.6.2-RC1/tutorials/store/assets/services/market/MarketCatalogImpl.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.market; + +import java.util.Vector; + +import org.osoa.sca.annotations.Property; +import org.osoa.sca.annotations.Reference; + +import services.Catalog; +import services.CurrencyConverter; +import services.Item; + + +public class MarketCatalogImpl implements Catalog { + + @Property + public String currencyCode = "USD"; + + @Reference + public CurrencyConverter currencyConverter; + + @Reference(required=false) + protected Catalog[] goodsCatalog; + + + public Item[] get() { + + String currencySymbol = currencyConverter.getCurrencySymbol(currencyCode); + Vector catalog = new Vector(); + + for (int i = 0; i < goodsCatalog.length; i++) { + Item[] items = goodsCatalog[i].get(); + + for (Item item : items) { + double price = Double.valueOf(item.getPrice().substring(1)); + price = currencyConverter.getConversion("USD", currencyCode, price); + catalog.addElement(new Item(item.getName(), currencySymbol + price)); + } + } + + Item[] catalogArray = new Item[catalog.size()]; + catalog.copyInto(catalogArray); + + return catalogArray; + } + +} \ No newline at end of file diff --git a/sca-java-1.x/tags/1.6.2-RC1/tutorials/store/assets/services/merger/MergedCatalogImpl.java b/sca-java-1.x/tags/1.6.2-RC1/tutorials/store/assets/services/merger/MergedCatalogImpl.java new file mode 100644 index 0000000000..c02b4e821e --- /dev/null +++ b/sca-java-1.x/tags/1.6.2-RC1/tutorials/store/assets/services/merger/MergedCatalogImpl.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.merger; + +import org.osoa.sca.annotations.Property; +import org.osoa.sca.annotations.Reference; + +import services.Catalog; +import services.CurrencyConverter; +import services.Item; + +public class MergedCatalogImpl 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; + } + +} -- cgit v1.2.3