def apply_default_behavior(entity_attr: 'CdmEntityAttributeDefinition', fk_attr_name: Optional[str], attr_group_name: Optional[str]): '''Applies the replaceAsForeignKey and addAttributeGroup operations to the entity attribute provided.''' ctx = entity_attr.ctx projection = CdmProjection(ctx) # Link for the Source property documentation. # https://docs.microsoft.com/en-us/common-data-model/sdk/convert-logical-entities-resolved-entities#source projection.source = entity_attr.entity # Link for the RunSequentially property documentation. # https://docs.microsoft.com/en-us/common-data-model/sdk/convert-logical-entities-resolved-entities#run-sequentially projection.run_sequentially = True entity_attr.entity = CdmEntityReference(ctx, projection, False) if fk_attr_name: foreign_key_attr = CdmTypeAttributeDefinition(ctx, fk_attr_name) foreign_key_attr.data_type = CdmDataTypeReference(ctx, 'entityId', True) # Link for the ReplaceAsForeignKey operation documentation. # https://docs.microsoft.com/en-us/common-data-model/sdk/projections/replaceasforeignkey replace_as_fk_operation = CdmOperationReplaceAsForeignKey(ctx) replace_as_fk_operation.condition = 'referenceOnly' replace_as_fk_operation.reference = 'addressLine' replace_as_fk_operation.replace_with = foreign_key_attr projection.operations.append(replace_as_fk_operation) if attr_group_name: # Link for the AddAttributeGroup operation documentation. # https://docs.microsoft.com/en-us/common-data-model/sdk/projections/addattributegroup add_attr_group_operation = CdmOperationAddAttributeGroup(ctx) add_attr_group_operation.condition = 'structured' add_attr_group_operation.attribute_group_name = attr_group_name projection.operations.append(add_attr_group_operation)
def _create_projection_with_operation_collection(self, corpus: 'CdmCorpusDefinition', owner: 'CdmObject') -> 'CdmProjection': """Create a projection object with operations""" projection = corpus.make_object(CdmObjectType.PROJECTION_DEF) projection.source = corpus.make_object(CdmObjectType.ENTITY_REF, 'TestSource', True) # AddCountAttribute Operation add_count_attribute_op = CdmOperationAddCountAttribute(corpus.ctx) add_count_attribute_op.count_attribute = corpus.make_object(CdmObjectType.TYPE_ATTRIBUTE_DEF) projection.operations.append(add_count_attribute_op) # AddSupportingAttribute Operation add_supporting_attribute_op = CdmOperationAddSupportingAttribute(corpus.ctx) add_supporting_attribute_op.supporting_attribute = corpus.make_object(CdmObjectType.TYPE_ATTRIBUTE_DEF) projection.operations.append(add_supporting_attribute_op) # AddTypeAttribute Operation add_type_attribute_op = CdmOperationAddTypeAttribute(corpus.ctx) add_type_attribute_op.type_attribute = corpus.make_object(CdmObjectType.TYPE_ATTRIBUTE_DEF) projection.operations.append(add_type_attribute_op) # ExcludeAttributes Operation exclude_attributes_op = CdmOperationExcludeAttributes(corpus.ctx) exclude_attributes_op.exclude_attributes = [] exclude_attributes_op.exclude_attributes.append('testAttribute1') projection.operations.append(exclude_attributes_op) # ArrayExpansion Operation array_expansion_op = CdmOperationArrayExpansion(corpus.ctx) array_expansion_op.start_ordinal = 0 array_expansion_op.end_ordinal = 1 projection.operations.append(array_expansion_op) # CombineAttributes Operation combine_attributes_op = CdmOperationCombineAttributes(corpus.ctx) combine_attributes_op.select = [] combine_attributes_op.merge_into = corpus.make_object(CdmObjectType.TYPE_ATTRIBUTE_DEF) combine_attributes_op.select.append('testAttribute1') projection.operations.append(combine_attributes_op) # RenameAttributes Operation rename_attributes_op = CdmOperationRenameAttributes(corpus.ctx) rename_attributes_op.rename_format = '{m}' projection.operations.append(rename_attributes_op) # ReplaceAsForeignKey Operation replace_as_foreign_key_op = CdmOperationReplaceAsForeignKey(corpus.ctx) replace_as_foreign_key_op.reference = 'testAttribute1' replace_as_foreign_key_op.replace_with = corpus.make_object(CdmObjectType.TYPE_ATTRIBUTE_DEF, 'testForeignKey', False) projection.operations.append(replace_as_foreign_key_op) # IncludeAttributes Operation include_attributes_op = CdmOperationIncludeAttributes(corpus.ctx) include_attributes_op.include_attributes = [] include_attributes_op.include_attributes.append('testAttribute1') projection.operations.append(include_attributes_op) return projection