Example usage for org.hibernate.criterion Restrictions conjunction

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

Introduction

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

Prototype

public static Conjunction conjunction() 

Source Link

Document

Group expressions together in a single conjunction (A and B and C...).

Usage

From source file:com.jaspersoft.jasperserver.api.metadata.common.service.impl.hibernate.HibernateRepositoryServiceImpl.java

License:Open Source License

protected DetachedCriteria translateFilterToCriteria(FilterCriteria filterCriteria) {
    Class filterClass = filterCriteria == null ? null : filterCriteria.getFilterClass();
    Class persistentClass;/*w  ww. j a va2s. c  o m*/
    if (filterClass == null) {
        persistentClass = RepoResource.class;
        //persistentClass = RepoOlapUnit.class;    
    } else {
        persistentClass = getPersistentClassMappings().getImplementationClass(filterClass);
    }

    DetachedCriteria criteria;
    if (persistentClass == null) {
        criteria = null;
    } else {
        criteria = DetachedCriteria.forClass(persistentClass);
        criteria.createAlias("parent", "parent");
        criteria.add(Restrictions.eq("parent.hidden", Boolean.FALSE));
        if (filterCriteria != null) {
            List filterElements = filterCriteria.getFilterElements();
            if (!filterElements.isEmpty()) {
                Conjunction conjunction = Restrictions.conjunction();
                HibernateFilter filter = new HibernateFilter(conjunction, this);
                for (Iterator it = filterElements.iterator(); it.hasNext();) {
                    FilterElement filterElement = (FilterElement) it.next();
                    filterElement.apply(filter);
                }
                criteria.add(conjunction);
            }
        }
    }

    return criteria;
}

From source file:com.jaspersoft.jasperserver.api.metadata.common.service.impl.hibernate.util.ResourceCriterionUtils.java

License:Open Source License

/**
 * Creates text criterion which allows to search occurrence of each word of the text in some of the possible
 * targeted properties of the resource. Example:
 * let say we have "Hello World" text to search resources by. In this case each word can be either in label or in
 * description. The SQL for this criterion should be:
 * (lower(label) like "hello" or lower(description) like "hello") &&
 * (lower(label) like "world" or lower(description) like "world")
 *
 * @param text the text to search.//from  w  w w .j a  va 2 s  .c  o m
 * @return text criterion.
 */
public static Criterion getTextCriterion(String text) {
    String[] words = text.trim().split("\\s+");

    Conjunction wordsCriterion = Restrictions.conjunction();

    for (String word : words) {
        Disjunction wordCriterion = Restrictions.disjunction();

        // Each word should be in label or in description.
        wordCriterion.add(new IlikeEscapeAwareExpression("label", word, MatchMode.ANYWHERE));
        wordCriterion.add(new IlikeEscapeAwareExpression("description", word, MatchMode.ANYWHERE));

        // Resource should contain all the words.
        wordsCriterion.add(wordCriterion);
    }

    return wordsCriterion;
}

From source file:com.kodemore.hibernate.criteria.KmAbstractCriteria.java

License:Open Source License

public KmJunction addAnd() {
    Junction j = Restrictions.conjunction();
    _add(j);
    return new KmJunction(this, j);
}

From source file:com.liferay.portal.workflow.jbpm.dao.CustomSession.java

License:Open Source License

protected Criteria buildTaskInstanceExtensionSearchCriteria(String taskName, String assetType,
        Long[] assetPrimaryKeys, Date dueDateGT, Date dueDateLT, Boolean completed, Boolean searchByUserRoles,
        boolean andOperator, ServiceContext serviceContext) throws SystemException {

    Criteria criteria = _session.createCriteria(TaskInstanceExtensionImpl.class);

    criteria.createAlias("taskInstance", "taskInstance");

    criteria.add(Restrictions.eq("companyId", serviceContext.getCompanyId()));

    if (Validator.isNotNull(taskName) || Validator.isNotNull(assetType) || (dueDateGT != null)
            || (dueDateLT != null)) {/*from   w  w w  . j  a v  a2  s  . co  m*/

        Junction junction = null;

        if (andOperator) {
            junction = Restrictions.conjunction();
        } else {
            junction = Restrictions.disjunction();
        }

        if (Validator.isNotNull(taskName)) {
            String[] taskNameKeywords = StringUtil.split(taskName, StringPool.SPACE);

            for (String taskNameKeyword : taskNameKeywords) {
                junction.add(Restrictions.like("taskInstance.name", "%" + taskNameKeyword + "%"));
            }
        }

        if (Validator.isNotNull(assetType)) {
            String[] assetTypeKeywords = StringUtil.split(assetType, StringPool.SPACE);

            for (String assetTypeKeyword : assetTypeKeywords) {
                junction.add(
                        Restrictions.like("workflowContext", "%\"entryType\":\"%" + assetTypeKeyword + "%\"%"));
            }
        }

        if (Validator.isNotNull(assetPrimaryKeys)) {
            for (Long assetPrimaryKey : assetPrimaryKeys) {
                junction.add(Restrictions.like("workflowContext",
                        "%\"entryClassPK\":\"%" + assetPrimaryKey + "%\"%"));
            }
        }

        if (dueDateGT != null) {
            junction.add(Restrictions.ge("taskInstance.dueDate", dueDateGT));
        }

        if (dueDateLT != null) {
            junction.add(Restrictions.lt("taskInstance.dueDate", dueDateGT));
        }

        criteria.add(junction);
    }

    addSearchByUserRolesCriterion(criteria, searchByUserRoles, serviceContext);

    if (completed != null) {
        if (completed.booleanValue()) {
            criteria.add(Restrictions.isNotNull("taskInstance.end"));
        } else {
            criteria.add(Restrictions.isNull("taskInstance.end"));
        }
    }

    return criteria;
}

From source file:com.maydesk.base.dao.DaoRole.java

License:Mozilla Public License

public static List<MUserRole> getRoles(MUser user, MBase context, Session... session2) {
    Session session = null;/* w  w  w.ja va2s  . c  om*/
    if (session2 != null && session2.length > 0) {
        session = session2[0];
    } else {
        session = PDHibernateFactory.getSession();
    }
    Criteria criteria = session.createCriteria(MUserRole.class);
    criteria.add(eq("userRef", user));
    if (context == null) {
        criteria.add(Restrictions.isNull("contextClass"));
    } else {
        Disjunction orCondition = Restrictions.disjunction();
        criteria.add(orCondition);
        Conjunction andCondition = Restrictions.conjunction();
        orCondition.add(andCondition);
        andCondition.add(eq("contextId", context.getId()));
        andCondition.add(eq("contextClass", context.getClass().getCanonicalName()));
        orCondition.add(Restrictions.isNull("contextClass"));

    }
    return criteria.list();
}

From source file:com.nec.harvest.service.impl.PurchaseCategoryServiceImpl.java

License:Open Source License

/**{@inheritDoc}*/
@Override//w ww .  j  av  a2  s.  c o m
public PurchaseCategory findByCtgCode(String ctgCode) throws ServiceException {
    if (StringUtils.isEmpty(ctgCode)) {
        throw new IllegalArgumentException(
                "PurchaseCategory classification (ctgCode) must not be null or empty");
    }

    final Session session = HibernateSessionManager.getSession();
    Transaction tx = null;

    PurchaseCategory purchaseCategory = null;
    try {
        tx = session.beginTransaction();
        Criterion criterion = Restrictions.conjunction().add(Restrictions.eq("ctgCode", ctgCode))
                .add(Restrictions.eq("delKbn", Constants.STATUS_ACTIVE));
        Criteria criteria = repository.getCriteria(session, PurchaseCategory.class);
        criteria.add(criterion);
        List<PurchaseCategory> purchaseCategorys = repository.findByCriteria(criteria);
        // Release transaction
        tx.commit();
        if (CollectionUtils.isEmpty(purchaseCategorys)) {
            throw new ObjectNotFoundException(
                    "Could not find the tighten for PurchaseCategory classification (ctgCode) " + ctgCode);
        }

        if (purchaseCategorys.size() > 1) {
            throw new TooManyObjectsException(
                    "Too many objects are matched with PurchaseCategory classification (ctgCode) " + ctgCode);
        }
        purchaseCategory = purchaseCategorys.get(0);
    } catch (HibernateException ex) {
        if (tx != null) {
            tx.rollback();
        }
        throw new ServiceException("An exception occured while finding a PurchaseCategory that matches with "
                + "PurchaseCategory classification " + ctgCode, ex);
    } finally {
        HibernateSessionManager.closeSession(session);
    }
    return purchaseCategory;
}

From source file:com.nec.harvest.service.impl.TightenServiceImpl.java

License:Open Source License

@Override
public Tighten findByClassifyAndMonth(String shimeKbn) throws ServiceException {
    if (StringUtils.isEmpty(shimeKbn)) {
        throw new IllegalArgumentException("Tighten classification (ShimeKbn) must not be null or empty");
    }/*from  ww  w .  ja  v a2  s  .co  m*/

    final Session session = HibernateSessionManager.getSession();
    Transaction tx = null;

    Tighten tighten = null;
    try {
        tx = session.beginTransaction();
        Criterion criterion = Restrictions.conjunction().add(Restrictions.eq("shimeKbn", shimeKbn))
                .add(Restrictions.eq("delKbn", Constants.STATUS_ACTIVE));
        Criteria criteria = repository.getCriteria(session, Tighten.class);
        criteria.add(criterion);
        List<Tighten> tightens = repository.findByCriteria(criteria);
        // Release transaction
        tx.commit();
        if (CollectionUtils.isEmpty(tightens)) {
            throw new ObjectNotFoundException(
                    "Could not find the tighten for tighten classification (ShimeKbn) " + shimeKbn);
        }

        if (tightens.size() > 1) {
            throw new TooManyObjectsException(
                    "Too many objects are matched with tighten classification (ShimeKbn) " + shimeKbn);
        }

        tighten = tightens.get(0);
    } catch (HibernateException ex) {
        if (tx != null) {
            tx.rollback();
        }
        throw new ServiceException("An exception occured while finding a tighten that matches with "
                + "tighten classification " + shimeKbn, ex);
    } finally {
        HibernateSessionManager.closeSession(session);
    }
    return tighten;
}

From source file:com.nec.harvest.service.impl.VendorServiceImpl.java

License:Open Source License

/**{@inheritDoc}*/
@Override/*from w  w w  . j av a  2s  .  com*/
public Vendor findBySrsCode(String srsCode) throws ServiceException {
    if (StringUtils.isEmpty(srsCode)) {
        throw new IllegalArgumentException("Vendor classification (srsCode) must not be null or empty");
    }

    final Session session = HibernateSessionManager.getSession();
    Transaction tx = null;

    Vendor vendor = null;
    try {
        tx = session.beginTransaction();
        Criterion criterion = Restrictions.conjunction().add(Restrictions.eq("srsCode", srsCode))
                .add(Restrictions.eq("delKbn", Constants.STATUS_ACTIVE));
        Criteria criteria = vendorRepository.getCriteria(session, Vendor.class);
        criteria.add(criterion);
        List<Vendor> vendors = vendorRepository.findByCriteria(criteria);
        // Release transaction
        tx.commit();
        if (CollectionUtils.isEmpty(vendors)) {
            throw new ObjectNotFoundException(
                    "Could not find the Vendor for Vendor classification (srsCode) " + srsCode);
        }

        if (vendors.size() > 1) {
            throw new TooManyObjectsException(
                    "Too many objects are matched with Vendor classification (srsCode) " + srsCode);
        }
        vendor = vendors.get(0);
    } catch (HibernateException ex) {
        if (tx != null) {
            tx.rollback();
        }
        throw new ServiceException("An exception occured while finding a Vendor that matches with "
                + "Vendor classification " + srsCode, ex);
    } finally {
        HibernateSessionManager.closeSession(session);
    }
    return vendor;
}

From source file:com.painiu.core.dao.hibernate.PhotoDAOHibernate.java

License:Open Source License

static Criteria buildPhotoCriteria(final Session session, User user, String[] tags, boolean taggedAll,
        String text, Relation relation, boolean count) {
    Criteria criteria = session.createCriteria(Photo.class);

    if (user != null) {
        criteria.add(Restrictions.eq("user", user));

        if (relation != null) {
            criteria.add(//  ww  w .  j  a  va 2 s . c  o  m
                    Restrictions.sqlRestriction(" {alias}.privacy & ? > 0", relation, UserTypes.relation()));
        }
    } else {
        criteria.add(
                Restrictions.sqlRestriction(" {alias}.privacy & ? > 0", Relation.NONE, UserTypes.relation()));

        criteria.setFetchMode("user", FetchMode.JOIN);
    }

    //if (user == null && group == null) {
    if (user == null) {
        Disjunction disjState = Restrictions.disjunction();

        disjState.add(Restrictions.eq("state", Photo.State.USER_POPULAR));
        disjState.add(Restrictions.eq("state", Photo.State.USER_COMMENDATORY));
        disjState.add(Restrictions.eq("state", Photo.State.USER_SENIOR));

        criteria.add(disjState);
    }

    //if (album != null) {
    //   criteria.createAlias("albumPhotos", "ap");
    //   criteria.add( Restrictions.eq("ap.album", album) );
    //}

    //if (group != null) {
    //   criteria.createAlias("groupPhotos", "gp");
    //   criteria.add( Restrictions.eq("gp.group", group) );
    //}

    if ((tags != null && tags.length > 0) || text != null) {
        Criteria subCriteria = criteria.createCriteria("photoTags", "tags");

        if (tags != null && tags.length > 0) {
            if (taggedAll) {
                Conjunction conj = Restrictions.conjunction();
                for (int i = 0; i < tags.length; i++) {
                    conj.add(Restrictions.eq("tagName", tags[i]));
                }
                subCriteria.add(conj);
            } else {
                Disjunction disj = Restrictions.disjunction();
                for (int i = 0; i < tags.length; i++) {
                    disj.add(Restrictions.eq("tagName", tags[i]));
                }
                subCriteria.add(disj);
            }
        }

        if (text != null) {
            Disjunction disj = Restrictions.disjunction();

            disj.add(Restrictions.like("title", text, MatchMode.ANYWHERE));
            disj.add(Restrictions.like("description", text, MatchMode.ANYWHERE));
            disj.add(Restrictions.eq("tags.tagName", text));

            criteria.add(disj);
        }
    }

    // TODO order parameters
    if (!count) {
        /*if (album != null) {
           criteria.addOrder(Order.asc("ap.position"));
        } else*/
        /*if (group != null) {
           criteria.addOrder(Order.asc("gp.position"));
        } else {*/
        criteria.addOrder(Order.desc("timestamp"));
        //}
    }
    // distinct ?
    if ((tags != null && tags.length > 1) || text != null) {
        ProjectionList proj = Projections.projectionList();

        proj.add(Projections.property("id")).add(Projections.property("title"))
                .add(Projections.property("width")).add(Projections.property("height"))
                .add(Projections.property("address.host")).add(Projections.property("address.dir"))
                .add(Projections.property("address.filename")).add(Projections.property("address.secret"))
                .add(Projections.property("address.username")).add(Projections.property("address.fileKey"));

        if (user == null) {
            criteria.createAlias("user", "user");
            proj.add(Projections.property("user.id")).add(Projections.property("user.username"))
                    .add(Projections.property("user.nickname")).add(Projections.property("user.buddyIcon.host"))
                    .add(Projections.property("user.buddyIcon.dir"))
                    .add(Projections.property("user.buddyIcon.filename"))
                    .add(Projections.property("user.buddyIcon.username"))
                    .add(Projections.property("user.buddyIcon.fileKey"));
        }

        criteria.setProjection(Projections.distinct(proj));

        criteria.setResultTransformer(new PhotoBeanResultTransformer());
    }

    return criteria;
}

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

License:Open Source License

/**
 * Creates restriction which join given restrictions with "AND" operator.
 * // w w w.j  a v a2s.  c o  m
 * @param firstCriterion
 *            first criterion
 * @param secondCriterion
 *            second criterion
 * @param otherCriteria
 *            other criteria
 * @return criterion
 */
public static SearchCriterion and(final SearchCriterion firstCriterion, final SearchCriterion secondCriterion,
        final SearchCriterion... otherCriteria) {
    Conjunction conjunction = Restrictions.conjunction();
    conjunction.add(firstCriterion.getHibernateCriterion());
    conjunction.add(secondCriterion.getHibernateCriterion());

    for (SearchCriterion criterion : otherCriteria) {
        conjunction.add(criterion.getHibernateCriterion());
    }

    return new SearchCriterionImpl(conjunction);
}