public void addNewTable(string name, DatabaseConnectionContext context, ISchemaProvider schemaProvider)
 {
     //TODO: improve way of assing last parameter
     TableModel tableModel  = new TableModel (name,context,schemaProvider,true);
     TableFigure tableFigure = new TableFigure (tableModel,canvas,diagram);
     tableModel.FigureOwner = tableFigure;
     view.Drawing.Add (tableFigure);
     System.Console.WriteLine("added table");
     foreach ( IFigure fig in view.Drawing.FiguresEnumerator){
         System.Console.WriteLine("figura"+fig.ToString());
     }
     diagram.AddTable(tableFigure);
 }
 //This functions works as dispatcher to correct one functions
 //TODO: improve this implementation of data interchange to propagate changes
 public void Update(bool refresh, bool changeConnection, TableFigure notifier, kindOptionality optionality, bool changeDatatype, ColumnSchema notifierColumn)
 {
     if (refresh) {
         Console.WriteLine ("La tabla " + this.Model.Name + " ya fue Avisada de REFRESCAR!!!!!!!!!!!!!!!!!!!!!!!!!!! de la tabla:" + notifier.Model.Name);
         refreshForeignKeys (notifier, optionality);
     } else if (changeConnection)
         Console.WriteLine ("La tabla " + this.Model.Name + " ya fue Avisada de CAMBIO DE CONEXION!!!!!!!!!!!!!!!!!!!!!!!!!!! de la tabla:" + notifier.Model.Name); else if (changeDatatype) {
         changeFkColumnDatatype (notifierColumn);
         Console.WriteLine ("La tabla " + this.Model.Name + " ya fue Avisada de Cambio de tipo de dato en columna !!!!!!!!!!!!!!!!!!!!!!!!!!!" + notifierColumn.Name);
     } else if (!changeConnection && !refresh && !changeDatatype) {
         Console.WriteLine ("La tabla " + this.Model.Name + " ya fue Avisada de ELIMINAR FK !!!!!!!!!!!!!!!!!!!!!!!!!!! de la tabla:" + notifier.Model.Name);
         removeAllForeignKeysFrom(notifier);
     }
 }
 public void removeAllForeignKeysFrom(TableFigure sourceFK)
 {
     ForeignKeyConstraintSchema deleteFk=null;
     foreach(ForeignKeyConstraintSchema fkc in Model.TableSchema.Constraints){
         if(fkc.ReferenceTableName==sourceFK.Model.TableSchema.Name){
             deleteFk=fkc;
         }
     }
     if(deleteFk!=null){
         Model.TableSchema.Constraints.Remove(deleteFk);
         //Remove columns metadata from table
         foreach(ColumnSchema colfk in deleteFk.Columns){
             Model.TableSchema.Columns.Remove(colfk);
         }
         List<ColumnFkFigure> deleteFigures = new List<ColumnFkFigure>();
         foreach (AbstractColumnFigure f in Model.columns){
             if(f is ColumnFkFigure && (f as ColumnFkFigure).originalTableName==sourceFK.Model.Name){
                 deleteFigures.Add(f as ColumnFkFigure);
             }
         }
         foreach (ColumnFkFigure f in deleteFigures){
             Model.columns.Remove(f);
             this.Remove(f);
         }
     }
     //TODO: Add multiple fk between two tables remove functionality when needed
 }
        public void RefreshRelationships(bool refresh, bool changeConnection, TableFigure notifier, kindOptionality optionality, bool changeDatatype, ColumnSchema notifierColumn)
        {
            if (refresh && changeConnection)
                throw new NotImplementedException ();

            if (NotifyChanged != null) {
                NotifyChanged (refresh, changeConnection, notifier, optionality, changeDatatype, notifierColumn);
            }
        }
        public void refreshForeignKeys(TableFigure sourceFk, kindOptionality optionality)
        {
            PrimaryKeyConstraintSchema fkConsColumns = null;
            //Lookup for pk at table level at reference table
            foreach (ConstraintSchema cs in sourceFk.Model.TableSchema.Constraints) {
                if (cs is PrimaryKeyConstraintSchema) {
                    fkConsColumns = cs as PrimaryKeyConstraintSchema;
                    break;
                }
            }

            //Lookup for pk at column level at reference table
            if (fkConsColumns == null) {
                foreach (ColumnSchema col in sourceFk.Model.TableSchema.Columns) {
                    fkConsColumns = col.Constraints.GetConstraint (ConstraintType.PrimaryKey) as PrimaryKeyConstraintSchema;
                    if (fkConsColumns != null)
                        break;
                }
            }

            //Add new fk(s) column to table
            if (fkConsColumns != null) {
                foreach (ColumnSchema colfk in fkConsColumns.Columns) {
                    bool exists = false;
                    Console.WriteLine ("comienzo a buscar :" + colfk.Parent.Name + "." + colfk.Name);
                    foreach (AbstractColumnFigure cf in Model.columns) {
                        if (cf is ColumnFkFigure) {
                            ColumnFkFigure cfk = cf as ColumnFkFigure;
                            Console.WriteLine ("\t\tNO JODA 666 COMPARO:" + cfk.originalTableName + "." + cfk.originalColumnName + " CON " + colfk.Parent.Name + "." + colfk.Name);
                            if (cfk.sameForeignKey (colfk.Parent.Name, colfk.Name)) {
                                exists = true;
                                Console.WriteLine (" \t\t!!!!!!!!! MATCHES: " + colfk.Name);
                            }
                        }
                    }
                    if (!exists) {
                        this.AddFkConstraintColumn (colfk, optionality);
                    }
                }
            }

            //Remove not existing fk(s) to table
            bool removeFk = true;
            ColumnFkFigure removeCfk = null;
            ColumnFkFigure colFigFk = null;
            foreach (AbstractColumnFigure cf in Model.columns) {
                if (cf is ColumnFkFigure) {
                    Console.WriteLine ("Busco si elimino a: " + cf.ColumnModel.Name);
                    colFigFk = cf as ColumnFkFigure;
                    removeFk = true;
                    if (fkConsColumns != null) {
                        foreach (ColumnSchema colfk in fkConsColumns.Columns) {
                            Console.WriteLine ("\t\tComparo con: " + colfk.Name);
                            if (colFigFk.sameForeignKey (colfk.Parent.Name, colfk.Name)) {
                                removeFk = false;
                                Console.WriteLine ("\t\t\tNo la debo remover tiene columna en el fkconstraint " + colfk.Name);
                            }
                        }
                    }
                    if (removeFk) {
                        removeCfk = colFigFk;
                        Console.WriteLine ("PORQ ENTRO AQUI CON: " + colFigFk.originalColumnName + "   " + colFigFk.ColumnModel.Name);
                    }
                }
            }

            if (removeCfk != null) {
                Console.WriteLine ("Mando a eliminar " + removeCfk.originalTableName + "." + removeCfk.originalColumnName);
                this.removeFkConstraintColumn (removeCfk);
            }
        }
        //TODO erase writes, redo with STARFIGURE and ENDFIGURE
        private void ConnectionChangedHandler(object sender, EventArgs args)
        {
            TableFigure oldStart=null, oldEnd=null;
            //Get Start Figure
            if(StartConnector!=null){ //TODO: needed StartConnector.Owner is TableFigure?
                oldStart = figStart;
                if(StartConnector.Owner!=null)
                    figStart = StartConnector.Owner as TableFigure;
                else
                    figStart = null;
                System.Console.WriteLine("Star Figure"+StartConnector.Owner.GetType());
            }
            //Get End Figure
            if(EndConnector!=null){
                oldEnd = figEnd;
                if(EndConnector.Owner!=null)
                    figEnd = EndConnector.Owner as TableFigure;
                else
                    figEnd = null;
                System.Console.WriteLine("End Figure"+EndConnector.Owner.GetType());
            }

            //Notify registration of a tablefigure as user of this tablefigure as foreignkey
            if(figStart!=null && figEnd!=null){
                if(oldStart!=figStart){
                    if(oldStart!=null)
                        figStart.RemoveObserver(oldStart);
                    figStart.AddObserver(figEnd);
                }

                if(oldEnd!=figEnd){
                    //if oldEnd!=null what to do?
                    figEnd.addFkConstraint(this, optionality);
                }
                figStart.RefreshRelationships(false,true,figStart,optionality,false,null);

            }else{
                figStart=oldStart;
                figEnd=oldEnd;
            }
        }
 public void addTable(string name, DatabaseConnectionContext context, ISchemaProvider schemaProvider, bool create)
 {
     TableModel tableModel = new TableModel(name,context,schemaProvider,false);
     TableFigure tableFigure = new TableFigure (tableModel,canvas,diagram);
     tableModel.FigureOwner=tableFigure;
     view.Drawing.Add (tableFigure);
     System.Console.WriteLine("added table22222");
     foreach ( IFigure fig in view.Drawing.FiguresEnumerator){
         System.Console.WriteLine("figura"+fig.ToString());
     }
     diagram.AddTable(tableFigure);
 }