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:net.purnama.pureff.dao.ReturnPurchaseDao.java

public List getReturnPurchaseList(Calendar begin, Calendar end, WarehouseEntity warehouse,
        PartnerEntity partner, CurrencyEntity currency, boolean status) {
    Session session = this.sessionFactory.getCurrentSession();
    Criteria c = session.createCriteria(ReturnPurchaseEntity.class);
    c.add(Restrictions.between("date", begin, end));
    if (partner != null) {
        c.add(Restrictions.eq("partner", partner));
    }//from w w  w.ja  v  a2 s  .c o m
    if (warehouse != null) {
        c.add(Restrictions.eq("warehouse", warehouse));
    }
    if (currency != null) {
        c.add(Restrictions.eq("currency", currency));
    }
    c.add(Restrictions.eq("status", status));
    c.addOrder(Order.asc("date"));
    c.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
    List ls = c.list();
    return ls;
}

From source file:net.purnama.pureff.dao.ReturnSalesDao.java

public List getReturnSalesList(Calendar begin, Calendar end, WarehouseEntity warehouse, PartnerEntity partner,
        CurrencyEntity currency, boolean status) {
    Session session = this.sessionFactory.getCurrentSession();
    Criteria c = session.createCriteria(ReturnSalesEntity.class);
    c.add(Restrictions.between("date", begin, end));
    if (partner != null) {
        c.add(Restrictions.eq("partner", partner));
    }/*from  www . j a  v a2s. c om*/
    if (warehouse != null) {
        c.add(Restrictions.eq("warehouse", warehouse));
    }
    if (currency != null) {
        c.add(Restrictions.eq("currency", currency));
    }
    c.add(Restrictions.eq("status", status));
    c.addOrder(Order.asc("date"));
    c.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
    List ls = c.list();
    return ls;
}

From source file:net.startbahn.web.app.WorksAction.java

public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest req,
        HttpServletResponse res) throws Exception {

    Session session = new HibernateSession().currentSession(this.getServlet().getServletContext());

    Vector vector = new Vector();
    Criteria criteria = session.createCriteria(Work.class);

    if (StringUtils.isNotBlank(req.getParameter("artist"))) {
        Criteria criteria2 = session.createCriteria(Artist.class);
        criteria2.add(Restrictions.idEq(Integer.valueOf(req.getParameter("artist"))));
        Artist artist = (Artist) criteria2.uniqueResult();
        criteria.add(Restrictions.eq("artist", artist));
    }/*from w w  w  . j  av  a 2s . c  om*/

    if (StringUtils.isNotBlank(req.getParameter("client"))) {
        if (req.getParameter("client").equals("true")) {
            criteria.add(Restrictions.eq("client", true));
        } else {
            criteria.add(Restrictions.eq("client", false));
        }
    }

    if (StringUtils.isNotBlank(req.getParameter("licensed"))) {
        if (req.getParameter("licensed").equals("true")) {
            criteria.add(Restrictions.eq("licensed", true));
        } else {
            criteria.add(Restrictions.eq("licensed", false));
        }
    }

    if (StringUtils.isNotBlank(req.getParameter("licensingpossibleafterexpired"))) {
        if (req.getParameter("licensingpossibleafterexpired").equals("true")) {
            criteria.add(Restrictions.eq("licensingpossibleafterexpired", true));
        } else {
            criteria.add(Restrictions.eq("licensingpossibleafterexpired", false));
        }
    }

    if (StringUtils.isNotBlank(req.getParameter("updatedatestartdate"))
            && StringUtils.isNotBlank(req.getParameter("updatedateenddate"))) {
        Date startDate = (new SimpleDateFormat("yyyy/MM/dd")).parse(req.getParameter("updatedatestartdate"));
        Date endDate = (new SimpleDateFormat("yyyy/MM/dd")).parse(req.getParameter("updatedateenddate"));
        criteria.add(Restrictions.between("date", startDate, endDate));
    }

    if (StringUtils.isNotBlank(req.getParameter("productiondatestartdate"))
            && StringUtils.isNotBlank(req.getParameter("productiondateenddate"))) {
        Date startDate = (new SimpleDateFormat("yyyy/MM/dd"))
                .parse(req.getParameter("productiondatestartdate"));
        Date endDate = (new SimpleDateFormat("yyyy/MM/dd")).parse(req.getParameter("productiondateenddate"));
        criteria.add(Restrictions.between("date", startDate, endDate));
    }

    if (StringUtils.isNotBlank(req.getParameter("datestartdate"))
            && StringUtils.isNotBlank(req.getParameter("dateenddate"))) {
        Date startDate = (new SimpleDateFormat("yyyy/MM/dd")).parse(req.getParameter("datestartdate"));
        Date endDate = (new SimpleDateFormat("yyyy/MM/dd")).parse(req.getParameter("dateenddate"));
        criteria.add(Restrictions.between("date", startDate, endDate));
    }

    req.setAttribute("works", criteria.list());

    //      for (Iterator iter = criteria.list().iterator(); iter.hasNext();) {
    //         Work work = (Work) iter.next();
    //         vector.add(work);
    //      }
    Work work = new WorkImpl();
    WorkForm workform = new WorkForm();
    criteria = session.createCriteria(Work.class);

    if (req.getAttribute("form") == null && req.getParameter("id") != null) {
        criteria.add(Restrictions.idEq(Integer.valueOf(req.getParameter("id"))));
        work = (Work) criteria.uniqueResult();
        new CopyProperties(work, workform);
    } else if (req.getAttribute("form") != null) {
        workform = (WorkForm) req.getAttribute("form");
        criteria.add(Restrictions.idEq(workform.getId()));
        work = (Work) criteria.uniqueResult();
    }

    req.setAttribute("model", work);
    req.setAttribute("form", workform);

    Criteria criteriaArtist = session.createCriteria(Artist.class);
    req.setAttribute("Artists", criteriaArtist.list());

    if (req.getParameter("displayexport") != null) {
        return mapping.findForward("displayexport");
    }

    return mapping.findForward("success");
}

From source file:org.andromda.timetracker.domain.crud.UserManageableDaoBase.java

/**
 * @param username TODO: Model Documentation for User.username
 * @param password TODO: Model Documentation for User.password
 * @param firstName TODO: Model Documentation for User.firstName
 * @param lastName TODO: Model Documentation for User.lastName
 * @param email TODO: Model Documentation for User.email
 * @param isActive TODO: Model Documentation for User.isActive
 * @param creationDate TODO: Model Documentation for User.creationDate
 * @param comment TODO: Model Documentation for User.comment
 * @param id TODO: Model Documentation for User.id
 * @param roles TODO: Model Documentation for UserRole
 * @return List<User>//from   w  w  w . j a  v  a 2 s  . c o  m
 */
@SuppressWarnings("unchecked")
public List<User> read(String username, String password, String firstName, String lastName, String email,
        Boolean isActive, Date creationDate, String comment, Long id, Long[] roles) {
    final Session session = getSession(false);

    try {
        final Criteria criteria = session.createCriteria(UserImpl.class);

        if (username != null) {
            criteria.add(Restrictions.ilike("username", username, MatchMode.START));
        }
        if (password != null) {
            criteria.add(Restrictions.ilike("password", password, MatchMode.START));
        }
        if (firstName != null) {
            criteria.add(Restrictions.ilike("firstName", firstName, MatchMode.START));
        }
        if (lastName != null) {
            criteria.add(Restrictions.ilike("lastName", lastName, MatchMode.START));
        }
        if (email != null) {
            criteria.add(Restrictions.ilike("email", email, MatchMode.START));
        }
        if (isActive != null) {
            criteria.add(Restrictions.eq("isActive", isActive.booleanValue()));
        }
        if (creationDate != null) {
            // we check whether or not the user supplied time information within this particular date argument
            // if he/she didn't we assume he/she wishes to search in the scope of the entire day
            final Calendar calendar = new GregorianCalendar();
            calendar.setTime(creationDate);
            if (calendar.get(Calendar.HOUR) != 0 || calendar.get(Calendar.MINUTE) != 0
                    || calendar.get(Calendar.SECOND) != 0 || calendar.get(Calendar.MILLISECOND) != 0) {
                criteria.add(Restrictions.eq("creationDate", creationDate));
            } else {
                calendar.add(Calendar.DATE, 1);
                calendar.add(Calendar.MILLISECOND, -1);
                criteria.add(Restrictions.between("creationDate", creationDate, calendar.getTime()));
            }
        }
        if (comment != null) {
            criteria.add(Restrictions.ilike("comment", comment, MatchMode.START));
        }
        if (id != null) {
            criteria.add(Restrictions.eq("id", id));
        }
        if (roles != null && roles.length > 0) {
            criteria.createCriteria("roles").add(Restrictions.in("id", roles));
        }
        criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
        criteria.setMaxResults(250);

        return criteria.list();
    } catch (HibernateException ex) {
        throw super.convertHibernateAccessException(ex);
    }
}

From source file:org.apache.usergrid.apm.service.charts.filter.DateIntervalFilter.java

License:Apache License

/** 
 * Prepare criteria according to filter values. Ideally when both from and to are present, it should result into two criterion
 * startTime >= from and endTime <= to but given that delta between startTime and endTime is not huge and we don't have to be 
 * precise upto few minutes, selecting startTime between from and to should work. It should also help with faster query as well.
 * @return/*from   w ww.j a va 2 s.c  o m*/
 */
public Criterion getCriteria() {

    if (getFilterEmpty())
        return null;

    if (from != null && to != null)
        return Restrictions.between(startTime, from, to);

    Criterion crit = null;
    if (from != null)
        crit = Restrictions.gt(startTime, from);
    else
        crit = Restrictions.lt(endTime, to);

    return crit;
}

From source file:org.balisunrise.daa.hibernate.HCriteria.java

License:Open Source License

private org.hibernate.criterion.Criterion makeCriterion(String propertyName, FilterType type, Object value,
        Object otherValue) {//from w  ww  .j  a  va  2s.co  m

    switch (type) {
    case EQUALS:
        return Restrictions.eq(propertyName, value);
    case EQUALS_OR_NULL:
        return Restrictions.eqOrIsNull(propertyName, value);
    case DIFFERENT:
        return Restrictions.ne(propertyName, value);
    case DIFFERENT_OR_NOT_NULL:
        return Restrictions.neOrIsNotNull(propertyName, value);
    case GREATHER:
        return Restrictions.gt(propertyName, value);
    case GREATHER_EQUALS:
        return Restrictions.ge(propertyName, value);
    case LESS:
        return Restrictions.lt(propertyName, value);
    case LESS_EQUALS:
        return Restrictions.le(propertyName, value);
    case LIKE:
        return Restrictions.like(propertyName, value + "%");
    case ILIKE:
        return Restrictions.ilike(propertyName, "%" + value + "%");
    case BETWEEN:
        return Restrictions.between(propertyName, value, otherValue);
    case IN:
        if (value instanceof Collection)
            return Restrictions.in(propertyName, (Collection) value);
        else if (value instanceof Object[])
            return Restrictions.in(propertyName, (Object[]) value);
        else
            return Restrictions.in(propertyName, new Object[] { value });
    case NULL:
        return Restrictions.isNull(propertyName);
    case NOT_NULL:
        return Restrictions.isNotNull(propertyName);
    default:
        return null;
    }
}

From source file:org.candlepin.model.EntitlementCurator.java

License:Open Source License

private Criteria createModifiesDateFilteringCriteria(Consumer consumer, Date startDate, Date endDate) {
    Criteria criteria = currentSession().createCriteria(Entitlement.class)
            .add(Restrictions.eq("consumer", consumer)).createCriteria("pool").add(Restrictions.or(
                    // Dates overlap if the start or end date is in our range
                    Restrictions.or(Restrictions.between("startDate", startDate, endDate),
                            Restrictions.between("endDate", startDate, endDate)),
                    Restrictions.and(//from w ww.j  ava  2  s. c o m
                            // The dates overlap if our range is completely encapsulated
                            Restrictions.le("startDate", startDate), Restrictions.ge("endDate", endDate))));
    return criteria;
}

From source file:org.cast.cwm.data.builders.ResponseCriteriaBuilder.java

License:Open Source License

@Override
public void buildUnordered(Criteria criteria) {
    if (promptModel != null && promptModel.getObject() != null)
        criteria.add(Restrictions.eq("prompt", promptModel.getObject()));
    if (userModel != null && userModel.getObject() != null)
        criteria.add(Restrictions.eq("user", userModel.getObject()));
    if (periodModel != null && periodModel.getObject() != null)
        criteria.createAlias("user", "user").createAlias("user.periods", "p")
                .add(Restrictions.eq("p.id", periodModel.getObject().getId()));
    if (responseType != null)
        criteria.add(Restrictions.eq("type", responseType));
    if (sortOrder != null)
        criteria.add(Restrictions.eq("sortOrder", sortOrder));
    if (maxResults != null)
        criteria.setMaxResults(maxResults);
    if (fromDate != null && toDate != null && fromDate.before(toDate))
        criteria.add(Restrictions.between("lastUpdated", fromDate, toDate));
    criteria.add(Restrictions.eq("valid", true));
    criteria.setCacheable(true);//from www  . j a  v a2s.  co m
}

From source file:org.castafiore.blog.BlogServiceImpl.java

License:Open Source License

public List<BlogPost> getPosts(Calendar startDate, Calendar endDate) {
    QueryParameters params = new QueryParameters().setEntity(BlogPost.class).addOrder(Order.desc("dateCreated"))
            .addRestriction(Restrictions.between("dateCreated", startDate, endDate));
    //String hql = "from " + BlogPost.class.getName() + " where dateCreated between ?  and ? order by dateCreated desc";
    List list = repositoryService.executeQuery(params, Util.getRemoteUser());
    return list;/*  w  ww .j  a va2  s  .co  m*/
}

From source file:org.codehaus.groovy.grails.orm.hibernate.query.AbstractHibernateCriterionAdapter.java

License:Apache License

protected void addRangeQueryCriterionAdapters() {
    criterionAdaptors.put(Query.Between.class, new CriterionAdaptor() {
        @Override/*from ww w .j  a  v  a 2 s.c  om*/
        public Criterion toHibernateCriterion(AbstractHibernateQuery hibernateQuery, Query.Criterion criterion,
                String alias) {
            Query.Between btwCriterion = (Query.Between) criterion;
            return Restrictions.between(
                    calculatePropertyName(calculatePropertyName(btwCriterion.getProperty(), alias), alias),
                    btwCriterion.getFrom(), btwCriterion.getTo());
        }
    });

    criterionAdaptors.put(Query.In.class, new CriterionAdaptor() {
        @Override
        public Criterion toHibernateCriterion(AbstractHibernateQuery hibernateQuery, Query.Criterion criterion,
                String alias) {
            Query.In inListQuery = (Query.In) criterion;
            QueryableCriteria subquery = inListQuery.getSubquery();
            if (subquery != null) {
                return Property.forName(getPropertyName(criterion, alias))
                        .in(toHibernateDetachedCriteria(hibernateQuery, subquery));
            } else {
                return Restrictions.in(getPropertyName(criterion, alias), inListQuery.getValues());
            }
        }
    });
}