public ActionResult DeleteOutputRelation(Knowledge knowledge)
        {
            string idOfRelationEnd = Request["IdOfRelationEnd"];
            ObjectId id = ObjectId.Parse(idOfRelationEnd);
            //_dbContext.OntologyRelationsCollection.DeleteOneAsync(x => x.EndId == id);
            foreach (var relation in knowledge.Outputs)
            {
                if (relation.EndId == id)
                {
                    if(knowledge.Id != ObjectId.Empty)  //If knowledge is just created and relations are not in DB
                        knowledge.RelationsToDelete.Add(relation);
                    knowledge.Outputs.Remove(relation);
                    break;
                }
            }

            if (knowledge.Id == ObjectId.Empty)
            {
                _logger.Trace("Delete output relation from knowledge during knowledge creation.");
                return View("CreateKnowledge", knowledge);
            }
            else
            {
                _logger.Trace("Delete output relation from knowledge during knowledge editing.");
                return View("EditKnowledge", knowledge);
            }
        }
        public ActionResult EditKnowledge(Knowledge knowledge)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    _dbContext.KnowledgesCollection.ReplaceOneAsync(x => x.Id == knowledge.Id, knowledge);
                    List<Relation> relations = new List<Relation>();
                    relations.AddRange(knowledge.Inputs);
                    relations.AddRange(knowledge.Outputs);

                    foreach (var relation in relations)
                    {
                        var relation1 = relation;
                        _dbContext.OntologyRelationsCollection.ReplaceOneAsync(x => x.Id == relation1.Id, relation);
                    }

                    foreach (var relation in knowledge.RelationsToDelete)
                    {
                        var relation1 = relation;
                        _dbContext.OntologyRelationsCollection.DeleteOneAsync(x => x.Id == relation1.Id);
                    }

                    _logger.Trace("knowledge editing.");
                    return RedirectToAction("Ontology", "Home");
                }
                catch (Exception ex)
                {
                    _logger.Trace("knowledge editing. Exception: " + ex.Message);
                    string exceptionMessage = ex.Message;
                    string wholeMessage = @"<script language=""javascript"">alert('\n" + "Error during saving to database\n" + exceptionMessage + @"\n')</script>";
                    Response.Write(wholeMessage);
                }
            }
            else
            {
                _logger.Trace("knowledge editing. Model is not valid.");
                return View(knowledge);
            }

            return RedirectToAction("Ontology", "Home");
        }
 public ActionResult CreateKnowledge()
 {
     _logger.Trace("Create knowledge page request.");
     Knowledge knowledge = new Knowledge();
     return View(knowledge);
 }
        public ActionResult CreateKnowledge(Knowledge knowledge)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    _dbContext.KnowledgesCollection.InsertOneAsync(knowledge);

                    foreach (var input in knowledge.Inputs)
                    {
                        input.EndId = knowledge.Id;
                    }

                    foreach (var output in knowledge.Outputs)
                    {
                        output.StartId = knowledge.Id;
                    }

                    List<Relation> knowledgeRelations = new List<Relation>();
                    knowledgeRelations.AddRange(knowledge.Inputs);
                    knowledgeRelations.AddRange(knowledge.Outputs);

                    foreach (var knowledgeRelation in knowledgeRelations)
                    {
                        _dbContext.OntologyRelationsCollection.InsertOneAsync(knowledgeRelation);
                    }
                    _logger.Trace("Create knowledge.");
                    return RedirectToAction("Ontology", "Home");
                }
                catch (Exception ex)
                {
                    _logger.Trace("Create knowledge. Exception: " + ex.Message);
                    string exceptionMessage = ex.Message;
                    string wholeMessage = @"<script language=""javascript"">alert('\n" + "Error during saving to database\n" + exceptionMessage + @"\n')</script>";
                    Response.Write(wholeMessage);
                }
            }

            _logger.Trace("Create knowledge. Model is not valid.");
            return View(knowledge);
        }
        public ActionResult AddSelectedKnowledgesAsOutputs(Knowledge knowledge)
        {
            string stringOfKnowledgesIds = Request["selectedIds"];
            if (!stringOfKnowledgesIds.IsEmpty())
            {
                HashSet<string> listOfKnIds = new HashSet<string>(stringOfKnowledgesIds.Split(',').ToList());
                foreach (var id in listOfKnIds)
                {
                    //var lo = db.GetLOByID(id);
                    var id1 = id;
                    var kn = _dbContext.KnowledgesCollection.Find(x => x.Id == ObjectId.Parse(id1)).SingleOrDefaultAsync().Result;
                    if (kn != null)
                    {
                        knowledge.Outputs.Add(new Relation(knowledge.Id, Relation.RelationType.NoConnection, kn.Id));
                    }
                }
            }

            if (knowledge.Id == ObjectId.Empty)
            {
                _logger.Trace("Knowledge or knowledges added as outputs to knowledge during knowledge creating");
                return View("CreateKnowledge", knowledge);
            }
            else
            {
                _logger.Trace("Knowledge or knowledges added as outputs to knowledge during knowledge editing");
                return View("EditKnowledge", knowledge);
            }
        }
        public ActionResult AddSelectedKnowledgesAsInputs(Knowledge knowledge)
        {
            string stringOfKnowledgesIds = Request["selectedIds"];
            if (!stringOfKnowledgesIds.IsEmpty())
            {
                HashSet<string> listOfKnIds = new HashSet<string>(stringOfKnowledgesIds.Split(',').ToList());
                foreach (var id in listOfKnIds)
                {
                    //var lo = db.GetLOByID(id);
                    var kn = _dbContext.Ontology.Find(x => x.Id == ObjectId.Parse(id)).SingleOrDefaultAsync().Result;
                    if (kn != null)
                    {
                        knowledge.Inputs.Add(new Relation(kn.Id, Relation.RelationType.NoConnection, knowledge.Id));
                    }
                }
            }

            if (knowledge.Id == ObjectId.Empty)
            {
                return View("CreateKnowledge", knowledge);
            }
            else
            {
                return View("EditKnowledge", knowledge);
            }
        }
        public ActionResult EditKnowledge(Knowledge knowledge)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    _dbContext.Ontology.ReplaceOneAsync(x => x.Id == knowledge.Id, knowledge);
                    return RedirectToAction("Ontology", "Home");
                }
                catch (Exception ex)
                {
                    string exceptionMessage = ex.Message;
                    string wholeMessage = @"<script language=""javascript"">alert('\n" + "Error during saving to database\n" + exceptionMessage + @"\n')</script>";
                    Response.Write(wholeMessage);
                }
            }
            else
            {
                return View(knowledge);
            }

            return RedirectToAction("Ontology", "Home");
        }
        public ActionResult DeleteOutputRelation(Knowledge knowledge)
        {
            string idOfRelationEnd = Request["IdOfRelationEnd"];
            ObjectId id = ObjectId.Parse(idOfRelationEnd);

            foreach (var relation in knowledge.Outputs)
            {
                if (relation.EndId == id)
                {
                    knowledge.Outputs.Remove(relation);
                    break;
                }
            }

            if (knowledge.Id == ObjectId.Empty)
            {
                return View("CreateKnowledge", knowledge);
            }
            else
            {
                return View("EditKnowledge", knowledge);
            }
        }
 public ActionResult CreateKnowledge()
 {
     Knowledge knowledge = new Knowledge();
     return View(knowledge);
 }