Java tutorial
/* * Copyright (C) 2015 Glauco Knihs. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. * * Please contact Glauco Knihs, Rua 10 de Junho, N469, Centro, * Guabiruba-SC, CEP 88360-000, BRAZIL, eglauko@hotmail.com, * if you need additional information or have any questions. */ package org.balisunrise.daa.hibernate; import java.util.Collection; import java.util.LinkedList; import java.util.List; import org.balisunrise.daa.Query; import org.balisunrise.daa.Query.Criteria; import org.balisunrise.daa.Query.Criterion; import org.balisunrise.daa.Query.FilterType; import org.balisunrise.daa.defaults.DefaultCriteria; import org.hibernate.criterion.Restrictions; /** * Classe HCriteria. * * @author Glauco Knihs * @version 1.0 * @param <T> Tipo de dado da entidade * @since 1.0 */ public class HCriteria<T> extends DefaultCriteria<T> { private org.hibernate.Criteria hcrit; private HAliasMap aliasMap; public HCriteria(Query<T> query, org.hibernate.Criteria criteria, HAliasMap aliasMap) { super(query); this.aliasMap = aliasMap; this.hcrit = criteria; } @Override public Criteria<T> addCriterion(Query.Criterion criterion) { org.hibernate.criterion.Criterion hc = makeCriterion(criterion); if (hc != null) { hcrit.add(hc); super.addCriterion(criterion); } return this; } private org.hibernate.criterion.Criterion makeCriterion(Criterion criterion) { switch (criterion.getCriterionType()) { case FILTER: return makeCriterion((Criterion.Filter) criterion); case LOGIC: return makeCriterion((Criterion.Junction) criterion); case COMPARATOR: return makeCriterion((Criterion.Comparator) criterion); } return null; } /** * Cria um criterion do hibernate a partir de um filter. * @param filter Filter com informaes do filtro a ser criado. * @return Criterion do hibernate, ou null se ignoreEmpty for aplicvel. */ private org.hibernate.criterion.Criterion makeCriterion(Criterion.Filter filter) { if (filter.ignoreEmpty() && filter.getValue() == null && (filter.getFilterType() != FilterType.BETWEEN || filter.getSecondValue() == null)) return null; return makeCriterion(aliasMap.getPropertyName(filter.getProperty()), filter.getFilterType(), filter.getValue(), filter.getSecondValue()); } private org.hibernate.criterion.Criterion makeCriterion(String propertyName, FilterType type, Object value, Object otherValue) { 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; } } private org.hibernate.criterion.Criterion makeCriterion(Criterion.Junction junction) { List<org.hibernate.criterion.Criterion> hcs = new LinkedList<>(); for (Criterion criterion : junction.getCriterions()) { org.hibernate.criterion.Criterion hc = makeCriterion(criterion); if (hc != null) hcs.add(hc); } if (hcs.isEmpty()) return null; if (hcs.size() == 1) return hcs.get(0); org.hibernate.criterion.Criterion[] array = (org.hibernate.criterion.Criterion[]) hcs.toArray(); if (junction.getLogicType() == Query.LogicType.OR) return Restrictions.or(array); else { org.hibernate.criterion.Criterion hc = Restrictions.and(array); if (junction.getLogicType() == Query.LogicType.NOT) return Restrictions.not(hc); return hc; } } private org.hibernate.criterion.Criterion makeCriterion(Criterion.Comparator comparator) { String fp = aliasMap.getPropertyName(comparator.getFirstProperty()); String sp = aliasMap.getPropertyName(comparator.getSecondProperty()); switch (comparator.getComparatorType()) { case EQUALS: return Restrictions.eqProperty(fp, sp); case DIFFERENT: return Restrictions.neProperty(fp, sp); case GREATHER: return Restrictions.gtProperty(fp, sp); case GREATHER_EQUALS: return Restrictions.geProperty(fp, sp); case LESS: return Restrictions.ltProperty(fp, sp); case LESS_EQUALS: return Restrictions.leProperty(fp, sp); } return null; } }