Java tutorial
/* * Copyright 2012 the original author or authors. * Licensed 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 gemfire.practice.driver; import gemfire.practice.domain.Address; import gemfire.practice.domain.Customer; import gemfire.practice.domain.CustomerFactory; import gemfire.practice.domain.EmailAddressFactory; import gemfire.practice.domain.LineItem; import gemfire.practice.domain.Order; import gemfire.practice.domain.Product; import gemfire.practice.service.CustomerService; import gemfire.practice.service.OrderService; import gemfire.practice.service.ProductService; import java.math.BigDecimal; import javax.annotation.Resource; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; /** * An example order application * * @author David Turanski * */ @Component public class OrderExample { private static Logger log = LoggerFactory.getLogger(OrderExample.class); @Autowired private CustomerService customerService; @Autowired private ProductService productService; @Autowired private OrderService orderService; @Resource(name = "emailAddressFactory") private EmailAddressFactory emailAddressFactory; @Resource(name = "customerFactory") private CustomerFactory customerFactory; /** * Run the sample application */ public void run() { Customer[] customers = createCustomers(); createProducts(); createOrders(customers); findCustomers(customers); findProducts(); findOrders(customers); deleteOrders(); deleteProducts(); deleteCustomers(); } private void deleteCustomers() { for (Customer customer : customerService.findAllCustomers()) { customerService.delete(customer.getId()); } } private void deleteProducts() { for (Product product : productService.findAllProducts()) { productService.delete(product); } } private void deleteOrders() { for (Order order : orderService.findAllOrders()) { orderService.delete(order); } } private void findOrders(Customer[] customers) { log.debug("looking for orders for customer ID " + customers[1].getId()); for (Order order : orderService.findOrdersByCustomerId(customers[1].getId())) { log.debug("********************found order ID " + order.getId() + " " + order.getBillingAddress().getStreet() + " " + order.getBillingAddress().getCity() + " " + order.getBillingAddress().getCountry()); for (LineItem lineItem : order.getLineItems()) { log.debug("product ID:" + lineItem.getProductId() + " quantity:" + lineItem.getAmount() + " unit price:" + lineItem.getUnitPrice().setScale(2, BigDecimal.ROUND_DOWN) + " total price:" + lineItem.getTotal().setScale(2, BigDecimal.ROUND_DOWN)); } } } private void findProducts() { log.debug("looking for products with description containing 'Apple'"); for (Product product : productService.findProductsByDescription("Apple")) { log.debug("found " + product.getName() + " " + product.getDescription()); } log.debug("looking for products with description containing 'notebook'"); for (Product product : productService.findProductsByDescription("notebook")) { log.debug("found " + product.getName() + " " + product.getDescription()); } log.debug("looking for products with warantee included"); for (Product product : productService.findProductsByAttribute("warantee", "included")) { log.debug("found " + product.getName() + " " + product.getDescription()); } } private void findCustomers(Customer[] customers) { log.debug("looking for all customers with last name " + customers[0].getLastname()); for (Customer customer : customerService.findCustomersByLastName(customers[0].getLastname())) { log.debug("found :" + customer.getFirstname() + " " + customer.getLastname()); } } /* * create some products */ private void createProducts() { Product ipod = new Product(1L, "Apple iPod", new BigDecimal(99.99), "An Apple portable music player"); Product ipad = new Product(2L, "Apple iPad", new BigDecimal(499.99), "An Apple tablet device"); Product macbook = new Product(3L, "Apple macBook", new BigDecimal(899.99), "An Apple notebook computer"); macbook.setAttribute("warantee", "included"); productService.create(ipod); productService.create(ipad); productService.create(macbook); } /* * create some customers */ private Customer[] createCustomers() { Customer dave = customerFactory.create(1l); Customer alicia = customerFactory.create(2l); customerService.create(dave); customerService.create(alicia); return new Customer[] { dave, alicia }; } /* * Create some customer orders */ private void createOrders(Customer[] customers) { Customer dave = customerService.findByEmailAddress(customers[0].getEmailAddress().getValue()); Product iPad = productService.findProductsByName("Apple iPad").get(0); Product macBook = productService.findProductsByName("Apple macBook").get(0); Product iPod = productService.findProductsByDescription("notebook").get(0); Order davesOrder = new Order(1L, dave.getId(), new Address("Dave Street", "Matthews", "USA")); davesOrder.add(new LineItem(iPad, 2)); davesOrder.add(new LineItem(macBook)); orderService.create(davesOrder); Customer alicia = customerService.findByEmailAddress(customers[1].getEmailAddress().getValue()); Order aliciasFirstOrder = new Order(2L, alicia.getId(), new Address("Alicia Street", "Keys", "USA")); aliciasFirstOrder.add(new LineItem(iPod, 3)); orderService.create(aliciasFirstOrder); Order aliciasNextOrder = new Order(3L, alicia.getId(), new Address("Alicia Street", "Keys", "USA")); aliciasNextOrder.add(new LineItem(macBook, 4)); aliciasNextOrder.add(new LineItem(iPad)); orderService.create(aliciasNextOrder); } }