Example usage for org.hibernate.criterion Restrictions gt

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

Introduction

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

Prototype

public static SimpleExpression gt(String propertyName, Object value) 

Source Link

Document

Apply a "greater than" constraint to the named property

Usage

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//from   w w  w . j  a va  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   w  ww . ja  v a 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.amrscustomization.db.HibernateAMRSCustomizationDAO.java

License:Open Source License

public List<Form> getPopularRecentFormsForUser(User user) {
    String monthsStr = Context.getAdministrationService()
            .getGlobalProperty(AMRSCustomizationConstants.GP_RECENT_FORMS_INTERVAL);
    Integer months = AMRSCustomizationConstants.DEFAULT_RECENT_FORMS_INTERVAL;
    try {/*w  ww. j a v a 2  s .  co  m*/
        months = Integer.parseInt(monthsStr);
    } catch (NumberFormatException ex) {
        log.warn("could not interpret " + monthsStr + " interval as an integer.");
    }

    Calendar monthsAgo = Calendar.getInstance();
    monthsAgo.add(Calendar.MONTH, -1 * months);

    ProjectionList projectionList = Projections.projectionList();
    projectionList.add(Projections.groupProperty("form"));
    projectionList.add(Projections.alias(Projections.rowCount(), "total"));

    Criteria crit = sessionFactory.getCurrentSession().createCriteria(Encounter.class)
            .add(Restrictions.and(Restrictions.eq("creator", user),
                    Restrictions.gt("dateCreated", monthsAgo.getTime())))
            .setProjection(projectionList).addOrder(Order.desc("total")).setMaxResults(5);

    List<Form> forms = new ArrayList<Form>();
    List<Object[]> foo = crit.list();
    for (Object[] result : foo) {
        log.warn(result[0] + ": " + result[1]);
        forms.add((Form) result[0]);
    }

    return forms;
}

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   www  .  j a v a2  s . com*/
 * @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.conceptsearch.db.hibernate.HibernateConceptSearchDAO.java

License:Open Source License

/**
 * Method to create the criteria from the ConceptSearch object
 * //w w w .j  av a 2  s.c o  m
 * @param cs ConceptSearch object that contains all criteria
 * @return search criteria
 */
private Criteria createGetConceptsCriteria(ConceptSearch cs) {
    Criteria crit = sessionFactory.getCurrentSession().createCriteria(Concept.class);

    if (!cs.getSearchQuery().isEmpty()) {
        crit.createAlias("names", "names");
        crit.add(Restrictions.like("names.name", "%" + cs.getSearchQuery() + "%"));
    }

    /*      if (CollectionUtils.isNotEmpty(cs.getSearchTermsList())) {
       crit.add(Restrictions.in("description", cs.getSearchTermsList())); //TODO: contains? like?
    }*/

    if (CollectionUtils.isNotEmpty(cs.getDataTypes())) {
        crit.add(Restrictions.in("datatype", cs.getDataTypes()));
    }

    if (CollectionUtils.isNotEmpty(cs.getConceptClasses())) {
        crit.add(Restrictions.in("conceptClass", cs.getConceptClasses()));
    }

    if (cs.getIsSet() != -1) {
        if (cs.getIsSet() == 0) {
            crit.add(Restrictions.eq("set", Boolean.FALSE));
        } else {
            crit.add(Restrictions.eq("set", Boolean.TRUE));
        }
    }

    if ((cs.getDateFrom() != null) && (cs.getDateTo() != null)) {
        crit.add(Restrictions.between("dateCreated", cs.getDateFrom(), cs.getDateTo()));
    } else if (cs.getDateFrom() != null) {
        crit.add(Restrictions.gt("dateCreated", cs.getDateFrom()));
    } else if (cs.getDateTo() != null) {
        crit.add(Restrictions.le("dateCreated", cs.getDateTo()));
    }

    return crit;

}

From source file:org.openmrs.module.dictionarypublishing.api.db.hibernate.HibernateDictionaryPublishingDAO.java

License:Open Source License

private Criteria newModifiedConceptsCriteria(Date fromDate) {
    Criteria criteria = sessionFactory.getCurrentSession().createCriteria(Concept.class);
    if (fromDate != null) {
        Criterion c;/*  w  w  w  .ja  va2s.c  o m*/
        if (hasDateRetiredColumn) {
            c = Restrictions.or(Restrictions.gt("dateCreated", fromDate), Restrictions
                    .or(Restrictions.gt("dateChanged", fromDate), Restrictions.gt("dateRetired", fromDate)));
        } else {
            c = Restrictions.or(Restrictions.gt("dateCreated", fromDate),
                    Restrictions.gt("dateChanged", fromDate));
        }
        criteria.add(c);
    }
    return criteria;
}

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

public List<InventoryStoreDrugTransactionDetail> listStoreDrugTransactionDetail(Integer storeId, Integer drugId,
        Integer formulationId, boolean haveQuantity) 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))
            .add(Restrictions.eq("transaction.typeTransaction", ActionValue.TRANSACTION[0]));

    String date = formatterExt.format(new Date());
    String startFromDate = date + " 00:00:00";

    if (haveQuantity) {
        criteria.add(Restrictions.gt("transactionDetail.currentQuantity", 0));
        try {/*from  w  w w. j a va 2s  . c o  m*/
            criteria.add(Restrictions.ge("transactionDetail.dateExpiry", formatter.parse(startFromDate)));
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
    criteria.addOrder(Order.asc("transactionDetail.dateExpiry"));
    List<InventoryStoreDrugTransactionDetail> l = criteria.list();
    return l;
}

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

public Integer sumCurrentQuantityDrugOfStore(Integer storeId, Integer drugId, Integer formulationId)
        throws DAOException {
    Criteria criteria = sessionFactory.getCurrentSession()
            .createCriteria(InventoryStoreDrugTransactionDetail.class, "transactionDetail")
            .createAlias("transactionDetail.transaction", "transaction")
            .add(Restrictions.eq("transaction.store.id", storeId))
            .add(Restrictions.eq("transaction.typeTransaction", ActionValue.TRANSACTION[0]))
            .add(Restrictions.eq("transactionDetail.drug.id", drugId))
            .add(Restrictions.eq("transactionDetail.formulation.id", formulationId));

    criteria.add(Restrictions.gt("transactionDetail.currentQuantity", 0));
    criteria.add(Restrictions.gt("transactionDetail.dateExpiry", new Date()));
    ProjectionList proList = Projections.projectionList();
    proList.add(Projections.sum("currentQuantity"));
    criteria.setProjection(proList);/*w w w  .j a v  a  2s  .  c  o m*/
    Object l = criteria.uniqueResult();
    return l != null ? (Integer) l : 0;
}

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

License:Open Source License

public List<Obs> getAllDiagnosis(Integer personId) throws DAOException {
    Criteria criteria = sessionFactory.getCurrentSession().createCriteria(Obs.class, "obs");
    String toDdate = formatter1.format(new Date());

    Date date1 = new Date();

    Date oldDate = new Date(date1.getTime() - TimeUnit.HOURS.toMillis(24));
    String fromDate = formatter1.format(oldDate);

    try {// w w w .  j  av a  2 s. c  om
        criteria.add(Restrictions.lt("obs.obsDatetime", formatter1.parse(toDdate)));
        criteria.add(Restrictions.gt("obs.obsDatetime", formatter1.parse(fromDate)));

    } catch (Exception e) {
        // TODO: handle exception
        System.out.println("Error convert date: " + e.toString());
        e.printStackTrace();
    }

    criteria.add(Restrictions.eq("obs.personId", personId));
    criteria.add(
            Restrictions.eq("obs.concept", Context.getConceptService().getConcept("PROVISIONAL DIAGNOSIS")));

    return criteria.list();
}

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

License:Open Source License

public List<Obs> getAllSymptom(Integer personId) throws DAOException {

    Criteria criteria = sessionFactory.getCurrentSession().createCriteria(Obs.class, "obs");
    String toDdate = formatter1.format(new Date());

    Date date1 = new Date();

    Date oldDate = new Date(date1.getTime() - TimeUnit.HOURS.toMillis(24));
    String fromDate = formatter1.format(oldDate);

    try {/*from  w w w .j  a  v  a  2s  .c  o  m*/
        criteria.add(Restrictions.lt("obs.obsDatetime", formatter1.parse(toDdate)));
        criteria.add(Restrictions.gt("obs.obsDatetime", formatter1.parse(fromDate)));

    } catch (Exception e) {
        // TODO: handle exception
        System.out.println("Error convert date: " + e.toString());
        e.printStackTrace();
    }

    criteria.add(Restrictions.eq("obs.personId", personId));
    criteria.add(Restrictions.eq("obs.concept", Context.getConceptService().getConcept("SYMPTOM")));

    return criteria.list();
}