/// <summary>
 ///     Translate Model to DSL Model.
 /// </summary>
 /// <param name="modelElement"></param>
 /// <param name="partition"></param>
 /// <returns></returns>
 internal abstract DslModeling.ModelElement TranslateModelToDslModel(EFObject modelElement, DslModeling.Partition partition);
        public MetaModel LoadLibrary(Microsoft.VisualStudio.Modeling.SerializationResult serializationResult)
        {
            System.IO.FileInfo fileInfo = new System.IO.FileInfo(this.AbsoluteFilePath);
            if (!fileInfo.Exists)
                return null;

            if (System.String.Compare(fileInfo.Extension, ".lngdsl", System.StringComparison.OrdinalIgnoreCase) != 0)
                return null;

            MetaModel dslLibrary = FindLibrary(Store, this.AbsoluteFilePath);
            if (dslLibrary != null)
            {
               
                if (dslLibrary.IsTopMost)
                {
                    MessageBox.Show("Circular dependency to the top most meta model! Can not load library.");
                    throw new ArgumentException("Circular dependency to the top most meta model! Can not load library.");
                }
                //this.ImportedLibrary = dslLibrary;
                this.Name = dslLibrary.Name;
                return dslLibrary;
            }

            
            Microsoft.VisualStudio.Modeling.Partition partition = new Microsoft.VisualStudio.Modeling.Partition(Store);
            
            MetaModel metaModel;
            try
            {
                metaModel = LanguageDSLSerializationHelper.Instance.LoadModel(serializationResult, partition, this.AbsoluteFilePath, null, null, null, false);

                ReadOnlyCollection<ModelElement> elements = this.Store.ElementDirectory.FindElements(MetaModel.DomainClassId);
                foreach (ModelElement element in elements)
                    if (element is MetaModel && element != metaModel)
                    {
                        MetaModel m = element as MetaModel;
                        if (m.Name == metaModel.Name)
                        {
                            metaModel.Delete();
                            throw new ArgumentException("Library can not have a name of an already included library or main meta model.");
                        }
                    }
            }
            catch (System.Xml.XmlException)
            {
                metaModel = null;
            }
            if ((metaModel != null) && !serializationResult.Failed)
            {
                this.ImportedLibrary = metaModel;
                this.Name = metaModel.Name;

                if( ImmutabilityExtensionMethods.GetLocks(metaModel) != Locks.All )
                    SetLocks(metaModel, Locks.All);
            }
            else
            {
                metaModel = null;
            }
            return metaModel;
        }
        internal override DslModeling.ModelElement TranslateModelToDslModel(EFObject modelElement, DslModeling.Partition partition)
        {
            DesignerModel.Diagram diagram = null;

            if (modelElement != null)
            {
                diagram = modelElement as DesignerModel.Diagram;
                if (diagram == null)
                {
                    throw new ArgumentException("modelElement should be a diagram");
                }
            }

            // get the service so that we can access the root of the entity model
            var service = _editingContext.GetEFArtifactService();

            if (service == null)
            {
                throw new InvalidOperationException(EntityDesignerResources.Error_NoArtifactService);
            }

            EntityDesignerViewModel entityViewModel = null;

            var entityDesignArtifact = service.Artifact as EntityDesignArtifact;

            Debug.Assert(entityDesignArtifact != null, "Artifact is not type of EntityDesignArtifact");

            if (entityDesignArtifact != null)
            {
                // Only translate the Escher Model to Dsl Model if the artifact is designer safe.
                if (entityDesignArtifact.IsDesignerSafe)
                {
                    // now get the root of the model.
                    var model = entityDesignArtifact.ConceptualModel;
                    Debug.Assert(model != null, "Could not get ConceptualModel from the artifact.");

                    if (model != null)
                    {
                        entityViewModel =
                            ModelToDesignerModelXRef.GetNewOrExisting(_editingContext, model, partition) as EntityDesignerViewModel;
                        entityViewModel.Namespace = model.Namespace.Value;

                        // If the passed-in diagram is null, retrieve the first diagram if available.
                        if (diagram == null &&
                            entityDesignArtifact.DesignerInfo() != null &&
                            entityDesignArtifact.DesignerInfo().Diagrams != null &&
                            entityDesignArtifact.DesignerInfo().Diagrams.FirstDiagram != null)
                        {
                            diagram = entityDesignArtifact.DesignerInfo().Diagrams.FirstDiagram;
                        }

                        IList <ModelEntityType>  entities;
                        IList <ModelAssociation> associations;

                        if (diagram != null)
                        {
                            RetrieveModelElementsFromDiagram(diagram, out entities, out associations);
                        }
                        else
                        {
                            entities     = model.EntityTypes().ToList();
                            associations = model.Associations().ToList();
                        }
                        TranslateEntityModel(entities, associations, entityViewModel);
                    }
                }
                else
                {
                    // return empty view model if the artifact is not designer safe so the Diagram can show safe-mode watermark
                    entityViewModel = new EntityDesignerViewModel(partition);
                    entityViewModel.EditingContext = _editingContext;
                }
            }

            return(entityViewModel);
        }