Example #1
0
        public ActionResult Create([Bind(Exclude = "Category.Name")] ArticleViewModel model)
        {
            try
            {
                ModelState.Remove("Category.Name");
                if (ModelState.IsValid)
                {
                    var article = ArticleFactory.CreateArticleFromViewModel(model, KBVaultHelperFunctions.UserAsKbUser(User).Id);
                    var id      = ArticleRepository.Add(article, model.Tags);
                    if (article.IsDraft == 0)
                    {
                        KbVaultLuceneHelper.AddArticleToIndex(article);
                    }

                    ShowOperationMessage(UIResources.ArticleCreatePageCreateSuccessMessage);
                    return(RedirectToAction("Edit", "Article", new { id = article.Id }));
                }

                return(View(model));
            }
            catch (Exception ex)
            {
                Log.Error(ex);
                ModelState.AddModelError("Exception", ex.Message);
                return(View(model));
            }
        }
Example #2
0
        public void RebuildIndexes()
        {
            var categories       = CategoryRepository.GetAllCategories();
            var totalCategories  = categories.Count();
            var indexingCategory = 1;

            foreach (var cat in categories)
            {
                Clients.All.updateProgress(indexingCategory, totalCategories, cat.Name, "-");
                var articles = CategoryRepository.GetArticles(cat.Id);
                foreach (var article in articles)
                {
                    Clients.All.updateProgress(indexingCategory, totalCategories, cat.Name, article.Title);
                    foreach (var attachment in article.Attachments)
                    {
                        try
                        {
                            KbVaultLuceneHelper.RemoveAttachmentFromIndex(attachment);
                            KbVaultLuceneHelper.AddAttachmentToIndex(attachment);
                        }
#pragma warning disable CC0004 // Catch block cannot be empty
                        catch (Exception)
                        {
                        }
#pragma warning restore CC0004 // Catch block cannot be empty
                    }

                    KbVaultLuceneHelper.AddArticleToIndex(article);
                }

                indexingCategory++;
            }

            Clients.All.updateProgress(string.Empty, string.Empty, string.Empty, "Finished indexing");
        }
Example #3
0
 public FileController(IWebHostEnvironment env, IHttpContextAccessor httpContextAccessor, KnowledgeBaseContext context, KbVaultLuceneHelper lucene)
 {
     _env = env;
     _httpContextAccessor = httpContextAccessor;
     _context             = context;
     _lucene = lucene;
 }
Example #4
0
        public void RebuildIndexes()
        {
            var categories       = CategoryRepository.GetAllCategories();
            int totalCategories  = categories.Count();
            int indexingCategory = 1;

            foreach (var cat in categories)
            {
                Clients.All.updateProgress(indexingCategory, totalCategories, cat.Name, "-");
                var articles = CategoryRepository.GetArticles(cat.Id);
                foreach (var article in articles)
                {
                    Clients.All.updateProgress(indexingCategory, totalCategories, cat.Name, article.Title);
                    foreach (var attachment in article.Attachments)
                    {
                        try
                        {
                            KbVaultLuceneHelper.RemoveAttachmentFromIndex(attachment);
                            KbVaultLuceneHelper.AddAttachmentToIndex(attachment);
                        }
                        catch (Exception ex)
                        {
                            //Eat it :d
                        }
                    }
                    KbVaultLuceneHelper.AddArticleToIndex(article);
                }
                indexingCategory++;
            }
            Clients.All.updateProgress("", "", "", "Finished indexing");
        }
Example #5
0
        public JsonResult Remove(string id)
        {
            JsonOperationResponse result = new JsonOperationResponse();

            result.Successful = false;
            try
            {
                var parts = id.Split(new[] { "|" }, StringSplitOptions.RemoveEmptyEntries);
                if (parts.Length == 2)
                {
                    var attachmentHash = parts[0];
                    var attachmentId   = parts[1];

                    Attachment at = new Attachment()
                    {
                        Id = Convert.ToInt64(attachmentId)
                    };
                    at.Author = KBVaultHelperFunctions.UserAsKbUser(User).Id;
                    KbVaultAttachmentHelper.RemoveAttachment(attachmentHash, KBVaultHelperFunctions.UserAsKbUser(User).Id);
                    KbVaultLuceneHelper.RemoveAttachmentFromIndex(at);
                    result.Successful = true;
                    return(Json(result));
                }
                throw new ArgumentOutOfRangeException("Invalid file hash");
            }
            catch (Exception ex)
            {
                Log.Error(ex);
                result.ErrorMessage = ex.Message;
                return(Json(result));
            }
        }
Example #6
0
 public SearchController(KnowledgeBaseContext context, ISettingsService settingService, IHttpContextAccessor httpContextAccessor, IWebHostEnvironment env, KbVaultLuceneHelper lucene)
 {
     _context             = context;
     _settingService      = settingService;
     _httpContextAccessor = httpContextAccessor;
     _env    = env;
     _lucene = lucene;
 }
Example #7
0
        public JsonResult Remove(int id)
        {
            var result = new JsonOperationResponse();

            try
            {
                using (var db = new KbVaultContext())
                {
                    var currentUserId = KBVaultHelperFunctions.UserAsKbUser(User).Id;
                    var queryParams   = new SqlParameter[] { new SqlParameter("ArticleId", id) };
                    db.Database.ExecuteSqlCommand("Delete from ArticleTag Where ArticleId = @ArticleId", queryParams);
                    var article = db.Articles.Single(a => a.Id == id);
                    if (article == null)
                    {
                        throw new Exception(ErrorMessages.ArticleNotFound);
                    }

                    while (article.Attachments.Count > 0)
                    {
                        var a = article.Attachments.First();
                        KbVaultAttachmentHelper.RemoveLocalAttachmentFile(a);
                        KbVaultLuceneHelper.RemoveAttachmentFromIndex(a);
                        article.Attachments.Remove(a);

                        /*
                         * Also remove the attachment from db.attachments collection
                         *
                         * http://stackoverflow.com/questions/17723626/entity-framework-remove-vs-deleteobject
                         *
                         * If the relationship is required (the FK doesn't allow NULL values) and the relationship is not
                         * identifying (which means that the foreign key is not part of the child's (composite) primary key)
                         * you have to either add the child to another parent or you have to explicitly delete the child
                         * (with DeleteObject then). If you don't do any of these a referential constraint is
                         * violated and EF will throw an exception when you call SaveChanges -
                         * the infamous "The relationship could not be changed because one or more of the foreign-key properties
                         * is non-nullable" exception or similar.
                         */
                        db.Attachments.Remove(a);
                    }

                    article.Author = currentUserId;
                    KbVaultLuceneHelper.RemoveArticleFromIndex(article);
                    db.Articles.Remove(article);
                    db.SaveChanges();
                    result.Data       = id;
                    result.Successful = true;
                    return(Json(result));
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex);
                result.Successful   = false;
                result.ErrorMessage = ex.Message;
                return(Json(result));
            }
        }
Example #8
0
 public ArticlesController(KnowledgeBaseContext context, IHttpContextAccessor httpContextAccessor, IArticleFactory articleFactory, IArticleRepository articleRepository, IUserRepository userRepository, IActivityRepository activityRepository, KbVaultLuceneHelper lucene, KbVaultAttachmentHelper attachmentHelper)
 {
     _context             = context;
     _httpContextAccessor = httpContextAccessor;
     _articleFactory      = articleFactory;
     _articleRepository   = articleRepository;
     _userRepository      = userRepository;
     _activityRepository  = activityRepository;
     _lucene = lucene;
     this.attachmentHelper = attachmentHelper;
 }
Example #9
0
        public JsonResult Upload()
        {
            var result = new JsonOperationResponse
            {
                Successful = false
            };

            try
            {
                if (Request.Params["ArticleId"] == null)
                {
                    result.ErrorMessage = ErrorMessages.FileUploadArticleNotFound;
                }
                else if (Request.Files.Count == 1)
                {
                    var articleId    = Convert.ToInt64(Request.Params["ArticleId"]);
                    var attachedFile = Request.Files[0];
                    var attachment   = KbVaultAttachmentHelper.SaveAttachment(articleId, attachedFile, KBVaultHelperFunctions.UserAsKbUser(User).Id);
                    attachment.Author = KBVaultHelperFunctions.UserAsKbUser(User).Id;
                    result.Successful = true;
                    result.Data       = new AttachmentViewModel(attachment);
                    using (var db = new KbVaultContext())
                    {
                        var sets = db.Settings.FirstOrDefault();
                        if (sets != null)
                        {
                            var extensions = sets.IndexFileExtensions.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);

                            if (extensions.FirstOrDefault(a => a.ToLowerInvariant() == attachment.Extension.ToLowerInvariant()) != null)
                            {
                                KbVaultLuceneHelper.AddAttachmentToIndex(attachment);
                            }
                        }
                    }
                }
                else
                {
                    result.ErrorMessage = ErrorMessages.FileUploadTooManyFiles;
                }

                return(Json(result));
            }
            catch (Exception ex)
            {
                log.Error(ex);
                result.ErrorMessage = ex.Message;
                return(Json(result));
            }
        }
Example #10
0
        public ActionResult Do(SearchFormViewModel model)
        {
            try
            {
                if (string.IsNullOrEmpty(model.SearchKeyword))
                {
                    return(RedirectToAction("Index", "Home"));
                }

                var articlePrefix = SettingsService.GetSettings().ArticlePrefix;
                if (!string.IsNullOrEmpty(articlePrefix))
                {
                    if (model.SearchKeyword.Length > articlePrefix.Length + 1 &&
                        model.SearchKeyword.Substring(0, articlePrefix.Length + 1) == articlePrefix + "-")
                    {
                        var articleId = model.SearchKeyword.Substring(articlePrefix.Length + 1);
                        model.ArticleId = Convert.ToInt32(articleId);
                    }

                    if (model.ArticleId > 0)
                    {
                        Article article = null;
                        using (var db = new KbVaultContext())
                        {
                            article = db.PublishedArticles().FirstOrDefault(a => a.Id == model.ArticleId);
                        }

                        if (article != null)
                        {
                            return(RedirectToRoute("Default", new { controller = "Home", action = "Detail", id = article.SefName }));
                        }
                    }
                }

                if (model.CurrentPage == 0)
                {
                    model.CurrentPage++;
                }

                model.Results = KbVaultLuceneHelper.DoSearch(model.SearchKeyword, model.CurrentPage, 10);

                return(View(model));
            }
            catch (Exception ex)
            {
                Log.Error(ex);
                throw;
            }
        }
        public ActionResult Edit([Bind(Exclude = "Category.Name,Category.SefName")] ArticleViewModel model)
        {
            try
            {
                ModelState.Remove("Category.Name");
                ModelState.Remove("Category.SefName");
                if (ModelState.IsValid)
                {
                    if (model.PublishEndDate < model.PublishStartDate)
                    {
                        ModelState.AddModelError("PublishDate", ErrorMessages.PublishEndDateMustBeGreater);
                    }
                    else
                    {
                        var article = ArticleRepository.Get(model.Id);
                        article.CategoryId       = model.Category.Id;
                        article.IsDraft          = model.IsDraft ? 1 : 0;
                        article.PublishEndDate   = model.PublishEndDate;
                        article.PublishStartDate = model.PublishStartDate;
                        article.Edited           = DateTime.Now;
                        article.Title            = model.Title;
                        article.Content          = model.Content;
                        article.Author           = KBVaultHelperFunctions.UserAsKbUser(User).Id;
                        article.SefName          = model.SefName;
                        ArticleRepository.Update(article, model.Tags);
                        if (article.IsDraft == 0)
                        {
                            KbVaultLuceneHelper.AddArticleToIndex(article);
                        }
                        else
                        {
                            KbVaultLuceneHelper.RemoveArticleFromIndex(article);
                        }

                        ShowOperationMessage(UIResources.ArticleCreatePageEditSuccessMessage);
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex);
                ModelState.AddModelError("Exception", ex.Message);
            }

            return(View("Create", model));
        }
Example #12
0
        public JsonResult More(SearchFormViewModel model)
        {
            JsonOperationResponse result = new JsonOperationResponse();

            try
            {
                model.CurrentPage++;
                model.Results     = KbVaultLuceneHelper.DoSearch(model.SearchKeyword, model.CurrentPage, 1);
                result.Successful = true;
                result.Data       = model;
            }
            catch (Exception ex)
            {
                Log.Error(ex);
                result.ErrorMessage = ex.Message;
            }
            return(Json(result));
        }
Example #13
0
        public JsonResult Ajax(string id)
        {
            JsonOperationResponse result = new JsonOperationResponse();

            try
            {
                if (Request.IsAjaxRequest())
                {
                    List <KbSearchResultItemViewModel> items = KbVaultLuceneHelper.DoSearch(id, 1, 10); //Request.Form["txtSearch"]);
                    result.Data       = items;
                    result.Successful = true;
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex);
                result.ErrorMessage = ex.Message;
            }
            return(Json(result));
        }
Example #14
0
        public JsonResult Ajax(string id)
        {
            var result = new JsonOperationResponse();

            try
            {
                if (Request.IsAjaxRequest())
                {
                    var items = KbVaultLuceneHelper.DoSearch(id, 1, 10);
                    result.Data       = items;
                    result.Successful = true;
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex);
                result.ErrorMessage = ex.Message;
            }

            return(Json(result));
        }