nHydrate.Dsl.nHydrateDiagram.OnChildConfiguring C# (CSharp) Method

OnChildConfiguring() protected method

protected OnChildConfiguring ( Microsoft child, bool createdDuringViewFixup ) : void
child Microsoft
createdDuringViewFixup bool
return void
        protected override void OnChildConfiguring(Microsoft.VisualStudio.Modeling.Diagrams.ShapeElement child, bool createdDuringViewFixup)
        {
            base.OnChildConfiguring(child, createdDuringViewFixup);

            try
            {
                if (!this.IsLoading)
                {
                    //Add a default field to entities
                    if (child.ModelElement is Entity)
                    {
                        var item = child.ModelElement as Entity;
                        var model = item.nHydrateModel;
                        if (item.Fields.Count == 0)
                        {
                            var field = new Field(item.Partition)
                                            {
                                                DataType = DataTypeConstants.Int,
                                                Identity = IdentityTypeConstants.Database,
                                                Name = "ID",
                                            };
                            item.Fields.Add(field); //Add then set PK so it will trigger index code
                            field.IsPrimaryKey = true;
                        }

                        #region Pasting
                        //If there are invalid indexes then try to remap them
                        foreach (var index in item.Indexes.Where(x => x.FieldList.Any(z => z == null)))
                        {
                            foreach (var c in index.IndexColumns.Where(x => x.Field == null && x.FieldID != Guid.Empty))
                            {
                                var f = model.Entities.SelectMany(x => x.Fields).FirstOrDefault(x => x.Id == c.FieldID);
                                if (f != null)
                                {
                                    var f2 = item.Fields.FirstOrDefault(x => x.Name == f.Name);
                                    if (f2 != null)
                                        c.FieldID = f2.Id;
                                }
                            }
                        }

                        //Add a PK index if not one
                        if (!item.Indexes.Any(x => x.IndexType == IndexTypeConstants.PrimaryKey) && item.PrimaryKeyFields.Count > 0)
                        {
                            var index = new Index(item.Partition) { IndexType = IndexTypeConstants.PrimaryKey };
                            item.Indexes.Add(index);
                            var loop = 0;
                            foreach (var field in item.PrimaryKeyFields)
                            {
                                var newIndexColumn = new IndexColumn(item.Partition);
                                index.IndexColumns.Add(newIndexColumn);
                                newIndexColumn.FieldID = field.Id;
                                newIndexColumn.SortOrder = loop;
                                loop++;
                            }
                        }
                        #endregion

                    }
                    else if (child.ModelElement is View)
                    {
                        var item = child.ModelElement as View;
                        //if ((item.nHydrateModel.DiagramVisibility & VisibilityTypeConstants.View) != VisibilityTypeConstants.View)
                        //{
                        //  if (MessageBox.Show("This type of object cannot be created by dragging onto the diagram because it is not visualized on the diagram. You can change the 'DiagramVisibility' property of the model to visualize it. Otherwise you can only create this object type by using the model tree window.\n\nWould you like to toggle on visualization for this object?", "Information", MessageBoxButtons.YesNo, MessageBoxIcon.Information) != DialogResult.Yes)
                        //  {
                        //    child.Delete();
                        //    return;
                        //  }
                        //  item.nHydrateModel.DiagramVisibility = (item.nHydrateModel.DiagramVisibility | VisibilityTypeConstants.View);
                        //}

                        if (item.Fields.Count == 0)
                        {
                            var field = new ViewField(item.Partition)
                                            {
                                                DataType = DataTypeConstants.Int,
                                                Name = "Field1",
                                            };
                            item.Fields.Add(field);
                        }
                    }
                    else if (child.ModelElement is StoredProcedure)
                    {
                        var item = child.ModelElement as StoredProcedure;
                        //if ((item.nHydrateModel.DiagramVisibility & VisibilityTypeConstants.StoredProcedure) != VisibilityTypeConstants.StoredProcedure)
                        //{
                        //  if (MessageBox.Show("This type of object cannot be created by dragging onto the diagram because it is not visualized on the diagram. You can change the 'DiagramVisibility' property of the model to visualize it. Otherwise you can only create this object type by using the model tree window.\n\nWould you like to toggle on visualization for this object?", "Information", MessageBoxButtons.YesNo, MessageBoxIcon.Information) != DialogResult.Yes)
                        //  {
                        //    child.Delete();
                        //    return;
                        //  }
                        //  item.nHydrateModel.DiagramVisibility = (item.nHydrateModel.DiagramVisibility | VisibilityTypeConstants.StoredProcedure);
                        //}

                        if (item.Fields.Count == 0)
                        {
                            var field = new StoredProcedureField(item.Partition)
                                            {
                                                DataType = DataTypeConstants.Int,
                                                Name = "Field1",
                                            };
                            item.Fields.Add(field);
                        }
                        if (item.Parameters.Count == 0)
                        {
                            var field = new StoredProcedureParameter(item.Partition)
                                            {
                                                DataType = DataTypeConstants.Int,
                                                Name = "Parameter1",
                                            };
                            item.Parameters.Add(field);
                        }
                    }
                    else if (child.ModelElement is Function)
                    {
                        var item = child.ModelElement as Function;
                        //if ((item.nHydrateModel.DiagramVisibility & VisibilityTypeConstants.Function) != VisibilityTypeConstants.Function)
                        //{
                        //  if (MessageBox.Show("This type of object cannot be created by dragging onto the diagram because it is not visualized on the diagram. You can change the 'DiagramVisibility' property of the model to visualize it. Otherwise you can only create this object type by using the model tree window.\n\nWould you like to toggle on visualization for this object?", "Information", MessageBoxButtons.YesNo, MessageBoxIcon.Information) != DialogResult.Yes)
                        //  {
                        //    child.Delete();
                        //    return;
                        //  }
                        //  item.nHydrateModel.DiagramVisibility = (item.nHydrateModel.DiagramVisibility | VisibilityTypeConstants.Function);
                        //}

                        if (item.Fields.Count == 0)
                        {
                            var field = new FunctionField(item.Partition)
                                            {
                                                DataType = DataTypeConstants.Int,
                                                Name = "Field1",
                                            };
                            item.Fields.Add(field);
                        }
                        if (item.Parameters.Count == 0)
                        {
                            var field = new FunctionParameter(item.Partition)
                                            {
                                                DataType = DataTypeConstants.Int,
                                                Name = "Parameter1",
                                            };
                            item.Parameters.Add(field);
                        }
                    }

                    this.OnShapeConfiguring(new ModelElementEventArgs() { Shape = child });
                }

            }
            catch (Exception ex)
            {
                throw;
            }
        }