List of usage examples for org.hibernate.criterion DetachedCriteria setProjection
public DetachedCriteria setProjection(Projection projection)
From source file:de.decidr.model.commands.tenant.GetUsersOfTenantCommand.java
License:Apache License
@SuppressWarnings("unchecked") @Override/*from w w w.j ava 2 s . com*/ public void transactionAllowed(TransactionStartedEvent evt) throws TransactionException { PaginatingCriteria c = new PaginatingCriteria(User.class, "u", evt.getSession()); String rootAlias = c.getAlias(); // Create criterion "user is a member of the tenant" DetachedCriteria memberRel = DetachedCriteria.forClass(UserIsMemberOfTenant.class, "memberRel"); memberRel.add(Property.forName("memberRel.user.id").eqProperty(rootAlias + ".id")) .add(Property.forName("memberRel.tenant.id").eq(getTenantId())); // Create criterion "user is the administrator of the tenant" DetachedCriteria adminRel = DetachedCriteria.forClass(Tenant.class, "adminTenant"); adminRel.add(Property.forName("adminTenant.id").eq(getTenantId())) .add(Property.forName("adminTenant.admin.id").eqProperty(rootAlias + ".id")); /* * 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 "*" */ Projection existsSubqueryProjection = Projections.sqlProjection("*", new String[0], new Type[0]); memberRel.setProjection(existsSubqueryProjection); adminRel.setProjection(existsSubqueryProjection); c.add(Restrictions.or(Subqueries.exists(memberRel), Subqueries.exists(adminRel))); // preload user profiles - no lazy loading desired c.createCriteria("userProfile", CriteriaSpecification.LEFT_JOIN); c.setResultTransformer(CriteriaSpecification.ROOT_ENTITY); if (paginator != null) { paginator.apply(c); } result = c.list(); }
From source file:de.decidr.model.commands.user.GetAdministratedWorkflowModelsCommand.java
License:Apache License
@SuppressWarnings("unchecked") @Override//from w ww.j av a2 s . co m 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.StartableWorkflowModelFilter.java
License:Apache License
/** * {@inheritDoc}/*from w ww .j a v a 2s . co m*/ */ public void apply(Criteria criteria) { DetachedCriteria exists = DetachedCriteria.forClass(StartableWorkflowModelView.class, "startableFilter"); exists.add(Restrictions.eqProperty(exists.getAlias() + ".id", criteria.getAlias() + ".id")); /* * 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 "*" */ exists.setProjection(Projections.sqlProjection("*", new String[0], new Type[0])); criteria.add(Subqueries.exists(exists)); }
From source file:de.escidoc.core.aa.business.filter.RoleFilter.java
License:Open Source License
/** * Evaluate a CQL term node./* w w w . j a v a 2s. com*/ * * @param node CQL node * @return Hibernate query reflecting the given CQL query * @throws InvalidSearchQueryException thrown if the given search query could not be translated into a SQL query */ @Override protected Criterion evaluate(final CQLTermNode node) throws InvalidSearchQueryException { Criterion result = null; final Object[] parts = criteriaMap.get(node.getIndex()); final String value = node.getTerm(); if (parts != null && !specialCriteriaNames.contains(node.getIndex())) { result = evaluate(node.getRelation(), (String) parts[1], value, (Integer) parts[0] == COMPARE_LIKE); } else { final String columnName = node.getIndex(); if (columnName != null) { if ("limited".equals(columnName)) { result = Boolean.parseBoolean(value) ? Restrictions.isNotEmpty("scopeDefs") : Restrictions.isEmpty("scopeDefs"); } else if ("granted".equals(columnName)) { final DetachedCriteria subQuery = DetachedCriteria.forClass(RoleGrant.class, "rg"); subQuery.setProjection(Projections.rowCount()); subQuery.add(Restrictions.eqProperty("escidocRole.id", "r.id")); result = Boolean.parseBoolean(value) ? Subqueries.lt(0L, subQuery) : Subqueries.eq(0L, subQuery); } else if (columnName.equals(Constants.FILTER_CREATION_DATE) || columnName.equals(Constants.FILTER_PATH_CREATION_DATE)) { result = evaluate(node.getRelation(), "creationDate", value != null && value.length() > 0 ? new Date(new DateTime(value).getMillis()) : null, false); } else { throw new InvalidSearchQueryException("unknown filter criteria: " + columnName); } } } return result; }
From source file:de.escidoc.core.aa.business.persistence.hibernate.HibernateEscidocRoleDao.java
License:Open Source License
/** * See Interface for functional description. * * @see EscidocRoleDaoInterface #retrieveRoles(java.util.Map, int, int, java.lang.String, * de.escidoc.core.common.util.list.ListSorting) *//*from w w w. j a v a 2 s.co m*/ @Override public List<EscidocRole> retrieveRoles(final Map<String, Object> criterias, final int offset, final int maxResults, final String orderBy, final ListSorting sorting) throws SqlDatabaseSystemException { final DetachedCriteria detachedCriteria = DetachedCriteria.forClass(EscidocRole.class, "r"); detachedCriteria.add(Restrictions.ne("id", EscidocRole.DEFAULT_USER_ROLE_ID)); if (criterias != null && !criterias.isEmpty()) { // ids final Set<String> roleIds = mergeSets((Set<String>) criterias.remove(Constants.DC_IDENTIFIER_URI), (Set<String>) criterias.remove(Constants.FILTER_PATH_ID)); if (roleIds != null && !roleIds.isEmpty()) { detachedCriteria.add(Restrictions.in("id", roleIds.toArray())); } // limited final String limited = (String) criterias.remove("limited"); if (limited != null) { if (Boolean.parseBoolean(limited)) { detachedCriteria.add(Restrictions.isNotEmpty("scopeDefs")); } else { detachedCriteria.add(Restrictions.isEmpty("scopeDefs")); } } // granted final String granted = (String) criterias.remove("granted"); if (granted != null) { final DetachedCriteria subQuery = DetachedCriteria.forClass(RoleGrant.class, "rg"); subQuery.setProjection(Projections.rowCount()); subQuery.add(Restrictions.eqProperty("escidocRole.id", "r.id")); if (Boolean.parseBoolean(granted)) { detachedCriteria.add(Subqueries.lt(0, subQuery)); } else { detachedCriteria.add(Subqueries.eq(0, subQuery)); } } for (final Entry<String, Object[]> stringEntry : criteriaMap.entrySet()) { final Object criteriaValue = criterias.remove(stringEntry.getKey()); if (criteriaValue != null) { final Object[] parts = stringEntry.getValue(); if (parts[0].equals(COMPARE_EQ)) { detachedCriteria.add(Restrictions.eq((String) parts[1], criteriaValue)); } else { detachedCriteria.add(Restrictions.like((String) parts[1], criteriaValue)); } } } } if (orderBy != null) { if (sorting == ListSorting.ASCENDING) { detachedCriteria.addOrder(Order.asc(propertiesNamesMap.get(orderBy))); } else if (sorting == ListSorting.DESCENDING) { detachedCriteria.addOrder(Order.desc(propertiesNamesMap.get(orderBy))); } } if (criterias != null && criterias.isEmpty()) { final List<EscidocRole> result; try { result = getHibernateTemplate().findByCriteria(detachedCriteria, offset, maxResults); } catch (final DataAccessException e) { throw new SqlDatabaseSystemException(e); } return result; } else { // unsupported filter criteria has been found, therefore the result // list must be empty. return new ArrayList<EscidocRole>(0); } }
From source file:de.forsthaus.backend.dao.impl.BrancheDAOImpl.java
License:Open Source License
@Override public int getBrancheSize() { DetachedCriteria criteria = DetachedCriteria.forClass(Branche.class); criteria.setProjection(Projections.rowCount()); return DataAccessUtils.intResult(getHibernateTemplate().findByCriteria(criteria)); }
From source file:de.forsthaus.backend.dao.impl.CustomerDAOImpl.java
License:Open Source License
@Override public int getMaxCustomerId() { DetachedCriteria criteria = DetachedCriteria.forClass(Customer.class); criteria.setProjection(Projections.projectionList().add(Projections.max("id"))); long max = DataAccessUtils.longResult(getHibernateTemplate().findByCriteria(criteria)); return (int) max; }
From source file:de.forsthaus.backend.dao.impl.OrderDAOImpl.java
License:Open Source License
@SuppressWarnings("unchecked") @Override//from w w w. ja va2 s .co m public BigDecimal getOrderSum(Order order) { DetachedCriteria criteria = DetachedCriteria.forClass(Orderposition.class); criteria.add(Restrictions.eq("order", order)); criteria.setProjection(Projections.sum("aupGesamtwert")); BigDecimal sumResult = (BigDecimal) DataAccessUtils .uniqueResult(getHibernateTemplate().findByCriteria(criteria)); return sumResult; }
From source file:de.forsthaus.backend.dao.impl.OrderpositionDAOImpl.java
License:Open Source License
@Override public int getCountOrderpositionsByOrder(Order order) { DetachedCriteria criteria = DetachedCriteria.forClass(Orderposition.class); criteria.add(Restrictions.eq("order", order)); criteria.setProjection(Projections.rowCount()); return DataAccessUtils.intResult(getHibernateTemplate().findByCriteria(criteria)); }
From source file:de.forsthaus.backend.dao.impl.SecGrouprightDAOImpl.java
License:Open Source License
@Override public boolean isRightInGroup(SecRight right, SecGroup group) { DetachedCriteria criteria = DetachedCriteria.forClass(SecGroupright.class); criteria.add(Restrictions.eq("secGroup", group)); criteria.add(Restrictions.eq("secRight", right)); criteria.setProjection(Projections.rowCount()); int count = DataAccessUtils.intResult(getHibernateTemplate().findByCriteria(criteria)); return count > 0; }