Exemplo n.º 1
0
        internal static void CreateStore(string providerName, DataScopeConfigurationElement scopeElement)
        {
            string filename = ResolvePath(scopeElement.Filename, providerName);

            string directoryPath = Path.GetDirectoryName(filename);

            if (!C1Directory.Exists(directoryPath))
            {
                C1Directory.CreateDirectory(directoryPath);
            }

            bool   keepExistingFile        = false;
            string rootLocalName           = XmlDataProviderDocumentWriter.GetRootElementName(scopeElement.ElementName);
            string obsoleteRootElementName = scopeElement.ElementName + "s";

            if (C1File.Exists(filename))
            {
                try
                {
                    XDocument existingDocument = XDocumentUtils.Load(filename);
                    if (existingDocument.Root.Name.LocalName == rootLocalName ||
                        existingDocument.Root.Name.LocalName == obsoleteRootElementName)
                    {
                        keepExistingFile = true;
                    }
                }
                catch (Exception)
                {
                    keepExistingFile = false;
                }

                if (!keepExistingFile)
                {
                    C1File.Delete(filename);
                }
            }

            if (!keepExistingFile)
            {
                var document = new XDocument();
                document.Add(new XElement(rootLocalName));
                XDocumentUtils.Save(document, filename);
            }
        }
Exemplo n.º 2
0
        private static void CopyData(string providerName, DataTypeChangeDescriptor dataTypeChangeDescriptor, DataScopeConfigurationElement oldDataScopeConfigurationElement, DataScopeConfigurationElement newDataScopeConfigurationElement, Dictionary <string, object> newFieldValues, bool deleteOldFile = true)
        {
            string oldFilename = ResolvePath(oldDataScopeConfigurationElement.Filename, providerName);
            string newFilename = ResolvePath(newDataScopeConfigurationElement.Filename, providerName);

            XDocument oldDocument = XDocumentUtils.Load(PathUtil.Resolve(oldFilename));

            List <XElement> newElements = new List <XElement>();

            bool addingVersionId = dataTypeChangeDescriptor.AddedFields.Any(f => f.Name == nameof(IVersioned.VersionId)) &&
                                   dataTypeChangeDescriptor.AlteredType.SuperInterfaces.Any(s => s == typeof(IVersioned));

            string versionIdSourceFieldName = null;

            if (addingVersionId)
            {
                if (dataTypeChangeDescriptor.AlteredType.DataTypeId == typeof(IPage).GetImmutableTypeId())
                {
                    versionIdSourceFieldName = nameof(IPage.Id);
                }
                else
                {
                    versionIdSourceFieldName = dataTypeChangeDescriptor.AlteredType.Fields
                                               .Where(f => f.InstanceType == typeof(Guid) &&
                                                      (f.ForeignKeyReferenceTypeName?.Contains(typeof(IPage).FullName) ?? false))
                                               .OrderByDescending(f => f.Name == nameof(IPageData.PageId))
                                               .Select(f => f.Name)
                                               .FirstOrDefault();
                }
            }


            foreach (XElement oldElement in oldDocument.Root.Elements())
            {
                List <XAttribute> newChildAttributes = new List <XAttribute>();

                foreach (XAttribute oldChildAttribute in oldElement.Attributes())
                {
                    var existingFieldInfo = GetExistingFieldInfo(dataTypeChangeDescriptor, oldChildAttribute.Name.LocalName);

                    if (existingFieldInfo != null)
                    {
                        if (existingFieldInfo.OriginalField.Name != existingFieldInfo.AlteredField.Name)
                        {
                            XAttribute newChildAttribute = new XAttribute(existingFieldInfo.AlteredField.Name, oldChildAttribute.Value);

                            newChildAttributes.Add(newChildAttribute);
                        }
                        else
                        {
                            newChildAttributes.Add(oldChildAttribute);
                        }
                    }
                    // It may happen that some data were added before data descriptors are updated, in the case of using
                    // [AutoUpdateable] attribute.
                    else if (dataTypeChangeDescriptor.AddedFields.Any(addedField => addedField.Name == oldChildAttribute.Name))
                    {
                        newChildAttributes.Add(oldChildAttribute);
                    }
                }

                // Adding default value for fields that are NULL and become required
                foreach (var existingFieldInfo in dataTypeChangeDescriptor.ExistingFields)
                {
                    bool fieldBecomeRequired = existingFieldInfo.OriginalField.IsNullable && !existingFieldInfo.AlteredField.IsNullable;

                    string fieldName = existingFieldInfo.AlteredField.Name;

                    if (fieldBecomeRequired && !newChildAttributes.Any(attr => attr.Name.LocalName == fieldName))
                    {
                        newChildAttributes.Add(new XAttribute(fieldName, GetDefaultValue(existingFieldInfo.AlteredField)));
                    }
                }

                foreach (DataFieldDescriptor fieldDescriptor in dataTypeChangeDescriptor.AddedFields)
                {
                    if (addingVersionId && fieldDescriptor.Name == nameof(IVersioned.VersionId) && versionIdSourceFieldName != null)
                    {
                        var existingField = (Guid)oldElement.Attribute(versionIdSourceFieldName);
                        if (existingField != null)
                        {
                            newChildAttributes.Add(new XAttribute(fieldDescriptor.Name, existingField));
                            continue;
                        }
                    }

                    if (!fieldDescriptor.IsNullable &&
                        !newChildAttributes.Any(attr => attr.Name == fieldDescriptor.Name))
                    {
                        object value;
                        if (!newFieldValues.TryGetValue(fieldDescriptor.Name, out value))
                        {
                            value = GetDefaultValue(fieldDescriptor);
                        }

                        newChildAttributes.Add(new XAttribute(fieldDescriptor.Name, value));
                    }
                    else if (newFieldValues.ContainsKey(fieldDescriptor.Name))
                    {
                        XAttribute attribute = newChildAttributes.SingleOrDefault(attr => attr.Name == fieldDescriptor.Name);

                        attribute?.SetValue(newFieldValues[fieldDescriptor.Name]);
                    }
                }

                XElement newElement = new XElement(newDataScopeConfigurationElement.ElementName, newChildAttributes);

                newElements.Add(newElement);
            }

            if (deleteOldFile)
            {
                C1File.Delete(oldFilename);
            }

            XElement newRoot = new XElement(XmlDataProviderDocumentWriter.GetRootElementName(newDataScopeConfigurationElement.ElementName));

            newRoot.Add(newElements);

            XDocument newDocument = new XDocument();

            newDocument.Add(newRoot);

            XDocumentUtils.Save(newDocument, newFilename);
        }
        internal static void DropStore(string providerName, DataScopeConfigurationElement scopeElement)
        {
            string filename = ResolvePath(scopeElement.Filename, providerName);

            if (C1File.Exists(filename))
            {
                C1File.Delete(filename);
            }
        }
        internal static void CreateStore(string providerName, DataScopeConfigurationElement scopeElement)
        {
            string filename = ResolvePath(scopeElement.Filename, providerName);

            string directoryPath = Path.GetDirectoryName(filename);
            if (!C1Directory.Exists(directoryPath))
            {
                C1Directory.CreateDirectory(directoryPath);
            }

            bool keepExistingFile = false;
            string rootLocalName = XmlDataProviderDocumentWriter.GetRootElementName(scopeElement.ElementName);
            string obsoleteRootElementName = scopeElement.ElementName + "s";

            if (C1File.Exists(filename))
            {
                try
                {
                    XDocument existingDocument = XDocumentUtils.Load(filename);
                    if (existingDocument.Root.Name.LocalName == rootLocalName
                        || existingDocument.Root.Name.LocalName == obsoleteRootElementName)
                    {
                        keepExistingFile = true;
                    }
                }
                catch (Exception)
                {
                    keepExistingFile = false;
                }

                if (!keepExistingFile)
                {
                    C1File.Delete(filename);
                }
            }

            if (!keepExistingFile)
            {
                var document = new XDocument();
                document.Add(new XElement(rootLocalName));
                XDocumentUtils.Save(document, filename);
            }
        }
        private static void CopyData(string providerName, DataTypeChangeDescriptor dataTypeChangeDescriptor, DataScopeConfigurationElement oldDataScopeConfigurationElement, DataScopeConfigurationElement newDataScopeConfigurationElement, Dictionary<string, object> newFieldValues, bool deleteOldFile = true)
        {
            string oldFilename = ResolvePath(oldDataScopeConfigurationElement.Filename, providerName);
            string newFilename = ResolvePath(newDataScopeConfigurationElement.Filename, providerName);

            XDocument oldDocument = XDocumentUtils.Load(PathUtil.Resolve(oldFilename));

            List<XElement> newElements = new List<XElement>();

            foreach (XElement oldElement in oldDocument.Root.Elements())
            {
                List<XAttribute> newChildAttributes = new List<XAttribute>();

                foreach (XAttribute oldChildAttribute in oldElement.Attributes())
                {
                    var existingFieldInfo = GetExistingFieldInfo(dataTypeChangeDescriptor, oldChildAttribute.Name.LocalName);

                    if (existingFieldInfo != null)
                    {
                        if (existingFieldInfo.OriginalField.Name != existingFieldInfo.AlteredField.Name)
                        {
                            XAttribute newChildAttribute = new XAttribute(existingFieldInfo.AlteredField.Name, oldChildAttribute.Value);

                            newChildAttributes.Add(newChildAttribute);
                        }
                        else
                        {
                            newChildAttributes.Add(oldChildAttribute);
                        }
                    }
                    // It may happen that some data were added before data descriptors are updated, in the case of using 
                    // [AutoUpdateable] attribute. 
                    else if (dataTypeChangeDescriptor.AddedFields.Any(addedField => addedField.Name == oldChildAttribute.Name))
                    {
                        newChildAttributes.Add(oldChildAttribute);
                    }
                }

                // Adding default value for fields that are NULL and become required
                foreach (var existingFieldInfo in dataTypeChangeDescriptor.ExistingFields)
                {
                    bool fieldBecomeRequired = existingFieldInfo.OriginalField.IsNullable && !existingFieldInfo.AlteredField.IsNullable;

                    string fieldName = existingFieldInfo.AlteredField.Name;

                    if (fieldBecomeRequired && !newChildAttributes.Any(attr => attr.Name.LocalName == fieldName))
                    {
                        newChildAttributes.Add(new XAttribute(fieldName, GetDefaultValue(existingFieldInfo.AlteredField)));
                    }
                }

                foreach (DataFieldDescriptor fieldDescriptor in dataTypeChangeDescriptor.AddedFields)
                {
                    if (fieldDescriptor.IsNullable == false
                        && !newChildAttributes.Any(attr => attr.Name == fieldDescriptor.Name))
                    {
                        XAttribute newChildAttribute = new XAttribute(fieldDescriptor.Name, GetDefaultValue(fieldDescriptor));

                        if (newFieldValues.ContainsKey(fieldDescriptor.Name))
                        {
                            newChildAttribute.SetValue(newFieldValues[fieldDescriptor.Name]);
                        }

                        newChildAttributes.Add(newChildAttribute);
                    }
                    else if (newFieldValues.ContainsKey(fieldDescriptor.Name))
                    {
                        XAttribute attribute = newChildAttributes.SingleOrDefault(attr => attr.Name == fieldDescriptor.Name);
                        if (attribute != null)
                        {
                            attribute.SetValue(newFieldValues[fieldDescriptor.Name]);
                        }
                    }
                }

                XElement newElement = new XElement(newDataScopeConfigurationElement.ElementName, newChildAttributes);

                newElements.Add(newElement);
            }

            if (deleteOldFile)
            {
                C1File.Delete(oldFilename);
            }

            XElement newRoot = new XElement(XmlDataProviderDocumentWriter.GetRootElementName(newDataScopeConfigurationElement.ElementName));
            newRoot.Add(newElements);

            XDocument newDocument = new XDocument();
            newDocument.Add(newRoot);

            XDocumentUtils.Save(newDocument, newFilename);
        }
Exemplo n.º 6
0
        private static void CopyData(string providerName, DataTypeChangeDescriptor dataTypeChangeDescriptor, DataScopeConfigurationElement oldDataScopeConfigurationElement, DataScopeConfigurationElement newDataScopeConfigurationElement, Dictionary <string, object> newFieldValues, bool deleteOldFile = true)
        {
            string oldFilename = ResolvePath(oldDataScopeConfigurationElement.Filename, providerName);
            string newFilename = ResolvePath(newDataScopeConfigurationElement.Filename, providerName);

            XDocument oldDocument = XDocumentUtils.Load(PathUtil.Resolve(oldFilename));

            List <XElement> newElements = new List <XElement>();

            foreach (XElement oldElement in oldDocument.Root.Elements())
            {
                List <XAttribute> newChildAttributes = new List <XAttribute>();

                foreach (XAttribute oldChildAttribute in oldElement.Attributes())
                {
                    var existingFieldInfo = GetExistingFieldInfo(dataTypeChangeDescriptor, oldChildAttribute.Name.LocalName);

                    if (existingFieldInfo != null)
                    {
                        if (existingFieldInfo.OriginalField.Name != existingFieldInfo.AlteredField.Name)
                        {
                            XAttribute newChildAttribute = new XAttribute(existingFieldInfo.AlteredField.Name, oldChildAttribute.Value);

                            newChildAttributes.Add(newChildAttribute);
                        }
                        else
                        {
                            newChildAttributes.Add(oldChildAttribute);
                        }
                    }
                    // It may happen that some data were added before data descriptors are updated, in the case of using
                    // [AutoUpdateable] attribute.
                    else if (dataTypeChangeDescriptor.AddedFields.Any(addedField => addedField.Name == oldChildAttribute.Name))
                    {
                        newChildAttributes.Add(oldChildAttribute);
                    }
                }

                // Adding default value for fields that are NULL and become required
                foreach (var existingFieldInfo in dataTypeChangeDescriptor.ExistingFields)
                {
                    bool fieldBecomeRequired = existingFieldInfo.OriginalField.IsNullable && !existingFieldInfo.AlteredField.IsNullable;

                    string fieldName = existingFieldInfo.AlteredField.Name;

                    if (fieldBecomeRequired && !newChildAttributes.Any(attr => attr.Name.LocalName == fieldName))
                    {
                        newChildAttributes.Add(new XAttribute(fieldName, GetDefaultValue(existingFieldInfo.AlteredField)));
                    }
                }

                foreach (DataFieldDescriptor fieldDescriptor in dataTypeChangeDescriptor.AddedFields)
                {
                    if (fieldDescriptor.IsNullable == false &&
                        !newChildAttributes.Any(attr => attr.Name == fieldDescriptor.Name))
                    {
                        XAttribute newChildAttribute = new XAttribute(fieldDescriptor.Name, GetDefaultValue(fieldDescriptor));

                        if (newFieldValues.ContainsKey(fieldDescriptor.Name))
                        {
                            newChildAttribute.SetValue(newFieldValues[fieldDescriptor.Name]);
                        }

                        newChildAttributes.Add(newChildAttribute);
                    }
                    else if (newFieldValues.ContainsKey(fieldDescriptor.Name))
                    {
                        XAttribute attribute = newChildAttributes.SingleOrDefault(attr => attr.Name == fieldDescriptor.Name);
                        if (attribute != null)
                        {
                            attribute.SetValue(newFieldValues[fieldDescriptor.Name]);
                        }
                    }
                }

                XElement newElement = new XElement(newDataScopeConfigurationElement.ElementName, newChildAttributes);

                newElements.Add(newElement);
            }

            if (deleteOldFile)
            {
                C1File.Delete(oldFilename);
            }

            XElement newRoot = new XElement(XmlDataProviderDocumentWriter.GetRootElementName(newDataScopeConfigurationElement.ElementName));

            newRoot.Add(newElements);

            XDocument newDocument = new XDocument();

            newDocument.Add(newRoot);

            XDocumentUtils.Save(newDocument, newFilename);
        }
        private static void CopyData(string providerName, DataTypeChangeDescriptor dataTypeChangeDescriptor, DataScopeConfigurationElement oldDataScopeConfigurationElement, DataScopeConfigurationElement newDataScopeConfigurationElement, Dictionary<string, object> newFieldValues, bool deleteOldFile = true)
        {
            string oldFilename = ResolvePath(oldDataScopeConfigurationElement.Filename, providerName);
            string newFilename = ResolvePath(newDataScopeConfigurationElement.Filename, providerName);

            XDocument oldDocument = XDocumentUtils.Load(PathUtil.Resolve(oldFilename));

            List<XElement> newElements = new List<XElement>();

            bool addingVersionId = dataTypeChangeDescriptor.AddedFields.Any(f => f.Name == nameof(IVersioned.VersionId))
                                   && dataTypeChangeDescriptor.AlteredType.SuperInterfaces.Any(s => s == typeof (IVersioned));

            string versionIdSourceFieldName = null;
            if (addingVersionId)
            {
                if (dataTypeChangeDescriptor.AlteredType.DataTypeId == typeof(IPage).GetImmutableTypeId())
                {
                    versionIdSourceFieldName = nameof(IPage.Id);
                }
                else
                {
                    versionIdSourceFieldName = dataTypeChangeDescriptor.AlteredType.Fields
                        .Where(f => f.InstanceType == typeof(Guid)
                                    && (f.ForeignKeyReferenceTypeName?.Contains(typeof(IPage).FullName) ?? false))
                        .OrderByDescending(f => f.Name == nameof(IPageData.PageId))
                        .Select(f => f.Name)
                        .FirstOrDefault();
                }
            }

            foreach (XElement oldElement in oldDocument.Root.Elements())
            {
                List<XAttribute> newChildAttributes = new List<XAttribute>();

                foreach (XAttribute oldChildAttribute in oldElement.Attributes())
                {
                    var existingFieldInfo = GetExistingFieldInfo(dataTypeChangeDescriptor, oldChildAttribute.Name.LocalName);

                    if (existingFieldInfo != null)
                    {
                        if (existingFieldInfo.OriginalField.Name != existingFieldInfo.AlteredField.Name)
                        {
                            XAttribute newChildAttribute = new XAttribute(existingFieldInfo.AlteredField.Name, oldChildAttribute.Value);

                            newChildAttributes.Add(newChildAttribute);
                        }
                        else
                        {
                            newChildAttributes.Add(oldChildAttribute);
                        }
                    }
                    // It may happen that some data were added before data descriptors are updated, in the case of using
                    // [AutoUpdateable] attribute.
                    else if (dataTypeChangeDescriptor.AddedFields.Any(addedField => addedField.Name == oldChildAttribute.Name))
                    {
                        newChildAttributes.Add(oldChildAttribute);
                    }
                }

                // Adding default value for fields that are NULL and become required
                foreach (var existingFieldInfo in dataTypeChangeDescriptor.ExistingFields)
                {
                    bool fieldBecomeRequired = existingFieldInfo.OriginalField.IsNullable && !existingFieldInfo.AlteredField.IsNullable;

                    string fieldName = existingFieldInfo.AlteredField.Name;

                    if (fieldBecomeRequired && !newChildAttributes.Any(attr => attr.Name.LocalName == fieldName))
                    {
                        newChildAttributes.Add(new XAttribute(fieldName, GetDefaultValue(existingFieldInfo.AlteredField)));
                    }
                }

                foreach (DataFieldDescriptor fieldDescriptor in dataTypeChangeDescriptor.AddedFields)
                {
                    if (addingVersionId && fieldDescriptor.Name == nameof(IVersioned.VersionId) && versionIdSourceFieldName != null)
                    {
                        if (!oldElement.Attributes().Any(a => a.Name.LocalName == nameof(IVersioned.VersionId)))
                        {
                            var sourceFieldValue = (Guid)oldElement.Attribute(versionIdSourceFieldName);

                            newChildAttributes.Add(new XAttribute(fieldDescriptor.Name, sourceFieldValue));
                        }

                        continue;
                    }

                    if (!fieldDescriptor.IsNullable
                        && !newChildAttributes.Any(attr => attr.Name == fieldDescriptor.Name))
                    {
                        object value;
                        if (!newFieldValues.TryGetValue(fieldDescriptor.Name, out value))
                        {
                            value = GetDefaultValue(fieldDescriptor);
                        }

                        newChildAttributes.Add(new XAttribute(fieldDescriptor.Name, value));
                    }
                    else if (newFieldValues.ContainsKey(fieldDescriptor.Name))
                    {
                        XAttribute attribute = newChildAttributes.SingleOrDefault(attr => attr.Name == fieldDescriptor.Name);

                        attribute?.SetValue(newFieldValues[fieldDescriptor.Name]);
                    }
                }

                XElement newElement = new XElement(newDataScopeConfigurationElement.ElementName, newChildAttributes);

                newElements.Add(newElement);
            }

            if (deleteOldFile)
            {
                C1File.Delete(oldFilename);
            }

            var newDocument = new XDocument(
                new XElement(XmlDataProviderDocumentWriter.GetRootElementName(newDataScopeConfigurationElement.ElementName),
                    newElements));

            XDocumentUtils.Save(newDocument, newFilename);
        }