private static void SaveChanges(FileRecord fileRecord)
        {
            fileRecord.LastModified         = DateTime.Now;
            fileRecord.ReadOnlyElementsList = fileRecord.RecordSet.Index.GetValues();
            fileRecord.CachedTable          = null;
            fileRecord.Dirty = false;

            XmlDataProviderDocumentWriter.Save(fileRecord);
        }
예제 #2
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);
            }
        }
예제 #3
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);
        }
 public static void ClearCache()
 {
     XmlDataProviderDocumentWriter.Flush();
     _cache.Clear();
 }
예제 #5
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);
        }