public static List <Portlet> FindPortlets(this Portlet portlet) { if (string.IsNullOrWhiteSpace(portlet?.ID) || !portlet.ID.IsValidUUID() || !string.IsNullOrWhiteSpace(portlet.OriginalPortletID)) { return(new List <Portlet>()); } var filter = Filters <Portlet> .Equals("OriginalPortletID", portlet.ID); var sort = Sorts <Portlet> .Ascending("DesktopID").ThenByAscending("Zone").ThenByAscending("OrderIndex"); return(Portlet.Find(filter, sort, 0, 1, Extensions.GetCacheKey(filter, sort))); }
private async IAsyncEnumerable <EventCommit> Enumerate(FilterDefinition <EventCommit> query, EnumerateDirection direction, int?limit, [EnumeratorCancellation] CancellationToken token) { FindOptions options; if (DeactivateTimeoutOnRead) { options = new FindOptions() { NoCursorTimeout = true, MaxTime = TimeSpan.MaxValue, MaxAwaitTime = TimeSpan.MaxValue, }; } else { options = new FindOptions(); } SortDefinition <EventCommit> sort; if (direction == EnumerateDirection.Ascending) { sort = Sorts.Ascending(x => x.Ordinal); } else { sort = Sorts.Descending(x => x.Ordinal); } var find = Collection .Find(query, options) .Sort(sort); if (limit.HasValue && limit > 0) { find = find.Limit(limit); } IAsyncCursor <EventCommit> cursor = await find .ToCursorAsync(token); while (await cursor.MoveNextAsync(token) && !token.IsCancellationRequested) { foreach (EventCommit item in cursor.Current) { token.ThrowIfCancellationRequested(); yield return(item); } } }
public static Task <List <Portlet> > FindPortletsAsync(this Portlet portlet, CancellationToken cancellationToken = default) { if (string.IsNullOrWhiteSpace(portlet?.ID) || !portlet.ID.IsValidUUID() || !string.IsNullOrWhiteSpace(portlet.OriginalPortletID)) { return(Task.FromResult(new List <Portlet>())); } var filter = Filters <Portlet> .Equals("OriginalPortletID", portlet.ID); var sort = Sorts <Portlet> .Ascending("DesktopID").ThenByAscending("Zone").ThenByAscending("OrderIndex"); return(Portlet.FindAsync(filter, sort, 0, 1, Extensions.GetCacheKey(filter, sort), cancellationToken)); }
public static List <Role> FindRoles(this string systemID, string parentID, bool updateCache = true) { if (string.IsNullOrWhiteSpace(systemID)) { return(new List <Role>()); } var filter = RoleProcessor.GetRolesFilter(systemID, parentID); var sort = Sorts <Role> .Ascending("Title"); var roles = Role.Find(filter, sort, 0, 1, Extensions.GetCacheKey(filter, sort, 0, 1)); roles.ForEach(role => role.Set(updateCache)); return(roles); }
public static List <Expression> FindExpressions(this string systemID, string repositoryID = null, string repositoryEntityID = null, string contentTypeDefinitionID = null, bool updateCache = true) { if (string.IsNullOrWhiteSpace(systemID)) { return(new List <Expression>()); } var filter = systemID.GetExpressionsFilter(repositoryID, repositoryEntityID, contentTypeDefinitionID); var sort = Sorts <Expression> .Ascending("Title"); var expressions = Expression.Find(filter, sort, 0, 1, Extensions.GetCacheKey(filter, sort, 0, 1)); expressions.ForEach(expression => expression.Set(updateCache)); return(expressions); }
public static async Task <List <Role> > FindRolesAsync(this string systemID, string parentID, CancellationToken cancellationToken = default, bool updateCache = true) { if (string.IsNullOrWhiteSpace(systemID)) { return(new List <Role>()); } var filter = RoleProcessor.GetRolesFilter(systemID, parentID); var sort = Sorts <Role> .Ascending("Title"); var roles = await Role.FindAsync(filter, sort, 0, 1, Extensions.GetCacheKey(filter, sort, 0, 1), cancellationToken).ConfigureAwait(false); await roles.ForEachAsync((role, token) => role.SetAsync(updateCache, token), cancellationToken).ConfigureAwait(false); return(roles); }
public static async Task <List <Expression> > FindExpressionsAsync(this string systemID, string repositoryID = null, string repositoryEntityID = null, string contentTypeDefinitionID = null, CancellationToken cancellationToken = default, bool updateCache = true) { if (string.IsNullOrWhiteSpace(systemID)) { return(new List <Expression>()); } var filter = systemID.GetExpressionsFilter(repositoryID, repositoryEntityID, contentTypeDefinitionID); var sort = Sorts <Expression> .Ascending("Title"); var expressions = await Expression.FindAsync(filter, sort, 0, 1, Extensions.GetCacheKey(filter, sort, 0, 1), cancellationToken).ConfigureAwait(false); await expressions.ForEachAsync((expression, token) => expression.SetAsync(updateCache, token), cancellationToken).ConfigureAwait(false); return(expressions); }
internal static async Task ClearRelatedCacheAsync(this ContentType contentType, CancellationToken cancellationToken, string correlationID = null, bool clearDataCache = true, bool clearHtmlCache = true, bool doRefresh = true) { // data cache keys var sort = Sorts <ContentType> .Ascending("Title"); var dataCacheKeys = clearDataCache ? Extensions.GetRelatedCacheKeys(Filters <ContentType> .And(), sort) .Concat(Extensions.GetRelatedCacheKeys(ContentTypeProcessor.GetContentTypesFilter(contentType.SystemID), sort)) .Concat(Extensions.GetRelatedCacheKeys(ContentTypeProcessor.GetContentTypesFilter(contentType.SystemID, contentType.RepositoryID, contentType.ContentTypeDefinitionID), sort)) .Concat(Extensions.GetRelatedCacheKeys(ContentTypeProcessor.GetContentTypesFilter(contentType.SystemID, contentType.RepositoryID, null), sort)) .Concat(Extensions.GetRelatedCacheKeys(ContentTypeProcessor.GetContentTypesFilter(contentType.SystemID, null, contentType.ContentTypeDefinitionID), sort)) .Concat(await Utility.Cache.GetSetMembersAsync(contentType.GetSetCacheKey(), cancellationToken).ConfigureAwait(false)) .Concat(new[] { contentType.GetSetCacheKey() }) .Distinct(StringComparer.OrdinalIgnoreCase) .ToList() : new List <string>(); // html cache keys (desktop HTMLs) var htmlCacheKeys = new List <string>(); if (clearHtmlCache) { htmlCacheKeys = contentType.Organization?.GetDesktopCacheKey() ?? new List <string>(); await new[] { contentType.Desktop?.GetSetCacheKey() } .Concat(await contentType.GetSetCacheKeysAsync(cancellationToken).ConfigureAwait(false) ?? new List <string>()) .Where(id => !string.IsNullOrWhiteSpace(id)) .Distinct(StringComparer.OrdinalIgnoreCase) .ToList() .ForEachAsync(async desktopSetCacheKey => { var cacheKeys = await Utility.Cache.GetSetMembersAsync(desktopSetCacheKey, cancellationToken).ConfigureAwait(false); if (cacheKeys != null && cacheKeys.Count > 0) { htmlCacheKeys = htmlCacheKeys.Concat(cacheKeys).Concat(new[] { desktopSetCacheKey }).ToList(); } }, true, false).ConfigureAwait(false); } htmlCacheKeys = htmlCacheKeys.Distinct(StringComparer.OrdinalIgnoreCase).ToList(); // clear related cache await Utility.Cache.RemoveAsync(htmlCacheKeys.Concat(dataCacheKeys).Distinct(StringComparer.OrdinalIgnoreCase).ToList(), cancellationToken).ConfigureAwait(false); await Task.WhenAll ( Utility.WriteCacheLogs?Utility.WriteLogAsync(correlationID, $"Clear related cache of a content-type [{contentType.Title} - ID: {contentType.ID}]\r\n- {dataCacheKeys.Count} data keys => {dataCacheKeys.Join(", ")}\r\n- {htmlCacheKeys.Count} html keys => {htmlCacheKeys.Join(", ")}", ServiceBase.ServiceComponent.CancellationToken, "Caches") : Task.CompletedTask, doRefresh?$"{Utility.PortalsHttpURI}/~{contentType.Organization.Alias}/".RefreshWebPageAsync(1, correlationID, $"Refresh desktop when related cache of a content-type was clean [{contentType.Title} - ID: {contentType.ID}]") : Task.CompletedTask ).ConfigureAwait(false); }
internal static Task ClearRelatedCacheAsync(this Role role, string oldParentID, CancellationToken cancellationToken, string correlationID = null) { var sort = Sorts <Role> .Ascending("Title"); var dataCacheKeys = Extensions.GetRelatedCacheKeys(RoleProcessor.GetRolesFilter(role.SystemID), sort); if (!string.IsNullOrWhiteSpace(role.ParentID) && role.ParentID.IsValidUUID()) { dataCacheKeys = Extensions.GetRelatedCacheKeys(RoleProcessor.GetRolesFilter(role.SystemID, role.ParentID), sort).Concat(dataCacheKeys).ToList(); } if (!string.IsNullOrWhiteSpace(oldParentID) && oldParentID.IsValidUUID()) { dataCacheKeys = Extensions.GetRelatedCacheKeys(RoleProcessor.GetRolesFilter(role.SystemID, oldParentID), sort).Concat(dataCacheKeys).ToList(); } dataCacheKeys = dataCacheKeys.Distinct(StringComparer.OrdinalIgnoreCase).ToList(); if (Utility.WriteCacheLogs) { Utility.WriteLogAsync(correlationID, $"Clear related cache of role [{role.ID} => {role.Title}]\r\n{dataCacheKeys.Count} keys => {dataCacheKeys.Join(", ")}", ServiceBase.ServiceComponent.CancellationToken, "Caches").Run(); } return(Utility.Cache.RemoveAsync(dataCacheKeys, cancellationToken)); }
public static async Task <List <ContentType> > FindContentTypesAsync(this string systemID, string repositoryID = null, string definitionID = null, CancellationToken cancellationToken = default, bool updateCache = true) { if (string.IsNullOrWhiteSpace(systemID)) { return(new List <ContentType>()); } var filter = ContentTypeProcessor.GetContentTypesFilter(systemID, repositoryID, definitionID); var sort = Sorts <ContentType> .Ascending("Title"); var contentTypes = await ContentType.FindAsync(filter, sort, 0, 1, Extensions.GetCacheKey(filter, sort, 0, 1), cancellationToken).ConfigureAwait(false); contentTypes.ForEach(contentType => { if (contentType.ID.GetContentTypeByID(false, false) == null) { contentType.Set(updateCache); } }); return(contentTypes); }
public static List <Module> FindModules(this string systemID, string definitionID = null, bool updateCache = true) { if (string.IsNullOrWhiteSpace(systemID)) { return(new List <Module>()); } var filter = ModuleProcessor.GetModulesFilter(systemID, definitionID); var sort = Sorts <Module> .Ascending("Title"); var modules = Module.Find(filter, sort, 0, 1, Extensions.GetCacheKey(filter, sort, 0, 1)); modules.ForEach(module => { if (module.ID.GetModuleByID(false, false) == null) { module.Set(updateCache); } }); return(modules); }
public static List <ContentType> FindContentTypes(this string systemID, string repositoryID = null, string definitionID = null, bool updateCache = true) { if (string.IsNullOrWhiteSpace(systemID)) { return(new List <ContentType>()); } var filter = ContentTypeProcessor.GetContentTypesFilter(systemID, repositoryID, definitionID); var sort = Sorts <ContentType> .Ascending("Title"); var contentTypes = ContentType.Find(filter, sort, 0, 1, Extensions.GetCacheKey(filter, sort, 0, 1)); contentTypes.ForEach(contentType => { if (contentType.ID.GetContentTypeByID(false, false) == null) { contentType.Set(updateCache); } }); return(contentTypes); }
internal static async Task ClearRelatedCacheAsync(this Organization organization, CancellationToken cancellationToken, string correlationID = null, bool clearDataCache = true, bool clearHtmlCache = true, bool doRefresh = true) { // data cache keys var dataCacheKeys = clearDataCache ? Extensions.GetRelatedCacheKeys(Filters <Organization> .And(), Sorts <Organization> .Ascending("Title")) .Concat(Extensions.GetRelatedCacheKeys(Filters <Organization> .And(Filters <Organization> .Equals("OwnerID", organization.OwnerID)), Sorts <Organization> .Ascending("Title"))) .Distinct(StringComparer.OrdinalIgnoreCase) .ToList() : new List <string>(); // html cache keys (desktop HTMLs) var htmlCacheKeys = clearHtmlCache ? organization.GetDesktopCacheKey().Concat(new[] { $"js#o_{organization.ID}", $"js#o_{organization.ID}:time" }).ToList() : new List <string>(); // clear related cache await Utility.Cache.RemoveAsync(htmlCacheKeys.Concat(dataCacheKeys).Distinct(StringComparer.OrdinalIgnoreCase).ToList(), cancellationToken).ConfigureAwait(false); await Task.WhenAll ( Utility.WriteCacheLogs?Utility.WriteLogAsync(correlationID, $"Clear related cache of an organization [{organization.Title} - ID: {organization.ID}]\r\n- {dataCacheKeys.Count} data keys => {dataCacheKeys.Join(", ")}\r\n- {htmlCacheKeys.Count} html keys => {htmlCacheKeys.Join(", ")}", ServiceBase.ServiceComponent.CancellationToken, "Caches") : Task.CompletedTask, doRefresh?$"{Utility.PortalsHttpURI}/~{organization.Alias}/".RefreshWebPageAsync(1, correlationID, $"Refresh desktop when related cache of an organization was clean [{organization.Title} - ID: {organization.ID}]") : Task.CompletedTask ).ConfigureAwait(false); }
internal static async Task ClearRelatedCacheAsync(this Expression expression, CancellationToken cancellationToken, string correlationID = null, bool clearDataCache = true, bool clearHtmlCache = true, bool doRefresh = true) { // data cache keys var sort = Sorts <Expression> .Ascending("Title"); var dataCacheKeys = clearDataCache ? Extensions.GetRelatedCacheKeys(Filters <Expression> .And(), sort) .Concat(Extensions.GetRelatedCacheKeys(expression.SystemID.GetExpressionsFilter(null), sort)) .Concat(Extensions.GetRelatedCacheKeys(expression.SystemID.GetExpressionsFilter(expression.RepositoryID), sort)) .Concat(Extensions.GetRelatedCacheKeys(expression.SystemID.GetExpressionsFilter(expression.RepositoryID, expression.RepositoryEntityID, expression.ContentTypeDefinitionID), sort)) .Concat(Extensions.GetRelatedCacheKeys(expression.SystemID.GetExpressionsFilter(expression.RepositoryID, expression.RepositoryEntityID, null), sort)) .Concat(Extensions.GetRelatedCacheKeys(expression.SystemID.GetExpressionsFilter(expression.RepositoryID, null, expression.ContentTypeDefinitionID), sort)) .ToList() : new List <string>(); if (clearDataCache) { if (!string.IsNullOrWhiteSpace(expression.RepositoryEntityID) && !string.IsNullOrWhiteSpace(expression.ContentTypeDefinitionID)) { var filter = Filters <Expression> .Or ( Filters <Expression> .Equals("ContentTypeDefinitionID", expression.ContentTypeDefinitionID), Filters <Expression> .Equals("RepositoryEntityID", expression.RepositoryEntityID) ); dataCacheKeys = dataCacheKeys.Concat(Extensions.GetRelatedCacheKeys(filter, sort)) .Concat(Extensions.GetRelatedCacheKeys(Filters <Expression> .And(Filters <Expression> .Equals("RepositoryID", expression.RepositoryID), filter), sort)) .ToList(); } if (expression.ContentType != null) { dataCacheKeys = dataCacheKeys.Concat(await Utility.Cache.GetSetMembersAsync(expression.ContentType.GetSetCacheKey(), cancellationToken).ConfigureAwait(false)).ToList(); } } dataCacheKeys = dataCacheKeys.Distinct(StringComparer.OrdinalIgnoreCase).ToList(); // html cache keys (desktop HTMLs) var htmlCacheKeys = new List <string>(); if (clearHtmlCache) { var desktopSetCacheKeys = await Utility.GetSetCacheKeysAsync(Filters <Portlet> .And(Filters <Portlet> .Equals("ExpressionID", expression.ID), Filters <Portlet> .IsNull("OriginalPortletID")), cancellationToken).ConfigureAwait(false); await desktopSetCacheKeys.ForEachAsync(async desktopSetCacheKey => { var cacheKeys = await Utility.Cache.GetSetMembersAsync(desktopSetCacheKey, cancellationToken).ConfigureAwait(false); if (cacheKeys != null && cacheKeys.Count > 0) { htmlCacheKeys = htmlCacheKeys.Concat(cacheKeys).Concat(new[] { desktopSetCacheKey }).ToList(); } }, true, false).ConfigureAwait(false); } htmlCacheKeys = htmlCacheKeys.Distinct(StringComparer.OrdinalIgnoreCase).ToList(); // clear related cache await Utility.Cache.RemoveAsync(htmlCacheKeys.Concat(dataCacheKeys).Distinct(StringComparer.OrdinalIgnoreCase).ToList(), cancellationToken).ConfigureAwait(false); await Task.WhenAll ( Utility.WriteCacheLogs?Utility.WriteLogAsync(correlationID, $"Clear related cache of an expression [{expression.Title} - ID: {expression.ID}]\r\n- {dataCacheKeys.Count} data keys => {dataCacheKeys.Join(", ")}\r\n- {htmlCacheKeys.Count} html keys => {htmlCacheKeys.Join(", ")}", ServiceBase.ServiceComponent.CancellationToken, "Caches") : Task.CompletedTask, doRefresh?$"{Utility.PortalsHttpURI}/~{expression.Organization.Alias}/".RefreshWebPageAsync(1, correlationID, $"Refresh desktop when related cache of an expression was clean [{expression.Title} - ID: {expression.ID}]") : Task.CompletedTask ).ConfigureAwait(false); }
internal static async Task <JObject> SearchPortletsAsync(this RequestInfo requestInfo, bool isSystemAdministrator = false, CancellationToken cancellationToken = default) { // prepare var request = requestInfo.GetRequestExpando(); var query = request.Get <string>("FilterBy.Query"); var filter = request.Get <ExpandoObject>("FilterBy")?.ToFilterBy <Portlet>() ?? Filters <Portlet> .And(); var sort = string.IsNullOrWhiteSpace(query) ? request.Get <ExpandoObject>("SortBy")?.ToSortBy <Portlet>() ?? Sorts <Portlet> .Ascending("DesktopID").ThenByAscending("Zone").ThenByAscending("OrderIndex") : null; var pagination = request.Get <ExpandoObject>("Pagination")?.GetPagination() ?? new Tuple <long, int, int, int>(-1, 0, 20, 1); var pageSize = pagination.Item3; var pageNumber = pagination.Item4; // check permission var gotRights = isSystemAdministrator; if (!gotRights) { // get organization var organizationID = filter.GetValue("SystemID") ?? requestInfo.GetParameter("SystemID") ?? requestInfo.GetParameter("x-system-id") ?? requestInfo.GetParameter("OrganizationID"); var organization = await(organizationID ?? "").GetOrganizationByIDAsync(cancellationToken).ConfigureAwait(false); if (organization == null) { throw new InformationExistedException("The organization is invalid"); } gotRights = requestInfo.Session.User.IsViewer(null, null, organization, requestInfo.CorrelationID); if (!gotRights) { throw new AccessDeniedException(); } } // process cache var json = string.IsNullOrWhiteSpace(query) ? await Utility.Cache.GetAsync <string>(Extensions.GetCacheKeyOfObjectsJson(filter, sort, pageSize, pageNumber), cancellationToken).ConfigureAwait(false) : null; if (!string.IsNullOrWhiteSpace(json)) { return(JObject.Parse(json)); } // prepare pagination var totalRecords = pagination.Item1 > -1 ? pagination.Item1 : -1; if (totalRecords < 0) { totalRecords = string.IsNullOrWhiteSpace(query) ? await Portlet.CountAsync(filter, Extensions.GetCacheKeyOfTotalObjects(filter, sort), cancellationToken).ConfigureAwait(false) : await Portlet.CountAsync(query, filter, cancellationToken).ConfigureAwait(false); } var totalPages = new Tuple <long, int>(totalRecords, pageSize).GetTotalPages(); if (totalPages > 0 && pageNumber > totalPages) { pageNumber = totalPages; } // search var objects = totalRecords > 0 ? string.IsNullOrWhiteSpace(query) ? await Portlet.FindAsync(filter, sort, pageSize, pageNumber, Extensions.GetCacheKey(filter, sort, pageSize, pageNumber), cancellationToken).ConfigureAwait(false) : await Portlet.SearchAsync(query, filter, pageSize, pageNumber, cancellationToken).ConfigureAwait(false) : new List <Portlet>(); // build result pagination = new Tuple <long, int, int, int>(totalRecords, totalPages, pageSize, pageNumber); var response = new JObject { { "FilterBy", filter.ToClientJson(query) }, { "SortBy", sort?.ToClientJson() }, { "Pagination", pagination.GetPagination() }, { "Objects", objects.ToJsonArray() } }; // update cache if (string.IsNullOrWhiteSpace(query)) { await Utility.Cache.SetAsync(Extensions.GetCacheKeyOfObjectsJson(filter, sort, pageSize, pageNumber), response.ToString(Formatting.None), cancellationToken).ConfigureAwait(false); } // response return(response); }
internal static async Task ClearRelatedCacheAsync(this Portlet portlet, CancellationToken cancellationToken = default, string correlationID = null, bool clearDataCache = true, bool clearHtmlCache = true, bool doRefresh = true) { // data cache keys var dataCacheKeys = new List <string>(); if (clearDataCache) { dataCacheKeys = Extensions.GetRelatedCacheKeys(Filters <Portlet> .And(Filters <Portlet> .Equals("DesktopID", portlet.DesktopID)), Sorts <Portlet> .Ascending("Zone").ThenByAscending("OrderIndex")); if (string.IsNullOrWhiteSpace(portlet.OriginalPortletID)) { dataCacheKeys = Extensions.GetRelatedCacheKeys(Filters <Portlet> .Equals("OriginalPortletID", portlet.ID), Sorts <Portlet> .Ascending("DesktopID").ThenByAscending("Zone").ThenByAscending("OrderIndex")).Concat(dataCacheKeys).ToList(); } } dataCacheKeys = dataCacheKeys.Distinct(StringComparer.OrdinalIgnoreCase).ToList(); // html cache keys (desktop HTMLs) var htmlCacheKeys = new List <string>(); if (clearHtmlCache) { var desktopSetCacheKeys = await Utility.GetSetCacheKeysAsync(Filters <Portlet> .Equals("ID", portlet.ID), cancellationToken).ConfigureAwait(false); await desktopSetCacheKeys.ForEachAsync(async desktopSetCacheKey => { var cacheKeys = await Utility.Cache.GetSetMembersAsync(desktopSetCacheKey, cancellationToken).ConfigureAwait(false); if (cacheKeys != null && cacheKeys.Count > 0) { htmlCacheKeys = htmlCacheKeys.Concat(cacheKeys).Concat(new[] { desktopSetCacheKey }).ToList(); } }, true, false).ConfigureAwait(false); } htmlCacheKeys = htmlCacheKeys.Distinct(StringComparer.OrdinalIgnoreCase).ToList(); // clear related cache await Utility.Cache.RemoveAsync(htmlCacheKeys.Concat(dataCacheKeys).Distinct(StringComparer.OrdinalIgnoreCase).ToList(), cancellationToken).ConfigureAwait(false); await Task.WhenAll ( Utility.WriteCacheLogs?Utility.WriteLogAsync(correlationID, $"Clear related cache of a portlet [{portlet.Title} - ID: {portlet.ID}]\r\n- {dataCacheKeys.Count} data keys => {dataCacheKeys.Join(", ")}\r\n- {htmlCacheKeys.Count} html keys => {htmlCacheKeys.Join(", ")}", ServiceBase.ServiceComponent.CancellationToken, "Caches") : Task.CompletedTask, doRefresh?$"{Utility.PortalsHttpURI}/~{portlet.Organization.Alias}/".RefreshWebPageAsync(1, correlationID, $"Refresh desktop when related cache of a portlet was clean [{portlet.Title} - ID: {portlet.ID}]") : Task.CompletedTask ).ConfigureAwait(false); }
internal static async Task <int> GetLastOrderIndexAsync(string desktopID, string zone, CancellationToken cancellationToken = default) { var portlets = await Portlet.FindAsync(Filters <Portlet> .And(Filters <Portlet> .Equals("DesktopID", desktopID), Filters <Portlet> .Equals("Zone", zone)), Sorts <Portlet> .Ascending("Zone").ThenByAscending("OrderIndex"), 0, 1, null, cancellationToken).ConfigureAwait(false); return(portlets != null && portlets.Count > 0 ? portlets.Last().OrderIndex : -1); }
internal static async Task <JObject> SearchRolesAsync(this RequestInfo requestInfo, bool isSystemAdministrator = false, CancellationToken cancellationToken = default) { // prepare var request = requestInfo.GetRequestExpando(); var query = request.Get <string>("FilterBy.Query"); var filter = request.Get <ExpandoObject>("FilterBy")?.ToFilterBy <Role>() ?? Filters <Role> .And(); if (filter is FilterBys <Role> ) { if (!string.IsNullOrWhiteSpace(query)) { var index = (filter as FilterBys <Role>).Children.FindIndex(exp => (exp as FilterBy <Role>).Attribute.IsEquals("ParentID")); if (index > -1) { (filter as FilterBys <Role>).Children.RemoveAt(index); } } else if ((filter as FilterBys <Role>).Children.FirstOrDefault(exp => (exp as FilterBy <Role>).Attribute.IsEquals("ParentID")) == null) { (filter as FilterBys <Role>).Children.Add(Filters <Role> .IsNull("ParentID")); } } var sort = string.IsNullOrWhiteSpace(query) ? request.Get <ExpandoObject>("SortBy")?.ToSortBy <Role>() ?? Sorts <Role> .Ascending("Title") : null; var pagination = request.Get <ExpandoObject>("Pagination")?.GetPagination() ?? new Tuple <long, int, int, int>(-1, 0, 20, 1); var pageSize = pagination.Item3; var pageNumber = pagination.Item4; // get organization var organizationID = filter.GetValue("SystemID") ?? requestInfo.GetParameter("SystemID") ?? requestInfo.GetParameter("x-system-id") ?? requestInfo.GetParameter("OrganizationID"); var organization = await(organizationID ?? "").GetOrganizationByIDAsync(cancellationToken).ConfigureAwait(false); if (organization == null) { throw new InformationExistedException("The organization is invalid"); } // check permission var gotRights = isSystemAdministrator || requestInfo.Session.User.IsViewer(null, null, organization, requestInfo.CorrelationID); if (!gotRights) { throw new AccessDeniedException(); } // process cache var addChildren = "true".IsEquals(requestInfo.GetHeaderParameter("x-children")); var cachedJson = string.IsNullOrWhiteSpace(query) && !addChildren ? await Utility.Cache.GetAsync <string>(Extensions.GetCacheKeyOfObjectsJson(filter, sort, pageSize, pageNumber), cancellationToken).ConfigureAwait(false) : null; if (!string.IsNullOrWhiteSpace(cachedJson)) { return(JObject.Parse(cachedJson)); } // prepare pagination var totalRecords = pagination.Item1 > -1 ? pagination.Item1 : -1; if (totalRecords < 0) { totalRecords = string.IsNullOrWhiteSpace(query) ? await Role.CountAsync(filter, Extensions.GetCacheKeyOfTotalObjects(filter, sort), cancellationToken).ConfigureAwait(false) : await Role.CountAsync(query, filter, cancellationToken).ConfigureAwait(false); } var totalPages = new Tuple <long, int>(totalRecords, pageSize).GetTotalPages(); if (totalPages > 0 && pageNumber > totalPages) { pageNumber = totalPages; } // search var objects = totalRecords > 0 ? string.IsNullOrWhiteSpace(query) ? await Role.FindAsync(filter, sort, pageSize, pageNumber, Extensions.GetCacheKey(filter, sort, pageSize, pageNumber), cancellationToken).ConfigureAwait(false) : await Role.SearchAsync(query, filter, pageSize, pageNumber, cancellationToken).ConfigureAwait(false) : new List <Role>(); // build result pagination = new Tuple <long, int, int, int>(totalRecords, totalPages, pageSize, pageNumber); if (addChildren) { await objects.Where(role => role._childrenIDs == null).ForEachAsync((role, _) => role.FindChildrenAsync(cancellationToken), cancellationToken, true, false).ConfigureAwait(false); } var response = new JObject { { "FilterBy", filter.ToClientJson(query) }, { "SortBy", sort?.ToClientJson() }, { "Pagination", pagination.GetPagination() }, { "Objects", objects.Select(role => role.ToJson(addChildren, false)).ToJArray() } }; // update cache if (string.IsNullOrWhiteSpace(query) && !addChildren) { await Utility.Cache.SetAsync(Extensions.GetCacheKeyOfObjectsJson(filter, sort, pageSize, pageNumber), response.ToString(Formatting.None), cancellationToken).ConfigureAwait(false); } // response return(response); }
internal static async Task <JObject> SearchOrganizationsAsync(this RequestInfo requestInfo, bool isSystemAdministrator = false, CancellationToken cancellationToken = default) { // check permissions if (!isSystemAdministrator) { throw new AccessDeniedException(); } // prepare var request = requestInfo.GetRequestExpando(); var query = request.Get <string>("FilterBy.Query"); var filter = request.Get <ExpandoObject>("FilterBy")?.ToFilterBy <Organization>() ?? Filters <Organization> .And(); var sort = string.IsNullOrWhiteSpace(query) ? request.Get <ExpandoObject>("SortBy")?.ToSortBy <Organization>() ?? Sorts <Organization> .Ascending("Title") : null; var pagination = request.Get <ExpandoObject>("Pagination")?.GetPagination() ?? new Tuple <long, int, int, int>(-1, 0, 20, 1); var pageSize = pagination.Item3; var pageNumber = pagination.Item4; // process cache var json = string.IsNullOrWhiteSpace(query) ? await Utility.Cache.GetAsync <string>(Extensions.GetCacheKeyOfObjectsJson(filter, sort, pageSize, pageNumber), cancellationToken).ConfigureAwait(false) : null; if (!string.IsNullOrWhiteSpace(json)) { return(JObject.Parse(json)); } // prepare pagination var totalRecords = pagination.Item1 > -1 ? pagination.Item1 : -1; if (totalRecords < 0) { totalRecords = string.IsNullOrWhiteSpace(query) ? await Organization.CountAsync(filter, Extensions.GetCacheKeyOfTotalObjects(filter, sort), cancellationToken).ConfigureAwait(false) : await Organization.CountAsync(query, filter, cancellationToken).ConfigureAwait(false); } var totalPages = new Tuple <long, int>(totalRecords, pageSize).GetTotalPages(); if (totalPages > 0 && pageNumber > totalPages) { pageNumber = totalPages; } // search var objects = totalRecords > 0 ? string.IsNullOrWhiteSpace(query) ? await Organization.FindAsync(filter, sort, pageSize, pageNumber, Extensions.GetCacheKey(filter, sort, pageSize, pageNumber), cancellationToken).ConfigureAwait(false) : await Organization.SearchAsync(query, filter, pageSize, pageNumber, cancellationToken).ConfigureAwait(false) : new List <Organization>(); // build result pagination = new Tuple <long, int, int, int>(totalRecords, totalPages, pageSize, pageNumber); var response = new JObject { { "FilterBy", filter.ToClientJson(query) }, { "SortBy", sort?.ToClientJson() }, { "Pagination", pagination.GetPagination() }, { "Objects", objects.ToJsonArray() } }; // update cache if (string.IsNullOrWhiteSpace(query)) { await Utility.Cache.SetAsync(Extensions.GetCacheKeyOfObjectsJson(filter, sort, pageSize, pageNumber), response.ToString(Formatting.None), cancellationToken).ConfigureAwait(false); } // response return(response); }