Search in sources :

Example 1 with IndirectionPolicy

use of org.eclipse.persistence.internal.indirection.IndirectionPolicy in project eclipselink by eclipse-ee4j.

the class ObjectPersistenceRuntimeXMLProject method buildManyToManyMappingMappingDescriptor.

protected ClassDescriptor buildManyToManyMappingMappingDescriptor() {
    XMLDescriptor descriptor = new XMLDescriptor();
    descriptor.setJavaClass(ManyToManyMapping.class);
    descriptor.getInheritancePolicy().setParentClass(CollectionMapping.class);
    XMLDirectMapping relationTableMapping = new XMLDirectMapping();
    relationTableMapping.setAttributeName("relationTableName");
    // CR#2407 Call getRelationTableQualifiedName that includes table qualifier.
    relationTableMapping.setGetMethodName("getRelationTableQualifiedName");
    relationTableMapping.setSetMethodName("setRelationTableName");
    relationTableMapping.setXPath(getSecondaryNamespaceXPath() + "relation-table/text()");
    descriptor.addMapping(relationTableMapping);
    XMLCompositeCollectionMapping sourceToRelationKeyFieldAssociationsMapping = new XMLCompositeCollectionMapping();
    sourceToRelationKeyFieldAssociationsMapping.setReferenceClass(Association.class);
    // Handle translation of foreign key associations to hashtables.
    sourceToRelationKeyFieldAssociationsMapping.setAttributeAccessor(new AttributeAccessor() {

        @Override
        public Object getAttributeValueFromObject(Object object) {
            List<DatabaseField> sourceFields = ((ManyToManyMapping) object).getSourceKeyFields();
            List<DatabaseField> relationFields = ((ManyToManyMapping) object).getSourceRelationKeyFields();
            List<Association> associations = new ArrayList<>(sourceFields.size());
            for (int index = 0; index < sourceFields.size(); index++) {
                associations.add(new Association(relationFields.get(index), sourceFields.get(index)));
            }
            return associations;
        }

        @Override
        public void setAttributeValueInObject(Object object, Object value) {
            ManyToManyMapping mapping = (ManyToManyMapping) object;
            @SuppressWarnings({ "unchecked" }) List<Association> associations = (List<Association>) value;
            mapping.setSourceKeyFields(NonSynchronizedVector.newInstance(associations.size()));
            mapping.setSourceRelationKeyFields(NonSynchronizedVector.newInstance(associations.size()));
            Iterator<Association> iterator = associations.iterator();
            while (iterator.hasNext()) {
                Association association = iterator.next();
                mapping.getSourceKeyFields().add((DatabaseField) association.getValue());
                mapping.getSourceRelationKeyFields().add((DatabaseField) association.getKey());
            }
        }
    });
    sourceToRelationKeyFieldAssociationsMapping.setAttributeName("sourceToRelationKeyFieldAssociationsMapping");
    sourceToRelationKeyFieldAssociationsMapping.setXPath(getSecondaryNamespaceXPath() + "source-relation-foreign-key/" + getSecondaryNamespaceXPath() + "field-reference");
    descriptor.addMapping(sourceToRelationKeyFieldAssociationsMapping);
    XMLCompositeCollectionMapping targetToRelationKeyFieldAssociationsMapping = new XMLCompositeCollectionMapping();
    targetToRelationKeyFieldAssociationsMapping.setReferenceClass(Association.class);
    // Handle translation of foreign key associations to hashtables.
    targetToRelationKeyFieldAssociationsMapping.setAttributeAccessor(new AttributeAccessor() {

        @Override
        public Object getAttributeValueFromObject(Object object) {
            List<DatabaseField> targetFields = ((ManyToManyMapping) object).getTargetKeyFields();
            List<DatabaseField> relationFields = ((ManyToManyMapping) object).getTargetRelationKeyFields();
            List<Association> associations = new ArrayList<>(targetFields.size());
            for (int index = 0; index < targetFields.size(); index++) {
                associations.add(new Association(relationFields.get(index), targetFields.get(index)));
            }
            return associations;
        }

        @Override
        public void setAttributeValueInObject(Object object, Object value) {
            ManyToManyMapping mapping = (ManyToManyMapping) object;
            @SuppressWarnings({ "unchecked" }) List<Association> associations = (List<Association>) value;
            mapping.setTargetKeyFields(NonSynchronizedVector.newInstance(associations.size()));
            mapping.setTargetRelationKeyFields(NonSynchronizedVector.newInstance(associations.size()));
            Iterator<Association> iterator = associations.iterator();
            while (iterator.hasNext()) {
                Association association = iterator.next();
                mapping.getTargetKeyFields().add((DatabaseField) association.getValue());
                mapping.getTargetRelationKeyFields().add((DatabaseField) association.getKey());
            }
        }
    });
    targetToRelationKeyFieldAssociationsMapping.setAttributeName("targetToRelationKeyFieldAssociations");
    targetToRelationKeyFieldAssociationsMapping.setXPath(getSecondaryNamespaceXPath() + "target-relation-foreign-key/" + getSecondaryNamespaceXPath() + "field-reference");
    descriptor.addMapping(targetToRelationKeyFieldAssociationsMapping);
    XMLDirectMapping relationshipPartnerAttributeNameMapping = new XMLDirectMapping();
    relationshipPartnerAttributeNameMapping.setAttributeName("relationshipPartnerAttributeName");
    relationshipPartnerAttributeNameMapping.setGetMethodName("getRelationshipPartnerAttributeName");
    relationshipPartnerAttributeNameMapping.setSetMethodName("setRelationshipPartnerAttributeName");
    relationshipPartnerAttributeNameMapping.setXPath(getPrimaryNamespaceXPath() + "bidirectional-target-attribute/text()");
    descriptor.addMapping(relationshipPartnerAttributeNameMapping);
    XMLDirectMapping usesBatchReadingMapping = new XMLDirectMapping();
    usesBatchReadingMapping.setAttributeName("usesBatchReading");
    usesBatchReadingMapping.setGetMethodName("shouldUseBatchReading");
    usesBatchReadingMapping.setSetMethodName("setUsesBatchReading");
    usesBatchReadingMapping.setXPath(getPrimaryNamespaceXPath() + "batch-reading/text()");
    usesBatchReadingMapping.setNullValue(Boolean.FALSE);
    descriptor.addMapping(usesBatchReadingMapping);
    XMLCompositeObjectMapping containerPolicyMapping = new XMLCompositeObjectMapping();
    containerPolicyMapping.setAttributeName("collectionPolicy");
    containerPolicyMapping.setGetMethodName("getContainerPolicy");
    containerPolicyMapping.setSetMethodName("setContainerPolicy");
    containerPolicyMapping.setReferenceClass(ContainerPolicy.class);
    containerPolicyMapping.setXPath(getPrimaryNamespaceXPath() + "container");
    descriptor.addMapping(containerPolicyMapping);
    XMLCompositeObjectMapping indirectionPolicyMapping = new XMLCompositeObjectMapping();
    indirectionPolicyMapping.setReferenceClass(IndirectionPolicy.class);
    // Handle translation of NoIndirectionPolicy -> null.
    indirectionPolicyMapping.setAttributeAccessor(new AttributeAccessor() {

        @Override
        public Object getAttributeValueFromObject(Object object) {
            IndirectionPolicy policy = ((ForeignReferenceMapping) object).getIndirectionPolicy();
            if (policy instanceof NoIndirectionPolicy) {
                return null;
            }
            return policy;
        }

        @Override
        public void setAttributeValueInObject(Object object, Object value) {
            IndirectionPolicy policy = (IndirectionPolicy) value;
            if (value == null) {
                policy = new NoIndirectionPolicy();
            }
            ((ForeignReferenceMapping) object).setIndirectionPolicy(policy);
        }
    });
    indirectionPolicyMapping.setAttributeName("indirectionPolicy");
    indirectionPolicyMapping.setXPath(getPrimaryNamespaceXPath() + "indirection");
    descriptor.addMapping(indirectionPolicyMapping);
    XMLCompositeObjectMapping selectionQueryMapping = new XMLCompositeObjectMapping();
    selectionQueryMapping.setAttributeName("selectionQuery");
    selectionQueryMapping.setGetMethodName("getSelectionQuery");
    selectionQueryMapping.setSetMethodName("setSelectionQuery");
    selectionQueryMapping.setReferenceClass(ReadQuery.class);
    selectionQueryMapping.setXPath(getPrimaryNamespaceXPath() + "selection-query");
    descriptor.addMapping(selectionQueryMapping);
    XMLCompositeObjectMapping insertQueryMapping = new XMLCompositeObjectMapping();
    insertQueryMapping.setAttributeName("insertQuery");
    insertQueryMapping.setGetMethodName("getInsertQuery");
    insertQueryMapping.setSetMethodName("setInsertQuery");
    insertQueryMapping.setReferenceClass(DataModifyQuery.class);
    insertQueryMapping.setXPath(getPrimaryNamespaceXPath() + "insert-query");
    descriptor.addMapping(insertQueryMapping);
    XMLCompositeObjectMapping deleteQueryMapping = new XMLCompositeObjectMapping();
    deleteQueryMapping.setAttributeName("deleteQuery");
    deleteQueryMapping.setGetMethodName("getDeleteQuery");
    deleteQueryMapping.setSetMethodName("setDeleteQuery");
    deleteQueryMapping.setReferenceClass(DataModifyQuery.class);
    deleteQueryMapping.setXPath(getPrimaryNamespaceXPath() + "delete-query");
    descriptor.addMapping(deleteQueryMapping);
    XMLCompositeObjectMapping deleteAllQueryMapping = new XMLCompositeObjectMapping();
    deleteAllQueryMapping.setAttributeName("deleteAllQuery");
    deleteAllQueryMapping.setGetMethodName("getDeleteAllQuery");
    deleteAllQueryMapping.setSetMethodName("setDeleteAllQuery");
    deleteAllQueryMapping.setReferenceClass(DataModifyQuery.class);
    deleteAllQueryMapping.setXPath(getPrimaryNamespaceXPath() + "delete-all-query");
    descriptor.addMapping(deleteAllQueryMapping);
    XMLCompositeObjectMapping historyPolicyMapping = new XMLCompositeObjectMapping();
    historyPolicyMapping.setAttributeName("historyPolicy");
    historyPolicyMapping.setGetMethodName("getHistoryPolicy");
    historyPolicyMapping.setSetMethodName("setHistoryPolicy");
    historyPolicyMapping.setReferenceClass(HistoryPolicy.class);
    historyPolicyMapping.setXPath(getPrimaryNamespaceXPath() + "history-policy");
    descriptor.addMapping(historyPolicyMapping);
    return descriptor;
}
Also used : ManyToManyMapping(org.eclipse.persistence.mappings.ManyToManyMapping) ProxyIndirectionPolicy(org.eclipse.persistence.internal.indirection.ProxyIndirectionPolicy) IndirectionPolicy(org.eclipse.persistence.internal.indirection.IndirectionPolicy) BasicIndirectionPolicy(org.eclipse.persistence.internal.indirection.BasicIndirectionPolicy) ContainerIndirectionPolicy(org.eclipse.persistence.internal.indirection.ContainerIndirectionPolicy) NoIndirectionPolicy(org.eclipse.persistence.internal.indirection.NoIndirectionPolicy) TransparentIndirectionPolicy(org.eclipse.persistence.internal.indirection.TransparentIndirectionPolicy) InMemoryQueryIndirectionPolicy(org.eclipse.persistence.queries.InMemoryQueryIndirectionPolicy) XMLDescriptor(org.eclipse.persistence.oxm.XMLDescriptor) Association(org.eclipse.persistence.mappings.Association) PropertyAssociation(org.eclipse.persistence.mappings.PropertyAssociation) TypedAssociation(org.eclipse.persistence.mappings.TypedAssociation) XMLDirectMapping(org.eclipse.persistence.oxm.mappings.XMLDirectMapping) DatabaseField(org.eclipse.persistence.internal.helper.DatabaseField) XMLCompositeCollectionMapping(org.eclipse.persistence.oxm.mappings.XMLCompositeCollectionMapping) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List) NoIndirectionPolicy(org.eclipse.persistence.internal.indirection.NoIndirectionPolicy) AttributeAccessor(org.eclipse.persistence.mappings.AttributeAccessor) XMLCompositeObjectMapping(org.eclipse.persistence.oxm.mappings.XMLCompositeObjectMapping)

Example 2 with IndirectionPolicy

use of org.eclipse.persistence.internal.indirection.IndirectionPolicy in project eclipselink by eclipse-ee4j.

the class NullDelegateInValueHolderTest method setup.

@Override
public void setup() {
    String customerName = "ACME, Inc.";
    if (indirectCollectionClass.equals(IndirectList.class)) {
        testOrder = new Order(customerName);
    } else if (indirectCollectionClass.equals(IndirectMap.class)) {
        testOrder = new MappedOrder(customerName);
    } else if (indirectCollectionClass.equals(IndirectSet.class)) {
        testOrder = new SetOrder(customerName);
    }
    ClassDescriptor descriptor = getSession().getDescriptor(testOrder);
    ForeignReferenceMapping mapping = (ForeignReferenceMapping) descriptor.getMappingForAttributeName("salesReps");
    IndirectionPolicy policy = mapping.getIndirectionPolicy();
    // replace indirect container's valueholder with a new ValueHolder instance
    mapping.setAttributeValueInObject(testOrder, policy.buildIndirectObject(new ValueHolder()));
}
Also used : ForeignReferenceMapping(org.eclipse.persistence.mappings.ForeignReferenceMapping) ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) IndirectionPolicy(org.eclipse.persistence.internal.indirection.IndirectionPolicy) IndirectMap(org.eclipse.persistence.indirection.IndirectMap) ValueHolder(org.eclipse.persistence.indirection.ValueHolder)

Example 3 with IndirectionPolicy

use of org.eclipse.persistence.internal.indirection.IndirectionPolicy in project eclipselink by eclipse-ee4j.

the class EISObjectPersistenceXMLProject method buildEISOneToManyMappingDescriptor.

protected ClassDescriptor buildEISOneToManyMappingDescriptor() {
    XMLDescriptor descriptor = new XMLDescriptor();
    descriptor.setJavaClass(EISOneToManyMapping.class);
    XMLCompositeCollectionMapping foreignKeyFieldNamesMapping = new XMLCompositeCollectionMapping();
    foreignKeyFieldNamesMapping.setAttributeName("sourceForeignKeyFields");
    foreignKeyFieldNamesMapping.useCollectionClass(java.util.ArrayList.class);
    foreignKeyFieldNamesMapping.setGetMethodName("getSourceForeignKeyFields");
    foreignKeyFieldNamesMapping.setSetMethodName("setSourceForeignKeyFields");
    foreignKeyFieldNamesMapping.setXPath(getPrimaryNamespaceXPath() + "source-foreign-key-fields/" + getPrimaryNamespaceXPath() + "field");
    foreignKeyFieldNamesMapping.setReferenceClass(DatabaseField.class);
    descriptor.addMapping(foreignKeyFieldNamesMapping);
    XMLCompositeCollectionMapping targetForeignKeyFieldNamesMapping = new XMLCompositeCollectionMapping();
    targetForeignKeyFieldNamesMapping.setAttributeName("targetForeignKeyFields");
    targetForeignKeyFieldNamesMapping.useCollectionClass(java.util.ArrayList.class);
    targetForeignKeyFieldNamesMapping.setGetMethodName("getTargetForeignKeyFields");
    targetForeignKeyFieldNamesMapping.setSetMethodName("setTargetForeignKeyFields");
    targetForeignKeyFieldNamesMapping.setXPath(getPrimaryNamespaceXPath() + "target-foreign-key-fields/" + getPrimaryNamespaceXPath() + "field");
    targetForeignKeyFieldNamesMapping.setReferenceClass(DatabaseField.class);
    descriptor.addMapping(targetForeignKeyFieldNamesMapping);
    XMLCompositeObjectMapping foreignKeyGroupingElementMapping = new XMLCompositeObjectMapping();
    foreignKeyGroupingElementMapping.setAttributeName("field");
    foreignKeyGroupingElementMapping.setReferenceClass(DatabaseField.class);
    foreignKeyGroupingElementMapping.setGetMethodName("getForeignKeyGroupingElement");
    foreignKeyGroupingElementMapping.setSetMethodName("setForeignKeyGroupingElement");
    foreignKeyGroupingElementMapping.setXPath(getPrimaryNamespaceXPath() + "foreign-key-grouping-element");
    descriptor.addMapping(foreignKeyGroupingElementMapping);
    XMLDirectMapping relationshipPartnerAttributeNameMapping = new XMLDirectMapping();
    relationshipPartnerAttributeNameMapping.setAttributeName("relationshipPartnerAttributeName");
    relationshipPartnerAttributeNameMapping.setGetMethodName("getRelationshipPartnerAttributeName");
    relationshipPartnerAttributeNameMapping.setSetMethodName("setRelationshipPartnerAttributeName");
    relationshipPartnerAttributeNameMapping.setXPath(getPrimaryNamespaceXPath() + "bidirectional-target-attribute/text()");
    descriptor.addMapping(relationshipPartnerAttributeNameMapping);
    XMLCompositeObjectMapping containerPolicyMapping = new XMLCompositeObjectMapping();
    containerPolicyMapping.setAttributeName("collectionPolicy");
    containerPolicyMapping.setGetMethodName("getContainerPolicy");
    containerPolicyMapping.setSetMethodName("setContainerPolicy");
    containerPolicyMapping.setReferenceClass(ContainerPolicy.class);
    containerPolicyMapping.setXPath(getPrimaryNamespaceXPath() + "container");
    descriptor.addMapping(containerPolicyMapping);
    XMLCompositeObjectMapping indirectionPolicyMapping = new XMLCompositeObjectMapping();
    indirectionPolicyMapping.setReferenceClass(IndirectionPolicy.class);
    // Handle translation of NoIndirectionPolicy -> null.
    indirectionPolicyMapping.setAttributeAccessor(new AttributeAccessor() {

        @Override
        public Object getAttributeValueFromObject(Object object) {
            IndirectionPolicy policy = ((ForeignReferenceMapping) object).getIndirectionPolicy();
            if (policy instanceof NoIndirectionPolicy) {
                return null;
            }
            return policy;
        }

        @Override
        public void setAttributeValueInObject(Object object, Object value) {
            IndirectionPolicy policy = (IndirectionPolicy) value;
            if (value == null) {
                policy = new NoIndirectionPolicy();
            }
            ((ForeignReferenceMapping) object).setIndirectionPolicy(policy);
        }
    });
    indirectionPolicyMapping.setAttributeName("indirectionPolicy");
    indirectionPolicyMapping.setXPath(getPrimaryNamespaceXPath() + "indirection");
    descriptor.addMapping(indirectionPolicyMapping);
    XMLCompositeObjectMapping selectionQueryMapping = new XMLCompositeObjectMapping();
    selectionQueryMapping.setAttributeName("selectionQuery");
    selectionQueryMapping.setReferenceClass(ReadQuery.class);
    selectionQueryMapping.setAttributeAccessor(new AttributeAccessor() {

        @Override
        public Object getAttributeValueFromObject(Object object) {
            if (((ForeignReferenceMapping) object).hasCustomSelectionQuery()) {
                return ((ForeignReferenceMapping) object).getSelectionQuery();
            }
            return null;
        }

        @Override
        public void setAttributeValueInObject(Object object, Object value) {
            if (value instanceof ReadQuery) {
                ((ForeignReferenceMapping) object).setCustomSelectionQuery((ReadQuery) value);
            }
        }
    });
    selectionQueryMapping.setXPath(getPrimaryNamespaceXPath() + "selection-query");
    descriptor.addMapping(selectionQueryMapping);
    // delete-all query
    XMLCompositeObjectMapping deleteAllQueryMapping = new XMLCompositeObjectMapping();
    deleteAllQueryMapping.setAttributeName("deleteAllQuery");
    deleteAllQueryMapping.setReferenceClass(ModifyQuery.class);
    deleteAllQueryMapping.setAttributeAccessor(new AttributeAccessor() {

        @Override
        public Object getAttributeValueFromObject(Object object) {
            boolean hasCustomDeleteAllQuery = ((EISOneToManyMapping) object).hasCustomDeleteAllQuery();
            if (hasCustomDeleteAllQuery) {
                return ((EISOneToManyMapping) object).getDeleteAllQuery();
            } else {
                return null;
            }
        }

        @Override
        public void setAttributeValueInObject(Object object, Object value) {
            if (value instanceof ModifyQuery) {
                ((EISOneToManyMapping) object).setCustomDeleteAllQuery((ModifyQuery) value);
            }
        }
    });
    deleteAllQueryMapping.setXPath(getPrimaryNamespaceXPath() + "delete-all-query");
    descriptor.addMapping(deleteAllQueryMapping);
    descriptor.getInheritancePolicy().setParentClass(CollectionMapping.class);
    return descriptor;
}
Also used : XMLDescriptor(org.eclipse.persistence.oxm.XMLDescriptor) XMLDirectMapping(org.eclipse.persistence.oxm.mappings.XMLDirectMapping) ModifyQuery(org.eclipse.persistence.queries.ModifyQuery) XMLCompositeCollectionMapping(org.eclipse.persistence.oxm.mappings.XMLCompositeCollectionMapping) IndirectionPolicy(org.eclipse.persistence.internal.indirection.IndirectionPolicy) NoIndirectionPolicy(org.eclipse.persistence.internal.indirection.NoIndirectionPolicy) NoIndirectionPolicy(org.eclipse.persistence.internal.indirection.NoIndirectionPolicy) AttributeAccessor(org.eclipse.persistence.mappings.AttributeAccessor) ReadQuery(org.eclipse.persistence.queries.ReadQuery) XMLCompositeObjectMapping(org.eclipse.persistence.oxm.mappings.XMLCompositeObjectMapping)

Example 4 with IndirectionPolicy

use of org.eclipse.persistence.internal.indirection.IndirectionPolicy in project eclipselink by eclipse-ee4j.

the class ProjectClassGenerator method addForeignReferenceMappingLines.

protected void addForeignReferenceMappingLines(NonreflectiveMethodDefinition method, String mappingName, ForeignReferenceMapping mapping) {
    if (mapping.getReferenceClassName() != null) {
        method.addLine(mappingName + ".setReferenceClass(" + mapping.getReferenceClassName() + ".class);");
    }
    if (mapping.getRelationshipPartnerAttributeName() != null) {
        method.addLine(mappingName + ".setRelationshipPartnerAttributeName(\"" + mapping.getRelationshipPartnerAttributeName() + "\");");
    }
    IndirectionPolicy policy = mapping.getIndirectionPolicy();
    if (policy instanceof ContainerIndirectionPolicy) {
        String containerClassName = ((ContainerIndirectionPolicy) policy).getContainerClassName();
        method.addLine(mappingName + ".useContainerIndirection(" + containerClassName + ".class);");
    // Bug#4251902 used in ObjectReferenceMapping
    } else if (policy instanceof ProxyIndirectionPolicy) {
        method.addLine(mappingName + ".useProxyIndirection();");
    } else if (policy instanceof BasicIndirectionPolicy) {
        method.addLine(mappingName + ".useBasicIndirection();");
    } else if (policy instanceof NoIndirectionPolicy) {
        method.addLine(mappingName + ".dontUseIndirection();");
    }
    if (mapping.shouldUseBatchReading()) {
        method.addLine(mappingName + ".useBatchReading();");
    }
    if (mapping.isJoinFetched()) {
        if (mapping.isInnerJoinFetched()) {
            method.addLine(mappingName + ".useInnerJoinFetch();");
        } else if (mapping.isOuterJoinFetched()) {
            method.addLine(mappingName + ".useOuterJoinFetch();");
        }
    }
    if ((!mapping.isDirectCollectionMapping()) && mapping.isPrivateOwned()) {
        method.addLine(mappingName + ".privateOwnedRelationship();");
    }
    if (mapping.isCollectionMapping()) {
        CollectionMapping collectionMapping = (CollectionMapping) mapping;
        String collectionClassName = collectionMapping.getContainerPolicy().getContainerClassName();
        if (mapping.getContainerPolicy().isCollectionPolicy()) {
            if (policy instanceof TransparentIndirectionPolicy) {
                method.addLine(mappingName + ".useTransparentCollection();");
            }
            if (!collectionClassName.equals(Vector.class.getName())) {
                method.addLine(mappingName + ".useCollectionClass(" + collectionClassName + ".class);");
            }
        } else if (collectionMapping.isDirectMapMapping()) {
            if (policy instanceof TransparentIndirectionPolicy) {
                method.addLine(mappingName + ".useTransparentMap();");
                if (!collectionClassName.equals(IndirectMap.class.getName())) {
                    method.addLine(mappingName + ".useMapClass(" + collectionClassName + ".class);");
                }
            } else {
                method.addLine(mappingName + ".useMapClass(" + collectionClassName + ".class);");
            }
        } else if (collectionMapping.getContainerPolicy().isMapPolicy()) {
            String keyMethodName = ((org.eclipse.persistence.internal.queries.MapContainerPolicy) collectionMapping.getContainerPolicy()).getKeyName();
            if (policy instanceof TransparentIndirectionPolicy) {
                method.addLine(mappingName + ".useTransparentMap(\"" + keyMethodName + "\");");
                if (!collectionClassName.equals(IndirectMap.class.getName())) {
                    method.addLine(mappingName + ".useMapClass(" + collectionClassName + ".class, \"" + keyMethodName + "\");");
                }
            } else {
                method.addLine(mappingName + ".useMapClass(" + collectionClassName + ".class, \"" + keyMethodName + "\");");
            }
        }
        // Ordering.
        Iterator<Expression> queryKeyExpressions = collectionMapping.getOrderByQueryKeyExpressions().iterator();
        while (queryKeyExpressions.hasNext()) {
            FunctionExpression expression = (FunctionExpression) queryKeyExpressions.next();
            String queryKeyName = expression.getBaseExpression().getName();
            if (expression.getOperator().getSelector() == ExpressionOperator.Descending) {
                method.addLine(mappingName + ".addDescendingOrdering(\"" + queryKeyName + "\");");
            } else {
                method.addLine(mappingName + ".addAscendingOrdering(\"" + queryKeyName + "\");");
            }
        }
    }
}
Also used : ProxyIndirectionPolicy(org.eclipse.persistence.internal.indirection.ProxyIndirectionPolicy) ContainerIndirectionPolicy(org.eclipse.persistence.internal.indirection.ContainerIndirectionPolicy) NoIndirectionPolicy(org.eclipse.persistence.internal.indirection.NoIndirectionPolicy) TransparentIndirectionPolicy(org.eclipse.persistence.internal.indirection.TransparentIndirectionPolicy) InMemoryQueryIndirectionPolicy(org.eclipse.persistence.queries.InMemoryQueryIndirectionPolicy) IndirectionPolicy(org.eclipse.persistence.internal.indirection.IndirectionPolicy) BasicIndirectionPolicy(org.eclipse.persistence.internal.indirection.BasicIndirectionPolicy) BasicIndirectionPolicy(org.eclipse.persistence.internal.indirection.BasicIndirectionPolicy) IndirectMap(org.eclipse.persistence.indirection.IndirectMap) TransparentIndirectionPolicy(org.eclipse.persistence.internal.indirection.TransparentIndirectionPolicy) FunctionExpression(org.eclipse.persistence.internal.expressions.FunctionExpression) ContainerIndirectionPolicy(org.eclipse.persistence.internal.indirection.ContainerIndirectionPolicy) ProxyIndirectionPolicy(org.eclipse.persistence.internal.indirection.ProxyIndirectionPolicy) FunctionExpression(org.eclipse.persistence.internal.expressions.FunctionExpression) Expression(org.eclipse.persistence.expressions.Expression) NoIndirectionPolicy(org.eclipse.persistence.internal.indirection.NoIndirectionPolicy) DirectCollectionMapping(org.eclipse.persistence.mappings.DirectCollectionMapping) CollectionMapping(org.eclipse.persistence.mappings.CollectionMapping) AggregateCollectionMapping(org.eclipse.persistence.mappings.AggregateCollectionMapping)

Example 5 with IndirectionPolicy

use of org.eclipse.persistence.internal.indirection.IndirectionPolicy in project eclipselink by eclipse-ee4j.

the class EISObjectPersistenceXMLProject method buildEISOneToOneMappingDescriptor.

protected ClassDescriptor buildEISOneToOneMappingDescriptor() {
    XMLDescriptor descriptor = new XMLDescriptor();
    descriptor.setJavaClass(EISOneToOneMapping.class);
    descriptor.getInheritancePolicy().setParentClass(ObjectReferenceMapping.class);
    XMLCompositeCollectionMapping sourceToTargetKeyFieldAssociationsMapping = new XMLCompositeCollectionMapping();
    sourceToTargetKeyFieldAssociationsMapping.setReferenceClass(Association.class);
    // Handle translation of foreign key associations to hashtables.
    sourceToTargetKeyFieldAssociationsMapping.setAttributeAccessor(new AttributeAccessor() {

        @Override
        public Object getAttributeValueFromObject(Object object) {
            Map<DatabaseField, DatabaseField> sourceToTargetKeyFields = ((EISOneToOneMapping) object).getSourceToTargetKeyFields();
            List<Association> associations = new ArrayList<>(sourceToTargetKeyFields.size());
            Iterator<Map.Entry<DatabaseField, DatabaseField>> iterator = sourceToTargetKeyFields.entrySet().iterator();
            while (iterator.hasNext()) {
                Map.Entry<DatabaseField, DatabaseField> entry = iterator.next();
                associations.add(new Association(entry.getKey(), entry.getValue()));
            }
            return associations;
        }

        @Override
        public void setAttributeValueInObject(Object object, Object value) {
            EISOneToOneMapping mapping = (EISOneToOneMapping) object;
            @SuppressWarnings({ "unchecked" }) List<Association> associations = (List<Association>) value;
            mapping.setSourceToTargetKeyFields(new HashMap<>(associations.size() + 1));
            mapping.setTargetToSourceKeyFields(new HashMap<>(associations.size() + 1));
            Iterator<Association> iterator = associations.iterator();
            while (iterator.hasNext()) {
                Association association = iterator.next();
                mapping.getSourceToTargetKeyFields().put((DatabaseField) association.getKey(), (DatabaseField) association.getValue());
                mapping.getTargetToSourceKeyFields().put((DatabaseField) association.getValue(), (DatabaseField) association.getKey());
            }
        }
    });
    sourceToTargetKeyFieldAssociationsMapping.setAttributeName("sourceToTargetKeyFieldAssociations");
    sourceToTargetKeyFieldAssociationsMapping.setXPath(getPrimaryNamespaceXPath() + "foreign-key/" + getPrimaryNamespaceXPath() + "field-reference");
    descriptor.addMapping(sourceToTargetKeyFieldAssociationsMapping);
    XMLCompositeCollectionMapping foreignKeyFieldNamesMapping = new XMLCompositeCollectionMapping();
    foreignKeyFieldNamesMapping.setAttributeName("foreignKeyFields");
    foreignKeyFieldNamesMapping.setGetMethodName("getForeignKeyFields");
    foreignKeyFieldNamesMapping.setSetMethodName("setForeignKeyFields");
    foreignKeyFieldNamesMapping.setXPath(getPrimaryNamespaceXPath() + "foreign-key-fields/" + getPrimaryNamespaceXPath() + "field");
    foreignKeyFieldNamesMapping.setReferenceClass(DatabaseField.class);
    descriptor.addMapping(foreignKeyFieldNamesMapping);
    XMLDirectMapping relationshipPartnerAttributeNameMapping = new XMLDirectMapping();
    relationshipPartnerAttributeNameMapping.setAttributeName("relationshipPartnerAttributeName");
    relationshipPartnerAttributeNameMapping.setGetMethodName("getRelationshipPartnerAttributeName");
    relationshipPartnerAttributeNameMapping.setSetMethodName("setRelationshipPartnerAttributeName");
    relationshipPartnerAttributeNameMapping.setXPath(getPrimaryNamespaceXPath() + "bidirectional-target-attribute/text()");
    descriptor.addMapping(relationshipPartnerAttributeNameMapping);
    XMLCompositeObjectMapping indirectionPolicyMapping = new XMLCompositeObjectMapping();
    indirectionPolicyMapping.setReferenceClass(IndirectionPolicy.class);
    // Handle translation of NoIndirectionPolicy -> null.
    indirectionPolicyMapping.setAttributeAccessor(new AttributeAccessor() {

        @Override
        public Object getAttributeValueFromObject(Object object) {
            IndirectionPolicy policy = ((ForeignReferenceMapping) object).getIndirectionPolicy();
            if (policy instanceof NoIndirectionPolicy) {
                return null;
            }
            return policy;
        }

        @Override
        public void setAttributeValueInObject(Object object, Object value) {
            IndirectionPolicy policy = (IndirectionPolicy) value;
            if (value == null) {
                policy = new NoIndirectionPolicy();
            }
            ((ForeignReferenceMapping) object).setIndirectionPolicy(policy);
        }
    });
    indirectionPolicyMapping.setAttributeName("indirectionPolicy");
    indirectionPolicyMapping.setXPath(getPrimaryNamespaceXPath() + "indirection");
    descriptor.addMapping(indirectionPolicyMapping);
    XMLCompositeObjectMapping selectionQueryMapping = new XMLCompositeObjectMapping();
    selectionQueryMapping.setAttributeName("selectionQuery");
    selectionQueryMapping.setReferenceClass(ReadQuery.class);
    selectionQueryMapping.setAttributeAccessor(new AttributeAccessor() {

        @Override
        public Object getAttributeValueFromObject(Object object) {
            if (((ForeignReferenceMapping) object).hasCustomSelectionQuery()) {
                return ((ForeignReferenceMapping) object).getSelectionQuery();
            }
            return null;
        }

        @Override
        public void setAttributeValueInObject(Object object, Object value) {
            if (value instanceof ReadQuery) {
                ((ForeignReferenceMapping) object).setCustomSelectionQuery((ReadQuery) value);
            }
        }
    });
    selectionQueryMapping.setXPath(getPrimaryNamespaceXPath() + "selection-query");
    descriptor.addMapping(selectionQueryMapping);
    return descriptor;
}
Also used : HashMap(java.util.HashMap) IndirectionPolicy(org.eclipse.persistence.internal.indirection.IndirectionPolicy) NoIndirectionPolicy(org.eclipse.persistence.internal.indirection.NoIndirectionPolicy) XMLDescriptor(org.eclipse.persistence.oxm.XMLDescriptor) Association(org.eclipse.persistence.mappings.Association) XMLDirectMapping(org.eclipse.persistence.oxm.mappings.XMLDirectMapping) DatabaseField(org.eclipse.persistence.internal.helper.DatabaseField) XMLCompositeCollectionMapping(org.eclipse.persistence.oxm.mappings.XMLCompositeCollectionMapping) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List) NoIndirectionPolicy(org.eclipse.persistence.internal.indirection.NoIndirectionPolicy) EISOneToOneMapping(org.eclipse.persistence.eis.mappings.EISOneToOneMapping) AttributeAccessor(org.eclipse.persistence.mappings.AttributeAccessor) HashMap(java.util.HashMap) Map(java.util.Map) ReadQuery(org.eclipse.persistence.queries.ReadQuery) XMLCompositeObjectMapping(org.eclipse.persistence.oxm.mappings.XMLCompositeObjectMapping)

Aggregations

IndirectionPolicy (org.eclipse.persistence.internal.indirection.IndirectionPolicy)13 NoIndirectionPolicy (org.eclipse.persistence.internal.indirection.NoIndirectionPolicy)11 BasicIndirectionPolicy (org.eclipse.persistence.internal.indirection.BasicIndirectionPolicy)9 ContainerIndirectionPolicy (org.eclipse.persistence.internal.indirection.ContainerIndirectionPolicy)9 ProxyIndirectionPolicy (org.eclipse.persistence.internal.indirection.ProxyIndirectionPolicy)9 TransparentIndirectionPolicy (org.eclipse.persistence.internal.indirection.TransparentIndirectionPolicy)9 AttributeAccessor (org.eclipse.persistence.mappings.AttributeAccessor)9 XMLDescriptor (org.eclipse.persistence.oxm.XMLDescriptor)9 XMLCompositeCollectionMapping (org.eclipse.persistence.oxm.mappings.XMLCompositeCollectionMapping)9 XMLCompositeObjectMapping (org.eclipse.persistence.oxm.mappings.XMLCompositeObjectMapping)9 XMLDirectMapping (org.eclipse.persistence.oxm.mappings.XMLDirectMapping)9 InMemoryQueryIndirectionPolicy (org.eclipse.persistence.queries.InMemoryQueryIndirectionPolicy)9 ArrayList (java.util.ArrayList)7 List (java.util.List)7 DatabaseField (org.eclipse.persistence.internal.helper.DatabaseField)7 Association (org.eclipse.persistence.mappings.Association)7 Iterator (java.util.Iterator)6 PropertyAssociation (org.eclipse.persistence.mappings.PropertyAssociation)6 TypedAssociation (org.eclipse.persistence.mappings.TypedAssociation)6 XMLField (org.eclipse.persistence.oxm.XMLField)3