Example usage for org.hibernate.criterion Restrictions ilike

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

Introduction

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

Prototype

public static Criterion ilike(String propertyName, String value, MatchMode matchMode) 

Source Link

Document

A case-insensitive "like" (similar to Postgres ilike operator) using the provided match mode

Usage

From source file:com.mac.green_leaves.v1.master.MasterRepository.java

public int totalItems(String keyword, Integer branch, Class modelClass) {
    Session session = getSession();/*w  w  w . j a v a2s  . co  m*/

    Criteria criteria = session.createCriteria(modelClass);
    if (keyword != null) {
        Field[] fields = modelClass.getDeclaredFields();
        ArrayList<Criterion> criterions = new ArrayList<>();
        for (Field field : fields) {
            if (field.getType().isAssignableFrom(String.class)) {
                criterions.add(Restrictions.ilike(field.getName(), keyword, MatchMode.ANYWHERE));
            }
        }
        criteria.add(Restrictions.or(criterions.toArray(new Criterion[] {})));
    }

    criteria.add(Restrictions.eq("branch", branch));

    criteria.setProjection(Projections.count("indexNo"));
    Integer totalItems = ((Long) criteria.uniqueResult()).intValue();
    return totalItems;
}

From source file:com.miranteinfo.seam.framework.service.GenericRetrieveService.java

License:Open Source License

/**
 * Retorna a lista de entidades filtradas pelos parametros informadas.
 * //from  ww  w .  j a  v  a 2  s . c  om
 * @param <T>
 * @param classe
 * @param propertyName
 * @param value
 * @param matchMode
 * @return lista de entidades.
 */
public <T extends BaseEntity> List<T> retrieveByProperty(Class<T> classe, String propertyName, String value,
        MatchMode matchMode) {
    if (matchMode != null) {
        return getSession().createCriteria(classe).add(Restrictions.ilike(propertyName, value, matchMode))
                .list();
    } else {
        return getSession().createCriteria(classe).add(Restrictions.ilike(propertyName, value, MatchMode.EXACT))
                .list();
    }
}

From source file:com.oliveira.pedidovenda.repository.Clientes.java

public List<Cliente> filtrados(ClienteFilter filtro) {
    Session session = manager.unwrap(Session.class);
    Criteria criteria = session.createCriteria(Cliente.class);

    if (StringUtils.isNotBlank(filtro.getNome())) {
        criteria.add(Restrictions.ilike("nome", filtro.getNome(), MatchMode.ANYWHERE));
    }/*  ww w  .j  av  a 2  s  . c o m*/

    return criteria.addOrder(Order.asc("nome")).list();
}

From source file:com.pmo.pmoitserv.Dao.ActionDao.java

public long getEnCoursActionCount_byCompte(int compteId) {
    Long result = 0L;//w  w w. j  av a2s  .  c om
    ;
    Compte cpte = null;
    List<Projet> pjts = new ArrayList<Projet>();
    List<Sousprojet> souspjts = new ArrayList<Sousprojet>();
    List<Chantier> chantiers = new ArrayList<Chantier>();
    try {
        this.session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = session.beginTransaction();
        cpte = (Compte) session.get(Compte.class, compteId);
        pjts = session.createCriteria(Projet.class).add(Restrictions.eq("compte", cpte)).list();
        souspjts = session.createCriteria(Sousprojet.class).add(Restrictions.in("projet", pjts)).list();
        chantiers = session.createCriteria(Chantier.class).add(Restrictions.in("sousprojet", souspjts)).list();
        Criteria cr = session.createCriteria(Action.class);
        Criterion c1 = Restrictions.ilike("actionStatut", "cours", MatchMode.ANYWHERE);
        Criterion c2 = Restrictions.in("chantier", chantiers);
        cr.add(Restrictions.and(c1, c2));
        cr.setProjection(Projections.rowCount());
        result = (Long) cr.uniqueResult();
        tx.commit();
        session.flush();
        session.close();
        return result;

    } catch (Exception e) {
        e.printStackTrace();
        session.close();
    }
    return result;
}

From source file:com.pmo.pmoitserv.Dao.ProjetDao.java

public long getEnCoursProjetCount() {
    Long result = 0L;//ww w  .  ja  v  a 2  s  . c o  m
    try {
        this.session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = session.beginTransaction();

        Criteria cr = session.createCriteria(Projet.class);
        Criterion c1 = Restrictions.ilike("projetStatut", "en", MatchMode.ANYWHERE);
        Criterion c2 = Restrictions.ilike("projetStatut", "cours", MatchMode.ANYWHERE);
        cr.add(Restrictions.and(c1, c2));
        cr.setProjection(Projections.rowCount());
        result = (Long) cr.uniqueResult();
        tx.commit();
        session.flush();
        session.close();
        return result;

    } catch (Exception e) {
        e.printStackTrace();
        session.close();
    }
    return result;
}

From source file:com.pmo.pmoitserv.Dao.ProjetDao.java

public long getClotureProjetCount() {
    Long result = 0L;/*w ww  .j av  a 2s .  com*/
    try {
        this.session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = session.beginTransaction();

        Criteria cr = session.createCriteria(Projet.class);
        cr.add(Restrictions.ilike("projetStatut", "clotur", MatchMode.ANYWHERE));
        cr.setProjection(Projections.rowCount());
        result = (Long) cr.uniqueResult();
        tx.commit();
        session.flush();
        session.close();
        return result;

    } catch (Exception e) {
        e.printStackTrace();
        session.close();
    }
    return result;
}

From source file:com.pmo.pmoitserv.Dao.ProjetDao.java

public long getStandByProjetCount() {
    Long result = 0L;//from  w w  w  . j a va 2  s  .  c  o  m
    try {
        this.session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = session.beginTransaction();

        Criteria cr = session.createCriteria(Projet.class);
        cr.add(Restrictions.ilike("projetStatut", "stand", MatchMode.ANYWHERE));
        cr.setProjection(Projections.rowCount());
        result = (Long) cr.uniqueResult();
        tx.commit();
        session.flush();
        session.close();
        return result;

    } catch (Exception e) {
        e.printStackTrace();
        session.close();
    }
    return result;
}

From source file:com.qcadoo.model.api.search.SearchRestrictions.java

License:Open Source License

/**
 * Creates criterion which checks if field is equal (using case-insensitive "like" operator) to given value.
 * /*w  ww .jav a2  s .  co m*/
 * @param field
 *            field
 * @param mode
 *            match mode
 * @param value
 *            value
 * @return criterion
 */
public static SearchCriterion ilike(final String field, final String value, final SearchMatchMode mode) {
    if (value == null) {
        return isNull(field);
    }
    return new SearchCriterionImpl(
            Restrictions.ilike(field, convertWildcards(value), mode.getHibernateMatchMode()));
}

From source file:com.reignite.parser.QueryParser.java

License:Open Source License

private Criterion processField(JSONObject where) throws ParserException {
    String field = JSONObject.getNames(where)[0];
    JSONObject exp;/*  w  w w .  j  a va  2 s  .  c  o m*/
    try {
        exp = where.getJSONObject(field);
    } catch (JSONException e) {
        throw new ParserException("field expressions must be JSON Object: " + field);
    }
    ExpressionType type = ExpressionType.get(JSONObject.getNames(exp)[0]);
    Object value = null;
    // if the field is a join
    if (field.indexOf(".") > -1) {
        String join = field.substring(0, field.indexOf("."));
        query.createJoin(join);
    }
    switch (type) {
    case IN:
    case NOT_IN:
        try {
            value = createArray(exp.getJSONArray(type.getName()));
        } catch (JSONException e) {
            throw new ParserException("in and not in expressions must be arrays: " + exp);
        }
        break;
    default:
        try {
            value = createValue(exp.get(type.getName()));
        } catch (JSONException e) {
            throw new ParserException("expressions must have a value: " + exp);
        }
    }

    switch (type) {
    case GREATER_THAN:
        return Restrictions.gt(field, value);
    case IN:
        return Restrictions.in(field, (Object[]) value);
    case LESS_THAN:
        return Restrictions.lt(field, value);
    case LIKE:
        MatchMode match = MatchMode.EXACT;
        String toMatch = value.toString();
        if (value.toString().startsWith("%")) {
            match = MatchMode.END;
            toMatch = toMatch.substring(1);
        }
        if (value.toString().endsWith("%")) {
            toMatch = toMatch.substring(0, toMatch.length() - 1);
            if (match == MatchMode.END) {
                match = MatchMode.ANYWHERE;
            } else {
                match = MatchMode.START;
            }
        }
        return Restrictions.ilike(field, toMatch, match);
    case NOT_EQUAL:
        if (value == null) {
            return Restrictions.isNotNull(field);
        }
        return Restrictions.ne(field, value);
    case NOT_IN:
        return Restrictions.not(Restrictions.in(field, (Object[]) value));
    case EQUAL:
    default:
        if (value == null) {
            return Restrictions.isNull(field);
        }
        return Restrictions.eq(field, value);
    }

}

From source file:com.scopix.periscope.corporatestructuremanagement.dao.CorporateStructureManagementHibernateDAOImpl.java

License:Open Source License

/**
 * Obtain a list of AreaType object using filters.
 *
 * @param areaType Filter object//from   w  w w.  j a va2 s .c  o m
 * @return List<AreaType> List of AreaType objects
 */
@Override
public List<AreaType> getAreaTypeList(AreaType areaType) {
    List<AreaType> areaTypes = null;
    Criteria criteria = this.getSession().createCriteria(AreaType.class);
    criteria.addOrder(Order.asc("id"));
    if (areaType != null) {
        if (areaType.getName() != null && areaType.getName().length() > 0) {
            criteria.add(Restrictions.ilike("name", areaType.getName(), MatchMode.ANYWHERE));
        }
        if (areaType.getDescription() != null && areaType.getDescription().length() > 0) {
            criteria.add(Restrictions.ilike("description", areaType.getDescription(), MatchMode.ANYWHERE));
        }
    }
    areaTypes = criteria.list();
    return areaTypes;
}