private void interpretPagingAndSorting( InterpretationContext context, ObjectQuery query, boolean countingObjects) throws QueryException { RootHibernateQuery hibernateQuery = context.getHibernateQuery(); String rootAlias = hibernateQuery.getPrimaryEntityAlias(); if (query != null && query.getPaging() instanceof ObjectPagingAfterOid) { ObjectPagingAfterOid paging = (ObjectPagingAfterOid) query.getPaging(); if (paging.getOidGreaterThan() != null) { Condition c = hibernateQuery.createSimpleComparisonCondition( rootAlias + ".oid", paging.getOidGreaterThan(), ">"); hibernateQuery.addCondition(c); } } if (!countingObjects && query != null && query.getPaging() != null) { if (query.getPaging() instanceof ObjectPagingAfterOid) { updatePagingAndSortingByOid( hibernateQuery, (ObjectPagingAfterOid) query.getPaging()); // very special case - ascending ordering by OID (nothing more) } else { updatePagingAndSorting(context, query.getPaging()); } } }
protected void updatePagingAndSortingByOid( RootHibernateQuery hibernateQuery, ObjectPagingAfterOid paging) { String rootAlias = hibernateQuery.getPrimaryEntityAlias(); if (paging.getOrderBy() != null || paging.getDirection() != null || paging.getOffset() != null) { throw new IllegalArgumentException( "orderBy, direction nor offset is allowed on ObjectPagingAfterOid"); } hibernateQuery.addOrdering(rootAlias + ".oid", OrderDirection.ASCENDING); if (paging.getMaxSize() != null) { hibernateQuery.setMaxResults(paging.getMaxSize()); } }
public <T extends Containerable> void updatePagingAndSorting( InterpretationContext context, ObjectPaging paging) throws QueryException { if (paging == null) { return; } RootHibernateQuery hibernateQuery = context.getHibernateQuery(); if (paging.getOffset() != null) { hibernateQuery.setFirstResult(paging.getOffset()); } if (paging.getMaxSize() != null) { hibernateQuery.setMaxResults(paging.getMaxSize()); } if (!paging.hasOrdering()) { return; } for (ObjectOrdering ordering : paging.getOrderingInstructions()) { addOrdering(context, ordering); } }
private void addOrdering(InterpretationContext context, ObjectOrdering ordering) throws QueryException { ItemPath orderByPath = ordering.getOrderBy(); // TODO if we'd like to have order-by extension properties, we'd need to provide itemDefinition // for them ProperDataSearchResult<JpaDataNodeDefinition> result = context .getItemPathResolver() .findProperDataDefinition( context.getRootEntityDefinition(), orderByPath, null, JpaDataNodeDefinition.class); if (result == null) { LOGGER.error( "Unknown path '" + orderByPath + "', couldn't find definition for it, " + "list will not be ordered by it."); return; } JpaDataNodeDefinition targetDefinition = result.getLinkDefinition().getTargetDefinition(); if (targetDefinition instanceof JpaAnyContainerDefinition) { throw new QueryException( "Sorting based on extension item or attribute is not supported yet: " + orderByPath); } else if (targetDefinition instanceof JpaReferenceDefinition) { throw new QueryException("Sorting based on reference is not supported: " + orderByPath); } else if (result.getLinkDefinition().isMultivalued()) { throw new QueryException( "Sorting based on multi-valued item is not supported: " + orderByPath); } else if (targetDefinition instanceof JpaEntityDefinition) { throw new QueryException("Sorting based on entity is not supported: " + orderByPath); } else if (!(targetDefinition instanceof JpaPropertyDefinition)) { throw new IllegalStateException("Unknown item definition type: " + result.getClass()); } JpaEntityDefinition baseEntityDefinition = result.getEntityDefinition(); JpaPropertyDefinition orderByDefinition = (JpaPropertyDefinition) targetDefinition; String hqlPropertyPath = context .getItemPathResolver() .resolveItemPath( orderByPath, null, context.getPrimaryEntityAlias(), baseEntityDefinition, true) .getHqlPath(); if (RPolyString.class.equals(orderByDefinition.getJpaClass())) { hqlPropertyPath += ".orig"; } RootHibernateQuery hibernateQuery = context.getHibernateQuery(); if (ordering.getDirection() != null) { switch (ordering.getDirection()) { case ASCENDING: hibernateQuery.addOrdering(hqlPropertyPath, OrderDirection.ASCENDING); break; case DESCENDING: hibernateQuery.addOrdering(hqlPropertyPath, OrderDirection.DESCENDING); break; } } else { hibernateQuery.addOrdering(hqlPropertyPath, OrderDirection.ASCENDING); } }
public RootHibernateQuery interpret( ObjectQuery query, Class<? extends Containerable> type, Collection<SelectorOptions<GetOperationOptions>> options, PrismContext prismContext, boolean countingObjects, Session session) throws QueryException { Validate.notNull(type, "Type must not be null."); Validate.notNull(session, "Session must not be null."); Validate.notNull(prismContext, "Prism context must not be null."); if (LOGGER.isTraceEnabled()) { LOGGER.trace("Interpreting query for type '{}', query:\n{}", new Object[] {type, query}); } InterpretationContext context = new InterpretationContext(this, type, prismContext, session); interpretQueryFilter(context, query); interpretPagingAndSorting(context, query, countingObjects); RootHibernateQuery hibernateQuery = context.getHibernateQuery(); if (countingObjects) { hibernateQuery.addProjectionElement(new ProjectionElement("count(*)")); } else { String rootAlias = hibernateQuery.getPrimaryEntityAlias(); hibernateQuery.addProjectionElement(new ProjectionElement(rootAlias + ".fullObject")); // TODO other objects if parent is requested? if (context.isObject()) { hibernateQuery.addProjectionElement(new ProjectionElement(rootAlias + ".stringsCount")); hibernateQuery.addProjectionElement(new ProjectionElement(rootAlias + ".longsCount")); hibernateQuery.addProjectionElement(new ProjectionElement(rootAlias + ".datesCount")); hibernateQuery.addProjectionElement(new ProjectionElement(rootAlias + ".referencesCount")); hibernateQuery.addProjectionElement(new ProjectionElement(rootAlias + ".polysCount")); hibernateQuery.addProjectionElement(new ProjectionElement(rootAlias + ".booleansCount")); hibernateQuery.setResultTransformer(GetObjectResult.RESULT_TRANSFORMER); } else { hibernateQuery.addProjectionElement(new ProjectionElement(rootAlias + ".ownerOid")); hibernateQuery.setResultTransformer(GetContainerableResult.RESULT_TRANSFORMER); } } return hibernateQuery; }