Example usage for javax.persistence.metamodel PluralAttribute getDeclaringType

List of usage examples for javax.persistence.metamodel PluralAttribute getDeclaringType

Introduction

In this page you can find the example usage for javax.persistence.metamodel PluralAttribute getDeclaringType.

Prototype

ManagedType<X> getDeclaringType();

Source Link

Document

Return the managed type representing the type in which the attribute was declared.

Usage

From source file:org.kuali.rice.krad.data.jpa.eclipselink.EclipseLinkJpaMetadataProviderImpl.java

/**
 * {@inheritDoc}/*from w  ww.  j a  v  a 2  s  .c  o m*/
 */
@Override
protected void populateImplementationSpecificCollectionLevelMetadata(DataObjectCollectionImpl collection,
        PluralAttribute<?, ?, ?> cd) {
    // OJB stores the related class object name. We need to go into the repository and grab the table name.
    Class<?> collectionElementClass = cd.getElementType().getJavaType();
    EntityType<?> elementEntityType = entityManager.getMetamodel().entity(collectionElementClass);
    // get table name behind element
    if (elementEntityType instanceof EntityTypeImpl) {
        collection.setBackingObjectName(((EntityTypeImpl<?>) elementEntityType).getDescriptor().getTableName());
    }

    // Set to read only if store (save) operations should not be pushed through
    PersistentAttributeType persistentAttributeType = cd.getPersistentAttributeType();

    if (cd instanceof PluralAttributeImpl) {
        PluralAttributeImpl<?, ?, ?> coll = (PluralAttributeImpl<?, ?, ?>) cd;
        CollectionMapping collectionMapping = coll.getCollectionMapping();

        if (collectionMapping instanceof OneToManyMapping) {
            OneToManyMapping otm = (OneToManyMapping) collectionMapping;
            populateInverseRelationship(otm, collection);
            Map<DatabaseField, DatabaseField> keyMap = otm.getSourceKeysToTargetForeignKeys();
            List<DataObjectAttributeRelationship> attributeRelationships = new ArrayList<DataObjectAttributeRelationship>();
            for (Map.Entry<DatabaseField, DatabaseField> keyRel : keyMap.entrySet()) {
                attributeRelationships.add(new DataObjectAttributeRelationshipImpl(
                        getPropertyNameFromDatabaseColumnName(cd.getDeclaringType(), keyRel.getKey().getName()),
                        getPropertyNameFromDatabaseColumnName(elementEntityType, keyRel.getValue().getName())));
            }
            collection.setAttributeRelationships(attributeRelationships);
        }

        collection.setReadOnly(collectionMapping.isReadOnly());
        collection.setSavedWithParent(collectionMapping.isCascadePersist());
        collection.setDeletedWithParent(collectionMapping.isCascadeRemove());
        collection
                .setLoadedAtParentLoadTime(collectionMapping.isCascadeRefresh() && !collectionMapping.isLazy());
        collection.setLoadedDynamicallyUponUse(
                collectionMapping.isCascadeRefresh() && collectionMapping.isLazy());
    } else {
        // get what we can based on JPA values (note that we just set some to have values here)
        collection.setReadOnly(false);
        collection.setSavedWithParent(persistentAttributeType == PersistentAttributeType.ONE_TO_MANY);
        collection.setDeletedWithParent(persistentAttributeType == PersistentAttributeType.ONE_TO_MANY);
        collection.setLoadedAtParentLoadTime(true);
        collection.setLoadedDynamicallyUponUse(false);
    }

    // We need to detect the case of a intermediate mapping table. These tables are not directly mapped
    // in OJB, but are referenced by their table and column names.
    // The attributes referenced are assumed to be in the order of the PK fields of the parent and child objects
    // as there is no way to identify the attributes/columns on the linked classes.

    // Extract the default sort order for the collection
    List<DataObjectCollectionSortAttribute> sortAttributes = new ArrayList<DataObjectCollectionSortAttribute>();
    if (cd instanceof PluralAttributeImpl) {
        PluralAttributeImpl<?, ?, ?> coll = (PluralAttributeImpl<?, ?, ?>) cd;
        CollectionMapping collectionMapping = coll.getCollectionMapping();
        if (collectionMapping.getSelectionQuery() instanceof ObjectLevelReadQuery) {
            ObjectLevelReadQuery readQuery = (ObjectLevelReadQuery) collectionMapping.getSelectionQuery();
            List<Expression> orderByExpressions = readQuery.getOrderByExpressions();
            for (Expression expression : orderByExpressions) {
                if (expression instanceof FunctionExpression) {
                    String attributeName = ((FunctionExpression) expression).getBaseExpression().getName();
                    SortDirection direction = SortDirection.ASCENDING;
                    if (expression.getOperator().isOrderOperator()) {
                        if (StringUtils.containsIgnoreCase(expression.getOperator().getDatabaseStrings()[0],
                                "DESC")) {
                            direction = SortDirection.DESCENDING;
                        }
                    }
                    sortAttributes.add(new DataObjectCollectionSortAttributeImpl(attributeName, direction));
                }
            }
        }

    }
    collection.setDefaultCollectionOrderingAttributeNames(sortAttributes);
}