Example usage for javax.persistence.criteria From get

List of usage examples for javax.persistence.criteria From get

Introduction

In this page you can find the example usage for javax.persistence.criteria From get.

Prototype

<Y> Path<Y> get(SingularAttribute<? super X, Y> attribute);

Source Link

Document

Create a path corresponding to the referenced single-valued attribute.

Usage

From source file:org.broadleafcommerce.admin.server.service.handler.ISOCountryPersistenceHandler.java

@Override
public DynamicResultSet fetch(PersistencePackage persistencePackage, CriteriaTransferObject cto,
        DynamicEntityDao dynamicEntityDao, RecordHelper helper) throws ServiceException {
    FilterMapping filterMapping = new FilterMapping().withFieldPath(new FieldPath().withTargetProperty("name"))
            .withDirectFilterValues(new EmptyFilterValues()).withRestriction(
                    new Restriction().withPredicateProvider(new PredicateProvider<Character, Character>() {
                        @Override
                        public Predicate buildPredicate(CriteriaBuilder builder,
                                FieldPathBuilder fieldPathBuilder, From root, String ceilingEntity,
                                String fullPropertyName, Path<Character> explicitPath,
                                List<Character> directValues) {
                            return builder.isNotNull(explicitPath);
                        }//from   ww  w . j  a va  2s  .c  o m
                    }));
    cto.getAdditionalFilterMappings().add(filterMapping);

    FilterMapping countryRestrictionMapping = new FilterMapping()
            .withDirectFilterValues(new EmptyFilterValues()).withRestriction(
                    new Restriction().withPredicateProvider(new PredicateProvider<Character, Character>() {
                        @Override
                        public Predicate buildPredicate(CriteriaBuilder builder,
                                FieldPathBuilder fieldPathBuilder, From root, String ceilingEntity,
                                String fullPropertyName, Path<Character> explicitPath,
                                List<Character> directValues) {
                            CriteriaQuery<Serializable> criteria = fieldPathBuilder.getCriteria();

                            Root<CountryImpl> blcCountry = criteria.from(CountryImpl.class);
                            Predicate join = builder.equal(root.get("alpha2").as(String.class),
                                    blcCountry.get("abbreviation").as(String.class));

                            return join;
                        }
                    }));
    cto.getAdditionalFilterMappings().add(countryRestrictionMapping);

    PersistenceModule myModule = helper.getCompatibleModule(
            persistencePackage.getPersistencePerspective().getOperationTypes().getFetchType());
    return myModule.fetch(persistencePackage, cto);
}

From source file:org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.FieldPathBuilder.java

public FieldPath getFieldPath(From root, String fullPropertyName) {
    String[] pieces = fullPropertyName.split("\\.");
    List<String> associationPath = new ArrayList<String>();
    List<String> basicProperties = new ArrayList<String>();
    int j = 0;//  ww w  .j  a v a 2s .c  om
    for (String piece : pieces) {
        checkPiece: {
            if (j == 0) {
                Path path = root.get(piece);
                if (path instanceof PluralAttributePath) {
                    associationPath.add(piece);
                    break checkPiece;
                }
            }
            basicProperties.add(piece);
        }
        j++;
    }
    FieldPath fieldPath = new FieldPath().withAssociationPath(associationPath)
            .withTargetPropertyPieces(basicProperties);

    return fieldPath;
}

From source file:org.finra.dm.dao.impl.DmDaoImpl.java

/**
 * Builds a query restriction predicate for the specified business object format entity as per business object format key values.
 *
 * @param builder the criteria builder//from   w w w .  j a va  2  s  . c o  m
 * @param businessObjectFormatEntity the business object format entity that appears in the from clause
 * @param fileTypeEntity the file type entity that appears in the from clause
 * @param businessObjectDefinitionEntity the business object definition entity that appears in the from clause
 * @param businessObjectFormatKey the business object format key
 * @param ignoreBusinessObjectFormatVersion specifies whether to ignore the business object format version when building the predicate
 *
 * @return the query restriction predicate
 */
private Predicate getQueryRestriction(CriteriaBuilder builder,
        From<?, BusinessObjectFormatEntity> businessObjectFormatEntity, From<?, FileTypeEntity> fileTypeEntity,
        From<?, BusinessObjectDefinitionEntity> businessObjectDefinitionEntity,
        BusinessObjectFormatKey businessObjectFormatKey, boolean ignoreBusinessObjectFormatVersion) {
    // Join to the other tables we can filter on.
    Join<BusinessObjectDefinitionEntity, NamespaceEntity> namespaceEntity = businessObjectDefinitionEntity
            .join(BusinessObjectDefinitionEntity_.namespace);

    // Create the standard restrictions based on the business object format key values (i.e. the standard where clauses).

    // Create a restriction on namespace code.
    Predicate predicate = builder.equal(builder.upper(namespaceEntity.get(NamespaceEntity_.code)),
            businessObjectFormatKey.getNamespace().toUpperCase());

    // Create and append a restriction on business object definition name.
    predicate = builder.and(predicate,
            builder.equal(
                    builder.upper(businessObjectDefinitionEntity.get(BusinessObjectDefinitionEntity_.name)),
                    businessObjectFormatKey.getBusinessObjectDefinitionName().toUpperCase()));

    // Create and append a restriction on business object format usage.
    predicate = builder.and(predicate,
            builder.equal(builder.upper(businessObjectFormatEntity.get(BusinessObjectFormatEntity_.usage)),
                    businessObjectFormatKey.getBusinessObjectFormatUsage().toUpperCase()));

    // Create and append a restriction on business object format file type.
    predicate = builder.and(predicate, builder.equal(builder.upper(fileTypeEntity.get(FileTypeEntity_.code)),
            businessObjectFormatKey.getBusinessObjectFormatFileType().toUpperCase()));

    // If specified, create and append a restriction on business object format version.
    if (!ignoreBusinessObjectFormatVersion
            && businessObjectFormatKey.getBusinessObjectFormatVersion() != null) {
        predicate = builder.and(predicate,
                builder.equal(
                        businessObjectFormatEntity.get(BusinessObjectFormatEntity_.businessObjectFormatVersion),
                        businessObjectFormatKey.getBusinessObjectFormatVersion()));
    }

    return predicate;
}

From source file:org.finra.dm.dao.impl.DmDaoImpl.java

/**
 * Builds a query restriction predicate for the specified entities as per business object data key values.
 *
 * @param builder the criteria builder/*from ww w  .  j a  v  a  2s  . com*/
 * @param businessObjectDataEntity the business object data entity that appears in the from clause
 * @param businessObjectFormatEntity the business object format entity that appears in the from clause
 * @param fileTypeEntity the file type entity that appears in the from clause
 * @param businessObjectDefinitionEntity the business object definition entity that appears in the from clause
 * @param businessObjectDataKey the business object data key
 *
 * @return the query restriction predicate
 */
private Predicate getQueryRestriction(CriteriaBuilder builder,
        From<?, BusinessObjectDataEntity> businessObjectDataEntity,
        From<?, BusinessObjectFormatEntity> businessObjectFormatEntity, From<?, FileTypeEntity> fileTypeEntity,
        From<?, BusinessObjectDefinitionEntity> businessObjectDefinitionEntity,
        BusinessObjectDataKey businessObjectDataKey) {
    // Create the standard restrictions based on the business object format key values that are part of the business object data key.
    // Please note that we specify not to ignore the business object format version.
    Predicate predicate = getQueryRestriction(builder, businessObjectFormatEntity, fileTypeEntity,
            businessObjectDefinitionEntity, getBusinessObjectFormatKey(businessObjectDataKey), false);

    // Create and append a restriction on partition values.
    predicate = builder.and(predicate,
            getQueryRestrictionOnPartitionValues(builder, businessObjectDataEntity, businessObjectDataKey));

    // If it is specified, create and append a restriction on business object data version.
    if (businessObjectDataKey.getBusinessObjectDataVersion() != null) {
        predicate = builder.and(predicate,
                builder.equal(businessObjectDataEntity.get(BusinessObjectDataEntity_.version),
                        businessObjectDataKey.getBusinessObjectDataVersion()));
    }

    return predicate;
}

From source file:org.finra.dm.dao.impl.DmDaoImpl.java

/**
 * Builds a query restriction predicate for the specified business object data entity as per primary and sub-partition values in the business object data
 * key./*  w  w  w .ja  v  a  2s. c om*/
 *
 * @param builder the criteria builder
 * @param businessObjectDataEntity the business object data entity that appears in the from clause
 * @param businessObjectDataKey the business object data key
 *
 * @return the query restriction predicate
 */
private Predicate getQueryRestrictionOnPartitionValues(CriteriaBuilder builder,
        From<?, BusinessObjectDataEntity> businessObjectDataEntity,
        BusinessObjectDataKey businessObjectDataKey) {
    // Create a standard restriction on primary partition value.
    Predicate predicate = builder.equal(businessObjectDataEntity.get(BusinessObjectDataEntity_.partitionValue),
            businessObjectDataKey.getPartitionValue());

    // Create and add standard restrictions on sub-partition values. Please note that the subpartition value columns are nullable.
    int subPartitionValuesCount = getSubPartitionValuesCount(businessObjectDataKey);
    for (int i = 0; i < BusinessObjectDataEntity.MAX_SUBPARTITIONS; i++) {
        predicate = builder.and(predicate, i < subPartitionValuesCount
                ? builder.equal(businessObjectDataEntity.get(BUSINESS_OBJECT_DATA_SUBPARTITIONS.get(i)),
                        businessObjectDataKey.getSubPartitionValues().get(i))
                : builder.isNull(businessObjectDataEntity.get(BUSINESS_OBJECT_DATA_SUBPARTITIONS.get(i))));
    }

    return predicate;
}

From source file:org.finra.dm.dao.impl.DmDaoImpl.java

/**
 * Builds a query restriction predicate for the sub-query business object data entity as per partition values from the specified main query business object
 * data entity.//  w ww  .j a v a 2  s  .  c  o m
 *
 * @param builder the criteria builder
 * @param subBusinessObjectDataEntity the sub-query business object data entity that appears in the from clause
 * @param mainBusinessObjectDataEntity the main query business object data entity that appears in the from clause
 *
 * @return the query restriction predicate
 */
private Predicate getQueryRestrictionOnPartitionValues(CriteriaBuilder builder,
        From<?, BusinessObjectDataEntity> subBusinessObjectDataEntity,
        From<?, BusinessObjectDataEntity> mainBusinessObjectDataEntity) {
    // Create a standard restriction on primary partition value.
    Predicate predicate = builder.equal(
            subBusinessObjectDataEntity.get(BusinessObjectDataEntity_.partitionValue),
            mainBusinessObjectDataEntity.get(BusinessObjectDataEntity_.partitionValue));

    // Create and add standard restrictions on sub-partition values. Please note that the subpartition value columns are nullable.
    for (SingularAttribute<BusinessObjectDataEntity, String> businessObjectDataPartitionValueSingularAttribute : BUSINESS_OBJECT_DATA_SUBPARTITIONS) {
        predicate = builder.and(predicate, builder.or(
                builder.and(
                        builder.isNull(subBusinessObjectDataEntity
                                .get(businessObjectDataPartitionValueSingularAttribute)),
                        builder.isNull(mainBusinessObjectDataEntity
                                .get(businessObjectDataPartitionValueSingularAttribute))),
                builder.equal(
                        subBusinessObjectDataEntity.get(businessObjectDataPartitionValueSingularAttribute),
                        mainBusinessObjectDataEntity.get(businessObjectDataPartitionValueSingularAttribute))));
    }

    return predicate;
}

From source file:org.finra.dm.dao.impl.DmDaoImpl.java

/**
 * Builds a query restriction predicate for the sub-query business object data entity as per partition values from the specified main query business object
 * data entity.//from ww w  .j  av  a 2 s  .  c o m
 *
 * @param builder the criteria builder
 * @param businessObjectDataEntity the business object data entity that appears in the from clause
 * @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.
 *
 * @return the query restriction predicate
 */
private Predicate getQueryRestrictionOnPartitionValues(CriteriaBuilder builder,
        From<?, BusinessObjectDataEntity> businessObjectDataEntity, List<List<String>> partitionFilters) {
    // Create a query restriction as per specified primary and/or sub-partition values.
    Predicate predicate = null;
    for (List<String> partitionFilter : partitionFilters) {
        // Add restriction for each partition level if the relative partition value is specified in the partition filter.
        Predicate partitionRestriction = null;
        for (int partitionLevel = 0; partitionLevel < BusinessObjectDataEntity.MAX_SUBPARTITIONS
                + 1; partitionLevel++) {
            String partitionValue = partitionFilter.get(partitionLevel);
            if (StringUtils.isNotBlank(partitionValue)) {
                Predicate partitionValueRestriction = builder.equal(
                        businessObjectDataEntity.get(BUSINESS_OBJECT_DATA_PARTITIONS.get(partitionLevel)),
                        partitionValue);
                partitionRestriction = (partitionRestriction == null ? partitionValueRestriction
                        : builder.and(partitionRestriction, partitionValueRestriction));
            }
        }
        predicate = (predicate == null ? partitionRestriction : builder.or(predicate, partitionRestriction));
    }

    return predicate;
}

From source file:org.finra.dm.dao.impl.DmDaoImpl.java

/**
 * Builds query restriction predicates and adds them to the query restriction as per specified business object data version and status. If a business object
 * data version is specified, the business object data status is ignored.  When both business object data version and business object data status are not
 * specified, the sub-query restriction is not getting updated.
 *
 * @param builder the criteria builder/*w w  w . j  av  a  2s .c  o  m*/
 * @param businessObjectDataEntity the business object data entity that appears in the from clause
 * @param businessObjectDataStatusEntity the business object data status entity that appears in the from clause
 * @param businessObjectDataVersion the business object data version
 * @param businessObjectDataStatus the business object data status. This parameter is ignored when the business object data version is specified.
 *
 * @return the query restriction predicate or null if both business object data version and business object data status are not specified
 */
private Predicate getQueryRestrictionOnBusinessObjectDataVersionAndStatus(CriteriaBuilder builder,
        From<?, BusinessObjectDataEntity> businessObjectDataEntity,
        From<?, BusinessObjectDataStatusEntity> businessObjectDataStatusEntity,
        Integer businessObjectDataVersion, String businessObjectDataStatus) {
    Predicate predicate = null;

    // If specified, create a standard restriction on the business object data version.
    if (businessObjectDataVersion != null) {
        predicate = builder.equal(businessObjectDataEntity.get(BusinessObjectDataEntity_.version),
                businessObjectDataVersion);
    }
    // Only if a business object data version is not specified, check if we need to add a restriction on the business object data status.
    else if (businessObjectDataStatus != null) {
        predicate = builder.equal(
                builder.upper(businessObjectDataStatusEntity.get(BusinessObjectDataStatusEntity_.code)),
                businessObjectDataStatus.toUpperCase());
    }

    return predicate;
}

From source file:org.finra.herd.dao.impl.AbstractHerdDao.java

/**
 * TODO This method may be bdata specific. Consider creating new abstract class to group all bdata related DAO. Builds a query restriction predicate for the
 * specified entities as per business object data key values.
 *
 * @param builder the criteria builder//from  w w  w .  j  a va 2 s. c  o m
 * @param businessObjectDataEntity the business object data entity that appears in the from clause
 * @param businessObjectFormatEntity the business object format entity that appears in the from clause
 * @param fileTypeEntity the file type entity that appears in the from clause
 * @param businessObjectDefinitionEntity the business object definition entity that appears in the from clause
 * @param businessObjectDataKey the business object data key
 *
 * @return the query restriction predicate
 */
protected Predicate getQueryRestriction(CriteriaBuilder builder,
        From<?, BusinessObjectDataEntity> businessObjectDataEntity,
        From<?, BusinessObjectFormatEntity> businessObjectFormatEntity, From<?, FileTypeEntity> fileTypeEntity,
        From<?, BusinessObjectDefinitionEntity> businessObjectDefinitionEntity,
        BusinessObjectDataKey businessObjectDataKey) {
    // Create the standard restrictions based on the business object format key values that are part of the business object data key.
    // Please note that we specify not to ignore the business object format version.
    Predicate predicate = getQueryRestriction(builder, businessObjectFormatEntity, fileTypeEntity,
            businessObjectDefinitionEntity, getBusinessObjectFormatKey(businessObjectDataKey), false);

    // Create and append a restriction on partition values.
    predicate = builder.and(predicate,
            getQueryRestrictionOnPartitionValues(builder, businessObjectDataEntity, businessObjectDataKey));

    // If it is specified, create and append a restriction on business object data version.
    if (businessObjectDataKey.getBusinessObjectDataVersion() != null) {
        predicate = builder.and(predicate,
                builder.equal(businessObjectDataEntity.get(BusinessObjectDataEntity_.version),
                        businessObjectDataKey.getBusinessObjectDataVersion()));
    }

    return predicate;
}

From source file:org.finra.herd.dao.impl.AbstractHerdDao.java

/**
 * TODO This method may be bdata specific. Consider creating new abstract class to group all bdata related DAO. Builds a query restriction predicate for the
 * specified business object data entity as per primary and sub-partition values in the business object data key.
 *
 * @param builder the criteria builder/* ww w.  ja v  a  2s  .c o m*/
 * @param businessObjectDataEntity the business object data entity that appears in the from clause
 * @param primaryPartitionValue the primary partition value of the business object data
 * @param subPartitionValues the list of sub-partition values for the business object data
 *
 * @return the query restriction predicate
 */
protected Predicate getQueryRestrictionOnPartitionValues(CriteriaBuilder builder,
        From<?, BusinessObjectDataEntity> businessObjectDataEntity, String primaryPartitionValue,
        List<String> subPartitionValues) {
    // Create a standard restriction on primary partition value.
    Predicate predicate = builder.equal(businessObjectDataEntity.get(BusinessObjectDataEntity_.partitionValue),
            primaryPartitionValue);

    // Create and add standard restrictions on sub-partition values. Please note that the subpartition value columns are nullable.
    int subPartitionValuesCount = CollectionUtils.size(subPartitionValues);
    for (int i = 0; i < BusinessObjectDataEntity.MAX_SUBPARTITIONS; i++) {
        predicate = builder.and(predicate, i < subPartitionValuesCount
                ? builder.equal(businessObjectDataEntity.get(BUSINESS_OBJECT_DATA_SUBPARTITIONS.get(i)),
                        subPartitionValues.get(i))
                : builder.isNull(businessObjectDataEntity.get(BUSINESS_OBJECT_DATA_SUBPARTITIONS.get(i))));
    }

    return predicate;
}