Esempio n. 1
0
        /// <summary>
        /// Alerts listeners when the shape is dragged and dropped.
        /// </summary>
        /// <param name="e">The diagram drag event arguments.</param>
        public override void OnDragDrop(DiagramDragEventArgs e)
        {
            base.OnDragDrop(e);

            bool canArrangeShapes = NestedChildShapes.Count == 0;

            if (DragDropHelper.OnDragDropOnPackage(this, e))
            {
                if (canArrangeShapes)
                {
                    ArrangeShapes();
                }
                return;
            }

            if (e.Data.GetDataPresent(ServerExplorerHelper.DataSourceReferenceFormat))
            {
                if (ServerExplorerHelper.ContainsTable(e.Data))
                {
                    if (ServerExplorerHelper.ImportTables(ModelElement as Package,
                                                          new ServiceProvider(
                                                              (IServiceProvider)
                                                              ModelingPackage.GetGlobalService(typeof(DTE))),
                                                          e.Data))
                    {
                        if (canArrangeShapes)
                        {
                            ArrangeShapes();
                        }
                    }
                }
                if (ServerExplorerHelper.ContainsStoredProcedures(e.Data))
                {
                    if (ServerExplorerHelper.ImportStoredProcedures(ModelElement as Package,
                                                                    new ServiceProvider(
                                                                        (IServiceProvider)
                                                                        ModelingPackage.GetGlobalService(typeof(DTE))),
                                                                    e.Data))
                    {
                        if (canArrangeShapes)
                        {
                            ArrangeShapes();
                        }
                    }
                }
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Alerts listeners when the shape is dragged over its bounds.
        /// </summary>
        /// <param name="e">The diagram drag event arguments.</param>
        public override void OnDragOver(DiagramDragEventArgs e)
        {
            base.OnDragOver(e);

            if (DragDropHelper.OnDragOverPackage(this, e))
            {
                return;
            }

            if (e.Data.GetDataPresent(ServerExplorerHelper.DataSourceReferenceFormat))
            {
                if (ServerExplorerHelper.ContainsTable(e.Data) || ServerExplorerHelper.ContainsStoredProcedures(e.Data))
                {
                    e.Effect = DragDropEffects.Copy;
                }
            }
        }
Esempio n. 3
0
        /// <summary>
        /// OnDragDrop is used to create classes corresponding to the selection dragged
        /// from the Server Explorer
        /// </summary>
        private void OnDragDrop(object sender, DragEventArgs e)
        {
            // Check if the data present is in the DSRef format
            if (e.Data.GetDataPresent(DSRefNavigator.DataSourceReferenceFormat))
            {
                try
                {
                    // Create a navigator for the DSRef Consumer (and dispose it when finished)
                    using (DSRefNavigator navigator = new DSRefNavigator(e.Data.GetData(
                                                                             DSRefNavigator.DataSourceReferenceFormat)
                                                                         as Stream))
                    {
                        _output = new OutputWindowHelper(DTEHelper.GetDTE(this.Store));

                        // Get connection info of the connection of selected tables
                        string        providerType = null;
                        IDbConnection connection   = ServerExplorerHelper.GetConnection(navigator, out providerType);

                        IDbHelper helper;

                        switch (providerType)
                        {
                        case "System.Data.SqlClient.SqlConnection":
                            helper = new SqlHelper(connection);
                            break;

                        case "System.Data.OracleClient.OracleConnection":
                        case "Oracle.DataAccess.Client.OracleConnection":
                            Log("Selecting Oracle Helper for provider " + providerType);
                            helper = new OracleHelper(connection);
                            break;

                        case "MySql.Data.MySqlClient.MySqlConnection":
                            helper = new MySqlHelper(connection);
                            break;

                        default:
                            // TODO: Support other databases with native providers.
                            Log(
                                string.Format(
                                    @"Failed: ActiveWriter does not support model generation through {0}. Supported providers: System.Data.SqlClient.SqlConnection, System.Data.OracleClient.OracleConnection, Oracle.DataAccess.Client.OracleConnection, MySql.Data.MySqlClient.MySqlConnection. You can help us improve this functionality, though. See http://www.castleproject.org/others/contrib/index.html to access ActiveWriter source code under the contrib repository, and check Dsl\ServerExplorerSupport\IDbHelper.cs for the start.",
                                    providerType));
                            return;
                        }

                        // Get the root element where we'll add the classes
                        Model model = Helper.GetModel(this.Store);
                        if (model == null)
                        {
                            Log("Failed: Cannot get the model for the store.");
                            return;
                        }

                        _manager = new DiagramManager(this.Store, model);
                        _manager.OutputWindow = _output;

                        // Create a transaction to add the clases.
                        using (Transaction txAdd =
                                   model.Store.TransactionManager.BeginTransaction("Add classes"))
                        {
                            List <DSRefNode> tableList = new List <DSRefNode>();
                            // Get the tables from the Server Explorer selection
                            // We'll iterate this list twice to use nodes' list to
                            // determine if we have to generate relations for each
                            // table or not.
                            foreach (DSRefNode node in navigator.ChildTableNodes)
                            {
                                tableList.Add(node);
                            }

                            _manager.Tables = tableList;

                            _relations = new List <Relation>();

                            foreach (DSRefNode node in tableList)
                            {
                                // Create the table and add it to the model
                                ModelClass cls = _manager.NewClass(node.Owner, node.Name);
                                PopulateClass(cls, connection, helper);
                                _manager.AssignModel(cls);
                            }

                            // Create relations
                            if (_relations != null && _relations.Count > 0)
                            {
                                HandleRelations();
                            }

                            // Commit the transaction and add tables to the model
                            txAdd.Commit();
                        }

                        // TODO: Auto layout doesn't work well. Will check with future versions of DSL tools.
                        // this.AutoLayoutShapeElements(this.NestedChildShapes);
                    }
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex.Message);
                }
                finally
                {
                    _manager   = null;
                    _relations = null;
                    _output    = null;
                }
            }
        }