Example usage for org.hibernate.criterion Restrictions isNotEmpty

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

Introduction

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

Prototype

public static Criterion isNotEmpty(String propertyName) 

Source Link

Document

Constrain a collection valued property to be non-empty

Usage

From source file:au.org.theark.core.dao.StudyDao.java

License:Open Source License

private List<Long> applyConsentStatusFilters(DataExtractionVO allTheData, Search search,
        List<Long> idsToInclude) {

    //for(Long l : idsToInclude) {
    //   log.info("including: " + l);
    //}/*from  w w w  .  j av  a2 s  .c om*/
    boolean hasConsentFilters = false;
    if (search.getQueryFilters().isEmpty()) {
        return idsToInclude;
    } else {
        for (QueryFilter filter : search.getQueryFilters()) {
            if (filter.getConsentStatusField() != null) {
                hasConsentFilters = true;
            }
        }
    }
    Criteria filter = getSession().createCriteria(Consent.class, "c");
    filter.add(Restrictions.eq("c.study.id", search.getStudy().getId()));
    filter.createAlias("c.linkSubjectStudy", "lss");
    if (!idsToInclude.isEmpty()) {
        filter.add(Restrictions.in("lss.id", idsToInclude));
    }
    filter.createAlias("c.studyComponentStatus", "cscs");
    filter.createAlias("c.studyComp", "csc");

    if (!hasConsentFilters) {

        for (QueryFilter qf : search.getQueryFilters()) {
            if (qf.getConsentStatusField() != null) {
                switch (qf.getOperator()) {
                case EQUAL:
                    filter.add(Restrictions.eq(getConsentFilterFieldName(qf), qf.getValue()));
                    break;
                case BETWEEN:
                    filter.add(Restrictions.between(getConsentFilterFieldName(qf), qf.getValue(),
                            qf.getSecondValue()));
                    break;
                case GREATER_THAN:
                    filter.add(Restrictions.gt(getConsentFilterFieldName(qf), qf.getValue()));
                    break;
                case GREATER_THAN_OR_EQUAL:
                    filter.add(Restrictions.ge(getConsentFilterFieldName(qf), qf.getValue()));
                    break;
                case IS_EMPTY:
                    filter.add(Restrictions.isEmpty(getConsentFilterFieldName(qf)));
                    break;
                case IS_NOT_EMPTY:
                    filter.add(Restrictions.isNotEmpty(getConsentFilterFieldName(qf)));
                    break;
                case LESS_THAN:
                    filter.add(Restrictions.lt(getConsentFilterFieldName(qf), qf.getValue()));
                    break;
                case LESS_THAN_OR_EQUAL:
                    filter.add(Restrictions.le(getConsentFilterFieldName(qf), qf.getValue()));
                    break;
                case LIKE:
                    filter.add(Restrictions.like(getConsentFilterFieldName(qf), qf.getValue(),
                            MatchMode.ANYWHERE));
                    break;
                case NOT_EQUAL:
                    filter.add(Restrictions.ne(getConsentFilterFieldName(qf), qf.getValue()));
                    break;
                default:
                    break;
                }
            }
        }
    }
    filter.setProjection(
            Projections.distinct(Projections.projectionList().add(Projections.property("lss.id"))));

    List<Long> consentStatusIDs = filter.list();

    Collection<Consent> csData = Collections.EMPTY_LIST;

    if (!consentStatusIDs.isEmpty()) {
        Criteria consentData = getSession().createCriteria(Consent.class, "c");
        consentData.add(Restrictions.eq("c.study.id", search.getStudy().getId()));
        consentData.createAlias("c.linkSubjectStudy", "lss");
        consentData.add(Restrictions.in("lss.id", consentStatusIDs));
        csData = consentData.list();
    }

    HashMap<String, ExtractionVO> hashOfConsentStatusData = allTheData.getConsentStatusData();

    ExtractionVO valuesForThisLss = new ExtractionVO();
    HashMap<String, String> map = null;
    LinkSubjectStudy previousLss = null;
    int count = 0;
    //will try to order our results and can therefore just compare to last LSS and either add to or create new Extraction VO
    for (Consent data : csData) {
        if (previousLss == null) {
            map = new HashMap<String, String>();
            previousLss = data.getLinkSubjectStudy();
            count = 0;
        } else if (data.getLinkSubjectStudy().getId().equals(previousLss.getId())) {
            //then just put the data in
            count++;
        } else { //if its a new LSS finalize previous map, etc
            valuesForThisLss.setKeyValues(map);
            valuesForThisLss.setSubjectUid(previousLss.getSubjectUID());
            hashOfConsentStatusData.put(previousLss.getSubjectUID(), valuesForThisLss);
            previousLss = data.getLinkSubjectStudy();
            map = new HashMap<String, String>();//reset
            valuesForThisLss = new ExtractionVO();
            count = 0;
        }
        if (data.getStudyComp().getName() != null) {
            map.put(count + "_Study Component Name", data.getStudyComp().getName());
        }
        if (data.getStudyComponentStatus() != null) {
            map.put(count + "_Study Component Status", data.getStudyComponentStatus().getName());
        }
        if (data.getConsentDate() != null) {
            map.put(count + "_Consent Date", data.getConsentDate().toString());
        }
        if (data.getConsentedBy() != null) {
            map.put(count + "_Consented By", data.getConsentedBy());
        }
    }

    //finalize the last entered key value sets/extraction VOs
    if (map != null && previousLss != null) {
        valuesForThisLss.setKeyValues(map);
        valuesForThisLss.setSubjectUid(previousLss.getSubjectUID());
        hashOfConsentStatusData.put(previousLss.getSubjectUID(), valuesForThisLss);
    }

    //can probably now go ahead and add these to the dataVO...even though inevitable further filters may further axe this list or parts of it.
    allTheData.setConsentStatusData(hashOfConsentStatusData);
    if (hasConsentFilters) {
        return consentStatusIDs;
    } else {
        return idsToInclude;
    }
}

From source file:com.abssh.util.GenericDao.java

License:Apache License

/**
 * ??Criterion,.//from  w w  w  .j  av a2  s. c o m
 */
protected Criterion buildPropertyFilterCriterion(final String propertyName, final Object[] propertyValue,
        final MatchType matchType) {
    Assert.hasText(propertyName, "propertyName should not be null!");
    Criterion criterion = null;
    try {
        // ?MatchTypecriterion
        if (MatchType.EQ.equals(matchType)) {
            criterion = Restrictions.eq(propertyName, propertyValue[0]);
        } else if (MatchType.LIKE.equals(matchType)) {
            criterion = Restrictions.like(propertyName, (String) propertyValue[0], MatchMode.ANYWHERE);
        } else if (MatchType.ELIKE.equals(matchType)) {
            criterion = Restrictions.like(propertyName, (String) propertyValue[0], MatchMode.END);
        } else if (MatchType.SLIKE.equals(matchType)) {
            criterion = Restrictions.like(propertyName, (String) propertyValue[0], MatchMode.START);
        } else if (MatchType.LE.equals(matchType)) {
            criterion = Restrictions.le(propertyName, propertyValue[0]);
        } else if (MatchType.LT.equals(matchType)) {
            criterion = Restrictions.lt(propertyName, propertyValue[0]);
        } else if (MatchType.GE.equals(matchType)) {
            criterion = Restrictions.ge(propertyName, propertyValue[0]);
        } else if (MatchType.GT.equals(matchType)) {
            criterion = Restrictions.gt(propertyName, propertyValue[0]);
        } else if (MatchType.NE.equals(matchType)) {
            criterion = Restrictions.ne(propertyName, propertyValue[0]);
        } else if (MatchType.ISNULL.equals(matchType)) {
            criterion = Restrictions.isNull(propertyName);
        } else if (MatchType.ISNOTNULL.equals(matchType)) {
            criterion = Restrictions.isNotNull(propertyName);
        } else if (MatchType.ISEMPTY.equals(matchType)) {
            criterion = Restrictions.isEmpty(propertyName);
        } else if (MatchType.ISNOTEMPTY.equals(matchType)) {
            criterion = Restrictions.isNotEmpty(propertyName);
        } else if (MatchType.IN.equals(matchType)) {
            criterion = Restrictions.in(propertyName, propertyValue);
        }
    } catch (Exception e) {
        throw ReflectionUtils.convertReflectionExceptionToUnchecked(e);
    }
    return criterion;
}

From source file:com.abssh.util.GenericDao.java

License:Apache License

/**
 * ??//from   www  . j a va2  s . com
 */
private Criterion getCriterion(String propertyName, Object[] propertyValue, MatchType matchType) {
    Criterion criterion = null;
    try {
        // ?MatchTypecriterion
        if (MatchType.EQ.equals(matchType)) {
            criterion = Restrictions.eq(propertyName, propertyValue[0]);
        } else if (MatchType.LIKE.equals(matchType)) {
            criterion = RestrictionsUtils.ilike(propertyName, String.valueOf(propertyValue[0]),
                    MatchMode.ANYWHERE);
        } else if (MatchType.ELIKE.equals(matchType)) {
            criterion = RestrictionsUtils.ilike(propertyName, (String) propertyValue[0], MatchMode.END);
        } else if (MatchType.SLIKE.equals(matchType)) {
            criterion = RestrictionsUtils.ilike(propertyName, (String) propertyValue[0], MatchMode.START);
        } else if (MatchType.LE.equals(matchType)) {
            criterion = Restrictions.le(propertyName, propertyValue[0]);
        } else if (MatchType.LT.equals(matchType)) {
            criterion = Restrictions.lt(propertyName, propertyValue[0]);
        } else if (MatchType.GE.equals(matchType)) {
            criterion = Restrictions.ge(propertyName, propertyValue[0]);
        } else if (MatchType.GT.equals(matchType)) {
            criterion = Restrictions.gt(propertyName, propertyValue[0]);
        } else if (MatchType.NE.equals(matchType)) {
            criterion = Restrictions.ne(propertyName, propertyValue[0]);
        } else if (MatchType.ISNULL.equals(matchType)) {
            criterion = Restrictions.isNull(propertyName);
        } else if (MatchType.ISNOTNULL.equals(matchType)) {
            criterion = Restrictions.isNotNull(propertyName);
        } else if (MatchType.ISEMPTY.equals(matchType)) {
            criterion = Restrictions.isEmpty(propertyName);
        } else if (MatchType.ISNOTEMPTY.equals(matchType)) {
            criterion = Restrictions.isNotEmpty(propertyName);
        } else if (MatchType.IN.equals(matchType)) {
            criterion = Restrictions.in(propertyName, propertyValue);
        } else if (MatchType.LEN.equals(matchType)) {
            criterion = Restrictions.le(propertyName, propertyValue[0]);
        }
    } catch (Exception e) {
        throw ReflectionUtils.convertReflectionExceptionToUnchecked(e);
    }
    return criterion;
}

From source file:com.algoTrader.CriteriaSearch.java

/**
 * Adds an <code>Restrictions</code> to a <code>Criteria</code>.
 *
 * @param criteria//from w w  w . j a  v  a 2 s  .  co  m
 * @param parameterName
 * @param parameterValue
 * @param comparator
 * @param matchMode
 */
private void addExpression(Criteria criteria, String parameterName, Object parameterValue, int comparator,
        MatchMode matchMode) {
    switch (comparator) {
    case SearchParameter.NOT_NULL_COMPARATOR: {
        criteria.add(Restrictions.isNotNull(parameterName));
        break;
    }
    case SearchParameter.NULL_COMPARATOR: {
        criteria.add(Restrictions.isNull(parameterName));
        break;
    }
    case SearchParameter.EMPTY_COMPARATOR: {
        criteria.add(Restrictions.isEmpty(parameterName));
        break;
    }
    case SearchParameter.NOT_EMPTY_COMPARATOR: {
        criteria.add(Restrictions.isNotEmpty(parameterName));
        break;
    }
    default: {
        if (parameterValue != null) {
            switch (comparator) {
            case SearchParameter.LIKE_COMPARATOR: {
                if ((matchMode != null) && (parameterValue instanceof String)) {
                    criteria.add(Restrictions.like(parameterName, (String) parameterValue, matchMode));
                } else {
                    criteria.add(Restrictions.like(parameterName, parameterValue));
                }
                break;
            }
            case SearchParameter.NOT_LIKE_COMPARATOR: {
                SimpleExpression expression;
                if ((matchMode != null) && (parameterValue instanceof String)) {
                    expression = Restrictions.like(parameterName, (String) parameterValue, matchMode);
                } else {
                    expression = Restrictions.like(parameterName, parameterValue);
                }
                criteria.add(Restrictions.not(expression));
                break;
            }
            case SearchParameter.INSENSITIVE_LIKE_COMPARATOR: {
                if ((matchMode != null) && (parameterValue instanceof String)) {
                    criteria.add(Restrictions.ilike(parameterName, (String) parameterValue, matchMode));
                } else {
                    criteria.add(Restrictions.ilike(parameterName, parameterValue));
                }
                break;
            }
            case SearchParameter.NOT_INSENSITIVE_LIKE_COMPARATOR: {
                Criterion criterion;
                if ((matchMode != null) && (parameterValue instanceof String)) {
                    criterion = Restrictions.ilike(parameterName, (String) parameterValue, matchMode);
                } else {
                    criterion = Restrictions.ilike(parameterName, parameterValue);
                }
                criteria.add(Restrictions.not(criterion));
                break;
            }
            case SearchParameter.EQUAL_COMPARATOR: {
                criteria.add(Restrictions.eq(parameterName, parameterValue));
                break;
            }
            case SearchParameter.GREATER_THAN_OR_EQUAL_COMPARATOR: {
                criteria.add(Restrictions.ge(parameterName, parameterValue));
                break;
            }
            case SearchParameter.GREATER_THAN_COMPARATOR: {
                criteria.add(Restrictions.gt(parameterName, parameterValue));
                break;
            }
            case SearchParameter.LESS_THAN_OR_EQUAL_COMPARATOR: {
                criteria.add(Restrictions.le(parameterName, parameterValue));
                break;
            }
            case SearchParameter.LESS_THAN_COMPARATOR: {
                criteria.add(Restrictions.lt(parameterName, parameterValue));
                break;
            }
            case SearchParameter.IN_COMPARATOR: {
                if (parameterValue instanceof Collection) {
                    criteria.add(Restrictions.in(parameterName, (Collection) parameterValue));
                }
                break;
            }
            case SearchParameter.NOT_IN_COMPARATOR: {
                if (parameterValue instanceof Collection) {
                    criteria.add(Restrictions.not(Restrictions.in(parameterName, (Collection) parameterValue)));
                }
                break;
            }
            case SearchParameter.NOT_EQUAL_COMPARATOR: {
                criteria.add(Restrictions.ne(parameterName, parameterValue));
                break;
            }
            }
        } else {
            criteria.add(Restrictions.isNull(parameterName));
        }
    }
    }
}

From source file:com.algoTrader.CriteriaSearch.java

/**
 * Adds an <code>Restrictions</code> to a <code>Criteria</code>. The given <code>parameterValues</code>
 * represents either an array of <code>String</code> or another object. The different values in the
 * array are added to a disjunction or conjunction which is connected with logical and to the other criteria of the
 * search./*from   w ww  .  jav a2  s .co m*/
 *
 * @param criteria
 * @param parameterName
 * @param parameterValues
 * @param searchIfNull
 * @param comparator
 * @param matchMode
 */
private void addExpression(Criteria criteria, String parameterName, Object[] parameterValues, int comparator,
        MatchMode matchMode) {
    if (parameterValues != null) {
        Disjunction disjunction = null;
        Conjunction conjunction = null;
        switch (comparator) {
        case SearchParameter.LIKE_COMPARATOR: {
            disjunction = Restrictions.disjunction();
            if ((matchMode != null) && (parameterValues instanceof String[])) {
                String[] stringParameterValues = (String[]) parameterValues;
                for (int index = 0; index < parameterValues.length; index++) {
                    if (stringParameterValues[index] != null) {
                        disjunction
                                .add(Restrictions.like(parameterName, stringParameterValues[index], matchMode));
                    } else {
                        disjunction.add(Restrictions.isNull(parameterName));
                    }
                }
            } else {
                for (int index = 0; index < parameterValues.length; index++) {
                    if (parameterValues[index] != null) {
                        disjunction.add(Restrictions.like(parameterName, parameterValues[index]));
                    } else {
                        disjunction.add(Restrictions.isNull(parameterName));
                    }
                }
            }
            break;
        }
        case SearchParameter.INSENSITIVE_LIKE_COMPARATOR: {
            disjunction = Restrictions.disjunction();
            if ((matchMode != null) && (parameterValues instanceof String[])) {
                String[] stringParameterValues = (String[]) parameterValues;
                for (int index = 0; index < parameterValues.length; index++) {
                    if (stringParameterValues[index] != null) {
                        disjunction.add(
                                Restrictions.ilike(parameterName, stringParameterValues[index], matchMode));
                    } else {
                        disjunction.add(Restrictions.isNull(parameterName));
                    }
                }
            } else {
                for (int index = 0; index < parameterValues.length; index++) {
                    if (parameterValues[index] != null) {
                        disjunction.add(Restrictions.ilike(parameterName, parameterValues[index]));
                    } else {
                        disjunction.add(Restrictions.isNull(parameterName));
                    }
                }
            }
            break;
        }
        case SearchParameter.EQUAL_COMPARATOR: {
            disjunction = Restrictions.disjunction();
            for (int index = 0; index < parameterValues.length; index++) {
                if (parameterValues[index] != null) {
                    disjunction.add(Restrictions.eq(parameterName, parameterValues[index]));
                } else {
                    disjunction.add(Restrictions.isNull(parameterName));
                }
            }
            break;
        }
        case SearchParameter.GREATER_THAN_OR_EQUAL_COMPARATOR: {
            disjunction = Restrictions.disjunction();
            for (int index = 0; index < parameterValues.length; index++) {
                if (parameterValues[index] != null) {
                    disjunction.add(Restrictions.ge(parameterName, parameterValues[index]));
                } else {
                    disjunction.add(Restrictions.isNull(parameterName));
                }
            }
            break;
        }
        case SearchParameter.GREATER_THAN_COMPARATOR: {
            disjunction = Restrictions.disjunction();
            for (int index = 0; index < parameterValues.length; index++) {
                if (parameterValues[index] != null) {
                    disjunction.add(Restrictions.gt(parameterName, parameterValues[index]));
                } else {
                    disjunction.add(Restrictions.isNull(parameterName));
                }
            }
            break;
        }
        case SearchParameter.LESS_THAN_OR_EQUAL_COMPARATOR: {
            disjunction = Restrictions.disjunction();
            for (int index = 0; index < parameterValues.length; index++) {
                if (parameterValues[index] != null) {
                    disjunction.add(Restrictions.le(parameterName, parameterValues[index]));
                } else {
                    disjunction.add(Restrictions.isNull(parameterName));
                }
            }
            break;
        }
        case SearchParameter.LESS_THAN_COMPARATOR: {
            disjunction = Restrictions.disjunction();
            for (int index = 0; index < parameterValues.length; index++) {
                if (parameterValues[index] != null) {
                    disjunction.add(Restrictions.lt(parameterName, parameterValues[index]));
                } else {
                    disjunction.add(Restrictions.isNull(parameterName));
                }
            }
            break;
        }
        case SearchParameter.IN_COMPARATOR: {
            criteria.add(Restrictions.in(parameterName, parameterValues));
            break;
        }
        case SearchParameter.NOT_IN_COMPARATOR: {
            criteria.add(Restrictions.not(Restrictions.in(parameterName, parameterValues)));
            break;
        }
        case SearchParameter.NOT_EQUAL_COMPARATOR: {
            conjunction = Restrictions.conjunction();
            for (int index = 0; index < parameterValues.length; index++) {
                if (parameterValues[index] != null) {
                    conjunction.add(Restrictions.ne(parameterName, parameterValues[index]));
                } else {
                    conjunction.add(Restrictions.isNotNull(parameterName));
                }
            }
            break;
        }
        }

        if (disjunction != null) {
            criteria.add(disjunction);
        }
        if (conjunction != null) {
            criteria.add(conjunction);
        }
    } else {
        switch (comparator) {
        case SearchParameter.EMPTY_COMPARATOR: {
            criteria.add(Restrictions.isEmpty(parameterName));
            break;
        }
        case SearchParameter.NOT_EMPTY_COMPARATOR: {
            criteria.add(Restrictions.isNotEmpty(parameterName));
            break;
        }
        default: {
            criteria.add(Restrictions.isNull(parameterName));
        }
        }
    }
}

From source file:com.benfante.minimark.blo.UserProfileBo.java

License:Apache License

/**
 * Search all users associated with at least one course.
 *
 * @return The list of all teachers//from   w ww.  j a  v a2s.com
 */
public List<UserProfile> searchAllTeachers() {
    DetachedCriteria crit = DetachedCriteria.forClass(UserProfile.class);
    crit.add(Restrictions.isNotEmpty("courseTeachers"));
    crit.addOrder(Order.asc("name"));
    return userProfileDao.searchByCriteria(crit);
}

From source file:com.bloatit.data.queries.DaoFeatureQuery.java

License:Open Source License

/**
 * Add a WHERE close ensuring that the features have at least one offer.
 */
public void withOffer() {
    add(Restrictions.isNotEmpty(OFFERS));
}

From source file:com.bloatit.data.queries.DaoFeatureQuery.java

License:Open Source License

/**
 * Add a WHERE close ensuring that the features have at least one
 * contribution.
 */
public void hasContributions() {
    add(Restrictions.isNotEmpty(CONTRIBUTION));
}

From source file:com.bloatit.data.queries.DaoMilestoneQuery.java

License:Open Source License

/**
 * Make sure this query will only return milestone having released.
 */
public void withRelease() {
    add(Restrictions.isNotEmpty("releases"));
}

From source file:com.bloatit.data.queries.DaoUserContentQuery.java

License:Open Source License

/**
 * Show content with file.
 */
public void withFile() {
    add(Restrictions.isNotEmpty(FILES));
}