Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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<T>
{

// ======================================
// = Attributes =
// ======================================

@PersistenceContext(unitName = "applicationPetstorePU")
protected EntityManager entityManager;

private Class<T> entityClass;

// ======================================
// = Constructors =
// ======================================

public AbstractService()
{
}

public AbstractService(Class<T> 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<T> listAll(Integer startPosition, Integer maxResult)
{
TypedQuery<T> findAllQuery = getListAllQuery();
if (startPosition != null)
{
findAllQuery.setFirstResult(startPosition);
}
if (maxResult != null)
{
findAllQuery.setMaxResults(maxResult);
}
final List<T> results = findAllQuery.getResultList();
return results;
}

public List<T> listAll()
{
return getListAllQuery().getResultList();
}

public TypedQuery<T> getListAllQuery()
{
CriteriaQuery<T> 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<Long> countCriteria = builder.createQuery(Long.class);
Root<T> root = countCriteria.from(entityClass);
countCriteria = countCriteria.select(builder.count(root)).where(getSearchPredicates(root, example));
long count = entityManager.createQuery(countCriteria).getSingleResult();
return count;
}

public List<T> page(T example, int page, int pageSize)
{

CriteriaBuilder builder = entityManager.getCriteriaBuilder();

// Populate pageItems

CriteriaQuery<T> criteria = builder.createQuery(entityClass);
Root<T> root = criteria.from(entityClass);
TypedQuery<T> query = entityManager.createQuery(criteria.select(root).where(getSearchPredicates(root, example)));
query.setFirstResult(page * pageSize).setMaxResults(pageSize);
List<T> pageItems = query.getResultList();
return pageItems;

}

// ======================================
// = Protected methods =
// ======================================

protected abstract Predicate[] getSearchPredicates(Root<T> root, T example);
}
Original file line number Diff line number Diff line change
@@ -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<Category> typedQuery = em.createNamedQuery(Category.FIND_BY_NAME, Category.class);
typedQuery.setParameter("pname", categoryName);
return typedQuery.getSingleResult();
}

public List<Category> findAllCategories() {
TypedQuery<Category> 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<Product> findProducts(@NotNull String categoryName) {
TypedQuery<Product> 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<Product> findAllProducts() {
TypedQuery<Product> 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<Item> findItems(@NotNull Long productId) {
TypedQuery<Item> 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<Item> searchItems(String keyword) {
if (keyword == null)
keyword = "";

TypedQuery<Item> typedQuery = em.createNamedQuery(Item.SEARCH, Item.class);
typedQuery.setParameter("keyword", "%" + keyword.toUpperCase() + "%");
return typedQuery.getResultList();
}

public List<Item> findAllItems() {
TypedQuery<Item> 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));
}
}
Original file line number Diff line number Diff line change
@@ -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<Category> implements Serializable
{

// ======================================
// = Constructors =
// ======================================

public CategoryService()
{
super(Category.class);
}

// ======================================
// = Protected methods =
// ======================================

@Override
protected Predicate[] getSearchPredicates(Root<Category> root, Category example)
{
CriteriaBuilder builder = this.entityManager.getCriteriaBuilder();
List<Predicate> predicatesList = new ArrayList<Predicate>();

String name = example.getName();
if (name != null && !"".equals(name))
{
predicatesList.add(builder.like(builder.lower(root.<String> get("name")), '%' + name.toLowerCase() + '%'));
}
String description = example.getDescription();
if (description != null && !"".equals(description))
{
predicatesList.add(builder.like(builder.lower(root.<String> get("description")), '%' + description.toLowerCase() + '%'));
}

return predicatesList.toArray(new Predicate[predicatesList.size()]);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package org.agoncal.application.petstore.service;

public interface ComputablePurchaseOrder
{
}
Loading