Exemplo n.º 1
0
        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));
        }
Exemplo n.º 2
0
        internal static async Task <List <string> > GetSetCacheKeysAsync(IFilterBy <Portlet> filter, CancellationToken cancellationToken = default)
        {
            var desktopIDs = new List <string>();
            var portlets   = await Portlet.FindAsync(filter, null, 0, 1, null, cancellationToken).ConfigureAwait(false);

            await portlets.ForEachAsync(async portlet =>
            {
                var mappingPortlets = await Portlet.FindAsync(Filters <Portlet> .Equals("OriginalPortletID", portlet.ID), null, 0, 1, null, cancellationToken).ConfigureAwait(false);
                desktopIDs          = desktopIDs.Concat(new[] { portlet.DesktopID }).Concat(mappingPortlets.Select(mappingPortlet => mappingPortlet.DesktopID)).ToList();
            }, true, false).ConfigureAwait(false);

            var desktops = new List <Desktop>();
            await desktopIDs.Where(desktopID => !string.IsNullOrWhiteSpace(desktopID) && desktopID.IsValidUUID()).Distinct(StringComparer.OrdinalIgnoreCase).ToList().ForEachAsync(async desktopID =>
            {
                var desktop = await desktopID.GetDesktopByIDAsync(cancellationToken).ConfigureAwait(false);
                if (desktop != null)
                {
                    desktops.Add(desktop);
                }
            }, true, false).ConfigureAwait(false);

            return(desktops.Select(desktop => desktop.GetSetCacheKey()).ToList());
        }
Exemplo n.º 3
0
        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);
        }
Exemplo n.º 4
0
        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);
        }