protected Relationship GetParentChildrenRelationship(string relationship, string entity, string childEntity) { Relationship oRelationship = new Relationship(relationship, entity, childEntity, Schema); if (oRelationship.DirectRelationships.Length == 1) { return(new OneToManyRelationship(entity, childEntity, oRelationship.DirectRelationships)); } if (oRelationship.DirectRelationships.Length == 2) { string intermediary = oRelationship.DirectRelationships[0].RelatedEntity; XElement xRelationship = Schema.Elements(SchemaVocab.Relationship).FirstOrDefault(r => r.Attribute(SchemaVocab.Type).Value == SchemaVocab.ManyToMany && r.Attribute(SchemaVocab.Name) != null && r.Attribute(SchemaVocab.Name).Value == relationship && r.Elements(SchemaVocab.Relationship).Count() == 2); if (xRelationship == null) { bool firstIsMatch = IsMatch(SchemaVocab.OneToMany, entity, intermediary, oRelationship.DirectRelationships[0].ToString()); if (!firstIsMatch) { firstIsMatch = IsMatch(SchemaVocab.ManyToOne, intermediary, entity, oRelationship.DirectRelationships[0].Reverse().ToString()); } if (!firstIsMatch) { return(null); } // bool secondIsMatch = IsMatch(SchemaVocab.ManyToOne, intermediary, childEntity, oRelationship.DirectRelationships[1].ToString()); if (!secondIsMatch) { secondIsMatch = IsMatch(SchemaVocab.OneToMany, entity, intermediary, oRelationship.DirectRelationships[1].Reverse().ToString()); } if (!secondIsMatch) { return(null); } } OneToManyRelationship oneToManyRelationship = new OneToManyRelationship(entity, intermediary, new List <DirectRelationship> { oRelationship.DirectRelationships[0] }); ManyToOneRelationship manyToOneRelationship = new ManyToOneRelationship(intermediary, childEntity, new List <DirectRelationship> { oRelationship.DirectRelationships[1] }); return(new ManyToManyRelationship(oneToManyRelationship, manyToOneRelationship)); } return(null); }
private void buttonOk_Click(object sender, EventArgs e) { for (int i = this.Controls.Count - 1; i >= 0; i--) { Control control = this.Controls[i]; control.Focus(); // First tab tabStripRelationship.SelectedIndex = 0; if (!ValidateControl(control)) { DialogResult = DialogResult.None; return; } // Second tab if (radioButtonOneToOne.Checked || radioButtonOneToMany.Checked || radioButtonManyToOne.Checked) { tabStripRelationship.SelectedIndex = 1; if (!ValidateControl(control)) { DialogResult = DialogResult.None; return; } } } Column[] primaryColumns = new Column[listViewPrimaryColumn.Items.Count]; for (int i = 0; i < listViewPrimaryColumn.Items.Count; i++) { ListViewItem listViewItem = listViewPrimaryColumn.Items[i]; primaryColumns[i] = (Column)listViewItem.Tag; } Column[] foreignColumns = new Column[listViewForeignColumn.Items.Count]; for (int i = 0; i < listViewForeignColumn.Items.Count; i++) { ListViewItem listViewItem = listViewForeignColumn.Items[i]; foreignColumns[i] = (Column)listViewItem.Tag; } if (_primaryRelationship == null) { ScriptObject primaryScriptObject = (ScriptObject)comboBoxPrimaryScriptObject.SelectedItem; ScriptObject foreignScriptObject = (ScriptObject)comboBoxForeignScriptObject.SelectedItem; Filter primaryFilter = (Filter)comboBoxPrimaryFilter.SelectedItem; Filter foreignFilter = (Filter)comboBoxForeignFilter.SelectedItem; if (radioButtonOneToOne.Checked) { _primaryRelationship = new OneToOneRelationship(textBoxName.Text, true, _parent, primaryColumns, foreignScriptObject, foreignColumns, primaryFilter, checkBoxIsBase.Checked); _primaryRelationship.Alias = textBoxAlias.Text; _primaryRelationship.Description = textBoxDescription.Text; _foreignRelationship = new OneToOneRelationship(primaryScriptObject.Alias, true, foreignScriptObject, foreignColumns, _parent, primaryColumns, foreignFilter, !checkBoxIsBase.Checked); _foreignRelationship.Alias = primaryScriptObject.Alias; } if (radioButtonOneToMany.Checked) { _primaryRelationship = new OneToManyRelationship(textBoxName.Text, true, _parent, primaryColumns, foreignScriptObject, foreignColumns, primaryFilter); _primaryRelationship.Alias = textBoxAlias.Text; _primaryRelationship.Description = textBoxDescription.Text; _foreignRelationship = new ManyToOneRelationship(primaryScriptObject.Alias, true, foreignScriptObject, foreignColumns, _parent, primaryColumns, foreignFilter); _foreignRelationship.Alias = primaryScriptObject.Alias; } if (radioButtonManyToOne.Checked) { _primaryRelationship = new ManyToOneRelationship(textBoxName.Text, true, _parent, primaryColumns, foreignScriptObject, foreignColumns, primaryFilter); _primaryRelationship.Alias = textBoxAlias.Text; _primaryRelationship.Description = textBoxDescription.Text; _foreignRelationship = new OneToManyRelationship(primaryScriptObject.AliasPlural, true, foreignScriptObject, foreignColumns, _parent, primaryColumns, foreignFilter); _foreignRelationship.Alias = primaryScriptObject.AliasPlural; } if (radioButtonManyToMany.Checked) { OneToManyRelationship oneToManyRelationship = (OneToManyRelationship)comboBoxIntermediatePrimaryRelationship.SelectedItem; ManyToOneRelationship manyToOneRelationship = (ManyToOneRelationship)comboBoxIntermediateForeignRelationship.SelectedItem; _primaryRelationship = new ManyToManyRelationship(textBoxName.Text, true, oneToManyRelationship, manyToOneRelationship, oneToManyRelationship.Filter); _primaryRelationship.Alias = textBoxAlias.Text; _primaryRelationship.Description = textBoxDescription.Text; _foreignRelationship = new ManyToManyRelationship(oneToManyRelationship.Parent.AliasPlural, true, (OneToManyRelationship)manyToOneRelationship.ForeignRelationship, (ManyToOneRelationship)oneToManyRelationship.ForeignRelationship, manyToOneRelationship.ForeignRelationship.Filter); _foreignRelationship.Alias = oneToManyRelationship.Parent.AliasPlural; } _primaryRelationship.ForeignRelationship = _foreignRelationship; _foreignRelationship.ForeignRelationship = _primaryRelationship; } else { _primaryRelationship.Name = textBoxName.Text; _primaryRelationship.Alias = textBoxAlias.Text; _primaryRelationship.Description = textBoxDescription.Text; _primaryRelationship.PrimaryColumns = primaryColumns; _primaryRelationship.ForeignScriptObject = (ScriptObject)comboBoxForeignScriptObject.SelectedItem; _primaryRelationship.ForeignColumns = foreignColumns; _primaryRelationship.Filter = (Filter)comboBoxPrimaryFilter.SelectedItem; _primaryRelationship.ForeignRelationship.Filter = (Filter)comboBoxForeignFilter.SelectedItem; if (_primaryRelationship.GetType() == typeof(ManyToManyRelationship)) { ManyToManyRelationship manyToManyRelationship = (ManyToManyRelationship)_primaryRelationship; manyToManyRelationship.IntermediatePrimaryRelationship = (OneToManyRelationship)comboBoxIntermediatePrimaryRelationship.SelectedItem; manyToManyRelationship.IntermediateForeignRelationship = (ManyToOneRelationship)comboBoxIntermediateForeignRelationship.SelectedItem; } } }
private void InitialUpdateRelationships(IList <Model.Table> tables) { foreach (Model.Table table in tables) { foreach (Key key in table.Keys) { // Look for one to one relationship if (key.Type != DatabaseConstant.KeyType.Foreign) { continue; } if (IsOneToOneRelationship(key)) { Filter filter = Search.GetFilter(table.Filters, new Helper(DalAssemblyName).GetPrimaryKey((Model.Table)key.Parent).Name); OneToOneRelationship oneToOneRelationship = new OneToOneRelationship("One_" + key.Name, false, table, key.Columns, key.ReferencedTable, key.ReferencedKey.Columns, filter, false); table.AddRelationship(oneToOneRelationship); // Back other way Filter filter2 = Search.GetFilter(key.ReferencedKey.Parent.Filters, new Helper(DalAssemblyName).GetPrimaryKey((Model.Table)key.ReferencedKey.Parent).Name); OneToOneRelationship oneToOneRelationship2 = new OneToOneRelationship("One_" + key.Name, false, key.ReferencedTable, key.ReferencedKey.Columns, table, key.Columns, filter2, true); key.ReferencedTable.AddRelationship(oneToOneRelationship2); oneToOneRelationship.ForeignRelationship = oneToOneRelationship2; oneToOneRelationship2.ForeignRelationship = oneToOneRelationship; } else { Filter filter = Search.GetFilter(table.Filters, key.Name); ManyToOneRelationship manyToOneRelationship = new ManyToOneRelationship("One_" + key.Name, false, table, key.Columns, key.ReferencedTable, key.ReferencedKey.Columns, filter); table.AddRelationship(manyToOneRelationship); // Back other way Filter filter2 = Search.GetFilter(key.ReferencedTable.Filters, key.ReferencedKey.Name); OneToManyRelationship oneToManyRelationship = new OneToManyRelationship("Many_" + key.Name, false, key.ReferencedTable, key.ReferencedKey.Columns, table, key.Columns, filter2); key.ReferencedTable.AddRelationship(oneToManyRelationship); manyToOneRelationship.ForeignRelationship = oneToManyRelationship; oneToManyRelationship.ForeignRelationship = manyToOneRelationship; } } } // Temp list to search for foreign relationship List <Relationship> manyToManyRelationships = new List <Relationship>(); foreach (Model.Table table in tables) { foreach (ManyToOneRelationship manyToOneRelationship in table.ManyToOneRelationships) { string path = manyToOneRelationship.ForeignScriptObject.Name + " -> " + GetColumnNameList(manyToOneRelationship.ForeignColumns) + " -> " + manyToOneRelationship.PrimaryScriptObject.Name + " -> " + GetColumnNameList(manyToOneRelationship.PrimaryColumns); foreach (ManyToOneRelationship manyToOneRelationship2 in table.ManyToOneRelationships) { if (manyToOneRelationship2.Name == manyToOneRelationship.Name) { continue; } path += " -> " + manyToOneRelationship2.PrimaryScriptObject.Name + " -> " + GetColumnNameList(manyToOneRelationship2.PrimaryColumns) + " -> " + manyToOneRelationship2.ForeignScriptObject.Name + " -> " + GetColumnNameList(manyToOneRelationship2.ForeignColumns); ManyToManyRelationship manyToManyRelationship = new ManyToManyRelationship("Many_" + manyToOneRelationship.Name + "_" + manyToOneRelationship2.Name, false, (OneToManyRelationship)manyToOneRelationship.ForeignRelationship, manyToOneRelationship2, manyToOneRelationship2.ForeignRelationship.Filter); manyToOneRelationship.ForeignScriptObject.AddRelationship(manyToManyRelationship); manyToManyRelationships.Add(manyToManyRelationship); } } } #region Fill foreign relationships foreach (Model.Table table in tables) { foreach (ManyToManyRelationship manyToManyRelationship in table.ManyToManyRelationships) { string foreignRelationshipName = "Many_" + manyToManyRelationship.IntermediatePrimaryRelationship.ForeignRelationship.Name + "_" + manyToManyRelationship.IntermediateForeignRelationship.Name; ManyToManyRelationship foreignRelationship = (ManyToManyRelationship)Search.GetRelationship(manyToManyRelationships.ToArray(), foreignRelationshipName); manyToManyRelationship.ForeignRelationship = foreignRelationship; } } #endregion }