Example usage for org.hibernate.criterion Restrictions le

List of usage examples for org.hibernate.criterion Restrictions le

Introduction

In this page you can find the example usage for org.hibernate.criterion Restrictions le.

Prototype

public static SimpleExpression le(String propertyName, Object value) 

Source Link

Document

Apply a "less than or equal" constraint to the named property

Usage

From source file:com.abssh.util.GenericDao.java

License:Apache License

/**
 * ??Criterion,./*from w w w.j  a  va  2s . c  om*/
 */
protected Criterion buildPropertyFilterCriterion(final String propertyName, final Object[] propertyValue,
        final MatchType matchType) {
    Assert.hasText(propertyName, "propertyName should not be null!");
    Criterion criterion = null;
    try {
        // ?MatchTypecriterion
        if (MatchType.EQ.equals(matchType)) {
            criterion = Restrictions.eq(propertyName, propertyValue[0]);
        } else if (MatchType.LIKE.equals(matchType)) {
            criterion = Restrictions.like(propertyName, (String) propertyValue[0], MatchMode.ANYWHERE);
        } else if (MatchType.ELIKE.equals(matchType)) {
            criterion = Restrictions.like(propertyName, (String) propertyValue[0], MatchMode.END);
        } else if (MatchType.SLIKE.equals(matchType)) {
            criterion = Restrictions.like(propertyName, (String) propertyValue[0], MatchMode.START);
        } else if (MatchType.LE.equals(matchType)) {
            criterion = Restrictions.le(propertyName, propertyValue[0]);
        } else if (MatchType.LT.equals(matchType)) {
            criterion = Restrictions.lt(propertyName, propertyValue[0]);
        } else if (MatchType.GE.equals(matchType)) {
            criterion = Restrictions.ge(propertyName, propertyValue[0]);
        } else if (MatchType.GT.equals(matchType)) {
            criterion = Restrictions.gt(propertyName, propertyValue[0]);
        } else if (MatchType.NE.equals(matchType)) {
            criterion = Restrictions.ne(propertyName, propertyValue[0]);
        } else if (MatchType.ISNULL.equals(matchType)) {
            criterion = Restrictions.isNull(propertyName);
        } else if (MatchType.ISNOTNULL.equals(matchType)) {
            criterion = Restrictions.isNotNull(propertyName);
        } else if (MatchType.ISEMPTY.equals(matchType)) {
            criterion = Restrictions.isEmpty(propertyName);
        } else if (MatchType.ISNOTEMPTY.equals(matchType)) {
            criterion = Restrictions.isNotEmpty(propertyName);
        } else if (MatchType.IN.equals(matchType)) {
            criterion = Restrictions.in(propertyName, propertyValue);
        }
    } catch (Exception e) {
        throw ReflectionUtils.convertReflectionExceptionToUnchecked(e);
    }
    return criterion;
}

From source file:com.abssh.util.GenericDao.java

License:Apache License

/**
 * ??//  ww w  .j  ava 2s  .c  om
 */
private Criterion getCriterion(String propertyName, Object[] propertyValue, MatchType matchType) {
    Criterion criterion = null;
    try {
        // ?MatchTypecriterion
        if (MatchType.EQ.equals(matchType)) {
            criterion = Restrictions.eq(propertyName, propertyValue[0]);
        } else if (MatchType.LIKE.equals(matchType)) {
            criterion = RestrictionsUtils.ilike(propertyName, String.valueOf(propertyValue[0]),
                    MatchMode.ANYWHERE);
        } else if (MatchType.ELIKE.equals(matchType)) {
            criterion = RestrictionsUtils.ilike(propertyName, (String) propertyValue[0], MatchMode.END);
        } else if (MatchType.SLIKE.equals(matchType)) {
            criterion = RestrictionsUtils.ilike(propertyName, (String) propertyValue[0], MatchMode.START);
        } else if (MatchType.LE.equals(matchType)) {
            criterion = Restrictions.le(propertyName, propertyValue[0]);
        } else if (MatchType.LT.equals(matchType)) {
            criterion = Restrictions.lt(propertyName, propertyValue[0]);
        } else if (MatchType.GE.equals(matchType)) {
            criterion = Restrictions.ge(propertyName, propertyValue[0]);
        } else if (MatchType.GT.equals(matchType)) {
            criterion = Restrictions.gt(propertyName, propertyValue[0]);
        } else if (MatchType.NE.equals(matchType)) {
            criterion = Restrictions.ne(propertyName, propertyValue[0]);
        } else if (MatchType.ISNULL.equals(matchType)) {
            criterion = Restrictions.isNull(propertyName);
        } else if (MatchType.ISNOTNULL.equals(matchType)) {
            criterion = Restrictions.isNotNull(propertyName);
        } else if (MatchType.ISEMPTY.equals(matchType)) {
            criterion = Restrictions.isEmpty(propertyName);
        } else if (MatchType.ISNOTEMPTY.equals(matchType)) {
            criterion = Restrictions.isNotEmpty(propertyName);
        } else if (MatchType.IN.equals(matchType)) {
            criterion = Restrictions.in(propertyName, propertyValue);
        } else if (MatchType.LEN.equals(matchType)) {
            criterion = Restrictions.le(propertyName, propertyValue[0]);
        }
    } catch (Exception e) {
        throw ReflectionUtils.convertReflectionExceptionToUnchecked(e);
    }
    return criterion;
}

From source file:com.algoTrader.CriteriaSearch.java

/**
 * Adds an <code>Restrictions</code> to a <code>Criteria</code>.
 *
 * @param criteria//from w w  w .j  a v a  2s .  c om
 * @param parameterName
 * @param parameterValue
 * @param comparator
 * @param matchMode
 */
private void addExpression(Criteria criteria, String parameterName, Object parameterValue, int comparator,
        MatchMode matchMode) {
    switch (comparator) {
    case SearchParameter.NOT_NULL_COMPARATOR: {
        criteria.add(Restrictions.isNotNull(parameterName));
        break;
    }
    case SearchParameter.NULL_COMPARATOR: {
        criteria.add(Restrictions.isNull(parameterName));
        break;
    }
    case SearchParameter.EMPTY_COMPARATOR: {
        criteria.add(Restrictions.isEmpty(parameterName));
        break;
    }
    case SearchParameter.NOT_EMPTY_COMPARATOR: {
        criteria.add(Restrictions.isNotEmpty(parameterName));
        break;
    }
    default: {
        if (parameterValue != null) {
            switch (comparator) {
            case SearchParameter.LIKE_COMPARATOR: {
                if ((matchMode != null) && (parameterValue instanceof String)) {
                    criteria.add(Restrictions.like(parameterName, (String) parameterValue, matchMode));
                } else {
                    criteria.add(Restrictions.like(parameterName, parameterValue));
                }
                break;
            }
            case SearchParameter.NOT_LIKE_COMPARATOR: {
                SimpleExpression expression;
                if ((matchMode != null) && (parameterValue instanceof String)) {
                    expression = Restrictions.like(parameterName, (String) parameterValue, matchMode);
                } else {
                    expression = Restrictions.like(parameterName, parameterValue);
                }
                criteria.add(Restrictions.not(expression));
                break;
            }
            case SearchParameter.INSENSITIVE_LIKE_COMPARATOR: {
                if ((matchMode != null) && (parameterValue instanceof String)) {
                    criteria.add(Restrictions.ilike(parameterName, (String) parameterValue, matchMode));
                } else {
                    criteria.add(Restrictions.ilike(parameterName, parameterValue));
                }
                break;
            }
            case SearchParameter.NOT_INSENSITIVE_LIKE_COMPARATOR: {
                Criterion criterion;
                if ((matchMode != null) && (parameterValue instanceof String)) {
                    criterion = Restrictions.ilike(parameterName, (String) parameterValue, matchMode);
                } else {
                    criterion = Restrictions.ilike(parameterName, parameterValue);
                }
                criteria.add(Restrictions.not(criterion));
                break;
            }
            case SearchParameter.EQUAL_COMPARATOR: {
                criteria.add(Restrictions.eq(parameterName, parameterValue));
                break;
            }
            case SearchParameter.GREATER_THAN_OR_EQUAL_COMPARATOR: {
                criteria.add(Restrictions.ge(parameterName, parameterValue));
                break;
            }
            case SearchParameter.GREATER_THAN_COMPARATOR: {
                criteria.add(Restrictions.gt(parameterName, parameterValue));
                break;
            }
            case SearchParameter.LESS_THAN_OR_EQUAL_COMPARATOR: {
                criteria.add(Restrictions.le(parameterName, parameterValue));
                break;
            }
            case SearchParameter.LESS_THAN_COMPARATOR: {
                criteria.add(Restrictions.lt(parameterName, parameterValue));
                break;
            }
            case SearchParameter.IN_COMPARATOR: {
                if (parameterValue instanceof Collection) {
                    criteria.add(Restrictions.in(parameterName, (Collection) parameterValue));
                }
                break;
            }
            case SearchParameter.NOT_IN_COMPARATOR: {
                if (parameterValue instanceof Collection) {
                    criteria.add(Restrictions.not(Restrictions.in(parameterName, (Collection) parameterValue)));
                }
                break;
            }
            case SearchParameter.NOT_EQUAL_COMPARATOR: {
                criteria.add(Restrictions.ne(parameterName, parameterValue));
                break;
            }
            }
        } else {
            criteria.add(Restrictions.isNull(parameterName));
        }
    }
    }
}

From source file:com.algoTrader.CriteriaSearch.java

/**
 * Adds an <code>Restrictions</code> to a <code>Criteria</code>. The given <code>parameterValues</code>
 * represents either an array of <code>String</code> or another object. The different values in the
 * array are added to a disjunction or conjunction which is connected with logical and to the other criteria of the
 * search./*from  w  ww . j a v a2s . c o  m*/
 *
 * @param criteria
 * @param parameterName
 * @param parameterValues
 * @param searchIfNull
 * @param comparator
 * @param matchMode
 */
private void addExpression(Criteria criteria, String parameterName, Object[] parameterValues, int comparator,
        MatchMode matchMode) {
    if (parameterValues != null) {
        Disjunction disjunction = null;
        Conjunction conjunction = null;
        switch (comparator) {
        case SearchParameter.LIKE_COMPARATOR: {
            disjunction = Restrictions.disjunction();
            if ((matchMode != null) && (parameterValues instanceof String[])) {
                String[] stringParameterValues = (String[]) parameterValues;
                for (int index = 0; index < parameterValues.length; index++) {
                    if (stringParameterValues[index] != null) {
                        disjunction
                                .add(Restrictions.like(parameterName, stringParameterValues[index], matchMode));
                    } else {
                        disjunction.add(Restrictions.isNull(parameterName));
                    }
                }
            } else {
                for (int index = 0; index < parameterValues.length; index++) {
                    if (parameterValues[index] != null) {
                        disjunction.add(Restrictions.like(parameterName, parameterValues[index]));
                    } else {
                        disjunction.add(Restrictions.isNull(parameterName));
                    }
                }
            }
            break;
        }
        case SearchParameter.INSENSITIVE_LIKE_COMPARATOR: {
            disjunction = Restrictions.disjunction();
            if ((matchMode != null) && (parameterValues instanceof String[])) {
                String[] stringParameterValues = (String[]) parameterValues;
                for (int index = 0; index < parameterValues.length; index++) {
                    if (stringParameterValues[index] != null) {
                        disjunction.add(
                                Restrictions.ilike(parameterName, stringParameterValues[index], matchMode));
                    } else {
                        disjunction.add(Restrictions.isNull(parameterName));
                    }
                }
            } else {
                for (int index = 0; index < parameterValues.length; index++) {
                    if (parameterValues[index] != null) {
                        disjunction.add(Restrictions.ilike(parameterName, parameterValues[index]));
                    } else {
                        disjunction.add(Restrictions.isNull(parameterName));
                    }
                }
            }
            break;
        }
        case SearchParameter.EQUAL_COMPARATOR: {
            disjunction = Restrictions.disjunction();
            for (int index = 0; index < parameterValues.length; index++) {
                if (parameterValues[index] != null) {
                    disjunction.add(Restrictions.eq(parameterName, parameterValues[index]));
                } else {
                    disjunction.add(Restrictions.isNull(parameterName));
                }
            }
            break;
        }
        case SearchParameter.GREATER_THAN_OR_EQUAL_COMPARATOR: {
            disjunction = Restrictions.disjunction();
            for (int index = 0; index < parameterValues.length; index++) {
                if (parameterValues[index] != null) {
                    disjunction.add(Restrictions.ge(parameterName, parameterValues[index]));
                } else {
                    disjunction.add(Restrictions.isNull(parameterName));
                }
            }
            break;
        }
        case SearchParameter.GREATER_THAN_COMPARATOR: {
            disjunction = Restrictions.disjunction();
            for (int index = 0; index < parameterValues.length; index++) {
                if (parameterValues[index] != null) {
                    disjunction.add(Restrictions.gt(parameterName, parameterValues[index]));
                } else {
                    disjunction.add(Restrictions.isNull(parameterName));
                }
            }
            break;
        }
        case SearchParameter.LESS_THAN_OR_EQUAL_COMPARATOR: {
            disjunction = Restrictions.disjunction();
            for (int index = 0; index < parameterValues.length; index++) {
                if (parameterValues[index] != null) {
                    disjunction.add(Restrictions.le(parameterName, parameterValues[index]));
                } else {
                    disjunction.add(Restrictions.isNull(parameterName));
                }
            }
            break;
        }
        case SearchParameter.LESS_THAN_COMPARATOR: {
            disjunction = Restrictions.disjunction();
            for (int index = 0; index < parameterValues.length; index++) {
                if (parameterValues[index] != null) {
                    disjunction.add(Restrictions.lt(parameterName, parameterValues[index]));
                } else {
                    disjunction.add(Restrictions.isNull(parameterName));
                }
            }
            break;
        }
        case SearchParameter.IN_COMPARATOR: {
            criteria.add(Restrictions.in(parameterName, parameterValues));
            break;
        }
        case SearchParameter.NOT_IN_COMPARATOR: {
            criteria.add(Restrictions.not(Restrictions.in(parameterName, parameterValues)));
            break;
        }
        case SearchParameter.NOT_EQUAL_COMPARATOR: {
            conjunction = Restrictions.conjunction();
            for (int index = 0; index < parameterValues.length; index++) {
                if (parameterValues[index] != null) {
                    conjunction.add(Restrictions.ne(parameterName, parameterValues[index]));
                } else {
                    conjunction.add(Restrictions.isNotNull(parameterName));
                }
            }
            break;
        }
        }

        if (disjunction != null) {
            criteria.add(disjunction);
        }
        if (conjunction != null) {
            criteria.add(conjunction);
        }
    } else {
        switch (comparator) {
        case SearchParameter.EMPTY_COMPARATOR: {
            criteria.add(Restrictions.isEmpty(parameterName));
            break;
        }
        case SearchParameter.NOT_EMPTY_COMPARATOR: {
            criteria.add(Restrictions.isNotEmpty(parameterName));
            break;
        }
        default: {
            criteria.add(Restrictions.isNull(parameterName));
        }
        }
    }
}

From source file:com.apt.facade.AssignmentFacade.java

public List<Assignment> getAssignmentList(AssignmentFinder finder, int page, int recordPerPage) {
    List<Assignment> lst = new ArrayList<>();

    Session session = null;/* w  w w .  j ava2 s .c  o m*/
    Transaction trans = null;

    try {
        session = HibernateUtil.getSessionFactory().getCurrentSession();
        trans = session.beginTransaction();

        Criteria crit = session.createCriteria(Assignment.class);

        crit.add(Restrictions.sqlRestriction("1=1"));
        if (finder.getAssignmentId() != null) {
            crit.add(Restrictions.and(Restrictions.eq("assignmentId", finder.getAssignmentId())));
        }
        if (finder.getAssignmentName() != null) {
            crit.add(Restrictions
                    .and(Restrictions.ilike("assignmentName", "%" + finder.getAssignmentName() + "%")));
        }
        if (finder.getBatch() != null) {
            crit.add(Restrictions.and(Restrictions.eq("batch", finder.getBatch())));
        }
        if (finder.getSubject() != null) {
            crit.add(Restrictions.and(Restrictions.eq("subject", finder.getSubject())));
        }
        if (finder.getStatus() != null) {
            crit.add(Restrictions.and(Restrictions.eq("status", finder.getStatus())));
        }
        if (finder.getStarttime() != null) {
            crit.add(Restrictions.and(Restrictions.ge("startTime", finder.getStarttime())));
        }
        if (finder.getEndtime() != null) {
            crit.add(Restrictions.and(Restrictions.le("endTime", finder.getEndtime())));
        }
        crit.setFirstResult((page - 1) * recordPerPage);
        crit.setMaxResults(recordPerPage);
        lst = crit.list();
        trans.commit();

    } catch (Exception e) {
        e.printStackTrace();
        if (trans != null) {
            trans.rollback();
        }
    } finally {
        if (session != null && session.isConnected()) {
            session.close();
        }
    }

    return lst;
}

From source file:com.apt.facade.AssignmentFacade.java

public int getNumberAssignment(AssignmentFinder finder) {
    List<Assignment> lst = new ArrayList<>();

    Session session = null;/*from  w  w w.j av  a2s .c o m*/
    Transaction trans = null;

    try {
        session = HibernateUtil.getSessionFactory().getCurrentSession();
        trans = session.beginTransaction();

        Criteria crit = session.createCriteria(Assignment.class);

        crit.add(Restrictions.sqlRestriction("1=1"));
        if (finder.getAssignmentId() != null) {
            crit.add(Restrictions.and(Restrictions.eq("assignmentId", finder.getAssignmentId())));
        }
        if (finder.getAssignmentName() != null) {
            crit.add(Restrictions
                    .and(Restrictions.ilike("assignmentName", "%" + finder.getAssignmentName() + "%")));
        }
        if (finder.getBatch() != null) {
            crit.add(Restrictions.and(Restrictions.eq("batch", finder.getBatch())));
        }
        if (finder.getSubject() != null) {
            crit.add(Restrictions.and(Restrictions.eq("subject", finder.getSubject())));
        }
        if (finder.getStatus() != null) {
            crit.add(Restrictions.and(Restrictions.eq("status", finder.getStatus())));
        }
        if (finder.getStarttime() != null) {
            crit.add(Restrictions.and(Restrictions.ge("startTime", finder.getStarttime())));
        }
        if (finder.getEndtime() != null) {
            crit.add(Restrictions.and(Restrictions.le("endTime", finder.getEndtime())));
        }

        lst = crit.list();
        trans.commit();

    } catch (Exception e) {
        e.printStackTrace();
        if (trans != null) {
            trans.rollback();
        }
    } finally {
        if (session != null && session.isConnected()) {
            session.close();
        }
    }

    return lst.size();
}

From source file:com.arg.arsoft.siantluis.repository.imp.OrderRepository.java

@Override
public Map findByQuery(OrderQuery query) {
    int pageSize = Configs.PAGE_SIZE;
    Criteria criteria = factory.getCurrentSession().createCriteria(Order.class);

    if (query.getCode() != null && !query.getCode().equals("")) {
        if (query.getCode().contains("*") || query.getCode().contains("?")) {
            criteria.add(Restrictions.like("code", query.getCode().replace("*", "%").replace("?", "_")));
        } else {//from   w w w  .  ja  va 2s. co  m
            criteria.add(Restrictions.eq("code", query.getCode()));
        }
    }
    if (query.getName() != null && !query.getName().equals("")) {
        if (query.getName().contains("*") || query.getName().contains("?")) {
            criteria.add(Restrictions.like("name", query.getName().replace("*", "%").replace("?", "_")));
        } else {
            criteria.add(Restrictions.eq("name", query.getName()));
        }
    }
    if (query.getEmployee() != null && !query.getEmployee().equals("")) {
        criteria.createAlias("requestBy", "e", JoinType.LEFT_OUTER_JOIN);
        criteria.add(Restrictions.eq("e.code", query.getCode()));
    }
    if (query.getCustomer() != null && !query.getCustomer().equals("")) {
        criteria.createAlias("customer", "c", JoinType.LEFT_OUTER_JOIN);
        criteria.add(Restrictions.eq("c.code", query.getCustomer()));
    }
    if (query.getShipper() != null && query.getShipper() != 0) {
        criteria.createAlias("shipper", "s", JoinType.LEFT_OUTER_JOIN);
        criteria.add(Restrictions.eq("s.id", query.getShipper()));
    }
    if (query.getOrderDateStart() != null) {
        criteria.add(Restrictions.ge("orderDate", query.getOrderDateStart()));
    }
    if (query.getOrderDateEnd() != null) {
        criteria.add(Restrictions.le("orderDate", query.getOrderDateEnd()));
    }
    if (query.getRequireDateStart() != null) {
        criteria.add(Restrictions.ge("requireDate", query.getRequireDateStart()));
    }
    if (query.getRequireDateEnd() != null) {
        criteria.add(Restrictions.le("requireDate", query.getRequireDateEnd()));
    }
    long totalRecord = (long) criteria.setProjection(Projections.count(Projections.id().toString()))
            .uniqueResult();
    int start = ((query.getPage() - 1) * pageSize);

    criteria.setProjection(null);
    criteria.setFetchMode("requestBy", FetchMode.JOIN);
    criteria.setFetchMode("approveBy", FetchMode.JOIN);
    criteria.setFetchMode("customer", FetchMode.JOIN);
    criteria.setFetchMode("shipper", FetchMode.JOIN);
    criteria.setFetchMode("address.amphur", FetchMode.JOIN);
    criteria.setFetchMode("address.province", FetchMode.JOIN);

    List<Order> result = criteria.setFirstResult(start).setMaxResults(pageSize)
            .setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY).list();
    long totalPage = totalRecord / pageSize;
    if ((totalRecord % pageSize) > 0) {
        totalPage++;
    }
    List<Integer> pages = new ArrayList<Integer>();
    for (int index = 1; index <= totalPage; index++) {
        pages.add(index);
    }
    Map data = new HashMap();
    data.put("list", result);
    data.put("totalPage", totalPage);
    data.put("totalRecord", totalRecord);
    data.put("pages", pages);

    return data;
}

From source file:com.arg.arsoft.siantluis.repository.imp.PaymentRepository.java

@Override
public Map findByQuery(PaymentQuery query) {
    int pageSize = Configs.PAGE_SIZE;
    Criteria criteria = factory.getCurrentSession().createCriteria(Payment.class);
    if (query.getCode() != null && !query.getCode().equals("")) {
        if (query.getCode().contains("*") || query.getCode().contains("?")) {
            criteria.add(Restrictions.like("code", query.getCode().replace("*", "%").replace("?", "_")));
        } else {/*from   w w w.j  a  va2  s  .co m*/
            criteria.add(Restrictions.eq("code", query.getCode()));
        }
    }
    if (query.getName() != null && !query.getName().equals("")) {
        if (query.getName().contains("*") || query.getName().contains("?")) {
            criteria.add(Restrictions.like("name", query.getName().replace("*", "%").replace("?", "_")));
        } else {
            criteria.add(Restrictions.eq("name", query.getName()));
        }
    }
    if (query.getPaymentBy() != null && !query.getPaymentBy().equals("")) {
        criteria.createAlias("paymentBy", "p", JoinType.LEFT_OUTER_JOIN);
        criteria.add(Restrictions.eq("p.code", query.getCode()));
    }
    if (query.getPaymentDateFrom() != null) {
        criteria.add(Restrictions.ge("paymentDate", query.getPaymentDateFrom()));
    }
    if (query.getPaymentDateTo() != null) {
        criteria.add(Restrictions.le("paymentDate", query.getPaymentDateTo()));
    }
    if (query.getDepartment() != null && query.getDepartment() != 0) {
        criteria.createAlias("department", "d", JoinType.LEFT_OUTER_JOIN);
        criteria.add(Restrictions.eq("d.id", query.getDepartment()));
    }
    if (query.getBranch() != null && query.getBranch() != 0) {
        criteria.createAlias("branch", "b", JoinType.LEFT_OUTER_JOIN);
        criteria.add(Restrictions.eq("b.id", query.getBranch()));
    }
    long totalRecord = (long) criteria.setProjection(Projections.count(Projections.id().toString()))
            .uniqueResult();

    criteria.setProjection(null);

    int start = ((query.getPage() - 1) * pageSize);

    // fetch assco
    criteria.setFetchMode("paymentBy", FetchMode.JOIN);
    criteria.setFetchMode("department", FetchMode.JOIN);
    criteria.setFetchMode("branch", FetchMode.JOIN);

    List<Payment> result = criteria.setResultTransformer(Criteria.ROOT_ENTITY).setFirstResult(start)
            .setMaxResults(pageSize).list();

    long totalPage = totalRecord / pageSize;
    if ((totalRecord % pageSize) > 0) {
        totalPage++;
    }
    List<Integer> pages = new ArrayList<Integer>();
    for (int index = 1; index <= totalPage; index++) {
        pages.add(index);
    }

    Map data = new HashMap();
    data.put("list", result);
    data.put("totalPage", totalPage);
    data.put("totalRecord", totalRecord);
    data.put("pages", pages);

    System.out.println(pages.size());
    return data;
}

From source file:com.arg.arsoft.siantluis.repository.imp.PurchaseRepository.java

@Override
public Map findByQuery(PurchaseQuery query) {
    int pageSize = Configs.PAGE_SIZE;
    Criteria criteria = factory.getCurrentSession().createCriteria(Purchase.class);
    if (query.getCode() != null && !query.getCode().equals("")) {
        if (query.getCode().contains("*") || query.getCode().contains("?")) {
            criteria.add(Restrictions.like("code", query.getCode().replace("*", "%").replace("?", "_")));
        } else {//from  w  w  w.  jav  a  2s.  c  o  m
            criteria.add(Restrictions.eq("code", query.getCode()));
        }
    }
    if (query.getName() != null && !query.getName().equals("")) {
        if (query.getName().contains("*") || query.getName().contains("?")) {
            criteria.add(Restrictions.like("name", query.getName().replace("*", "%").replace("?", "_")));
        } else {
            criteria.add(Restrictions.eq("name", query.getName()));
        }
    }
    if (query.getPurchaseDateStart() != null) {
        criteria.add(Restrictions.ge("purchaseDate", query.getPurchaseDateStart()));
    }
    if (query.getPurchaseDateEnd() != null) {
        criteria.add(Restrictions.le("purchaseDate", query.getPurchaseDateEnd()));
    }
    if (query.getPurchaseBy() != null && !query.getPurchaseBy().equals("")) {
        criteria.createAlias("purchaseBy", "e", JoinType.LEFT_OUTER_JOIN);
        criteria.add(Restrictions.eq("e.code", query.getPurchaseBy()));
    }
    if (query.getSupplier() != null && !query.getSupplier().equals("")) {
        criteria.createAlias("supplier", "s", JoinType.LEFT_OUTER_JOIN);
        criteria.add(Restrictions.eq("s.code", query.getSupplier()));
    }
    if (query.getDepartment() != null && query.getDepartment() != 0) {
        criteria.createAlias("department", "d", JoinType.LEFT_OUTER_JOIN);
        criteria.add(Restrictions.eq("d.id", query.getDepartment()));
    }
    if (query.getBranch() != null && query.getBranch() != 0) {
        criteria.createAlias("branch", "b", JoinType.LEFT_OUTER_JOIN);
        criteria.add(Restrictions.eq("b.id", query.getBranch()));
    }
    if (query.getStatus() != null && !query.getStatus().equals("")) {
        PurchaseStatus status = PurchaseStatus.valueOf(query.getStatus());
        criteria.add(Restrictions.eq("status", status));
    }

    long totalRecord = (long) criteria.setProjection(Projections.count(Projections.id().toString()))
            .uniqueResult();
    criteria.setProjection(null);
    int start = ((query.getPage() - 1) * pageSize);
    criteria.setFetchMode("department", FetchMode.JOIN);
    criteria.setFetchMode("branch", FetchMode.JOIN);
    criteria.setFetchMode("supplier", FetchMode.JOIN);
    criteria.setFetchMode("purchaseBy", FetchMode.JOIN);
    List<Purchase> result = criteria.setFirstResult(start).setMaxResults(pageSize)
            .setResultTransformer(Criteria.ROOT_ENTITY).list();

    long totalPage = totalRecord / pageSize;
    if ((totalRecord % pageSize) > 0) {
        totalPage++;
    }
    List<Integer> pages = new ArrayList<Integer>();
    for (int index = 1; index <= totalPage; index++) {
        pages.add(index);
    }

    Map data = new HashMap();
    data.put("list", result);
    data.put("totalPage", totalPage);
    data.put("totalRecord", totalRecord);
    data.put("pages", pages);

    System.out.println(pages.size());
    return data;
}

From source file:com.arg.arsoft.siantluis.repository.imp.ReservationRepository.java

@Override
public Map findByQuery(ReservationQuery query) {
    int pageSize = Configs.PAGE_SIZE;
    Criteria criteria = factory.getCurrentSession().createCriteria(Reservation.class);
    if (query.getCode() != null && !query.getCode().equals("")) {
        if (query.getCode().contains("*") || query.getCode().contains("?")) {
            criteria.add(Restrictions.like("code", query.getCode().replace("*", "%").replace("?", "_")));
        } else {//from  w  w w. jav  a 2  s .  c  om
            criteria.add(Restrictions.eq("code", query.getCode()));
        }
    }
    if (query.getDescription() != null && !query.getDescription().equals("")) {
        if (query.getDescription().contains("*") || query.getDescription().contains("?")) {
            criteria.add(Restrictions.like("description",
                    query.getDescription().replace("*", "%").replace("?", "*")));
        } else {
            criteria.add(Restrictions.eq("description", query.getDescription()));
        }
    }
    if (query.getRequestBy() != null && !query.getRequestBy().equals("")) {
        criteria.createAlias("reservationBy", "em", JoinType.LEFT_OUTER_JOIN);
        criteria.add(Restrictions.eq("em.code", query.getRequestBy()));
    }
    if (query.getReservationDateFrom() != null) {
        criteria.add(Restrictions.ge("reservationDate", query.getReservationDateFrom()));
    }
    if (query.getReservationDateTo() != null) {
        criteria.add(Restrictions.le("reservationDate", query.getReservationDateTo()));
    }

    long totalRecord = (long) criteria.setProjection(Projections.count(Projections.id().toString()))
            .uniqueResult();
    int start = ((query.getPage() - 1) * pageSize);

    criteria.setProjection(null);

    criteria.setFetchMode("reservationBy", FetchMode.JOIN);

    List<Reservation> result = criteria.setFirstResult(start).setMaxResults(pageSize)
            .setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY).list();
    long totalPage = totalRecord / pageSize;
    if ((totalRecord % pageSize) > 0) {
        totalPage++;
    }
    List<Integer> pages = new ArrayList<Integer>();
    for (int index = 1; index <= totalPage; index++) {
        pages.add(index);
    }
    Map data = new HashMap();
    data.put("list", result);
    data.put("totalPage", totalPage);
    data.put("totalRecord", totalRecord);
    data.put("pages", pages);

    return data;
}