Beispiel #1
0
        /// <summary>
        /// Updates the URL Entity
        /// </summary>
        /// <param name="urlEntity">URL Entity</param>
        public virtual async Task UpdateEntityUrl(EntityUrl urlEntity)
        {
            if (urlEntity == null)
            {
                throw new ArgumentNullException(nameof(urlEntity));
            }

            await _urlEntityRepository.UpdateAsync(urlEntity);

            //cache
            await _cacheBase.RemoveByPrefix(CacheKey.URLEntity_PATTERN_KEY);
        }
        private int IsMatch(EntityUrl url, Dictionary <string, List <EntityData> > entitiesDictionary)
        {
            EntityData data;
            int        maxOrderNumber = -1;

            foreach (var requirement in url.Requirements)
            {
                data = GetEntityData(entitiesDictionary, requirement.EntityType, requirement.Relationship);

                if (data == null)
                {
                    return(-1);
                }

                if (requirement.IdEquals != null && string.Compare(data.Id, requirement.IdEquals, StringComparison.OrdinalIgnoreCase) != 0)
                {
                    return(-1);
                }

                if (requirement.ContainerTypeIdEquals != null && requirement.ContainerTypeIdEquals != data.ContainerTypeId)
                {
                    return(-1);
                }

                if (requirement.ApplicationTypeIdEquals != null && requirement.ApplicationTypeIdEquals != data.ApplicationTypeId)
                {
                    return(-1);
                }

                else if (data.OrderNumber > maxOrderNumber)
                {
                    maxOrderNumber = data.OrderNumber;
                }
            }

            foreach (var token in url.Tokens)
            {
                data = GetEntityData(entitiesDictionary, token.EntityType, token.Relationship);
                if (data == null)
                {
                    return(-1);
                }
                else if (data.OrderNumber > maxOrderNumber)
                {
                    maxOrderNumber = data.OrderNumber;
                }
            }

            return(maxOrderNumber + url.Requirements.Count() + url.Tokens.Count());
        }
        public bool TryGetRedirectUrl(Host host, string url, out string redirectUrl)
        {
            if (string.IsNullOrEmpty(url))
            {
                redirectUrl = null;
                return(false);
            }

            string cacheKey = CacheKey(url);

            redirectUrl = (string)host.Cache.Get(cacheKey);
            if (redirectUrl == null)
            {
                var       entitiesDictionary  = GetEntitiesDictionary(host, url);
                EntityUrl selectedUrl         = null;
                int       selectedOrderNumber = -1;

                foreach (EntityUrl entityUrl in Configuration.EntityUrls)
                {
                    var o = IsMatch(entityUrl, entitiesDictionary);
                    if (o > -1 && o > selectedOrderNumber)
                    {
                        selectedUrl         = entityUrl;
                        selectedOrderNumber = o;
                    }
                }

                if (selectedUrl != null)
                {
                    redirectUrl = FormatUrl(selectedUrl, entitiesDictionary);
                }
                else
                {
                    redirectUrl = string.Empty;
                }

                host.Cache.Put(cacheKey, redirectUrl, 30 * 60);
            }

            if (string.IsNullOrEmpty(redirectUrl))
            {
                redirectUrl = null;
                return(false);
            }
            else
            {
                return(true);
            }
        }
        private string FormatUrl(EntityUrl url, Dictionary <string, List <EntityData> > entitiesDictionary)
        {
            string     outUrl = url.Url;
            EntityData data;

            foreach (var token in url.Tokens)
            {
                data = GetEntityData(entitiesDictionary, token.EntityType, token.Relationship);
                if (data != null)
                {
                    outUrl = outUrl.Replace(string.Concat("{", token.Name, "}"), data.Id);
                }
            }

            return(outUrl);
        }
        protected override void Execute(CodeActivityContext ctx)
        {
            var url = EntityUrl.Get(ctx);

            if (string.IsNullOrWhiteSpace(url))
            {
                EntityId.Set(ctx, Guid.Empty.ToString()); // Assuming the entity id is being used as an AK
                return;
            }

            if (!url.Contains("?"))
            {
                throw new Exception("Record URL does not query string");
            }

            url = url.Substring(url.IndexOf('?') + 1);
            var pairs = url.Split('&');
            var id    = string.Empty;

            foreach (var pair in pairs)
            {
                if (!pair.Contains("="))
                {
                    continue;
                }
                var items = pair.Split('=');
                if (items[0] != "id")
                {
                    continue;
                }

                id = items[1];
            }

            if (string.IsNullOrWhiteSpace(id))
            {
                throw new Exception("Record URL query string does not contain record id");
            }
            EntityId.Set(ctx, id);
        }
Beispiel #6
0
        public async Task <EntityUrl> ValidateAsync(EntityUrl entityUrl, CancellationToken cancellationToken)
        {
            var url = entityUrl.Url;

            if (!Uri.IsWellFormedUriString(url.OriginalString, UriKind.Absolute))
            {
                entityUrl.SetBroken();
                return(entityUrl);
            }

            if (url.Scheme == Uri.UriSchemeHttp)
            {
                if (await IsHttpsSupported(url, cancellationToken))
                {
                    entityUrl.SetSupportsHttps();
                }
            }
            try
            {
                using var response = await GetAsync(url, cancellationToken);

                if ((int)response.StatusCode >= 300 && (int)response.StatusCode < 400)
                {
                    entityUrl.SetRedirectsTo(response.Headers.Location);
                }
                else if (!response.IsSuccessStatusCode)
                {
                    entityUrl.SetBroken();
                }
            }
            catch (Exception ex) when(ex is HttpRequestException ||
                                      ex is TaskCanceledException)
            {
                entityUrl.SetBroken();
            }

            return(entityUrl);
        }
Beispiel #7
0
        /// <summary>
        /// Save slug
        /// </summary>
        /// <typeparam name="T">Type</typeparam>
        /// <param name="entity">Entity</param>
        /// <param name="slug">Slug</param>
        /// <param name="languageId">Language ID</param>
        public virtual async Task SaveSlug <T>(T entity, string slug, string languageId) where T : BaseEntity, ISlugEntity
        {
            if (entity == null)
            {
                throw new ArgumentNullException(nameof(entity));
            }

            string entityId   = entity.Id;
            string entityName = typeof(T).Name;

            var query = from ur in _urlEntityRepository.Table
                        where ur.EntityId == entityId &&
                        ur.EntityName == entityName &&
                        ur.LanguageId == languageId
                        select ur;

            var allUrlEntity    = query.ToList();
            var activeUrlEntity = allUrlEntity.FirstOrDefault(x => x.IsActive);

            if (!string.IsNullOrWhiteSpace(slug))
            {
                slug = slug.ToLowerInvariant();
            }

            if (activeUrlEntity == null && !string.IsNullOrWhiteSpace(slug))
            {
                //find in non-active records with the specified slug
                var nonActiveRecordWithSpecifiedSlug = allUrlEntity
                                                       .FirstOrDefault(x => x.Slug.Equals(slug, StringComparison.OrdinalIgnoreCase) && !x.IsActive);
                if (nonActiveRecordWithSpecifiedSlug != null)
                {
                    //mark non-active record as active
                    nonActiveRecordWithSpecifiedSlug.IsActive = true;
                    await UpdateEntityUrl(nonActiveRecordWithSpecifiedSlug);
                }
                else
                {
                    //new record
                    var entityUrl = new EntityUrl
                    {
                        EntityId   = entityId,
                        EntityName = entityName,
                        Slug       = slug,
                        LanguageId = languageId,
                        IsActive   = true,
                    };
                    await InsertEntityUrl(entityUrl);
                }
            }

            if (activeUrlEntity != null && string.IsNullOrWhiteSpace(slug))
            {
                activeUrlEntity.IsActive = false;
                await UpdateEntityUrl(activeUrlEntity);
            }

            if (activeUrlEntity != null && !string.IsNullOrWhiteSpace(slug))
            {
                if (!activeUrlEntity.Slug.Equals(slug, StringComparison.OrdinalIgnoreCase))
                {
                    //find in non-active records with the specified slug
                    var nonActiveRecordWithSpecifiedSlug = allUrlEntity
                                                           .FirstOrDefault(x => x.Slug.Equals(slug, StringComparison.OrdinalIgnoreCase) && !x.IsActive);
                    if (nonActiveRecordWithSpecifiedSlug != null)
                    {
                        nonActiveRecordWithSpecifiedSlug.IsActive = true;
                        await UpdateEntityUrl(nonActiveRecordWithSpecifiedSlug);

                        activeUrlEntity.IsActive = false;
                        await UpdateEntityUrl(activeUrlEntity);
                    }
                    else
                    {
                        //insert new record
                        var entityUrl = new EntityUrl
                        {
                            EntityId   = entityId,
                            EntityName = entityName,
                            Slug       = slug,
                            LanguageId = languageId,
                            IsActive   = true,
                        };
                        await InsertEntityUrl(entityUrl);

                        activeUrlEntity.IsActive = false;
                        await UpdateEntityUrl(activeUrlEntity);
                    }
                }
            }
        }
        private static string GetRelativeUrl(string baseUrl, EntityAction entityAction, EntityUrl entityUrl, bool isPrimaryEntity)
        {
            string relativeUrl = baseUrl;

            if (entityUrl.IsFullPath)
            {
                relativeUrl = entityUrl.URL;
            }
            else if (!isPrimaryEntity || (isPrimaryEntity && string.IsNullOrEmpty(entityAction.EntityKey)))
            {
                // Not Primary: Add Sub URL
                // Primary w/ out ID: Add Sub URL
                relativeUrl += entityUrl.URL;
            }

            if (!string.IsNullOrEmpty(entityAction.EntityKey))
            {
                if (entityAction.EntityKey.Contains("?"))
                {
                    //Note : This is added to build link with search parameters
                    relativeUrl += entityAction.EntityKey;
                }
                else
                {
                    relativeUrl += "/" + entityAction.EntityKey;
                }
            }

            return(relativeUrl);
        }
Beispiel #9
0
 public override int GetHashCode()
 {
     return(EntityUrl.GetHashCode() ^ Url.GetHashCode());
 }