public static IEnumerable<int> FindAssociatedOrgUnitIds(int? organizationalUnitId, DescendantOption descentantOption, LinkedOption linkedOption, IOperationContext operationContext, ObjectContext objectContext) { return AssociatedOrgUnitFinder.FindAssociatedOrgUnits(objectContext, new AssociatedOrgUnitsDto { OrganizationalUnitId = organizationalUnitId, DescendantOption = descentantOption, LinkedOption = linkedOption }); }
public static IEnumerable<int> FindAssociatedOrgUnitIds(int? organizationalUnitId, DescendantOption descentantOption, LinkedOption linkedOption, IOperationContext operationContext, ObjectContext objectContext) { var key = string.Concat("AssociatedOrgUnitIds_", organizationalUnitId, "_", descentantOption, "_", linkedOption); //return operationContext.FindOrCreate<IEnumerable<int>>(key, () => //{ return AssociatedOrgUnitFinder.FindAssociatedOrgUnits(objectContext, new AssociatedOrgUnitsDto { OrganizationalUnitId = organizationalUnitId, DescendantOption = descentantOption, LinkedOption = linkedOption }); //}); }
/// <summary> /// Adds the org unit conditions. /// </summary> /// <param name="objectSet">The object set.</param> /// <param name="orgUnitContextId"></param> /// <param name="descendantOption"></param> /// <param name="linkedOption"></param> /// <returns></returns> public static IQueryable<OrgUnitCacheView> AddOrgUnitContextConditions(this IQueryable<OrgUnitCacheView> objectSet, int? orgUnitContextId, DescendantOption descendantOption, LinkedOption linkedOption) { if (orgUnitContextId.HasValue && orgUnitContextId.Value > 0) { var orgUnitIdFormatted = "-" + orgUnitContextId.Value.ToString(CultureInfo.InvariantCulture) + "-"; // Limit org units to those within current org unit context if (descendantOption == DescendantOption.NoDescendants && linkedOption == LinkedOption.NoLinkedUnits) objectSet = objectSet.Where(o => o.DirectOrgUnits != null && o.DirectOrgUnits.Contains(orgUnitIdFormatted)); else if (descendantOption == DescendantOption.IncludeDescendants && linkedOption == LinkedOption.NoLinkedUnits) objectSet = objectSet.Where(o => o.DirectAndParentOrgUnits != null && o.DirectAndParentOrgUnits.Contains(orgUnitIdFormatted)); else if (descendantOption == DescendantOption.NoDescendants && linkedOption == LinkedOption.IncludeLinkedUnits) objectSet = objectSet.Where(o => o.DirectAndLinkedOrgUnits != null && o.DirectAndLinkedOrgUnits.Contains(orgUnitIdFormatted)); else if (descendantOption == DescendantOption.IncludeDescendants && linkedOption == LinkedOption.IncludeLinkedUnits) objectSet = objectSet.Where(o => o.DirectAndParentAndLinkedOrgUnits != null && o.DirectAndParentAndLinkedOrgUnits.Contains(orgUnitIdFormatted)); else objectSet = objectSet.Where(o => o.DirectAndParentOrgUnits != null && o.DirectAndParentOrgUnits.Contains(orgUnitIdFormatted)); } return objectSet; }
/// <summary> /// Retrieves the provider cache data. /// </summary> /// <returns></returns> private IEnumerable<ProviderCacheView> GetData(int? orgUnitContextId, string orgUnitContextKey, DescendantOption descendantOption, LinkedOption linkedOption) { string cacheKey = _cacheKeyPrefix + orgUnitContextKey; string cacheLastRefreshDateKey = _cacheLastRefreshDateKeyPrefix + orgUnitContextKey; lock (_getDataLock) { if (_cache.Contains(cacheLastRefreshDateKey)) { // Cache has been populated previously so do async refresh and returned previously cached data RefreshCacheDataAsync(orgUnitContextId, descendantOption, linkedOption, cacheKey, cacheLastRefreshDateKey); return (IEnumerable<ProviderCacheView>)_cache.Get(cacheKey); } else { if (_cache.Contains(_noContextCacheLastRefreshDateKey)) { // Only unfiltered data exists in cache, so filter and return it after initiating an async refresh of filtered data RefreshCacheDataAsync(orgUnitContextId, descendantOption, linkedOption, cacheKey, cacheLastRefreshDateKey); return ((IEnumerable<ProviderCacheView>)_cache.Get(_noContextCacheKey)) .AsQueryable() .AddOrgUnitContextConditions(orgUnitContextId, descendantOption, linkedOption) .ToArray(); } else { // Cache has never been populated so refresh immediately and return return RefreshCacheData(orgUnitContextId, descendantOption, linkedOption, cacheKey, cacheLastRefreshDateKey, _objectContext); } } } }
/// <summary> /// If expired, refreshes the cache data in an asynchronous fashion. /// </summary> private void RefreshCacheDataAsync(int? orgUnitContextId, DescendantOption descendantOption, LinkedOption linkedOption, string cacheKey, string cacheLastRefreshDateKey) { ObjectContext objectContextForThread = null; int cacheDurationSeconds = int.Parse(_settingsManager.GetString(Common.Constants.ApplicationId, Common.Constants.BusinessCacheDurationKey, CacheSeconds.Large.ToString(CultureInfo.InvariantCulture)), CultureInfo.InvariantCulture); var lastRefreshDate = _cache.Get(cacheLastRefreshDateKey) != null ? (DateTime)_cache.Get(cacheLastRefreshDateKey) : DateTime.MinValue; var cacheIsExpired = ((DateTime.Now - lastRefreshDate).TotalSeconds >= cacheDurationSeconds); if (cacheIsExpired && !_isRefreshingCache) { try { _isRefreshingCache = true; // Initialize a new object context for use by new thread objectContextForThread = new ObjectContext(_objectContext.Connection.ConnectionString); objectContextForThread.DefaultContainerName = _objectContext.DefaultContainerName; ThreadStart starter = delegate { RefreshCacheData(orgUnitContextId, descendantOption, linkedOption, cacheKey, cacheLastRefreshDateKey, objectContextForThread); }; new Thread(starter).Start(); } catch(Exception) { _isRefreshingCache = false; } } }
private IEnumerable<ProviderCacheView> RefreshCacheData(int? orgUnitContextId, DescendantOption descendantOption, LinkedOption linkedOption, string cacheKey, string cacheLastRefreshDateKey, ObjectContext objectContext) { try { _logger.Debug("Refreshing Provider Cache from Data Source"); var cacheData = new ProviderDataCacheSource(objectContext).GetData(); _logger.Debug("Provider Cache was populated", "Provider Count", cacheData.Count().ToString(CultureInfo.InvariantCulture)); if (orgUnitContextId.HasValue) { cacheData = cacheData.AsQueryable() .AddOrgUnitContextConditions(orgUnitContextId.Value, descendantOption, linkedOption) .ToArray(); _logger.Debug("Provider Cache was filtered by Org Unit Context", "Provider Count", cacheData.Count().ToString(CultureInfo.InvariantCulture), "Org Unit ID", orgUnitContextId.Value.ToString(CultureInfo.InvariantCulture)); } _cache.Put(cacheKey, cacheData, new TimeSpan(365, 0, 0, 0)); _cache.Put(cacheLastRefreshDateKey, DateTime.Now, new TimeSpan(365, 0, 0, 0)); ReloadDynamicColumnSettings(objectContext); return cacheData; } catch (Exception ex) { _logger.Error(ex.Message, ex); return new List<ProviderCacheView>(); } finally { _isRefreshingCache = false; } }
/// <summary> /// Adds the org unit conditions. /// </summary> /// <param name="objectSet">The object set.</param> /// <param name="orgUnitContextId"></param> /// <param name="descendantOption"></param> /// <param name="linkedOption"></param> /// <returns></returns> public static IQueryable<EventCacheView> AddOrgUnitContextConditions(this IQueryable<EventCacheView> objectSet, int? orgUnitContextId, DescendantOption descendantOption, LinkedOption linkedOption) { if (orgUnitContextId.HasValue && orgUnitContextId.Value > 0) { var orgUnitIdFormatted = "-" + orgUnitContextId.Value.ToString(CultureInfo.InvariantCulture) + "-"; // Limit events to those within current org unit context if (descendantOption == DescendantOption.NoDescendants && linkedOption == LinkedOption.NoLinkedUnits) objectSet = objectSet.Where(o => o.DirectOrgUnits != null && o.DirectOrgUnits.Contains(orgUnitIdFormatted)); else if (descendantOption == DescendantOption.IncludeDescendants && linkedOption == LinkedOption.NoLinkedUnits) objectSet = objectSet.Where(o => o.DirectAndParentOrgUnits != null && o.DirectAndParentOrgUnits.Contains(orgUnitIdFormatted)); else if (descendantOption == DescendantOption.NoDescendants && linkedOption == LinkedOption.IncludeLinkedUnits) objectSet = objectSet.Where(o => o.DirectAndLinkedOrgUnits != null && o.DirectAndLinkedOrgUnits.Contains(orgUnitIdFormatted)); else if (descendantOption == DescendantOption.IncludeDescendants && linkedOption == LinkedOption.IncludeLinkedUnits) objectSet = objectSet.Where(o => o.DirectAndParentAndLinkedOrgUnits != null && o.DirectAndParentAndLinkedOrgUnits.Contains(orgUnitIdFormatted)); else objectSet = objectSet.Where(o => o.DirectAndParentOrgUnits != null && o.DirectAndParentOrgUnits.Contains(orgUnitIdFormatted)); } objectSet = objectSet.Where(e => e.Occurrences.Any(o => o.OrgUnit != null && (o.OrgUnit.OrgUnitTypes.Count == 0 || o.OrgUnit.OrgUnitTypes.Any(t => !t.IsOutsideOfOrganization)))); return objectSet; }
private static List<int> GetLinkedUnits(List<int> orgUnitIds, LinkedOption option, ObjectSet<OrgUnitAssociationPublished> orgUnitAssociationPublished, ObjectSet<OrgUnitPublished> orgUnitPublished) { List<int> applicableUnits = new List<int>(); //add the linked units var linkedUnits = GetAnyLinkedUnits(orgUnitPublished, orgUnitIds); AddUnitsToList(ref applicableUnits, linkedUnits); //get descendant units of the linked units and their linked units if (option == LinkedOption.IncludeLinkedUnitsAndTheirDescendants) { //add the descendant units of the linked units var subDescendantUnits = GetAnyDescendantUnits(orgUnitAssociationPublished, linkedUnits); AddUnitsToList(ref applicableUnits, subDescendantUnits); if (subDescendantUnits.Count() > 0) { //recursively travers the graph to load all linked units (and their respective descendant units) of the descendant units var newUnits = GetLinkedUnits(subDescendantUnits, option, orgUnitAssociationPublished, orgUnitPublished); AddUnitsToList(ref applicableUnits, newUnits); } } return applicableUnits.ToList(); }