private AnsweringBodyAllocation giveMeAnsweringBodyAllocation(Response data)
        {
            AnsweringBodyAllocation answeringBodyAllocation = null;

            if (data.AnsweringDeptSesId != null)
            {
                Uri answeringBodyId = IdRetrieval.GetSubject("sesId", data.AnsweringDeptSesId, false, logger);
                if (answeringBodyId != null)
                {
                    answeringBodyAllocation = new AnsweringBodyAllocation()
                    {
                        Id = GenerateNewId(),
                        AnsweringBodyAllocationHasAnsweringBody = new AnsweringBody()
                        {
                            Id = answeringBodyId
                        }
                    };
                    WrittenAnswer writtenAnswer = giveMeWrittenAnswer(data);
                    if (writtenAnswer != null)
                    {
                        answeringBodyAllocation
                        .AnsweringBodyAllocationHasAnsweringBody
                        .AnsweringBodyHasWrittenAnswer = new WrittenAnswer[] { writtenAnswer }
                    }
                    ;
                }
                else
                {
                    logger.Warning($"Answering body with Ses Id ({data.AnsweringDeptSesId}) not found");
                }
            }

            return(answeringBodyAllocation);
        }
        public override BaseResource[] SynchronizeIds(BaseResource[] source, Uri subjectUri, BaseResource[] target)
        {
            IndexingAndSearchWrittenQuestion question = source.OfType <IndexingAndSearchWrittenQuestion>().SingleOrDefault();

            question.Id = subjectUri;
            if ((question.QuestionHasAnsweringBodyAllocation != null) && (question.QuestionHasAnsweringBodyAllocation.Any()))
            {
                AnsweringBodyAllocation answeringBodyAllocationTarget = target.OfType <AnsweringBodyAllocation>().SingleOrDefault();
                if (answeringBodyAllocationTarget != null)
                {
                    question.QuestionHasAnsweringBodyAllocation.SingleOrDefault().Id = answeringBodyAllocationTarget.Id;
                }
                if (question.QuestionHasAnsweringBodyAllocation.SingleOrDefault()?.AnsweringBodyAllocationHasAnsweringBody?.AnsweringBodyHasWrittenAnswer != null)
                {
                    question.QuestionHasAnsweringBodyAllocation
                    .SingleOrDefault()
                    .AnsweringBodyAllocationHasAnsweringBody
                    .AnsweringBodyHasWrittenAnswer
                    .SingleOrDefault()
                    .AnswerHasQuestion = new Question[]
                    {
                        new Question()
                        {
                            Id = subjectUri
                        }
                    };
                    WrittenAnswer answerTarget = target.OfType <WrittenAnswer>().SingleOrDefault();
                    if (answerTarget != null)
                    {
                        question.QuestionHasAnsweringBodyAllocation
                        .SingleOrDefault()
                        .AnsweringBodyAllocationHasAnsweringBody
                        .AnsweringBodyHasWrittenAnswer
                        .SingleOrDefault()
                        .Id = answerTarget.Id;
                    }
                }
            }
            if ((question.QuestionHasWrittenAnswerExpectation != null) && (question.QuestionHasWrittenAnswerExpectation.Any()))
            {
                WrittenAnswerExpectation expectationTarget = target.OfType <WrittenAnswerExpectation>().SingleOrDefault();
                if (expectationTarget != null)
                {
                    question.QuestionHasWrittenAnswerExpectation
                    .SingleOrDefault()
                    .Id = expectationTarget.Id;
                }
            }

            return(new BaseResource[] { question });
        }
        private AnsweringBodyAllocation giveMeCorrectingAnsweringBodyAllocation(Response data, Uri questionId)
        {
            AnsweringBodyAllocation answeringBodyAllocation = null;

            if (data.CorrectingAnsweringDeptSesId != null)
            {
                Uri answeringBodyId = IdRetrieval.GetSubject("sesId", data.CorrectingAnsweringDeptSesId, false, logger);
                if (answeringBodyId != null)
                {
                    string command = @"
                        construct{
                            ?question parl:questionHasAnsweringBodyAllocation ?questionHasAnsweringBodyAllocation.
                        }
                        where{
                            bind(@question as ?question)
                            ?question parl:questionHasAnsweringBodyAllocation ?questionHasAnsweringBodyAllocation.
                            ?questionHasAnsweringBodyAllocation parl:answeringBodyAllocationHasAnsweringBody @answeringBody.
                        }";
                    SparqlParameterizedString sparql = new SparqlParameterizedString(command);
                    sparql.Namespaces.AddNamespace("parl", new Uri(schemaNamespace));
                    sparql.SetUri("question", questionId);
                    sparql.SetUri("answeringBody", answeringBodyId);
                    IGraph graph = GraphRetrieval.GetGraph(sparql.ToString(), logger, "true");
                    answeringBodyAllocation = new AnsweringBodyAllocation()
                    {
                        Id = graph.IsEmpty ? GenerateNewId() : (graph.Triples.SingleOrDefault().Object as IUriNode).Uri,
                        AnsweringBodyAllocationHasAnsweringBody = new AnsweringBody()
                        {
                            Id = answeringBodyId
                        }
                    };
                }
                else
                {
                    logger.Warning($"Answering body with Ses Id ({data.CorrectingAnsweringDeptSesId}) not found");
                }
            }

            return(answeringBodyAllocation);
        }
        public override BaseResource[] TransformSource(XDocument doc)
        {
            var questionElements = doc?.Element("response")?.Element("result")?.Element("doc")?.Elements("arr")?.ToList();

            if (questionElements == null)
            {
                return(null);
            }
            Response data = new Response();

            data.QuestionUri = FindXElementByAttributeName(questionElements, "correctedItem_uri", "str").GetText();
            if (data.QuestionUri == null)
            {
                data.QuestionUri = FindXElementByAttributeName(questionElements, "correctedItem_t", "str").GetText();
            }
            questionUriText = data.QuestionUri;
            data.CorrectingAnsweringDeptSesId   = FindXElementByAttributeName(questionElements, "answeringDept_ses", "int").GetText();
            data.CorrectingAnsweringMemberSesId = FindXElementByAttributeName(questionElements, "correctingMember_ses", "int").GetText();
            data.CorrectingAnswerText           = FindXElementByAttributeName(questionElements, "content_t", "str").GetText();

            var dateElements = doc.Element("response").Element("result").Element("doc").Elements("date").ToList();

            data.CorrectingDateOfAnswer = dateElements.Where(x => x.Attribute("name").Value == "date_dt").FirstOrDefault().GetDate();

            CorrectingAnswer correctingAnswer = new CorrectingAnswer();

            correctingAnswer.Id              = GenerateNewId();
            correctingAnswer.AnswerText      = new string[] { data.CorrectingAnswerText };
            correctingAnswer.AnswerGivenDate = data.CorrectingDateOfAnswer;
            if (string.IsNullOrWhiteSpace(data.CorrectingAnsweringMemberSesId))
            {
                logger.Warning("No information about correcting minister");
            }
            else
            {
                Uri ministerId = GetMemberId(data.CorrectingAnsweringMemberSesId, data.CorrectingDateOfAnswer, logger);

                if (ministerId != null)
                {
                    correctingAnswer.AnswerHasAnsweringPerson = new Person[]
                    {
                        new Person()
                        {
                            Id = ministerId
                        }
                    }
                }
                ;
                else
                {
                    logger.Warning($"Minister with Ses Id ({data.CorrectingAnsweringMemberSesId}) not found");
                }
            }
            Uri questionId = IdRetrieval.GetSubject("indexingAndSearchUri", data.QuestionUri, false, logger);

            Question question = null;

            if (questionId != null)
            {
                question = new Question()
                {
                    Id = questionId
                };
                correctingAnswer.CorrectingAnswerHasQuestion = new Question[] { question };
            }
            else
            {
                logger.Warning($"Question with Uri ({data.QuestionUri}) not found");
                return(null);
            }

            Answer originalAnswer = giveMeOriginalAnswer(questionId);

            if (originalAnswer != null)
            {
                correctingAnswer.AnswerReplacesAnswer = originalAnswer;
            }
            else
            {
                logger.Warning($"No answer found to replace for question with Uri ({data.QuestionUri})");
                return(null);
            }

            AnsweringBodyAllocation correctingAnsweringBodyAllocation = giveMeCorrectingAnsweringBodyAllocation(data, questionId);

            if (correctingAnsweringBodyAllocation != null)
            {
                question.QuestionHasAnsweringBodyAllocation = new AnsweringBodyAllocation[] { correctingAnsweringBodyAllocation };
                correctingAnsweringBodyAllocation.AnsweringBodyAllocationHasAnsweringBody.AnsweringBodyHasWrittenAnswer
                    = new WrittenAnswer[] { new WrittenAnswer()
                                            {
                                                Id = correctingAnswer.Id
                                            } };
            }
            else
            {
                return(null);
            }

            IndexingAndSearchThing iast = new IndexingAndSearchThing();
            var uriElements             = doc.Element("response").Element("result").Element("doc").Elements("str").ToList();

            iast.IndexingAndSearchUri = new String[] { uriElements.Where(x => x.Attribute("name").Value == "uri").FirstOrDefault().GetText() };

            return(new BaseResource[] { correctingAnswer, iast });
        }
        public override BaseResource[] TransformSource(XDocument doc)
        {
            var arrElements = doc?.Element("response")?.Element("result")?.Element("doc")?.Elements("arr")?.ToList();
            var strElements = doc?.Element("response")?.Element("result")?.Element("doc")?.Elements("str")?.ToList();

            if ((arrElements == null) || (strElements == null))
            {
                return(null);
            }

            Response data = new Response();

            data.DateTabled           = FindXElementByAttributeName(arrElements, "dateTabled_dt", "date").GetDate();
            data.QuestionText         = FindXElementByAttributeName(arrElements, "questionText_t", "str").GetText();
            data.QuestionHeading      = strElements.Where(x => x.Attribute("name").Value == "title_t").FirstOrDefault().GetText();
            data.AskingMemberSesId    = FindXElementByAttributeName(arrElements, "askingMember_ses", "int").GetText();
            data.AnsweringDeptSesId   = FindXElementByAttributeName(arrElements, "answeringDept_ses", "int").GetText();
            data.HeadingDueDate       = FindXElementByAttributeName(arrElements, "headingDueDate_dt", "date").GetDate();
            data.AnswerText           = FindXElementByAttributeName(arrElements, "answerText_t", "str").GetText();
            data.DateOfAnswer         = FindXElementByAttributeName(arrElements, "dateOfAnswer_dt", "date").GetDate();
            data.AnsweringMemberSesId = FindXElementByAttributeName(arrElements, "answeringMember_ses", "int").GetText();
            data.DateForAnswer        = FindXElementByAttributeName(arrElements, "dateForAnswer_dt", "date").GetDate();
            data.Uin = FindXElementByAttributeName(arrElements, "uin_t", "str").GetText();

            IndexingAndSearchWrittenQuestion question = new IndexingAndSearchWrittenQuestion();

            question.QuestionAskedAt = data.DateTabled;
            question.QuestionHeading = data.QuestionHeading;
            question.QuestionText    = data.QuestionText;
            if (string.IsNullOrWhiteSpace(data.Uin) == false)
            {
                question.WrittenQuestionIndexingAndSearchUin = new string[] { data.Uin }
            }
            ;

            question.IndexingAndSearchUri = new String[] { strElements.Where(x => x.Attribute("name").Value == "uri").FirstOrDefault().GetText() };

            if (data.AskingMemberSesId != null)
            {
                Uri memberId = GetMemberId(data.AskingMemberSesId, data.DateTabled == null ? data.DateOfAnswer : data.DateTabled, logger);
                if (memberId != null)
                {
                    question.QuestionHasAskingPerson = new Person[]
                    {
                        new Person()
                        {
                            Id = memberId
                        }
                    }
                }
                ;
                else
                {
                    logger.Warning($"Member with Ses Id ({data.AskingMemberSesId}) not found");
                }
            }

            AnsweringBodyAllocation answeringBodyAllocation = giveMeAnsweringBodyAllocation(data);

            if (answeringBodyAllocation != null)
            {
                question.QuestionHasAnsweringBodyAllocation = new AnsweringBodyAllocation[] { answeringBodyAllocation }
            }
            ;
            WrittenAnswerExpectation writtenAnswerExpectation = giveMeWrittenAnswerExpectation(data);

            if (writtenAnswerExpectation != null)
            {
                question.QuestionHasWrittenAnswerExpectation = new WrittenAnswerExpectation[] { writtenAnswerExpectation }
            }
            ;

            return(new BaseResource[] { question });
        }