private static void FixUpDiagramView(LibraryModelContext model)
        {
            foreach (DiagramClass diagramClass in model.DiagramClasses)
            {
                DiagramClassView vm = new DiagramClassView(model.Store);
                vm.IsExpanded = true;
                vm.DiagramClass = diagramClass;

                // add shapes views
                foreach (PresentationElementClass p in diagramClass.PresentationElements)
                {
                    if (p is ShapeClass)
                    {
                        ShapeClass shapeClass = p as ShapeClass;
                        if (shapeClass.Parent == null)
                        {
                            RootDiagramNode node = new RootDiagramNode(p.Store);
                            node.PresentationElementClass = p;

                            vm.RootDiagramNodes.Add(node);
                        }
                        else
                        {
                            EmbeddingDiagramNode newNode = new EmbeddingDiagramNode(model.Store);
                            newNode.PresentationElementClass = p;
                        }
                    }
                    else
                    {
                        RootDiagramNode node = new RootDiagramNode(p.Store);
                        node.PresentationElementClass = p;

                        vm.RootDiagramNodes.Add(node);
                    }
                }

                foreach (PresentationElementClass p in diagramClass.PresentationElements)
                {
                    if (p is ShapeClass)
                    {
                        ShapeClass shapeClass = p as ShapeClass;
                        if (shapeClass.Parent != null)
                        {
                            EmbeddingDiagramNode source = shapeClass.Parent.DiagramTreeNode as EmbeddingDiagramNode;
                            EmbeddingDiagramNode target = p.DiagramTreeNode as EmbeddingDiagramNode;

                            if (source != null && target != null)
                                new EmbeddingDiagramNodeHasEmbeddingDiagramNodes(source, target);
                        }
                    }
                }

                model.ViewContext.DiagramView.DiagramClassViews.Add(vm);
            }
        }
        public override void ElementAdded(ElementAddedEventArgs e)
        {
            if (e.ModelElement != null)
                if (e.ModelElement.Store.TransactionManager.CurrentTransaction != null)
                    if (e.ModelElement.Store.TransactionManager.CurrentTransaction.IsSerializing)
                        return;

            if (e.ModelElement == null)
                return;

            if (ImmutabilityExtensionMethods.GetLocks(e.ModelElement) != Locks.None)
                return;

            ModelContext modelContext = e.ModelElement as ModelContext;
            if (modelContext != null)
            {
                if (modelContext.ViewContext == null)
                {
                    ViewContext viewContext = new ViewContext(modelContext.Store);
                    viewContext.DomainModelTreeView = new DomainModelTreeView(modelContext.Store);
                    viewContext.DiagramView = new DiagramView(modelContext.Store);
                    modelContext.ViewContext = viewContext;
                    modelContext.MetaModel.View.ViewContexts.Add(viewContext);
                }

                if (modelContext.DiagramClasses.Count == 0)
                {
                    DesignerDiagramClass ddC = new DesignerDiagramClass(modelContext.Store);
                    //ddC.Name = "DesignerDiagram";
                    ddC.Name = NameHelper.GetUniqueName(modelContext.Store, DesignerDiagramClass.DomainClassId);
                    ddC.Title = "Designer";
                    modelContext.DiagramClasses.Add(ddC);

                    DiagramClassView vm = new DiagramClassView(modelContext.Store);
                    vm.IsExpanded = true;
                    vm.DiagramClass = ddC;
                    modelContext.ViewContext.DiagramView.DiagramClassViews.Add(vm);
                }

                DomainClass domainClass = modelContext.Store.ElementFactory.CreateElement(DomainClass.DomainClassId) as DomainClass;
                domainClass.IsDomainModel = true;
                Microsoft.VisualStudio.Modeling.ElementOperations elementOperations = new Microsoft.VisualStudio.Modeling.ElementOperations(modelContext.Store as IServiceProvider, modelContext.Store.DefaultPartition);
                Microsoft.VisualStudio.Modeling.ElementGroup elementGroup = new Microsoft.VisualStudio.Modeling.ElementGroup(modelContext.Store.DefaultPartition);
                elementGroup.Add(domainClass);
                elementGroup.MarkAsRoot(domainClass);
                elementOperations.MergeElementGroup(modelContext, elementGroup);
                domainClass.Name = NameHelper.GetUniqueName(modelContext.Store, DomainClass.DomainClassId);

                SerializedDomainModel child = new SerializedDomainModel(domainClass.Store);
                child.DomainClass = domainClass;
                child.SerializationName = domainClass.SerializationName;

                if (modelContext.SerializationModel == null)
                {
                    modelContext.SerializationModel = new SerializationModel(modelContext.Store);
                }

                modelContext.SerializationModel.SerializedDomainModel = child;
                SerializationHelper.AddSerializationDomainProperties(domainClass.Store, domainClass);
            }
        }
        public static void PostProcessModelLoad(MetaModel model)
        {
            // package and custom editor GUIDs
            if (model.PackageGuid == null || model.PackageGuid == Guid.Empty)
            {
                model.PackageGuid = Guid.NewGuid();
            }
            if (model.CustomExtensionGuid == null || model.CustomExtensionGuid == Guid.Empty)
            {
                model.CustomExtensionGuid = Guid.NewGuid();
            }
            

            #region relationship targets fixup
            ReadOnlyCollection<DomainRelationship> rels = model.AllRelationships;
            foreach (DomainRelationship rel in rels)
            {
                if (rel.Target.RolePlayer == null)
                {
                    ReferenceRelationship referenceRelationship = rel as ReferenceRelationship;
                    if (referenceRelationship != null)
                    {
                        if (referenceRelationship.ReferenceRSNode != null)
                            referenceRelationship.ReferenceRSNode.Delete();

                        if (referenceRelationship.SerializedReferenceRelationship != null)
                            referenceRelationship.SerializedReferenceRelationship.Delete();
                    }

                    EmbeddingRelationship embeddingRelationship = rel as EmbeddingRelationship;
                    if (embeddingRelationship != null)
                    {
                        if (embeddingRelationship.EmbeddingRSNode != null)
                            embeddingRelationship.EmbeddingRSNode.Delete();

                        if (embeddingRelationship.SerializedEmbeddingRelationship != null)
                            embeddingRelationship.SerializedEmbeddingRelationship.Delete();
                    }

                    rel.Delete();
                }
            }
            #endregion

            #region inconsistent serialization elements
            foreach (BaseModelContext context in model.ModelContexts)
                if (context is LibraryModelContext)
                {
                    LibraryModelContext lib = context as LibraryModelContext;
                    if (lib.SerializationModel != null)
                        for (int i = lib.SerializationModel.Children.Count - 1; i >= 0; i--)
                        {
                            SerializationClass c = lib.SerializationModel.Children[i];
                            if (c is SerializedDomainClass)
                            {
                                SerializedDomainClass s = c as SerializedDomainClass;
                                if (s.DomainClass == null)
                                {
                                    s.Delete();
                                }

                                continue;
                            }
                            else if (c is SerializedEmbeddingRelationship)
                            {
                                SerializedEmbeddingRelationship s = c as SerializedEmbeddingRelationship;
                                if (s.EmbeddingRelationship == null)
                                {
                                    s.Delete();
                                }

                                continue;
                            }
                            else if (c is SerializedReferenceRelationship)
                            {
                                SerializedReferenceRelationship s = c as SerializedReferenceRelationship;
                                if (s.ReferenceRelationship == null)
                                {
                                    s.Delete();
                                }

                                continue;
                            }

                            // element has not been deleted, see if its properties are ok
                            for (int y = c.Properties.Count - 1; y >= 0; y--)
                                if (c.Properties[y] == null)
                                {
                                    c.Properties[y].Delete();
                                }

                        }
                }
            #endregion

            #region derived classes serialization items fixup
            if (model.MetaModelLibraries.Count > 0)
            {
                ReadOnlyCollection<ModelElement> elements = model.Store.ElementDirectory.FindElements(DomainClassReferencesBaseClass.DomainClassId);
                foreach (ModelElement m in elements)
                {
                    DomainClassReferencesBaseClass con = m as DomainClassReferencesBaseClass;
                    if (con != null)
                        if (con.BaseClass != null)
                        {
                            if (con.BaseClass.ModelContext.MetaModel != model)
                            {
                                foreach (DomainClass derivedClass in con.BaseClass.DerivedClasses)
                                    FixUpDerivedClasses(derivedClass, model);
                            }
                        }
                }

                ReadOnlyCollection<ModelElement> elementsCon = model.Store.ElementDirectory.FindElements(DomainRelationshipReferencesBaseRelationship.DomainClassId);
                foreach (ModelElement m in elementsCon)
                {
                    DomainRelationshipReferencesBaseRelationship con = m as DomainRelationshipReferencesBaseRelationship;
                    if (con != null)
                        if (con.BaseRelationship != null)
                        {
                            if (con.BaseRelationship.ModelContext.MetaModel != model)
                            {
                                foreach (DomainRelationship derivedClass in con.BaseRelationship.DerivedRelationships)
                                    FixUpDerivedRelationships(derivedClass, model);
                            }
                        }
                }


            }
            #endregion

            #region check if model contains all required elements
            // property grid editors
            if (model.PropertyGridEditors.Count == 0)
                FixUpPropertyGridEditors(model);

            // domain types
            if (model.DomainTypes.Count == 0)
                FixUpDomainTypes(model);

            // model context
            if (model.ModelContexts.Count == 0)
                FixUpModelContext(model);

            // validation
            if (model.Validation == null)
                model.Validation = new Validation(model.Store);

            if (model.View == null)
                model.View = new View(model.Store);

            if (model.View.ModelTree == null)
                model.View.ModelTree = new ModelTree(model.Store);

            foreach (BaseModelContext mContext in model.ModelContexts)
                if (mContext is LibraryModelContext)
                {
                    LibraryModelContext m = mContext as LibraryModelContext;
                    if (m.DiagramClasses.Count == 0 && m is ModelContext)
                    {
                        DesignerDiagramClass ddC = new DesignerDiagramClass(model.Store);
                        ddC.Name = "DesignerDiagram";
                        ddC.Title = "Designer";

                        m.DiagramClasses.Add(ddC);
                    }

                    if (m.ViewContext == null)
                    {
                        m.ViewContext = new ViewContext(model.Store);
                        m.ViewContext.DomainModelTreeView = new DomainModelTreeView(model.Store);
                        m.ViewContext.DiagramView = new DiagramView(model.Store);

                        model.View.ViewContexts.Add(m.ViewContext);

                        FixUpDomainModelTreeView(m);
                        FixUpDiagramView(m);
                    }

                    if (m.ViewContext.DiagramView == null || m.ViewContext.DomainModelTreeView == null)
                    {
                        if (m.ViewContext.DomainModelTreeView == null)
                        {
                            m.ViewContext.DomainModelTreeView = new DomainModelTreeView(model.Store);
                            FixUpDomainModelTreeView(m);
                        }

                        if (m.ViewContext.DiagramView == null)
                        {
                            m.ViewContext.DiagramView = new DiagramView(model.Store);
                            FixUpDiagramView(m);
                        }
                    }

                    // diagram class view for designer diagram
                    if (m.ViewContext.DiagramView.DiagramClassViews.Count == 0 && m is ModelContext)
                    {
                        DiagramClassView vm = new DiagramClassView(model.Store);
                        vm.IsExpanded = true;
                        foreach (DiagramClass d in m.DiagramClasses)
                            if (d is DesignerDiagramClass)
                            {
                                vm.DiagramClass = d;
                                break;
                            }

                        m.ViewContext.DiagramView.DiagramClassViews.Add(vm);
                    }

                    // serialization
                    if (m.SerializationModel == null)
                        m.SerializationModel = new SerializationModel(model.Store);

                    // serialized domain model
                    if (m is ModelContext)
                        if (m.SerializationModel.SerializedDomainModel == null)
                            FixUpSerializedDomainModel(m as ModelContext);
                }
            #endregion
            
            // view ids.
            if (model.View != null)
            {
                if (model.View.ModelTreeId == null || model.View.ModelTreeId == Guid.Empty)
                    model.View.ModelTreeId = Guid.NewGuid();

                if (model.View.DependenciesViewId == null || model.View.DependenciesViewId == Guid.Empty)
                    model.View.DependenciesViewId = Guid.NewGuid();

                if (model.View.ErrorListId == null || model.View.ErrorListId == Guid.Empty)
                    model.View.ErrorListId = Guid.NewGuid();

                if (model.View.PropertyGridId == null || model.View.PropertyGridId == Guid.Empty)
                    model.View.PropertyGridId = Guid.NewGuid();

                if (model.View.SearchId == null || model.View.SearchId == Guid.Empty)
                    model.View.SearchId = Guid.NewGuid();

                if (model.View.SearchResultId == null || model.View.SearchResultId == Guid.Empty)
                    model.View.SearchResultId = Guid.NewGuid();

                if (model.View.PluginWindowId == null || model.View.PluginWindowId == Guid.Empty)
                    model.View.PluginWindowId = Guid.NewGuid();
            }
        }