Example usage for org.hibernate.criterion Restrictions lt

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

Introduction

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

Prototype

public static SimpleExpression lt(String propertyName, Object value) 

Source Link

Document

Apply a "less than" constraint to the named property

Usage

From source file:org.openmrs.logic.util.LogicExpressionToCriterion.java

License:Open Source License

public List<Criterion> evaluateBefore(String leftItem, Operand leftOperand, Operand rightOperand,
        Operator operator, Date indexDate, List<Criterion> c, Criteria criteria) throws LogicException {
    if (!rightOperand.supports(ComparisonOperator.BEFORE))
        throw new LogicException("'before' is not a valid operator on " + leftItem + " and " + rightOperand);
    //leftItem needs to support Criterion.alias for EncounterType
    nullSafeCriterionAdd(c, Restrictions.lt(mapHelper(leftItem), rightOperand));
    return c;/*from   ww w  . ja  v  a2 s  . c o m*/
}

From source file:org.openmrs.logic.util.LogicExpressionToCriterion.java

License:Open Source License

public List<Criterion> evaluateLT(String leftItem, Operand leftOperand, Operand rightOperand, Operator operator,
        Date indexDate, List<Criterion> c, Criteria criteria) throws LogicException {
    if (!rightOperand.supports(ComparisonOperator.LT))
        throw new LogicException("'lt' is not a valid operator on " + leftItem + " and " + rightOperand);
    if (rightOperand instanceof OperandNumeric)
        nullSafeCriterionAdd(c,//  ww  w  .  j a  v a 2 s  .  c om
                Restrictions.lt(mapHelper(leftItem), ((OperandNumeric) rightOperand).asDouble()));
    else if (rightOperand instanceof OperandDate)
        nullSafeCriterionAdd(c, Restrictions.lt(mapHelper(leftItem), rightOperand));
    return c;
}

From source file:org.openmrs.module.accounting.api.db.AccountingDAO.java

License:Open Source License

@SuppressWarnings("unchecked")
public List<IncomeReceipt> getListIncomeReceiptByDate(String startDate, String endDate, boolean includeVoided) {
    Date dStartDate = DateUtils.getDateFromStr(startDate);
    Date dEndDate = DateUtils.getDateFromStr(endDate);

    Criteria criteria = sessionFactory.getCurrentSession().createCriteria(IncomeReceipt.class);

    criteria.add(Restrictions.and(Restrictions.ge("receiptDate", dStartDate),
            Restrictions.lt("receiptDate", DateUtils.addDate(dEndDate, 1))));

    if (!includeVoided)
        criteria.add(Restrictions.eq("voided", false));

    return criteria.list();

}

From source file:org.openmrs.module.accounting.api.db.AccountingDAO.java

License:Open Source License

@SuppressWarnings("unchecked")
public List<IncomeReceiptItem> getListIncomeReceiptItemByDate(String startDate, String endDate) {
    Date dStartDate = DateUtils.getDateFromStr(startDate);
    Date dEndDate = DateUtils.getDateFromStr(endDate);
    Criteria criteria = sessionFactory.getCurrentSession().createCriteria(IncomeReceiptItem.class);
    criteria.add(Restrictions.and(Restrictions.le("receiptDate", dStartDate),
            Restrictions.lt("receiptDate", DateUtils.addDate(dEndDate, 1))));
    return criteria.list();
}

From source file:org.openmrs.module.accounting.api.db.AccountingDAO.java

License:Open Source License

/**
 * Check if given date range is overlap with existing fiscal years
 *   (StartA <= EndB)  and  (EndA >= StartB)
 * @param from/*w w  w .  j  ava 2  s .co  m*/
 * @param to
 * @return
 */
@SuppressWarnings("unchecked")
public List<FiscalYear> getOverlapFiscalYears(Integer fiscalYearId, Date from, Date to) {
    Criteria criteria = sessionFactory.getCurrentSession().createCriteria(FiscalYear.class);
    criteria.add(Restrictions.and(Restrictions.lt("startDate", to), Restrictions.gt("endDate", from)));
    criteria.add(Restrictions.ne("status", GeneralStatus.DELETED));
    return criteria.list();

}

From source file:org.openmrs.module.accounting.api.db.AccountingDAO.java

License:Open Source License

public boolean isBudgetItemOverlap(Account account, Date startDate, Date endDate) {
    Criteria criteria = sessionFactory.getCurrentSession().createCriteria(BudgetItem.class);
    criteria.add(Restrictions.eq("account", account));
    criteria.add(//from  www  .j a  va  2  s. c o m
            Restrictions.and(Restrictions.lt("startDate", endDate), Restrictions.gt("endDate", startDate)));
    return criteria.list().isEmpty() ? false : true;
}

From source file:org.openmrs.module.appointmentscheduling.api.db.hibernate.HibernateAppointmentBlockDAO.java

License:Open Source License

/**
 * Returns the overlapping appointment blocks to the given appointment block.
 * //from  w w w  .j ava 2 s .  co  m
 * @param appointmentBlock is the appointment block for which we want to test overlap.
 * @return the appointment blocks that overlaps to the given appointment block.
 */
@SuppressWarnings("unchecked")
@Override
@Transactional(readOnly = true)
public List<AppointmentBlock> getOverlappingAppointmentBlocks(AppointmentBlock appointmentBlock) {
    Criteria criteria = sessionFactory.getCurrentSession().createCriteria(AppointmentBlock.class);
    Disjunction disjunction = Restrictions.disjunction();
    if (appointmentBlock != null) {
        Date fromDate = appointmentBlock.getStartDate();
        Date toDate = appointmentBlock.getEndDate();
        if (fromDate != null && toDate != null) {
            //let givenAppointmentBlock.startDate = fromDate, givenAppointmentBlock.endDate = toDate.
            //let checkedAppointmentBlock.startDate = fromDate' , checkedAppointmentBlock.endDate = toDate'.

            //1) create the conjunction - (fromDate>=fromDate' AND fromDate<toDate') 
            Conjunction conjunction = Restrictions.conjunction();
            conjunction.add(Restrictions.le("startDate", fromDate));
            conjunction.add(Restrictions.gt("endDate", fromDate));
            //add the conjunction to the disjunction
            disjunction.add(conjunction);
            //2) create the conjunction - (fromDate<fromDate' AND toDate>fromDate')
            conjunction = Restrictions.conjunction();
            conjunction.add(Restrictions.gt("startDate", fromDate));
            conjunction.add(Restrictions.lt("startDate", toDate));
            //add the conjunction to the disjunction
            disjunction.add(conjunction); //the disjunction - (fromDate>=fromDate' AND fromDate<toDate') OR (fromDate<fromDate' AND toDate>fromDate')
            criteria.add(disjunction);

            //restriction for the provider
            criteria.add(Restrictions.eq("provider", appointmentBlock.getProvider()));
            if (appointmentBlock.getAppointmentBlockId() != null) {
                //restriction for not comparing the same appointment blocks
                criteria.add(Restrictions.ne("appointmentBlockId", appointmentBlock.getAppointmentBlockId()));
            }
            //restriction for ignoring "voided" appointment blocks
            criteria.add(Restrictions.eq("voided", false));

            return criteria.list();
        }
    }
    return new ArrayList<AppointmentBlock>();
}

From source file:org.openmrs.module.hospitalcore.db.hibernate.HibernateInventoryDAO.java

public List<InventoryStoreDrugTransactionDetail> listStoreDrugTransactionDetail(Integer storeId, Integer drugId,
        Integer formulationId, Integer isExpiry) throws DAOException {
    Criteria criteria = sessionFactory.getCurrentSession()
            .createCriteria(InventoryStoreDrugTransactionDetail.class, "transactionDetail")
            .createAlias("transactionDetail.transaction", "transaction")
            .add(Restrictions.eq("transaction.store.id", storeId))
            .add(Restrictions.eq("transactionDetail.drug.id", drugId))
            .add(Restrictions.eq("transactionDetail.formulation.id", formulationId));
    criteria.addOrder(Order.desc("transactionDetail.createdOn"));
    if (isExpiry != null && isExpiry == 1) {
        criteria.add(Restrictions.lt("transactionDetail.dateExpiry", new Date()));
    } else {/*from w  w  w . j  a  v a2s . c o  m*/
        criteria.add(Restrictions.ge("transactionDetail.dateExpiry", new Date()));
    }
    List<InventoryStoreDrugTransactionDetail> l = criteria.list();
    return l;
}

From source file:org.openmrs.module.hospitalcore.db.hibernate.HibernateInventoryDAO.java

public List<InventoryStoreDrugTransactionDetail> listViewStockBalance(Integer storeId, Integer categoryId,
        String drugName, String fromDate, String toDate, boolean isExpiry, int min, int max)
        throws DAOException {
    Criteria criteria = sessionFactory.getCurrentSession()
            .createCriteria(InventoryStoreDrugTransactionDetail.class, "transactionDetail")
            .createAlias("transactionDetail.transaction", "transaction")
            .createAlias("transactionDetail.drug", "drugAlias")
            .setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);

    ProjectionList proList = Projections.projectionList();
    proList.add(Projections.groupProperty("drug")).add(Projections.groupProperty("formulation"))
            .add(Projections.sum("currentQuantity")).add(Projections.sum("quantity"))
            .add(Projections.sum("issueQuantity"));
    criteria.add(Restrictions.eq("transaction.store.id", storeId));
    if (categoryId != null) {
        criteria.add(Restrictions.eq("drugAlias.category.id", categoryId));
    }/*from   w  w w  .  j  a  v a2  s. co  m*/
    if (!StringUtils.isBlank(drugName)) {
        criteria.add(Restrictions.like("drugAlias.name", "%" + drugName + "%"));
    }
    if (!StringUtils.isBlank(fromDate) && StringUtils.isBlank(toDate)) {
        String startFromDate = fromDate + " 00:00:00";
        String endFromDate = fromDate + " 23:59:59";
        try {
            criteria.add(Restrictions.and(
                    Restrictions.ge("transactionDetail.createdOn", formatter.parse(startFromDate)),
                    Restrictions.le("transactionDetail.createdOn", formatter.parse(endFromDate))));
        } catch (Exception e) {
            // TODO: handle exception
            System.out.println("listSubStoreIndent>>Error convert date: " + e.toString());
            e.printStackTrace();
        }
    } else if (StringUtils.isBlank(fromDate) && !StringUtils.isBlank(toDate)) {
        String startToDate = toDate + " 00:00:00";
        String endToDate = toDate + " 23:59:59";
        try {
            criteria.add(Restrictions.and(
                    Restrictions.ge("transactionDetail.createdOn", formatter.parse(startToDate)),
                    Restrictions.le("transactionDetail.createdOn", formatter.parse(endToDate))));
        } catch (Exception e) {
            // TODO: handle exception
            System.out.println("listSubStoreIndent>>Error convert date: " + e.toString());
            e.printStackTrace();
        }
    } else if (!StringUtils.isBlank(fromDate) && !StringUtils.isBlank(toDate)) {
        String startToDate = fromDate + " 00:00:00";
        String endToDate = toDate + " 23:59:59";
        try {
            criteria.add(Restrictions.and(
                    Restrictions.ge("transactionDetail.createdOn", formatter.parse(startToDate)),
                    Restrictions.le("transactionDetail.createdOn", formatter.parse(endToDate))));
        } catch (Exception e) {
            // TODO: handle exception
            System.out.println("listInventorySubStoreIndent>>Error convert date: " + e.toString());
            e.printStackTrace();
        }
    }
    if (isExpiry) {
        criteria.add(Restrictions.lt("transactionDetail.dateExpiry", new Date()));
    } else {
        criteria.add(Restrictions.ge("transactionDetail.dateExpiry", new Date()));
    }

    /*
     * Sagar Bele : 13-08-2012 Bug #330 ( [INVENTORY]-error in Current
     * quantity of pharmacy )
     */
    criteria.add(Restrictions.ge("transactionDetail.currentQuantity", 0));

    criteria.setProjection(proList);
    if (max > 0) {
        criteria.setFirstResult(min).setMaxResults(max);
    }
    List<Object> lst = criteria.list();
    if (lst == null || lst.size() == 0)
        return null;
    List<InventoryStoreDrugTransactionDetail> list = new ArrayList<InventoryStoreDrugTransactionDetail>();
    for (int i = 0; i < lst.size(); i++) {
        Object[] row = (Object[]) lst.get(i);
        InventoryStoreDrugTransactionDetail tDetail = new InventoryStoreDrugTransactionDetail();
        tDetail.setDrug((InventoryDrug) row[0]);
        tDetail.setFormulation((InventoryDrugFormulation) row[1]);
        tDetail.setCurrentQuantity((Integer) row[2]);
        tDetail.setQuantity((Integer) row[3]);
        tDetail.setIssueQuantity((Integer) row[4]);
        list.add(tDetail);
    }

    return list;
}

From source file:org.openmrs.module.hospitalcore.db.hibernate.HibernateInventoryDAO.java

public Integer countViewStockBalance(Integer storeId, Integer categoryId, String drugName, String fromDate,
        String toDate, boolean isExpiry) throws DAOException {
    Criteria criteria = sessionFactory.getCurrentSession()
            .createCriteria(InventoryStoreDrugTransactionDetail.class, "transactionDetail")
            .createAlias("transactionDetail.transaction", "transaction")
            .createAlias("transactionDetail.drug", "drugAlias");

    ProjectionList proList = Projections.projectionList();
    proList.add(Projections.groupProperty("drug")).add(Projections.groupProperty("formulation"))
            .add(Projections.sum("currentQuantity")).add(Projections.sum("quantity"))
            .add(Projections.sum("issueQuantity"));
    criteria.add(Restrictions.eq("transaction.store.id", storeId));
    if (categoryId != null) {
        criteria.add(Restrictions.eq("drugAlias.category.id", categoryId));
    }/*from   w  ww .j  av  a2s .co  m*/
    if (!StringUtils.isBlank(drugName)) {
        criteria.add(Restrictions.like("drugAlias.name", "%" + drugName + "%"));
    }
    if (!StringUtils.isBlank(fromDate) && StringUtils.isBlank(toDate)) {
        String startFromDate = fromDate + " 00:00:00";
        String endFromDate = fromDate + " 23:59:59";
        try {
            criteria.add(Restrictions.and(
                    Restrictions.ge("transactionDetail.createdOn", formatter.parse(startFromDate)),
                    Restrictions.le("transactionDetail.createdOn", formatter.parse(endFromDate))));
        } catch (Exception e) {
            // TODO: handle exception
            System.out.println("listSubStoreIndent>>Error convert date: " + e.toString());
            e.printStackTrace();
        }
    } else if (StringUtils.isBlank(fromDate) && !StringUtils.isBlank(toDate)) {
        String startToDate = toDate + " 00:00:00";
        String endToDate = toDate + " 23:59:59";
        try {
            criteria.add(Restrictions.and(
                    Restrictions.ge("transactionDetail.createdOn", formatter.parse(startToDate)),
                    Restrictions.le("transactionDetail.createdOn", formatter.parse(endToDate))));
        } catch (Exception e) {
            // TODO: handle exception
            System.out.println("listSubStoreIndent>>Error convert date: " + e.toString());
            e.printStackTrace();
        }
    } else if (!StringUtils.isBlank(fromDate) && !StringUtils.isBlank(toDate)) {
        String startToDate = fromDate + " 00:00:00";
        String endToDate = toDate + " 23:59:59";
        try {
            criteria.add(Restrictions.and(
                    Restrictions.ge("transactionDetail.createdOn", formatter.parse(startToDate)),
                    Restrictions.le("transactionDetail.createdOn", formatter.parse(endToDate))));
        } catch (Exception e) {
            // TODO: handle exception
            System.out.println("listInventorySubStoreIndent>>Error convert date: " + e.toString());
            e.printStackTrace();
        }
    }
    if (isExpiry) {
        criteria.add(Restrictions.lt("transactionDetail.dateExpiry", new Date()));
    } else {
        criteria.add(Restrictions.ge("transactionDetail.dateExpiry", new Date()));
    }
    criteria.setProjection(proList);
    List<Object> list = criteria.list();
    Number total = 0;
    if (!CollectionUtils.isEmpty(list)) {
        total = (Number) list.size();
    }
    return total.intValue();
}