Package dao.jpa

Source Code of dao.jpa.OrderJpaDao

package dao.jpa;

import dao.DataAccessException;
import dao.OrderDao;
import models.dto.OrderSearchDto;
import models.entities.Contact;
import models.entities.Order;
import play.Logger;
import play.db.jpa.JPA;

import javax.persistence.TypedQuery;
import java.util.ArrayList;
import java.util.List;

/**
* Created by kiryl on 17.08.2014.
*/
public class OrderJpaDao extends BaseJpaDao<Order, Integer> implements OrderDao {
    @Override
    public List<Order> getAllByContact(Contact contact, int pageNumber, int pageSize) throws DataAccessException {
        try {
            if (pageNumber < 0 || pageSize < 1) {
                return new ArrayList<Order>();
            }
            List<Order> orders = null;
            TypedQuery<Order> query = JPA.em().createQuery("SELECT o FROM Order o WHERE o.client = :client", Order.class);
            query.setFirstResult(pageNumber * pageSize);
            query.setMaxResults(pageSize);
            query.setParameter("client", contact);
            orders = query.getResultList();
            return orders;
        } catch (Throwable throwable) {
            Logger.error("Failed to get contactOrders with contact's id " + contact.getId());
            throw new DataAccessException("failed to get contactOrders with contact's id " + contact.getId());
        }
    }

    @Override
    public List<Order> search(int pageNumber, int pageSize, OrderSearchDto orderSearch) throws DataAccessException {
        try {
            Logger.info("find order");
            TypedQuery<Order> typedQuery = getSearchQuery(orderSearch);

            return getBatch(pageNumber, pageSize, typedQuery);
        } catch (Throwable throwable) {
            Logger.error("failed to get order's batch " + pageNumber);
            throw new DataAccessException("failed to get order's batch " + pageNumber, throwable);
        }
    }

    public Integer searchResultsCount(OrderSearchDto orderSearch) throws DataAccessException {
        try {
            Logger.info("Search pattern:" + orderSearch.toString());
            TypedQuery<Order> typedQuery = getSearchQuery(orderSearch);

            return typedQuery.getResultList().size();
        } catch (Throwable throwable) {
            Logger.error("failed to get order search results count");
            throw new DataAccessException("failed to get order search results count", throwable);
        }
    }

    protected TypedQuery<Order> getSearchQuery(OrderSearchDto orderSearch) {
        String queryString = "";
        queryString = "SELECT o FROM Order o WHERE o.client.lastName LIKE :client AND o.recipient.lastName LIKE :recipient";
        if (orderSearch.getBeforeDate() != null) {
            queryString += " AND o.createdOn <= :beforeDate";
        }
        if (orderSearch.getFromDate() != null) {
            queryString += " AND o.createdOn >= :fromDate";
        }
        if (orderSearch.getCreatorId() != null) {
            queryString += " AND o.creator.id = :userId";
        }
        if (orderSearch.getExecutorId() != null) {
            queryString += " AND o.executor.id = :userId";
        }
        if (orderSearch.getDeliveryManagerId() != null) {
            queryString += " AND o.deliveryManager.id = :userId";
        }
        if (orderSearch.getState() != null) {
            queryString += " AND o.state.name = :state";
        }
        Logger.debug("search query: " + queryString);
        TypedQuery<Order> typedQuery = JPA.em().createQuery(queryString, Order.class);
        typedQuery.setParameter("client", ((orderSearch.getClient() != null) ? (orderSearch.getClient() + "%") : "%"));
        typedQuery.setParameter("recipient", ((orderSearch.getRecipient() != null) ? (orderSearch.getRecipient() + "%") : "%"));
        if (orderSearch.getBeforeDate() != null) {
            typedQuery.setParameter("beforeDate", orderSearch.getBeforeDate());
        }
        if (orderSearch.getFromDate() != null) {
            typedQuery.setParameter("fromDate", orderSearch.getFromDate());
        }
        if (orderSearch.getCreatorId() != null) {
            typedQuery.setParameter("userId", orderSearch.getCreatorId());
        }
        if (orderSearch.getExecutorId() != null) {
            typedQuery.setParameter("userId", orderSearch.getExecutorId());
        }
        if (orderSearch.getDeliveryManagerId() != null) {
            typedQuery.setParameter("userId", orderSearch.getDeliveryManagerId());
        }
        if (orderSearch.getState() != null) {
            typedQuery.setParameter("state", orderSearch.getState());
        }

        return typedQuery;
    }
}
TOP

Related Classes of dao.jpa.OrderJpaDao

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.