Example usage for org.hibernate.criterion Restrictions between

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

Introduction

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

Prototype

public static Criterion between(String propertyName, Object low, Object high) 

Source Link

Document

Apply a "between" constraint to the named property

Usage

From source file:org.openmrs.module.mohbilling.db.hibernate.HibernateBillingDAO.java

License:Open Source License

@Override
public List<InsurancePolicy> getInsurancePoliciesBetweenTwodates(Date startDate, Date endDate) {
    // TODO Auto-generated method stub

    Criteria crit = sessionFactory.getCurrentSession().createCriteria(InsurancePolicy.class)
            .add(Restrictions.between("createdDate", startDate, endDate));

    return crit.list();

}

From source file:org.openmrs.module.mohbilling.db.hibernate.HibernateBillingDAO.java

License:Open Source License

public List<PatientBill> getBillsByBeneficiary(Beneficiary beneficiary, Date startDate, Date endDate) {
    Criteria crit = sessionFactory.getCurrentSession().createCriteria(PatientBill.class)
            .add(Restrictions.between("createdDate", startDate, endDate));

    if (beneficiary != null) {
        crit.add(Expression.eq("beneficiary", beneficiary));
    }//from w w  w . j av a 2 s  .co m
    return crit.list();
}

From source file:org.openmrs.module.mohbilling.db.hibernate.HibernateBillingDAO.java

License:Open Source License

@Override
public Set<PatientBill> getRefundedBills(Date startDate, Date endDate, User collector) {

    Criteria crit = sessionFactory.getCurrentSession().createCriteria(BillPayment.class)
            .add(Restrictions.between("createdDate", startDate, endDate));
    //Criteria crit1 = sessionFactory.getCurrentSession().createCriteria(PatientBill.class).add(Restrictions.between("createdDate", startDate, endDate));

    if (collector != null && collector.getUserId() != null) {
        crit.add(Expression.eq("collector", collector));
    }//from w w  w. j  ava  2s.  co  m

    List<BillPayment> payments = crit.list();

    Set<PatientBill> refundedBills = new HashSet<PatientBill>();

    for (BillPayment pay : payments) {

        PatientBill pb = pay.getPatientBill();
        if (pb.getBillItems().size() == 0)
            refundedBills.add(pb);

    }

    return refundedBills;
}

From source file:org.openmrs.module.openhmis.cashier.api.impl.TimesheetServiceImpl.java

License:Open Source License

@Override
public List<Timesheet> getTimesheetsByDate(Provider cashier, Date date) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);//w w w  .  j av a2s.c  o  m
    calendar.set(Calendar.HOUR_OF_DAY, 0);
    calendar.set(Calendar.MINUTE, 0);
    calendar.set(Calendar.SECOND, 0);
    Date startDate = calendar.getTime();

    calendar.set(Calendar.HOUR_OF_DAY, END_DATE_HOUR_OF_DAY);
    calendar.set(Calendar.MINUTE, END_DATE_MINUTE);
    calendar.set(Calendar.SECOND, END_DATE_SECOND);
    Date endDate = calendar.getTime();

    Criteria criteria = getRepository().createCriteria(Timesheet.class);
    criteria.add(Restrictions.and(Restrictions.eq("cashier", cashier), Restrictions.or(
            // Start or end on date
            Restrictions.or(Restrictions.between(CLOCK_IN, startDate, endDate),
                    Restrictions.between(CLOCK_OUT, startDate, endDate)),
            Restrictions.or(
                    // Start on or before date and have not ended
                    Restrictions.and(Restrictions.le(CLOCK_IN, endDate), Restrictions.isNull(CLOCK_OUT)),
                    // Start before and end after date
                    Restrictions.and(Restrictions.le(CLOCK_IN, startDate),
                            Restrictions.ge(CLOCK_OUT, endDate))))));
    criteria.addOrder(Order.desc(CLOCK_IN));

    return getRepository().select(Timesheet.class, criteria);
}

From source file:org.openmrs.module.orderextension.api.db.HibernateOrderExtensionDAO.java

License:Open Source License

/**
  * @see org.openmrs.module.orderextension.api.db.OrderExtensionDAO#getExtendedDrugOrdersForPatient(Patient patient)
  *///from w w w .ja v  a2  s  .  c om
@Override
public List<ExtendedDrugOrder> getExtendedDrugOrdersForPatient(Patient patient, Concept indication,
        Date startDateAfter, Date startDateBefore) {
    Criteria criteria = getCurrentSession().createCriteria(ExtendedDrugOrder.class);

    if (patient != null) {
        criteria.add(Restrictions.eq("patient", patient));
    }

    if (indication != null) {
        criteria.add(Restrictions.eq("indication", indication));
    }
    if (startDateAfter != null && startDateBefore != null) {
        criteria.add(Restrictions.between("startDate", startDateAfter, startDateBefore));
    } else if (startDateAfter != null) {
        criteria.add(Restrictions.ge("startDate", startDateAfter));
    } else if (startDateBefore != null) {
        criteria.add(Restrictions.lt("startDate", startDateBefore));
    }
    criteria.add(Restrictions.eq("voided", false));
    return criteria.list();
}

From source file:org.openmrs.module.registrationcore.api.search.BasicSimilarPatientSearchAlgorithm.java

License:Open Source License

/**
 * Adds a restriction on date to be within +/- the given period in ms.
 * //ww w  .  ja  v  a2s .c  o  m
 * @param criteria
 * @param date
 * @param period
 */
public void addDateWithinPeriodRestriction(Criteria criteria, Date date, long period) {
    Date low = new Date(date.getTime() - period);
    Date high = new Date(date.getTime() + period);
    criteria.add(Restrictions.between("birthdate", low, high));
}

From source file:org.openmrs.module.rwandaprimarycare.db.hibernate.HibernatePrimaryCareDAO.java

License:Open Source License

/**
 * Make this very specific for now, can generalize later if needed. Params are all self-explanatory search terms
 * //from   w  w w .ja  va2  s.  co m
 * @param givenName
 * @param familyName
 * @param age
 * @param gender
 * @param mothersName
 * @param address1
 * @return
 */
public List<Patient> getPatients(String givenName, String familyName, String gender, Float age, int ageRange,
        String address1, PersonAttributeType healthCenterPat, Location userLocation,
        boolean restrictByHealthCenter) {

    //TODO: should this only find patients who have the context's health center?
    //relationships handled at impl level

    List<Patient> patients = new ArrayList<Patient>();

    Date minBirthdate = null;
    Date maxBirthdate = null;

    Criteria crit = sessionFactory.getCurrentSession().createCriteria(Patient.class)
            .add(Restrictions.eq("voided", false));
    //System.out.println("givenName " +  givenName + " familyName " + familyName + " gender " + gender
    //       + " age " + age + " address1 " + address1);

    if (age != null && !age.equals(Float.valueOf(0))) {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.YEAR, -Math.round(age));
        cal.add(Calendar.YEAR, -ageRange);
        minBirthdate = cal.getTime();

        cal = Calendar.getInstance();
        cal.add(Calendar.YEAR, -Math.round(age));
        cal.add(Calendar.YEAR, ageRange);
        maxBirthdate = cal.getTime();

        crit.add(Restrictions.between("birthdate", minBirthdate, maxBirthdate));
    }

    //this does strange things:
    //crit.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);

    if ((givenName != null && !givenName.equals("")) || (familyName != null && !familyName.equals(""))) {
        Criteria namesSubquery = crit.createCriteria("names").add(Restrictions.eq("voided", false));
        if (givenName != null && !givenName.equals(""))
            namesSubquery.add(
                    Restrictions.or(Restrictions.like("givenName", givenName, MatchMode.ANYWHERE).ignoreCase(),
                            Restrictions.like("familyName", givenName, MatchMode.ANYWHERE).ignoreCase()));
        if (familyName != null && !familyName.equals("")) {
            namesSubquery.add(
                    Restrictions.or(Restrictions.like("givenName", familyName, MatchMode.ANYWHERE).ignoreCase(),
                            Restrictions.like("familyName", familyName, MatchMode.ANYWHERE).ignoreCase()));
        }
    }

    if (gender != null && !gender.equals(""))
        crit.add(Restrictions.eq("gender", gender));

    //       
    if (address1 != null && !address1.equals(""))
        crit.createCriteria("addresses").add(Restrictions.eq("voided", false))
                .add(Restrictions.like("address1", address1, MatchMode.ANYWHERE).ignoreCase());

    //relationships -- can we write a straight SQL subquery??

    //restrict by registered health center
    if (restrictByHealthCenter) {
        if (userLocation != null && healthCenterPat != null)
            crit.createCriteria("attributes").add(Restrictions.eq("voided", false))
                    .add(Restrictions.eq("attributeType", healthCenterPat))
                    .add(Restrictions.or(Restrictions.eq("value", userLocation.getLocationId().toString()),
                            Restrictions.isNull("value")));
    }
    patients = crit.list();
    return patients;
}

From source file:org.openmrs.module.smsreminder.api.db.hibernate.HibernateSmsReminderDAO.java

License:Open Source License

@SuppressWarnings("unchecked")
@Override/*from w  w  w. j a v  a2s .co m*/
public List<Sent> getSentBetweenCreatedAndStatus(final Date start, final Date end,
        @SuppressWarnings("rawtypes") final List statuses) throws DAOException {
    final Criteria c = this.sessionFactory.getCurrentSession().createCriteria(Sent.class);
    c.add(Restrictions.between("dateCreated", DatasUtil.formatarMysqlDate(start),
            DatasUtil.formatarMysqlDate(end)));
    c.add(Restrictions.in("status", statuses));
    return c.list();
}

From source file:org.openmrs.module.usagestatistics.api.db.hibernate.HibernateUsageStatisticsDAO.java

License:Open Source License

public List<LogDetailUsage> getLogDetailUsage(Date from, Date until, User user, Patient patient)
        throws DAOException {

    Criteria query = sessionFactory.getCurrentSession().createCriteria(Usage.class);
    query.add(Restrictions.between("timestamp", from, until));
    if (user != null) { // null means all users
        query.add(Restrictions.eq("user", user));
    }//  w  ww .  ja  v a 2  s  .c o m
    if (patient != null) { // null means all patients
        query.add(Restrictions.eq("patient", patient));
    }
    List<Usage> matchUsages = query.list();

    if (matchUsages.size() < 1) {
        return null;
    }

    List<LogDetailUsage> logDetailUsages = new ArrayList<LogDetailUsage>();

    for (Usage usage : matchUsages) {
        LogDetailUsage logDetailUsage = new LogDetailUsage();
        logDetailUsage.setDatetime(usage.getTimestamp());
        logDetailUsage.setUser(usage.getUser());
        logDetailUsage.setPatient(usage.getPatient());

        PatientUsage patientUsage = getPatientUsage(usage);
        logDetailUsage.setPatientUsage(patientUsage);
        List<EncounterUsage> encounterUsages = getEncounterUsages(usage);
        logDetailUsage.setEncounterUsages(encounterUsages);
        List<VisitUsage> visitUsages = getVisitUsages(usage);
        logDetailUsage.setVisitUsages(visitUsages);
        List<OrderUsage> orderUsages = getOrderUsages(usage);
        logDetailUsage.setOrderUsages(orderUsages);

        logDetailUsages.add(logDetailUsage);
    }

    return logDetailUsages;

}

From source file:org.openmrs.module.vcttrac.db.hibernate.VCTModuleDAOImpl.java

License:Open Source License

@SuppressWarnings({ "unchecked" })
@Override/*from w ww . j a  v  a  2s.c  om*/
public Integer getNumberOfNewClientsCounseledAndTestedForHIV(String from, String to, Integer locationId,
        String admissionMode, Integer minAge, Integer maxAge, String gender) {

    List<VCTClient> result = new ArrayList<VCTClient>();

    try {
        List<VCTClient> clientList = getSession().createCriteria(VCTClient.class)
                .add(Restrictions.eq("registrationEntryPoint", admissionMode))
                .add(Restrictions.eq("location", Context.getLocationService().getLocation(locationId)))
                .add(Restrictions.isNotNull("counselingObs")).add(Restrictions.isNotNull("codeTest"))
                .add(Restrictions.between("dateOfRegistration", Context.getDateFormat().parse(from),
                        Context.getDateFormat().parse(to)))
                .list();

        for (VCTClient c : clientList) {
            Person p = c.getClient();
            if (p.getGender().compareToIgnoreCase(gender) == 0 && (p.getAge() >= minAge))
                if (maxAge > 0) {
                    if (p.getAge() < maxAge)
                        result.add(c);
                } else
                    result.add(c);
        }
    } catch (Exception e) {
        log.error(">>>VCT>>Number>>of>>new>>clients>>counseled>>and>>tested>>for>>hiv>> from: " + from
                + ", to: " + to + ", location: " + locationId + ", admissionMode: " + admissionMode
                + ", minAge: " + minAge + ", maxAge: " + maxAge + ", gender: " + gender);
        e.printStackTrace();
    }
    return result.size();
}