示例#1
0
 public override void InvokeHandler(CommandSetState state)
 {
     ModelerTransaction.Enter(() =>
     {
         var diagram = state.CurrentDocView.CurrentDiagram;
         var sync    = new Utilities.DbSchemaImporter(diagram);
         sync.ImportModels();
     });
 }
示例#2
0
        public void HandleElementAdded(object sender, ElementAddedEventArgs e)
        {
            if (ModelerTransaction.IsInTransaction)
            {
                return;
            }

            ModelerTransaction.Enter(() =>
            {
                using (Transaction tx = (sender as Store).TransactionManager.BeginTransaction())
                {
                    if (e.ModelElement is ModelClass)
                    {
                        e.ModelElement.Delete();
                        var dlg = new AddEntityForm(sender as Store);
                        dlg.ShowDialog();
                    }
                    else if (e.ModelElement is Inheritance)
                    {
                        var inheritance      = e.ModelElement as Inheritance;
                        var baseClassName    = inheritance.Superclass.Name;
                        var derivedClassName = inheritance.Subclass.Name;
                        inheritance.Delete();
                        InheritanceUtil.AddInheritance(sender as Store, baseClassName, derivedClassName);
                    }
                    else if (e.ModelElement is Association)
                    {
                        var assoc = e.ModelElement as Association;
                        var dlg   = new AddAssociationForm(sender as Store);
                        dlg.SetSelectedClasses(assoc.Source, assoc.Target);
                        assoc.Delete();
                        dlg.ShowDialog();
                    }
                    else if (e.ModelElement is NavigationProperty)
                    {
                        var parentClass = (e.ModelElement as NavigationProperty).ModelClass;
                        e.ModelElement.Delete();
                        var addAssociationForm = new AddAssociationForm(sender as Store);
                        addAssociationForm.SetSelectedClasses(parentClass);
                        addAssociationForm.ShowDialog();
                    }

                    if (tx.HasPendingChanges)
                    {
                        tx.Commit();
                    }
                }
            });
        }
示例#3
0
        public override void InvokeHandler(CommandSetState state)
        {
            var store = state.CurrentDocView.CurrentDiagram.Store;

            ModelerTransaction.Enter(() =>
            {
                using (Transaction tx = store.TransactionManager.BeginTransaction())
                {
                    var addAssociationForm = new AddAssociationForm(state.CurrentDocView.CurrentDiagram.Store);
                    if (state.CurrentSelection.Count == 1)
                    {
                        var selection = state.CurrentSelection.Cast <object>().First();
                        if (selection is ClassShape)
                        {
                            var modelClass = ((ClassShape)selection).ModelElement as ModelClass;
                            addAssociationForm.SetSelectedClasses(modelClass);
                        }
                    }
                    addAssociationForm.ShowDialog();

                    tx.Commit();
                }
            });

            /*
             * MenuCommand command = sender as MenuCommand;
             *
             * StringBuilder sb = new StringBuilder();
             * sb.Append("Command: " + Commands.Values.First(x => x.CommandGuid == command.CommandID.Guid).Type.ToString() + "\n");
             * foreach (object selectedObject in this.CurrentSelection)
             * {
             *  sb.AppendLine("Selected Shape: " + selectedObject.ToString());
             *
             *  if (selectedObject is ClassShape)
             *  {
             *      ModelClass modelClass = (ModelClass)(selectedObject as ClassShape).ModelElement;
             *      sb.AppendLine("*** Related Domain Class: " + modelClass.ToString());
             *  }
             *
             *  if (selectedObject is ClassDiagram)
             *  {
             *  }
             * }
             *
             *
             */
        }
示例#4
0
        public override void InvokeHandler(CommandSetState state)
        {
            var diagram          = state.CurrentDocView.CurrentDiagram;
            var modelRoot        = diagram.Store.ElementDirectory.FindElements <ModelRoot>().Single();
            var connectionString = modelRoot.ConnectionString;

            var dlg = new GenerateSQLForm();

            if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                var sqlGenerator = new DbSchemaGenerator(diagram)
                {
                    CleanUpDbSchema = dlg.CleanupDbSchema,
                    UseNavigationPropertyNameForFKeys = dlg.UseNavigationPropertyNameForFKeys,
                };
                var sb = sqlGenerator.GenerateScripts();
                System.IO.File.WriteAllText(dlg.Filename, sb.ToString());

                if (dlg.OverwriteDatabase)
                {
                    //Creating a connection to the given database
                    using (SqlConnection sqlConnection = new SqlConnection(connectionString))
                    {
                        var originalDatabase = sqlConnection.Database;
                        sqlConnection.Open();

                        //Switching to master database
                        sqlConnection.ChangeDatabase("master");
                        ServerConnection svrConnection = new ServerConnection(sqlConnection);

                        //Recreating database and executing the query file
                        DropAndRecreateDatabase(originalDatabase, svrConnection);
                        svrConnection.ExecuteNonQuery(System.IO.File.ReadAllText(dlg.Filename));
                    }

                    ModelerTransaction.Enter(() =>
                    {
                        //Importing the new schema from database
                        var sync = new Utilities.DbSchemaImporter(diagram);
                        sync.FullDatabaseReload = true;
                        sync.ImportModels();
                    });
                }
                System.Windows.Forms.MessageBox.Show("Sql script generation completed.");
            }
        }
示例#5
0
        public static void AddInheritance(Store store, string baseClassName, string derivedClassName)
        {
            ModelerTransaction.Enter(() =>
            {
                using (Transaction tx = store.TransactionManager.BeginTransaction())
                {
                    var entity = store.ElementDirectory.FindElements <ModelClass>()
                                 .First(m => m.Name == derivedClassName);

                    var baseClass = store.ElementDirectory.FindElements <ModelClass>()
                                    .First(m => m.Name == baseClassName);

                    AddInheritance(baseClass, entity);

                    tx.Commit();
                }
            });
        }
示例#6
0
        public void HandleElementDeleted(object sender, ElementDeletedEventArgs e)
        {
            if (ModelerTransaction.IsInTransaction)
            {
                return;
            }

            ModelerTransaction.Enter(() =>
            {
                var store = sender as Store;
                using (Transaction tx = (sender as Store).TransactionManager.BeginTransaction())
                {
                    if (e.ModelElement is NavigationProperty)
                    {
                        var navProp = e.ModelElement as NavigationProperty;
                        var assoc   = (sender as Store).ElementDirectory.FindElements <Association>().FirstOrDefault(a => a.Name == navProp.Association);
                        if (assoc != null && assoc.IsActive)
                        {
                            if (navProp.IsForeignkey)
                            {
                                var modelClassName = (assoc.End1NavigationProperty == navProp.Name) ? assoc.End1RoleName : assoc.End2RoleName;
                                var modelClass     = (sender as Store).ElementDirectory.FindElements <ModelClass>().FirstOrDefault(c => c.Name == modelClassName);
                                if (modelClass != null)
                                {
                                    deleteField(modelClass, navProp.ForeignkeyColumn);
                                }
                            }
                            DeleteAssociation(assoc);
                        }
                    }
                    else if (e.ModelElement is Association)
                    {
                        DeleteAssociation(e.ModelElement as Association);
                    }

                    if (tx.HasPendingChanges)
                    {
                        tx.Commit();
                    }
                }
            });
        }
示例#7
0
        private void okButton_Click(object sender, EventArgs e)
        {
            ModelerTransaction.Enter(() =>
            {
                using (Transaction tx = _Store.TransactionManager.BeginTransaction())
                {
                    var entity = new ModelClass(_Store)
                    {
                        Name      = entityNameTextBox.Text,
                        TableName = entityNameTextBox.Text
                    };
                    _Store.ElementDirectory.FindElements <ModelRoot>().Single().Types.Add(entity);

                    if (baseClassComboBox.Text != "(None)")
                    {
                        var baseClass = _Store.ElementDirectory.FindElements <ModelClass>()
                                        .First(m => m.Name == baseClassComboBox.Text);

                        InheritanceUtil.AddInheritance(baseClass, entity);
                    }
                    else if (createKeyCheckBox.Checked)
                    {
                        var propType = (BuiltInTypes)Enum.Parse(typeof(BuiltInTypes), propertyTypeComboBox.Text);
                        entity.Fields.Add(new ModelField(_Store)
                        {
                            IsPrimaryKey  = true,
                            Name          = propertyNameTextBox.Text,
                            Type          = (BuiltInTypes)Enum.Parse(typeof(BuiltInTypes), propertyTypeComboBox.Text),
                            IsIdentity    = true,
                            IsDbGenerated = true,
                            ColumnName    = propertyNameTextBox.Text
                        });
                    }

                    tx.Commit();
                }
            });
        }
示例#8
0
        private void InitializeDiagram()
        {
            var diagram = GetCurrentDiagram(this.Store);

            if (!diagram.Initialized)
            {
                // Display a form to the user. The form collects
                // input for initializing the diagram.
                var dlg = new ItemDetailsForm();
                dlg.ShowDialog();
                var data = dlg.GetInitializeData();

                ModelerTransaction.Enter(() =>
                {
                    using (var tx = diagram.Store.TransactionManager.BeginTransaction("Initialize", false))
                    {
                        (diagram.ModelElement as ModelRoot).DataContextName = data.ContextName;
                        diagram.Initialized = true;

                        if (tx.HasPendingChanges)
                        {
                            tx.Commit();
                        }
                    }
                    if (data.ImportFromDatabase)
                    {
                        ConnectionUtil.SetExistingConnection(diagram.ModelElement as ModelRoot);
                        var sync = new Utilities.DbSchemaImporter(diagram);
                        sync.ImportModels();
                    }
                    else
                    {
                        ConnectionUtil.GetOrCreateConnectionString(diagram.ModelElement as ModelRoot, diagram.Name);
                    }
                });
            }
        }
示例#9
0
        public void HandleElementPropertyChanged(object sender, ElementPropertyChangedEventArgs e)
        {
            if (ModelerTransaction.IsInTransaction || e.TransactionContext.ContextInfo.Count != 0 ||
                e.ModelElement.IsDeleted)
            {
                return;
            }

            ModelerTransaction.Enter(() =>
            {
                using (Transaction tx = (sender as Store).TransactionManager.BeginTransaction())
                {
                    if (e.ModelElement is ModelFieldBase)
                    {
                        (e.ModelElement as ModelFieldBase).IsEdited = true;
                    }
                    if (e.ModelElement is Association)
                    {
                        (e.ModelElement as Association).IsEdited = true;
                    }

                    if (e.ModelElement is ModelClass)
                    {
                        if (e.DomainProperty.Name == "Name")
                        {
                            (sender as Store).ElementDirectory.FindElements <Association>().Where(a => a.End1RoleName == e.OldValue.ToString())
                            .ToList().ForEach(a =>
                            {
                                a.End1RoleName = e.NewValue.ToString();
                                setPropertyTypeNName(sender as Store, a.End2RoleName, a.End2NavigationProperty, e.NewValue.ToString());
                            });
                            (sender as Store).ElementDirectory.FindElements <Association>().Where(a => a.End2RoleName == e.OldValue.ToString())
                            .ToList().ForEach(a =>
                            {
                                a.End2RoleName = e.NewValue.ToString();
                                setPropertyTypeNName(sender as Store, a.End1RoleName, a.End1NavigationProperty, e.NewValue.ToString());
                            });
                        }
                    }
                    else if (e.ModelElement is NavigationProperty)
                    {
                        var navProp     = e.ModelElement as NavigationProperty;
                        var propName    = (e.DomainProperty.Name == "Name") ? e.OldValue.ToString() : navProp.Name;
                        var association = (sender as Store).ElementDirectory.FindElements <Association>().First(a => a.Name == navProp.Association);
                        if (e.DomainProperty.Name == "IsForeignkey")
                        {
                            var otherEnd = ModelUtil.GetOtherEnd(navProp);
                            if (ValidateForeignkeyChange((bool)e.NewValue, navProp, otherEnd))
                            {
                                if (navProp.IsForeignkey)
                                {
                                    SetForeignkey(navProp, otherEnd);
                                }
                                else
                                {
                                    SetForeignkey(otherEnd, navProp);
                                }
                            }
                            else
                            {
                                MessageBox.Show("Invalid entry");
                                navProp.IsForeignkey = (bool)e.OldValue;
                            }
                        }
                        else if (e.DomainProperty.Name == "Name" || e.DomainProperty.Name == "Multiplicity")
                        {
                            if (propName == association.End1NavigationProperty)
                            {
                                association.End1NavigationProperty = navProp.Name;
                                association.End2Multiplicity       = navProp.Multiplicity;
                            }
                            else if (propName == association.End2NavigationProperty)
                            {
                                association.End2NavigationProperty = navProp.Name;
                                association.End1Multiplicity       = navProp.Multiplicity;
                            }
                            if (e.DomainProperty.Name == "Multiplicity")
                            {
                                MakeChangesForMultiplicity(association, sender as Store);
                            }
                        }
                    }
                    else if (e.ModelElement is ModelField)
                    {
                        var field = e.ModelElement as ModelField;
                        if (new[] { "Type", "UpdateCheck", "MaxLength" }.Any(x => x == e.DomainProperty.Name))
                        {
                            var nonComparableTypes = new[] { BuiltInTypes.Binary, BuiltInTypes.Timestamp };
                            if ((nonComparableTypes.Any(t => t == field.Type) || (field.Type == BuiltInTypes.String && field.MaxLength == 0)) &&
                                field.UpdateCheck != ConcurrencyCheckFrequency.Never)
                            {
                                field.UpdateCheck = ConcurrencyCheckFrequency.Never;
                            }
                        }
                        else if (e.DomainProperty.Name == "IsPrimaryKey" && (bool)e.NewValue)
                        {
                            field.IsDbGenerated = true;
                            field.IsIdentity    = true;
                            field.Nullable      = false;
                        }
                    }
                    else if (e.ModelElement is Association)
                    {
                        var assoc = e.ModelElement as Association;
                        if (e.DomainProperty.Name.StartsWith("End1"))
                        {
                            var role1         = (sender as Store).ElementDirectory.FindElements <ModelClass>().First(c => c.Name == assoc.End1RoleName);
                            var navPropName   = (e.DomainProperty.Name == "End1NavigationProperty") ? e.OldValue.ToString() : assoc.End1NavigationProperty;
                            var navProperty1  = role1.NavigationProperties.Find(np => np.Name == navPropName);
                            navProperty1.Name = assoc.End1NavigationProperty;
                            ModelUtil.GetOtherEnd(navProperty1).Multiplicity = assoc.End1Multiplicity;
                        }
                        else
                        {
                            var role2         = (sender as Store).ElementDirectory.FindElements <ModelClass>().First(c => c.Name == assoc.End2RoleName);
                            var navPropName   = (e.DomainProperty.Name == "End2NavigationProperty") ? e.OldValue.ToString() : assoc.End2NavigationProperty;
                            var navProperty2  = role2.NavigationProperties.Find(np => np.Name == navPropName);
                            navProperty2.Name = assoc.End2NavigationProperty;
                            ModelUtil.GetOtherEnd(navProperty2).Multiplicity = assoc.End2Multiplicity;
                        }
                        if (e.DomainProperty.Name.EndsWith("Multiplicity"))
                        {
                            MakeChangesForMultiplicity(assoc, sender as Store);
                        }
                    }
                    if (tx.HasPendingChanges)
                    {
                        tx.Commit();
                    }
                }
            });
        }