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.motechproject.mobile.core.dao.hibernate.GatewayRequestDAOImpl.java

License:Open Source License

/**
 *  @see {@link org.motechproject.mobile.core.dao.GatewayRequestDAO#getByStatusAndSchedule}
 *///from ww  w  . ja  v a2 s  .c om
public List<GatewayRequest> getByStatusAndSchedule(MStatus status, Date schedule) {
    logger.debug("variables passed to getByStatusAndSchedule. status: " + status + "And schedule: " + schedule);

    try {

        List<GatewayRequest> allbystatandSdule;
        Criteria criteria = this.getSessionFactory().getCurrentSession().createCriteria(getPersistentClass());
        if (schedule == null) {
            criteria = criteria.add(Restrictions.isNull("dateTo")).add(Restrictions.isNull("dateFrom"))
                    .add(Restrictions.eq("messageStatus", status));
        } else {
            criteria = criteria.add(Restrictions.eq("messageStatus", status))
                    .add(Restrictions.or(Restrictions.isNull("dateFrom"),
                            Restrictions.lt("dateFrom", schedule)))
                    .add(Restrictions.or(Restrictions.isNull("dateTo"), Restrictions.gt("dateTo", schedule)));
        }

        allbystatandSdule = (List<GatewayRequest>) criteria.add(Restrictions.isNotNull("gatewayRequestDetails"))
                .list();
        logger.debug(allbystatandSdule);

        return allbystatandSdule;

    } catch (HibernateException he) {

        logger.error("Persistence or JDBC Exception in Method getByStatusAndSchedule", he);
        return null;
    } catch (Exception ex) {

        logger.error("Exception in Method getByStatusAndSchedule", ex);
        return null;
    }
}

From source file:org.motechproject.mobile.core.dao.hibernate.MessageRequestDAOImpl.java

License:Open Source License

/**
 * @see  {@link org.motechproject.mobile.core.dao.MessageRequestDAO#getMsgRequestByStatusAndSchedule(org.motechproject.mobile.core.model.MStatus, java.util.Date)  }
 *//*from  w  ww .  j a  va  2s . c  o m*/
public List getMsgRequestByStatusAndSchedule(MStatus status, Date schedule) {
    logger.debug("variables passed to getMsgRequestByStatusAndSchedule. status: " + status + "And schedule: "
            + schedule);

    try {

        List msgRequest = this.getSessionFactory().getCurrentSession().createCriteria(this.getPersistentClass())
                .add(Restrictions.eq("status", status)).add(Restrictions.lt("dateFrom", schedule))
                .add(Restrictions.gt("dateTo", schedule)).list();
        logger.debug(msgRequest);
        return msgRequest;

    } catch (HibernateException he) {
        logger.error("Persistence or JDBC Exception in Method getMsgRequestByStatusAndSchedule", he);
        return null;
    } catch (Exception ex) {
        logger.error("Exception in Method getMsgRequestByStatusAndSchedule", ex);
        return null;
    }

}

From source file:org.motechproject.mobile.core.dao.hibernate.MessageRequestDAOImpl.java

License:Open Source License

/**
 *
 * @see {@link org.motechproject.mobile.core.dao.MessageRequestDAO#getMsgRequestByRecipientAndSchedule(java.lang.String, java.util.Date)}
 *//*from  ww w. j a  v a2 s.c  om*/
public List<MessageRequest> getMsgRequestByRecipientAndSchedule(String recipientID, Date schedule) {
    logger.debug("variables passed to getMsgRequestByRecipientAndSchedule. recipientID: " + recipientID
            + "And schedule: " + schedule);

    try {

        List msgRequest = this.getSessionFactory().getCurrentSession().createCriteria(this.getPersistentClass())
                .add(Restrictions.eq("recipientId", recipientID)).add(Restrictions.lt("dateFrom", schedule))
                .add(Restrictions.gt("dateTo", schedule)).list();
        logger.debug(msgRequest);
        return msgRequest;

    } catch (HibernateException he) {
        logger.error("Persistence or JDBC Exception in Method getMsgRequestByStatusAndSchedule", he);
        return null;
    } catch (Exception ex) {
        logger.error("Exception in Method getMsgRequestByStatusAndSchedule", ex);
        return null;
    }
}

From source file:org.motechproject.mobile.core.dao.hibernate.MessageRequestDAOImpl.java

License:Open Source License

/**
 *
 * @see {@link org.motechproject.mobile.core.dao.MessageRequestDAO#getMsgRequestByRecipientDateFromBetweenDates(java.lang.String, java.util.Date, java.util.Date) }
 *//*  w  ww. ja v a 2  s  . c  om*/
public List<MessageRequest> getMsgRequestByRecipientDateFromBetweenDates(String recipientID, Date startDate,
        Date endDate) {
    logger.debug("variables passed to getMsgRequestByRecipientDateFromBetweenDates. recipientID: " + recipientID
            + " dateFrom after " + startDate + " before " + endDate);

    try {

        List msgRequest = this.getSessionFactory().getCurrentSession().createCriteria(this.getPersistentClass())
                .add(Restrictions.eq("recipientId", recipientID)).add(Restrictions.gt("dateFrom", startDate))
                .add(Restrictions.lt("dateFrom", endDate)).list();
        logger.debug(msgRequest);
        return msgRequest;

    } catch (HibernateException he) {
        logger.error("Persistence or JDBC Exception in Method getMsgRequestByStatusAndSchedule", he);
        return null;
    } catch (Exception ex) {
        logger.error("Exception in Method getMsgRequestByStatusAndSchedule", ex);
        return null;
    }
}

From source file:org.n52.sos.ds.hibernate.util.TemporalRestriction.java

License:Open Source License

/**
 * Creates a <tt>Criterion</tt> for the specified property. Used to easily
 * swap &lt; and &le;./*from   w w w .  j a  v  a2 s.c o m*/
 * 
 * @param property
 *            the property name
 * @param value
 *            the compared value
 * 
 * @return the <tt>Criterion</tt>
 */
protected Criterion lower(String property, Date value) {
    return ALLOW_EQUALITY ? Restrictions.le(property, value) : Restrictions.lt(property, value);
}

From source file:org.nema.medical.mint.server.domain.StudyDAO.java

License:Apache License

@SuppressWarnings("unchecked")
public List<MINTStudy> findStudies(final String studyInstanceUID, final String accessionNumber,
        final String accessionNumberIssuer, final String patientID, final String patientIDIssuer,
        final Date minStudyDateTime, final Date minStudyDate, final Date maxStudyDateTime,
        final Date maxStudyDate, final int limit, final int offset) throws DateTimeParseException {

    final DetachedCriteria detachedCriteria = DetachedCriteria.forClass(MINTStudy.class);
    detachedCriteria.addOrder(Order.desc("lastModified"));

    if (studyInstanceUID != null && StringUtils.isNotBlank(studyInstanceUID)) {
        detachedCriteria.add(Restrictions.eq("studyInstanceUID", studyInstanceUID));
    }/*from w  w  w.  j  a  v  a  2  s.c o m*/
    if (accessionNumber != null && StringUtils.isNotBlank(accessionNumber)) {
        detachedCriteria.add(Restrictions.eq("accessionNumber", accessionNumber));
    }
    if (accessionNumberIssuer != null && StringUtils.isNotBlank(accessionNumberIssuer)) {
        detachedCriteria.add(Restrictions.eq("issuerOfAccessionNumber", accessionNumberIssuer));
    }
    if (patientID != null && StringUtils.isNotBlank(patientID)) {
        detachedCriteria.add(Restrictions.eq("patientID", patientID));
    }
    if (patientIDIssuer != null && StringUtils.isNotBlank(patientIDIssuer)) {
        detachedCriteria.add(Restrictions.eq("issuerOfPatientID", patientIDIssuer));
    }
    if (minStudyDateTime != null) {
        detachedCriteria.add(Restrictions.ge("dateTime", minStudyDateTime));
    }
    if (minStudyDate != null) {
        detachedCriteria.add(Restrictions.ge("dateTime", minStudyDate));
    }
    if (maxStudyDateTime != null) {
        detachedCriteria.add(Restrictions.le("dateTime", maxStudyDateTime));
    }
    if (maxStudyDate != null) {
        detachedCriteria.add(Restrictions.lt("dateTime", maxStudyDate));
    }

    //Eliminate deleted studies from search results
    detachedCriteria.add(Restrictions.ne("studyVersion", -1));

    int firstResult = (offset - 1) * limit;
    final List<MINTStudy> list = (List<MINTStudy>) getHibernateTemplate().findByCriteria(detachedCriteria,
            firstResult, limit);
    return list;
}

From source file:org.openbp.server.persistence.hibernate.HibernatePersistenceContext.java

License:Apache License

/**
 * Returns a list of the objects of a particular type that match the given criterion.
 *
 * @param query Query to run//from w w  w  .ja  va 2s. c o m
 * @return The list or null
 * @throws PersistenceException On error
 */
public Collection runQuery(final PersistenceQuery query) throws PersistenceException {
    TransactionGuard tg = new TransactionGuard(this);
    try {
        Class cls = getMappedObjectClass(query.getObjectClass(), false);
        Criteria hc = getHibernateSession().createCriteria(cls);
        if (query.getMaxResults() > 0) {
            hc.setMaxResults(query.getMaxResults());
        }

        for (Iterator it = query.getOrderings(); it.hasNext();) {
            PersistenceOrdering ordering = (PersistenceOrdering) it.next();

            Order ho = ordering.isAscending() ? Order.asc(ordering.getPropertyName())
                    : Order.desc(ordering.getPropertyName());
            hc.addOrder(ho);
        }

        for (Iterator it = query.getCriterions(); it.hasNext();) {
            PersistenceCriterion criterion = (PersistenceCriterion) it.next();

            String property = criterion.getProperty();
            String operator = criterion.getOperator();
            Object value = criterion.getOperand();
            if (PersistenceCriterion.OPERATOR_EQ.equals(operator)) {
                hc = hc.add(Restrictions.eq(property, value));
            } else if (PersistenceCriterion.OPERATOR_EQ_OR_NULL.equals(operator)) {
                hc = hc.add(Restrictions.disjunction().add(Restrictions.isNull(property))
                        .add(Restrictions.eq(property, value)));
            } else if (PersistenceCriterion.OPERATOR_NEQ.equals(operator)) {
                hc = hc.add(Restrictions.ne(property, value));
            } else if (PersistenceCriterion.OPERATOR_GT.equals(operator)) {
                hc = hc.add(Restrictions.gt(property, value));
            } else if (PersistenceCriterion.OPERATOR_GTE.equals(operator)) {
                hc = hc.add(Restrictions.ge(property, value));
            } else if (PersistenceCriterion.OPERATOR_LT.equals(operator)) {
                hc = hc.add(Restrictions.lt(property, value));
            } else if (PersistenceCriterion.OPERATOR_LTE.equals(operator)) {
                hc = hc.add(Restrictions.le(property, value));
            } else if (PersistenceCriterion.OPERATOR_LIKE.equals(operator)) {
                hc = hc.add(Restrictions.gt(property, value));
            } else if (PersistenceCriterion.OPERATOR_NULL.equals(operator)) {
                hc = hc.add(Restrictions.isNull(property));
            } else if (PersistenceCriterion.OPERATOR_ALIAS.equals(operator)) {
                hc = hc.createAlias(property, (String) value);
            }
        }

        // Run query and wrap result list into a collection that calls onLoad for each element that is being accessed.
        Collection root = hc.list();
        return new DeferedOnLoadCollection(root, this);
    } catch (HibernateException e) {
        tg.doCatch();
        throw createLoggedException(e);
    } finally {
        tg.doFinally();
    }
}

From source file:org.openbravo.advpaymentmngt.dao.AdvPaymentMngtDao.java

License:Open Source License

/**
 * This method returns list of Payments that are in Awaiting Execution status and filtered by the
 * following parameters./*  w  w w  .  j  a  v a 2s . c  o  m*/
 * 
 * @param organizationId
 *          Organization
 * @param paymentMethodId
 *          Payment Method used for the payment.
 * @param financialAccountId
 *          Financial Account used for the payment.
 * @param dateFrom
 *          Optional. Filters payments made after the specified date.
 * @param dateTo
 *          Optional. Filters payments made before the specified date.
 * @param offset
 *          Starting register number.
 * @param pageSize
 *          Limited the max number of results.
 * @param strOrderByProperty
 *          Property used for ordering the results.
 * @param strAscDesc
 *          if true order by asc, if false order by desc
 * @param isReceipt
 *          if true sales, if false purchase
 * @return Filtered Payment list.
 */
public List<FIN_Payment> getPayExecRowCount(String organizationId, String paymentMethodId,
        String financialAccountId, Date dateFrom, Date dateTo, int offset, int pageSize,
        String strOrderByProperty, String strAscDesc, boolean isReceipt) {

    List<FIN_Payment> emptyList = new ArrayList<FIN_Payment>();
    if (organizationId == null || organizationId.isEmpty()) {
        return emptyList;
    }

    OBContext.setAdminMode();
    try {

        FIN_PaymentMethod obPayMethod = OBDal.getInstance().get(FIN_PaymentMethod.class, paymentMethodId);
        FIN_FinancialAccount obFinAccount = OBDal.getInstance().get(FIN_FinancialAccount.class,
                financialAccountId);

        OBCriteria<FIN_Payment> obcPayment = OBDal.getInstance().createCriteria(FIN_Payment.class);
        obcPayment.add(Restrictions.in("organization.id", OBContext.getOBContext()
                .getOrganizationStructureProvider().getParentTree(organizationId, true)));
        obcPayment.add(Restrictions.eq(FIN_Payment.PROPERTY_STATUS, "RPAE"));
        obcPayment.add(Restrictions.eq(FIN_Payment.PROPERTY_PAYMENTMETHOD, obPayMethod));
        obcPayment.add(Restrictions.eq(FIN_Payment.PROPERTY_ACCOUNT, obFinAccount));
        obcPayment.add(Restrictions.eq(FIN_Payment.PROPERTY_RECEIPT, isReceipt));
        if (dateFrom != null)
            obcPayment.add(Restrictions.ge(FIN_Payment.PROPERTY_PAYMENTDATE, dateFrom));
        if (dateTo != null)
            obcPayment.add(Restrictions.lt(FIN_Payment.PROPERTY_PAYMENTDATE, dateTo));

        boolean ascDesc = true;
        if (strAscDesc != null && !strAscDesc.isEmpty())
            ascDesc = "asc".equalsIgnoreCase(strAscDesc);
        if (strOrderByProperty != null && !strOrderByProperty.isEmpty())
            obcPayment.addOrderBy(strOrderByProperty, ascDesc);
        obcPayment.setFirstResult(offset);
        obcPayment.setMaxResults(pageSize);

        return obcPayment.list();

    } finally {
        OBContext.restorePreviousMode();
    }
}

From source file:org.openbravo.advpaymentmngt.dao.MatchTransactionDao.java

License:Open Source License

private static FIN_Reconciliation getPreviousReconciliation(FIN_Reconciliation reconciliation) {
    OBContext.setAdminMode(false);/* www .  ja v  a 2  s.  co m*/
    try {
        final OBCriteria<FIN_Reconciliation> obc = OBDal.getInstance().createCriteria(FIN_Reconciliation.class);
        obc.add(Restrictions.le(FIN_Reconciliation.PROPERTY_ENDINGDATE, reconciliation.getEndingDate()));
        obc.add(Restrictions.lt(FIN_Reconciliation.PROPERTY_CREATIONDATE, reconciliation.getCreationDate()));
        obc.add(Restrictions.eq(FIN_Reconciliation.PROPERTY_ACCOUNT, reconciliation.getAccount()));
        obc.addOrder(Order.desc(FIN_Reconciliation.PROPERTY_ENDINGDATE));
        obc.addOrder(Order.desc(FIN_Reconciliation.PROPERTY_CREATIONDATE));
        obc.setMaxResults(1);
        return (FIN_Reconciliation) obc.uniqueResult();
    } finally {
        OBContext.restorePreviousMode();
    }
}

From source file:org.openbravo.advpaymentmngt.utility.FIN_Utility.java

License:Open Source License

/**
 * Generic OBCriteria./*from ww  w  .j a  v a  2 s.  c  o  m*/
 * 
 * @param clazz
 *          Class (entity).
 * @param setFilterClient
 *          If true then only objects from readable clients are returned, if false then objects
 *          from all clients are returned
 * @param setFilterOrg
 *          If true then when querying (for example call list()) a filter on readable
 *          organizations is added to the query, if false then this is not done
 * @param values
 *          Value. Property, value and operator.
 * @return All the records that satisfy the conditions.
 */
public static <T extends BaseOBObject> List<T> getAllInstances(Class<T> clazz, boolean setFilterClient,
        boolean setFilterOrg, Value... values) {
    OBCriteria<T> obc = OBDal.getInstance().createCriteria(clazz);
    obc.setFilterOnReadableClients(setFilterClient);
    obc.setFilterOnReadableOrganization(setFilterOrg);
    for (Value value : values) {
        if (value.getValue() == null && "==".equals(value.getOperator())) {
            obc.add(Restrictions.isNull(value.getField()));
        } else if (value.getValue() == null && "!=".equals(value.getOperator())) {
            obc.add(Restrictions.isNotNull(value.getField()));
        } else if ("==".equals(value.getOperator())) {
            obc.add(Restrictions.eq(value.getField(), value.getValue()));
        } else if ("!=".equals(value.getOperator())) {
            obc.add(Restrictions.ne(value.getField(), value.getValue()));
        } else if ("<".equals(value.getOperator())) {
            obc.add(Restrictions.lt(value.getField(), value.getValue()));
        } else if (">".equals(value.getOperator())) {
            obc.add(Restrictions.gt(value.getField(), value.getValue()));
        } else if ("<=".equals(value.getOperator())) {
            obc.add(Restrictions.le(value.getField(), value.getValue()));
        } else if (">=".equals(value.getOperator())) {
            obc.add(Restrictions.ge(value.getField(), value.getValue()));
        } else {
            obc.add(Restrictions.eq(value.getField(), value.getValue()));
        }
    }
    return obc.list();
}