Example #1
0
        private static DataTypeDescriptor LoadFromFile(string filePath)
        {
            XDocument doc;

            try
            {
                doc = XDocumentUtils.Load(filePath, LoadOptions.SetBaseUri | LoadOptions.SetLineInfo);
            }
            catch (XmlException e)
            {
                throw new ConfigurationErrorsException("Error loading meta data file '{0}': {1}".FormatWith(filePath, e.Message), e, filePath, e.LineNumber);
            }

            return(DataTypeDescriptor.FromXml(doc.Root));
        }
Example #2
0
        /// <exclude />
        public override IEnumerable <PackageFragmentValidationResult> Validate()
        {
            var validationResult = new List <PackageFragmentValidationResult>();

            var foreignKeyReferences = new List <string>();

            if (this.Configuration.Count(f => f.Name == "Types") > 1)
            {
                validationResult.AddFatal(Texts.DynamicDataTypePackageFragmentInstaller_OnlyOneElement);
                return(validationResult);
            }

            XElement typesElement = this.Configuration.SingleOrDefault(f => f.Name == "Types");

            if (typesElement == null)
            {
                validationResult.AddFatal(Texts.DynamicDataTypePackageFragmentInstaller_MissingElement);
                return(validationResult);
            }

            _dataTypeDescriptors = new List <DataTypeDescriptor>();

            foreach (XElement typeElement in typesElement.Elements("Type"))
            {
                XElement serializedDataTypeDescriptor;

                XAttribute fileAttribute = typeElement.Attribute("dataTypeDescriptorFile");
                if (fileAttribute != null)
                {
                    string relativeFilePath = (string)fileAttribute;

                    string markup;

                    using (var stream = this.InstallerContext.ZipFileSystem.GetFileStream(relativeFilePath))
                        using (var reader = new StreamReader(stream))
                        {
                            markup = reader.ReadToEnd();
                        }

                    serializedDataTypeDescriptor = XElement.Parse(markup);
                }
                else
                {
                    var dataTypeDescriptorAttribute = typeElement.Attribute("dataTypeDescriptor");
                    if (dataTypeDescriptorAttribute == null)
                    {
                        validationResult.AddFatal(Texts.DataTypePackageFragmentInstaller_MissingAttribute("dataTypeDescriptor"), typeElement);
                        continue;
                    }

                    try
                    {
                        serializedDataTypeDescriptor = XElement.Parse(dataTypeDescriptorAttribute.Value);
                    }
                    catch (Exception)
                    {
                        validationResult.AddFatal(Texts.DynamicDataTypePackageFragmentInstaller_DataTypeDescriptorParseError, dataTypeDescriptorAttribute);
                        continue;
                    }
                }

                DataTypeDescriptor dataTypeDescriptor;
                try
                {
                    bool inheritedFieldsIncluded = serializedDataTypeDescriptor.Descendants().Any(e => e.Attributes("inherited").Any(a => (string)a == "true"));

                    dataTypeDescriptor = DataTypeDescriptor.FromXml(serializedDataTypeDescriptor, inheritedFieldsIncluded);
                }
                catch (Exception e)
                {
                    validationResult.AddFatal(Texts.DynamicDataTypePackageFragmentInstaller_DataTypeDescriptorDeserializeError(e.Message));
                    continue;
                }

                Type type = TypeManager.TryGetType(dataTypeDescriptor.TypeManagerTypeName);
                if (type != null && DataFacade.GetAllKnownInterfaces().Contains(type))
                {
                    validationResult.AddFatal(Texts.DynamicDataTypePackageFragmentInstaller_TypeExists(type));
                }

                if (dataTypeDescriptor.SuperInterfaces.Any(f => f.Name == nameof(IVersioned)))
                {
                    if (dataTypeDescriptor.Fields.All(f => f.Name != nameof(IVersioned.VersionId)))
                    {
                        dataTypeDescriptor.Fields.Add(new DataFieldDescriptor(Guid.NewGuid(), nameof(IVersioned.VersionId), StoreFieldType.Guid, typeof(Guid), true));
                    }
                }

                foreach (var field in dataTypeDescriptor.Fields)
                {
                    if (!field.ForeignKeyReferenceTypeName.IsNullOrEmpty())
                    {
                        foreignKeyReferences.Add(field.ForeignKeyReferenceTypeName);
                    }
                }

                _dataTypeDescriptors.Add(dataTypeDescriptor);
                this.InstallerContext.AddPendingDataTypeDescritpor(dataTypeDescriptor.TypeManagerTypeName, dataTypeDescriptor);
            }

            foreach (string foreignKeyTypeName in foreignKeyReferences)
            {
                if (!TypeManager.HasTypeWithName(foreignKeyTypeName) &&
                    !_dataTypeDescriptors.Any(descriptor => descriptor.TypeManagerTypeName == foreignKeyTypeName))
                {
                    validationResult.AddFatal(Texts.DynamicDataTypePackageFragmentInstaller_MissingReferencedType(foreignKeyTypeName));
                }
            }

            if (validationResult.Count > 0)
            {
                _dataTypeDescriptors = null;
            }

            return(validationResult);
        }
Example #3
0
        /// <exclude />
        public override IEnumerable <PackageFragmentValidationResult> Validate()
        {
            var validationResult = new List <PackageFragmentValidationResult>();

            var foreignKeyReferences = new List <string>();

            if (this.Configuration.Count(f => f.Name == "Types") > 1)
            {
                validationResult.AddFatal(GetText("DynamicDataTypePackageFragmentInstaller.OnlyOneElement"));
                return(validationResult);
            }

            XElement typesElement = this.Configuration.SingleOrDefault(f => f.Name == "Types");

            if (typesElement == null)
            {
                validationResult.AddFatal(GetText("DynamicDataTypePackageFragmentInstaller.MissingElement"));
                return(validationResult);
            }

            _dataTypeDescriptors = new List <DataTypeDescriptor>();

            foreach (XElement typeElement in typesElement.Elements("Type"))
            {
                XAttribute dataTypeDescriptorAttribute = typeElement.Attribute("dataTypeDescriptor");

                if (dataTypeDescriptorAttribute == null)
                {
                    validationResult.AddFatal(GetText("DataTypePackageFragmentInstaller.MissingAttribute").FormatWith("dataTypeDescriptor"), typeElement);
                    continue;
                }

                XElement serializedDataTypeDescriptor;
                try
                {
                    serializedDataTypeDescriptor = XElement.Parse(dataTypeDescriptorAttribute.Value);
                }
                catch (Exception)
                {
                    validationResult.AddFatal(GetText("DynamicDataTypePackageFragmentInstaller.DataTypeDescriptorParseError"), dataTypeDescriptorAttribute);
                    continue;
                }

                DataTypeDescriptor dataTypeDescriptor;
                try
                {
                    bool inheritedFieldsIncluded = serializedDataTypeDescriptor.Descendants().Any(e => e.Attributes("inherited").Any(a => (string)a == "true"));

                    dataTypeDescriptor = DataTypeDescriptor.FromXml(serializedDataTypeDescriptor, inheritedFieldsIncluded);
                }
                catch (Exception e)
                {
                    validationResult.AddFatal(GetText("DynamicDataTypePackageFragmentInstaller.DataTypeDescriptorDeserializeError").FormatWith(e.Message));
                    continue;
                }

                Type type = TypeManager.TryGetType(dataTypeDescriptor.TypeManagerTypeName);
                if ((type != null) && (DataFacade.GetAllKnownInterfaces().Contains(type)))
                {
                    validationResult.AddFatal(GetText("DynamicDataTypePackageFragmentInstaller.TypeExists").FormatWith(type));
                }

                foreach (var field in dataTypeDescriptor.Fields)
                {
                    if (!field.ForeignKeyReferenceTypeName.IsNullOrEmpty())
                    {
                        foreignKeyReferences.Add(field.ForeignKeyReferenceTypeName);
                    }
                }

                _dataTypeDescriptors.Add(dataTypeDescriptor);
                this.InstallerContext.AddPendingDataTypeDescritpor(dataTypeDescriptor.TypeManagerTypeName, dataTypeDescriptor);
            }

            foreach (string foreignKeyTypeName in foreignKeyReferences)
            {
                if (!TypeManager.HasTypeWithName(foreignKeyTypeName) &&
                    !_dataTypeDescriptors.Any(descriptor => descriptor.TypeManagerTypeName == foreignKeyTypeName))
                {
                    validationResult.AddFatal(GetText("DynamicDataTypePackageFragmentInstaller.MissingReferencedType").FormatWith(foreignKeyTypeName));
                }
            }

            if (validationResult.Count > 0)
            {
                _dataTypeDescriptors = null;
            }

            return(validationResult);
        }