Esempio n. 1
0
        internal PatternModelSchema LoadModelAndDiagrams(
            DslModeling.SerializationResult serializationResult,
            DslModeling.Partition modelPartition,
            string modelFileName,
            DslModeling.Partition diagramPartition,
            IEnumerable <string> diagramFileNames,
            DslModeling.ISchemaResolver schemaResolver,
            ValidationController validationController,
            DslModeling.ISerializerLocator serializerLocator)
        {
            Guard.NotNull(() => serializationResult, serializationResult);
            Guard.NotNull(() => modelPartition, modelPartition);
            Guard.NotNullOrEmpty(() => modelFileName, modelFileName);
            Guard.NotNull(() => diagramPartition, diagramPartition);
            Guard.NotNull(() => diagramFileNames, diagramFileNames);
            Guard.NotNull(() => schemaResolver, schemaResolver);
            Guard.NotNull(() => serializerLocator, serializerLocator);

            PatternModelSchema modelRoot;

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

            modelRoot = this.LoadModel(serializationResult, modelPartition, modelFileName, schemaResolver, validationController, serializerLocator);

            if (serializationResult.Failed)
            {
                // don't try to deserialize diagram data if model load failed.
                return(modelRoot);
            }

            var directory         = this.GetDirectory(diagramPartition.Store);
            var diagramSerializer = directory.GetSerializer(PatternModelSchemaDiagram.DomainClassId);
            var views             = modelRoot.Store.ElementDirectory.AllElements.OfType <ViewSchema>().ToList();

            foreach (var view in views)
            {
                var diagramFileName = diagramFileNames.FirstOrDefault(file => Path.GetFileNameWithoutExtension(file).Contains(view.DiagramId));

                if (diagramFileName == null)
                {
                    PatternModelSerializationHelper.CreatePatternModelSchemaDiagram(serializationResult, diagramPartition, modelRoot, view.DiagramId);
                }
                else
                {
                    this.LoadDiagram(serializationResult, diagramPartition, modelRoot, directory, diagramSerializer, diagramFileName, schemaResolver, validationController);
                }
            }

            // Set tracking properties
            ResetTrackingProperties(modelPartition.Store);

            return(modelRoot);
        }
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);
                    }
                }
            }
        }
Esempio n. 3
0
        internal static PatternModelSchemaDiagram CreatePatternModelSchemaDiagram(DslModeling.SerializationResult serializationResult, DslModeling.Partition diagramPartition, PatternModelSchema modelRoot, string diagramId)
        {
            var diagram = new PatternModelSchemaDiagram(
                diagramPartition.Store,
                new DslModeling.PropertyAssignment(DslDiagrams.Diagram.NameDomainPropertyId, diagramId),
                new DslModeling.PropertyAssignment(
                    DslModeling.ElementFactory.IdPropertyAssignment,
                    !string.IsNullOrEmpty(diagramId) ? new Guid(diagramId) : Guid.NewGuid()));

            diagram.ModelElement = modelRoot;
            diagram.PostDeserialization(true);
            DoCheckForOrphanedShapes(diagram, serializationResult);

            return(diagram);
        }
Esempio n. 4
0
        /// <summary>
        /// Loads the model from a filename.
        /// </summary>
        /// <param name="serializationResult">The seriazation result.</param>
        /// <param name="partition">The state partition.</param>
        /// <param name="fileName">the filename of the model.</param>
        /// <param name="schemaResolver">The schemaresolver.</param>
        /// <param name="validationController">The validation controller.</param>
        /// <param name="serializerLocator">The serialization locator.</param>
        /// <remarks>This overload is called by teh T4Runner engine.</remarks>
        public override PatternModelSchema LoadModel(DslModeling.SerializationResult serializationResult, DslModeling.Partition partition, string fileName, DslModeling.ISchemaResolver schemaResolver, ValidationController validationController, DslModeling.ISerializerLocator serializerLocator)
        {
            var productLine = base.LoadModel(serializationResult, partition, fileName, schemaResolver, validationController, serializerLocator);

            // Set tracking properties
            if (partition != null)
            {
                ResetTrackingProperties(partition.Store);
            }

            return(productLine);
        }