private void LoadDiagram(
            DslModeling.SerializationResult serializationResult,
            DslModeling.Partition diagramPartition,
            PatternModelSchema modelRoot,
            DslModeling.DomainXmlSerializerDirectory directory,
            DslModeling.DomainClassXmlSerializer diagramSerializer,
            string diagramFileName,
            DslModeling.ISchemaResolver schemaResolver,
            ValidationController validationController)
        {
            if (diagramSerializer != null)
            {
                PatternModelSchemaDiagram diagram = null;

                using (FileStream fileStream = File.OpenRead(diagramFileName))
                {
                    var serializationContext = new DslModeling.SerializationContext(directory, fileStream.Name, serializationResult);
                    this.InitializeSerializationContext(diagramPartition, serializationContext, true);
                    var transactionContext = new DslModeling.TransactionContext();
                    transactionContext.Add(DslModeling.SerializationContext.TransactionContextKey, serializationContext);

                    using (var transaction = diagramPartition.Store.TransactionManager.BeginTransaction(Resources.PatternModelSerializationHelper_TransactionDescriptionLoadingDiagram, true, transactionContext))
                    {
                        // Ensure there is some content in the file. Blank (or almost blank, to account for encoding header bytes, etc.)
                        // files will cause a new diagram to be created and returned 
                        if (fileStream.Length > 5)
                        {
                            var settings = PatternModelSerializationHelper.Instance.CreateXmlReaderSettings(serializationContext, false);
                            try
                            {
                                using (var reader = XmlReader.Create(fileStream, settings))
                                {
                                    reader.MoveToContent();
                                    diagram = diagramSerializer.TryCreateInstance(serializationContext, reader, diagramPartition) as PatternModelSchemaDiagram;
                                    if (diagram != null)
                                    {
                                        this.ReadRootElement(serializationContext, diagram, reader, schemaResolver);
                                    }
                                }
                            }
                            catch (XmlException ex)
                            {
                                DslModeling.SerializationUtilities.AddMessage(serializationContext, DslModeling.SerializationMessageKind.Error, ex);
                            }

                            if (serializationResult.Failed)
                            {
                                // Serialization error encountered, rollback the transaction.
                                diagram = null;
                                transaction.Rollback();
                            }
                        }

                        if (diagram == null && !serializationResult.Failed)
                        {
                            // Create diagram if it doesn't exist
                            if (diagramFileName.EndsWith(DesignerConstants.DiagramFileExtension, StringComparison.OrdinalIgnoreCase))
                            {
                                diagram = new PatternModelSchemaDiagram(
                                    diagramPartition.Store,
                                    new DslModeling.PropertyAssignment(DslDiagrams.Diagram.NameDomainPropertyId, Guid.NewGuid()));
                            }
                            else
                            {
                                diagram = this.CreateDiagramHelper(diagramPartition, modelRoot);
                            }
                        }

                        if (transaction.IsActive)
                        {
                            transaction.Commit();
                        }
                    } // End inner Tx

                    // Do load-time validation if a ValidationController is provided.
                    if (!serializationResult.Failed && validationController != null)
                    {
                        using (new SerializationValidationObserver(serializationResult, validationController))
                        {
                            validationController.Validate(diagramPartition, ValidationCategories.Load);
                        }
                    }
                }

                if (diagram != null)
                {
                    if (!serializationResult.Failed)
                    {	// Succeeded.
                        diagram.ModelElement = modelRoot;
                        diagram.PostDeserialization(true);
                        this.CheckForOrphanedShapes(diagram, serializationResult);
                    }
                    else
                    {	// Failed.
                        diagram.PostDeserialization(false);
                    }
                }
            }
        }
Esempio n. 2
0
        private void LoadDiagram(
            DslModeling.SerializationResult serializationResult,
            DslModeling.Partition diagramPartition,
            PatternModelSchema modelRoot,
            DslModeling.DomainXmlSerializerDirectory directory,
            DslModeling.DomainClassXmlSerializer diagramSerializer,
            string diagramFileName,
            DslModeling.ISchemaResolver schemaResolver,
            ValidationController validationController)
        {
            if (diagramSerializer != null)
            {
                PatternModelSchemaDiagram diagram = null;

                using (FileStream fileStream = File.OpenRead(diagramFileName))
                {
                    var serializationContext = new DslModeling.SerializationContext(directory, fileStream.Name, serializationResult);
                    this.InitializeSerializationContext(diagramPartition, serializationContext, true);
                    var transactionContext = new DslModeling.TransactionContext();
                    transactionContext.Add(DslModeling.SerializationContext.TransactionContextKey, serializationContext);

                    using (var transaction = diagramPartition.Store.TransactionManager.BeginTransaction(Resources.PatternModelSerializationHelper_TransactionDescriptionLoadingDiagram, true, transactionContext))
                    {
                        // Ensure there is some content in the file. Blank (or almost blank, to account for encoding header bytes, etc.)
                        // files will cause a new diagram to be created and returned
                        if (fileStream.Length > 5)
                        {
                            var settings = PatternModelSerializationHelper.Instance.CreateXmlReaderSettings(serializationContext, false);
                            try
                            {
                                using (var reader = XmlReader.Create(fileStream, settings))
                                {
                                    reader.MoveToContent();
                                    diagram = diagramSerializer.TryCreateInstance(serializationContext, reader, diagramPartition) as PatternModelSchemaDiagram;
                                    if (diagram != null)
                                    {
                                        this.ReadRootElement(serializationContext, diagram, reader, schemaResolver);
                                    }
                                }
                            }
                            catch (XmlException ex)
                            {
                                DslModeling.SerializationUtilities.AddMessage(serializationContext, DslModeling.SerializationMessageKind.Error, ex);
                            }

                            if (serializationResult.Failed)
                            {
                                // Serialization error encountered, rollback the transaction.
                                diagram = null;
                                transaction.Rollback();
                            }
                        }

                        if (diagram == null && !serializationResult.Failed)
                        {
                            // Create diagram if it doesn't exist
                            if (diagramFileName.EndsWith(DesignerConstants.DiagramFileExtension, StringComparison.OrdinalIgnoreCase))
                            {
                                diagram = new PatternModelSchemaDiagram(
                                    diagramPartition.Store,
                                    new DslModeling.PropertyAssignment(DslDiagrams.Diagram.NameDomainPropertyId, Guid.NewGuid()));
                            }
                            else
                            {
                                diagram = this.CreateDiagramHelper(diagramPartition, modelRoot);
                            }
                        }

                        if (transaction.IsActive)
                        {
                            transaction.Commit();
                        }
                    } // End inner Tx

                    // Do load-time validation if a ValidationController is provided.
                    if (!serializationResult.Failed && validationController != null)
                    {
                        using (new SerializationValidationObserver(serializationResult, validationController))
                        {
                            validationController.Validate(diagramPartition, ValidationCategories.Load);
                        }
                    }
                }

                if (diagram != null)
                {
                    if (!serializationResult.Failed)
                    {   // Succeeded.
                        diagram.ModelElement = modelRoot;
                        diagram.PostDeserialization(true);
                        this.CheckForOrphanedShapes(diagram, serializationResult);
                    }
                    else
                    {   // Failed.
                        diagram.PostDeserialization(false);
                    }
                }
            }
        }
        public PatternModelSchema LoadModel(DslModeling.Store store, Stream stream, DslModeling.ISchemaResolver schemaResolver, DslModeling.ISerializerLocator serializerLocator)
        {
            Guard.NotNull(() => store, store);
            Guard.NotNull(() => stream, stream);

            if (!store.DefaultPartition.Store.TransactionActive)
            {
                throw new InvalidOperationException(PatternModelDomainModel.SingletonResourceManager.GetString("MissingTransaction"));
            }

            var directory = this.GetDirectory(store.DefaultPartition.Store);
            var modelRootSerializer = directory.GetSerializer(PatternModelSchema.DomainClassId);
            var serializationResult = new DslModeling.SerializationResult();

            Debug.Assert(modelRootSerializer != null, @"Cannot find serializer for PatternModel!");

            if (modelRootSerializer != null)
            {
                PatternModelSchema modelRoot = null;

                var serializationContext = new DslModeling.SerializationContext(directory, string.Empty, serializationResult);
                this.InitializeSerializationContext(store.DefaultPartition, serializationContext, true);

                var transactionContext = new DslModeling.TransactionContext();
                transactionContext.Add(DslModeling.SerializationContext.TransactionContextKey, serializationContext);

                using (var tx = store.DefaultPartition.Store.TransactionManager.BeginTransaction(Resources.PatternModelSerializationHelper_TransactionDescriptionLoadingModel, true, transactionContext))
                {
                    try
                    {
                        var settings = PatternModelSerializationHelper.Instance.CreateXmlReaderSettings(serializationContext, false);

                        using (var reader = XmlReader.Create(stream, settings))
                        {
                            reader.Read();
                            Encoding encoding;
                            if (this.TryGetEncoding(reader, out encoding))
                            {
                                serializationResult.Encoding = encoding;
                            }

                            DslModeling.SerializationUtilities.ResolveDomainModels(reader, serializerLocator, store.DefaultPartition.Store);

                            reader.MoveToContent();

                            modelRoot = modelRootSerializer.TryCreateInstance(serializationContext, reader, store.DefaultPartition) as PatternModelSchema;

                            if (modelRoot != null && !serializationResult.Failed)
                            {
                                this.ReadRootElement(serializationContext, modelRoot, reader, schemaResolver);
                            }
                        }
                    }
                    catch (XmlException ex)
                    {
                        DslModeling.SerializationUtilities.AddMessage(serializationContext, DslModeling.SerializationMessageKind.Error, ex);
                    }

                    if (modelRoot == null && !serializationResult.Failed)
                    {
                        modelRoot = this.CreateModelHelper(store.DefaultPartition);
                    }

                    if (tx.IsActive)
                    {
                        tx.Commit();
                    }
                }

                // Set tracking properties
                ResetTrackingProperties(store);

                return modelRoot;
            }

            return null;
        }
Esempio n. 4
0
        public PatternModelSchema LoadModel(DslModeling.Store store, Stream stream, DslModeling.ISchemaResolver schemaResolver, DslModeling.ISerializerLocator serializerLocator)
        {
            Guard.NotNull(() => store, store);
            Guard.NotNull(() => stream, stream);

            if (!store.DefaultPartition.Store.TransactionActive)
            {
                throw new InvalidOperationException(PatternModelDomainModel.SingletonResourceManager.GetString("MissingTransaction"));
            }

            var directory           = this.GetDirectory(store.DefaultPartition.Store);
            var modelRootSerializer = directory.GetSerializer(PatternModelSchema.DomainClassId);
            var serializationResult = new DslModeling.SerializationResult();

            Debug.Assert(modelRootSerializer != null, @"Cannot find serializer for PatternModel!");

            if (modelRootSerializer != null)
            {
                PatternModelSchema modelRoot = null;

                var serializationContext = new DslModeling.SerializationContext(directory, string.Empty, serializationResult);
                this.InitializeSerializationContext(store.DefaultPartition, serializationContext, true);

                var transactionContext = new DslModeling.TransactionContext();
                transactionContext.Add(DslModeling.SerializationContext.TransactionContextKey, serializationContext);

                using (var tx = store.DefaultPartition.Store.TransactionManager.BeginTransaction(Resources.PatternModelSerializationHelper_TransactionDescriptionLoadingModel, true, transactionContext))
                {
                    try
                    {
                        var settings = PatternModelSerializationHelper.Instance.CreateXmlReaderSettings(serializationContext, false);

                        using (var reader = XmlReader.Create(stream, settings))
                        {
                            reader.Read();
                            Encoding encoding;
                            if (this.TryGetEncoding(reader, out encoding))
                            {
                                serializationResult.Encoding = encoding;
                            }

                            DslModeling.SerializationUtilities.ResolveDomainModels(reader, serializerLocator, store.DefaultPartition.Store);

                            reader.MoveToContent();

                            modelRoot = modelRootSerializer.TryCreateInstance(serializationContext, reader, store.DefaultPartition) as PatternModelSchema;

                            if (modelRoot != null && !serializationResult.Failed)
                            {
                                this.ReadRootElement(serializationContext, modelRoot, reader, schemaResolver);
                            }
                        }
                    }
                    catch (XmlException ex)
                    {
                        DslModeling.SerializationUtilities.AddMessage(serializationContext, DslModeling.SerializationMessageKind.Error, ex);
                    }

                    if (modelRoot == null && !serializationResult.Failed)
                    {
                        modelRoot = this.CreateModelHelper(store.DefaultPartition);
                    }

                    if (tx.IsActive)
                    {
                        tx.Commit();
                    }
                }

                // Set tracking properties
                ResetTrackingProperties(store);

                return(modelRoot);
            }

            return(null);
        }