private IEnumerable<ContactWithTaskWrapper> ToSimpleListContactWrapper(IReadOnlyList<Contact> itemList) { if (itemList.Count == 0) return new List<ContactWithTaskWrapper>(); var result = new List<ContactWithTaskWrapper>(); var personsIDs = new List<int>(); var companyIDs = new List<int>(); var contactIDs = new int[itemList.Count]; var peopleCompanyIDs = new List<int>(); var peopleCompanyList = new Dictionary<int, ContactBaseWrapper>(); var contactDao = DaoFactory.GetContactDao(); for (var index = 0; index < itemList.Count; index++) { var contact = itemList[index]; if (contact is Company) { companyIDs.Add(contact.ID); } else { var person = contact as Person; if (person != null) { personsIDs.Add(person.ID); if (person.CompanyID > 0) { peopleCompanyIDs.Add(person.CompanyID); } } } contactIDs[index] = itemList[index].ID; } if (peopleCompanyIDs.Count > 0) { var tmpList = contactDao.GetContacts(peopleCompanyIDs.ToArray()).ConvertAll(item => ToContactBaseWrapperQuick(item)); var tmpListCanDelete = contactDao.CanDelete(tmpList.Select(item => item.ID).ToArray()); foreach (var contactBaseWrapperQuick in tmpList) { contactBaseWrapperQuick.CanDelete = contactBaseWrapperQuick.CanEdit && tmpListCanDelete[contactBaseWrapperQuick.ID]; peopleCompanyList.Add(contactBaseWrapperQuick.ID, contactBaseWrapperQuick); } } var contactInfos = new Dictionary<int, List<ContactInfoWrapper>>(); var addresses = new Dictionary<int, List<Address>>(); DaoFactory.GetContactInfoDao().GetAll(contactIDs).ForEach( item => { if (item.InfoType == ContactInfoType.Address) { if (!addresses.ContainsKey(item.ContactID)) { addresses.Add(item.ContactID, new List<Address> { new Address(item) }); } else { addresses[item.ContactID].Add(new Address(item)); } } else { if (!contactInfos.ContainsKey(item.ContactID)) { contactInfos.Add(item.ContactID, new List<ContactInfoWrapper> {new ContactInfoWrapper(item)}); } else { contactInfos[item.ContactID].Add(new ContactInfoWrapper(item)); } } } ); var nearestTasks = DaoFactory.GetTaskDao().GetNearestTask(contactIDs.ToArray()); IEnumerable<TaskCategoryBaseWrapper> taskCategories = new List<TaskCategoryBaseWrapper>(); if (nearestTasks.Any()) { taskCategories = DaoFactory.GetListItemDao().GetItems(ListType.TaskCategory).ConvertAll(item => new TaskCategoryBaseWrapper(item)); } foreach (var contact in itemList) { ContactWrapper contactWrapper; var person = contact as Person; if (person != null) { var people = person; var peopleWrapper = PersonWrapper.ToPersonWrapperQuick(people); if (people.CompanyID > 0 && peopleCompanyList.ContainsKey(people.CompanyID)) { peopleWrapper.Company = peopleCompanyList[people.CompanyID]; } contactWrapper = peopleWrapper; } else { var company = contact as Company; if (company != null) { contactWrapper = CompanyWrapper.ToCompanyWrapperQuick(company); } else { throw new ArgumentException(); } } contactWrapper.CommonData = contactInfos.ContainsKey(contact.ID) ? contactInfos[contact.ID] : new List<ContactInfoWrapper>(); TaskBaseWrapper taskWrapper = null; if (nearestTasks.ContainsKey(contactWrapper.ID)) { var task = nearestTasks[contactWrapper.ID]; taskWrapper = new TaskBaseWrapper(task); if (task.CategoryID > 0) { taskWrapper.Category = taskCategories.First(x => x.ID == task.CategoryID); } } result.Add(new ContactWithTaskWrapper { Contact = contactWrapper, Task = taskWrapper }); } #region CanDelete for main contacts if (result.Count > 0) { var resultListCanDelete = contactDao.CanDelete(result.Select(item => item.Contact.ID).ToArray()); foreach (var contactBaseWrapperQuick in result) { contactBaseWrapperQuick.Contact.CanDelete = contactBaseWrapperQuick.Contact.CanEdit && resultListCanDelete[contactBaseWrapperQuick.Contact.ID]; } } #endregion return result; }
private IEnumerable<ContactWithTaskWrapper> ToSimpleListContactWrapper(IReadOnlyList<Contact> itemList) { if (itemList.Count == 0) return new List<ContactWithTaskWrapper>(); var result = new List<ContactWithTaskWrapper>(); var personsIDs = new List<int>(); var companyIDs = new List<int>(); var contactIDs = new int[itemList.Count]; var peopleCompanyIDs = new List<int>(); var peopleCompanyList = new Dictionary<int, ContactBaseWrapper>(); for (int index = 0; index < itemList.Count; index++) { var contact = itemList[index]; if (contact is Company) companyIDs.Add(contact.ID); else if (contact is Person) { var person = (Person)contact; personsIDs.Add(contact.ID); if (person.CompanyID > 0) peopleCompanyIDs.Add(person.CompanyID); } contactIDs[index] = itemList[index].ID; } if (peopleCompanyIDs.Count > 0) peopleCompanyList = DaoFactory.GetContactDao().GetContacts(peopleCompanyIDs.ToArray()) .ToDictionary(item => item.ID, ToContactBaseWrapper); var contactInfos = new Dictionary<int, List<ContactInfoWrapper>>(); var addresses = new Dictionary<int, List<Address>>(); DaoFactory.GetContactInfoDao().GetAll(contactIDs).ForEach( item => { if (item.InfoType == ContactInfoType.Address) { if (!addresses.ContainsKey(item.ContactID)) addresses.Add(item.ContactID, new List<Address> { new Address(item) }); else addresses[item.ContactID].Add(new Address(item)); } else { if (!contactInfos.ContainsKey(item.ContactID)) contactInfos.Add(item.ContactID, new List<ContactInfoWrapper> { new ContactInfoWrapper(item) }); else contactInfos[item.ContactID].Add(new ContactInfoWrapper(item)); } } ); var nearestTasks = DaoFactory.GetTaskDao().GetNearestTask(contactIDs.ToArray()); IEnumerable<TaskCategoryBaseWrapper> taskCategories = new List<TaskCategoryBaseWrapper>(); if (nearestTasks.Any()) { taskCategories = DaoFactory.GetListItemDao().GetItems(ListType.TaskCategory).ConvertAll(item => new TaskCategoryBaseWrapper(item)); } foreach (var contact in itemList) { ContactWrapper contactWrapper; if (contact is Person) { var people = (Person)contact; var peopleWrapper = new PersonWrapper(people); if (people.CompanyID > 0 && peopleCompanyList.ContainsKey(people.CompanyID)) { peopleWrapper.Company = peopleCompanyList[people.CompanyID]; } contactWrapper = peopleWrapper; } else if (contact is Company) { contactWrapper = new CompanyWrapper((Company)contact); } else { throw new ArgumentException(); } contactWrapper.CommonData = contactInfos.ContainsKey(contact.ID) ? contactInfos[contact.ID] : new List<ContactInfoWrapper>(); TaskBaseWrapper taskWrapper = null; if (nearestTasks.ContainsKey(contactWrapper.ID)) { var task = nearestTasks[contactWrapper.ID]; taskWrapper = new TaskBaseWrapper(task); if (task.CategoryID > 0) taskWrapper.Category = taskCategories.First(x => x.ID == task.CategoryID); } result.Add(new ContactWithTaskWrapper { Contact = contactWrapper, Task = taskWrapper }); } return result; }