Package com.abiquo.server.core.pricing

Source Code of com.abiquo.server.core.pricing.CurrencyDAO

/**
* Copyright (C) 2008 - Abiquo Holdings S.L. All rights reserved.
*
* Please see /opt/abiquo/tomcat/webapps/legal/ on Abiquo server
* or contact contact@abiquo.com for licensing information.
*/
package com.abiquo.server.core.pricing;

import java.util.Collection;
import java.util.List;

import javax.persistence.EntityManager;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Disjunction;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.abiquo.server.core.common.persistence.DefaultDAOBase;
import com.abiquo.server.core.util.PagedList;

@Repository("jpaCurrencyDAO")
public class CurrencyDAO extends DefaultDAOBase<Integer, Currency>
{
    public CurrencyDAO()
    {
        super(Currency.class);
    }

    public CurrencyDAO(final EntityManager entityManager)
    {
        super(Currency.class, entityManager);
    }

    public boolean existAnyOtherCurrencyWithName(final String name)
    {
        return existsAnyByCriterions(sameName(name));
    }

    public static Criterion sameName(final String name)
    {
        return Restrictions.eq(Currency.NAME_PROPERTY, name);
    }

    public boolean existAnyOtherCurrencyWithName(final Currency currency, final String name)
    {
        return existsAnyOtherByCriterions(currency, sameName(name));
    }

    public Collection<Currency> find(final String filter, final String orderBy, final boolean desc,
        final int offset, int numResults)
    {
        Criteria criteria = createCriteria(filter, orderBy, desc);

        Number total = count(criteria);

        criteria = createCriteria(filter, orderBy, desc);
        numResults = (numResults != 0 ? numResults : total.intValue());
        if (numResults != 0)
        {
            criteria.setFirstResult(offset * numResults);
            criteria.setMaxResults(numResults);
        }

        List<Currency> result = getResultList(criteria);

        PagedList<Currency> page = new PagedList<Currency>();
        page.addAll(result);
        if (numResults != 0)
        {
            page.setCurrentElement(offset);
            page.setPageSize(numResults);
        }
        page.setTotalResults(total.intValue());

        return page;
    }

    private Criteria createCriteria(final String filter, final String orderBy, final boolean desc)
    {
        Criteria criteria = createCriteria();

        if (!StringUtils.isEmpty(filter))
        {
            criteria.add(filterBy(filter));
        }

        if (!StringUtils.isEmpty(orderBy))
        {
            Order order = Order.asc(orderBy);
            if (desc)
            {
                order = Order.desc(orderBy);
            }
            criteria.addOrder(order);
        }

        return criteria;
    }

    private Criterion filterBy(final String filter)
    {
        Disjunction filterDisjunction = Restrictions.disjunction();

        filterDisjunction.add(Restrictions.like(Currency.NAME_PROPERTY, '%' + filter + '%'));
        filterDisjunction.add(Restrictions.like(Currency.SYMBOL_PROPERTY, '%' + filter + '%'));

        return filterDisjunction;
    }

}
TOP

Related Classes of com.abiquo.server.core.pricing.CurrencyDAO

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.