Example usage for org.hibernate.criterion Restrictions ge

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

Introduction

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

Prototype

public static SimpleExpression ge(String propertyName, Object value) 

Source Link

Document

Apply a "greater than or equal" constraint to the named property

Usage

From source file:com.zutubi.pulse.master.model.persistence.hibernate.HibernateBuildResultDao.java

License:Apache License

private void addNumbersToCriteria(long lowestNumber, long highestNumber, Criteria criteria) {
    if (lowestNumber > 0) {
        criteria.add(Restrictions.ge("number", lowestNumber));
    }//  w  ww.j  a  v a2 s . c  o  m

    if (highestNumber > 0) {
        criteria.add(Restrictions.le("number", highestNumber));
    }
}

From source file:cz.jirutka.rsql.hibernate.AbstractCriterionBuilder.java

License:Open Source License

/**
 * Apply a "greater than or equal" constraint to the named property.
 * //ww w  .  ja v a  2  s.com
 * @param propertyPath property name prefixed with an association alias
 * @param argument value
 * @return Criterion
 */
protected Criterion createGreaterEqual(String propertyPath, Object argument) {
    return Restrictions.ge(propertyPath, argument);
}

From source file:cz.jirutka.rsql.hibernate.AbstractCriterionBuilderTest.java

License:Open Source License

@Test
public void testCreateCriterion3args() {
    String property = "foo";
    Criterion exptected;/*from ww  w.j  a v a  2  s  .  co m*/
    Criterion actual;

    exptected = Restrictions.eq(property, "bar");
    actual = instance.createCriterion(property, Comparison.EQUAL, "bar");
    assertEquals(exptected.toString(), actual.toString());

    exptected = Restrictions.ilike(property, "bar%");
    actual = instance.createCriterion(property, Comparison.EQUAL, "bar*");
    assertEquals(exptected.toString(), actual.toString());

    exptected = Restrictions.ne(property, "bar");
    actual = instance.createCriterion(property, Comparison.NOT_EQUAL, "bar");
    assertEquals(exptected.toString(), actual.toString());

    exptected = Restrictions.not(Restrictions.ilike(property, "%bar"));
    actual = instance.createCriterion(property, Comparison.NOT_EQUAL, "*bar");
    assertEquals(exptected.toString(), actual.toString());

    exptected = Restrictions.gt(property, 42);
    actual = instance.createCriterion(property, Comparison.GREATER_THAN, 42);
    assertEquals(exptected.toString(), actual.toString());

    exptected = Restrictions.ge(property, -42);
    actual = instance.createCriterion(property, Comparison.GREATER_EQUAL, -42);
    assertEquals(exptected.toString(), actual.toString());

    exptected = Restrictions.lt(property, 42.2);
    actual = instance.createCriterion(property, Comparison.LESS_THAN, 42.2);
    assertEquals(exptected.toString(), actual.toString());

    exptected = Restrictions.le(property, -42.2);
    actual = instance.createCriterion(property, Comparison.LESS_EQUAL, -42.2);
    assertEquals(exptected.toString(), actual.toString());

}

From source file:cz.jirutka.rsql.visitor.hibernate.HibernateCriterionVisitor.java

License:Apache License

@Override
public Criterion visit(ComparisonNode node) {
    String name = node.getSelector();
    /*//from w ww .ja  v  a 2s.  c  o  m
     Get the path and property names
     */
    Tuple<Type, String>[] path = getPath(root, name);
    Type type = getType(path);
    boolean isPrimitive = type instanceof StringRepresentableType;
    boolean isCustom = type instanceof CustomType;
    boolean isCollection = type instanceof CollectionType;

    String exp = getExpression(path, isCollection);
    List<Object> arguments = new ArrayList<Object>(node.getArguments().size());
    for (String argument : node.getArguments()) {
        Object value = argument;
        if (isPrimitive) {
            value = ((StringRepresentableType) type).fromStringValue((String) value);
        } else if (isCustom) {
            value = ((CustomType) type).stringToObject((String) value);
        } else if (isCollection) {
            value = IntegerType.INSTANCE.fromString((String) value);
        }
        if (value instanceof String) {
            value = toSqlWildcardString((String) value);
        }
        arguments.add(value);
    }
    ComparisonOperator operator = node.getOperator();

    assert arguments.size() >= 1;

    Object firstArgument = arguments.get(0);
    if (operator.equals(RSQLOperators.EQUAL)) {
        if (!isCollection) {
            if (String.class.isInstance(firstArgument) && ((String) firstArgument).contains("%")) {
                return Restrictions.ilike(exp, firstArgument);
            } else {
                return Restrictions.eq(exp, firstArgument);
            }
        } else {
            return Restrictions.sizeEq(exp, (Integer) firstArgument);
        }
    } else if (operator.equals(RSQLOperators.NOT_EQUAL)) {
        if (!isCollection) {
            if (String.class.isInstance(firstArgument) && ((String) firstArgument).contains("%")) {
                return Restrictions.not(Restrictions.ilike(exp, firstArgument));
            } else {
                return Restrictions.ne(exp, firstArgument);
            }
        } else {
            return Restrictions.sizeNe(exp, (Integer) firstArgument);
        }
    } else if (operator.equals(RSQLOperators.GREATER_THAN)) {
        if (!isCollection) {
            return Restrictions.gt(exp, firstArgument);
        } else {
            return Restrictions.sizeGt(exp, (Integer) firstArgument);
        }
    } else if (operator.equals(RSQLOperators.GREATER_THAN_OR_EQUAL)) {
        if (!isCollection) {
            return Restrictions.ge(exp, firstArgument);
        } else {
            return Restrictions.sizeGe(exp, (Integer) firstArgument);
        }
    } else if (operator.equals(RSQLOperators.LESS_THAN)) {
        if (!isCollection) {
            return Restrictions.lt(exp, firstArgument);
        } else {
            return Restrictions.sizeLt(exp, (Integer) firstArgument);
        }
    } else if (operator.equals(RSQLOperators.LESS_THAN_OR_EQUAL)) {
        if (!isCollection) {
            return Restrictions.le(exp, firstArgument);
        } else {
            return Restrictions.sizeLe(exp, (Integer) firstArgument);
        }
    } else if (operator.equals(RSQLOperators.IN)) {
        if (!isCollection) {
            return Restrictions.in(exp, arguments);
        }
    } else if (operator.equals(RSQLOperators.NOT_IN)) {
        if (!isCollection) {
            return Restrictions.not(Restrictions.in(exp, arguments));
        }
    }
    throw new IllegalArgumentException("Unknown operation " + operator.toString() + " for property" + name);
}

From source file:cz.zcu.kiv.eegdatabase.data.dao.SimpleReservationDao.java

License:Apache License

public int createChecked(Reservation newInstance) throws DaoException {

    if (newInstance.getStartTime().getTime() >= newInstance.getEndTime().getTime()) {
        throw new DaoException("Time of start cannot be lower than or equal to time of end!");
    }/*w w  w . j  a  v a2  s  . co  m*/
    Criteria overlapCriteria = getSession().createCriteria(Reservation.class);
    //start overlap
    LogicalExpression startOverlap = Restrictions.and(
            Restrictions.and(Restrictions.ge("startTime", newInstance.getStartTime()),
                    Restrictions.ge("endTime", newInstance.getEndTime())),
            Restrictions.le("startTime", newInstance.getEndTime()));
    //end overlap
    LogicalExpression endOverlap = Restrictions.and(
            Restrictions.and(Restrictions.le("startTime", newInstance.getStartTime()),
                    Restrictions.le("endTime", newInstance.getEndTime())),
            Restrictions.ge("endTime", newInstance.getStartTime()));
    //include overlap
    LogicalExpression inOverlap = Restrictions.and(Restrictions.le("startTime", newInstance.getStartTime()),
            Restrictions.ge("endTime", newInstance.getEndTime()));

    //complete overlap
    LogicalExpression completeOverlap = Restrictions.and(
            Restrictions.ge("startTime", newInstance.getStartTime()),
            Restrictions.le("endTime", newInstance.getEndTime()));

    overlapCriteria.add(Restrictions.or(Restrictions.or(inOverlap, completeOverlap),
            Restrictions.or(startOverlap, endOverlap)));

    //if some overlap was found, it is an error
    if (overlapCriteria.list().size() > 0) {
        throw new DaoException(
                "Reservation could not be created due to existing records within the time range.");
    }

    return super.create(newInstance);
}

From source file:dao.CondominioDAO.java

public static List<Condominio> findByFiltros(Integer idMorador, Date dtInicial, Date dtFinal, boolean emAberto)
        throws Exception {
    SessionFactory sf = HibernateUtil.getSessionFactory();
    Session session = sf.openSession();/*from w w w .  j  a  v  a  2 s  .c  o m*/
    Criteria crit = session.createCriteria(Condominio.class);
    Criteria subCrit = crit.createCriteria("pagamento", "pagamento", JoinType.INNER_JOIN);
    Criteria subCrit2 = crit.createCriteria("morador", "morador", JoinType.INNER_JOIN);

    if (emAberto) {
        subCrit.add(Restrictions.isNull("data"));
        crit.add(Restrictions.lt("data", new Date()));
    }
    if (idMorador != null && idMorador > 0) {
        subCrit2.add(Restrictions.eq("id", idMorador));
    }
    if (dtInicial != null) {
        crit.add(Restrictions.ge("data", dtInicial));
    }
    if (dtFinal != null) {
        crit.add(Restrictions.le("data", dtFinal));
    }

    List<Condominio> list = crit.list();
    session.flush();
    session.close();
    return list;
}

From source file:Dao.MovieDAO.java

public List<Movie> getOldMovies() {
    Session session = this.sessionFactory.getCurrentSession();
    Calendar cal2000 = Calendar.getInstance();
    cal2000.add(Calendar.DATE, -6330);
    Calendar cal2016 = Calendar.getInstance();
    cal2016.add(Calendar.DATE, -486);

    Date date = cal2000.getTime();
    Date date2 = cal2016.getTime();

    Timestamp year2000 = new Timestamp(date.getTime());
    Timestamp year2016 = new Timestamp(date2.getTime());

    List movies = session.createCriteria(Movie.class)
            .add(Restrictions.between("releaseDate", year2000, year2016))
            .add(Restrictions.ge("movieScore", 7.5)).list();

    if (movies.isEmpty()) {
        return null;
    }/*from   ww  w.  j  a v  a 2  s .co m*/
    logger.info("Old Movies successfully, Movie details=" + movies);
    return movies;
}

From source file:Dao.MovieDAO.java

public List<Movie> getMoviesTopRated() {
    Session session = this.sessionFactory.getCurrentSession();
    Calendar cal2016 = Calendar.getInstance();
    cal2016.add(Calendar.DATE, -486);
    Date date = cal2016.getTime();
    Timestamp year2016 = new Timestamp(date.getTime());
    Timestamp today = new Timestamp(System.currentTimeMillis());

    List movies = session.createCriteria(Movie.class).add(Restrictions.between("releaseDate", year2016, today))
            .add(Restrictions.ge("movieScore", 7.5)).list();

    if (movies.isEmpty()) {
        return null;
    }/*  www.j  a v  a 2s .  c o m*/
    logger.info("Top Rated successfully, Movie details=" + movies);
    return movies;
}

From source file:Dao.ShowingDAO.java

public List<Showing> getShowingByTimestamp() {

    Timestamp today = new Timestamp(System.currentTimeMillis());
    Session session = this.sessionFactory.getCurrentSession();
    List showings = session.createCriteria(Showing.class).add(Restrictions.ge("time", today)).list();
    if (showings.isEmpty()) {
        return null;
    } else {/*from w  w w . j  a v  a2  s  . c om*/
        return showings;
    }
}

From source file:Dao.ShowingDAO.java

public List<Showing> getShowingByMovieAndTheatreAndTime(Movie movie, Theatre theatre, Timestamp date) {
    Session session = this.sessionFactory.getCurrentSession();

    Calendar cal = Calendar.getInstance();
    cal.setTime(date);//  w w  w. j av a 2  s  .com
    cal.add(Calendar.DAY_OF_WEEK, 1);
    Timestamp tomorrow = new Timestamp(cal.getTime().getTime());

    List showings = session.createCriteria(Showing.class).add(Restrictions.eq("movie", movie))
            .add(Restrictions.eq("theatre", theatre)).add(Restrictions.ge("time", date))
            .add(Restrictions.le("time", tomorrow)).list();
    if (showings.isEmpty()) {
        return null;
    } else {
        return showings;
    }
}