From 7f673eb07e7367799f8b8cc3e2e47b754f2ee84f Mon Sep 17 00:00:00 2001 From: Jim Laredo Date: Wed, 18 Dec 2019 17:29:47 -0500 Subject: [PATCH] add service code --- .../petstore/services/AbstractService.java | 125 ++++++++++++++ .../petstore/services/CatalogService.java | 151 +++++++++++++++++ .../petstore/services/CategoryService.java | 54 ++++++ .../services/ComputablePurchaseOrder.java | 5 + .../petstore/services/CountryService.java | 68 ++++++++ .../petstore/services/CustomerService.java | 158 ++++++++++++++++++ .../petstore/services/InventoryService.java | 8 + .../petstore/services/ItemService.java | 65 +++++++ .../petstore/services/OrderLineService.java | 54 ++++++ .../petstore/services/ProductService.java | 59 +++++++ .../services/PurchaseOrderDecorator.java | 15 ++ .../services/PurchaseOrderService.java | 124 ++++++++++++++ .../petstore/services/ShippingService.java | 8 + .../petstore/services/StatisticService.java | 8 + 14 files changed, 902 insertions(+) create mode 100644 src/main/java/org/agoncal/application/petstore/services/AbstractService.java create mode 100755 src/main/java/org/agoncal/application/petstore/services/CatalogService.java create mode 100644 src/main/java/org/agoncal/application/petstore/services/CategoryService.java create mode 100644 src/main/java/org/agoncal/application/petstore/services/ComputablePurchaseOrder.java create mode 100644 src/main/java/org/agoncal/application/petstore/services/CountryService.java create mode 100755 src/main/java/org/agoncal/application/petstore/services/CustomerService.java create mode 100644 src/main/java/org/agoncal/application/petstore/services/InventoryService.java create mode 100644 src/main/java/org/agoncal/application/petstore/services/ItemService.java create mode 100644 src/main/java/org/agoncal/application/petstore/services/OrderLineService.java create mode 100644 src/main/java/org/agoncal/application/petstore/services/ProductService.java create mode 100644 src/main/java/org/agoncal/application/petstore/services/PurchaseOrderDecorator.java create mode 100755 src/main/java/org/agoncal/application/petstore/services/PurchaseOrderService.java create mode 100644 src/main/java/org/agoncal/application/petstore/services/ShippingService.java create mode 100644 src/main/java/org/agoncal/application/petstore/services/StatisticService.java diff --git a/src/main/java/org/agoncal/application/petstore/services/AbstractService.java b/src/main/java/org/agoncal/application/petstore/services/AbstractService.java new file mode 100644 index 0000000..1c5b217 --- /dev/null +++ b/src/main/java/org/agoncal/application/petstore/services/AbstractService.java @@ -0,0 +1,125 @@ +package org.agoncal.application.petstore.service; + +import javax.persistence.EntityManager; +import javax.persistence.PersistenceContext; +import javax.persistence.TypedQuery; +import javax.persistence.criteria.CriteriaBuilder; +import javax.persistence.criteria.CriteriaQuery; +import javax.persistence.criteria.Predicate; +import javax.persistence.criteria.Root; +import java.util.List; +import org.agoncal.application.petstore.util.Loggable; + +@Loggable +public abstract class AbstractService +{ + + // ====================================== + // = Attributes = + // ====================================== + + @PersistenceContext(unitName = "applicationPetstorePU") + protected EntityManager entityManager; + + private Class entityClass; + + // ====================================== + // = Constructors = + // ====================================== + + public AbstractService() + { + } + + public AbstractService(Class entityClass) + { + this.entityClass = entityClass; + } + + // ====================================== + // = Business methods = + // ====================================== + + public T persist(T entity) + { + entityManager.persist(entity); + return entity; + } + + public T findById(Long id) + { + return entityManager.find(entityClass, id); + } + + public void remove(T entity) + { + entityManager.remove(entityManager.merge(entity)); + } + + public T merge(T entity) + { + return entityManager.merge(entity); + } + + public List listAll(Integer startPosition, Integer maxResult) + { + TypedQuery findAllQuery = getListAllQuery(); + if (startPosition != null) +{ + findAllQuery.setFirstResult(startPosition); + } + if (maxResult != null) + { + findAllQuery.setMaxResults(maxResult); + } + final List results = findAllQuery.getResultList(); + return results; + } + + public List listAll() + { + return getListAllQuery().getResultList(); + } + + public TypedQuery getListAllQuery() + { + CriteriaQuery criteria = entityManager.getCriteriaBuilder().createQuery(entityClass); + return entityManager.createQuery(criteria.select(criteria.from(entityClass))); + } + + public long count(T example) + { + + CriteriaBuilder builder = entityManager.getCriteriaBuilder(); + + // Populate count + + CriteriaQuery countCriteria = builder.createQuery(Long.class); + Root root = countCriteria.from(entityClass); + countCriteria = countCriteria.select(builder.count(root)).where(getSearchPredicates(root, example)); + long count = entityManager.createQuery(countCriteria).getSingleResult(); + return count; + } + + public List page(T example, int page, int pageSize) + { + + CriteriaBuilder builder = entityManager.getCriteriaBuilder(); + + // Populate pageItems + + CriteriaQuery criteria = builder.createQuery(entityClass); + Root root = criteria.from(entityClass); + TypedQuery query = entityManager.createQuery(criteria.select(root).where(getSearchPredicates(root, example))); + query.setFirstResult(page * pageSize).setMaxResults(pageSize); + List pageItems = query.getResultList(); + return pageItems; + + } + + // ====================================== + // = Protected methods = + // ====================================== + + protected abstract Predicate[] getSearchPredicates(Root root, T example); +} \ No newline at end of file diff --git a/src/main/java/org/agoncal/application/petstore/services/CatalogService.java b/src/main/java/org/agoncal/application/petstore/services/CatalogService.java new file mode 100755 index 0000000..f3c1f2b --- /dev/null +++ b/src/main/java/org/agoncal/application/petstore/services/CatalogService.java @@ -0,0 +1,151 @@ +package org.agoncal.application.petstore.service; + +import org.agoncal.application.petstore.model.Category; +import org.agoncal.application.petstore.model.Item; +import org.agoncal.application.petstore.model.Product; +import org.agoncal.application.petstore.util.Loggable; + +import javax.ejb.Stateless; +import javax.inject.Inject; +import javax.persistence.EntityManager; +import javax.persistence.TypedQuery; +import javax.validation.constraints.NotNull; +import java.io.Serializable; +import java.util.List; + +/** + * @author Antonio Goncalves + * http://www.antoniogoncalves.org + * -- + */ + +@Stateless +@Loggable +public class CatalogService implements Serializable { + + // ====================================== + // = Attributes = + // ====================================== + + @Inject + private EntityManager em; + + // ====================================== + // = Public Methods = + // ====================================== + + public Category findCategory(@NotNull Long categoryId) { + return em.find(Category.class, categoryId); + } + + public Category findCategory(@NotNull String categoryName) { + TypedQuery typedQuery = em.createNamedQuery(Category.FIND_BY_NAME, Category.class); + typedQuery.setParameter("pname", categoryName); + return typedQuery.getSingleResult(); + } + + public List findAllCategories() { + TypedQuery typedQuery = em.createNamedQuery(Category.FIND_ALL, Category.class); + return typedQuery.getResultList(); + } + + public Category createCategory(@NotNull Category category) { + em.persist(category); + return category; + } + + public Category updateCategory(@NotNull Category category) { + return em.merge(category); + } + + public void removeCategory(@NotNull Category category) { + em.remove(em.merge(category)); + } + + public void removeCategory(@NotNull Long categoryId) { + removeCategory(findCategory(categoryId)); + } + + public List findProducts(@NotNull String categoryName) { + TypedQuery typedQuery = em.createNamedQuery(Product.FIND_BY_CATEGORY_NAME, Product.class); + typedQuery.setParameter("pname", categoryName); + return typedQuery.getResultList(); + } + + public Product findProduct(@NotNull Long productId) { + Product product = em.find(Product.class, productId); + return product; + } + + public List findAllProducts() { + TypedQuery typedQuery = em.createNamedQuery(Product.FIND_ALL, Product.class); + return typedQuery.getResultList(); + } + + public Product createProduct(@NotNull Product product) { + if (product.getCategory() != null && product.getCategory().getId() == null) + em.persist(product.getCategory()); + + em.persist(product); + return product; + } + + public Product updateProduct(@NotNull Product product) { + return em.merge(product); + } + + public void removeProduct(@NotNull Product product) { + em.remove(em.merge(product)); + } + + public void removeProduct(@NotNull Long productId) { + removeProduct(findProduct(productId)); + } + + public List findItems(@NotNull Long productId) { + TypedQuery typedQuery = em.createNamedQuery(Item.FIND_BY_PRODUCT_ID, Item.class); + typedQuery.setParameter("productId", productId); + return typedQuery.getResultList(); + } + + public Item findItem(@NotNull Long itemId) { + return em.find(Item.class, itemId); + } + + public List searchItems(String keyword) { + if (keyword == null) + keyword = ""; + + TypedQuery typedQuery = em.createNamedQuery(Item.SEARCH, Item.class); + typedQuery.setParameter("keyword", "%" + keyword.toUpperCase() + "%"); + return typedQuery.getResultList(); + } + + public List findAllItems() { + TypedQuery typedQuery = em.createNamedQuery(Item.FIND_ALL, Item.class); + return typedQuery.getResultList(); + } + + public Item createItem(@NotNull Item item) { + if (item.getProduct() != null && item.getProduct().getId() == null) { + em.persist(item.getProduct()); + if (item.getProduct().getCategory() != null && item.getProduct().getCategory().getId() == null) + em.persist(item.getProduct().getCategory()); + } + + em.persist(item); + return item; + } + + public Item updateItem(@NotNull Item item) { + return em.merge(item); + } + + public void removeItem(@NotNull Item item) { + em.remove(em.merge(item)); + } + + public void removeItem(@NotNull Long itemId) { + removeItem(findItem(itemId)); + } +} diff --git a/src/main/java/org/agoncal/application/petstore/services/CategoryService.java b/src/main/java/org/agoncal/application/petstore/services/CategoryService.java new file mode 100644 index 0000000..de9db52 --- /dev/null +++ b/src/main/java/org/agoncal/application/petstore/services/CategoryService.java @@ -0,0 +1,54 @@ +package org.agoncal.application.petstore.service; + +import org.agoncal.application.petstore.model.Category; + +import javax.annotation.Resource; +import javax.ejb.Stateless; +import javax.ejb.LocalBean; +import javax.persistence.criteria.CriteriaBuilder; +import javax.persistence.criteria.Predicate; +import javax.persistence.criteria.Root; +import java.io.Serializable; +import java.util.ArrayList; +import java.util.List; +import org.agoncal.application.petstore.util.Loggable; + +@Stateless +@LocalBean +@Loggable +public class CategoryService extends AbstractService implements Serializable +{ + + // ====================================== + // = Constructors = + // ====================================== + + public CategoryService() + { + super(Category.class); + } + + // ====================================== + // = Protected methods = + // ====================================== + + @Override + protected Predicate[] getSearchPredicates(Root root, Category example) + { + CriteriaBuilder builder = this.entityManager.getCriteriaBuilder(); + List predicatesList = new ArrayList(); + + String name = example.getName(); + if (name != null && !"".equals(name)) + { + predicatesList.add(builder.like(builder.lower(root. get("name")), '%' + name.toLowerCase() + '%')); + } + String description = example.getDescription(); + if (description != null && !"".equals(description)) + { + predicatesList.add(builder.like(builder.lower(root. get("description")), '%' + description.toLowerCase() + '%')); + } + + return predicatesList.toArray(new Predicate[predicatesList.size()]); + } +} \ No newline at end of file diff --git a/src/main/java/org/agoncal/application/petstore/services/ComputablePurchaseOrder.java b/src/main/java/org/agoncal/application/petstore/services/ComputablePurchaseOrder.java new file mode 100644 index 0000000..4136a28 --- /dev/null +++ b/src/main/java/org/agoncal/application/petstore/services/ComputablePurchaseOrder.java @@ -0,0 +1,5 @@ +package org.agoncal.application.petstore.service; + +public interface ComputablePurchaseOrder +{ +} \ No newline at end of file diff --git a/src/main/java/org/agoncal/application/petstore/services/CountryService.java b/src/main/java/org/agoncal/application/petstore/services/CountryService.java new file mode 100644 index 0000000..4baaed1 --- /dev/null +++ b/src/main/java/org/agoncal/application/petstore/services/CountryService.java @@ -0,0 +1,68 @@ +package org.agoncal.application.petstore.service; + +import org.agoncal.application.petstore.model.Country; + +import javax.ejb.Stateless; +import javax.ejb.LocalBean; +import javax.persistence.criteria.CriteriaBuilder; +import javax.persistence.criteria.Predicate; +import javax.persistence.criteria.Root; +import java.io.Serializable; +import java.util.ArrayList; +import java.util.List; +import org.agoncal.application.petstore.util.Loggable; + +@Stateless +@LocalBean +@Loggable +public class CountryService extends AbstractService implements Serializable +{ + + // ====================================== + // = Constructors = + // ====================================== + + public CountryService() + { + super(Country.class); + } + + // ====================================== + // = Protected methods = + // ====================================== + + @Override + protected Predicate[] getSearchPredicates(Root root, Country example) + { + CriteriaBuilder builder = this.entityManager.getCriteriaBuilder(); + List predicatesList = new ArrayList(); + + String isoCode = example.getIsoCode(); + if (isoCode != null && !"".equals(isoCode)) + { + predicatesList.add(builder.like(builder.lower(root. get("isoCode")), '%' + isoCode.toLowerCase() + '%')); + } + String name = example.getName(); + if (name != null && !"".equals(name)) + { + predicatesList.add(builder.like(builder.lower(root. get("name")), '%' + name.toLowerCase() + '%')); + } + String printableName = example.getPrintableName(); + if (printableName != null && !"".equals(printableName)) + { + predicatesList.add(builder.like(builder.lower(root. get("printableName")), '%' + printableName.toLowerCase() + '%')); + } + String iso3 = example.getIso3(); + if (iso3 != null && !"".equals(iso3)) + { + predicatesList.add(builder.like(builder.lower(root. get("iso3")), '%' + iso3.toLowerCase() + '%')); + } + String numcode = example.getNumcode(); + if (numcode != null && !"".equals(numcode)) + { + predicatesList.add(builder.like(builder.lower(root. get("numcode")), '%' + numcode.toLowerCase() + '%')); + } + + return predicatesList.toArray(new Predicate[predicatesList.size()]); + } +} \ No newline at end of file diff --git a/src/main/java/org/agoncal/application/petstore/services/CustomerService.java b/src/main/java/org/agoncal/application/petstore/services/CustomerService.java new file mode 100755 index 0000000..bff7dc3 --- /dev/null +++ b/src/main/java/org/agoncal/application/petstore/services/CustomerService.java @@ -0,0 +1,158 @@ +package org.agoncal.application.petstore.service; + +import org.agoncal.application.petstore.model.Country; +import org.agoncal.application.petstore.model.Customer; +import org.agoncal.application.petstore.util.Loggable; + +import javax.ejb.LocalBean; +import javax.ejb.Stateless; +import javax.persistence.NoResultException; +import javax.persistence.TypedQuery; +import javax.persistence.criteria.CriteriaBuilder; +import javax.persistence.criteria.Predicate; +import javax.persistence.criteria.Root; +import javax.validation.constraints.NotNull; +import java.io.Serializable; +import java.util.ArrayList; +import java.util.List; + +/** + * @author Antonio Goncalves + * http://www.antoniogoncalves.org + * -- + */ + +@Stateless +@LocalBean +@Loggable +public class CustomerService extends AbstractService implements Serializable +{ + + // ====================================== + // = Constructors = + // ====================================== + + public CustomerService() + { + super(Customer.class); + } + + // ====================================== + // = Attributes = + // ====================================== + + + // ====================================== + // = Public Methods = + // ====================================== + + public boolean doesLoginAlreadyExist(@NotNull String login) + { + + // Login has to be unique + TypedQuery typedQuery = entityManager.createNamedQuery(Customer.FIND_BY_LOGIN, Customer.class); + typedQuery.setParameter("login", login); + try + { + typedQuery.getSingleResult(); + return true; + } + catch (NoResultException e) + { + return false; + } + } + + public Customer createCustomer(@NotNull Customer customer) + { + Country country = entityManager.find(Country.class, customer.getHomeAddress().getCountry().getId()); + customer.getHomeAddress().setCountry(country); + entityManager.persist(customer); + return customer; + } + + public Customer findCustomer(@NotNull String login) + { + TypedQuery typedQuery = entityManager.createNamedQuery(Customer.FIND_BY_LOGIN, Customer.class); + typedQuery.setParameter("login", login); + + try + { + return typedQuery.getSingleResult(); + } + catch (NoResultException e) + { + return null; + } + } + + public Customer findCustomer(@NotNull String login, @NotNull String password) + { + TypedQuery typedQuery = entityManager.createNamedQuery(Customer.FIND_BY_LOGIN_PASSWORD, Customer.class); + typedQuery.setParameter("login", login); + typedQuery.setParameter("password", password); + + return typedQuery.getSingleResult(); + } + + public List findAllCustomers() + { + TypedQuery typedQuery = entityManager.createNamedQuery(Customer.FIND_ALL, Customer.class); + return typedQuery.getResultList(); + } + + public Customer updateCustomer(@NotNull Customer customer) + { + entityManager.merge(customer); + return customer; + } + + public void removeCustomer(@NotNull Customer customer) + { + entityManager.remove(entityManager.merge(customer)); + } + + public Country findCountry(@NotNull Long countryId) + { + return entityManager.find(Country.class, countryId); + } + + // ====================================== + // = Protected methods = + // ====================================== + + @Override + protected Predicate[] getSearchPredicates(Root root, Customer example) + { + CriteriaBuilder builder = this.entityManager.getCriteriaBuilder(); + List predicatesList = new ArrayList(); + + String firstName = example.getFirstName(); + if (firstName != null && !"".equals(firstName)) + { + predicatesList.add(builder.like(builder.lower(root.get("firstName")), '%' + firstName.toLowerCase() + '%')); + } + String lastName = example.getLastName(); + if (lastName != null && !"".equals(lastName)) + { + predicatesList.add(builder.like(builder.lower(root.get("lastName")), '%' + lastName.toLowerCase() + '%')); + } + String telephone = example.getTelephone(); + if (telephone != null && !"".equals(telephone)) + { + predicatesList.add(builder.like(builder.lower(root.get("telephone")), '%' + telephone.toLowerCase() + '%')); + } + String email = example.getEmail(); + if (email != null && !"".equals(email)) + { + predicatesList.add(builder.like(builder.lower(root.get("email")), '%' + email.toLowerCase() + '%')); + } + String login = example.getLogin(); + if (login != null && !"".equals(login)) + { + predicatesList.add(builder.like(builder.lower(root.get("login")), '%' + login.toLowerCase() + '%')); + } + + return predicatesList.toArray(new Predicate[predicatesList.size()]); + } +} diff --git a/src/main/java/org/agoncal/application/petstore/services/InventoryService.java b/src/main/java/org/agoncal/application/petstore/services/InventoryService.java new file mode 100644 index 0000000..1a612d4 --- /dev/null +++ b/src/main/java/org/agoncal/application/petstore/services/InventoryService.java @@ -0,0 +1,8 @@ +package org.agoncal.application.petstore.service; + +import org.agoncal.application.petstore.util.Loggable; + +@Loggable +public class InventoryService +{ +} \ No newline at end of file diff --git a/src/main/java/org/agoncal/application/petstore/services/ItemService.java b/src/main/java/org/agoncal/application/petstore/services/ItemService.java new file mode 100644 index 0000000..1ad862f --- /dev/null +++ b/src/main/java/org/agoncal/application/petstore/services/ItemService.java @@ -0,0 +1,65 @@ +package org.agoncal.application.petstore.service; + +import org.agoncal.application.petstore.model.Item; +import org.agoncal.application.petstore.model.Product; + +import javax.ejb.Stateless; +import javax.ejb.LocalBean; +import javax.persistence.criteria.CriteriaBuilder; +import javax.persistence.criteria.Predicate; +import javax.persistence.criteria.Root; +import java.io.Serializable; +import java.util.ArrayList; +import java.util.List; +import org.agoncal.application.petstore.util.Loggable; + +@Stateless +@LocalBean +@Loggable +public class ItemService extends AbstractService implements Serializable +{ + + + // ====================================== + // = Constructors = + // ====================================== + + public ItemService() + { + super(Item.class); + } + + // ====================================== + // = Protected methods = + // ====================================== + + @Override + protected Predicate[] getSearchPredicates(Root root, Item example) + { + CriteriaBuilder builder = this.entityManager.getCriteriaBuilder(); + List predicatesList = new ArrayList(); + + String name = example.getName(); + if (name != null && !"".equals(name)) + { + predicatesList.add(builder.like(builder.lower(root. get("name")), '%' + name.toLowerCase() + '%')); + } + String description = example.getDescription(); + if (description != null && !"".equals(description)) + { + predicatesList.add(builder.like(builder.lower(root. get("description")), '%' + description.toLowerCase() + '%')); + } + String imagePath = example.getImagePath(); + if (imagePath != null && !"".equals(imagePath)) + { + predicatesList.add(builder.like(builder.lower(root. get("imagePath")), '%' + imagePath.toLowerCase() + '%')); + } + Product product = example.getProduct(); + if (product != null) + { + predicatesList.add(builder.equal(root.get("product"), product)); + } + + return predicatesList.toArray(new Predicate[predicatesList.size()]); + } +} \ No newline at end of file diff --git a/src/main/java/org/agoncal/application/petstore/services/OrderLineService.java b/src/main/java/org/agoncal/application/petstore/services/OrderLineService.java new file mode 100644 index 0000000..7203cdd --- /dev/null +++ b/src/main/java/org/agoncal/application/petstore/services/OrderLineService.java @@ -0,0 +1,54 @@ +package org.agoncal.application.petstore.service; + +import org.agoncal.application.petstore.model.Item; +import org.agoncal.application.petstore.model.OrderLine; + +import javax.ejb.Stateless; +import javax.ejb.LocalBean; +import javax.persistence.criteria.CriteriaBuilder; +import javax.persistence.criteria.Predicate; +import javax.persistence.criteria.Root; +import java.io.Serializable; +import java.util.ArrayList; +import java.util.List; +import org.agoncal.application.petstore.util.Loggable; + +@Stateless +@LocalBean +@Loggable +public class OrderLineService extends AbstractService implements Serializable +{ + + // ====================================== + // = Constructors = + // ====================================== + + public OrderLineService() + { + super(OrderLine.class); + } + + // ====================================== + // = Protected methods = + // ====================================== + + @Override + protected Predicate[] getSearchPredicates(Root root, OrderLine example) + { + CriteriaBuilder builder = this.entityManager.getCriteriaBuilder(); + List predicatesList = new ArrayList(); + + Integer quantity = example.getQuantity(); + if (quantity != null && quantity.intValue() != 0) + { + predicatesList.add(builder.equal(root.get("quantity"), quantity)); + } + Item item = example.getItem(); + if (item != null) + { + predicatesList.add(builder.equal(root.get("item"), item)); + } + + return predicatesList.toArray(new Predicate[predicatesList.size()]); + } +} \ No newline at end of file diff --git a/src/main/java/org/agoncal/application/petstore/services/ProductService.java b/src/main/java/org/agoncal/application/petstore/services/ProductService.java new file mode 100644 index 0000000..8cdcc11 --- /dev/null +++ b/src/main/java/org/agoncal/application/petstore/services/ProductService.java @@ -0,0 +1,59 @@ +package org.agoncal.application.petstore.service; + +import org.agoncal.application.petstore.model.Category; +import org.agoncal.application.petstore.model.Product; + +import javax.ejb.Stateless; +import javax.ejb.LocalBean; +import javax.persistence.criteria.CriteriaBuilder; +import javax.persistence.criteria.Predicate; +import javax.persistence.criteria.Root; +import java.io.Serializable; +import java.util.ArrayList; +import java.util.List; +import org.agoncal.application.petstore.util.Loggable; + +@Stateless +@LocalBean +@Loggable +public class ProductService extends AbstractService implements Serializable +{ + + // ====================================== + // = Constructors = + // ====================================== + + public ProductService() + { + super(Product.class); + } + + // ====================================== + // = Protected methods = + // ====================================== + + @Override + protected Predicate[] getSearchPredicates(Root root, Product example) + { + CriteriaBuilder builder = this.entityManager.getCriteriaBuilder(); + List predicatesList = new ArrayList(); + + String name = example.getName(); + if (name != null && !"".equals(name)) + { + predicatesList.add(builder.like(builder.lower(root. get("name")), '%' + name.toLowerCase() + '%')); + } + String description = example.getDescription(); + if (description != null && !"".equals(description)) + { + predicatesList.add(builder.like(builder.lower(root. get("description")), '%' + description.toLowerCase() + '%')); + } + Category category = example.getCategory(); + if (category != null) + { + predicatesList.add(builder.equal(root.get("category"), category)); + } + + return predicatesList.toArray(new Predicate[predicatesList.size()]); + } +} \ No newline at end of file diff --git a/src/main/java/org/agoncal/application/petstore/services/PurchaseOrderDecorator.java b/src/main/java/org/agoncal/application/petstore/services/PurchaseOrderDecorator.java new file mode 100644 index 0000000..4b7bb9e --- /dev/null +++ b/src/main/java/org/agoncal/application/petstore/services/PurchaseOrderDecorator.java @@ -0,0 +1,15 @@ +package org.agoncal.application.petstore.service; + +import org.agoncal.application.petstore.service.ComputablePurchaseOrder; +import javax.decorator.Decorator; +import javax.inject.Inject; +import javax.decorator.Delegate; + +@Decorator +public abstract class PurchaseOrderDecorator implements ComputablePurchaseOrder +{ + + @Inject + @Delegate + private ComputablePurchaseOrder delegate; +} \ No newline at end of file diff --git a/src/main/java/org/agoncal/application/petstore/services/PurchaseOrderService.java b/src/main/java/org/agoncal/application/petstore/services/PurchaseOrderService.java new file mode 100755 index 0000000..ff3900a --- /dev/null +++ b/src/main/java/org/agoncal/application/petstore/services/PurchaseOrderService.java @@ -0,0 +1,124 @@ +package org.agoncal.application.petstore.service; + +import org.agoncal.application.petstore.exceptions.ValidationException; +import org.agoncal.application.petstore.model.*; +import org.agoncal.application.petstore.util.Loggable; +import org.agoncal.application.petstore.view.shopping.ShoppingCartItem; + +import javax.ejb.LocalBean; +import javax.ejb.Stateless; +import javax.persistence.TypedQuery; +import javax.persistence.criteria.CriteriaBuilder; +import javax.persistence.criteria.Predicate; +import javax.persistence.criteria.Root; +import javax.validation.constraints.NotNull; +import java.io.Serializable; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +/** + * @author Antonio Goncalves + * http://www.antoniogoncalves.org + * -- + */ + +@Stateless +@LocalBean +@Loggable +public class PurchaseOrderService extends AbstractServiceimplements Serializable +{ + + // ====================================== + // = Attributes = + // ====================================== + + + // ====================================== + // = Public Methods = + // ====================================== + + public PurchaseOrder createOrder(@NotNull Customer customer, @NotNull CreditCard creditCard, final List cartItems) + { + + // OMake sure the object is valid + if (cartItems == null || cartItems.size() == 0) + throw new ValidationException("Shopping cart is empty"); // TODO exception bean validation + + // Creating the order + Address deliveryAddress = customer.getHomeAddress(); + Country country = entityManager.find(Country.class, customer.getHomeAddress().getCountry().getId()); + deliveryAddress.setCountry(country); + PurchaseOrder order = new PurchaseOrder(entityManager.merge(customer), creditCard, deliveryAddress); + + // From the shopping cart we create the order lines + Set orderLines = new HashSet<>(); + + for (ShoppingCartItem cartItem : cartItems) + { + orderLines.add(new OrderLine(cartItem.getQuantity(), entityManager.merge(cartItem.getItem()))); + } + order.setOrderLines(orderLines); + + // Persists the object to the database + entityManager.persist(order); + + return order; + } + + public PurchaseOrder findOrder(@NotNull Long orderId) + { + return entityManager.find(PurchaseOrder.class, orderId); + } + + public List findAllOrders() + { + TypedQuery typedQuery = entityManager.createNamedQuery(PurchaseOrder.FIND_ALL, PurchaseOrder.class); + return typedQuery.getResultList(); + } + + public void removeOrder(@NotNull PurchaseOrder order) + { + entityManager.remove(entityManager.merge(order)); + } + + // ====================================== + // = Protected methods = + // ====================================== + + @Override + protected Predicate[] getSearchPredicates(Root root, PurchaseOrder example) + { + CriteriaBuilder builder = this.entityManager.getCriteriaBuilder(); + List predicatesList = new ArrayList(); + + String street1 = example.getCustomer().getHomeAddress().getStreet1(); + if (street1 != null && !"".equals(street1)) + { + predicatesList.add(builder.like(builder.lower(root.get("street1")), '%' + street1.toLowerCase() + '%')); + } + String street2 = example.getCustomer().getHomeAddress().getStreet2(); + if (street2 != null && !"".equals(street2)) + { + predicatesList.add(builder.like(builder.lower(root.get("street2")), '%' + street2.toLowerCase() + '%')); + } + String city = example.getCustomer().getHomeAddress().getCity(); + if (city != null && !"".equals(city)) + { + predicatesList.add(builder.like(builder.lower(root.get("city")), '%' + city.toLowerCase() + '%')); + } + String state = example.getCustomer().getHomeAddress().getState(); + if (state != null && !"".equals(state)) + { + predicatesList.add(builder.like(builder.lower(root.get("state")), '%' + state.toLowerCase() + '%')); + } + String zipcode = example.getCustomer().getHomeAddress().getZipcode(); + if (zipcode != null && !"".equals(zipcode)) + { + predicatesList.add(builder.like(builder.lower(root.get("zipcode")), '%' + zipcode.toLowerCase() + '%')); + } + + return predicatesList.toArray(new Predicate[predicatesList.size()]); + } +} diff --git a/src/main/java/org/agoncal/application/petstore/services/ShippingService.java b/src/main/java/org/agoncal/application/petstore/services/ShippingService.java new file mode 100644 index 0000000..5790796 --- /dev/null +++ b/src/main/java/org/agoncal/application/petstore/services/ShippingService.java @@ -0,0 +1,8 @@ +package org.agoncal.application.petstore.service; + +import org.agoncal.application.petstore.util.Loggable; + +@Loggable +public class ShippingService +{ +} \ No newline at end of file diff --git a/src/main/java/org/agoncal/application/petstore/services/StatisticService.java b/src/main/java/org/agoncal/application/petstore/services/StatisticService.java new file mode 100644 index 0000000..876c0f0 --- /dev/null +++ b/src/main/java/org/agoncal/application/petstore/services/StatisticService.java @@ -0,0 +1,8 @@ +package org.agoncal.application.petstore.service; + +import org.agoncal.application.petstore.util.Loggable; + +@Loggable +public class StatisticService +{ +} \ No newline at end of file