List of usage examples for javax.persistence.criteria CriteriaBuilder max
<N extends Number> Expression<N> max(Expression<N> x);
From source file:org.apache.ambari.server.orm.dao.ServiceConfigDAO.java
@RequiresSession public List<ServiceConfigEntity> getLastServiceConfigVersionsForGroups(Collection<Long> configGroupIds) { if (configGroupIds == null || configGroupIds.isEmpty()) { return Collections.emptyList(); }/*from w ww . java 2s.co m*/ CriteriaBuilder cb = entityManagerProvider.get().getCriteriaBuilder(); CriteriaQuery<Tuple> cq = cb.createTupleQuery(); Root<ServiceConfigEntity> groupVersion = cq.from(ServiceConfigEntity.class); cq.multiselect(groupVersion.get("groupId").alias("groupId"), cb.max(groupVersion.<Long>get("version")).alias("lastVersion")); cq.where(groupVersion.get("groupId").in(configGroupIds)); cq.groupBy(groupVersion.get("groupId")); List<Tuple> tuples = daoUtils.selectList(entityManagerProvider.get().createQuery(cq)); List<ServiceConfigEntity> result = new ArrayList<ServiceConfigEntity>(); //subquery look to be very poor, no bulk select then, cache should help here as result size is naturally limited for (Tuple tuple : tuples) { CriteriaQuery<ServiceConfigEntity> sce = cb.createQuery(ServiceConfigEntity.class); Root<ServiceConfigEntity> sceRoot = sce.from(ServiceConfigEntity.class); sce.where(cb.and(cb.equal(sceRoot.get("groupId"), tuple.get("groupId")), cb.equal(sceRoot.get("version"), tuple.get("lastVersion")))); sce.select(sceRoot); result.add(daoUtils.selectSingle(entityManagerProvider.get().createQuery(sce))); } return result; }
From source file:org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.CriteriaTranslatorImpl.java
@SuppressWarnings("unchecked") protected TypedQuery<Serializable> constructQuery(DynamicEntityDao dynamicEntityDao, String ceilingEntity, List<FilterMapping> filterMappings, boolean isCount, boolean isMax, Integer firstResult, Integer maxResults, String maxField) { CriteriaBuilder criteriaBuilder = dynamicEntityDao.getStandardEntityManager().getCriteriaBuilder(); Class<Serializable> ceilingMarker; try {/*w ww .ja v a 2 s.c o m*/ ceilingMarker = (Class<Serializable>) Class.forName(ceilingEntity); } catch (ClassNotFoundException e) { throw new RuntimeException(e); } Class<Serializable> securityRoot = rowSecurityService.getFetchRestrictionRoot( adminSecurityService.getPersistentAdminUser(), ceilingMarker, filterMappings); if (securityRoot != null) { ceilingMarker = securityRoot; } Class<Serializable> ceilingClass = determineRoot(dynamicEntityDao, ceilingMarker, filterMappings); CriteriaQuery<Serializable> criteria = criteriaBuilder.createQuery(ceilingMarker); Root<Serializable> original = criteria.from(ceilingClass); if (isCount) { criteria.select(criteriaBuilder.count(original)); } else if (isMax) { criteria.select(criteriaBuilder.max((Path<Number>) ((Object) original.get(maxField)))); } else { criteria.select(original); } List<Predicate> restrictions = new ArrayList<Predicate>(); List<Order> sorts = new ArrayList<Order>(); addRestrictions(ceilingEntity, filterMappings, criteriaBuilder, original, restrictions, sorts, criteria); criteria.where(restrictions.toArray(new Predicate[restrictions.size()])); if (!isCount && !isMax) { criteria.orderBy(sorts.toArray(new Order[sorts.size()])); //If someone provides a firstResult value, then there is generally pagination going on. //In order to produce consistent results, especially with certain databases such as PostgreSQL, //there has to be an "order by" clause. We'll add one here if we can. if (firstResult != null && sorts.isEmpty()) { Map<String, Object> idMetaData = dynamicEntityDao.getIdMetadata(ceilingClass); if (idMetaData != null) { Object idFldName = idMetaData.get("name"); Object type = idMetaData.get("type"); if ((idFldName instanceof String) && (type instanceof SingleColumnType)) { criteria.orderBy(criteriaBuilder.asc(original.get((String) idFldName))); } } } } TypedQuery<Serializable> response = dynamicEntityDao.getStandardEntityManager().createQuery(criteria); if (!isCount && !isMax) { addPaging(response, firstResult, maxResults); } return response; }
From source file:org.easy.criteria.CriteriaComposer.java
/** * @param criteriaBuilder/*from www .ja va 2 s. com*/ * @param out */ @SuppressWarnings("unchecked") protected void generateHaving(final CriteriaBuilder criteriaBuilder, final List<Predicate> out) { // log.debug("generateHaving :"); Preconditions.checkNotNull(out); Preconditions.checkNotNull(criteriaBuilder); Preconditions.checkNotNull(root); if (_joins != null && _joins.size() > 0) { Set<Entry<JoinContainer<E>, CriteriaComposer<?>>> allSetJoins = _joins.entrySet(); for (Entry<JoinContainer<E>, CriteriaComposer<?>> joinEntry : allSetJoins) { CriteriaComposer<?> subCriteria = joinEntry.getValue(); if (subCriteria != null) subCriteria.generateHaving(criteriaBuilder, out); } } if (_havings != null) { List<Predicate> andPredicates = new ArrayList<Predicate>(0); List<Predicate> orPredicates = new ArrayList<Predicate>(0); LogicOperator lastOperator = LogicOperator.NONE; for (HavingContainer<E> havingContainer : _havings) { @SuppressWarnings("rawtypes") Expression expression = null; switch (havingContainer.function) { case COUNT: expression = criteriaBuilder.count(root.get(havingContainer.attribute)); break; case AVG: expression = criteriaBuilder.avg(root.get(havingContainer.attribute)); break; case SUM: expression = criteriaBuilder.sum(root.get(havingContainer.attribute)); break; case MAX: expression = criteriaBuilder.max(root.get(havingContainer.attribute)); break; case MIN: expression = criteriaBuilder.min(root.get(havingContainer.attribute)); break; } Predicate predicate = ComparisonOperatorProcessor.process(criteriaBuilder, expression, havingContainer.comparisionOperator, havingContainer.value); if (havingContainer.notOperator != null) predicate = ComparisonOperatorProcessor.negate(criteriaBuilder, predicate); LogicOperator nextOperator = havingContainer.logicOperator; if (nextOperator == LogicOperator.NONE && !nextOperator.equals(lastOperator)) nextOperator = lastOperator; switch (nextOperator) { case AND: case NONE: andPredicates.add(predicate); break; case OR: orPredicates.add(predicate); break; default: throw new java.lang.IllegalStateException( "Unknown operator found " + havingContainer.comparisionOperator.toString()); } lastOperator = nextOperator; } // end for if (andPredicates != null && andPredicates.size() > 0) out.add(criteriaBuilder.and(andPredicates.toArray(new Predicate[andPredicates.size()]))); if (orPredicates != null && orPredicates.size() > 0) out.add(criteriaBuilder.or(orPredicates.toArray(new Predicate[orPredicates.size()]))); } // end if }
From source file:org.easy.criteria.CriteriaComposer.java
/** * @param out/* w w w. j av a 2 s.co m*/ */ @SuppressWarnings("unchecked") protected void generateSelect(final CriteriaBuilder criteriaBuilder, final List<Selection<?>> out) { Preconditions.checkNotNull(out); if (multiselect != null && multiselect.size() > 0) { out.addAll(multiselect); } if (_selects != null && _selects.size() > 0) { for (SelectContainer<E> selectContainer : _selects) { if (selectContainer.function == null) { Selection<?> selection = root.get(selectContainer.singularAttribute); selection.alias(selectContainer.alias); out.add(selection); } else { @SuppressWarnings("rawtypes") Expression numExp = null; switch (selectContainer.function) { case COUNT: numExp = criteriaBuilder.count(root.get(selectContainer.singularAttribute)); numExp.alias(selectContainer.alias); break; case AVG: numExp = criteriaBuilder.avg(root.get(selectContainer.singularAttribute)); numExp.alias(selectContainer.alias); break; case SUM: numExp = criteriaBuilder.sum(root.get(selectContainer.singularAttribute)); numExp.alias(selectContainer.alias); break; case MAX: numExp = criteriaBuilder.max(root.get(selectContainer.singularAttribute)); numExp.alias(selectContainer.alias); break; case MIN: numExp = criteriaBuilder.min(root.get(selectContainer.singularAttribute)); numExp.alias(selectContainer.alias); break; } if (numExp != null) out.add(numExp); } } } if (_joins != null) { Set<Entry<JoinContainer<E>, CriteriaComposer<?>>> allSetJoins = _joins.entrySet(); for (Entry<JoinContainer<E>, CriteriaComposer<?>> join : allSetJoins) { CriteriaComposer<?> subCriteria = join.getValue(); if (subCriteria != null) subCriteria.generateSelect(criteriaBuilder, out); } } }
From source file:org.finra.dm.dao.impl.DmDaoImpl.java
/** * {@inheritDoc}/* ww w . java 2 s .co m*/ */ @Override public Integer getBusinessObjectFormatMaxVersion(BusinessObjectFormatKey businessObjectFormatKey) { // Create the criteria builder and the criteria. CriteriaBuilder builder = entityManager.getCriteriaBuilder(); CriteriaQuery<Integer> criteria = builder.createQuery(Integer.class); // The criteria root is the business object format. Root<BusinessObjectFormatEntity> businessObjectFormatEntity = criteria .from(BusinessObjectFormatEntity.class); // Join to the other tables we can filter on. Join<BusinessObjectFormatEntity, FileTypeEntity> fileTypeEntity = businessObjectFormatEntity .join(BusinessObjectFormatEntity_.fileType); Join<BusinessObjectFormatEntity, BusinessObjectDefinitionEntity> businessObjectDefinitionEntity = businessObjectFormatEntity .join(BusinessObjectFormatEntity_.businessObjectDefinition); // Create the standard restrictions (i.e. the standard where clauses). // Business object format version should be ignored when building the query restriction. Predicate queryRestriction = getQueryRestriction(builder, businessObjectFormatEntity, fileTypeEntity, businessObjectDefinitionEntity, businessObjectFormatKey, true); // Create the path. Expression<Integer> maxBusinessObjectFormatVersion = builder .max(businessObjectFormatEntity.get(BusinessObjectFormatEntity_.businessObjectFormatVersion)); criteria.select(maxBusinessObjectFormatVersion).where(queryRestriction); return entityManager.createQuery(criteria).getSingleResult(); }
From source file:org.finra.dm.dao.impl.DmDaoImpl.java
/** * {@inheritDoc}//from ww w .j a v a 2 s . c o m */ @Override public List<BusinessObjectFormatKey> getBusinessObjectFormats( BusinessObjectDefinitionKey businessObjectDefinitionKey, boolean latestBusinessObjectFormatVersion) { // Create the criteria builder and a tuple style criteria query. CriteriaBuilder builder = entityManager.getCriteriaBuilder(); CriteriaQuery<Tuple> criteria = builder.createTupleQuery(); // The criteria root is the business object format. Root<BusinessObjectFormatEntity> businessObjectFormatEntity = criteria .from(BusinessObjectFormatEntity.class); // Join to the other tables we can filter on. Join<BusinessObjectFormatEntity, BusinessObjectDefinitionEntity> businessObjectDefinitionEntity = businessObjectFormatEntity .join(BusinessObjectFormatEntity_.businessObjectDefinition); Join<BusinessObjectFormatEntity, FileTypeEntity> fileTypeEntity = businessObjectFormatEntity .join(BusinessObjectFormatEntity_.fileType); Join<BusinessObjectDefinitionEntity, NamespaceEntity> namespaceEntity = businessObjectDefinitionEntity .join(BusinessObjectDefinitionEntity_.namespace); // Get the columns. Path<String> namespaceCodeColumn = namespaceEntity.get(NamespaceEntity_.code); Path<String> businessObjectDefinitionNameColumn = businessObjectDefinitionEntity .get(BusinessObjectDefinitionEntity_.name); Path<String> businessObjectFormatUsageColumn = businessObjectFormatEntity .get(BusinessObjectFormatEntity_.usage); Path<String> fileTypeCodeColumn = fileTypeEntity.get(FileTypeEntity_.code); Path<Integer> businessObjectFormatVersionColumn = businessObjectFormatEntity .get(BusinessObjectFormatEntity_.businessObjectFormatVersion); Expression<Integer> maxBusinessObjectFormatVersionExpression = builder .max(businessObjectFormatEntity.get(BusinessObjectFormatEntity_.businessObjectFormatVersion)); // Create the standard restrictions (i.e. the standard where clauses). Predicate queryRestriction = builder.equal(builder.upper(namespaceEntity.get(NamespaceEntity_.code)), businessObjectDefinitionKey.getNamespace().toUpperCase()); queryRestriction = builder.and(queryRestriction, builder.equal( builder.upper(businessObjectDefinitionEntity.get(BusinessObjectDefinitionEntity_.name)), businessObjectDefinitionKey.getBusinessObjectDefinitionName().toUpperCase())); // Add the select clause. criteria.multiselect(namespaceCodeColumn, businessObjectDefinitionNameColumn, businessObjectFormatUsageColumn, fileTypeCodeColumn, latestBusinessObjectFormatVersion ? maxBusinessObjectFormatVersionExpression : businessObjectFormatVersionColumn); // Add the where clause. criteria.where(queryRestriction); // If only the latest (maximum) business object format versions to be returned, create and apply the group by clause. if (latestBusinessObjectFormatVersion) { List<Expression<?>> grouping = new ArrayList<>(); grouping.add(namespaceCodeColumn); grouping.add(businessObjectDefinitionNameColumn); grouping.add(businessObjectFormatUsageColumn); grouping.add(fileTypeCodeColumn); criteria.groupBy(grouping); } // Add the order by clause. List<Order> orderBy = new ArrayList<>(); orderBy.add(builder.asc(businessObjectFormatUsageColumn)); orderBy.add(builder.asc(fileTypeCodeColumn)); if (!latestBusinessObjectFormatVersion) { orderBy.add(builder.asc(businessObjectFormatVersionColumn)); } criteria.orderBy(orderBy); // Run the query to get a list of tuples back. List<Tuple> tuples = entityManager.createQuery(criteria).getResultList(); // Populate the "keys" objects from the returned tuples (i.e. 1 tuple for each row). List<BusinessObjectFormatKey> businessObjectFormatKeys = new ArrayList<>(); for (Tuple tuple : tuples) { BusinessObjectFormatKey businessObjectFormatKey = new BusinessObjectFormatKey(); businessObjectFormatKeys.add(businessObjectFormatKey); businessObjectFormatKey.setNamespace(tuple.get(namespaceCodeColumn)); businessObjectFormatKey.setBusinessObjectDefinitionName(tuple.get(businessObjectDefinitionNameColumn)); businessObjectFormatKey.setBusinessObjectFormatUsage(tuple.get(businessObjectFormatUsageColumn)); businessObjectFormatKey.setBusinessObjectFormatFileType(tuple.get(fileTypeCodeColumn)); businessObjectFormatKey.setBusinessObjectFormatVersion( tuple.get(latestBusinessObjectFormatVersion ? maxBusinessObjectFormatVersionExpression : businessObjectFormatVersionColumn)); } return businessObjectFormatKeys; }
From source file:org.finra.dm.dao.impl.DmDaoImpl.java
/** * {@inheritDoc}// w w w. jav a 2 s. com */ @Override public BusinessObjectDataEntity getBusinessObjectDataByAltKeyAndStatus( BusinessObjectDataKey businessObjectDataKey, String businessObjectDataStatus) { // Create the criteria builder and the criteria. CriteriaBuilder builder = entityManager.getCriteriaBuilder(); CriteriaQuery<BusinessObjectDataEntity> criteria = builder.createQuery(BusinessObjectDataEntity.class); // The criteria root is the business object data. Root<BusinessObjectDataEntity> businessObjectDataEntity = criteria.from(BusinessObjectDataEntity.class); // Join to other tables that we need to filter on. Join<BusinessObjectDataEntity, BusinessObjectFormatEntity> businessObjectFormatEntity = businessObjectDataEntity .join(BusinessObjectDataEntity_.businessObjectFormat); Join<BusinessObjectFormatEntity, FileTypeEntity> fileTypeEntity = businessObjectFormatEntity .join(BusinessObjectFormatEntity_.fileType); Join<BusinessObjectFormatEntity, BusinessObjectDefinitionEntity> businessObjectDefinitionEntity = businessObjectFormatEntity .join(BusinessObjectFormatEntity_.businessObjectDefinition); // Create the standard restrictions (i.e. the standard where clauses). Predicate mainQueryRestriction = getQueryRestriction(builder, businessObjectDataEntity, businessObjectFormatEntity, fileTypeEntity, businessObjectDefinitionEntity, businessObjectDataKey); // If a format version was specified, use the latest available for this partition value. if (businessObjectDataKey.getBusinessObjectFormatVersion() == null) { // Business object format version is not specified, so just use the latest available for this set of partition values. Subquery<Integer> subQuery = criteria.subquery(Integer.class); // The criteria root is the business object data. Root<BusinessObjectDataEntity> subBusinessObjectDataEntity = subQuery .from(BusinessObjectDataEntity.class); // Join to the other tables we can filter on. Join<BusinessObjectDataEntity, BusinessObjectFormatEntity> subBusinessObjectFormatEntity = subBusinessObjectDataEntity .join(BusinessObjectDataEntity_.businessObjectFormat); Join<BusinessObjectFormatEntity, BusinessObjectDefinitionEntity> subBusinessObjectDefinitionEntity = subBusinessObjectFormatEntity .join(BusinessObjectFormatEntity_.businessObjectDefinition); Join<BusinessObjectFormatEntity, FileTypeEntity> subBusinessObjectFormatFileTypeEntity = subBusinessObjectFormatEntity .join(BusinessObjectFormatEntity_.fileType); Join<BusinessObjectDataEntity, BusinessObjectDataStatusEntity> subBusinessObjectDataStatusEntity = subBusinessObjectDataEntity .join(BusinessObjectDataEntity_.status); // Create the standard restrictions (i.e. the standard where clauses). Predicate subQueryRestriction = builder.equal(subBusinessObjectDefinitionEntity, businessObjectDefinitionEntity); subQueryRestriction = builder.and(subQueryRestriction, builder.equal(subBusinessObjectFormatEntity.get(BusinessObjectFormatEntity_.usage), businessObjectFormatEntity.get(BusinessObjectFormatEntity_.usage))); subQueryRestriction = builder.and(subQueryRestriction, builder.equal(subBusinessObjectFormatFileTypeEntity, fileTypeEntity)); // Create and add standard restrictions on primary and sub-partition values. subQueryRestriction = builder.and(subQueryRestriction, getQueryRestrictionOnPartitionValues(builder, subBusinessObjectDataEntity, businessObjectDataEntity)); // Add restrictions on business object data version and business object data status. Predicate subQueryRestrictionOnBusinessObjectDataVersionAndStatus = getQueryRestrictionOnBusinessObjectDataVersionAndStatus( builder, subBusinessObjectDataEntity, subBusinessObjectDataStatusEntity, businessObjectDataKey.getBusinessObjectDataVersion(), businessObjectDataStatus); if (subQueryRestrictionOnBusinessObjectDataVersionAndStatus != null) { subQueryRestriction = builder.and(subQueryRestriction, subQueryRestrictionOnBusinessObjectDataVersionAndStatus); } subQuery.select(builder.max( subBusinessObjectFormatEntity.get(BusinessObjectFormatEntity_.businessObjectFormatVersion))) .where(subQueryRestriction); mainQueryRestriction = builder.and(mainQueryRestriction, builder.in( businessObjectFormatEntity.get(BusinessObjectFormatEntity_.businessObjectFormatVersion)) .value(subQuery)); } // If a data version was not specified, use the latest one as per specified business object data status. if (businessObjectDataKey.getBusinessObjectDataVersion() == null) { // Since business object data version is not specified, just use the latest one as per specified business object data status. if (businessObjectDataStatus != null) { // Business object data version is not specified, so get the latest one as per specified business object data status. Subquery<Integer> subQuery = criteria.subquery(Integer.class); // The criteria root is the business object data. Root<BusinessObjectDataEntity> subBusinessObjectDataEntity = subQuery .from(BusinessObjectDataEntity.class); // Join to the other tables we can filter on. Join<BusinessObjectDataEntity, BusinessObjectDataStatusEntity> subBusinessObjectDataStatusEntity = subBusinessObjectDataEntity .join(BusinessObjectDataEntity_.status); Join<BusinessObjectDataEntity, BusinessObjectFormatEntity> subBusinessObjectFormatEntity = subBusinessObjectDataEntity .join(BusinessObjectDataEntity_.businessObjectFormat); // Create the standard restrictions (i.e. the standard where clauses). Predicate subQueryRestriction = builder.equal(subBusinessObjectFormatEntity, businessObjectFormatEntity); // Create and add standard restrictions on primary and sub-partition values. subQueryRestriction = builder.and(subQueryRestriction, getQueryRestrictionOnPartitionValues(builder, subBusinessObjectDataEntity, businessObjectDataEntity)); // Create and add standard restrictions on business object data status. subQueryRestriction = builder.and(subQueryRestriction, builder.equal( builder.upper(subBusinessObjectDataStatusEntity.get(BusinessObjectDataStatusEntity_.code)), businessObjectDataStatus.toUpperCase())); subQuery.select(builder.max(subBusinessObjectDataEntity.get(BusinessObjectDataEntity_.version))) .where(subQueryRestriction); mainQueryRestriction = builder.and(mainQueryRestriction, builder .in(businessObjectDataEntity.get(BusinessObjectDataEntity_.version)).value(subQuery)); } else { // Both business object data version and business object data status are not specified, so just use the latest business object data version. mainQueryRestriction = builder.and(mainQueryRestriction, builder.equal(businessObjectDataEntity.get(BusinessObjectDataEntity_.latestVersion), true)); } } criteria.select(businessObjectDataEntity).where(mainQueryRestriction); return executeSingleResultQuery(criteria, String.format( "Found more than one business object data instance with parameters {namespace=\"%s\", businessObjectDefinitionName=\"%s\"," + " businessObjectFormatUsage=\"%s\", businessObjectFormatFileType=\"%s\", businessObjectFormatVersion=\"%d\"," + " businessObjectDataPartitionValue=\"%s\", businessObjectDataSubPartitionValues=\"%s\", businessObjectDataVersion=\"%d\"," + " businessObjectDataStatus=\"%s\"}.", businessObjectDataKey.getNamespace(), businessObjectDataKey.getBusinessObjectDefinitionName(), businessObjectDataKey.getBusinessObjectFormatUsage(), businessObjectDataKey.getBusinessObjectFormatFileType(), businessObjectDataKey.getBusinessObjectFormatVersion(), businessObjectDataKey.getPartitionValue(), CollectionUtils.isEmpty(businessObjectDataKey.getSubPartitionValues()) ? "" : StringUtils.join(businessObjectDataKey.getSubPartitionValues(), ","), businessObjectDataKey.getBusinessObjectDataVersion(), businessObjectDataStatus)); }
From source file:org.finra.dm.dao.impl.DmDaoImpl.java
/** * {@inheritDoc}/*from w w w . ja va 2s. c o m*/ */ @Override public Integer getBusinessObjectDataMaxVersion(BusinessObjectDataKey businessObjectDataKey) { // Create the criteria builder and the criteria. CriteriaBuilder builder = entityManager.getCriteriaBuilder(); CriteriaQuery<Integer> criteria = builder.createQuery(Integer.class); // The criteria root is the business object data. Root<BusinessObjectDataEntity> businessObjectDataEntity = criteria.from(BusinessObjectDataEntity.class); // Join to the other tables we can filter on. Join<BusinessObjectDataEntity, BusinessObjectFormatEntity> businessObjectFormatEntity = businessObjectDataEntity .join(BusinessObjectDataEntity_.businessObjectFormat); Join<BusinessObjectFormatEntity, FileTypeEntity> fileTypeEntity = businessObjectFormatEntity .join(BusinessObjectFormatEntity_.fileType); Join<BusinessObjectFormatEntity, BusinessObjectDefinitionEntity> businessObjectDefinitionEntity = businessObjectFormatEntity .join(BusinessObjectFormatEntity_.businessObjectDefinition); Join<BusinessObjectDefinitionEntity, NamespaceEntity> namespaceEntity = businessObjectDefinitionEntity .join(BusinessObjectDefinitionEntity_.namespace); // Create the path. Expression<Integer> maxBusinessObjectDataVersion = builder .max(businessObjectDataEntity.get(BusinessObjectDataEntity_.version)); // Create the standard restrictions (i.e. the standard where clauses). Predicate queryRestriction = builder.equal(builder.upper(namespaceEntity.get(NamespaceEntity_.code)), businessObjectDataKey.getNamespace().toUpperCase()); queryRestriction = builder.and(queryRestriction, builder.equal( builder.upper(businessObjectDefinitionEntity.get(BusinessObjectDefinitionEntity_.name)), businessObjectDataKey.getBusinessObjectDefinitionName().toUpperCase())); queryRestriction = builder.and(queryRestriction, builder.equal(builder.upper(businessObjectFormatEntity.get(BusinessObjectFormatEntity_.usage)), businessObjectDataKey.getBusinessObjectFormatUsage().toUpperCase())); queryRestriction = builder.and(queryRestriction, builder.equal(builder.upper(fileTypeEntity.get(FileTypeEntity_.code)), businessObjectDataKey.getBusinessObjectFormatFileType().toUpperCase())); queryRestriction = builder.and(queryRestriction, builder.equal( businessObjectFormatEntity.get(BusinessObjectFormatEntity_.businessObjectFormatVersion), businessObjectDataKey.getBusinessObjectFormatVersion())); queryRestriction = builder.and(queryRestriction, builder.equal(businessObjectDataEntity.get(BusinessObjectDataEntity_.partitionValue), businessObjectDataKey.getPartitionValue())); for (int i = 0; i < BusinessObjectDataEntity.MAX_SUBPARTITIONS; i++) { queryRestriction = builder.and(queryRestriction, i < businessObjectDataKey.getSubPartitionValues().size() ? builder.equal(businessObjectDataEntity.get(BUSINESS_OBJECT_DATA_SUBPARTITIONS.get(i)), businessObjectDataKey.getSubPartitionValues().get(i)) : builder.isNull( businessObjectDataEntity.get(BUSINESS_OBJECT_DATA_SUBPARTITIONS.get(i)))); } criteria.select(maxBusinessObjectDataVersion).where(queryRestriction); return entityManager.createQuery(criteria).getSingleResult(); }
From source file:org.finra.dm.dao.impl.DmDaoImpl.java
/** * Retrieves a list of business object data entities per specified parameters. This method processes a sublist of partition filters specified by * partitionFilterSubListFromIndex and partitionFilterSubListSize parameters. * * @param businessObjectFormatKey the business object format key (case-insensitive). If a business object format version isn't specified, the latest * available format version for each partition value will be used. * @param partitionFilters the list of partition filter to be used to select business object data instances. Each partition filter contains a list of * primary and sub-partition values in the right order up to the maximum partition levels allowed by business object data registration - with partition * values for the relative partitions not to be used for selection passed as nulls. * @param businessObjectDataVersion the business object data version. If a business object data version isn't specified, the latest data version based on * the specified business object data status is returned. * @param businessObjectDataStatus the business object data status. This parameter is ignored when the business object data version is specified. When * business object data version and business object data status both are not specified, the latest data version for each set of partition values will be * used regardless of the status.// ww w.ja va2 s . c o m * @param storageName the name of the storage where the business object data storage unit is located (case-insensitive) * @param partitionFilterSubListFromIndex the index of the first element in the partition filter sublist * @param partitionFilterSubListSize the size of the partition filter sublist * * @return the list of business object data entities sorted by partition values */ private List<BusinessObjectDataEntity> getBusinessObjectDataEntities( BusinessObjectFormatKey businessObjectFormatKey, List<List<String>> partitionFilters, Integer businessObjectDataVersion, String businessObjectDataStatus, String storageName, int partitionFilterSubListFromIndex, int partitionFilterSubListSize) { // Create the criteria builder and the criteria. CriteriaBuilder builder = entityManager.getCriteriaBuilder(); CriteriaQuery<BusinessObjectDataEntity> criteria = builder.createQuery(BusinessObjectDataEntity.class); // The criteria root is the business object data. Root<BusinessObjectDataEntity> businessObjectDataEntity = criteria.from(BusinessObjectDataEntity.class); // Join to the other tables we can filter on. Join<BusinessObjectDataEntity, StorageUnitEntity> storageUnitEntity = businessObjectDataEntity .join(BusinessObjectDataEntity_.storageUnits); Join<StorageUnitEntity, StorageEntity> storageEntity = storageUnitEntity.join(StorageUnitEntity_.storage); Join<BusinessObjectDataEntity, BusinessObjectFormatEntity> businessObjectFormatEntity = businessObjectDataEntity .join(BusinessObjectDataEntity_.businessObjectFormat); Join<BusinessObjectFormatEntity, FileTypeEntity> fileTypeEntity = businessObjectFormatEntity .join(BusinessObjectFormatEntity_.fileType); Join<BusinessObjectFormatEntity, BusinessObjectDefinitionEntity> businessObjectDefinitionEntity = businessObjectFormatEntity .join(BusinessObjectFormatEntity_.businessObjectDefinition); // Create the standard restrictions (i.e. the standard where clauses). // Create a standard restriction based on the business object format key values. // Please note that we specify not to ignore the business object format version. Predicate mainQueryRestriction = getQueryRestriction(builder, businessObjectFormatEntity, fileTypeEntity, businessObjectDefinitionEntity, businessObjectFormatKey, false); // If a format version was not specified, use the latest available for this set of partition values. if (businessObjectFormatKey.getBusinessObjectFormatVersion() == null) { // Business object format version is not specified, so just use the latest available for this set of partition values. Subquery<Integer> subQuery = criteria.subquery(Integer.class); // The criteria root is the business object data. Root<BusinessObjectDataEntity> subBusinessObjectDataEntity = subQuery .from(BusinessObjectDataEntity.class); // Join to the other tables we can filter on. Join<BusinessObjectDataEntity, StorageUnitEntity> subStorageUnitEntity = subBusinessObjectDataEntity .join(BusinessObjectDataEntity_.storageUnits); Join<StorageUnitEntity, StorageEntity> subStorageEntity = subStorageUnitEntity .join(StorageUnitEntity_.storage); Join<BusinessObjectDataEntity, BusinessObjectFormatEntity> subBusinessObjectFormatEntity = subBusinessObjectDataEntity .join(BusinessObjectDataEntity_.businessObjectFormat); Join<BusinessObjectFormatEntity, BusinessObjectDefinitionEntity> subBusinessObjectDefinitionEntity = subBusinessObjectFormatEntity .join(BusinessObjectFormatEntity_.businessObjectDefinition); Join<BusinessObjectFormatEntity, FileTypeEntity> subBusinessObjectFormatFileTypeEntity = subBusinessObjectFormatEntity .join(BusinessObjectFormatEntity_.fileType); Join<BusinessObjectDataEntity, BusinessObjectDataStatusEntity> subBusinessObjectDataStatusEntity = subBusinessObjectDataEntity .join(BusinessObjectDataEntity_.status); // Create the standard restrictions (i.e. the standard where clauses). Predicate subQueryRestriction = builder.equal(subBusinessObjectDefinitionEntity, businessObjectDefinitionEntity); subQueryRestriction = builder.and(subQueryRestriction, builder.equal(subBusinessObjectFormatEntity.get(BusinessObjectFormatEntity_.usage), businessObjectFormatEntity.get(BusinessObjectFormatEntity_.usage))); subQueryRestriction = builder.and(subQueryRestriction, builder.equal(subBusinessObjectFormatFileTypeEntity, fileTypeEntity)); // Create and add standard restrictions on primary and sub-partition values. subQueryRestriction = builder.and(subQueryRestriction, getQueryRestrictionOnPartitionValues(builder, subBusinessObjectDataEntity, businessObjectDataEntity)); // Add restrictions on business object data version and business object data status. Predicate subQueryRestrictionOnBusinessObjectDataVersionAndStatus = getQueryRestrictionOnBusinessObjectDataVersionAndStatus( builder, subBusinessObjectDataEntity, subBusinessObjectDataStatusEntity, businessObjectDataVersion, businessObjectDataStatus); if (subQueryRestrictionOnBusinessObjectDataVersionAndStatus != null) { subQueryRestriction = builder.and(subQueryRestriction, subQueryRestrictionOnBusinessObjectDataVersionAndStatus); } // Create and add a standard restriction on storage. subQueryRestriction = builder.and(subQueryRestriction, builder.equal(subStorageEntity, storageEntity)); subQuery.select(builder.max( subBusinessObjectFormatEntity.get(BusinessObjectFormatEntity_.businessObjectFormatVersion))) .where(subQueryRestriction); mainQueryRestriction = builder.and(mainQueryRestriction, builder.in( businessObjectFormatEntity.get(BusinessObjectFormatEntity_.businessObjectFormatVersion)) .value(subQuery)); } // Add restriction as per specified primary and/or sub-partition values. mainQueryRestriction = builder.and(mainQueryRestriction, getQueryRestrictionOnPartitionValues(builder, businessObjectDataEntity, partitionFilters.subList(partitionFilterSubListFromIndex, partitionFilterSubListFromIndex + partitionFilterSubListSize))); // If a data version was specified, use it. Otherwise, use the latest one as per specified business object data status. if (businessObjectDataVersion != null) { mainQueryRestriction = builder.and(mainQueryRestriction, builder.equal( businessObjectDataEntity.get(BusinessObjectDataEntity_.version), businessObjectDataVersion)); } else { // Business object data version is not specified, so get the latest one as per specified business object data status, if any. // Meaning, when both business object data version and business object data status are not specified, we just return // the latest business object data version in the specified storage. Subquery<Integer> subQuery = getMaximumBusinessObjectDataVersionSubQuery(builder, criteria, businessObjectDataEntity, businessObjectFormatEntity, businessObjectDataStatus, storageEntity); mainQueryRestriction = builder.and(mainQueryRestriction, builder.in(businessObjectDataEntity.get(BusinessObjectDataEntity_.version)).value(subQuery)); } // Add a storage name restriction to the main query where clause. mainQueryRestriction = builder.and(mainQueryRestriction, builder.equal(builder.upper(storageEntity.get(StorageEntity_.name)), storageName.toUpperCase())); // Add the clauses for the query. criteria.select(businessObjectDataEntity).where(mainQueryRestriction); // Order by partitions. List<Order> orderBy = new ArrayList<>(); for (SingularAttribute<BusinessObjectDataEntity, String> businessObjectDataPartition : BUSINESS_OBJECT_DATA_PARTITIONS) { orderBy.add(builder.asc(businessObjectDataEntity.get(businessObjectDataPartition))); } criteria.orderBy(orderBy); return entityManager.createQuery(criteria).getResultList(); }
From source file:org.finra.dm.dao.impl.DmDaoImpl.java
/** * Builds a sub-query to select the maximum business object data version. * * @param builder the criteria builder/* w w w . j a v a2s.com*/ * @param criteria the criteria query * @param businessObjectDataEntity the business object data entity that appears in the from clause of the main query * @param businessObjectFormatEntity the business object format entity that appears in the from clause of the main query * @param businessObjectDataStatus the business object data status * * @return the sub-query to select the maximum business object data version */ private Subquery<Integer> getMaximumBusinessObjectDataVersionSubQuery(CriteriaBuilder builder, CriteriaQuery<?> criteria, From<?, BusinessObjectDataEntity> businessObjectDataEntity, From<?, BusinessObjectFormatEntity> businessObjectFormatEntity, String businessObjectDataStatus, From<?, StorageEntity> storageEntity) { // Business object data version is not specified, so get the latest one in the specified storage. Subquery<Integer> subQuery = criteria.subquery(Integer.class); // The criteria root is the business object data. Root<BusinessObjectDataEntity> subBusinessObjectDataEntity = subQuery.from(BusinessObjectDataEntity.class); // Join to the other tables we can filter on. Join<BusinessObjectDataEntity, StorageUnitEntity> subStorageUnitEntity = subBusinessObjectDataEntity .join(BusinessObjectDataEntity_.storageUnits); Join<StorageUnitEntity, StorageEntity> subStorageEntity = subStorageUnitEntity .join(StorageUnitEntity_.storage); Join<BusinessObjectDataEntity, BusinessObjectFormatEntity> subBusinessObjectFormatEntity = subBusinessObjectDataEntity .join(BusinessObjectDataEntity_.businessObjectFormat); // Add a standard restriction on business object format. Predicate subQueryRestriction = builder.equal(subBusinessObjectFormatEntity, businessObjectFormatEntity); // Create and add standard restrictions on primary and sub-partition values. subQueryRestriction = builder.and(subQueryRestriction, getQueryRestrictionOnPartitionValues(builder, subBusinessObjectDataEntity, businessObjectDataEntity)); // If specified, create and add a standard restriction on business object data status. if (businessObjectDataStatus != null) { Join<BusinessObjectDataEntity, BusinessObjectDataStatusEntity> subBusinessObjectDataStatusEntity = subBusinessObjectDataEntity .join(BusinessObjectDataEntity_.status); subQueryRestriction = builder.and(subQueryRestriction, builder.equal( builder.upper( subBusinessObjectDataStatusEntity.get(BusinessObjectDataStatusEntity_.code)), businessObjectDataStatus.toUpperCase())); } // Create and add a standard restriction on storage. subQueryRestriction = builder.and(subQueryRestriction, builder.equal(subStorageEntity, storageEntity)); subQuery.select(builder.max(subBusinessObjectDataEntity.get(BusinessObjectDataEntity_.version))) .where(subQueryRestriction); return subQuery; }