public string AddTagToBatchContacts( IEnumerable <string> tags, int contactStage, int contactType, ContactListViewType contactListView, ApiDateTime fromDate, ApiDateTime toDate, string tagName) { var contacts = DaoFactory .ContactDao .GetContacts(_context.FilterValue, tags, contactStage, contactType, contactListView, fromDate, toDate, 0, 0, null).Where(CRMSecurity.CanEdit).ToList(); foreach (var contact in contacts) { AddTagTo("contact", contact.ID, tagName); } return(tagName); }
public String AddTagToBatchContacts(IEnumerable <String> tags, int contactStage, int contactType, ContactListViewType contactListView, ApiDateTime fromDate, ApiDateTime toDate, String tagName) { var contacts = DaoFactory.GetContactDao().GetContacts(_context.FilterValue, tags, contactStage, contactType, contactListView, fromDate, toDate, 0, 0, null); foreach (var contact in contacts) { AddTagTo("contact", contact.ID, tagName); } return(tagName); }
public IEnumerable<ContactWrapper> GetContacts(IEnumerable<String> tags, int contactType, ContactListViewType contactListView) { IEnumerable<ContactWrapper> result; OrderBy contactsOrderBy; ContactSortedByType sortBy; var searchString = _context.FilterValue; if (ASC.Web.CRM.Classes.EnumExtension.TryParse(_context.SortBy, true, out sortBy)) contactsOrderBy = new OrderBy(sortBy, !_context.SortDescending); else if (String.IsNullOrEmpty(_context.SortBy)) contactsOrderBy = new OrderBy(ContactSortedByType.DisplayName, true); else contactsOrderBy = null; if (contactsOrderBy != null) { result = ToListContactWrapper(DaoFactory.GetContactDao() .GetContacts(searchString, tags, contactType, contactListView, (int) _context.StartIndex, (int) _context.Count, contactsOrderBy)); _context.SetDataPaginated(); _context.SetDataFiltered(); _context.SetDataSorted(); } else { result = ToListContactWrapper(DaoFactory.GetContactDao() .GetContacts(searchString, tags, contactType, contactListView, 0, 0, null)); } var totalCount = DaoFactory.GetContactDao().GetContactsCount(searchString, tags, contactType, contactListView); _context.SetTotalCount(totalCount); return result.ToSmartList(); }
public int GetContactsCount(String searchText, IEnumerable<String> tags, int contactStage, int contactType, ContactListViewType contactListView, DateTime fromDate, DateTime toDate, Guid responsibleid = default(Guid)) { var cacheKey = TenantID.ToString() + "contacts" + SecurityContext.CurrentAccount.ID.ToString() + searchText + contactStage + contactType + (int)contactListView + responsibleid; if (tags != null) cacheKey += String.Join("", tags.ToArray()); if (fromDate != DateTime.MinValue) cacheKey += fromDate.ToString(); if (toDate != DateTime.MinValue) cacheKey += toDate.ToString(); var fromCache = _cache.Get(cacheKey); if (fromCache != null) return Convert.ToInt32(fromCache); var withParams = !(String.IsNullOrEmpty(searchText) && (tags == null || !tags.Any()) && contactStage <= 0 && contactType <= 0 && contactListView == ContactListViewType.All && fromDate == DateTime.MinValue && toDate == DateTime.MinValue && responsibleid == default(Guid)); int result; using (var db = GetDb()) { if (withParams) { ICollection<int> excludedContactIDs; switch (contactListView) { case ContactListViewType.Person: excludedContactIDs = CRMSecurity.GetPrivateItems(typeof(Person)) .Except(db.ExecuteList(Query("crm_contact").Select("id") .Where(Exp.Eq("is_shared", true) & Exp.Eq("is_company", false))) .Select(x => Convert.ToInt32(x[0]))).ToList(); break; case ContactListViewType.Company: excludedContactIDs = CRMSecurity.GetPrivateItems(typeof(Company)) .Except(db.ExecuteList(Query("crm_contact").Select("id") .Where(Exp.Eq("is_shared", true) & Exp.Eq("is_company", true))) .Select(x => Convert.ToInt32(x[0]))).ToList(); break; default: excludedContactIDs = CRMSecurity.GetPrivateItems(typeof(Company)).Union(CRMSecurity.GetPrivateItems(typeof(Person))) .Except(db.ExecuteList(Query("crm_contact").Select("id").Where("is_shared", true)) .Select(x => Convert.ToInt32(x[0]))).ToList(); break; } var whereConditional = WhereConditional(excludedContactIDs, searchText, tags, contactStage, contactType, contactListView, fromDate, toDate, responsibleid); if (whereConditional != null) { var sqlQuery = Query("crm_contact").SelectCount().Where(whereConditional); result = db.ExecuteScalar<int>(sqlQuery); } else { result = 0; } } else { var countWithoutPrivate = db.ExecuteScalar<int>(Query("crm_contact").SelectCount()); var privateCount = CRMSecurity.GetPrivateItemsCount(typeof(Person)) + CRMSecurity.GetPrivateItemsCount(typeof(Company)) - db.ExecuteScalar<int>(Query("crm_contact").Where("is_shared", true).SelectCount()); if (privateCount < 0) privateCount = 0; if (privateCount > countWithoutPrivate) { _log.Error("Private contacts count more than all contacts"); privateCount = 0; } result = countWithoutPrivate - privateCount; } } if (result > 0) _cache.Insert(cacheKey, result, new CacheDependency(null, new[] { _contactCacheKey }), Cache.NoAbsoluteExpiration, TimeSpan.FromMinutes(1)); return result; }
public int GetContactsCount(String searchText, IEnumerable <String> tags, int contactStatus, ContactListViewType contactListView) { var cacheKey = TenantID.ToString() + "contacts" + SecurityContext.CurrentAccount.ID.ToString() + searchText + contactStatus + (int)contactListView; if (tags != null) { cacheKey += String.Join("", tags.ToArray()); } var fromCache = _cache.Get(cacheKey); if (fromCache != null) { return(Convert.ToInt32(fromCache)); } var withParams = !(String.IsNullOrEmpty(searchText) && (tags == null || !tags.Any()) && contactStatus <= 0 && contactListView == ContactListViewType.All); int result; if (withParams) { ICollection <int> excludedContactIDs; switch (contactListView) { case ContactListViewType.Person: excludedContactIDs = CRMSecurity.GetPrivateItems(typeof(Person)).ToList(); break; case ContactListViewType.Company: excludedContactIDs = CRMSecurity.GetPrivateItems(typeof(Company)).ToList(); break; default: excludedContactIDs = CRMSecurity.GetPrivateItems(typeof(Company)).Union(CRMSecurity.GetPrivateItems(typeof(Person))).ToList(); break; } var whereConditional = WhereConditional(excludedContactIDs, searchText, tags, contactStatus, contactListView); if (whereConditional != null) { result = DbManager.ExecuteScalar <int>(Query("crm_contact").Where(whereConditional).SelectCount()); } else { result = 0; } } else { var countWithoutPrivate = DbManager.ExecuteScalar <int>(Query("crm_contact").SelectCount()); var privateCount = CRMSecurity.GetPrivateItemsCount(typeof(Person)) + CRMSecurity.GetPrivateItemsCount(typeof(Company)); if (privateCount > countWithoutPrivate) { _log.Error("Private contacts count more than all contacts"); privateCount = 0; } result = countWithoutPrivate - privateCount; } if (result > 0) { _cache.Insert(cacheKey, result, new CacheDependency(null, new[] { _contactCacheKey }), Cache.NoAbsoluteExpiration, TimeSpan.FromSeconds(30)); } return(result); }
public IEnumerable<ContactWithTaskWrapper> GetSimpleContacts( IEnumerable<string> tags, int? contactStage, int? contactType, ContactListViewType contactListView, Guid? responsibleid, bool? isShared, ApiDateTime fromDate, ApiDateTime toDate) { IEnumerable<ContactWithTaskWrapper> result; OrderBy contactsOrderBy; ContactSortedByType sortBy; var searchString = _context.FilterValue; if (Web.CRM.Classes.EnumExtension.TryParse(_context.SortBy, true, out sortBy)) { contactsOrderBy = new OrderBy(sortBy, !_context.SortDescending); } else if (String.IsNullOrEmpty(_context.SortBy)) { contactsOrderBy = new OrderBy(ContactSortedByType.DisplayName, true); } else { contactsOrderBy = null; } var fromIndex = (int)_context.StartIndex; var count = (int)_context.Count; var contactStageInt = contactStage.HasValue ? contactStage.Value : -1; var contactTypeInt = contactType.HasValue ? contactType.Value : -1; if (contactsOrderBy != null) { result = ToSimpleListContactWrapper(DaoFactory.GetContactDao().GetContacts( searchString, tags, contactStageInt, contactTypeInt, contactListView, fromDate, toDate, fromIndex, count, contactsOrderBy, responsibleid, isShared)); _context.SetDataPaginated(); _context.SetDataFiltered(); _context.SetDataSorted(); } else { result = ToSimpleListContactWrapper(DaoFactory.GetContactDao().GetContacts( searchString, tags, contactStageInt, contactTypeInt, contactListView, fromDate, toDate, 0, 0, null, responsibleid, isShared)); } int totalCount; if (result.Count() < count) { totalCount = fromIndex + result.Count(); } else { totalCount = DaoFactory.GetContactDao().GetContactsCount( searchString, tags, contactStageInt, contactTypeInt, contactListView, fromDate, toDate, responsibleid, isShared); } _context.SetTotalCount(totalCount); return result; }
public string AddTagToBatchContacts( IEnumerable<string> tags, int contactStage, int contactType, ContactListViewType contactListView, ApiDateTime fromDate, ApiDateTime toDate, string tagName) { var contacts = DaoFactory .GetContactDao() .GetContacts(_context.FilterValue, tags, contactStage, contactType, contactListView, fromDate, toDate, 0, 0, null).Where(CRMSecurity.CanAccessTo).ToList(); foreach (var contact in contacts) { AddTagTo("contact", contact.ID, tagName); } return tagName; }
private Exp WhereConditional( ICollection<int> exceptIDs, String searchText, IEnumerable<String> tags, int contactStatus, ContactListViewType contactListView) { var conditions = new List<Exp>(); var ids = new List<int>(); if (!String.IsNullOrEmpty(searchText)) { searchText = searchText.Trim(); var keywords = searchText.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries) .ToArray(); if (!FullTextSearch.SupportModule(FullTextSearch.CRMContactsModule)) conditions.Add(BuildLike(new[] { "display_name" }, keywords)); else { ids = FullTextSearch.Search(searchText, FullTextSearch.CRMContactsModule) .GetIdentifiers() .Select(item => Convert.ToInt32(item.Split('_')[1])).Distinct() .ToList(); if (ids.Count == 0) return null; } } if (tags != null && tags.Any()) { ids = SearchByTags(EntityType.Contact, ids.ToArray(), tags); if (ids.Count == 0) return null; } switch (contactListView) { case ContactListViewType.Company: conditions.Add(Exp.Eq("is_company", true)); break; case ContactListViewType.Person: conditions.Add(Exp.Eq("is_company", false)); break; case ContactListViewType.WithOpportunity: if (ids.Count > 0) ids = DbManager.ExecuteList(Query("crm_deal").Select("contact_id") .Distinct() .Where(Exp.In("contact_id", ids))).ConvertAll(row => Convert.ToInt32(row[0])); else ids = DbManager.ExecuteList(Query("crm_deal").Select("contact_id") .Distinct() .Where(!Exp.Eq("contact_id", 0))).ConvertAll(row => Convert.ToInt32(row[0])); if (ids.Count == 0) return null; break; } if (contactStatus > 0) conditions.Add(Exp.Eq("status_id", contactStatus)); if (ids.Count > 0) { if (exceptIDs.Count > 0) { ids = ids.Except(exceptIDs).ToList(); if (ids.Count == 0) return null; } conditions.Add(Exp.In("id", ids)); } else if (exceptIDs.Count > 0) { conditions.Add(!Exp.In("id", exceptIDs.ToArray())); } if (conditions.Count == 0) return null; if (conditions.Count == 1) return conditions[0]; return conditions.Aggregate((i, j) => i & j); }
public int GetContactsCount(String searchText, IEnumerable<String> tags, int contactStatus, ContactListViewType contactListView) { var cacheKey = TenantID.ToString() + "contacts" + SecurityContext.CurrentAccount.ID.ToString() + searchText + contactStatus + (int)contactListView; if (tags != null) cacheKey += String.Join("", tags.ToArray()); var fromCache = _cache.Get(cacheKey); if (fromCache != null) return Convert.ToInt32(fromCache); var withParams = !(String.IsNullOrEmpty(searchText) && (tags == null || !tags.Any()) && contactStatus <= 0 && contactListView == ContactListViewType.All); int result; if (withParams) { ICollection<int> excludedContactIDs; switch (contactListView) { case ContactListViewType.Person: excludedContactIDs = CRMSecurity.GetPrivateItems(typeof(Person)).ToList(); break; case ContactListViewType.Company: excludedContactIDs = CRMSecurity.GetPrivateItems(typeof(Company)).ToList(); break; default: excludedContactIDs = CRMSecurity.GetPrivateItems(typeof(Company)).Union(CRMSecurity.GetPrivateItems(typeof(Person))).ToList(); break; } var whereConditional = WhereConditional(excludedContactIDs, searchText, tags, contactStatus, contactListView); if (whereConditional != null) result = DbManager.ExecuteScalar<int>(Query("crm_contact").Where(whereConditional).SelectCount()); else result = 0; } else { var countWithoutPrivate = DbManager.ExecuteScalar<int>(Query("crm_contact").SelectCount()); var privateCount = CRMSecurity.GetPrivateItemsCount(typeof(Person)) + CRMSecurity.GetPrivateItemsCount(typeof(Company)); if (privateCount > countWithoutPrivate) { _log.Error("Private contacts count more than all contacts"); privateCount = 0; } result = countWithoutPrivate - privateCount; } if (result > 0) _cache.Insert(cacheKey, result, new CacheDependency(null, new[] { _contactCacheKey }), Cache.NoAbsoluteExpiration, TimeSpan.FromSeconds(30)); return result; }
public IEnumerable<ContactWrapper> SetAccessToBatchContact( IEnumerable<String> tags, int contactStage, int contactType, ContactListViewType contactListView, ApiDateTime fromDate, ApiDateTime toDate, bool isPrivate, IEnumerable<Guid> managerList ) { var result = new List<Contact>(); var contacts = DaoFactory.GetContactDao().GetContacts(_context.FilterValue, tags, contactStage, contactType, contactListView, fromDate, toDate, 0, 0, null); if (!contacts.Any()) return Enumerable.Empty<ContactWrapper>(); foreach (var contact in contacts) { if (contact == null) throw new ItemNotFoundException(); if (!(CRMSecurity.IsAdmin || contact.CreateBy == Core.SecurityContext.CurrentAccount.ID)) continue; SetAccessToContact(contact, isPrivate, managerList); result.Add(contact); } return ToListContactWrapper(result); }
private Exp WhereConditional( ICollection<int> exceptIDs, String searchText, IEnumerable<String> tags, int contactStage, int contactType, ContactListViewType contactListView, DateTime fromDate, DateTime toDate, Guid? responsibleid = null, bool? isShared = null) { var conditions = new List<Exp>(); var ids = new List<int>(); if (responsibleid.HasValue) { if (responsibleid != default(Guid)) { ids = CRMSecurity.GetContactsIdByManager(responsibleid.Value).ToList(); if (ids.Count == 0) return null; } else { if (exceptIDs == null) exceptIDs = new List<int>(); exceptIDs = exceptIDs.Union(CRMSecurity.GetContactsIdByManager(Guid.Empty)).ToList(); if (!exceptIDs.Any()) // HACK exceptIDs.Add(0); } } if (!String.IsNullOrEmpty(searchText)) { searchText = searchText.Trim(); var keywords = searchText.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries) .ToArray(); var modules = SearchDao.GetFullTextSearchModule(EntityType.Contact, searchText); if (!FullTextSearch.SupportModule(modules)) { _log.Debug("FullTextSearch.SupportModule('CRM.Contacts') = false"); conditions.Add(BuildLike(new[] { "display_name" }, keywords)); } else { _log.Debug("FullTextSearch.SupportModule('CRM.Contacts') = true"); _log.DebugFormat("FullTextSearch.Search: searchText = {0}", searchText); var full_text_ids = FullTextSearch.Search(modules); if (full_text_ids.Count == 0) return null; if (ids.Count != 0) { ids = ids.Where(i => full_text_ids.Contains(i)).ToList(); } else { ids = full_text_ids; } } } if (tags != null && tags.Any()) { ids = SearchByTags(EntityType.Contact, ids.ToArray(), tags); if (ids.Count == 0) return null; } using (var db = GetDb()) { switch (contactListView) { case ContactListViewType.Company: conditions.Add(Exp.Eq("is_company", true)); break; case ContactListViewType.Person: conditions.Add(Exp.Eq("is_company", false)); break; case ContactListViewType.WithOpportunity: if (ids.Count > 0) { ids = db.ExecuteList(new SqlQuery("crm_entity_contact").Select("contact_id") .Distinct() .Where(Exp.In("contact_id", ids) & Exp.Eq("entity_type", (int)EntityType.Opportunity))) .ConvertAll(row => Convert.ToInt32(row[0])); } else { ids = db.ExecuteList(new SqlQuery("crm_entity_contact").Select("contact_id") .Distinct() .Where(Exp.Eq("entity_type", (int)EntityType.Opportunity))) .ConvertAll(row => Convert.ToInt32(row[0])); } if (ids.Count == 0) return null; break; } } if (contactStage >= 0) conditions.Add(Exp.Eq("status_id", contactStage)); if (contactType >= 0) conditions.Add(Exp.Eq("contact_type_id", contactType)); if (fromDate != DateTime.MinValue && toDate != DateTime.MinValue) conditions.Add(Exp.Between("create_on", TenantUtil.DateTimeToUtc(fromDate), TenantUtil.DateTimeToUtc(toDate.AddDays(1).AddMinutes(-1)))); else if (fromDate != DateTime.MinValue) conditions.Add(Exp.Ge("create_on", TenantUtil.DateTimeToUtc(fromDate))); else if (toDate != DateTime.MinValue) conditions.Add(Exp.Le("create_on", TenantUtil.DateTimeToUtc(toDate.AddDays(1).AddMinutes(-1)))); if (isShared.HasValue) { if (isShared.Value == true) { conditions.Add(Exp.Or(Exp.In("is_shared", new[]{(int)ShareType.Read , (int)ShareType.ReadWrite}), Exp.Eq("is_shared", null))); } else { conditions.Add(Exp.Or(Exp.Eq("is_shared", (int)ShareType.None), Exp.Eq("is_shared", null))); } } if (ids.Count > 0) { if (exceptIDs.Count > 0) { ids = ids.Except(exceptIDs).ToList(); if (ids.Count == 0) return null; } conditions.Add(Exp.In("id", ids)); } else if (exceptIDs.Count > 0) { conditions.Add(!Exp.In("id", exceptIDs.ToArray())); } if (conditions.Count == 0) return null; return conditions.Count == 1 ? conditions[0] : conditions.Aggregate((i, j) => i & j); }
public String AddTagToBatchContacts(IEnumerable<String> tags, int contactStage, int contactType, ContactListViewType contactListView, ApiDateTime fromDate, ApiDateTime toDate, String tagName) { var contacts = DaoFactory.GetContactDao().GetContacts(_context.FilterValue, tags, contactStage, contactType, contactListView, fromDate, toDate, 0, 0, null); foreach (var contact in contacts) AddTagTo("contact", contact.ID, tagName); return tagName; }
private List <Contact> GetCrudeContacts( String searchText, IEnumerable <String> tags, int contactStatus, ContactListViewType contactListView, int from, int count, OrderBy orderBy) { var sqlQuery = GetContactSqlQuery(null); var withParams = !(String.IsNullOrEmpty(searchText) && (tags == null || !tags.Any()) && contactStatus <= 0 && contactListView == ContactListViewType.All); var whereConditional = WhereConditional(new List <int>(), searchText, tags, contactStatus, contactListView); if (withParams && whereConditional == null) { return(new List <Contact>()); } sqlQuery.Where(whereConditional); if (0 < from && from < int.MaxValue) { sqlQuery.SetFirstResult(from); } if (0 < count && count < int.MaxValue) { sqlQuery.SetMaxResults(count); } if (orderBy != null) { if (!Enum.IsDefined(typeof(ContactSortedByType), orderBy.SortedBy.ToString())) { orderBy.SortedBy = ContactSortedByType.DisplayName; } switch ((ContactSortedByType)orderBy.SortedBy) { case ContactSortedByType.DisplayName: sqlQuery.OrderBy("display_name", orderBy.IsAsc); break; case ContactSortedByType.Created: sqlQuery.OrderBy("create_on", orderBy.IsAsc); break; case ContactSortedByType.ContactType: sqlQuery.OrderBy("status_id", orderBy.IsAsc); break; default: sqlQuery.OrderBy("display_name", orderBy.IsAsc); break; } } return(DbManager.ExecuteList(sqlQuery).ConvertAll(contact => ToContact(contact))); }
private Exp WhereConditional( ICollection <int> exceptIDs, String searchText, IEnumerable <String> tags, int contactStatus, ContactListViewType contactListView) { var conditions = new List <Exp>(); var ids = new List <int>(); if (!String.IsNullOrEmpty(searchText)) { searchText = searchText.Trim(); var keywords = searchText.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries) .ToArray(); if (!FullTextSearch.SupportModule(FullTextSearch.CRMContactsModule)) { conditions.Add(BuildLike(new[] { "display_name" }, keywords)); } else { ids = FullTextSearch.Search(searchText, FullTextSearch.CRMContactsModule) .GetIdentifiers() .Select(item => Convert.ToInt32(item.Split('_')[1])).Distinct() .ToList(); if (ids.Count == 0) { return(null); } } } if (tags != null && tags.Any()) { ids = SearchByTags(EntityType.Contact, ids.ToArray(), tags); if (ids.Count == 0) { return(null); } } switch (contactListView) { case ContactListViewType.Company: conditions.Add(Exp.Eq("is_company", true)); break; case ContactListViewType.Person: conditions.Add(Exp.Eq("is_company", false)); break; case ContactListViewType.WithOpportunity: if (ids.Count > 0) { ids = DbManager.ExecuteList(Query("crm_deal").Select("contact_id") .Distinct() .Where(Exp.In("contact_id", ids))).ConvertAll(row => Convert.ToInt32(row[0])); } else { ids = DbManager.ExecuteList(Query("crm_deal").Select("contact_id") .Distinct() .Where(!Exp.Eq("contact_id", 0))).ConvertAll(row => Convert.ToInt32(row[0])); } if (ids.Count == 0) { return(null); } break; } if (contactStatus > 0) { conditions.Add(Exp.Eq("status_id", contactStatus)); } if (ids.Count > 0) { if (exceptIDs.Count > 0) { ids = ids.Except(exceptIDs).ToList(); if (ids.Count == 0) { return(null); } } conditions.Add(Exp.In("id", ids)); } else if (exceptIDs.Count > 0) { conditions.Add(!Exp.In("id", exceptIDs.ToArray())); } if (conditions.Count == 0) { return(null); } if (conditions.Count == 1) { return(conditions[0]); } return(conditions.Aggregate((i, j) => i & j)); }
public List <Contact> GetContacts(String searchText, IEnumerable <String> tags, int contactStatus, ContactListViewType contactListView, int from, int count, OrderBy orderBy) { if (CRMSecurity.IsAdmin) { return(GetCrudeContacts( searchText, tags, contactStatus, contactListView, from, count, orderBy)); } var crudeContacts = GetCrudeContacts( searchText, tags, contactStatus, contactListView, 0, from + count, orderBy); if (crudeContacts.Count == 0) { return(crudeContacts); } if (crudeContacts.Count < from + count) { return(crudeContacts .FindAll(CRMSecurity.CanAccessTo).Skip(from).ToList()); } var result = crudeContacts.FindAll(CRMSecurity.CanAccessTo); if (result.Count == crudeContacts.Count) { return(result.Skip(from).ToList()); } var localCount = count; var localFrom = from + count; while (true) { crudeContacts = GetCrudeContacts( searchText, tags, contactStatus, contactListView, localFrom, localCount, orderBy); if (crudeContacts.Count == 0) { break; } result.AddRange(crudeContacts.Where(CRMSecurity.CanAccessTo)); if ((result.Count >= count + from) || (crudeContacts.Count < localCount)) { break; } localFrom += localCount; localCount = localCount * 2; } return(result.Skip(from).Take(count).ToList()); }
private Exp WhereConditional( ICollection<int> exceptIDs, String searchText, IEnumerable<String> tags, int contactStage, int contactType, ContactListViewType contactListView, DateTime fromDate, DateTime toDate, Guid responsibleid = default(Guid)) { var conditions = new List<Exp>(); var ids = new List<int>(); if (responsibleid != default(Guid)) { ids = CRMSecurity.GetContactsIdByManager(responsibleid).ToList(); } if (!String.IsNullOrEmpty(searchText)) { searchText = searchText.Trim(); var keywords = searchText.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries) .ToArray(); if (!FullTextSearch.SupportModule(FullTextSearch.CRMContactsModule)) { conditions.Add(BuildLike(new[] { "display_name" }, keywords)); } else { ids = FullTextSearch.Search(searchText, FullTextSearch.CRMContactsModule) .GetIdentifiers() .Select(item => Convert.ToInt32(item.Split('_')[1])).Distinct() .ToList(); if (ids.Count == 0) return null; } } if (tags != null && tags.Any()) { ids = SearchByTags(EntityType.Contact, ids.ToArray(), tags); if (ids.Count == 0) return null; } using (var db = GetDb()) { switch (contactListView) { case ContactListViewType.Company: conditions.Add(Exp.Eq("is_company", true)); break; case ContactListViewType.Person: conditions.Add(Exp.Eq("is_company", false)); break; case ContactListViewType.WithOpportunity: if (ids.Count > 0) ids = db.ExecuteList(Query("crm_deal").Select("contact_id") .Distinct() .Where(Exp.In("contact_id", ids))).ConvertAll(row => Convert.ToInt32(row[0])); else ids = db.ExecuteList(Query("crm_deal").Select("contact_id") .Distinct() .Where(!Exp.Eq("contact_id", 0))).ConvertAll(row => Convert.ToInt32(row[0])); if (ids.Count == 0) return null; break; } } if (contactStage >= 0) conditions.Add(Exp.Eq("status_id", contactStage)); if (contactType >= 0) conditions.Add(Exp.Eq("contact_type_id", contactType)); if (fromDate != DateTime.MinValue && toDate != DateTime.MinValue) conditions.Add(Exp.Between("create_on", TenantUtil.DateTimeToUtc(fromDate), TenantUtil.DateTimeToUtc(toDate.AddDays(1).AddMinutes(-1)))); else if (fromDate != DateTime.MinValue) conditions.Add(Exp.Ge("create_on", TenantUtil.DateTimeToUtc(fromDate))); else if (toDate != DateTime.MinValue) conditions.Add(Exp.Le("create_on", TenantUtil.DateTimeToUtc(toDate.AddDays(1).AddMinutes(-1)))); if (ids.Count > 0) { if (exceptIDs.Count > 0) { ids = ids.Except(exceptIDs).ToList(); if (ids.Count == 0) return null; } conditions.Add(Exp.In("id", ids)); } else if (exceptIDs.Count > 0) { conditions.Add(!Exp.In("id", exceptIDs.ToArray())); } if (conditions.Count == 0) return null; return conditions.Count == 1 ? conditions[0] : conditions.Aggregate((i, j) => i & j); }
private List<Contact> GetCrudeContacts( String searchText, IEnumerable<String> tags, int contactStage, int contactType, ContactListViewType contactListView, DateTime fromDate, DateTime toDate, int from, int count, OrderBy orderBy, Guid responsibleid = default(Guid)) { var sqlQuery = GetContactSqlQuery(null); var withParams = !(String.IsNullOrEmpty(searchText) && (tags == null || !tags.Any()) && contactStage <= 0 && contactType <= 0 && contactListView == ContactListViewType.All && responsibleid == default(Guid) && fromDate == DateTime.MinValue && toDate == DateTime.MinValue); var whereConditional = WhereConditional(new List<int>(), searchText, tags, contactStage, contactType, contactListView, fromDate, toDate, responsibleid); if (withParams && whereConditional == null) return new List<Contact>(); sqlQuery.Where(whereConditional); if (0 < from && from < int.MaxValue) sqlQuery.SetFirstResult(from); if (0 < count && count < int.MaxValue) sqlQuery.SetMaxResults(count); if (orderBy != null) { if (!Enum.IsDefined(typeof(ContactSortedByType), orderBy.SortedBy.ToString())) orderBy.SortedBy = ContactSortedByType.Created; switch ((ContactSortedByType)orderBy.SortedBy) { case ContactSortedByType.DisplayName: sqlQuery.OrderBy("display_name", orderBy.IsAsc); break; case ContactSortedByType.Created: sqlQuery.OrderBy("create_on", orderBy.IsAsc); break; case ContactSortedByType.ContactType: sqlQuery.OrderBy("status_id", orderBy.IsAsc); break; case ContactSortedByType.FirstName: sqlQuery.OrderBy("first_name", orderBy.IsAsc); sqlQuery.OrderBy("last_name", orderBy.IsAsc); break; case ContactSortedByType.LastName: sqlQuery.OrderBy("last_name", orderBy.IsAsc); sqlQuery.OrderBy("first_name", orderBy.IsAsc); break; default: sqlQuery.OrderBy("display_name", orderBy.IsAsc); break; } } using (var db = GetDb()) { return db.ExecuteList(sqlQuery).ConvertAll(contact => ToContact(contact)); } }
public IEnumerable<ContactWrapper> GetContacts(IEnumerable<String> tags, int contactStage, int contactType, ContactListViewType contactListView, ApiDateTime fromDate, ApiDateTime toDate) { IEnumerable<ContactWrapper> result; OrderBy contactsOrderBy; ContactSortedByType sortBy; var searchString = _context.FilterValue; if (Web.CRM.Classes.EnumExtension.TryParse(_context.SortBy, true, out sortBy)) { contactsOrderBy = new OrderBy(sortBy, !_context.SortDescending); } else if (String.IsNullOrEmpty(_context.SortBy)) { contactsOrderBy = new OrderBy(ContactSortedByType.Created, false); } else { contactsOrderBy = null; } var fromIndex = (int)_context.StartIndex; var count = (int)_context.Count; if (contactsOrderBy != null) { result = ToListContactWrapper(DaoFactory.GetContactDao() .GetContacts(searchString, tags, contactStage, contactType, contactListView, fromDate, toDate, fromIndex, count, contactsOrderBy)); _context.SetDataPaginated(); _context.SetDataFiltered(); _context.SetDataSorted(); } else { result = ToListContactWrapper(DaoFactory.GetContactDao() .GetContacts(searchString, tags, contactStage, contactType, contactListView, fromDate, toDate, 0, 0, null)); } int totalCount; if (result.Count() < count) { totalCount = fromIndex + result.Count(); } else { totalCount = DaoFactory.GetContactDao().GetContactsCount(searchString, tags, contactStage, contactType, contactListView, fromDate, toDate); } _context.SetTotalCount(totalCount); return result; }
public List<Contact> GetContacts(String searchText, IEnumerable<String> tags, int contactStatus, ContactListViewType contactListView, int from, int count, OrderBy orderBy) { if (CRMSecurity.IsAdmin) return GetCrudeContacts( searchText, tags, contactStatus, contactListView, from, count, orderBy); var crudeContacts = GetCrudeContacts( searchText, tags, contactStatus, contactListView, 0, from + count, orderBy); if (crudeContacts.Count == 0) return crudeContacts; if (crudeContacts.Count < from + count) return crudeContacts .FindAll(CRMSecurity.CanAccessTo).Skip(from).ToList(); var result = crudeContacts.FindAll(CRMSecurity.CanAccessTo); if (result.Count == crudeContacts.Count) return result.Skip(from).ToList(); var localCount = count; var localFrom = from + count; while (true) { crudeContacts = GetCrudeContacts( searchText, tags, contactStatus, contactListView, localFrom, localCount, orderBy); if (crudeContacts.Count == 0) break; result.AddRange(crudeContacts.Where(CRMSecurity.CanAccessTo)); if ((result.Count >= count + from) || (crudeContacts.Count < localCount)) break; localFrom += localCount; localCount = localCount * 2; } return result.Skip(from).Take(count).ToList(); }
public IEnumerable<ContactBaseWrapper> DeleteBatchContacts(IEnumerable<String> tags, int contactStage, int contactType, ContactListViewType contactListView, ApiDateTime fromDate, ApiDateTime toDate) { var contacts = DaoFactory.GetContactDao().GetContacts(_context.FilterValue, tags, contactStage, contactType, contactListView, fromDate, toDate, 0, 0, null); var result = contacts.Select(x => ToContactBaseWrapper(x)); DaoFactory.GetContactDao().DeleteBatchContact(contacts); return result; }
private List<Contact> GetCrudeContacts( String searchText, IEnumerable<String> tags, int contactStatus, ContactListViewType contactListView, int from, int count, OrderBy orderBy) { var sqlQuery = GetContactSqlQuery(null); var withParams = !(String.IsNullOrEmpty(searchText) && (tags == null || !tags.Any()) && contactStatus <= 0 && contactListView == ContactListViewType.All); var whereConditional = WhereConditional(new List<int>(), searchText, tags, contactStatus, contactListView); if (withParams && whereConditional == null) return new List<Contact>(); sqlQuery.Where(whereConditional); if (0 < from && from < int.MaxValue) sqlQuery.SetFirstResult(from); if (0 < count && count < int.MaxValue) sqlQuery.SetMaxResults(count); if (orderBy != null) { if (!Enum.IsDefined(typeof(ContactSortedByType), orderBy.SortedBy.ToString())) orderBy.SortedBy = ContactSortedByType.DisplayName; switch ((ContactSortedByType)orderBy.SortedBy) { case ContactSortedByType.DisplayName: sqlQuery.OrderBy("display_name", orderBy.IsAsc); break; case ContactSortedByType.Created: sqlQuery.OrderBy("create_on", orderBy.IsAsc); break; case ContactSortedByType.ContactType: sqlQuery.OrderBy("status_id", orderBy.IsAsc); break; default: sqlQuery.OrderBy("display_name", orderBy.IsAsc); break; } } return DbManager.ExecuteList(sqlQuery).ConvertAll(contact => ToContact(contact)); }
public int GetContactsCount(String searchText, IEnumerable<String> tags, int contactStage, int contactType, ContactListViewType contactListView, DateTime fromDate, DateTime toDate, Guid? responsibleid = null, bool? isShared = null) { var cacheKey = TenantID.ToString(CultureInfo.InvariantCulture) + "contacts" + SecurityContext.CurrentAccount.ID + searchText + contactStage + contactType + (int)contactListView + responsibleid + isShared; if (tags != null) cacheKey += String.Join("", tags.ToArray()); if (fromDate != DateTime.MinValue) cacheKey += fromDate.ToString(); if (toDate != DateTime.MinValue) cacheKey += toDate.ToString(); var fromCache = _cache.Get(cacheKey); if (fromCache != null) return Convert.ToInt32(fromCache); var withParams = HasSearchParams(searchText, tags, contactStage, contactType, contactListView, fromDate, toDate, responsibleid, isShared); int result; using (var db = GetDb()) { if (withParams) { ICollection<int> excludedContactIDs; switch (contactListView) { case ContactListViewType.Person: excludedContactIDs = CRMSecurity.GetPrivateItems(typeof(Person)) .Except(db.ExecuteList(Query("crm_contact").Select("id") .Where(Exp.Eq("is_shared", true) & Exp.Eq("is_company", false))) .Select(x => Convert.ToInt32(x[0]))).ToList(); break; case ContactListViewType.Company: excludedContactIDs = CRMSecurity.GetPrivateItems(typeof(Company)) .Except(db.ExecuteList(Query("crm_contact").Select("id") .Where(Exp.Eq("is_shared", true) & Exp.Eq("is_company", true))) .Select(x => Convert.ToInt32(x[0]))).ToList(); break; default: excludedContactIDs = CRMSecurity.GetPrivateItems(typeof(Company)).Union(CRMSecurity.GetPrivateItems(typeof(Person))) .Except(db.ExecuteList(Query("crm_contact").Select("id").Where("is_shared", true)) .Select(x => Convert.ToInt32(x[0]))).ToList(); break; } var whereConditional = WhereConditional(excludedContactIDs, searchText, tags, contactStage, contactType, contactListView, fromDate, toDate, responsibleid, isShared); if (whereConditional != null) { if (!isShared.HasValue) { var sqlQuery = Query("crm_contact").SelectCount().Where(whereConditional); result = db.ExecuteScalar<int>(sqlQuery); } else { var sqlQuery = Query("crm_contact").Select("id, is_company, is_shared").Where(whereConditional); var sqlResultRows = db.ExecuteList(sqlQuery); var resultContactsNewScheme_Count = sqlResultRows.Where(row => row[2] != null).ToList().Count; //new scheme var fakeContactsOldScheme = sqlResultRows .Where(row => row[2] == null).ToList() // old scheme .ConvertAll(row => Convert.ToBoolean(row[1]) == true ? new Company() {ID = Convert.ToInt32(row[0])} as Contact : new Person() {ID = Convert.ToInt32(row[0])} as Contact ); var resultFakeContactsOldScheme_Count = fakeContactsOldScheme.Where(fc => { var accessSubjectToContact = CRMSecurity.GetAccessSubjectTo(fc); if (isShared.Value == true) { return !accessSubjectToContact.Any(); } else { return accessSubjectToContact.Any(); } }).ToList().Count; return resultContactsNewScheme_Count + resultFakeContactsOldScheme_Count; } } else { result = 0; } } else { var countWithoutPrivate = db.ExecuteScalar<int>(Query("crm_contact").SelectCount()); var privateCount = CRMSecurity.GetPrivateItemsCount(typeof(Person)) + CRMSecurity.GetPrivateItemsCount(typeof(Company)) - db.ExecuteScalar<int>(Query("crm_contact").Where("is_shared", true).SelectCount()); if (privateCount < 0) privateCount = 0; if (privateCount > countWithoutPrivate) { _log.Error("Private contacts count more than all contacts"); privateCount = 0; } result = countWithoutPrivate - privateCount; } } if (result > 0) _cache.Insert(cacheKey, result, new CacheDependency(null, new[] { _contactCacheKey }), Cache.NoAbsoluteExpiration, TimeSpan.FromMinutes(1)); return result; }
public IEnumerable <ContactWrapper> GetContacts(IEnumerable <String> tags, int contactType, ContactListViewType contactListView) { IEnumerable <ContactWrapper> result; OrderBy contactsOrderBy; ContactSortedByType sortBy; var searchString = _context.FilterValue; if (ASC.Web.CRM.Classes.EnumExtension.TryParse(_context.SortBy, true, out sortBy)) { contactsOrderBy = new OrderBy(sortBy, !_context.SortDescending); } else if (String.IsNullOrEmpty(_context.SortBy)) { contactsOrderBy = new OrderBy(ContactSortedByType.DisplayName, true); } else { contactsOrderBy = null; } if (contactsOrderBy != null) { result = ToListContactWrapper(DaoFactory.GetContactDao() .GetContacts(searchString, tags, contactType, contactListView, (int)_context.StartIndex, (int)_context.Count, contactsOrderBy)); _context.SetDataPaginated(); _context.SetDataFiltered(); _context.SetDataSorted(); } else { result = ToListContactWrapper(DaoFactory.GetContactDao() .GetContacts(searchString, tags, contactType, contactListView, 0, 0, null)); } var totalCount = DaoFactory.GetContactDao().GetContactsCount(searchString, tags, contactType, contactListView); _context.SetTotalCount(totalCount); return(result.ToSmartList()); }
public List<Contact> GetContacts(String searchText, IEnumerable<String> tags, int contactStage, int contactType, ContactListViewType contactListView, DateTime fromDate, DateTime toDate, int from, int count, OrderBy orderBy, Guid? responsibleId = null, bool? isShared = null) { if (CRMSecurity.IsAdmin) { if (!isShared.HasValue) { return GetCrudeContacts( searchText, tags, contactStage, contactType, contactListView, fromDate, toDate, from, count, orderBy, responsibleId, isShared, false); } else { var crudeContacts = GetCrudeContacts( searchText, tags, contactStage, contactType, contactListView, fromDate, toDate, 0, from + count, orderBy, responsibleId, isShared, false); if (crudeContacts.Count == 0) return crudeContacts; var result = crudeContacts.Where(c => (isShared.Value == true ? c.ShareType != ShareType.None : c.ShareType == ShareType.None)).ToList(); if (result.Count == crudeContacts.Count) return result.Skip(from).ToList(); var localCount = count; var localFrom = from + count; while (true) { crudeContacts = GetCrudeContacts( searchText, tags, contactStage, contactType, contactListView, fromDate, toDate, localFrom, localCount, orderBy, responsibleId, isShared, false); if (crudeContacts.Count == 0) break; result.AddRange(crudeContacts.Where(c => (isShared.Value == true ? c.ShareType != ShareType.None : c.ShareType == ShareType.None))); if ((result.Count >= count + from) || (crudeContacts.Count < localCount)) break; localFrom += localCount; localCount = localCount * 2; } return result.Skip(from).Take(count).ToList(); } } else { var crudeContacts = GetCrudeContacts( searchText, tags, contactStage, contactType, contactListView, fromDate, toDate, 0, from + count, orderBy, responsibleId, isShared, false); if (crudeContacts.Count == 0) return crudeContacts; var tmp = isShared.HasValue ? crudeContacts.Where(c => (isShared.Value == true ? c.ShareType != ShareType.None : c.ShareType == ShareType.None)).ToList() : crudeContacts; if (crudeContacts.Count < from + count) { return tmp.FindAll(CRMSecurity.CanAccessTo).Skip(from).ToList(); } var result = tmp.FindAll(CRMSecurity.CanAccessTo); if (result.Count == crudeContacts.Count) return result.Skip(from).ToList(); var localCount = count; var localFrom = from + count; while (true) { crudeContacts = GetCrudeContacts( searchText, tags, contactStage, contactType, contactListView, fromDate, toDate, localFrom, localCount, orderBy, responsibleId, isShared, false); if (crudeContacts.Count == 0) break; tmp = isShared.HasValue ? crudeContacts.Where(c => (isShared.Value == true ? c.ShareType != ShareType.None : c.ShareType == ShareType.None)).ToList() : crudeContacts; result.AddRange(tmp.Where(CRMSecurity.CanAccessTo)); if ((result.Count >= count + from) || (crudeContacts.Count < localCount)) break; localFrom += localCount; localCount = localCount * 2; } return result.Skip(from).Take(count).ToList(); } }
public IEnumerable<ContactWrapper> SetAccessToBatchContact( IEnumerable<String> tags, int? contactStage, int? contactType, ContactListViewType contactListView, ApiDateTime fromDate, ApiDateTime toDate, bool isPrivate, IEnumerable<Guid> managerList ) { int contactStageInt = contactStage.HasValue ? contactStage.Value : -1; int contactTypeInt = contactType.HasValue ? contactType.Value : -1; var result = new List<Contact>(); var contacts = DaoFactory.GetContactDao().GetContacts( _context.FilterValue, tags, contactStageInt, contactTypeInt, contactListView, fromDate, toDate, 0, 0, null); if (!contacts.Any()) return Enumerable.Empty<ContactWrapper>(); foreach (var contact in contacts) { if (contact == null) throw new ItemNotFoundException(); if (!CRMSecurity.CanEdit(contact)) continue; SetAccessToContact(contact, isPrivate, managerList, false); result.Add(contact); } return ToListContactWrapper(result); }
private bool HasSearchParams(String searchText, IEnumerable<String> tags, int contactStage, int contactType, ContactListViewType contactListView, DateTime fromDate, DateTime toDate, Guid? responsibleid = null, bool? isShared = null) { var hasNoParams = String.IsNullOrEmpty(searchText) && (tags == null || !tags.Any()) && contactStage < 0 && contactType < 0 && contactListView == ContactListViewType.All && !isShared.HasValue && fromDate == DateTime.MinValue && toDate == DateTime.MinValue && !responsibleid.HasValue; return !hasNoParams; }
public IEnumerable<ContactBaseWrapper> DeleteBatchContacts( IEnumerable<String> tags, int? contactStage, int? contactType, ContactListViewType contactListView, ApiDateTime fromDate, ApiDateTime toDate) { int contactStageInt = contactStage.HasValue ? contactStage.Value : -1; int contactTypeInt = contactType.HasValue ? contactType.Value : -1; var contacts = DaoFactory.GetContactDao().GetContacts( _context.FilterValue, tags, contactStageInt, contactTypeInt, contactListView, fromDate, toDate, 0, 0, null); contacts = DaoFactory.GetContactDao().DeleteBatchContact(contacts); MessageService.Send(Request, MessageAction.ContactsDeleted, contacts.Select(c => c.ID.ToString(CultureInfo.InvariantCulture))); return contacts.Select(ToContactBaseWrapper); }
private List<Contact> GetCrudeContacts( String searchText, IEnumerable<String> tags, int contactStage, int contactType, ContactListViewType contactListView, DateTime fromDate, DateTime toDate, int from, int count, OrderBy orderBy, Guid? responsibleid = null, bool? isShared = null, bool selectIsSharedInNewScheme = true) { var sqlQuery = GetContactSqlQuery(null); var withParams = HasSearchParams(searchText, tags, contactStage, contactType, contactListView, fromDate, toDate, responsibleid, isShared); var whereConditional = WhereConditional(new List<int>(), searchText, tags, contactStage, contactType, contactListView, fromDate, toDate, responsibleid, isShared); if (withParams && whereConditional == null) return new List<Contact>(); sqlQuery.Where(whereConditional); if (0 < from && from < int.MaxValue) sqlQuery.SetFirstResult(from); if (0 < count && count < int.MaxValue) sqlQuery.SetMaxResults(count); if (orderBy != null) { if (!Enum.IsDefined(typeof(ContactSortedByType), orderBy.SortedBy.ToString())) orderBy.SortedBy = ContactSortedByType.Created; switch ((ContactSortedByType)orderBy.SortedBy) { case ContactSortedByType.DisplayName: sqlQuery.OrderBy("display_name", orderBy.IsAsc); break; case ContactSortedByType.Created: sqlQuery.OrderBy("create_on", orderBy.IsAsc); break; case ContactSortedByType.ContactType: sqlQuery.OrderBy("status_id", orderBy.IsAsc); break; case ContactSortedByType.FirstName: sqlQuery.OrderBy("first_name", orderBy.IsAsc); sqlQuery.OrderBy("last_name", orderBy.IsAsc); break; case ContactSortedByType.LastName: sqlQuery.OrderBy("last_name", orderBy.IsAsc); sqlQuery.OrderBy("first_name", orderBy.IsAsc); break; default: sqlQuery.OrderBy("display_name", orderBy.IsAsc); break; } } using (var db = GetDb()) { var contacts = db.ExecuteList(sqlQuery).ConvertAll(ToContact); return selectIsSharedInNewScheme && isShared.HasValue ? contacts.Where(c => (isShared.Value ? c.ShareType != ShareType.None : c.ShareType == ShareType.None)).ToList() : contacts; } }