Exemplo n.º 1
0
    async def to_data(instance: 'CdmManifestDefinition',
                      res_opt: 'ResolveOptions',
                      options: 'CopyOptions') -> Optional['Model']:
        result = Model()

        # process_traits_and_annotations_to_data also processes extensions.
        await utils.process_traits_and_annotations_to_data(
            instance.ctx, result, instance.exhibits_traits)

        result.name = instance.manifest_name
        result.description = instance.explanation
        result.modifiedTime = utils.get_formatted_date_string(
            instance.last_file_modified_time)
        result.lastChildFileModifiedTime = utils.get_formatted_date_string(
            instance.last_child_file_modified_time)
        result.lastFileStatusCheckTime = utils.get_formatted_date_string(
            instance.last_file_status_check_time)
        result.documentVersion = instance.document_version

        t2pm = TraitToPropertyMap(instance)

        result.isHidden = bool(
            t2pm._fetch_trait_reference('is.hidden')) or None

        application_trait = t2pm._fetch_trait_reference('is.managedBy')
        if application_trait:
            result.application = application_trait.arguments[0].value

        version_trait = t2pm._fetch_trait_reference(
            'is.modelConversion.modelVersion')
        if version_trait:
            result.version = version_trait.arguments[0].value
        else:
            result.version = '1.0'

        culture_trait = t2pm._fetch_trait_reference('is.partition.culture')
        if culture_trait:
            result.culture = culture_trait.arguments[0].value

        reference_entity_locations = {}
        reference_models = OrderedDict()

        reference_models_trait = t2pm._fetch_trait_reference(
            'is.modelConversion.referenceModelMap')

        if reference_models_trait:
            ref_models = reference_models_trait.arguments[0].value

            for element in ref_models:
                reference_models[element.id] = element.location
                reference_entity_locations[element.location] = element.id

        if instance.entities:
            result.entities = []
            # Schedule processing of each entity to be added to the manifest
            for entity in instance.entities:
                element = None
                if entity.object_type == CdmObjectType.LOCAL_ENTITY_DECLARATION_DEF:
                    element = await LocalEntityDeclarationPersistence.to_data(
                        entity, instance, res_opt, options)
                elif entity.object_type == CdmObjectType.REFERENCED_ENTITY_DECLARATION_DEF:
                    element = await ReferencedEntityDeclarationPersistence.to_data(
                        entity, res_opt, options)

                    location = instance.ctx.corpus.storage.corpus_path_to_adapter_path(
                        entity.entity_path)

                    if StringUtils.is_blank_by_cdm_standard(location):
                        logger.error(
                            instance.ctx, _TAG, 'to_data',
                            instance.at_corpus_path, CdmLogCode.
                            ERR_PERSIST_MODELJSON_INVALID_ENTITY_PATH,
                            entity.entity_name)
                        element = None

                    reference_entity = element  # type: ReferenceEntity
                    if reference_entity:
                        # path separator can differ depending on the adapter, cover the case where path uses '/' or '\'
                        last_slash_location = location.rfind(
                            '/') if location.rfind('/') > location.rfind(
                                '\\') else location.rfind('\\')
                        if last_slash_location > 0:
                            location = location[:last_slash_location]

                        if reference_entity.modelId:
                            saved_location = reference_models.get(
                                reference_entity.modelId)
                            if saved_location is not None and saved_location != location:
                                logger.error(
                                    instance.ctx, 'to_data',
                                    instance.at_corpus_path, _TAG, CdmLogCode.
                                    ERR_PERSIST_MODELJSON_MODEL_ID_DUPLICATION)
                                element = None
                            elif saved_location is None:
                                reference_models[
                                    reference_entity.modelId] = location
                                reference_entity_locations[
                                    location] = reference_entity.modelId
                        elif not reference_entity.modelId and location in reference_entity_locations:
                            reference_entity.modelId = reference_entity_locations[
                                location]
                        else:
                            reference_entity.modelId = str(uuid.uuid4())
                            reference_models[
                                reference_entity.modelId] = location
                            reference_entity_locations[
                                location] = reference_entity.modelId

                if element:
                    result.entities.append(element)
                else:
                    logger.error(
                        instance.ctx, _TAG, 'to_data', instance.at_corpus_path,
                        CdmLogCode.
                        ERR_PERSIST_MODELJSON_ENTITY_DECLARATION_CONVERSION_ERROR,
                        entity.entity_name)

        if reference_models:
            result.referenceModels = []
            for value, key in reference_models.items():
                model = ReferenceModel()
                model.id = value
                model.location = key
                result.referenceModels.append(model)

        if instance.relationships is not None and len(
                instance.relationships) > 0:
            result.relationships = []  # type: List[SingleKeyRelationship]
            for cdm_relationship in instance.relationships:
                relationship = await RelationshipPersistence.to_data(
                    cdm_relationship, res_opt, options, instance.ctx)
                if relationship is not None:
                    result.relationships.append(relationship)

        result.imports = []

        if instance.imports:
            for element in instance.imports:
                import_obj = ImportPersistence.to_data(element, res_opt,
                                                       options)
                if import_obj:
                    result.imports.append(import_obj)

        # Importing foundations.cdm.json to resolve trait properly on manifest
        if instance.imports is None or instance.imports.item(
                Constants._FOUNDATIONS_CORPUS_PATH,
                check_moniker=False) is None:
            foundations_import = Import()
            foundations_import.corpusPath = Constants._FOUNDATIONS_CORPUS_PATH
            result.imports.append(foundations_import)

        return result
Exemplo n.º 2
0
    async def to_data(instance: 'CdmManifestDefinition', res_opt: 'ResolveOptions', options: 'CopyOptions') -> Optional['Model']:
        result = Model()

        # process_traits_and_annotations_to_data also processes extensions.
        utils.process_traits_and_annotations_to_data(instance.ctx, result, instance.exhibits_traits)

        result.name = instance.manifest_name
        result.description = instance.explanation
        result.modifiedTime = utils.get_formatted_date_string(instance.last_file_modified_time)
        result.lastChildFileModifiedTime = utils.get_formatted_date_string(instance.last_child_file_modified_time)
        result.lastFileStatusCheckTime = utils.get_formatted_date_string(instance.last_file_status_check_time)
        result.documentVersion = instance.document_version

        t2pm = TraitToPropertyMap(instance)

        result.isHidden = bool(t2pm._fetch_trait_reference('is.hidden')) or None

        application_trait = t2pm._fetch_trait_reference('is.managedBy')
        if application_trait:
            result.application = application_trait.arguments[0].value

        version_trait = t2pm._fetch_trait_reference('is.modelConversion.modelVersion')
        if version_trait:
            result.version = version_trait.arguments[0].value
        else:
            result.version = '1.0'

        culture_trait = t2pm._fetch_trait_reference('is.partition.culture')
        if culture_trait:
            result.culture = culture_trait.arguments[0].value

        reference_entity_locations = {}
        reference_models = OrderedDict()

        reference_models_trait = t2pm._fetch_trait_reference('is.modelConversion.referenceModelMap')

        if reference_models_trait:
            ref_models = reference_models_trait.arguments[0].value

            for element in ref_models:
                reference_models[element.id] = element.location
                reference_entity_locations[element.location] = element.id

        if instance.entities:
            result.entities = []
            # Schedule processing of each entity to be added to the manifest
            for entity in instance.entities:
                element = None
                if entity.object_type == CdmObjectType.LOCAL_ENTITY_DECLARATION_DEF:
                    element = await LocalEntityDeclarationPersistence.to_data(entity, instance, res_opt, options)
                elif entity.object_type == CdmObjectType.REFERENCED_ENTITY_DECLARATION_DEF:
                    element = await ReferencedEntityDeclarationPersistence.to_data(entity, res_opt, options)

                    location = instance.ctx.corpus.storage.corpus_path_to_adapter_path(entity.entity_path)

                    if not location:
                        logger.error(_TAG, instance.ctx, 'Invalid entity path set in entity {}'.format(entity.entity_name))
                        element = None

                    reference_entity = element  # type: ReferenceEntity
                    if reference_entity:
                        location = location[:location.rfind('/')]

                        if reference_entity.modelId:
                            saved_location = reference_models.get(reference_entity.modelId)
                            if saved_location is not None and saved_location != location:
                                logger.error(_TAG, instance.ctx, 'Same ModelId pointing to different locations')
                                element = None
                            elif saved_location is None:
                                reference_models[reference_entity.modelId] = location
                                reference_entity_locations[location] = reference_entity.modelId
                        elif not reference_entity.modelId and location in reference_entity_locations:
                            reference_entity.modelId = reference_entity_locations[location]
                        else:
                            reference_entity.modelId = str(uuid.uuid4())
                            reference_models[reference_entity.modelId] = location
                            reference_entity_locations[location] = reference_entity.modelId

                if element:
                    result.entities.append(element)
                else:
                    logger.error(_TAG, instance.ctx,
                                 'There was an error while trying to convert {}\'s entity declaration to model json format.'.format(entity.entity_name))

        if reference_models:
            result.referenceModels = []
            for value, key in reference_models.items():
                model = ReferenceModel()
                model.id = value
                model.location = key
                result.referenceModels.append(model)

        if instance.relationships is not None and len(instance.relationships) > 0:
            result.relationships = []  # type: List[SingleKeyRelationship]
            for cdm_relationship in instance.relationships:
                relationship = await RelationshipPersistence.to_data(cdm_relationship, res_opt, options, instance.ctx)
                if relationship is not None:
                    result.relationships.append(relationship)
                else:
                    logger.error(_TAG, instance.ctx, 'There was an error while trying to convert cdm relationship to model.json relationship.')
                    return None

        if instance.imports:
            result.imports = []
            for element in instance.imports:
                import_obj = ImportPersistence.to_data(element, res_opt, options)
                if import_obj:
                    result.imports.append(import_obj)

        return result