private Task DetermineRelationshipType(List <RelationshipImportItem> listNew, List <TableDrivedEntityDTO> entities) { return(Task.Run(() => { BizTableDrivedEntity bizTableDrivedEntity = new BizTableDrivedEntity(); ModelDataHelper dataHelper = new ModelDataHelper(); Dictionary <string, string> isaKeys = new Dictionary <string, string>(); foreach (var item in listNew) { var pkEntity = entities.First(x => x.Name == item.Relationship.Entity1); var fkEntity = entities.First(x => x.Name == item.Relationship.Entity2); item.Relationship.EntityID1 = pkEntity.ID; item.Relationship.EntityID2 = fkEntity.ID; item.Relationship.Entity1 = pkEntity.Name; item.Relationship.Entity2 = fkEntity.Name; item.Relationship.DatabaseID1 = pkEntity.DatabaseID; item.Relationship.DatabaseID2 = fkEntity.DatabaseID; foreach (var relcol in item.Relationship.RelationshipColumns) { var col1 = pkEntity.Columns.First(x => x.Name == relcol.FirstSideColumn.Name); relcol.FirstSideColumnID = col1.ID; relcol.FirstSideColumn = col1; var col2 = fkEntity.Columns.First(x => x.Name == relcol.SecondSideColumn.Name); relcol.SecondSideColumnID = col2.ID; relcol.SecondSideColumn = col2; } //item.Relationship.RelatesOnPrimaryKeys = bizRelationship.RelatesOnPrimaryKeys(item.Relationship, new List<TableDrivedEntityDTO>() { pkEntity, fkEntity }); //item.Relationship.FKCoumnIsNullable = item.Relationship.RelationshipColumns.Any(x => x.SecondSideColumn.IsNull); InfoUpdated(this, new ItemImportingStartedArg() { CurrentProgress = listNew.IndexOf(item) + 1, TotalProgressCount = listNew.Count, ItemName = "Determinig Relationship Type" + " " + item.Relationship.Name }); item.Relationship.RelationInfo = dataHelper.GetRelationshipsInfo(item.Relationship, pkEntity, fkEntity); //if (relationInfo.FKRelatesOnPrimaryKey) //{ // //var subEntity = bizTableDrivedEntity.GetTableDrivedEntity(relationship.EntityID2, EntityColumnInfoType.WithSimpleColumns, EntityRelationshipInfoType.WithoutRelationships); // //if (isaRelationship == null) // //{ // // isaRelationship = new Tuple<TableDrivedEntityDTO, List<Tuple<RelationshipDTO, TableDrivedEntityDTO>>>(entity, new List<Tuple<RelationshipDTO, TableDrivedEntityDTO>>() { new Tuple<RelationshipDTO, TableDrivedEntityDTO>(relationship, subEntity) }); // //} // //else // // isaRelationship.Item2.Add(new Tuple<RelationshipDTO, TableDrivedEntityDTO>(relationship, subEntity)); // item.Relationship.TypeEnum = Enum_RelationshipType.SuperToSub; //} //else //{ Enum_OrginalRelationshipType originalType = Enum_OrginalRelationshipType.None; if (item.Relationship.FKSidePKColumnsAreFkColumns) { originalType = Enum_OrginalRelationshipType.SuperToSub; } else { if (Database.DBHasData) { if (item.Relationship.RelationInfo.MoreThanOneFkForEachPK == false) { originalType = Enum_OrginalRelationshipType.OneToOne; } else { originalType = Enum_OrginalRelationshipType.OneToMany; } } else { originalType = Enum_OrginalRelationshipType.OneToMany; } } if (originalType == Enum_OrginalRelationshipType.SuperToSub) { item.Relationship.OrginalTypeEnum = Enum_OrginalRelationshipType.SuperToSub; if (isaKeys.ContainsKey(item.Relationship.Entity1)) { item.Relationship.OrginalRelationshipGroup = isaKeys[item.Relationship.Entity1]; } else { var key = "ISA_OnPK_" + item.Relationship.Entity1; isaKeys.Add(item.Relationship.Entity1, key); item.Relationship.OrginalRelationshipGroup = key; } } else if (originalType == Enum_OrginalRelationshipType.OneToOne) { bool otoCondition = false; if (item.Relationship.RelationInfo.FKHasData == true) { //این شرط جدید اضافه شد. برای اینکه اگر کلید خارجی جزو پرایمری ها باشد معلوم است که ارتباط چند به چند یا مالکیت منظور بوده است if (!item.Relationship.RelationshipColumns.Any(x => x.SecondSideColumn.PrimaryKey)) { long relcount = dataHelper.GetRelationDataCount(fkEntity, item.Relationship); if (relcount > 10) { long fkcount = dataHelper.GetDataCount(fkEntity); long pkcount = dataHelper.GetDataCount(pkEntity); long biggerCount = 0; long smallerCount = 0; if (fkcount >= pkcount) { biggerCount = fkcount; smallerCount = pkcount; } else { biggerCount = pkcount; smallerCount = fkcount; } //if (pkcount > relcount) //{ if (biggerCount > 50000) { if (relcount >= biggerCount / 10) { otoCondition = true; } } else if (pkcount > 10000) { if (relcount >= biggerCount / 5) { otoCondition = true; } } else if (pkcount > 2000) { if (relcount >= biggerCount / 2.5) { otoCondition = true; } } else { if (relcount >= biggerCount / 1.5) { otoCondition = true; } } } //} //else //{ // if(relcount>10) // otoCondition = true; //} } } if (otoCondition) { item.Relationship.OrginalTypeEnum = Enum_OrginalRelationshipType.OneToOne; } else { item.Relationship.OrginalTypeEnum = Enum_OrginalRelationshipType.OneToMany; } } else { item.Relationship.OrginalTypeEnum = Enum_OrginalRelationshipType.OneToMany; } //} } })); }
//DatabaseDTO Database { set; get; } public void ConvertRelationship(RelationshipDTO relationship, Enum_RelationshipType targetRaltionshipType) { ModelDataHelper dataHelper = new ModelDataHelper(); var relationshinInfo = dataHelper.GetRelationshipsInfoWithEntityIds(MyProjectManager.GetMyProjectManager.GetRequester(), relationship); if (relationship.TypeEnum == Enum_RelationshipType.OneToMany || targetRaltionshipType == Enum_RelationshipType.ManyToOne) { if (relationshinInfo.FKHasData == true && relationshinInfo.MoreThanOneFkForEachPK == true) { throw new Exception("بعلت وجود ارتباط یک به چند بین داده های دو جدول امکان تبدیل وجود ندارد"); } } if (targetRaltionshipType == Enum_RelationshipType.OneToMany || targetRaltionshipType == Enum_RelationshipType.ManyToOne || targetRaltionshipType == Enum_RelationshipType.ImplicitOneToOne || targetRaltionshipType == Enum_RelationshipType.ExplicitOneToOne ) { bizRelationship.ConvertRelationship(MyProjectManager.GetMyProjectManager.GetRequester(), relationship, targetRaltionshipType); } else if (targetRaltionshipType == Enum_RelationshipType.SuperToSub) { // OneToMany , ImplicitOneToOne , UnionToSubUnion_SubUnionHoldsKeys , SubUnionToUnion_UnionHoldsKeys var isaRelationships = bizISARelationship.GetISARelationshipsByEntityID(relationship.EntityID1); ISARelationshipCreateOrSelect(isaRelationships, relationship, targetRaltionshipType); } else if (targetRaltionshipType == Enum_RelationshipType.SubToSuper) { // ManyToOne , ExplicitOneToOne , UnionToSubUnion_UnionHoldsKeys , SubUnionToUnion_SubUnionHoldsKeys var isaRelationships = bizISARelationship.GetISARelationshipsByEntityID(relationship.EntityID2); ISARelationshipCreateOrSelect(isaRelationships, relationship, targetRaltionshipType); } else if (targetRaltionshipType == Enum_RelationshipType.SubUnionToUnion) { // OneToMany , ImplicitOneToOne , SuperToSub , UnionToSubUnion_SubUnionHoldsKeys var unionRelationships = bizUnionRelationship.GetUnionRelationshipsBySuperUnionEntity(relationship.EntityID2, true); UnionRelationshipCreateOrSelect(unionRelationships, true, relationship, targetRaltionshipType); } //else if (targetRaltionshipType == Enum_RelationshipType.SubUnionToUnion_SubUnionHoldsKeys) //{ // // ManyToOne , ExplicitOneToOne , SuperToSub , UnionToSubUnion_UnionHoldsKeys // var unionRelationships = bizUnionRelationship.GetUnionRelationshipsBySuperUnionEntity(relationship.EntityID2, false); // UnionRelationshipCreateOrSelect(unionRelationships, false, relationship, targetRaltionshipType); //} //else if (targetRaltionshipType == Enum_RelationshipType.UnionToSubUnion_SubUnionHoldsKeys) //{ // // OneToMany , ImplicitOneToOne , SuperToSub , SubUnionToUnion_UnionHoldsKeys // var unionRelationships = bizUnionRelationship.GetUnionRelationshipsBySuperUnionEntity(relationship.EntityID1, true); // UnionRelationshipCreateOrSelect(unionRelationships, true, relationship, targetRaltionshipType); //} else if (targetRaltionshipType == Enum_RelationshipType.UnionToSubUnion) { // ManyToOne , ExplicitOneToOne , SuperToSub , SubUnionToUnion_SubUnionHoldsKeys var unionRelationships = bizUnionRelationship.GetUnionRelationshipsBySuperUnionEntity(relationship.EntityID1, false); UnionRelationshipCreateOrSelect(unionRelationships, false, relationship, targetRaltionshipType); } //else if (relationship.TypeEnum == Enum_RelationshipType.ManyToOne) //{ // if (bizRelationship.RelationshipHasManyData(relationship)) // { // throw new Exception("بعلت وجود ارتباط یک به چند بین داده های دو جدول امکان تبدیل وجود ندارد"); // } // if (targetRaltionshipType == Enum_RelationshipType.ExplicitOneToOne) // bizRelationship.ConvertManyToOneToExplicit(relationship); // else if (targetRaltionshipType == Enum_RelationshipType.SubToSuper) // { // var isaRelationships = bizISARelationship.GetISARelationships(relationship.EntityID2); // ISARelationshipCreateOrSelect(isaRelationships, relationship, targetRaltionshipType); // } // else if (targetRaltionshipType == Enum_RelationshipType.UnionToSubUnion_UnionHoldsKeys) // { // var unionRelationships = bizUnionRelationship.GetUnionRelationships(relationship.EntityID1, true); // UnionRelationshipCreateOrSelect(unionRelationships, true, relationship, targetRaltionshipType); // } // else if (targetRaltionshipType == Enum_RelationshipType.SubUnionToUnion_SubUnionHoldsKeys) // { // var unionRelationships = bizUnionRelationship.GetUnionRelationships(relationship.EntityID1, false); // UnionRelationshipCreateOrSelect(unionRelationships, false, relationship, targetRaltionshipType); // } //} }