public int Save(SaveSearchs Data, int UpdatingUserID) { TopContractsV01Entities context = new TopContractsV01Entities(Utilities.getTestEnvName()); TopContractsEntities.SaveSearch efSaveSearch = null; TopContractsEntities.SearchResultsContract efSaveContracts = null; foreach (TopContractsDAL10.DasboardTables.SaveSearch svSearch in Data.Entries) { if (svSearch.New) { efSaveSearch = new TopContractsEntities.SaveSearch(); } else { efSaveSearch = context.SaveSearches.Where(c => c.SearchId == svSearch.ID).SingleOrDefault(); } if (svSearch.Deleted == false) { efSaveSearch.DisplayOrder = svSearch.DisplayOrder; efSaveSearch.Description = svSearch.Description; efSaveSearch.Name = svSearch.Name; efSaveSearch.ExternalID = svSearch.ExternalID; efSaveSearch.SearchQuery = svSearch.SearchQuery; efSaveSearch.Inactive = svSearch.Inactive; efSaveSearch.Locked = svSearch.Locked; efSaveSearch.OrganizationIdentifier = this.organizationIdentifier; foreach (TopContractsDAL10.DasboardTables.SearchResultsContract svContracts in svSearch.searchResultsContracts.Entries) { if (svContracts.New) { efSaveContracts = new TopContractsEntities.SearchResultsContract(); } else { efSaveContracts = context.SearchResultsContracts.Where(c => c.SearchContractId == svContracts.ID).SingleOrDefault(); } if (svContracts.Deleted == false) { efSaveContracts.SearchId = svContracts.SearchId; efSaveContracts.ContractId = svContracts.ContractId; } if ((svContracts.New == true && svContracts.Deleted == false) || (svContracts.New == false && svContracts.Deleted == true)) { if (svContracts.New) efSaveSearch.SearchResultsContracts.Add(efSaveContracts); else { if (svContracts.Deleted && efSaveContracts != null) { efSaveSearch.SearchResultsContracts.Remove(efSaveContracts); context.SearchResultsContracts.Remove(efSaveContracts); } } } } } if (svSearch.New) { context.SaveSearches.Add(efSaveSearch); } else { if (svSearch.Deleted && efSaveSearch != null) { foreach (TopContractsDAL10.DasboardTables.SearchResultsContract svContracts in svSearch.searchResultsContracts.Entries) { efSaveContracts = context.SearchResultsContracts.Where(c => c.SearchContractId == svContracts.ID).SingleOrDefault(); context.SearchResultsContracts.Remove(efSaveContracts); } context.SaveSearches.Remove(efSaveSearch); } } } bool updateSavedSearchDetails = false; foreach (var entry in context.ChangeTracker.Entries()) { if (entry.State == System.Data.EntityState.Added || entry.State == System.Data.EntityState.Modified) { updateSavedSearchDetails = true; } } if (updateSavedSearchDetails) foreach (var entry in context.ChangeTracker.Entries()) { if (entry.Entity is TopContractsEntities.SaveSearch && (entry.State == EntityState.Added || entry.State == EntityState.Modified)) { ((TopContractsEntities.SaveSearch)entry.Entity).UpdateDate = DateTime.Now; ((TopContractsEntities.SaveSearch)entry.Entity).UpdateUserID = UpdatingUserID; } } return context.SaveChanges(); }
public long Save(TopContractsDAL10.DasboardTables.SaveSearch search, bool overwrite, long searchID) { TopContractsV01Entities context = new TopContractsV01Entities(Utilities.getTestEnvName()); TopContractsEntities.SaveSearch efSearch = null; if (searchID == 0) { efSearch = new TopContractsEntities.SaveSearch(); } else { efSearch = context.SaveSearches.Where(c => c.SearchId == searchID).SingleOrDefault(); } efSearch.SearchQuery = search.SearchQuery; efSearch.OrganizationIdentifier = this.organizationIdentifier; efSearch.Name = search.Name; efSearch.Description = search.Description; efSearch.DisplayOrder = search.DisplayOrder; efSearch.UpdateDate = DateTime.Now; efSearch.UpdateUserID = search.UpdateUserId; //efSearch.Inactive = true; efSearch.Inactive = false; if (!overwrite) { efSearch.UserId = search.UserId; efSearch.Shared = search.Shared; efSearch.DateCreated = search.CreatedDate; } //Added By Salil + Boaz //1-May-2013 if (search.searchResultsContracts != null) if (search.searchResultsContracts.Entries != null) if (search.searchResultsContracts.Entries.Count > 0) { foreach (TopContractsDAL10.DasboardTables.SearchResultsContract _searchResultsContracts in search.searchResultsContracts.Entries) { context.SearchResultsContracts.Add(new TopContractsEntities.SearchResultsContract() { SearchId = _searchResultsContracts.SearchId, ContractId = _searchResultsContracts.ContractId }); } } if (!overwrite) context.SaveSearches.Add(efSearch); context.SaveChanges(); return Convert.ToInt64(efSearch.SearchId); }