Example usage for org.hibernate.criterion Restrictions disjunction

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

Introduction

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

Prototype

public static Disjunction disjunction() 

Source Link

Document

Group expressions together in a single disjunction (A or B or C...).

Usage

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

License:Open Source License

@SuppressWarnings("unchecked")
public List<Study> getParentStudyList() {
    List<Study> studyList = new ArrayList<Study>(0);

    // Restrict on study criteria (by default, NOT 'Archive' status)
    Criteria studyCriteria = getSession().createCriteria(Study.class);
    StudyStatus status;// ww  w.  ja  v  a2  s .co  m
    try {
        status = getStudyStatus("Archive");
        studyCriteria.add(Restrictions.ne("studyStatus", status));
    } catch (StatusNotAvailableException e) {
        log.error(e.getMessage(), e);
    }

    // Can only select studies with null parent, or where the study is a parent
    studyCriteria.add(Restrictions.disjunction().add(Restrictions.isNull("parentStudy"))
            .add(Restrictions.eqProperty("parentStudy.id", "id")));
    studyCriteria.addOrder(Order.asc("name"));
    studyList = studyCriteria.list();
    return studyList;
}

From source file:au.org.theark.study.model.dao.StudyDao.java

License:Open Source License

public List<Correspondences> getCorrespondenceList(LinkSubjectStudy lss, Correspondences correspondence)
        throws ArkSystemException {

    Criteria criteria = getSession().createCriteria(Correspondences.class, "co");
    criteria.createAlias("lss", "lss", JoinType.LEFT_OUTER_JOIN);
    //criteria.createAlias("study", "st", JoinType.LEFT_OUTER_JOIN);
    //criteria.createAlias("st.parentStudy", "pstudy", JoinType.LEFT_OUTER_JOIN);

    if (lss != null) {
        criteria.add(Restrictions.eq("lss", lss));
    }//from ww  w .  j  av  a 2s .co  m

    if (correspondence != null) {

        // Check context study is match with correspondence study or it's parent study
        if (correspondence.getLss() != null && correspondence.getLss().getStudy() != null) {
            criteria.add(Restrictions.disjunction()
                    .add(Restrictions.eq("lss.study", correspondence.getLss().getStudy())));//.add(Restrictions.eq("pstudy", correspondence.getLss().getStudy())));
        }
        if (correspondence.getCorrespondenceDirectionType() != null) {
            criteria.add(Restrictions.eq("co.correspondenceDirectionType",
                    correspondence.getCorrespondenceDirectionType()));
        }
        if (correspondence.getCorrespondenceModeType() != null) {
            criteria.add(
                    Restrictions.eq("co.correspondenceModeType", correspondence.getCorrespondenceModeType()));
        }
        if (correspondence.getCorrespondenceOutcomeType() != null) {
            criteria.add(Restrictions.eq("co.correspondenceOutcomeType",
                    correspondence.getCorrespondenceOutcomeType()));
        }
        if (correspondence.getDate() != null) {
            criteria.add(Restrictions.eq("co.date", correspondence.getDate()));
        }
        if (correspondence.getTime() != null) {
            criteria.add(Restrictions.eq("co.time", correspondence.getTime()));
        }
        if (correspondence.getDetails() != null) {
            criteria.add(Restrictions.ilike("co.details", correspondence.getDetails(), MatchMode.ANYWHERE));
        }
        if (correspondence.getReason() != null) {
            criteria.add(Restrictions.ilike("co.reason", correspondence.getDetails(), MatchMode.ANYWHERE));
        }
        if (correspondence.getComments() != null) {
            criteria.add(Restrictions.ilike("co.comments", correspondence.getComments(), MatchMode.ANYWHERE));
        }
        if (correspondence.getOperator() != null) {
            criteria.add(Restrictions.eq("co.operator", correspondence.getOperator()));
        }
    }

    List<Correspondences> personCorrespondenceList = criteria.list();
    return personCorrespondenceList;
}

From source file:au.org.theark.study.model.dao.StudyDao.java

License:Open Source License

public List<LinkSubjectTwin> getTwins(final Set<String> subjectUids, final Long studyId) {
    List<LinkSubjectTwin> twins = new ArrayList<LinkSubjectTwin>();
    Criteria criteria = getSession().createCriteria(LinkSubjectTwin.class, "lst");
    criteria.createAlias("firstSubject", "lssa", JoinType.INNER_JOIN);
    criteria.createAlias("lssa.study", "sta", JoinType.INNER_JOIN);
    criteria.createAlias("secondSubject", "lssb", JoinType.INNER_JOIN);
    criteria.createAlias("lssb.study", "stb", JoinType.INNER_JOIN);

    criteria.setFetchMode("firstSubject", FetchMode.JOIN);
    criteria.setFetchMode("secondSubject", FetchMode.JOIN);
    criteria.setFetchMode("twinType", FetchMode.JOIN);

    criteria.add(Restrictions.eq("sta.id", studyId));
    criteria.add(Restrictions.eq("stb.id", studyId));
    Disjunction or = Restrictions.disjunction();
    or.add(Restrictions.in("lssa.subjectUID", subjectUids));
    or.add(Restrictions.in("lssb.subjectUID", subjectUids));
    criteria.add(or);// w w w.j  a  v  a2 s  . c  om

    twins = criteria.list();

    return twins;

}

From source file:au.org.theark.study.model.dao.StudyDao.java

License:Open Source License

public long getRelationshipCount(final String subjectUID, final Long studyId) {
    long count = 0;
    Criteria criteria = getSession().createCriteria(LinkSubjectPedigree.class, "lsp");
    criteria.createAlias("subject", "sub", JoinType.INNER_JOIN);
    criteria.createAlias("relative", "rel", JoinType.INNER_JOIN);
    criteria.createAlias("sub.study", "substudy", JoinType.INNER_JOIN);
    criteria.createAlias("rel.study", "relstudy", JoinType.INNER_JOIN);
    criteria.add(Restrictions.eq("substudy.id", studyId));
    criteria.add(Restrictions.eq("relstudy.id", studyId));
    Disjunction or = Restrictions.disjunction();
    or.add(Restrictions.eq("sub.subjectUID", subjectUID));
    or.add(Restrictions.eq("rel.subjectUID", subjectUID));
    criteria.add(or);/*from w  w w .ja  va 2s.c o m*/
    ProjectionList projList = Projections.projectionList();
    projList.add(Projections.count("lsp.id"));
    criteria.setProjection(projList);
    List list = criteria.list();
    if (list.size() > 0) {
        count = Integer.parseInt(list.get(0).toString());
    }
    return count;
}

From source file:br.com.itw.qopsearch.api.persistence.core.FeatureProductRepositoryImpl.java

License:Apache License

@Override
public Page<ProductFeature> searchText(String text, Pageable pageable) {
    if (pageable == null) {
        pageable = PageableHelper.deafultPageable();
    }//w  ww .java 2s .c  om
    Session session = (Session) entityManager.getDelegate();
    Criteria criteria = session.createCriteria(ProductFeature.class);
    criteria.add(Restrictions.disjunction());
    return (Page<ProductFeature>) PageableHelper.getPage(criteria, pageable);
}

From source file:br.com.itw.qopsearch.api.persistence.core.FeatureRepositoryImpl.java

License:Apache License

@Override
public Page<Feature> searchText(String text, Pageable pageable) {
    if (pageable == null) {
        pageable = PageableHelper.deafultPageable();
    }/*from   ww  w .  j  a v a 2 s.  co m*/
    Session session = (Session) entityManager.getDelegate();
    Criteria criteria = session.createCriteria(Feature.class);
    criteria.add(Restrictions.disjunction().add(Restrictions.ilike("name", text, MatchMode.ANYWHERE)));
    return (Page<Feature>) PageableHelper.getPage(criteria, pageable);
}

From source file:br.com.itw.qopsearch.api.persistence.core.ProductRepositoryImpl.java

License:Apache License

@Override
public Page<Product> searchText(String text, Pageable pageable) {
    if (pageable == null) {
        pageable = PageableHelper.deafultPageable();
    }//from w w  w  .  j a  v  a  2  s.  co  m
    Session session = (Session) entityManager.getDelegate();
    Criteria criteria = session.createCriteria(Product.class);
    criteria.add(Restrictions.disjunction().add(Restrictions.ilike("description", text, MatchMode.ANYWHERE))
            .add(Restrictions.ilike("image", text, MatchMode.ANYWHERE)));
    return (Page<Product>) PageableHelper.getPage(criteria, pageable);
}

From source file:cn.hxh.springside.orm.hibernate.HibernateDao.java

License:Apache License

/**
 * ?Criterion,./*  ww  w  .j a v a  2  s  .  c  om*/
 */
protected Criterion[] buildCriterionByPropertyFilter(final List<PropertyFilter> filters) {
    List<Criterion> criterionList = new ArrayList<Criterion>();
    for (PropertyFilter filter : filters) {
        if (!filter.hasMultiProperties()) { //??.
            Criterion criterion = buildCriterion(filter.getPropertyName(), filter.getMatchValue(),
                    filter.getMatchType());
            criterionList.add(criterion);
        } else {//??,or?.
            Disjunction disjunction = Restrictions.disjunction();
            for (String param : filter.getPropertyNames()) {
                Criterion criterion = buildCriterion(param, filter.getMatchValue(), filter.getMatchType());
                disjunction.add(criterion);
            }
            criterionList.add(disjunction);
        }
    }
    return criterionList.toArray(new Criterion[criterionList.size()]);
}

From source file:cn.newtouch.util.hibernate.HibernateDao.java

License:Apache License

/**
 * ?Criterion,./* www. j a  v a 2s.  co m*/
 */
protected Criterion[] buildCriterionByPropertyFilter(final List<PropertyFilter> filters) {
    List<Criterion> criterionList = new ArrayList<Criterion>();
    for (PropertyFilter filter : filters) {
        if (!filter.hasMultiProperties()) { // ??.
            Criterion criterion = buildCriterion(filter.getPropertyName(), filter.getMatchValue(),
                    filter.getMatchType());
            criterionList.add(criterion);
        } else {// ??,or?.
            Disjunction disjunction = Restrictions.disjunction();
            for (String param : filter.getPropertyNames()) {
                Criterion criterion = buildCriterion(param, filter.getMatchValue(), filter.getMatchType());
                disjunction.add(criterion);
            }
            criterionList.add(disjunction);
        }
    }
    return criterionList.toArray(new Criterion[criterionList.size()]);
}

From source file:com.abiquo.abiserver.commands.UserCommand.java

License:Mozilla Public License

/**
 * Returns a list of users stored in the Data Base
 * //  www .j  a  va 2 s  .c  om
 * @param userSession
 * @param userListOptions an UserListOptions object containing the options to retrieve the list
 *            of users
 * @return A DataResult object containing an UserListResult object with an ArrayList of User and
 *         the number of total users
 */
@SuppressWarnings("unchecked")
protected DataResult<UserListResult> getUsers(UserSession userSession, UserListOptions userListOptions) {
    DataResult<UserListResult> dataResult = new DataResult<UserListResult>();
    UserListResult userListResult = new UserListResult();

    Session session = null;
    Transaction transaction = null;

    try {
        session = HibernateUtil.getSession();
        transaction = session.beginTransaction();

        // Getting the user that called this method
        UserHB userHB = (UserHB) session.createCriteria(UserHB.class)
                .add(Restrictions.eq("user", userSession.getUser())).uniqueResult();

        // getUsers has two different authorization resources
        // If the user who called this method, does not match the security level for the
        // authorization resource
        // USER_GET_ALL_USERS, this method will only return those users who belongs to the same
        // enterprise than
        // the user that called this method

        // Getting the authorization resource USER_GET_ALL_USERS
        AuthResourceHB authResourceHB = (AuthResourceHB) session.createCriteria(AuthResourceHB.class)
                .add(Restrictions.eq("name", "USER_GET_ALL_USERS")).uniqueResult();

        // Checking if the user has not the necessary security level to see the whole list of
        // Users. If so, we force
        // the userListOptions object to filter by the enterprise assigned to the user
        if (authResourceHB.getRoleHB().getSecurityLevel()
                .compareTo(userHB.getRoleHB().getSecurityLevel()) == -1) {
            // NOT GRANTED!
            userListOptions.setByEnterprise((Enterprise) userHB.getEnterpriseHB().toPojo());
        }

        // Creating users criteria
        Criteria usersListCriteria = session.createCriteria(UserHB.class);
        Criteria usersCountCriteria = session.createCriteria(UserHB.class);

        // Removing the users that are deleted
        usersListCriteria.add(Restrictions.eq("deleted", 0));
        usersCountCriteria.add(Restrictions.eq("deleted", 0));

        // Adding filter by name, surname or email
        Disjunction filterDisjunction = Restrictions.disjunction();
        if (userListOptions.getFilter().length() > 0) {
            filterDisjunction.add(Restrictions.like("name", '%' + userListOptions.getFilter() + '%'));
            filterDisjunction.add(Restrictions.like("surname", '%' + userListOptions.getFilter() + '%'));
            filterDisjunction.add(Restrictions.like("email", '%' + userListOptions.getFilter() + '%'));
        }
        usersListCriteria.add(filterDisjunction);
        usersCountCriteria.add(filterDisjunction);

        // Adding filter by Enterprise
        if (userListOptions.getByEnterprise() != null) {
            usersListCriteria.add(Restrictions.eq("enterpriseHB",
                    (EnterpriseHB) userListOptions.getByEnterprise().toPojoHB()));
            usersCountCriteria.add(Restrictions.eq("enterpriseHB",
                    (EnterpriseHB) userListOptions.getByEnterprise().toPojoHB()));
        }

        // Adding order
        // Little fix to match the object used in Hibernate
        if (userListOptions.getOrderBy().compareTo("role") == 0)
            userListOptions.setOrderBy("roleHB");

        Order order;
        if (userListOptions.getAsc())
            order = Order.asc(userListOptions.getOrderBy());
        else
            order = Order.desc(userListOptions.getOrderBy());

        usersListCriteria.addOrder(order);
        usersCountCriteria.addOrder(order);

        // Adding filter to get only the users that currently have an active session
        if (userListOptions.getLoggedOnly()) {
            ArrayList<String> currentLoggedUsers = (ArrayList<String>) session
                    .createSQLQuery("SELECT user FROM session WHERE expireDate > CURRENT_TIMESTAMP()").list();
            usersListCriteria.add(Restrictions.in("user", currentLoggedUsers));
            usersCountCriteria.add(Restrictions.in("user", currentLoggedUsers));
        }

        // Once we have the criteria...
        // 1. Getting the total number of users that match userListOptions
        Integer totalUsers = (Integer) usersCountCriteria.setProjection(Projections.rowCount()).uniqueResult();

        // 2. Getting the list of users, applying an offset and a max of results
        ArrayList<UserHB> usersHB = (ArrayList<UserHB>) usersListCriteria
                .setFirstResult(userListOptions.getOffset()).setMaxResults(userListOptions.getLength()).list();

        // Building result
        ArrayList<User> usersList = new ArrayList<User>();
        for (UserHB userToReturnHB : usersHB) {
            usersList.add((User) userToReturnHB.toPojo());
        }

        userListResult.setTotalUsers(totalUsers);
        userListResult.setUsersList(usersList);

        transaction.commit();
    } catch (Exception e) {
        if (transaction != null && transaction.isActive())
            transaction.rollback();

        this.errorManager.reportError(resourceManager, dataResult, "getUsers", e);

        return dataResult;
    }

    dataResult.setData(userListResult);
    dataResult.setSuccess(true);
    dataResult.setMessage(UserCommand.resourceManager.getMessage("getUsers.success"));

    return dataResult;
}