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.dspace.harvest.dao.impl.HarvestedCollectionDAOImpl.java

License:BSD License

@Override
public List<HarvestedCollection> findByLastHarvestedAndHarvestTypeAndHarvestStatusesAndHarvestTime(
        Context context, Date startTime, int minimalType, int[] statuses, int expirationStatus,
        Date expirationTime) throws SQLException {
    //      Old query: "SELECT * FROM harvested_collection WHERE
    // (last_harvested < ? or last_harvested is null) and harvest_type > ? and (harvest_status = ? or harvest_status = ? or (harvest_status=? and harvest_start_time < ?)) ORDER BY last_harvested",
    //                new java.sql.Timestamp(startTime.getTime()), 0, HarvestedCollection.STATUS_READY, HarvestedCollection.STATUS_OAI_ERROR, HarvestedCollection.STATUS_BUSY, new java.sql.Timestamp(expirationTime.getTime()));
    Criteria criteria = createCriteria(context, HarvestedCollection.class);
    LogicalExpression lastHarvestedRestriction = Restrictions.or(Restrictions.lt("lastHarvested", startTime),
            Restrictions.isNull("lastHarvested"));
    Disjunction statusRestriction = Restrictions.or();
    for (int status : statuses) {
        statusRestriction.add(Restrictions.eq("harvestStatus", status));
    }//  w  w w . j  a v a  2 s. com
    statusRestriction.add(Restrictions.and(Restrictions.eq("harvestStatus", expirationStatus),
            Restrictions.gt("harvestStartTime", expirationTime)));

    criteria.add(Restrictions.and(lastHarvestedRestriction, Restrictions.gt("harvestType", minimalType),
            statusRestriction

    ));
    criteria.addOrder(Order.asc("lastHarvested"));
    return list(criteria);

}

From source file:org.dspace.harvest.dao.impl.HarvestedCollectionDAOImpl.java

License:BSD License

protected Criteria getByStatusAndMinimalTypeCriteria(Context context, int status, int type, int limit)
        throws SQLException {
    Criteria criteria = createCriteria(context, HarvestedCollection.class);
    criteria.add(//from   w  w w . j ava2s  .  c  om
            Restrictions.and(Restrictions.gt("harvestType", type), Restrictions.eq("harvestStatus", status)));
    if (limit != -1) {
        criteria.setMaxResults(1);
    }
    return criteria;
}

From source file:org.egov.egf.web.actions.masters.JQueryGridActionSupport.java

License:Open Source License

/**
 * Used to convert jqgrid search operator to hibernate restriction.
 **//*from w w  w . j a va  2 s. com*/
private Criterion applyRestriction() {
    final Object convertedValue = convertValueType(searchField, searchString);

    if ("eq".equals(searchOper))
        return Restrictions.eq(searchField, convertedValue);
    else if ("ne".equals(searchOper))
        return Restrictions.ne(searchField, convertedValue);

    if (convertedValue instanceof String) {
        if ("bw".equals(searchOper))
            return Restrictions.ilike(searchField, searchString + "%");
        else if ("cn".equals(searchOper))
            return Restrictions.ilike(searchField, "%" + searchString + "%");
        else if ("ew".equals(searchOper))
            return Restrictions.ilike(searchField, "%" + searchString);
        else if ("bn".equals(searchOper))
            return Restrictions.not(Restrictions.ilike(searchField, searchString + "%"));
        else if ("en".equals(searchOper))
            return Restrictions.not(Restrictions.ilike(searchField, "%" + searchString));
        else if ("nc".equals(searchOper))
            return Restrictions.not(Restrictions.ilike(searchField, "%" + searchString + "%"));
        else if ("in".equals(searchOper))
            return Restrictions.in(searchField, searchString.split(","));
        else if ("ni".equals(searchOper))
            return Restrictions.not(Restrictions.in(searchField, searchString.split(",")));
    } else if ("lt".equals(searchOper))
        return Restrictions.lt(searchField, convertedValue);
    else if ("le".equals(searchOper))
        return Restrictions.le(searchField, convertedValue);
    else if ("gt".equals(searchOper))
        return Restrictions.gt(searchField, convertedValue);
    else if ("ge".equals(searchOper))
        return Restrictions.ge(searchField, convertedValue);
    return null;
}

From source file:org.egov.ptis.domain.dao.property.TaxPercHibernateDAO.java

License:Open Source License

@Override
public TaxPerc getTaxPerc(Category category, PropertyUsage propertyUsage, BigDecimal amount, Date date) {
    LOGGER.info("getTaxPerc invoked");
    TaxPerc taxPerc = null;/*  w  w  w  . ja  v a 2s. co m*/
    Criteria crit = getCurrentSession().createCriteria(TaxPerc.class);
    if (category != null) {
        crit.add(Restrictions.eq("category", category));
    }
    if (propertyUsage != null) {
        crit.add(Restrictions.eq("propertyUsage", propertyUsage));
    }
    if (amount != null) {
        crit.add(Restrictions.le("fromAmt", amount));
        crit.add(Restrictions.ge("toAmt", amount));
    }
    if (date != null) {
        crit.add(Restrictions.lt("fromDate", date));
        crit.add(Restrictions.gt("toDate", date));
    }
    if (crit.list().size() == 1) {
        taxPerc = (TaxPerc) crit.uniqueResult();
    }
    return taxPerc;
}

From source file:org.encuestame.persistence.dao.imp.FrontEndDao.java

License:Apache License

/**
 * Get TweetPoll Front End.//from ww  w .  j  a  v a 2 s  .  c o m
 * @param period date period
 * @param firstResult first results
 * @param maxResults max results
 * @return list of tweetPoll.
 */
@SuppressWarnings("unchecked")
public final List<TweetPoll> getTweetPollFrontEnd(final SearchPeriods period, final Integer start,
        final Integer maxResults, final Integer firstResult) {
    final DetachedCriteria criteria = DetachedCriteria.forClass(TweetPoll.class);
    criteria.createAlias("question", "question");
    calculateSearchPeriodsDates(period, criteria, "createDate");
    criteria.add(Restrictions.eq("publishTweetPoll", Boolean.TRUE)); //should be published
    criteria.add(Restrictions.gt("relevance", 0L));
    criteria.addOrder(Order.desc("relevance"));
    criteria.addOrder(Order.desc("createDate"));
    return (List<TweetPoll>) filterByMaxorStart(criteria, maxResults, start);
    //return getHibernateTemplate().findByCriteria(criteria, firstResult, maxResults);
}

From source file:org.encuestame.persistence.dao.imp.FrontEndDao.java

License:Apache License

/**
 * Get Poll on Front End//from   www .  ja v  a 2  s  .  c  o m
 * @param period period
 * @param maxResults maxResults
 * @param firstResult firstResults.
 * @return list of poll.
 */
@SuppressWarnings("unchecked")
public final List<Poll> getPollFrontEnd(final SearchPeriods period, final Integer start,
        final Integer maxResults, final Integer firstResult) {
    final DetachedCriteria criteria = DetachedCriteria.forClass(Poll.class);
    criteria.createAlias("question", "question");
    calculateSearchPeriodsDates(period, criteria, "createDate");
    criteria.add(Restrictions.gt("relevance", 0L));
    criteria.add(Restrictions.eq("publish", Boolean.TRUE)); //should be published
    criteria.add(Restrictions.or(Restrictions.eq("isHidden", Boolean.FALSE), Restrictions.isNull("isHidden")));
    criteria.add(Restrictions.isNotNull("isPasswordProtected"));
    criteria.add(Restrictions.isNull("passProtection"));
    //criteria.add(Restrictions.isEmpty("passProtection"));
    criteria.addOrder(Order.desc("createDate"));
    criteria.addOrder(Order.desc("relevance"));
    List<Poll> polls = (List<Poll>) filterByMaxorStart(criteria, maxResults, start);
    return polls;
}

From source file:org.encuestame.persistence.dao.imp.FrontEndDao.java

License:Apache License

@SuppressWarnings("unchecked")
public final List<Survey> getSurveyFrontEnd(final SearchPeriods period, final Integer start,
        final Integer maxResults, final Integer firstResult) {
    final DetachedCriteria criteria = DetachedCriteria.forClass(Survey.class);
    //criteria.createAlias("question", "question");
    // TODO: Complete method, adding criteria params

    calculateSearchPeriodsDates(period, criteria, "createDate");
    criteria.add(Restrictions.gt("relevance", 0L));
    criteria.addOrder(Order.desc("relevance"));
    criteria.addOrder(Order.desc("createDate"));
    return (List<Survey>) filterByMaxorStart(criteria, maxResults, start);
}

From source file:org.encuestame.persistence.dao.imp.HashTagDao.java

License:Apache License

@SuppressWarnings("unchecked")
public List<HashTag> getHashTags(final Integer maxResults, final Integer start, final String tagCriteria) {
    final DetachedCriteria criteria = DetachedCriteria.forClass(HashTag.class);
    // TODO: please replace "hashTagsCloud" by ENUM.
    if (tagCriteria.equals("hashTagsCloud")) {
        criteria.add(Restrictions.gt("size", MIN_SIZE_CLOUD));
        // TODO: date?
        // criteria.add(Restrictions.gl("updatedDate",
        // getCurrentdMidnightDate()));
        criteria.addOrder(Order.desc("size"));
        criteria.addOrder(Order.asc("hashTag"));
    } else {/*from  ww w .  j a  v a2s . c om*/
        criteria.addOrder(Order.desc("hits"));
        criteria.addOrder(Order.asc("hashTag"));
    }
    return (List<HashTag>) filterByMaxorStart(criteria, maxResults, start);
}

From source file:org.eulerframework.web.core.base.dao.impl.hibernate5.BaseDao.java

License:Apache License

protected Criterion generateRestriction(String property, String value, QueryMode queryMode) {
    Field field;/*from   w ww . j a v a  2s. c  o m*/
    try {
        String fieldName = property;
        if (property.indexOf('.') > 0) {
            fieldName = property.substring(0, property.indexOf('.'));
        }
        field = this.entityClass.getDeclaredField(fieldName);
    } catch (NoSuchFieldException e) {
        throw new IllegalArgumentException("Property '" + property + "' not exist");
    }

    if (BaseEmbeddable.class.isAssignableFrom(field.getType())) {
        try {
            field = field.getType().getDeclaredField(property.substring(property.indexOf('.') + 1));
        } catch (NoSuchFieldException e) {
            throw new IllegalArgumentException("Property '" + property + "' not exist");
        }
    }

    switch (queryMode) {
    case ANYWHERE:
        return RestrictionsX.like(property, value, MatchMode.ANYWHERE);
    case START:
        return RestrictionsX.like(property, value, MatchMode.START);
    case END:
        return RestrictionsX.like(property, value, MatchMode.END);
    case EXACT:
        return RestrictionsX.like(property, value, MatchMode.EXACT);
    case GE:
        return Restrictions.ge(property, JavaObjectUtils.analyzeStringValueToObject(value, field.getType()));
    case GT:
        return Restrictions.gt(property, JavaObjectUtils.analyzeStringValueToObject(value, field.getType()));
    case LE:
        return Restrictions.le(property, JavaObjectUtils.analyzeStringValueToObject(value, field.getType()));
    case LT:
        return Restrictions.lt(property, JavaObjectUtils.analyzeStringValueToObject(value, field.getType()));
    case IN:
        return Restrictions.in(property, this.analyzeInterval(value, field.getType()));
    case NOTIN:
        return Restrictions.not(RestrictionsX.in(property, this.analyzeInterval(value, field.getType())));
    case IS:
        return Restrictions.eq(property, JavaObjectUtils.analyzeStringValueToObject(value, field.getType()));
    case NOT:
        return Restrictions.ne(property, JavaObjectUtils.analyzeStringValueToObject(value, field.getType()));
    case BETWEEN:
        Object[] array1 = this.analyzeInterval(value, field.getType());
        return Restrictions.between(property, array1[0], array1[1]);
    case OUTSIDE:
        Object[] array2 = this.analyzeInterval(value, field.getType());
        return Restrictions.not(Restrictions.between(property, array2[0], array2[1]));
    default:
        throw new IllegalArgumentException("Unknown query mode: " + queryMode);
    }
}

From source file:org.faster.orm.criteria.GenericCriteria.java

License:Open Source License

public DetachedCriteria buildCriteria() {
    beforeBuildCriteria();//from   w  ww .j  a  va2  s.c  om
    DetachedCriteria dc = DetachedCriteria.forClass(persistClass);
    List<Field> queryFields = QueryHelper.getQueryFields(this.getClass());
    Set<String> aliases = new HashSet<String>(); // ??
    for (Field f : queryFields) {
        try {
            Object obj = f.get(this);
            boolean ignoreNull = QueryHelper.isIgnoreNull(f);
            if (obj == null) {
                if (ignoreNull) {
                    continue;
                }
            }

            String fieldName = QueryHelper.getFieldName(f);
            if (fieldName.contains(".")) {
                String[] subFieldNames = fieldName.split("\\.");
                String fieldPath = subFieldNames[0];
                String alias = subFieldNames[0];
                if (!aliases.contains(alias)) {
                    dc.createAlias(fieldPath, alias);
                    aliases.add(alias);
                }

                // ?alias
                for (int i = 1; i < subFieldNames.length - 1; i++) {
                    fieldPath += "." + subFieldNames[i];
                    alias += "_" + subFieldNames[i];
                    if (!aliases.contains(alias)) {
                        dc.createAlias(fieldPath, alias);
                        aliases.add(alias);
                    }
                }

                if (subFieldNames.length > 2) {
                    fieldName = alias + "." + subFieldNames[subFieldNames.length - 1];
                }
            }

            if (obj == null && !ignoreNull) {
                dc.add(Restrictions.isNull(fieldName));
                continue;
            }

            String stringValue = String.valueOf(obj);
            boolean ignoreEmptyOrZero = QueryHelper.isIgnoreEmptyOrZero(f);
            if (ignoreEmptyOrZero && isBlank(stringValue)) {
                continue;
            }

            if (stringValue.startsWith(NULL)) {
                dc.add(Restrictions.isNull(fieldName));
                continue;
            }

            if (stringValue.startsWith(NOT_NULL)) {
                dc.add(Restrictions.isNotNull(fieldName));
                continue;
            }

            String delimiter = QueryHelper.getDelimiter(f);
            DataType dt = QueryHelper.getDataType(f);
            MatchMode mm = QueryHelper.getMatchMode(f);

            switch (dt) {
            case STRING:
                switch (mm) {
                case EQ:
                    dc.add(Restrictions.eq(fieldName, stringValue));
                    continue;
                case LIKE:
                    dc.add(Restrictions.like(fieldName, stringValue,
                            org.hibernate.criterion.MatchMode.ANYWHERE));
                    continue;
                case LIKE_START:
                    dc.add(Restrictions.like(fieldName, stringValue, org.hibernate.criterion.MatchMode.START));
                    continue;
                case LIKE_END:
                    dc.add(Restrictions.like(fieldName, stringValue, org.hibernate.criterion.MatchMode.END));
                    continue;
                case ILIKE:
                    dc.add(Restrictions.ilike(fieldName, stringValue,
                            org.hibernate.criterion.MatchMode.ANYWHERE));
                    continue;
                case ILIKE_START:
                    dc.add(Restrictions.ilike(fieldName, stringValue, org.hibernate.criterion.MatchMode.START));
                    continue;
                case ILIKE_END:
                    dc.add(Restrictions.ilike(fieldName, stringValue, org.hibernate.criterion.MatchMode.END));
                    continue;
                case IN:
                    CriteriaRender.renderFieldByStringFilterValues(dc, fieldName, stringValue, delimiter);
                    continue;
                default:
                    throw new IllegalArgumentException("Invalid MatchMode for String: " + mm);
                }
            case INTEGER:
                if (ignoreEmptyOrZero && stringValue.equals("0")) {
                    continue;
                }

                Integer intValue = 0;
                if (mm != MatchMode.IN) {
                    try {
                        intValue = Integer.valueOf(stringValue);
                    } catch (Exception e) {
                        throw new IllegalArgumentException(
                                stringValue + " is not valid int value and can't use MatchMode: " + mm);
                    }
                }

                switch (mm) {
                case EQ:
                    dc.add(Restrictions.eq(fieldName, intValue));
                    continue;
                case NE:
                    dc.add(Restrictions.ne(fieldName, intValue));
                    continue;
                case IN:
                    CriteriaRender.renderFieldByIntegerFilterValues(dc, fieldName, stringValue, delimiter);
                    continue;
                case GT:
                    dc.add(Restrictions.gt(fieldName, intValue));
                    continue;
                case GE:
                    dc.add(Restrictions.ge(fieldName, intValue));
                    continue;
                case LT:
                    dc.add(Restrictions.lt(fieldName, intValue));
                    continue;
                case LE:
                    dc.add(Restrictions.le(fieldName, intValue));
                    continue;
                default:
                    throw new IllegalArgumentException("Invalid MatchMode for Long: " + mm);
                }
            case LONG:
                if (ignoreEmptyOrZero && stringValue.equals("0")) {
                    continue;
                }

                Long longValue = 0L;
                if (mm != MatchMode.IN) {
                    try {
                        longValue = Long.valueOf(stringValue);
                    } catch (Exception e) {
                        throw new IllegalArgumentException(
                                stringValue + " is not valid long value and can't use MatchMode: " + mm);
                    }
                }

                switch (mm) {
                case EQ:
                    dc.add(Restrictions.eq(fieldName, longValue));
                    continue;
                case NE:
                    dc.add(Restrictions.ne(fieldName, longValue));
                    continue;
                case IN:
                    CriteriaRender.renderFieldByLongFilterValues(dc, fieldName, stringValue, delimiter);
                    continue;
                case GT:
                    dc.add(Restrictions.gt(fieldName, longValue));
                    continue;
                case GE:
                    dc.add(Restrictions.ge(fieldName, longValue));
                    continue;
                case LT:
                    dc.add(Restrictions.lt(fieldName, longValue));
                    continue;
                case LE:
                    dc.add(Restrictions.le(fieldName, longValue));
                    continue;
                default:
                    throw new IllegalArgumentException("Invalid MatchMode for Long: " + mm);
                }
            case DATE:
                // TODO ??????
                Date date = DateTimes.parseStringToDate(stringValue);
                switch (mm) {
                case EQ:
                    dc.add(Restrictions.eq(fieldName, date));
                    continue;
                case NE:
                    dc.add(Restrictions.ne(fieldName, date));
                    continue;
                case GT:
                    dc.add(Restrictions.gt(fieldName, date));
                    continue;
                case GE:
                    dc.add(Restrictions.ge(fieldName, date));
                    continue;
                case LT:
                    dc.add(Restrictions.lt(fieldName, date));
                    continue;
                case LE:
                    dc.add(Restrictions.le(fieldName, date));
                    continue;
                default:
                    throw new IllegalArgumentException("Invalid MatchMode for Date: " + mm);
                }
            case BOOLEAN:
                Boolean bool = BooleanUtils.toBooleanObject(stringValue);
                switch (mm) {
                case EQ:
                    dc.add(Restrictions.eq(fieldName, bool));
                    continue;
                case NE:
                    dc.add(Restrictions.ne(fieldName, bool));
                    continue;
                default:
                    throw new IllegalArgumentException("Invalid MatchMode for Boolean: " + mm);
                }
            case LatLong:
                LatLngBound b = new LatLngBound(stringValue);
                if (b.isValid()) {
                    String minLatitude = b.getMinLatitude() == null ? "0.0" : b.getMinLatitude().toString();
                    String maxLatitude = b.getMaxLatitude() == null ? "0.0" : b.getMaxLatitude().toString();
                    String minLongitude = b.getMinLongitude() == null ? "0.0" : b.getMinLongitude().toString();
                    String maxLongitude = b.getMaxLongitude() == null ? "0.0" : b.getMaxLongitude().toString();
                    if ("area".equalsIgnoreCase(fieldName)) {
                        dc.add(Restrictions.between("latitude", minLatitude, maxLatitude));
                        dc.add(Restrictions.between("longitude", minLongitude, maxLongitude));
                    } else {
                        if (fieldName.contains(".")) {
                            String alias = fieldName.replace(".", "_");
                            if (!aliases.contains(alias)) {
                                dc.createAlias(fieldName, alias);
                                aliases.add(alias);
                            }
                            fieldName = alias;
                        } else {
                            if (!aliases.contains(fieldName)) {
                                dc.createAlias(fieldName, fieldName);
                                aliases.add(fieldName);
                            }
                        }
                        dc.add(Restrictions.between(fieldName + ".latitude", minLatitude, maxLatitude));
                        dc.add(Restrictions.between(fieldName + ".longitude", minLongitude, maxLongitude));
                    }
                }
                continue;
            default:
                throw new IllegalArgumentException("Unsupported DataType: " + dt);
            }
        } catch (Exception e) {
            throw new IllegalArgumentException(e);
        }
    }

    renderCriteria(dc);

    if (isNotBlank(sort)) {
        String[] fields = sort.split("-");
        if (fields.length < 2 || "desc".equalsIgnoreCase(fields[1])) {
            dc.addOrder(Order.desc(fields[0]));
        } else {
            dc.addOrder(Order.asc(fields[0]));
        }
    }

    return dc;
}