private static void WriteSchemaModifications(SchemaEditor origSchema, SchemaEditor newSchema, SchemaWriter writer, List <PropertySet> modifiedPropertySets)
        {
            // #1: Delete types

            foreach (NodeType type in GetNodeTypeRootsToDelete(origSchema.NodeTypes, newSchema.NodeTypes))
            {
                WriteDeleteNodeType(writer, type, origSchema, modifiedPropertySets);
            }
            foreach (var type in GetTypesToDelete <ContentListType>(origSchema.ContentListTypes, newSchema.ContentListTypes))
            {
                WriteDeleteContentListType(writer, type, origSchema, modifiedPropertySets);
            }

            // #2: Create or modify types

            WriteCreateOrModifyPropertyTypes(origSchema, newSchema, writer);
            WriteCreateOrModifyNodeTypes(origSchema, newSchema, modifiedPropertySets, writer);
            WriteCreateOrModifyContentListTypes(origSchema, newSchema, modifiedPropertySets, writer);

            // #3: Delete PropertyTypes
            foreach (PropertyType type in GetTypesToDelete <PropertyType>(origSchema.PropertyTypes, newSchema.PropertyTypes))
            {
                WriteDeletePropertyType(writer, type);
            }
        }
        private static void RegisterSchema(SchemaEditor origSchema, SchemaEditor newSchema, SchemaWriter schemaWriter)
        {
            using (var op = SnTrace.Database.StartOperation("Write storage schema modifications."))
            {
                // Ensure transaction encapsulation
                bool isLocalTransaction = !TransactionScope.IsActive;
                if (isLocalTransaction)
                {
                    TransactionScope.Begin();
                }
                try
                {
                    List <PropertySet> modifiedPropertySets = new List <PropertySet>();
                    schemaWriter.Open();
                    WriteSchemaModifications(origSchema, newSchema, schemaWriter, modifiedPropertySets);
                    foreach (PropertySet modifiedPropertySet in modifiedPropertySets)
                    {
                        NodeTypeDependency.FireChanged(modifiedPropertySet.Id);
                    }
                    schemaWriter.Close();
                    if (isLocalTransaction)
                    {
                        TransactionScope.Commit();
                    }
                    ActiveSchema.Reset();
                    op.Successful = true;
                }
                catch (Exception ex)
                {
                    SnLog.WriteException(ex, null, EventId.RepositoryRuntime);
                    throw new SchemaEditorCommandException("Error during schema registration.", ex);
                }
                finally
                {
                    IDisposable unmanagedWriter = schemaWriter as IDisposable;
                    if (unmanagedWriter != null)
                    {
                        unmanagedWriter.Dispose();
                    }
                    try
                    {
                        if (isLocalTransaction && TransactionScope.IsActive)
                        {
                            TransactionScope.Rollback();
                        }
                    }
                    catch (Exception ex2)
                    {
                        // This catch block will handle any errors that may have occurred
                        // on the server that would cause the rollback to fail, such as
                        // a closed connection (MSDN).
                        const string msg = "Error during schema transaction rollback.";
                        SnLog.WriteException(ex2, msg, EventId.RepositoryRuntime);

                        throw new SchemaEditorCommandException(msg, ex2);
                    }
                }
            }
        }
Beispiel #3
0
        public void Register()
        {
            SchemaEditor origSchema = new SchemaEditor();

            origSchema.Load();
            DataProvider.Current.AssertSchemaTimestampAndWriteModificationDate(this.SchemaTimestamp);
            SchemaWriter schemaWriter = DataProvider.Current.CreateSchemaWriter();

            RegisterSchema(origSchema, this, schemaWriter);
        }
Beispiel #4
0
        private static void RegisterSchema(SchemaEditor origSchema, SchemaEditor newSchema, SchemaWriter schemaWriter)
        {
            if (schemaWriter.CanWriteDifferences)
            {
                using (var op = SnTrace.Database.StartOperation("Write storage schema modifications."))
                {
                    try
                    {
                        var modifiedPropertySets = new List <PropertySet>();

                        schemaWriter.Open();
                        WriteSchemaModifications(origSchema, newSchema, schemaWriter, modifiedPropertySets);

                        foreach (var modifiedPropertySet in modifiedPropertySets)
                        {
                            NodeTypeDependency.FireChanged(modifiedPropertySet.Id);
                        }

                        schemaWriter.Close();
                        ActiveSchema.Reset();

                        op.Successful = true;
                    }
                    catch (Exception ex)
                    {
                        SnLog.WriteException(ex, null, EventId.RepositoryRuntime);
                        throw new SchemaEditorCommandException("Error during schema registration.", ex);
                    }
                    finally
                    {
                        if (schemaWriter is IDisposable unmanagedWriter)
                        {
                            unmanagedWriter.Dispose();
                        }
                    }
                }
            }
            else
            {
                using (var op = SnTrace.Database.StartOperation("Update storage schema."))
                {
                    var modifiedPropertySetIds = GetModifiedPropertySetIds(origSchema, newSchema);

                    schemaWriter.WriteSchemaAsync(newSchema.ToRepositorySchemaData()).GetAwaiter().GetResult();
                    ActiveSchema.Reset();
                    foreach (var id in modifiedPropertySetIds)
                    {
                        NodeTypeDependency.FireChanged(id);
                    }

                    op.Successful = true;
                }
            }
        }
Beispiel #5
0
 private static void WriteCreateOrModifyContentListTypes(SchemaEditor origSchema, SchemaEditor newSchema, List <PropertySet> modifiedPropertySets, SchemaWriter writer)
 {
     foreach (var newType in newSchema.ContentListTypes)
     {
         if (NeedToCreate <ContentListType>(origSchema.ContentListTypes, newType))
         {
             writer.CreateContentListType(newType.Name);
         }
         WriteAddOrRemovePropertyTypes(origSchema.ContentListTypes[newType.Name], newType, modifiedPropertySets, writer);
     }
 }
Beispiel #6
0
        // -------------------------------------------------------------- CreateOrRemove commands

        private static void WriteCreateOrModifyPropertyTypes(SchemaEditor origSchema, SchemaEditor newSchema, SchemaWriter writer)
        {
            // new
            foreach (PropertyType newType in newSchema.PropertyTypes)
            {
                if (NeedToCreate <PropertyType>(origSchema.PropertyTypes, newType))
                {
                    writer.CreatePropertyType(newType.Name, newType.DataType, newType.Mapping, newType.IsContentListProperty);
                }
            }
        }
Beispiel #7
0
 private static void WriteCreateOrModifyPermissionTypes(SchemaEditor origSchema, SchemaEditor newSchema, SchemaWriter writer)
 {
     //-- new
     foreach (PermissionType newType in newSchema.PermissionTypes)
     {
         if (NeedToCreate <PermissionType>(origSchema.PermissionTypes, newType))
         {
             writer.CreatePermissionType(newType.Name);
         }
     }
 }
Beispiel #8
0
        private static void WriteCreateOrModifyNodeTypes(SchemaEditor origSchema, SchemaEditor newSchema, List <PropertySet> modifiedPropertySets, SchemaWriter writer)
        {
            List <NodeType> _nodeTypesToEnumerate = new List <NodeType>();

            // collect only roots
            foreach (NodeType rootNodeType in newSchema.NodeTypes)
            {
                if (rootNodeType.Parent == null)
                {
                    _nodeTypesToEnumerate.Add(rootNodeType);
                }
            }

            int index = 0;

            while (index < _nodeTypesToEnumerate.Count)
            {
                NodeType currentType = _nodeTypesToEnumerate[index++];
                NodeType origType    = null;

                if (NeedToCreate <NodeType>(origSchema.NodeTypes, currentType))
                {
                    if (currentType.ClassName == null)
                    {
                        throw new InvalidSchemaException("ClassName cannot be null. NodeType: " + currentType.Name);
                    }
                    writer.CreateNodeType(currentType.Parent, currentType.Name, currentType.ClassName);
                }
                else
                {
                    origType = origSchema.NodeTypes[currentType.Name];
                    string origParentName = origType.Parent == null ? null : origType.Parent.Name;
                    string newParentName  = currentType.Parent == null ? null : currentType.Parent.Name;
                    bool   parentChanged  = origParentName != newParentName;
                    if (parentChanged || origType.ClassName != currentType.ClassName)
                    {
                        writer.ModifyNodeType(origType, currentType.Parent, currentType.ClassName);
                        if (!modifiedPropertySets.Contains(origType))
                        {
                            modifiedPropertySets.Add(origType);
                        }
                    }
                }

                // Property list (origType can be null)
                WriteAddOrRemovePropertyTypes(origType, currentType, modifiedPropertySets, writer);

                // Add children to enumerator
                _nodeTypesToEnumerate.AddRange(currentType.GetChildren());
            }
        }
        public void Register()
        {
            var origSchema = Retrier.Retry(3, 100, typeof(InvalidSchemaException), () =>
            {
                var sche = new SchemaEditor();
                sche.Load();
                return(sche);
            });

            DataProvider.Current.AssertSchemaTimestampAndWriteModificationDate(this.SchemaTimestamp);
            SchemaWriter schemaWriter = DataProvider.Current.CreateSchemaWriter();

            RegisterSchema(origSchema, this, schemaWriter);
        }
Beispiel #10
0
 private static void RegisterSchema(SchemaEditor origSchema, SchemaEditor newSchema, SchemaWriter schemaWriter)
 {
     using (var traceOperation = Logger.TraceOperation("Write storage schema modifications."))
     {
         // Ensure transaction encapsulation
         bool isLocalTransaction = !TransactionScope.IsActive;
         if (isLocalTransaction)
         {
             TransactionScope.Begin();
         }
         try
         {
             List <PropertySet> modifiedPropertySets = new List <PropertySet>();
             schemaWriter.Open();
             WriteSchemaModifications(origSchema, newSchema, schemaWriter, modifiedPropertySets);
             foreach (PropertySet modifiedPropertySet in modifiedPropertySets)
             {
                 NodeTypeDependency.FireChanged(modifiedPropertySet.Id);
             }
             schemaWriter.Close();
             if (isLocalTransaction)
             {
                 TransactionScope.Commit();
             }
             ActiveSchema.Reset();
             traceOperation.IsSuccessful = true;
         }
         finally
         {
             IDisposable unmanagedWriter = schemaWriter as IDisposable;
             if (unmanagedWriter != null)
             {
                 unmanagedWriter.Dispose();
             }
             if (isLocalTransaction && TransactionScope.IsActive)
             {
                 TransactionScope.Rollback();
             }
         }
     }
 }
Beispiel #11
0
        public void Register()
        {
            var origSchema = Retrier.Retry(3, 100, typeof(InvalidSchemaException), () =>
            {
                var sche = new SchemaEditor();
                sche.Load();
                return(sche);
            });

            var schemaLock   = DataStore.StartSchemaUpdateAsync(this.SchemaTimestamp, CancellationToken.None).GetAwaiter().GetResult();
            var schemaWriter = DataStore.CreateSchemaWriter();

            try
            {
                RegisterSchema(origSchema, this, schemaWriter);
            }
            finally
            {
                DataStore.FinishSchemaUpdateAsync(schemaLock, CancellationToken.None).GetAwaiter().GetResult();
            }
        }
Beispiel #12
0
        private static IEnumerable <int> GetModifiedPropertySetIds(SchemaEditor origSchema, SchemaEditor newSchema)
        {
            var origTypes = origSchema.NodeTypes;
            var newTypes  = newSchema.NodeTypes;
            var origIds   = origTypes.Select(x => x.Id).ToArray();
            var newIds    = newTypes.Select(x => x.Id).ToArray();

            var deletedIds = origIds.Except(newIds).ToArray();

            var canBeModifiedIds = origIds.Intersect(newIds).ToArray();
            var modifiedIds      = new List <int>();

            foreach (var id in canBeModifiedIds)
            {
                var orig = GetControlString(origTypes.GetItemById(id));
                var @new = GetControlString(newTypes.GetItemById(id));
                if (orig != @new)
                {
                    modifiedIds.Add(id);
                }
            }

            return(deletedIds.Union(modifiedIds));
        }
Beispiel #13
0
 private static void WriteDeleteContentListType(SchemaWriter writer, ContentListType contentListType, SchemaEditor origSchema, List <PropertySet> modifiedPropertySets)
 {
     writer.DeleteContentListType(contentListType);
     if (!modifiedPropertySets.Contains(contentListType))
     {
         modifiedPropertySets.Add(contentListType);
     }
 }
Beispiel #14
0
        private static void WriteDeleteNodeType(SchemaWriter writer, NodeType nodeType, SchemaEditor origSchema, List <PropertySet> modifiedPropertySets)
        {
            // recursive
            foreach (NodeType childType in nodeType.Children)
            {
                WriteDeleteNodeType(writer, childType, origSchema, modifiedPropertySets);
            }

            writer.DeleteNodeType(nodeType);
            if (!modifiedPropertySets.Contains(nodeType))
            {
                modifiedPropertySets.Add(nodeType);
            }
        }