Example #1
0
        /// <summary>
        /// Creates the specified tag.
        /// </summary>
        /// <param name="tag">The tag.</param>
        /// <returns></returns>
        public Tag Create(string dataSetName, Tag tag)
        {
            var        tagQuery         = TagQuery(dataSetName);
            TagElastic parentTagElastic = tagQuery.Get(tag.ParentId);

            var level        = 1;
            var parentIdList = new List <string>();

            if (parentTagElastic != null)
            {
                parentIdList.AddRange(parentTagElastic.ParentIdList);
                parentIdList.Add(parentTagElastic.Id);
                level = parentTagElastic.Level + 1;

                // If parent is currently leaf then set to false
                if (parentTagElastic.IsLeaf)
                {
                    parentTagElastic.IsLeaf = false;
                    tagQuery.Update(parentTagElastic.Id, parentTagElastic);
                }
            }

            var tagElastic = new TagElastic
            {
                Id           = tag.Id,
                Name         = tag.Name,
                ParentIdList = parentIdList,
                IsLeaf       = true,
                Level        = level
            };

            tagQuery.Index(tagElastic);

            return(GetTagModel(dataSetName, tag.Id, false));
        }
Example #2
0
 public string Update(string id, TagElastic tagElastic)
 {
     Delete(id);
     Index(tagElastic);
     ResponseValidator(Client.Flush(IndexName));
     return(tagElastic.Id);
 }
Example #3
0
        public bool HasChildren(TagElastic tagElastic)
        {
            if (tagElastic == null)
            {
                throw new ArgumentNullException(nameof(tagElastic));
            }

            var response = Count <TagElastic>(GetDescendantSearchDescriptor(tagElastic.Id));

            return(response > 0);
        }
Example #4
0
        /// <summary>
        /// Updates Tag with the specified identifier.
        /// </summary>
        /// <param name="id">The identifier.</param>
        /// <param name="tag">The tag.</param>
        /// <returns></returns>
        public bool Update(string dataSetName, string id, Tag tag)
        {
            var        tagQuery            = TagQuery(dataSetName);
            var        tagElastic          = tagQuery.Get(id);
            TagElastic oldParentTagElastic = GetParentTag(dataSetName, tagElastic);
            TagElastic newParentTagElastic = null;

            if (!string.IsNullOrWhiteSpace(tag.ParentId))
            {
                newParentTagElastic = tagQuery.Get(tag.ParentId);

                if (newParentTagElastic == null)
                {
                    return(false);
                }
            }

            var level        = 1;
            var parentIdList = new List <string>();

            if (newParentTagElastic != null)
            {
                parentIdList.AddRange(newParentTagElastic.ParentIdList);
                parentIdList.Add(newParentTagElastic.Id);
                level = newParentTagElastic.Level + 1;

                // If parent is currently leaf then set to false
                if (newParentTagElastic.IsLeaf)
                {
                    newParentTagElastic.IsLeaf = false;
                    tagQuery.Update(newParentTagElastic.Id, newParentTagElastic);
                }
            }

            tagElastic.Id           = tag.Id;
            tagElastic.Name         = tag.Name;
            tagElastic.ModifiedDate = DateTime.UtcNow;
            tagElastic.Level        = level;
            tagElastic.ParentIdList = parentIdList;

            tagQuery.Update(id, tagElastic);
            tagQuery.AdjustLeafStatus(oldParentTagElastic);

            return(true);
        }
Example #5
0
        /// <summary>
        /// Deletes the specified tag.
        /// </summary>
        /// <param name="id">The identifier.</param>
        /// <param name="force">if set to <c>true</c> [force].</param>
        /// <param name="cleanDocuments">if set to <c>true</c> [clean documents].</param>
        /// <returns></returns>
        public void Delete(string dataSetName, string id, bool force, bool cleanDocuments)
        {
            var dataSet       = DataSet(dataSetName).DataSet;
            var result        = new BulkResults();
            var tagQuery      = TagQuery(dataSetName);
            var documentQuery = DocumentQuery(dataSetName);
            var tagElastic    = tagQuery.Get(id);
            var childTagIds   = tagQuery.GetDescendantTagIds(id);

            TagElastic parentTagElastic = null;

            if (!string.IsNullOrWhiteSpace(tagElastic.ParentId()))
            {
                parentTagElastic = tagQuery.Get(tagElastic.ParentId());
            }

            if (cleanDocuments)
            {
                foreach (var childTagId in childTagIds)
                {
                    foreach (var documentElastic in documentQuery.GetByTagId(childTagId, dataSet.TagField))
                    {
                        DocumentHelper.RemoveTagIds(documentElastic.DocumentObject, dataSet.TagField, new List <string> {
                            childTagId
                        });
                        documentQuery.Index(documentElastic);
                    }
                }
            }

            var deleteTagIds = new List <string> {
                id
            };

            if (force)
            {
                deleteTagIds.AddRange(childTagIds);
            }

            tagQuery.DeleteByIds(deleteTagIds);
            tagQuery.AdjustLeafStatus(parentTagElastic);
        }
Example #6
0
        public void AdjustLeafStatus(TagElastic tagElastic)
        {
            if (tagElastic == null)
            {
                return;
            }

            var hasChildren = GetDescendantTagIds(tagElastic.Id).Any();

            if (hasChildren && tagElastic.IsLeaf == true)
            {
                tagElastic.IsLeaf = false;
                Update(tagElastic.Id, tagElastic);
            }
            else if (!hasChildren && tagElastic.IsLeaf == false)
            {
                tagElastic.IsLeaf = true;
                Update(tagElastic.Id, tagElastic);
            }
        }
Example #7
0
 public string Index(TagElastic tagElastic)
 {
     return(IndexWithBulkResponse(new List <TagElastic> {
         tagElastic
     }).Items.FirstOrDefault().Id);
 }
Example #8
0
        /// <summary>
        /// Bulk tag creation.
        /// </summary>
        /// <param name="settings">The settings.</param>
        /// <returns></returns>
        public Result <BulkResults> BulkCreate(string dataSetName, List <Tag> tags, int parallelLimit, long requestSize)
        {
            var results = new BulkResults();

            var tagIsInteger = DataSet(dataSetName).TagIsInteger;

            if (tagIsInteger && tags.Any(tag => tag.Id.Any(ch => !char.IsDigit(ch))))
            {
                results.Results = tags
                                  .Where(tag => tag.Id.Any(ch => !char.IsDigit(ch)))
                                  .Select(t => BulkResult.Create(
                                              t.Id,
                                              StatusCodes.Status400BadRequest,
                                              string.Format(TagResources.TagIdShouldBeIntegerType, t.ParentId, t.Id)))
                                  .ToList();

                return(Result.Ok(results));
            }

            var tagIdsByLevel = TagHelper.GetTagIdsByLevel(tags, item => item.ParentId, item => item.Id);
            var validIds      = tagIdsByLevel.SelectMany(l => l.Value).ToList();
            var invalidIds    = tags.Select(t => t.Id).Except(validIds);

            if (invalidIds.Any())
            {
                results.Results = tags
                                  .Where(t => invalidIds.Contains(t.Id))
                                  .Select(t => BulkResult.Create(t.Id, StatusCodes.Status404NotFound, string.Format(TagResources.ParentId_0_NotFoundInTagWithId_1, t.ParentId, t.Id))).ToList();

                // returns with OK status, individual items contain error code
                return(Result.Ok(results));
            }

            var orderedTagElasticList = tagIdsByLevel
                                        .SelectMany(dic => dic.Value)
                                        .Select(id =>
            {
                var tag        = tags.FirstOrDefault(t => t.Id == id);
                var tagElastic = new TagElastic
                {
                    Id           = tag.Id,
                    Name         = tag.Name,
                    ParentIdList = new List <string>()
                };
                if (!string.IsNullOrWhiteSpace(tag.ParentId))
                {
                    tagElastic.ParentIdList.Add(tag.ParentId);
                }
                return(tagElastic);
            })
                                        .ToList();

            TagHelper.AdjustTagElastics(orderedTagElasticList);

            var tagQuery = TagQuery(dataSetName);

            tagQuery.DeleteAll();

            var bulkResponseStruct = tagQuery.ParallelBulkIndex(orderedTagElasticList, parallelLimit, requestSize);

            results.Results.AddRange(bulkResponseStruct.ToBulkResult());

            return(Result.Ok(results));
        }
Example #9
0
 public TagElastic GetParentTag(string dataSetName, TagElastic tagElastic)
 {
     return(!string.IsNullOrWhiteSpace(tagElastic.ParentId()) ? TagQuery(dataSetName).Get(tagElastic.ParentId()) : null);
 }