Inheritance: IDisposable
        public ActionResult AddKoment(string Komenatar, int? aid)
        {
            User user = Autorizacija.Autorizacija.GetCurrentUser(this.HttpContext);
            if (!aid.HasValue && string.IsNullOrEmpty(Komenatar))
                return RedirectToAction("Index", "Wellcome");
            int ArticlesID = aid.Value;
            try
            {

                Comment c = new Comment();

                c.CreatorName = user.FirstName + " " + user.LastName;
                c.CreatorUserAgent = this.Request.UserAgent;
                c.CreatorIP = this.Request.GetIpAdresa();
                c.GUID = Guid.NewGuid();
                c.IsActive = true;
                c.IsSpam = false;
                c.CreatorEmail = user.Email;
                c.Created = DateTime.Now;
                c.CreatedByUserID = user.UserID;
                c.Body = Komenatar;
                c.ArticleID = ArticlesID;

                using (DBBL Baza = new DBBL())
                {
                    Baza.AddKomentar(c);
                }

            }
            catch (Exception) { }
            return RedirectToRoute("GetWikiRoute", new { id = ArticlesID });
        }
        //
        // GET: /Wellcome/
        public ActionResult Index(string tab, int page=1)
        {
            DB.DAL.User a = auth.Autorizacija.GetCurrentUser(this.HttpContext);
            if (a != null)
                TempData["user"] = a;
            using (DBBL Baza = new DBBL())
            {
                TempData["br_User"] = Baza.GetBrojUsera();
                TempData["br_Wiki"] = Baza.GetBrojWiki();
                TempData["br_QA"] = Baza.GetBrojQA();
                var listaTopPitanja = Baza.context.Questions.OrderByDescending(q => q.NumOfViews).Take(7).ToList();
                var listaTopWiki = Baza.context.Articles.OrderByDescending(q => q.Views).Take(7).ToList();

                HttpContext.Cache.Insert("TopListaWiki", listaTopWiki);
                HttpContext.Cache.Insert("TopListaPitanja", listaTopPitanja);

                ViewBag.Kategorija = Baza.GetKatogorije();
                ViewBag.AllCategory = Baza.context.Categories.ToList();
                ViewBag.brojKomentara = Baza.context.Questions.Count();
                var tagovi = Baza.context.Tags.Include(q=>q.Questions).OrderByDescending(q => q.Questions.Count).Take(50).ToList();
                var randomLista = tagovi.OrderBy(q => q.TagID).ToList();
                HttpContext.Cache.Insert("TopListaTagova", randomLista);

                if (tab == "popularno")
                {
                    var questions = Baza.context.Questions.Include(q => q.Categories).Include(q => q.User).Include(q => q.QuestionLikes).Include(q => q.Tags).Include(q => q.Answers).OrderByDescending(q => q.Likes);

                    if (Request.IsAjaxRequest())
                    {
                        return PartialView("_render_questions", questions.ToList().ToPagedList(page, 5));
                    }

                    return View(questions.ToList().ToPagedList(page, 2));
                }

                if (tab == "najcitanije")
                {
                    var questions = Baza.context.Questions.Include(q => q.Categories).Include(q => q.User).Include(q => q.Tags).Include(q=>q.QuestionLikes).Include(q => q.Answers).OrderByDescending(q => q.NumOfViews);

                    if (Request.IsAjaxRequest())
                    {
                        return PartialView("_render_questions", questions.ToList().ToPagedList(page, 5));
                    }

                    return View(questions.ToList().ToPagedList(page, 5));
                }
                else
                {

                    var questions = Baza.context.Questions.Include(q => q.Categories).Include(q => q.User).Include(q => q.Tags).Include(q => q.Answers).Include(q=>q.QuestionLikes).OrderByDescending(q => q.CreatedDate);
                    if (Request.IsAjaxRequest())
                    {
                        return PartialView("_render_questions", questions.ToList().ToPagedList(page, 5));
                    }

                    return View(questions.ToList().ToPagedList(page, 5));
                }

            }
        }
 public ActionResult Login(string User, string Pass)
 {
     using (DBBL DB = new DBBL())
     {
         Igman.DB.DAL.User u = DB.GetUserByUserAndPass(User, Pass);
         if (u != null)
             Autorizacija.Autorizacija.AddUserLogin(u, this.HttpContext);
         else
             TempData["wrongPass"] = true;
     }
     return RedirectToAction("index", "wellcome");
 }
 public string GetAI(string args)
 {
     LuceneEngine.LuceneDbEngine ldbe = new LuceneEngine.LuceneDbEngine();
     List<DB.DAL.Article> lista = null;
     List<int> ids = ldbe.AiComplete(args);
     args = GetIds(ids);
     using (DBBL Baza = new DBBL())
     {
         lista = Baza.GetAI(args);
     }
     return Newtonsoft.Json.JsonConvert.SerializeObject(lista);
 }
        public ActionResult get_questions_by_kategory(int id, int page = 1)
        {
            using (DBBL Baza = new DBBL())
            {
                List<int> listaIdPitanja = Baza.get_questionsIDs(id).ToList();
                List<Question> lista = new List<Question>();
                foreach (var item in listaIdPitanja)
                {

                    Question p = Baza.context.Questions.Include(q => q.Categories).Include(q => q.QuestionLikes).Include(q => q.Answers).Include(q=>q.Tags).Include(q=>q.User).Where(i => i.QuestionID == item).SingleOrDefault();
                    lista.Add(p);
                }
                return PartialView("_render_questions", lista.ToPagedList(page, 5));
            }
        }
        public List<ArticleRecommender> GetArtikliPreporucine(double ElasticnostFinal = 0.65, double ElasticnostTag = 0.65, double ElasticnostKategorije = 0.65, double ElasticnostOcjene = 0.35)
        {
            List<Article> la = new List<Article>();
            List<ArticleRecommender> listaPreporuka = new List<ArticleRecommender>();

            using (DBBL Baza = new DBBL())
            {
                la = Baza.GetAllWikis();
                wiki = this.wiki;

                VektorskaDuzina<Tag> vektorTagDomaci = new VektorskaDuzina<Tag>(wiki.Tags.ToArray());
                VektorskaDuzina<Category> vektorKategorijeDomaci = new VektorskaDuzina<Category>(wiki.Categories.ToArray());
                VektorskaDuzina<ArticlesRating> vektorRatingDomaci = new VektorskaDuzina<ArticlesRating>(wiki.ArticlesRatings.ToArray());

                foreach (var w in la)
                {
                    VektorskaDuzina<Tag> vektorTagExterni = new VektorskaDuzina<Tag>(w.Tags.ToArray());
                    ItemBase<Tag> ibTag = new ItemBase<Tag>(vektorTagDomaci, vektorTagExterni);

                    VektorskaDuzina<Category> vektorKategorijeExterni = new VektorskaDuzina<Category>(w.Categories.ToArray());
                    ItemBase<Category> ibKategorije = new ItemBase<Category>(vektorKategorijeDomaci, vektorKategorijeExterni);

                    VektorskaDuzina<ArticlesRating> vektorRatingExterni = new VektorskaDuzina<ArticlesRating>(w.ArticlesRatings.ToArray());
                    ItemBase<ArticlesRating> ibRating = new ItemBase<ArticlesRating>(vektorRatingDomaci, vektorRatingExterni);

                    double tpr = ibTag.GetSlicnost(false);
                    double kpr = ibKategorije.GetSlicnost(false);
                    double rpr = ibRating.GetSlicnost(false);
                    double pr = (tpr + kpr ) * 1/(double)2;
                    if (pr >= ElasticnostFinal && w.ArticlesID != wiki.ArticlesID)
                    {
                        listaPreporuka.Add(new ArticleRecommender()
                        {
                            Name = w.Name,
                            Score = pr,
                            WikiID = w.ArticlesID
                        });
                    }

                }
                return listaPreporuka.OrderByDescending(x=>x.Score).Take(10).ToList();

            }
        }
        public ActionResult Registration(Models.Registartion r)
        {
            if (ModelState.IsValid)
            {
                using (DBBL DB = new DBBL())
                {
                    //check postojeceg usera
                    Igman.DB.DAL.User u;

                    u = DB.GetUserByEmail(r.Email);
                    if (u != null)
                    {
                        TempData["ExistUser"] = u;
                        return RedirectToAction("index", "wellcome");
                    }
                    u = new Igman.DB.DAL.User();

                    u.FirstName = r.FirstName;
                    u.LastName = r.LastName;

                    u.LoweredEmail = r.Email.ToLower();
                    u.Email = r.Email;

                    u.Password = r.Password;
                    u.GUID = Guid.NewGuid();

                    u.IsApproved = true;
                    u.LastLogin = DateTime.Now;

                    u.Roles.Add(DB.GetRoleByID(2)); // User (2)

                    u = DB.AddUser(u);
                    Autorizacija.Autorizacija.AddUserLogin(u, this.HttpContext);
                }
            }
            return RedirectToAction("index", "wellcome");
        }
        public List<QuestionRecommender> GetQuestionsPreporke(double ElasticnostFinal = 0.65, double ElasticnostTag = 0.65, double ElasticnostKategorije = 0.65, double ElasticnostOcjene = 0.35)
        {
            List<Question> la = new List<Question>();
            List<QuestionRecommender> listaPreporuka = new List<QuestionRecommender>();

            using (DBBL Baza = new DBBL())
            {
                la = Baza.GetAllQuestios();

                question = this.question;

                VektorskaDuzina<Tag> vektorTagDomaci = new VektorskaDuzina<Tag>(question.Tags.ToArray());
                VektorskaDuzina<Category> vektorKategorijeDomaci = new VektorskaDuzina<Category>(question.Categories.ToArray());
                VektorskaDuzina<QuestionsRating> vektorRatingDomaci = new VektorskaDuzina<QuestionsRating>(question.QuestionsRatings.ToArray());

                foreach (var w in la)
                {
                    VektorskaDuzina<Tag> vektorTagExterni = new VektorskaDuzina<Tag>(w.Tags.ToArray());
                    ItemBase<Tag> ibTag = new ItemBase<Tag>(vektorTagDomaci, vektorTagExterni);

                    VektorskaDuzina<Category> vektorKategorijeExterni = new VektorskaDuzina<Category>(w.Categories.ToArray());
                    ItemBase<Category> ibKategorije = new ItemBase<Category>(vektorKategorijeDomaci, vektorKategorijeExterni);

                    VektorskaDuzina<QuestionsRating> vektorRatingExterni = new VektorskaDuzina<QuestionsRating>(w.QuestionsRatings.ToArray());
                    ItemBase<QuestionsRating> ibRating = new ItemBase<QuestionsRating>(vektorRatingDomaci, vektorRatingExterni);

                    double tpr = ibTag.GetSlicnost(false);
                    double kpr = ibKategorije.GetSlicnost(false);
                    double rpr = ibRating.GetSlicnost(false);
                    double pr = (tpr + kpr) * 1 / (double)2;
                    if (pr >= ElasticnostFinal && w.QuestionID != question.QuestionID)
                    {
                        listaPreporuka.Add(new QuestionRecommender()
                        {
                            Name = w.QuestionTitle,
                            Score = pr,
                            QuestionID = w.QuestionID,
                            CreatorID=w.User.UserID
                        });
                    }

                }
                return listaPreporuka.OrderByDescending(x => x.Score).Take(10).ToList();

            }
        }
        public ActionResult Wiki(int? id)
        {
            CheckUser();
            if (id.HasValue)
            {
                using (DBBL Baza = new DBBL())
                {
                    Article a = Baza.GetWikiByID(id.Value);
                    int rating = 0;
                    if (a != null)
                    {

                        int suma = 0;
                        if (a.ArticlesRatings.Count > 0)
                        {
                            foreach (var rat in a.ArticlesRatings.ToList())
                            {
                                suma += rat.Score.Value;
                            }

                            rating = (suma / a.ArticlesRatings.Count);
                        }

                        TempData["wiki"] = a;
                        TempData["tags"] = a.Tags.ToList();
                        TempData["grup"] = a.Categories.ToList();
                        TempData["likes"] = a.ArticlesLikes.ToList().Count;
                        TempData["rating"] = rating;
                        TempData["Kom"] = a.Comments.OrderByDescending(x => x.CommentID).ToList();

                        ExteranlBase eb = new ExteranlBase(a.Tags.ToList());

                        TempData["ExteranlWiki"] = eb.Preporuci();

                        Baza.IncremenViews(a.ArticlesID);

                        TempData["RecWiki"] = GetPreporuka(a);
                        return View();
                    }
                }
            }
            return RedirectToAction("Index", "Wellcome");
        }
        public ActionResult AddWiki(string Naslov, string Content, string jsonTag, string jsonKat)
        {
            Article a = new Article();
            a.Name = Server.HtmlDecode(Naslov).Replace("'", "&#39;").Trim();
            a.Content = Server.HtmlDecode(Content).Replace("'", "&#39;").Trim();
            a.CreatorIP = this.HttpContext.Request.GetIpAdresa();
            a.CreatorUserAgent = this.HttpContext.Request.UserAgent;
            a.DatePublish = DateTime.Now;
            a.GUID = Guid.NewGuid();
            a.IsActive = true;
            a.Views = 0;
            a.IsPublish = true;
            a.UserID = Autorizacija.Autorizacija.GetCurrentUser(this.HttpContext).UserID;

            var ListaTagova = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Models.Json.Tag>>(jsonTag);
            var ListaKategorija = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Models.Json.Kategorija>>(jsonKat);

            List<Igman.DB.DAL.Tag> listTempTag = SinhronyzeWithDB(ListaTagova.Where(x => x.TagID == "-1").ToList(), ListaTagova.Where(x => x.TagID != "-1").ToList());
            List<Category> listaKategorija = SinhronyzeWithDB(ListaKategorija);

            a.Categories = listaKategorija;
            a.Tags = listTempTag;

            using (DBBL Baza = new DBBL())
            {
                a = Baza.AddWiki(a);
            }

            #region Lucine
            LuceneEngine.LuceneDbEngine ldbe = new LuceneEngine.LuceneDbEngine();
            ldbe.InsertWiki(a);
            #endregion

            TempData["wikiSuccess"] = true;
            return RedirectToAction("index", "Articles");
        }
 public JsonResult GetTags(string arg)
 {
     List<Igman.DB.DAL.Tag> tags;
     using (DBBL Baza = new DBBL())
     {
         tags = Baza.GetTags(arg);
     }
     return Json(tags);
 }
        private List<Category> SinhronyzeWithDB(List<Models.Json.Kategorija> ListaKategorija)
        {
            List<Category> ltl = new List<Category>();
            foreach (var lista in ListaKategorija)
            {
                using (DBBL Baza = new DBBL())
                {
                    ltl.Add(Baza.GetCategotyById(lista.KategorijaID.ToInt()));
                }

            }
            return ltl;
        }
 private List<Igman.DB.DAL.Tag> SinhronyzeWithDB(List<Models.Json.Tag> list, List<Models.Json.Tag> insideList)
 {
     var lTag = new List<Igman.DB.DAL.Tag>();
     using (DBBL Baza = new DBBL())
     {
         foreach (var tag in list)
         {
             Igman.DB.DAL.Tag t = new Igman.DB.DAL.Tag();
             t.Name = tag.Naziv;
             t.GUID = Guid.NewGuid();
             lTag.Add(Baza.AddTag(t));
         }
     }
     foreach (var tag in insideList)
     {
         Igman.DB.DAL.Tag t = new Igman.DB.DAL.Tag();
         t.Name = tag.Naziv;
         t.GUID = Guid.NewGuid();
         t.TagID = tag.TagID.ToInt();
         lTag.Add(t);
     }
     return lTag;
 }
        private bool CheckRoleByUser(User loginUser, TipKorsnika tipKorsnika)
        {
            using (DBBL Baza = new DBBL())
            {

                Role r = Baza.usp_GetRoleByUserID(loginUser.UserID);
                if (r.RoleID == (int)tipKorsnika)
                    return true;
                return false;
            }
        }
 public ActionResult WikiEdit(int? id)
 {
     if (id.HasValue)
     {
         using (DBBL Baza = new DBBL())
         {
             ViewBag.Kategorije = Baza.GetKatogorije();
             Article w = Baza.GetWikiByID(id.Value);
             TempData["w"] = w;
             TempData["w-tag"] = w.Tags.ToList();
             TempData["w-cat"] = w.Categories.ToList();
         }
         CheckUser();
         return View();
     }
     else
         return View("index");
 }
 public ActionResult Index()
 {
     using (DBBL Baza = new DBBL())
     {
         ViewBag.Kategorije = Baza.GetKatogorije();
     }
     CheckUser();
     return View();
 }
        public ActionResult Like(int? id)
        {
            DB.DAL.User a = Autorizacija.Autorizacija.GetCurrentUser(this.HttpContext);
            if (a != null)
                TempData["user"] = a;

            Question q = null;
            int Uid = Autorizacija.Autorizacija.GetCurrentUser(this.HttpContext).UserID;
            string likes_new = "";
            if (id.HasValue)
            {
                using (DBBL Baza = new DBBL())
                {

                    QuestionLike ql = new QuestionLike()
                    {
                        QuestionID = id.Value,
                        UserID = Uid,
                        GUID = Guid.NewGuid(),
                        DateLike = DateTime.Now,
                        CreatorIP = this.Request.GetIpAdresa()
                    };
                    try
                    {
                        Baza.IncetrementLikesQuestion(ql);
                    }
                    catch (Exception)
                    {

                        return Content("False");
                    }

                    q = Baza.GetQuestionByID(id.Value);
                    likes_new = q.QuestionLikes.Count.ToString();
                }

            }

            return Json(new { likes = likes_new, id = id }, JsonRequestBehavior.AllowGet);
        }
        public ActionResult Search(string args, int? strana)
        {
            CheckUser();
            Stopwatch st = new Stopwatch();
            st.Start();
            if (string.IsNullOrEmpty(args) || string.IsNullOrWhiteSpace(args))
            {
                return View();
            }
            if (args.Count() > 35)
            {
                TempData["err"] = "Maximalan upit do 35 karaktera";
                return View();
            }
            if (!strana.HasValue)
                strana = 1;
            args = Formatiraj(args);

            LuceneEngine.LuceneDbEngine ldbe = new LuceneEngine.LuceneDbEngine();

            List<Rezultat> ids = ldbe.GetArticleIDByArg(args, false);

            string idsParams = GetIds(ids);
            string scores = GetScore(ids);
            List<DB.DalHelpClass.ArticleSerch.ArticleSerchModel> listaNadjenih;
            using (DBBL Baza = new DBBL())
            {
                listaNadjenih = Baza.PretragaWiki(idsParams, scores, strana.Value);

                st.Stop();
                if (listaNadjenih.Count > 0)
                {
                    TempData["stat"] = st.ElapsedMilliseconds / (double)1000;
                    TempData["lp"] = listaNadjenih;
                    TempData["args"] = args;
                }
                else
                {
                    List<Igman.DB.DAL.Tag> mislilac = Baza.GetDaliSteMilili(args);
                    TempData["mislilac"] = mislilac;

                }
            }
            return View();
        }
        public string Rating(int? id, int? score)
        {
            Article a = null;
            int Uid = Autorizacija.Autorizacija.GetCurrentUser(this.HttpContext).UserID;
            string rating = "";
            if (id.HasValue && score.HasValue)
            {
                using (DBBL Baza = new DBBL())
                {

                    ArticlesRating ar = new ArticlesRating()
                    {
                        ArticlesID = id.Value,
                        UserID = Uid,
                        GUID = Guid.NewGuid(),
                        DateRating = DateTime.Now,
                        Score = score
                    };
                    try
                    {
                        Baza.AddRating(ar);
                    }
                    catch (Exception)
                    {

                        return "False";
                    }

                    a = Baza.GetWikiByID(id.Value);
                    int suma = 0;
                    foreach (var rat in a.ArticlesRatings.ToList())
                    {
                        suma += rat.Score.Value;
                    }
                    rating = (suma / a.ArticlesRatings.Count).ToString();
                }

            }
            return rating;
        }
        public string Like(int? id)
        {
            CheckUser();
            Article a = null;
            int Uid = Autorizacija.Autorizacija.GetCurrentUser(this.HttpContext).UserID;
            string likes = "";
            if (id.HasValue)
            {
                using (DBBL Baza = new DBBL())
                {

                    ArticlesLike al = new ArticlesLike()
                    {
                        ArticleID = id.Value,
                        UserID = Uid,
                        GUID = Guid.NewGuid(),
                        DateLike = DateTime.Now,
                        CreatorIP = this.Request.GetIpAdresa()
                    };
                    try
                    {
                        Baza.IncetrementLikes(al);
                    }
                    catch (Exception)
                    {

                        return "False";
                    }

                    a = Baza.GetWikiByID(id.Value);
                    likes = a.ArticlesLikes.Count.ToString();
                }

            }
            return likes;
        }
 public ActionResult Tag(int? id, int? strana)
 {
     CheckUser();
     if (!strana.HasValue)
         strana = 1;
     if (id.HasValue)
     {
         using (DBBL Baza = new DBBL())
         {
             List<DB.DalHelpClass.ArticleSerch.ArticleSerchModel> listaFromTag = Baza.GetTagByIDWikis(strana.Value, id.Value);
             if (listaFromTag.Count == 0)
                 return RedirectToAction("index", "wellcome");
             TempData["artcles"] = listaFromTag;
             return View();
         }
     }
     return RedirectToAction("index", "wellcome");
 }
        public ActionResult Search(string args, int page = 1)
        {
            CheckUser();
            Stopwatch st = new Stopwatch();
            st.Start();
            if (string.IsNullOrEmpty(args) || string.IsNullOrWhiteSpace(args))
            {
                return View();
            }
            if (args.Count() > 35)
            {
                TempData["err"] = "Maximalan upit do 35 karaktera";
                return View();
            }
            args = Formatiraj(args);

            #region Lucine
            //if (!strana.HasValue)
            //    strana = 1;
            //args = Formatiraj(args);
            //LuceneEngine.LuceneDbEngine ldbe = new LuceneEngine.LuceneDbEngine();
            //List<Rezultat> ids = ldbe.GetArticleIDByArg(args, false);
            //string idsParams = GetIds(ids);
            //string scores = GetScore(ids);
            //List<DB.DalHelpClass.ArticleSerch.ArticleSerchModel> listaNadjenih;
            #endregion
            using (DBBL Baza = new DBBL())
            {
                var listaNadjenih = Baza.GetPitanja(args);

                st.Stop();
                var br_rez = listaNadjenih.Count();
                if (br_rez > 0)
                {
                    TempData["stat"] = st.ElapsedMilliseconds / (double)1000;
                    TempData["lp"] = listaNadjenih;
                    TempData["args"] = args;
                    TempData["br_rez"] = br_rez;

                }

                else
                {
                    List<Igman.DB.DAL.Tag> mislilac = Baza.GetDaliSteMilili(args);
                    TempData["args"] = args;
                    TempData["mislilac"] = mislilac;

                }

                if (Request.IsAjaxRequest())
                {

                    return PartialView("_QAPretraga", listaNadjenih.ToPagedList(page, 5));
                }
                return View(listaNadjenih.ToPagedList(page, 5));
            }
        }