Exemplo n.º 1
0
    def from_data(ctx: 'CdmCorpusContext', object_type: 'CdmObjectType',
                  data: 'OperationBase'):
        if data is None:
            return None

        operation = ctx.corpus.make_object(
            object_type)  # type: CdmOperationBase
        operation_type = OperationTypeConvertor._from_object_type(
            object_type)  # type: CdmOperationType
        operation_name = OperationTypeConvertor._operation_type_to_string(
            CdmOperationType.COMBINE_ATTRIBUTES)
        if data.type and not StringUtils.equals_with_ignore_case(
                data.type, operation_name):
            logger.error(ctx, operation_name,
                         OperationBasePersistence.from_data.__name__, None,
                         CdmLogCode.ERR_PERSIST_PROJ_INVALID_OPS_TYPE,
                         data.type)
        else:
            operation.type = operation_type

        operation.condition = data.condition
        operation.explanation = data.explanation
        operation.source_input = data.sourceInput

        return operation
Exemplo n.º 2
0
    def from_data(
            ctx: 'CdmCorpusContext',
            data: 'OperationArrayExpansion') -> 'CdmOperationArrayExpansion':
        if not data:
            return None

        array_expansion_op = ctx.corpus.make_object(
            CdmObjectType.OPERATION_ARRAY_EXPANSION_DEF)

        if data.type and not StringUtils.equals_with_ignore_case(
                data.type,
                OperationTypeConvertor._operation_type_to_string(
                    CdmOperationType.ARRAY_EXPANSION)):
            logger.error(
                _TAG, ctx,
                '$type {} is invalid for this operation.'.format(data.type))
        else:
            array_expansion_op.type = CdmOperationType.ARRAY_EXPANSION

        if data.explanation:
            array_expansion_op.explanation = data.explanation

        array_expansion_op.start_ordinal = data.startOrdinal
        array_expansion_op.end_ordinal = data.endOrdinal

        return array_expansion_op
Exemplo n.º 3
0
    def from_data(
        ctx: 'CdmCorpusContext', data: 'OperationAddSupportingAttribute'
    ) -> 'CdmOperationAddSupportingAttribute':
        if not data:
            return None

        add_supporting_attribute_op = ctx.corpus.make_object(
            CdmObjectType.OPERATION_ADD_SUPPORTING_ATTRIBUTE_DEF)

        if data.type and not StringUtils.equals_with_ignore_case(
                data.type,
                OperationTypeConvertor._operation_type_to_string(
                    CdmOperationType.ADD_SUPPORTING_ATTRIBUTE)):
            logger.error(
                _TAG, ctx,
                '$type {} is invalid for this operation.'.format(data.type))
        else:
            add_supporting_attribute_op.type = CdmOperationType.ADD_SUPPORTING_ATTRIBUTE

        if data.explanation:
            add_supporting_attribute_op.explanation = data.explanation

        if data.supportingAttribute:
            add_supporting_attribute_op.supporting_attribute = utils.create_attribute(
                ctx, data.supportingAttribute)

        return add_supporting_attribute_op
    def from_data(
            ctx: 'CdmCorpusContext', data: 'OperationIncludeAttributes'
    ) -> 'CdmOperationIncludeAttributes':
        if not data:
            return None

        include_attributes_op = ctx.corpus.make_object(
            CdmObjectType.OPERATION_INCLUDE_ATTRIBUTES_DEF)

        if data.type and not StringUtils.equals_with_ignore_case(
                data.type,
                OperationTypeConvertor._operation_type_to_string(
                    CdmOperationType.INCLUDE_ATTRIBUTES)):
            logger.error(
                _TAG, ctx,
                '$type {} is invalid for this operation.'.format(data.type))
        else:
            include_attributes_op.type = CdmOperationType.INCLUDE_ATTRIBUTES

        if data.explanation:
            include_attributes_op.explanation = data.explanation

        include_attributes_op.include_attributes = data.includeAttributes

        return include_attributes_op
Exemplo n.º 5
0
    def from_data(
            ctx: 'CdmCorpusContext', data: 'OperationCombineAttributes'
    ) -> 'CdmOperationCombineAttributes':
        if not data:
            return None

        combine_attributes_op = ctx.corpus.make_object(
            CdmObjectType.OPERATION_COMBINE_ATTRIBUTES_DEF)

        if data.type and not StringUtils.equals_with_ignore_case(
                data.type,
                OperationTypeConvertor._operation_type_to_string(
                    CdmOperationType.COMBINE_ATTRIBUTES)):
            logger.error(
                _TAG, ctx,
                '$type {} is invalid for this operation.'.format(data.type))
        else:
            combine_attributes_op.type = CdmOperationType.COMBINE_ATTRIBUTES

        if data.explanation:
            combine_attributes_op.explanation = data.explanation

        combine_attributes_op.take = data.take
        combine_attributes_op.merge_into = utils.create_attribute(
            ctx, data.mergeInto)

        return combine_attributes_op
    def from_data(ctx: 'CdmCorpusContext',
                  data: 'OperationAlterTraits') -> 'CdmOperationAlterTraits':
        if not data:
            return None

        alter_traits_op = OperationBasePersistence.from_data(
            ctx, CdmObjectType.OPERATION_ALTER_TRAITS_DEF,
            data)  # type: CdmOperationAlterTraits

        alter_traits_op.traits_to_add = utils.create_trait_reference_array(
            ctx, data.traitsToAdd)
        alter_traits_op.traits_to_remove = utils.create_trait_reference_array(
            ctx, data.traitsToRemove)
        alter_traits_op.arguments_contain_wildcards = data.argumentsContainWildcards

        if isinstance(data.applyTo, str):
            alter_traits_op.apply_to = [data.applyTo]
        elif isinstance(data.applyTo, list):
            alter_traits_op.apply_to = data.applyTo
        elif data.applyTo is not None:
            logger.error(ctx, _TAG,
                         OperationAlterTraitsPersistence.from_data.__name__,
                         None, CdmLogCode.ERR_PERSIST_PROJ_UNSUPPORTED_PROP,
                         'applyTo', 'string or list of strings')

        return alter_traits_op
Exemplo n.º 7
0
 def _add(self, pas: '******') -> None:
     """Add to the collection"""
     if not pas or not pas._current_resolved_attribute or not pas._current_resolved_attribute.resolved_name:
         logger.error(self._ctx, ProjectionAttributeStateSet._add.__name__,
                      self._TAG, None,
                      CdmLogCode.ERR_PROJ_INVALID_ATTR_STATE)
     else:
         self._states.append(pas)
Exemplo n.º 8
0
 def maximum(self, val: str) -> None:
     if not CardinalitySettings._is_maximum_valid(val):
         logger.error(self._TAG, self._ctx,
                      'Invalid maximum cardinality {}.'.format(val))
     else:
         self._maximum = val
         self._maximum_number = self._get_number(self._maximum,
                                                 self._default_maximum)
 def _add(self, pas: '******') -> None:
     """Add to the collection"""
     if not pas or not pas._current_resolved_attribute or not pas._current_resolved_attribute.resolved_name:
         logger.error(
             self._TAG, self._ctx,
             'Invalid ProjectionAttributeState provided for addition to the Set. Add operation failed.',
             ProjectionAttributeStateSet._add.__name__)
     else:
         self._states.append(pas)
Exemplo n.º 10
0
 def maximum(self, val: str) -> None:
     if not CardinalitySettings._is_maximum_valid(val):
         logger.error(self._ctx, self._TAG, 'maximum',
                      self._owner.at_corpus_path,
                      CdmLogCode.ERR_VALDN_INVALID_MAX_CARDINALITY, val)
     else:
         self._maximum = val
         self._maximum_number = self._get_number(self._maximum,
                                                 self._default_maximum)
Exemplo n.º 11
0
    def _get_number(self, val: str, default_value: int) -> int:
        """Converts the string cardinality to number"""
        if StringUtils.equals_with_ignore_case(val, '*'):
            return CardinalitySettings._infinite_maximum

        try:
            number = int(val)
            return number
        except ValueError:
            logger.error(self._ctx, self._TAG, '_get_number',
                         self._owner.at_corpus_path,
                         CdmLogCode.ERR_PROJ_STRING_ERROR, val, default_value)
            # defaults to min:max DefaultMinimum:DefaultMaximum in the invalid values
            return default_value
Exemplo n.º 12
0
    def minimum(self, val: str) -> None:
        from cdm.objectmodel import CdmTypeAttributeDefinition

        if not CardinalitySettings._is_minimum_valid(val):
            logger.error(self._TAG, self._ctx,
                         'Invalid minimum cardinality {}.'.format(val))
        else:
            self._minimum = val
            self._minimum_number = self._get_number(self._minimum,
                                                    self._default_minimum)

        # In the case of type attributes, a '0' minimum cardinality represents a nullable attribute
        if self._owner and isinstance(self._owner, CdmTypeAttributeDefinition):
            self._owner.is_nullable = self._minimum_number == 0
    def from_data(ctx: 'CdmCorpusContext', data: 'OperationAddAttributeGroup') -> 'CdmOperationAddAttributeGroup':
        if not data:
            return None

        add_attribute_group_op = ctx.corpus.make_object(CdmObjectType.OPERATION_ADD_ATTRIBUTE_GROUP_DEF)  # type: CdmOperationAddAttributeGroup

        if data.type and not StringUtils.equals_with_ignore_case(data.type, OperationTypeConvertor._operation_type_to_string(CdmOperationType.ADD_ATTRIBUTE_GROUP)):
            logger.error(_TAG, ctx, '$type {} is invalid for this operation.'.format(data.type))
        else:
            add_attribute_group_op.type = CdmOperationType.ADD_ATTRIBUTE_GROUP

        add_attribute_group_op.attribute_group_name = data.attributeGroupName
        add_attribute_group_op.explanation = data.explanation

        return add_attribute_group_op
Exemplo n.º 14
0
    def minimum(self, val: str) -> None:
        from cdm.objectmodel import CdmTypeAttributeDefinition

        if not CardinalitySettings._is_minimum_valid(val):
            logger.error(self._ctx, self._TAG, 'minimum',
                         self._owner.at_corpus_path,
                         CdmLogCode.ERR_VALDN_INVALID_MIN_CARDINALITY, val)
        else:
            self._minimum = val
            self._minimum_number = self._get_number(self._minimum,
                                                    self._default_minimum)

        # In the case of type attributes, a '0' minimum cardinality represents a nullable attribute
        if self._owner and isinstance(self._owner, CdmTypeAttributeDefinition):
            self._owner.is_nullable = self._minimum_number == 0
Exemplo n.º 15
0
    def _get_number(self, val: str, default_value: int) -> int:
        """Converts the string cardinality to number"""
        if StringUtils.equals_with_ignore_case(val, '*'):
            return CardinalitySettings._infinite_maximum

        try:
            number = int(val)
            return number
        except ValueError:
            logger.error(
                self._TAG, self._ctx,
                'Unable to get number for string \'{}\'. Falling to default value {}.'
                .format(val, default_value))
            # defaults to min:max DefaultMinimum:DefaultMaximum in the invalid values
            return default_value
Exemplo n.º 16
0
    def from_data(ctx: 'CdmCorpusContext', data: 'OperationRenameAttributes') -> 'CdmOperationRenameAttributes':
        if not data:
            return None

        rename_attributes_op = ctx.corpus.make_object(CdmObjectType.OPERATION_RENAME_ATTRIBUTES_DEF)

        if data.type and not StringUtils.equals_with_ignore_case(data.type, OperationTypeConvertor._operation_type_to_string(CdmOperationType.RENAME_ATTRIBUTES)):
            logger.error(_TAG, ctx, '$type {} is invalid for this operation.'.format(data.type))
        else:
            rename_attributes_op.type = CdmOperationType.RENAME_ATTRIBUTES

        if data.explanation:
            rename_attributes_op.explanation = data.explanation

        rename_attributes_op.rename_format = data.renameFormat
        rename_attributes_op.apply_to = data.applyTo

        return rename_attributes_op
Exemplo n.º 17
0
    def from_data(
            ctx: 'CdmCorpusContext', data: 'OperationRenameAttributes'
    ) -> 'CdmOperationRenameAttributes':
        if not data:
            return None

        rename_attributes_op = OperationBasePersistence.from_data(
            ctx, CdmObjectType.OPERATION_RENAME_ATTRIBUTES_DEF,
            data)  # type: CdmOperationRenameAttributes

        rename_attributes_op.rename_format = data.renameFormat
        if isinstance(data.applyTo, str):
            rename_attributes_op.apply_to = [data.applyTo]
        elif isinstance(data.applyTo, list):
            rename_attributes_op.apply_to = data.applyTo
        elif data.applyTo is not None:
            logger.error(
                ctx, _TAG,
                OperationRenameAttributesPersistence.from_data.__name__, None,
                CdmLogCode.ERR_PERSIST_PROJ_UNSUPPORTED_PROP)

        return rename_attributes_op
Exemplo n.º 18
0
    def from_data(
            ctx: 'CdmCorpusContext', data: 'OperationRenameAttributes'
    ) -> 'CdmOperationRenameAttributes':
        if not data:
            return None

        rename_attributes_op = OperationBasePersistence.from_data(
            ctx, CdmObjectType.OPERATION_RENAME_ATTRIBUTES_DEF,
            data)  # type: CdmOperationRenameAttributes

        rename_attributes_op.rename_format = data.renameFormat
        if isinstance(data.applyTo, str):
            rename_attributes_op.apply_to = [data.applyTo]
        elif isinstance(data.applyTo, list):
            rename_attributes_op.apply_to = data.applyTo
        elif data.applyTo is not None:
            logger.error(
                _TAG, ctx,
                'Unsupported: applyTo property type should be string or List<string>.'
            )

        return rename_attributes_op
Exemplo n.º 19
0
    def from_data(ctx: 'CdmCorpusContext', object_type: 'CdmObjectType',
                  data: 'OperationBase'):
        if data is None:
            return None

        operation = ctx.corpus.make_object(
            object_type)  # type: CdmOperationBase
        operation_type = OperationTypeConvertor._from_object_type(
            object_type)  # type: CdmOperationType
        operation_name = OperationTypeConvertor._operation_type_to_string(
            CdmOperationType.COMBINE_ATTRIBUTES)
        if data.type and not StringUtils.equals_with_ignore_case(
                data.type, operation_name):
            logger.error(
                operation_name, ctx,
                '$type {} is invalid for this operation.'.format(data.type))
        else:
            operation.type = operation_type

        operation.condition = data.condition
        operation.explanation = data.explanation
        operation.source_input = data.sourceInput

        return operation
Exemplo n.º 20
0
    def from_data(
            ctx: 'CdmCorpusContext',
            data: Union[str, 'EntityReference',
                        'Projection']) -> 'CdmProjection':
        from cdm.persistence.cdmfolder.entity_reference_persistence import EntityReferencePersistence

        if not data:
            return None

        projection = ctx.corpus.make_object(CdmObjectType.PROJECTION_DEF)

        source = EntityReferencePersistence.from_data(ctx, data.get('source'))

        if data.explanation:
            projection.explanation = data.explanation

        if data.condition:
            projection.condition = data.condition

        projection.run_sequentially = data.runSequentially

        if data.operations is not None:
            operation_jsons = data.operations
            for operation_json in operation_jsons:
                type = operation_json.get('$type')

                if type == 'addCountAttribute':
                    add_count_attribute_op = OperationAddCountAttributePersistence.from_data(
                        ctx, operation_json)
                    projection.operations.append(add_count_attribute_op)
                elif type == 'addSupportingAttribute':
                    add_supporting_attribute_op = OperationAddSupportingAttributePersistence.from_data(
                        ctx, operation_json)
                    projection.operations.append(add_supporting_attribute_op)
                elif type == 'addTypeAttribute':
                    add_type_attribute_op = OperationAddTypeAttributePersistence.from_data(
                        ctx, operation_json)
                    projection.operations.append(add_type_attribute_op)
                elif type == 'excludeAttributes':
                    exclude_attributes_op = OperationExcludeAttributesPersistence.from_data(
                        ctx, operation_json)
                    projection.operations.append(exclude_attributes_op)
                elif type == 'arrayExpansion':
                    array_expansion_op = OperationArrayExpansionPersistence.from_data(
                        ctx, operation_json)
                    projection.operations.append(array_expansion_op)
                elif type == 'combineAttributes':
                    combine_attributes_op = OperationCombineAttributesPersistence.from_data(
                        ctx, operation_json)
                    projection.operations.append(combine_attributes_op)
                elif type == 'renameAttributes':
                    rename_attributes_op = OperationRenameAttributesPersistence.from_data(
                        ctx, operation_json)
                    projection.operations.append(rename_attributes_op)
                elif type == 'replaceAsForeignKey':
                    replace_as_foreign_key_op = OperationReplaceAsForeignKeyPersistence.from_data(
                        ctx, operation_json)
                    projection.operations.append(replace_as_foreign_key_op)
                elif type == 'includeAttributes':
                    include_attributes_op = OperationIncludeAttributesPersistence.from_data(
                        ctx, operation_json)
                    projection.operations.append(include_attributes_op)
                elif type == 'addAttributeGroup':
                    add_attribute_group_op = OperationAddAttributeGroupPersistence.from_data(
                        ctx, operation_json)
                    projection.operations.append(add_attribute_group_op)
                else:
                    logger.error(_TAG, ctx,
                                 'Invalid operation type \'{}\'.'.format(type),
                                 ProjectionPersistence.from_data.__name__)

        projection.source = source

        return projection