示例#1
0
        public async Task DataPortal_Create(Criteria.TranslationSearchCriteria criteria)
        {
            RetrieverId = Guid.NewGuid();
            //Translation = TranslationEdit.NewTranslationEdit();

            using (var dalManager = DalFactory.GetDalManager())
            {
                var phraseDal = dalManager.GetProvider <IPhraseDal>();
                Result <ICollection <PhraseDto> > result = phraseDal.GetAll();
                if (!result.IsSuccess)
                {
                    Exception error = result.GetExceptionFromInfo();
                    if (error != null)
                    {
                        throw error;
                    }
                    else
                    {
                        throw new FetchFailedException(result.Msg);
                    }
                }

                var allPhraseDtos = result.Obj;


                //WE HAVE A LIST OF ALL THE PHRASES.  NOW SEARCH THROUGH FOR OUR PHRASE.
                var foundPhraseDto = (from phraseDto in allPhraseDtos
                                      where phraseDto.Text == criteria.Phrase.Text &&
                                      phraseDto.LanguageId == criteria.Phrase.LanguageId
                                      select phraseDto).FirstOrDefault();

                //ASSUME NO TRANSLATION
                Translation = null;

                //IF WE HAVEN'T FOUND A PHRASE, THEN WE WON'T FIND A TRANSLATION.
                if (foundPhraseDto == null)
                {
                    return;
                }

                //WE FOUND A PHRASE, BUT WE STILL NEED TO LOOK FOR TRANSLATION FOR THAT PHRASE.
                var phraseEdit = DataPortal.Fetch <PhraseEdit>(foundPhraseDto.Id);
                var translationsContainingPhrase = TranslationList.GetAllTranslationsContainingPhraseById(phraseEdit);

                if (translationsContainingPhrase.Count == 0)
                {
                    await TrySearchOnline(criteria);

                    return;
                }

                //WE FOUND TRANSLATIONS WITH THAT PHRASE, BUT WE NEED THE ONES IN THE TARGET LANGUAGE ONLY
                var translationsInTargetLanguage = (from translation in translationsContainingPhrase
                                                    where (from phrase in translation.Phrases
                                                           where phrase.Language.Text == criteria.TargetLanguageText
                                                           select phrase).Count() > 0
                                                    select translation);
                if (translationsInTargetLanguage.Count() == 0)
                {
                    await TrySearchOnline(criteria);

                    return;
                }

                //WE FOUND TRANSLATIONS IN THE TARGET LANGUAGE, AND WE MUST NOW CHECK AGAINST CONTEXT (IF PROVIDED)
                if (!string.IsNullOrEmpty(criteria.ContextText))
                {
                    //CONTEXT TEXT HAS BEEN PROVIDED
                    Translation = (from t in translationsInTargetLanguage
                                   where t.ContextPhrase != null &&
                                   t.ContextPhrase.Text == criteria.ContextText
                                   select t).FirstOrDefault();
                }
                else
                {
                    //CONTEXT TEXT HAS NOT BEEN PROVIDED
                    //IF WE FOUND ONE, THIS SETS TRANSLATION TO THE FIRST TRANSLATION FOUND.
                    Translation = translationsInTargetLanguage.First();
                }

                //IF THE TRANSLATION IS STILL NULL, TRY TO SEARCH ONLINE.
                if (Translation == null)
                {
                    await TrySearchOnline(criteria);
                }
            }
        }
示例#2
0
        public void DataPortal_Create(Criteria.TranslationPairSearchCriteria criteria)
        {
            RetrieverId = Guid.NewGuid();
            Translation = null;
            //Translation = TranslationEdit.NewTranslationEdit();

            using (var dalManager = DalFactory.GetDalManager())
            {
                var phraseDal = dalManager.GetProvider <IPhraseDal>();
                Result <ICollection <PhraseDto> > result = phraseDal.GetAll();
                if (!result.IsSuccess)
                {
                    Exception error = result.GetExceptionFromInfo();
                    if (error != null)
                    {
                        throw error;
                    }
                    else
                    {
                        throw new FetchFailedException(result.Msg);
                    }
                }

                var allPhraseDtos = result.Obj;


                //WE HAVE A LIST OF ALL THE PHRASES.  NOW SEARCH THROUGH FOR OUR PHRASES.

                //PHRASE A
                var foundPhraseADto = (from phraseDto in allPhraseDtos
                                       where phraseDto.Text == criteria.PhraseA.Text &&
                                       phraseDto.LanguageId == criteria.PhraseA.LanguageId
                                       select phraseDto).FirstOrDefault();

                if (foundPhraseADto == null)
                {
                    //PHRASE A WAS NOT FOUND IN DB, SO THERE IS NO TRANSLATION.
                    return;
                }

                //PHRASE B
                var foundPhraseBDto = (from phraseDto in allPhraseDtos
                                       where phraseDto.Text == criteria.PhraseB.Text &&
                                       phraseDto.LanguageId == criteria.PhraseB.LanguageId
                                       select phraseDto).FirstOrDefault();

                if (foundPhraseBDto == null)
                {
                    //PHRASE A WAS NOT FOUND IN DB, SO THERE IS NO TRANSLATION.
                    return;
                }


                //WE FOUND BOTH PHRASES, NOW WE LOOK FOR TRANSLATION WITH PHRASE A.  WE WILL THEN
                //LOOK WITHIN THOSE RESULTS FOR PHRASE B.  WE WILL THEN NARROW ANY POSSIBLE MULTIPLE
                //TRANSLATIONS WITH CONTEXT TEXT.


                //LOOK FOR TRANSLATION WITH PHRASE A
                var phraseAEdit = DataPortal.Fetch <PhraseEdit>(foundPhraseADto.Id);
                var translationsContainingPhraseA = TranslationList.GetAllTranslationsContainingPhraseById(phraseAEdit);

                //IF WE FIND NO TRANSLATIONS WITH PHRASE A, THEN WE RETURN WITH NO TRANSLATION FOUND
                if (translationsContainingPhraseA.Count == 0)
                {
                    return;
                }

                //WE FOUND TRANSLATIONS WITH PHRASE A
                //NOW LOOK WITHIN RESULTS FOR PHRASE B
                var translationsWithBothPhrases = (from translation in translationsContainingPhraseA
                                                   where (from phrase in translation.Phrases
                                                          where phrase.Id == foundPhraseBDto.Id
                                                          select phrase).Count() > 0
                                                   select translation);

                //THERE ARE NO TRANSLATIONS WITH BOTH PHRASES, SO RETURN WITH TRANSLATION == NULL
                if (translationsWithBothPhrases.Count() == 0)
                {
                    return;
                }


                //WE FOUND TRANSLATIONS CONTAINING BOTH PHRASES,
                //AND WE MUST NOW CHECK AGAINST CONTEXT (IF PROVIDED)
                if (!string.IsNullOrEmpty(criteria.ContextText))
                {
                    //CONTEXT TEXT HAS BEEN PROVIDED
                    //WE CHOOSE [SQL].FIRSTORDEFAULT() BECAUSE IF WE CANNOT MATCH THE CONTEXT, THEN OUR SEARCH
                    //SHOULD RETURN NULL
                    Translation = (from t in translationsWithBothPhrases
                                   where t.ContextPhrase != null &&
                                   t.ContextPhrase.Text == criteria.ContextText
                                   select t).FirstOrDefault();
                }
                else
                {
                    //CONTEXT TEXT HAS NOT BEEN PROVIDED
                    //WE KNOW WE HAVE FOUND AT LEAST ONE, AND THIS SETS TRANSLATION TO THE FIRST TRANSLATION FOUND.
                    Translation = translationsWithBothPhrases.First();
                }
            }
        }