Esempio n. 1
0
        internal PropertyDefinition(
            string name,
            string type,
            string defaultValue = null,
            bool isCollection   = false,
            XElement element    = null)
        {
            if (!object.ReferenceEquals(name, IdName))
            {
                if (DefaultComparer.NameEquals(name, Id.Name))
                {
                    ThrowHelper.ThrowInvalidOperation(ExceptionMessages.PropertyNameIsReserved(Id.Name), element);
                }

                if (DefaultComparer.NameEquals(name, AttributeNames.Tag))
                {
                    ThrowHelper.ThrowInvalidOperation(ExceptionMessages.PropertyNameIsReserved(AttributeNames.Tag), element);
                }
            }

            Name         = name;
            Type         = type;
            DefaultValue = defaultValue;
            IsCollection = isCollection;
        }
Esempio n. 2
0
        public RecordCollection Records()
        {
            XElement documentElement = XDocument.FirstElement();

            if (documentElement == null ||
                !DefaultComparer.NameEquals(documentElement, ElementNames.Document))
            {
                ThrowInvalidOperation(ErrorMessages.MissingElement(ElementNames.Document));
            }

            string versionText = documentElement.AttributeValueOrDefault(AttributeNames.Version);

            if (versionText != null)
            {
                if (!Version.TryParse(versionText, out Version version))
                {
                    ThrowInvalidOperation(ErrorMessages.InvalidDocumentVersion());
                }
                else if (version > SchemaVersion)
                {
                    ThrowInvalidOperation(ErrorMessages.DocumentVersionIsNotSupported(version, SchemaVersion));
                }
            }

            XElement entitiesElement = EntitiesElement(documentElement);

            if (entitiesElement != null)
            {
                return(ReadRecords(entitiesElement.Elements()));
            }
            else
            {
                return(Empty.RecordCollection);
            }
        }
Esempio n. 3
0
        private Record CreateRecord(XElement element)
        {
            string id = null;

            Collection <IPropertyOperation> operations = null;

            foreach (XAttribute attribute in element.Attributes())
            {
                if (DefaultComparer.NameEquals(attribute, AttributeNames.Id))
                {
                    id = GetValue(attribute);
                }
                else
                {
                    IPropertyOperation operation = CreateOperationFromAttribute(element, attribute);

                    (operations ?? (operations = new Collection <IPropertyOperation>())).Add(operation);
                }
            }

            Record record = CreateRecord(id);

            operations?.ExecuteAll(record);

            ExecuteChildOperations(element, record);

            ExecutePendingOperations(record);

            foreach (PropertyDefinition property in Entity.AllProperties())
            {
                if (property.DefaultValue != null)
                {
                    if (!record.ContainsProperty(property.Name))
                    {
                        if (property.IsCollection)
                        {
                            record[property.Name] = new List <object>()
                            {
                                property.DefaultValue
                            };
                        }
                        else
                        {
                            record[property.Name] = property.DefaultValue;
                        }
                    }
                }
                else if (ShouldCheckRequiredProperty &&
                         property.IsRequired &&
                         !record.ContainsProperty(property.Name))
                {
                    Throw(ErrorMessages.PropertyIsRequired(property.Name));
                }
            }

            return(record);
        }
Esempio n. 4
0
        private XElement DocumentElement()
        {
            XElement element = XDocument.Elements().FirstOrDefault();

            if (element == null || !DefaultComparer.NameEquals(element, ElementNames.Document))
            {
                ThrowHelper.ThrowInvalidOperation(ExceptionMessages.MissingElement(ElementNames.Document));
            }

            return(element);
        }
Esempio n. 5
0
        private Operation CreateOperationFromAttribute(
            XElement element,
            ElementKind kind,
            XAttribute attribute,
            bool throwOnId = false)
        {
            string attributeName = attribute.LocalName();

            if (throwOnId &&
                DefaultComparer.NameEquals(attributeName, AttributeNames.Id))
            {
                Throw(ErrorMessages.CannotUseOperationOnProperty(element, attributeName));
            }

            PropertyDefinition property;

            string name = attribute.LocalName();

            if (name == PropertyDefinition.TagsName)
            {
                property = PropertyDefinition.Tags;
            }
            else
            {
                property = GetProperty(attribute);
            }

            switch (kind)
            {
            case ElementKind.With:
            {
                return(new Operation(property, GetValue(attribute), _depth, OperationKind.With));
            }

            case ElementKind.Without:
            {
                if (!property.IsCollection)
                {
                    Throw(ErrorMessages.CannotUseOperationOnNonCollectionProperty(element, property.Name));
                }

                return(new Operation(property, GetValue(attribute), _depth, OperationKind.Without));
            }

            default:
            {
                Debug.Assert(kind == ElementKind.New, kind.ToString());

                return(new Operation(property, GetValue(attribute), _depth, OperationKind.With));
            }
            }
        }
Esempio n. 6
0
        internal Variable FindVariable(string name)
        {
            if (Variables != null)
            {
                Variable variable = Variables.FirstOrDefault(f => DefaultComparer.NameEquals(name, f.Name));

                if (variable != null)
                {
                    return(variable);
                }
            }

            return(Entity.FindVariable(name));
        }
Esempio n. 7
0
        internal Variable FindVariable(string name)
        {
            if (_variables != null)
            {
                Variable variable = _variables.FirstOrDefault(f => DefaultComparer.NameEquals(name, f.Name));

                if (!variable.IsDefault)
                {
                    return(variable);
                }
            }

            return(_entityDefinition.FindVariable(name));
        }
Esempio n. 8
0
        private Record CreateRecord(XElement element)
        {
            string id = null;

            Collection <Operation> operations = null;

            foreach (XAttribute attribute in element.Attributes())
            {
                if (DefaultComparer.NameEquals(attribute, AttributeNames.Id))
                {
                    id = GetValue(attribute);
                }
                else
                {
                    Operation operation = CreateOperationFromAttribute(element, ElementKind.New, attribute);

                    (operations ?? (operations = new Collection <Operation>())).Add(operation);
                }
            }

            Record record = CreateRecord(id);

            if (operations != null)
            {
                ExecuteAll(operations, record);
            }

            ExecuteChildOperations(element, record);

            ExecutePendingOperations(record);

            foreach (PropertyDefinition property in _entityDefinition.AllProperties())
            {
                if (property.DefaultValue != null)
                {
                    if (!record.ContainsProperty(property.Name))
                    {
                        record[property.Name] = property.DefaultValue;
                    }
                }
                else if (_state == State.Records &&
                         property.IsRequired &&
                         !record.ContainsProperty(property.Name))
                {
                    Throw(ErrorMessages.PropertyIsRequired(property.Name));
                }
            }

            return(record);
        }
Esempio n. 9
0
        private IPropertyOperation CreateOperationFromAttribute(
            XElement element,
            XAttribute attribute,
            bool throwOnId  = false,
            bool throwOnTag = false,
            bool throwOnSet = false,
            bool throwOnAdd = false)
        {
            string attributeName = attribute.LocalName();

            if (throwOnId &&
                DefaultComparer.NameEquals(attributeName, AttributeNames.Id))
            {
                Throw(ErrorMessages.CannotUseCommandOnProperty(element, attributeName));
            }

            if (DefaultComparer.NameEquals(attributeName, AttributeNames.Tag))
            {
                if (throwOnTag)
                {
                    Throw(ErrorMessages.CannotUseCommandOnProperty(element, attributeName));
                }

                return(new AddTagOperation(GetValue(attribute), Depth));
            }

            PropertyDefinition property = GetProperty(attribute);

            if (property.IsCollection)
            {
                if (throwOnAdd)
                {
                    Throw(ErrorMessages.CannotUseCommandOnCollectionProperty(element, property.Name));
                }

                return(new AddItemOperation(property, GetValue(attribute), Depth));
            }
            else
            {
                if (throwOnSet)
                {
                    Throw(ErrorMessages.CannotUseCommandOnNonCollectionProperty(element, property.Name));
                }

                return(new SetOperation(property, GetValue(attribute), Depth));
            }
        }
Esempio n. 10
0
        private PropertyDefinition GetProperty(XAttribute attribute)
        {
            string propertyName = attribute.LocalName();

            if (DefaultComparer.NameEquals(propertyName, PropertyDefinition.TagsName))
            {
                return(PropertyDefinition.Tags);
            }

            if (_entityDefinition.TryGetProperty(propertyName, out PropertyDefinition property))
            {
                return(property);
            }

            Throw(ErrorMessages.PropertyIsNotDefined(propertyName), attribute);

            return(null);
        }
Esempio n. 11
0
        private Command CreateCommandFromAttribute(XAttribute attribute)
        {
            if (DefaultComparer.NameEquals(attribute, AttributeNames.Tag))
            {
                return(new AddTagCommand(GetValue(attribute)));
            }
            else
            {
                string propertyName = GetPropertyName(attribute);

                string value = GetValue(attribute);

                PropertyDefinition propertyDefinition = Entity.FindProperty(propertyName);

                if (propertyDefinition?.IsCollection == true)
                {
                    return(new AddItemCommand(propertyName, value));
                }

                return(new SetCommand(propertyName, value));
            }
        }
Esempio n. 12
0
        private Record CreateRecord(XElement element)
        {
            string id = null;

            CommandCollection commands = null;

            foreach (XAttribute attribute in element.Attributes())
            {
                if (DefaultComparer.NameEquals(attribute, AttributeNames.Id))
                {
                    id = GetValue(attribute);
                }
                else
                {
                    if (commands == null)
                    {
                        commands = new CommandCollection();
                    }

                    commands.Add(CreateCommandFromAttribute(attribute));
                }
            }

            Record record = CreateRecord(id);

            if (commands != null)
            {
                commands.ExecuteAll(record);
            }

            GetChildCommands(element).ExecuteAll(record);

            Commands?.ExecuteAll(record);

            SetDefaultValues(record);

            return(record);
        }
Esempio n. 13
0
 internal static bool IsReservedName(string name)
 {
     return(DefaultComparer.NameEquals(name, IdName) ||
            DefaultComparer.NameEquals(name, TagsName));
 }
Esempio n. 14
0
        public void ReadAll()
        {
            _documentElement = Document.FirstElement();

            if (_documentElement == null ||
                !DefaultComparer.NameEquals(_documentElement, ElementNames.Document))
            {
                ThrowInvalidOperation(ErrorMessages.MissingElement(ElementNames.Document));
            }

            string versionText = _documentElement.AttributeValueOrDefault(AttributeNames.Version);

            if (versionText != null)
            {
                if (!Version.TryParse(versionText, out Version version))
                {
                    ThrowInvalidOperation(ErrorMessages.InvalidDocumentVersion());
                }
                else if (version > Pihrtsoft.Records.Document.SchemaVersion)
                {
                    ThrowInvalidOperation(ErrorMessages.DocumentVersionIsNotSupported(version, Pihrtsoft.Records.Document.SchemaVersion));
                }
            }

            foreach (XElement element in _documentElement.Elements())
            {
                switch (element.Kind())
                {
                case ElementKind.Entities:
                {
                    if (_entitiesElement != null)
                    {
                        ThrowOnMultipleElementsWithEqualName(element);
                    }

                    _entitiesElement = element;
                    break;
                }

                default:
                {
                    ThrowOnUnknownElement(element);
                    break;
                }
                }
            }

            if (_entitiesElement == null)
            {
                return;
            }

            _entities.Enqueue(new EntitiesInfo(_entitiesElement));

            while (_entities.Count > 0)
            {
                EntitiesInfo entities = _entities.Dequeue();

                foreach (XElement element in entities.Element.Elements())
                {
                    if (element.Kind() != ElementKind.Entity)
                    {
                        ThrowOnUnknownElement(element);
                    }

                    _entityElement = element;

                    ScanEntity();

                    ExtendedKeyedCollection <string, PropertyDefinition> properties = null;
                    ExtendedKeyedCollection <string, Variable>           variables  = null;

                    if (_declarationsElement != null)
                    {
                        ScanDeclarations(out properties, out variables);
                    }

                    _entityDefinition = CreateEntityDefinition(_entityElement, baseEntity: entities.BaseEntity, properties, variables);

                    if (_recordsElement != null)
                    {
                        if (_withElement != null)
                        {
                            _withRecords?.Clear();
                            _state = State.WithRecords;

                            ReadRecords(_withElement);
                        }

                        _state = State.Records;
                        ReadRecords(_recordsElement);
                        _state = State.None;
                    }

                    if (_childEntitiesElement != null)
                    {
                        _entities.Enqueue(new EntitiesInfo(_childEntitiesElement, _entityDefinition));
                    }

                    _entityDefinition     = null;
                    _entityElement        = null;
                    _declarationsElement  = null;
                    _withElement          = null;
                    _recordsElement       = null;
                    _childEntitiesElement = null;
                }
            }
        }
Esempio n. 15
0
        private IPropertyOperation CreateOperationFromAttribute(
            XElement element,
            ElementKind kind,
            XAttribute attribute,
            char separator         = ',',
            bool throwOnId         = false,
            bool throwOnCollection = false)
        {
            string attributeName = attribute.LocalName();

            if (throwOnId &&
                DefaultComparer.NameEquals(attributeName, AttributeNames.Id))
            {
                Throw(ErrorMessages.CannotUseOperationOnProperty(element, attributeName));
            }

            PropertyDefinition property;

            string name = attribute.LocalName();

            if (name == PropertyDefinition.TagsName)
            {
                property = PropertyDefinition.Tags;
            }
            else
            {
                property = GetProperty(attribute);
            }

            if (throwOnCollection &&
                property.IsCollection)
            {
                Throw(ErrorMessages.CannotUseOperationOnCollectionProperty(element, property.Name));
            }

            switch (kind)
            {
            case ElementKind.Add:
                return(new AddOperation(property, GetValue(attribute), Depth));

            case ElementKind.AddRange:
                return(new AddRangeOperation(property, GetValue(attribute), separator, Depth));

            case ElementKind.Remove:
                return(new RemoveOperation(property, GetValue(attribute), Depth));

            case ElementKind.RemoveRange:
                return(new RemoveRangeOperation(property, GetValue(attribute), separator, Depth));

            default:
            {
                Debug.Assert(kind == ElementKind.Set || kind == ElementKind.New, kind.ToString());

                if (property.IsCollection)
                {
                    return(new AddOperation(property, GetValue(attribute), Depth));
                }
                else
                {
                    return(new SetOperation(property, GetValue(attribute), Depth));
                }
            }
            }
        }