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:de.decidr.model.commands.user.GetAdministratedWorkflowModelsCommand.java

License:Apache License

@SuppressWarnings("unchecked")
@Override/*from w w w.  j  a  va 2 s. c om*/
public void transactionAllowed(TransactionStartedEvent evt) throws TransactionException {
    result = null;

    // does the user exist? returning an empty list might be ambigous.
    String hql = "select u.id from User u where u.id = :userId";
    Object id = evt.getSession().createQuery(hql).setLong("userId", getUserId()).setMaxResults(1)
            .uniqueResult();

    if (id == null) {
        throw new EntityNotFoundException(User.class, getUserId());
    }

    /*
     * Criteria that represent the following query:
     * 
     * "from WorkflowModel w where w.tenant.id = DEFAULT_TENANT_ID or
     * (exists(from UserAdministratesWorkflowModel rel where
     * rel.workflowModel = w and rel.user.id = :userId) or w.tenant.admin.id
     * = :userId) or exists (from SystemSettings s where s.admin.id =
     * :userId))"
     */
    PaginatingCriteria criteria = new PaginatingCriteria(WorkflowModel.class, "m", evt.getSession());

    /*
     * A user administers a workflow model if there's an explicit
     * relationship.
     */
    DetachedCriteria explicitWorkflowAdminCriteria = DetachedCriteria
            .forClass(UserAdministratesWorkflowModel.class, "rel");
    explicitWorkflowAdminCriteria
            .add(Restrictions.conjunction().add(Restrictions.eqProperty("rel.workflowModel.id", "m.id"))
                    .add(Restrictions.eq("rel.user.id", getUserId())));

    /*
     * A user administers *any* workflow model if he is the super admin.
     */
    DetachedCriteria superAdminCriteria = DetachedCriteria.forClass(SystemSettings.class, "s");
    superAdminCriteria.add(Restrictions.eq("s.superAdmin.id", getUserId()));

    /*
     * Workaround for Hibernate issue HHH-993: Criteria subquery without
     * projection fails throwing NullPointerException.
     * 
     * Additionally, Mysql doesn't seem to like aliases in EXISTS
     * subqueries, so we have to explicitly specify "*"
     */
    explicitWorkflowAdminCriteria.setProjection(Projections.sqlProjection("*", new String[0], new Type[0]));
    superAdminCriteria.setProjection(Projections.sqlProjection("*", new String[0], new Type[0]));

    /*
     * Finally, a user administers a workflow model if he is the tenant
     * admin of the tenant that owns the model. We now add each criterion to
     * a disjuncion.
     */
    criteria.createAlias("tenant", "t");
    Disjunction allAdministrationCriteria = Restrictions.disjunction();
    allAdministrationCriteria.add(Subqueries.exists(superAdminCriteria));
    allAdministrationCriteria.add(Subqueries.exists(explicitWorkflowAdminCriteria));
    allAdministrationCriteria.add(Restrictions.eq("t.admin.id", getUserId()));

    /*
     * Everyone is a workflow admin for models within the default tenant.
     */
    criteria.add(Restrictions.disjunction().add(allAdministrationCriteria)
            .add(Restrictions.eq("m.tenant.id", DecidrGlobals.DEFAULT_TENANT_ID)));

    Filters.apply(criteria, filters, paginator);

    criteria.setResultTransformer(CriteriaSpecification.ROOT_ENTITY);

    result = criteria.list();
}

From source file:de.decidr.model.filters.KeywordFilter.java

License:Apache License

/**
 * {@inheritDoc}//from   ww  w .j  ava 2 s. c  o  m
 */
public void apply(Criteria criteria) {

    if ((properties == null) || (keyword == null)) {
        return;
    }

    Disjunction keywordCrit = Restrictions.disjunction();

    /*
     * where (prop1 like "keyword%") or (prop2 like "keyword%") or (prop3
     * like "keyword%") ...
     */
    for (String property : properties) {
        keywordCrit.add(Restrictions.like(property, keyword + "%"));
    }

    criteria.add(keywordCrit);
}

From source file:de.iteratec.iteraplan.businesslogic.service.InformationSystemReleaseServiceImpl.java

License:Open Source License

/** {@inheritDoc} */
@Override/*from   w ww . j  a  v  a 2s .c  o m*/
public List<InformationSystemRelease> findByNames(Set<String> names) {
    if (names.isEmpty()) {
        return Collections.emptyList();
    }

    DetachedCriteria criteria = DetachedCriteria.forClass(InformationSystemRelease.class);
    criteria.createAlias("informationSystem", "informationSystem");

    Disjunction disjunction = Restrictions.disjunction();
    for (String name : names) {
        String[] partsOfReleaseName = GeneralHelper.getPartsOfReleaseName(name);

        //FIXME will eq do the trick here too or do we need like?
        //if like is needed we should use the IteraplanLikeExpression
        //      SimpleExpression nameExpression = Restrictions.like("informationSystem.name", partsOfReleaseName[0], MatchMode.EXACT).ignoreCase();
        Criterion nameExpression = new IteraplanLikeExpression("informationSystem.name", partsOfReleaseName[0],
                true);
        String version = "version";
        if (partsOfReleaseName[1] != null) {
            //FIXME will eq do the trick here too or do we need like?
            //if like is needed we should use the IteraplanLikeExpression
            //        SimpleExpression versionExpression = Restrictions.like(version, partsOfReleaseName[1], MatchMode.EXACT).ignoreCase();
            Criterion versionExpression = new IteraplanLikeExpression(version, partsOfReleaseName[1], true);
            disjunction.add(Restrictions.and(nameExpression, versionExpression));
        } else {
            Criterion versionExpression = Restrictions.or(Restrictions.isNull(version),
                    Restrictions.eq(version, Constants.DB_NU1L));
            disjunction.add(Restrictions.and(nameExpression, versionExpression));
        }
    }
    criteria.add(disjunction);

    return informationSystemReleaseDAO.findByCriteria(criteria);
}

From source file:de.iteratec.iteraplan.businesslogic.service.TechnicalComponentReleaseServiceImpl.java

License:Open Source License

/** {@inheritDoc} */
@Override/*from   w  ww . j  a  v  a 2  s .c  o m*/
public List<TechnicalComponentRelease> findByNames(Set<String> names) {
    if (names.isEmpty()) {
        return Collections.emptyList();
    }

    DetachedCriteria criteria = DetachedCriteria.forClass(TechnicalComponentRelease.class);
    criteria.createAlias("technicalComponent", "technicalComponent");

    Disjunction disjunction = Restrictions.disjunction();
    for (String name : names) {
        String[] partsOfReleaseName = GeneralHelper.getPartsOfReleaseName(name);

        //FIXME will eq do the trick here too or do we need like?
        //if like is needed we should use the IteraplanLikeExpression
        //      SimpleExpression nameExpression = Restrictions.like("technicalComponent.name", partsOfReleaseName[0], MatchMode.EXACT).ignoreCase();
        Criterion nameExpression = new IteraplanLikeExpression("technicalComponent.name", partsOfReleaseName[0],
                true);
        if (partsOfReleaseName[1] != null) {
            //FIXME will eq do the trick here too or do we need like?
            //if like is needed we should use the IteraplanLikeExpression
            //        SimpleExpression versionExpression = Restrictions.like(VERSION, partsOfReleaseName[1], MatchMode.EXACT).ignoreCase();
            Criterion versionExpression = new IteraplanLikeExpression(VERSION, partsOfReleaseName[1], true);
            disjunction.add(Restrictions.and(nameExpression, versionExpression));
        } else {
            Criterion versionExpression = Restrictions.or(Restrictions.isNull(VERSION),
                    Restrictions.eq(VERSION, Constants.DB_NU1L));
            disjunction.add(Restrictions.and(nameExpression, versionExpression));
        }
    }
    criteria.add(disjunction);

    return technicalComponentReleaseDAO.findByCriteria(criteria);
}

From source file:de.iteratec.iteraplan.persistence.dao.BusinessMappingDAOImpl.java

License:Open Source License

/** {@inheritDoc} */
public int deleteOrphanedBusinessMappings() {
    Criterion isrNull = Restrictions.isNull("informationSystemRelease.id");
    Criterion buNull = Restrictions.isNull("businessUnit.id");
    Criterion bpNull = Restrictions.isNull("businessProcess.id");
    Criterion prodNull = Restrictions.isNull("product.id");
    Junction invalidBusinessMappings = Restrictions.disjunction().add(isrNull).add(buNull).add(bpNull)
            .add(prodNull);/*from w  w w.java 2 s.  c o m*/

    List<BusinessMapping> orphanedMappings = getBusinessMappings(invalidBusinessMappings);
    for (BusinessMapping orphan : orphanedMappings) {
        delete(orphan);
    }

    return orphanedMappings.size();
}

From source file:de.iteratec.iteraplan.persistence.dao.GenericBaseDAO.java

License:Open Source License

/** {@inheritDoc} */
public List<E> findByNames(Set<String> names) {
    if (names.isEmpty()) {
        return Collections.emptyList();
    }//from  www.j av  a 2  s.  c om

    DetachedCriteria criteria = DetachedCriteria.forClass(getPersistentClass());
    Disjunction disjunction = Restrictions.disjunction();
    for (String name : names) {
        //FIXME will eq do the trick here too or do we need like?
        //if like is needed we should use the IteraplanLikeExpression
        //      disjunction.add(Restrictions.like(getNameAttribute(), name, MatchMode.EXACT).ignoreCase());
        disjunction.add(new IteraplanLikeExpression(getNameAttribute(), name, true));
    }
    criteria.add(disjunction);

    return findByCriteria(criteria);
}

From source file:de.iteratec.iteraplan.persistence.util.CriteriaUtil.java

License:Open Source License

public static <T> Criterion createInRestrictions(String propName, final Iterable<T> elements) {
    final Iterable<List<T>> partitions = Iterables.partition(elements, Constants.DB_ORACLE_MAX_IN);
    final Disjunction disjunction = Restrictions.disjunction();
    for (List<T> partition : partitions) {
        disjunction.add(Restrictions.in(propName, partition));
    }//from  w ww  .  ja v a2s . c o m

    return disjunction;
}

From source file:de.iteratec.iteraplan.persistence.util.CriteriaUtil.java

License:Open Source License

public static <T> Criterion createNotInRestrictions(String propName, final Iterable<T> elements) {
    final Iterable<List<T>> partitions = Iterables.partition(elements, Constants.DB_ORACLE_MAX_IN);
    final Disjunction disjunction = Restrictions.disjunction();
    for (List<T> partition : partitions) {
        disjunction.add(Restrictions.in(propName, partition));
    }//from w  w w.  j a  v  a 2s. com

    return Restrictions.not(disjunction);
}

From source file:de.lemo.apps.integration.CourseDAOImpl.java

License:Open Source License

public List<Course> searchCourses(final List<String> searchStrings) {
    List<Long> searchLongs = new ArrayList<Long>();
    for (String s : searchStrings) {
        try {//from   www .j  a  v a 2s . co m
            searchLongs.add(Long.valueOf(s).longValue());
        } catch (NumberFormatException e) {

        }
    }
    Criteria criteria = this.session.createCriteria(Course.class);

    Junction res = Restrictions.disjunction();
    for (String s : searchStrings) {
        res.add(Restrictions.like("courseName", "%" + s + "%"));
        res.add(Restrictions.like("courseDescription", "%" + s + "%"));
    }
    if (!searchLongs.isEmpty())
        res.add(Restrictions.in("courseId", searchLongs));
    criteria.add(res);
    final List<Course> results = criteria.list();
    if (results.size() == 0) {
        return new ArrayList<Course>();
    }
    return results;
}

From source file:de.sub.goobi.forms.ProzesskopieForm.java

License:Open Source License

/**
 * Get Process templates.//from   ww w. j a  v  a2  s.com
 *
 * @return list of SelectItem objects
 */
public List<SelectItem> getProzessTemplates() throws DAOException {
    List<SelectItem> myProzessTemplates = new ArrayList<SelectItem>();
    Session session = Helper.getHibernateSession();
    Criteria crit = session.createCriteria(Process.class);
    crit.add(Restrictions.eq("template", Boolean.FALSE));
    crit.add(Restrictions.eq("inChoiceListShown", Boolean.TRUE));
    crit.addOrder(Order.asc("title"));

    /* Einschrnkung auf bestimmte Projekte, wenn kein Admin */
    LoginForm loginForm = (LoginForm) Helper.getManagedBeanValue("#{LoginForm}");
    User aktuellerNutzer = loginForm.getMyBenutzer();
    try {
        aktuellerNutzer = serviceManager.getUserService().find(loginForm.getMyBenutzer().getId());
    } catch (DAOException e) {
        myLogger.error(e);
    }
    if (aktuellerNutzer != null) {
        /*
         * wenn die maximale Berechtigung nicht Admin ist, dann nur
         * bestimmte
         */
        if (loginForm.getMaximaleBerechtigung() > 1) {
            Hibernate.initialize(aktuellerNutzer);
            Disjunction dis = Restrictions.disjunction();
            for (Project proj : aktuellerNutzer.getProjects()) {
                dis.add(Restrictions.eq("project", proj));
            }
            crit.add(dis);
        }
    }

    for (Object proz : crit.list()) {
        myProzessTemplates.add(new SelectItem(((Process) proz).getId(), ((Process) proz).getTitle(), null));
    }
    return myProzessTemplates;
}