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.obp.log.LogDao.java

License:Apache License

@SuppressWarnings("unchecked")
public List<LogEntry> selectEntries(String origin, long startTime, long endTime) {
    return sessionFactory.getCurrentSession().createCriteria(LogEntry.class)
            .add(Restrictions.eq("origin", origin)).add(Restrictions.between("timestamp", startTime, endTime))
            .list();//ww  w.  j a v a2  s. com
}

From source file:org.onebusaway.gtfs_realtime.archiver.service.TripUpdateDaoImpl.java

License:Apache License

@Override
public List<TripUpdateModel> findByDate(Date startDate, Date endDate) {
    DetachedCriteria criteria = DetachedCriteria.forClass(TripUpdateModel.class);
    criteria.add(Restrictions.between("timestamp", startDate, endDate));
    return (List<TripUpdateModel>) _template.findByCriteria(criteria);
}

From source file:org.onebusaway.gtfs_realtime.archiver.service.VehiclePositionDaoImpl.java

License:Apache License

@Override
public List<VehiclePositionModel> getVehiclePositions(String vehicleId, Date startDate, Date endDate) {

    if (endDate == null)
        endDate = new Date();

    if (startDate == null) // 1 hr = 3600000 millisec
        startDate = new Date(endDate.getTime() - 3600000);

    // from VehiclePositionModel where vehicleId=:vehicleId and timestamp >=
    // :startDate and timestamp < :endDate order by timestamp

    DetachedCriteria criteria = DetachedCriteria.forClass(VehiclePositionModel.class);

    if (!StringUtils.isEmpty(vehicleId)) {
        criteria.add(Restrictions.eq("vehicleId", vehicleId));
    }/* w  ww.j  a  v  a 2  s .  co  m*/
    criteria.add(Restrictions.between("timestamp", startDate, endDate));
    criteria.addOrder(Order.asc("timestamp"));

    return _template.findByCriteria(criteria);
}

From source file:org.openfaces.component.filter.HibernateCriterionBuilder.java

License:LGPL

private static Criterion convertToHibernateCriteria(final ExpressionFilterCriterion expressionFilterCriterion) {
    final String property = expressionFilterCriterion.getExpressionStr();
    final Object parameter = expressionFilterCriterion.getArg1();
    FilterCondition condition = expressionFilterCriterion.getCondition();
    Criterion result = condition.process(new FilterConditionProcessor<Criterion>() {
        @Override//w ww . j  av  a2  s .c  o  m
        public Criterion processEmpty() {
            return Restrictions.isNull(property);
        }

        @Override
        public Criterion processEquals() {
            if (parameter == null) {
                return Restrictions.isNull(property);
            }

            if (parameter instanceof Date) {
                TimeZone timeZone = (TimeZone) expressionFilterCriterion.getParameters().get("timeZone");
                Date dayStart = ParametersInterpreter.dayStart((Date) parameter, timeZone);
                Date dayEnd = ParametersInterpreter.dayEnd((Date) parameter, timeZone);
                return Restrictions.and(Restrictions.ge(property, dayStart), Restrictions.le(property, dayEnd));
            } else if (parameter instanceof String) {
                boolean caseSensitive = expressionFilterCriterion.isCaseSensitive();
                if (!caseSensitive) {
                    return Restrictions.like(property, parameter.toString().toLowerCase()).ignoreCase();
                } else {
                    return Restrictions.like(property, parameter);
                }
            } else {
                return Restrictions.eq(property, parameter);
            }
        }

        @Override
        public Criterion processContains() {
            boolean caseSensitive = expressionFilterCriterion.isCaseSensitive();
            if (!caseSensitive) {
                return Restrictions.like(property, "%" + parameter.toString().toLowerCase() + "%").ignoreCase();
            } else {
                return Restrictions.like(property, "%" + parameter + "%");
            }
        }

        @Override
        public Criterion processBegins() {
            boolean caseSensitive = expressionFilterCriterion.isCaseSensitive();
            if (!caseSensitive) {
                return Restrictions.like(property, parameter.toString().toLowerCase() + "%").ignoreCase();
            } else {
                return Restrictions.like(property, parameter + "%");
            }
        }

        @Override
        public Criterion processEnds() {
            boolean caseSensitive = expressionFilterCriterion.isCaseSensitive();
            if (!caseSensitive) {
                return Restrictions.like(property, "%" + parameter.toString().toLowerCase()).ignoreCase();
            } else {
                return Restrictions.like(property, "%" + parameter);
            }
        }

        @Override
        public Criterion processLess() {
            Object correctedParameter = parameter;
            if (parameter instanceof Date) {
                TimeZone timeZone = (TimeZone) expressionFilterCriterion.getParameters().get("timeZone");
                correctedParameter = ParametersInterpreter.dayStart((Date) parameter, timeZone);
            }
            return Restrictions.lt(property, correctedParameter);
        }

        @Override
        public Criterion processGreater() {
            Object correctedParameter = parameter;
            if (parameter instanceof Date) {
                TimeZone timeZone = (TimeZone) expressionFilterCriterion.getParameters().get("timeZone");
                correctedParameter = ParametersInterpreter.dayEnd((Date) parameter, timeZone);
            }
            return Restrictions.gt(property, correctedParameter);
        }

        @Override
        public Criterion processLessOrEqual() {
            Object correctedParameter = parameter;
            if (parameter instanceof Date) {
                TimeZone timeZone = (TimeZone) expressionFilterCriterion.getParameters().get("timeZone");
                correctedParameter = ParametersInterpreter.dayEnd((Date) parameter, timeZone);
            }
            return Restrictions.le(property, correctedParameter);
        }

        @Override
        public Criterion processGreaterOrEqual() {
            Object correctedParameter = parameter;
            if (parameter instanceof Date) {
                TimeZone timeZone = (TimeZone) expressionFilterCriterion.getParameters().get("timeZone");
                correctedParameter = ParametersInterpreter.dayStart((Date) parameter, timeZone);
            }
            return Restrictions.ge(property, correctedParameter);
        }

        @Override
        public Criterion processBetween() {
            Object parameter2 = expressionFilterCriterion.getArg2();
            if (parameter instanceof Date && parameter2 instanceof Date) {
                TimeZone timeZone = (TimeZone) expressionFilterCriterion.getParameters().get("timeZone");
                Date periodStart = ParametersInterpreter.dayStart((Date) parameter, timeZone);
                Date periodEnd = ParametersInterpreter.dayEnd((Date) parameter2, timeZone);
                return Restrictions.and(Restrictions.ge(property, periodStart),
                        Restrictions.le(property, periodEnd));
            } else {
                return Restrictions.between(property, parameter, parameter2);
            }
        }
    });

    if (parameter != null) {
        result = Restrictions.and(Restrictions.isNotNull(property), result);
    }
    return (expressionFilterCriterion.isInverse()) ? Restrictions.not(result) : result;
}

From source file:org.openmrs.logic.db.hibernate.HibernateLogicEncounterDAO.java

License:Open Source License

/**
 * Convenience method to get the list of hibernate queries for this expression
 * /*from   www .  j  av  a 2s.co m*/
 * @param logicExpression
 * @param indexDate
 * @param criteria Criteria object so that certain expressions can add aliases, etc
 * @return Criterion to be added to the Criteria
 */
public Criterion getCriterion(LogicExpression logicExpression, Date indexDate, Criteria criteria) {
    Operator operator = logicExpression.getOperator();
    Object rightOperand = logicExpression.getRightOperand();
    Object leftOperand = null;
    if (logicExpression instanceof LogicExpressionBinary) {
        leftOperand = ((LogicExpressionBinary) logicExpression).getLeftOperand();
    }
    List<Criterion> criterion = new ArrayList<Criterion>();

    //if the leftOperand is a String and does not match any components,
    //see if it is a concept name and restrict accordingly
    //a null operator implies a concept restriction
    if (leftOperand instanceof LogicExpression) {
        // no restrictions if there is no operator
        // TODO restrict on provider != null for encounterProvider token?
    }

    String token = logicExpression.getRootToken();

    if (operator == null) {
        // no restrictions if there is no operator
        // TODO restrict on provider != null for encounterProvider token?
    } else if (operator == Operator.BEFORE || operator == Operator.LT) {
        if (ENCOUNTER_KEY.equalsIgnoreCase(token) && rightOperand instanceof OperandDate) {
            criterion.add(Restrictions.lt("encounterDatetime", rightOperand));
        } else {
            throw new LogicException("'before' is not a valid operator on " + token + " and " + rightOperand);
        }
    } else if (operator == Operator.AFTER || operator == Operator.GT) {
        if (ENCOUNTER_KEY.equalsIgnoreCase(token) && rightOperand instanceof OperandDate) {
            criterion.add(Restrictions.gt("encounterDatetime", rightOperand));
        } else {
            throw new LogicException("'after' is not a valid operator on " + token + " and " + rightOperand);
        }
    } else if (operator == Operator.AND || operator == Operator.OR) {
        if (ENCOUNTER_KEY.equalsIgnoreCase(token) && rightOperand instanceof OperandDate) {
            Criterion leftCriteria = null;
            Criterion rightCriteria = null;
            if (leftOperand instanceof LogicExpression) {
                leftCriteria = this.getCriterion((LogicExpression) leftOperand, indexDate, criteria);
            }
            if (rightOperand instanceof LogicExpression) {
                rightCriteria = this.getCriterion((LogicExpression) rightOperand, indexDate, criteria);
            }

            if (leftCriteria != null && rightCriteria != null) {
                if (operator == Operator.AND) {
                    criterion.add(Restrictions.and(leftCriteria, rightCriteria));
                }
                if (operator == Operator.OR) {
                    criterion.add(Restrictions.or(leftCriteria, rightCriteria));
                }
            }
        } else {
            throw new LogicException("'and/or' are not valid operators on " + token + " and " + rightOperand);
        }
    } else if (operator == Operator.NOT) {

        Criterion rightCriteria = null;

        if (rightOperand instanceof LogicExpression) {
            rightCriteria = this.getCriterion((LogicExpression) rightOperand, indexDate, criteria);
        }

        if (rightCriteria != null) {
            criterion.add(Restrictions.not(rightCriteria));
        }

    } else if (operator == Operator.CONTAINS) {
        if (ENCOUNTER_KEY.equalsIgnoreCase(token) && rightOperand instanceof OperandText) {
            criteria.createAlias("encounterType", "encounterType");
            criterion.add(Expression.eq("encounterType.name", ((OperandText) rightOperand).asString()));
        } else if (LOCATION_KEY.equalsIgnoreCase(token) && rightOperand instanceof OperandText) {
            criteria.createAlias("location", "location");
            criterion.add(Restrictions.eq("location.name", ((OperandText) rightOperand).asString()));
        } else if (PROVIDER_KEY.equalsIgnoreCase(token) && rightOperand instanceof OperandNumeric) {
            criteria.createAlias("provider", "provider");
            criterion.add(Restrictions.eq("provider.personId", ((OperandNumeric) rightOperand).asInteger()));
        } else {
            throw new LogicException("'contains' is not a valid operator on " + token + " and " + rightOperand);
        }
    } else if (operator == Operator.IN) {
        if (ENCOUNTER_KEY.equalsIgnoreCase(token) && rightOperand instanceof OperandCollection) {
            criteria.createAlias("encounterType", "encounterType");
            criterion.add(
                    Expression.in("encounterType.name", ((OperandCollection) rightOperand).asCollection()));
        } else if (LOCATION_KEY.equalsIgnoreCase(token) && rightOperand instanceof OperandCollection) {
            criteria.createAlias("location", "location");
            criterion.add(Restrictions.in("location.name", ((OperandCollection) rightOperand).asCollection()));
        } else if (PROVIDER_KEY.equalsIgnoreCase(token) && rightOperand instanceof OperandCollection) {
            criteria.createAlias("provider", "provider");
            criterion.add(
                    Restrictions.in("provider.systemId", ((OperandCollection) rightOperand).asCollection()));
        } else {
            throw new LogicException("'in' is not a valid operator on " + token + " and " + rightOperand);
        }
    } else if (operator == Operator.EQUALS) {
        if (ENCOUNTER_KEY.equalsIgnoreCase(token) && rightOperand instanceof OperandDate) {
            criterion.add(Restrictions.eq("encounterDatetime", rightOperand));
        } else if (ENCOUNTER_KEY.equalsIgnoreCase(token) && rightOperand instanceof OperandText) {
            criteria.createAlias("encounterType", "encounterType");
            criterion.add(Restrictions.eq("encounterType.name", ((OperandText) rightOperand).asString()));
        } else if (LOCATION_KEY.equalsIgnoreCase(token) && rightOperand instanceof OperandText) {
            criteria.createAlias("location", "location");
            criterion.add(Restrictions.eq("location.name", ((OperandText) rightOperand).asString()));
        } else if (PROVIDER_KEY.equalsIgnoreCase(token) && rightOperand instanceof OperandText) {
            criteria.createAlias("provider", "provider");
            criterion.add(Restrictions.eq("provider.systemId", ((OperandText) rightOperand).asString()));
        } else {
            throw new LogicException("'equals' is not a valid operator on " + token + " and " + rightOperand);
        }
    } else if (operator == Operator.LTE) {
        if (rightOperand instanceof OperandDate)
            criterion.add(Restrictions.le("encounterDatetime", rightOperand));
        else
            throw new LogicException(
                    "'less than or equals' is not a valid operator on " + token + " and " + rightOperand);
    } else if (operator == Operator.GTE) {
        if (rightOperand instanceof OperandDate)
            criterion.add(Restrictions.ge("encounterDatetime", rightOperand));
        else
            throw new LogicException(
                    "'greater than or equals' is not a valid operator on " + token + " and " + rightOperand);
    } else if (operator == Operator.LT) {
        if (rightOperand instanceof OperandDate)
            criterion.add(Restrictions.lt("encounterDatetime", rightOperand));
        else
            throw new LogicException(
                    "'less than' is not a valid operator on " + token + " and " + rightOperand);

    } else if (operator == Operator.GT) {
        if (rightOperand instanceof OperandDate)
            criterion.add(Restrictions.gt("encounterDatetime", rightOperand));
        else
            throw new LogicException(
                    "'greater than' is not a valid operator on " + token + " and " + rightOperand);

    } else if (operator == Operator.EXISTS) {
        // EXISTS can be handled on the higher level (above
        // LogicService, even) by coercing the Result into a Boolean for
        // each patient
    } else if (operator == Operator.ASOF && rightOperand instanceof Date) {
        indexDate = (Date) rightOperand;
        criterion.add(Restrictions.le("encounterDatetime", indexDate));

    } else if (operator == Operator.WITHIN) {
        if (rightOperand instanceof Duration) {
            Duration duration = (Duration) rightOperand;
            Calendar within = Calendar.getInstance();
            within.setTime(indexDate);

            if (duration.getUnits() == Duration.Units.YEARS) {
                within.add(Calendar.YEAR, duration.getDuration().intValue());
            } else if (duration.getUnits() == Duration.Units.MONTHS) {
                within.add(Calendar.MONTH, duration.getDuration().intValue());
            } else if (duration.getUnits() == Duration.Units.WEEKS) {
                within.add(Calendar.WEEK_OF_YEAR, duration.getDuration().intValue());
            } else if (duration.getUnits() == Duration.Units.DAYS) {
                within.add(Calendar.DAY_OF_YEAR, duration.getDuration().intValue());
            } else if (duration.getUnits() == Duration.Units.HOURS) {
                within.add(Calendar.HOUR_OF_DAY, duration.getDuration().intValue());
            } else if (duration.getUnits() == Duration.Units.MINUTES) {
                within.add(Calendar.MINUTE, duration.getDuration().intValue());
            } else if (duration.getUnits() == Duration.Units.SECONDS) {
                within.add(Calendar.SECOND, duration.getDuration().intValue());
            }

            if (indexDate.compareTo(within.getTime()) > 0) {
                criterion.add(Restrictions.between("encounterDatetime", within.getTime(), indexDate));
            } else {
                criterion.add(Restrictions.between("encounterDatetime", indexDate, within.getTime()));
            }
        } else {
            throw new LogicException("'within' is not a valid operator on " + token + " and " + rightOperand);
        }
    }

    Criterion c = null;

    for (Criterion crit : criterion) {
        if (c == null) {
            c = crit;
        } else {
            c = Restrictions.and(c, crit);
        }
    }
    return c;
}

From source file:org.openmrs.logic.db.hibernate.HibernateLogicObsDAO.java

License:Open Source License

public Criterion getCriterion(LogicExpression logicExpression, Date indexDate, Criteria criteria) {
    Operator operator = logicExpression.getOperator();
    Operand rightOperand = logicExpression.getRightOperand();
    Operand leftOperand = null;/*from   w ww  .  jav a2 s  . com*/
    if (logicExpression instanceof LogicExpressionBinary) {
        leftOperand = ((LogicExpressionBinary) logicExpression).getLeftOperand();
    }
    List<Criterion> criterion = new ArrayList<Criterion>();

    //the root token can be a concept name for the obs datasource
    String rootToken = logicExpression.getRootToken();

    Concept concept = getConceptForToken(rootToken);
    if (concept != null) {
        criterion.add(Restrictions.eq("concept", concept));
    } else {
        if (rootToken != null && (rootToken.equalsIgnoreCase(COMPONENT_ENCOUNTER_ID)
                || rootToken.equalsIgnoreCase(COMPONENT_OBS_DATETIME))) {
            //this is a component not a concept so it is fine
        } else {
            throw new LogicException("Concept: " + rootToken + " does not exist");
        }
    }

    if (operator == Operator.BEFORE) {
        criterion.add(Restrictions.lt("obsDatetime", rightOperand));

    } else if (operator == Operator.AFTER) {
        criterion.add(Restrictions.gt("obsDatetime", rightOperand));

    } else if (operator == Operator.AND || operator == Operator.OR) {

        Criterion leftCriteria = null;
        Criterion rightCriteria = null;

        if (leftOperand instanceof LogicExpression) {
            leftCriteria = this.getCriterion((LogicExpression) leftOperand, indexDate, criteria);
        }
        if (rightOperand instanceof LogicExpression) {
            rightCriteria = this.getCriterion((LogicExpression) rightOperand, indexDate, criteria);
        }

        if (leftCriteria != null && rightCriteria != null) {
            if (operator == Operator.AND) {
                criterion.add(Restrictions.and(leftCriteria, rightCriteria));
            }
            if (operator == Operator.OR) {
                criterion.add(Restrictions.or(leftCriteria, rightCriteria));
            }
        }
    } else if (operator == Operator.NOT) {

        Criterion rightCriteria = null;

        if (rightOperand instanceof LogicExpression) {
            rightCriteria = this.getCriterion((LogicExpression) rightOperand, indexDate, criteria);
        }

        if (rightCriteria != null) {
            criterion.add(Restrictions.not(rightCriteria));
        }

    } else if (operator == Operator.CONTAINS) {
        // used with PROBLEM ADDED concept, to retrieve the "ANSWERED
        // BY" concept, stashed inside the concept's valueCoded member
        // variable. for example:
        // new LogicCriteria("PROBLEM ADDED").contains("HIV INFECTED");
        if (rightOperand instanceof OperandNumeric) {
            concept = Context.getConceptService().getConcept(((OperandNumeric) rightOperand).asInteger());
            criterion.add(Restrictions.eq("valueCoded", concept));
        } else if (rightOperand instanceof OperandText) {
            concept = Context.getConceptService().getConcept((String) ((OperandText) rightOperand).asString());
            criterion.add(Restrictions.eq("valueCoded", concept));

        } else if (rightOperand instanceof OperandConcept) {
            criterion.add(Restrictions.eq("valueCoded", ((OperandConcept) rightOperand).asConcept()));

        } else
            log.error("Invalid operand value for CONTAINS operation");
    } else if (operator == Operator.IN) {
        log.error("Invalid operand value for IN operation");
    } else if (operator == Operator.EQUALS) {
        if (rightOperand instanceof OperandNumeric) {
            if (rootToken.equalsIgnoreCase(COMPONENT_ENCOUNTER_ID)) {
                EncounterService encounterService = Context.getEncounterService();
                Encounter encounter = encounterService
                        .getEncounter(((OperandNumeric) rightOperand).asInteger());
                criterion.add(Restrictions.eq("encounter", encounter));
            } else
                criterion.add(Restrictions.eq("valueNumeric", ((OperandNumeric) rightOperand).asDouble()));
        } else if (rightOperand instanceof OperandText)
            criterion.add(Restrictions.eq("valueText", ((OperandText) rightOperand).asString()));
        else if (rightOperand instanceof OperandDate)
            if (leftOperand instanceof OperandText
                    && ((OperandText) leftOperand).asString().equals(COMPONENT_OBS_DATETIME)) {
                criterion.add(Restrictions.eq(COMPONENT_OBS_DATETIME, rightOperand));
            } else {
                criterion.add(Restrictions.eq("valueDatetime", rightOperand));
            }
        else if (rightOperand instanceof OperandConcept)
            criterion.add(Restrictions.eq("valueCoded", ((OperandConcept) rightOperand).asConcept()));
        else
            log.error("Invalid operand value for EQUALS operation");

    } else if (operator == Operator.LTE) {
        if (rightOperand instanceof OperandNumeric)
            criterion.add(Restrictions.le("valueNumeric", ((OperandNumeric) rightOperand).asDouble()));
        else if (rightOperand instanceof OperandDate)
            if (leftOperand instanceof OperandText
                    && ((OperandText) leftOperand).asString().equals(COMPONENT_OBS_DATETIME)) {
                criterion.add(Restrictions.le(COMPONENT_OBS_DATETIME, rightOperand));
            } else {
                criterion.add(Restrictions.le("valueDatetime", rightOperand));
            }
        else
            log.error("Invalid operand value for LESS THAN EQUAL operation");

    } else if (operator == Operator.GTE) {
        if (rightOperand instanceof OperandNumeric)
            criterion.add(Restrictions.ge("valueNumeric", ((OperandNumeric) rightOperand).asDouble()));
        else if (rightOperand instanceof OperandDate)
            if (leftOperand instanceof OperandText
                    && ((OperandText) leftOperand).asString().equals(COMPONENT_OBS_DATETIME)) {
                criterion.add(Restrictions.ge(COMPONENT_OBS_DATETIME, rightOperand));
            } else {
                criterion.add(Restrictions.ge("valueDatetime", rightOperand));
            }
        else
            log.error("Invalid operand value for GREATER THAN EQUAL operation");

    } else if (operator == Operator.LT) {
        if (rightOperand instanceof OperandNumeric)
            criterion.add(Restrictions.lt("valueNumeric", ((OperandNumeric) rightOperand).asDouble()));
        else if (rightOperand instanceof OperandDate)
            if (leftOperand instanceof OperandText
                    && ((OperandText) leftOperand).asString().equals(COMPONENT_OBS_DATETIME)) {
                criterion.add(Restrictions.lt(COMPONENT_OBS_DATETIME, rightOperand));
            } else {
                criterion.add(Restrictions.lt("valueDatetime", rightOperand));
            }
        else
            log.error("Invalid operand value for LESS THAN operation");

    } else if (operator == Operator.GT) {
        if (rightOperand instanceof OperandNumeric)
            criterion.add(Restrictions.gt("valueNumeric", ((OperandNumeric) rightOperand).asDouble()));
        else if (rightOperand instanceof OperandDate)
            if (leftOperand instanceof OperandText
                    && ((OperandText) leftOperand).asString().equals(COMPONENT_OBS_DATETIME)) {
                criterion.add(Restrictions.gt(COMPONENT_OBS_DATETIME, rightOperand));
            } else {
                criterion.add(Restrictions.gt("valueDatetime", rightOperand));
            }
        else
            log.error("Invalid operand value for GREATER THAN operation");

    } else if (operator == Operator.EXISTS) {
        // EXISTS can be handled on the higher level (above
        // LogicService, even) by coercing the Result into a Boolean for
        // each patient
    } else if (operator == Operator.ASOF && rightOperand instanceof OperandDate) {
        indexDate = (Date) rightOperand;
        criterion.add(Restrictions.le("obsDatetime", indexDate));

    } else if (operator == Operator.WITHIN && rightOperand instanceof Duration) {

        Duration duration = (Duration) rightOperand;
        Calendar within = Calendar.getInstance();
        within.setTime(indexDate);

        if (duration.getUnits() == Duration.Units.YEARS) {
            within.add(Calendar.YEAR, duration.getDuration().intValue());
        } else if (duration.getUnits() == Duration.Units.MONTHS) {
            within.add(Calendar.MONTH, duration.getDuration().intValue());
        } else if (duration.getUnits() == Duration.Units.WEEKS) {
            within.add(Calendar.WEEK_OF_YEAR, duration.getDuration().intValue());
        } else if (duration.getUnits() == Duration.Units.DAYS) {
            within.add(Calendar.DAY_OF_YEAR, duration.getDuration().intValue());
        } else if (duration.getUnits() == Duration.Units.HOURS) {
            within.add(Calendar.HOUR_OF_DAY, duration.getDuration().intValue());
        } else if (duration.getUnits() == Duration.Units.MINUTES) {
            within.add(Calendar.MINUTE, duration.getDuration().intValue());
        } else if (duration.getUnits() == Duration.Units.SECONDS) {
            within.add(Calendar.SECOND, duration.getDuration().intValue());
        }

        if (indexDate.compareTo(within.getTime()) > 0) {
            criterion.add(Restrictions.between("obsDatetime", within.getTime(), indexDate));
        } else {
            criterion.add(Restrictions.between("obsDatetime", indexDate, within.getTime()));
        }
    }

    Criterion c = null;

    for (Criterion crit : criterion) {
        if (c == null) {
            c = crit;
        } else {
            c = Restrictions.and(c, crit);
        }
    }
    return c;
}

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

License:Open Source License

public List<Criterion> evaluateWithin(String leftItem, Operand leftOperand, Operand rightOperand,
        Operator operator, Date indexDate, List<Criterion> c, Criteria criteria) throws LogicException {
    if (!rightOperand.supports(ComparisonOperator.WITHIN))
        throw new LogicException("'within' is not a valid operator on " + leftItem + " and " + rightOperand);

    Duration duration = (Duration) rightOperand;
    Calendar within = Calendar.getInstance();
    within.setTime(indexDate);/*from w  ww .j  a  v  a  2 s  .  co m*/

    if (duration.getUnits() == Duration.Units.YEARS) {
        within.add(Calendar.YEAR, duration.getDuration().intValue());
    } else if (duration.getUnits() == Duration.Units.MONTHS) {
        within.add(Calendar.MONTH, duration.getDuration().intValue());
    } else if (duration.getUnits() == Duration.Units.WEEKS) {
        within.add(Calendar.WEEK_OF_YEAR, duration.getDuration().intValue());
    } else if (duration.getUnits() == Duration.Units.DAYS) {
        within.add(Calendar.DAY_OF_YEAR, duration.getDuration().intValue());
    } else if (duration.getUnits() == Duration.Units.MINUTES) {
        within.add(Calendar.MINUTE, duration.getDuration().intValue());
    } else if (duration.getUnits() == Duration.Units.SECONDS) {
        within.add(Calendar.SECOND, duration.getDuration().intValue());
    }
    if (indexDate.compareTo(within.getTime()) > 0) {
        nullSafeCriterionAdd(c, Restrictions.between(mapHelper(leftItem), within.getTime(), indexDate));
    } else {
        nullSafeCriterionAdd(c, Restrictions.between(mapHelper(leftItem), indexDate, within.getTime()));
    }
    return c;
}

From source file:org.openmrs.module.accessmonitor.api.db.hibernate.HibernatePersonAccessDAO.java

License:Open Source License

public List<PersonServiceAccess> getPersonAccessesByAccessDateOrderByPersonId(Date from, Date to) {
    Criteria crit = sessionFactory.getCurrentSession().createCriteria(PersonServiceAccess.class);
    crit.addOrder(Order.asc("personId"));
    if (from != null || to != null) {
        if (from == null) {
            crit.add(Restrictions.le("accessDate", to));
        } else if (to == null) {
            crit.add(Restrictions.ge("accessDate", from));
        } else {/* w  w  w  .j a v  a2 s  .  c  o m*/
            crit.add(Restrictions.between("accessDate", from, to));
        }
    }
    return crit.list();
}

From source file:org.openmrs.module.accessmonitor.api.db.hibernate.HibernatePersonAccessDAO.java

License:Open Source License

public List<PersonServiceAccess> getPersonServiceAccessesByAccessDate(Date from, Date to) {
    Criteria crit = sessionFactory.getCurrentSession().createCriteria(PersonServiceAccess.class);
    if (from != null || to != null) {
        if (from == null) {
            crit.add(Restrictions.le("accessDate", to));
        } else if (to == null) {
            crit.add(Restrictions.ge("accessDate", from));
        } else {//from   w w  w  .j ava2s.c  o  m
            crit.add(Restrictions.between("accessDate", from, to));
        }
    }
    return crit.list();
}

From source file:org.openmrs.module.accessmonitor.api.db.hibernate.HibernatePersonAccessDAO.java

License:Open Source License

public List<PersonServiceAccess> getPersonServiceAccessesByAccessorId(Integer accessorId, Date from, Date to) {
    Criteria crit = sessionFactory.getCurrentSession().createCriteria(PersonServiceAccess.class);
    crit.add(Restrictions.eq("accessorId", accessorId));
    if (from != null || to != null) {
        if (from == null) {
            crit.add(Restrictions.le("accessDate", to));
        } else if (to == null) {
            crit.add(Restrictions.ge("accessDate", from));
        } else {//from   w w w .j  a  va 2s .co  m
            crit.add(Restrictions.between("accessDate", from, to));
        }
    }
    return crit.list();
}