/// ------------------------------------------------------------------------------------
        /// <summary>
        /// Forces a save of lexicon
        /// </summary>
        /// ------------------------------------------------------------------------------------
        public void Save()
        {
            LexicalProviderManager.ResetLexicalProviderTimer();

            // ENHANCE: We could do a save on the cache here. It doesn't seem really important
            // since the cache will be saved automatically with 10 seconds of inactivity anyways.
        }
        /// ------------------------------------------------------------------------------------
        /// <summary>
        /// Displays the related words to the specified entry using the application with the
        /// lexical data.
        /// </summary>
        /// ------------------------------------------------------------------------------------
        public void ShowRelatedWords(string entry, EntryType entryType)
        {
            LexicalProviderManager.ResetLexicalProviderTimer();
            Logger.WriteEvent("Showing related word from external application for the " + entryType + " " + entry);

            if (entryType != EntryType.Word)
            {
                throw new ArgumentException("Unknown entry type specified.");
            }

            // An asynchronous call is necessary because the WCF server (FieldWorks) will not
            // respond until this method returns. This also allows methods that show dialogs on the
            // WCF server to not be OneWay. (Otherwise, time-out exceptions occur.)
            FieldWorks.ThreadHelper.InvokeAsync(() =>
            {
                ITsString tss                 = TsStringUtils.MakeTss(entry, FieldWorks.Cache.DefaultVernWs);
                Mediator mediator             = new Mediator();
                mediator.HelpTopicProvider    = FieldWorks.GetHelpTopicProvider(FwUtils.ksFlexAbbrev);
                mediator.FeedbackInfoProvider = FieldWorks.GetOrCreateFlexApp();
                mediator.PropertyTable.SetProperty("App", FieldWorks.GetOrCreateFlexApp());

                LexEntryUi.DisplayRelatedEntries(FieldWorks.Cache, mediator, mediator.HelpTopicProvider,
                                                 "UserHelpFile", tss);
            });
        }
        /// ------------------------------------------------------------------------------------
        /// <summary>
        /// Adds the lexeme to the lexicon.
        /// </summary>
        /// <exception cref="ArgumentException">if matching lexeme is already in lexicon</exception>
        /// ------------------------------------------------------------------------------------
        public void AddLexeme(LexicalEntry lexeme)
        {
            LexicalProviderManager.ResetLexicalProviderTimer();
            Logger.WriteEvent("Adding new lexeme from an external application: " + lexeme.LexicalForm);

            NonUndoableUnitOfWorkHelper.Do(m_cache.ActionHandlerAccessor, () =>
            {
                string sForm = lexeme.LexicalForm;
                switch (lexeme.Type)
                {
                case LexemeType.Word:
                    ITsString tss = TsStringUtils.MakeTss(lexeme.LexicalForm, m_cache.DefaultVernWs);
                    m_cache.ServiceLocator.GetInstance <IWfiWordformFactory>().Create(tss);
                    break;

                default:
                    {
                        SandboxGenericMSA msa = new SandboxGenericMSA();
                        msa.MsaType           = (lexeme.Type == LexemeType.Stem) ? MsaType.kStem : MsaType.kUnclassified;

                        IMoMorphType morphType = GetMorphTypeForLexemeType(lexeme.Type);
                        ITsString tssForm      = TsStringUtils.MakeTss(sForm, m_cache.DefaultVernWs);
                        m_cache.ServiceLocator.GetInstance <ILexEntryFactory>().Create(morphType, tssForm, null, msa);
                        break;
                    }
                }
            });
        }
Beispiel #4
0
        /// ------------------------------------------------------------------------------------
        /// <summary>
        /// Displays the specified entry using the application with the lexical data.
        /// </summary>
        /// ------------------------------------------------------------------------------------
        public void ShowEntry(string entry, EntryType entryType)
        {
            LexicalProviderManager.ResetLexicalProviderTimer();
            Logger.WriteEvent("Showing entry from external application for the " + entryType + " " + entry);

            if (entryType != EntryType.Word)
            {
                throw new ArgumentException("Unknown entry type specified.");
            }

            // An asynchronous call is necessary because the WCF server (FieldWorks) will not
            // respond until this method returns. This also allows methods that show dialogs on the
            // WCF server to not be OneWay. (Otherwise, time-out exceptions occur.)
            FieldWorks.ThreadHelper.InvokeAsync(() =>
            {
                ITsString tss = TsStringUtils.MakeString(entry, FieldWorks.Cache.DefaultVernWs);
                using (Mediator mediator = new Mediator())
                    using (PropertyTable propertyTable = new PropertyTable(mediator))
                    {
                        propertyTable.SetProperty("HelpTopicProvider", FieldWorks.GetHelpTopicProvider(), true);
                        propertyTable.SetPropertyPersistence("HelpTopicProvider", false);
                        var flexApp = FieldWorks.GetOrCreateFlexApp();
                        propertyTable.SetProperty("FeedbackInfoProvider", flexApp, true);
                        propertyTable.SetPropertyPersistence("FeedbackInfoProvider", false);
                        propertyTable.SetProperty("App", flexApp, true);

                        LexEntryUi.DisplayEntry(FieldWorks.Cache, mediator, propertyTable, propertyTable.GetValue <IHelpTopicProvider>("HelpTopicProvider"),
                                                "UserHelpFile", tss, null);
                    }
            });
        }
        /// ------------------------------------------------------------------------------------
        /// <summary>
        /// This must be called before entries are changed to ensure that
        /// it is saved to disk. Since the lexicon is a complex structure
        /// and other features depend on knowing when it is changed,
        /// all work done with the lexicon is marked with a begin and
        /// end change.
        /// </summary>
        /// ------------------------------------------------------------------------------------
        public void BeginChange()
        {
            LexicalProviderManager.ResetLexicalProviderTimer();

            // Ideally, we would like to be able to begin an undo task here and then end it in
            // EndChange(). However, because there is no guarantee that EndChange() will ever
            // get called (and to keep the data in a consistant state), we need to wrap
            // each individual change in it's own undo task.
        }
Beispiel #6
0
 /// ------------------------------------------------------------------------------------
 /// <summary>
 /// Gets the version of the specified provider that the server supports. If the
 /// providerType is not supported, return 0 for the version.
 /// </summary>
 /// ------------------------------------------------------------------------------------
 public int GetSupportedVersion(string providerType)
 {
     LexicalProviderManager.ResetLexicalProviderTimer();
     if (providerType == kLexicalProviderType)
     {
         return(kSupportedLexicalProviderVersion);
     }
     return(0);
 }
        /// ------------------------------------------------------------------------------------
        /// <summary>
        /// Adds a new sense to the lexeme with the specified information
        /// </summary>
        /// ------------------------------------------------------------------------------------
        public LexSense AddSenseToEntry(LexemeType type, string lexicalForm, int homograph)
        {
            LexicalProviderManager.ResetLexicalProviderTimer();
            Logger.WriteEvent("Adding new sense to lexeme '" + lexicalForm + "' from an external application");

            return(NonUndoableUnitOfWorkHelper.Do(m_cache.ActionHandlerAccessor, () =>
            {
                string guid = string.Empty;
                switch (type)
                {
                case LexemeType.Word:
                    {
                        IWfiWordform dbWordform = GetDbWordform(lexicalForm);
                        if (dbWordform == null)
                        {
                            throw new ArgumentException("Entry in the lexicon not found for the specified information");
                        }

                        // For wordforms, our "senses" could be new meanings of an analysis for the word
                        // or it could be a brand new analysis. Because we have no idea what the user actually
                        // wanted, we just assume the worst (they want to create a new analysis for the word
                        // with a new meaning).
                        IWfiAnalysis dbAnalysis = m_cache.ServiceLocator.GetInstance <IWfiAnalysisFactory>().Create();
                        dbWordform.AnalysesOC.Add(dbAnalysis);
                        guid = kAnalysisPrefix + dbAnalysis.Guid;
                        break;
                    }

                default:
                    {
                        ILexEntry dbEntry = GetDbLexeme(type, lexicalForm, homograph);
                        if (dbEntry == null)
                        {
                            throw new ArgumentException("Entry in the lexicon not found for the specified information");
                        }

                        if (dbEntry.SensesOS.Count == 1 && dbEntry.SensesOS[0].Gloss.StringCount == 0)
                        {
                            // An empty sense exists (probably was created during a call to AddLexeme)
                            guid = dbEntry.SensesOS[0].Guid.ToString();
                            break;
                        }

                        ILexSense newSense = m_cache.ServiceLocator.GetInstance <ILexSenseFactory>().Create(
                            dbEntry, new SandboxGenericMSA(), null);
                        guid = newSense.Guid.ToString();
                        break;
                    }
                }
                return new LexSense(guid);
            }));
        }
        /// ------------------------------------------------------------------------------------
        /// <summary>
        /// Gets the location for the provider for the specified project and provider type.
        /// If the providerType is not supported, return null for the Uri.
        /// </summary>
        /// ------------------------------------------------------------------------------------
        public Uri GetProviderLocation(string projhandle, string providerType)
        {
            LexicalProviderManager.ResetLexicalProviderTimer();

            if (providerType == kLexicalProviderType)
            {
                Uri projUri = new Uri("net.pipe://localhost/" + FwUtils.GeneratePipeHandle(projhandle) + ":LP");
                LexicalProviderManager.StartProvider(projUri, new LexicalProviderImpl(m_cache), typeof(ILexicalProvider));
                return(projUri);
            }

            return(null);
        }
Beispiel #9
0
        /// ------------------------------------------------------------------------------------
        /// <summary>
        /// Gets the location for the provider for the specified project and provider type.
        /// If the providerType is not supported, return null for the Uri.
        /// </summary>
        /// ------------------------------------------------------------------------------------
        public Uri GetProviderLocation(string projhandle, string providerType)
        {
            LexicalProviderManager.ResetLexicalProviderTimer();

            if (providerType == kLexicalProviderType)
            {
                var url     = LexicalProviderManager.UrlPrefix + LexicalProviderManager.FixPipeHandle(FwUtils.GeneratePipeHandle(projhandle + ":LP"));
                Uri projUri = new Uri(url);
                LexicalProviderManager.StartProvider(projUri, new LexicalProviderImpl(m_cache), typeof(ILexicalProvider));
                return(projUri);
            }

            return(null);
        }
        /// ------------------------------------------------------------------------------------
        /// <summary>
        /// Looks up an lexeme
        /// </summary>
        /// ------------------------------------------------------------------------------------
        public LexicalEntry GetLexeme(LexemeType type, string lexicalForm, int homograph)
        {
            LexicalProviderManager.ResetLexicalProviderTimer();

            switch (type)
            {
            case LexemeType.Word:
                IWfiWordform wf = GetDbWordform(lexicalForm);
                return((wf != null) ? CreateEntryFromDbWordform(wf) : null);

            default:
                ILexEntry dbEntry = GetDbLexeme(type, lexicalForm, homograph);
                return((dbEntry != null) ? CreateEntryFromDbEntry(type, dbEntry) : null);
            }
        }
        /// ------------------------------------------------------------------------------------
        /// <summary>
        /// Adds a new gloss to the sense with the specified information
        /// </summary>
        /// ------------------------------------------------------------------------------------
        public LexGloss AddGlossToSense(LexemeType type, string lexicalForm, int homograph,
                                        string senseId, string language, string text)
        {
            LexicalProviderManager.ResetLexicalProviderTimer();
            Logger.WriteEvent("Adding new gloss to lexeme '" + lexicalForm + "' from an external application");

            return(NonUndoableUnitOfWorkHelper.Do(m_cache.ActionHandlerAccessor, () =>
            {
                IMultiUnicode dbGlosses;
                if (senseId.StartsWith(kAnalysisPrefix))
                {
                    // The "sense" is actually an analysis for a wordform and our new
                    // gloss is a new meaning for that analysis.
                    Guid analysisGuid = new Guid(senseId.Substring(kAnalysisPrefix.Length));
                    IWfiAnalysis dbAnalysis = m_cache.ServiceLocator.GetInstance <IWfiAnalysisRepository>().GetObject(analysisGuid);
                    IWfiGloss dbGloss = dbAnalysis.MeaningsOC.FirstOrDefault();
                    if (dbGloss == null)
                    {
                        dbGloss = m_cache.ServiceLocator.GetInstance <IWfiGlossFactory>().Create();
                        dbAnalysis.MeaningsOC.Add(dbGloss);
                    }
                    dbGlosses = dbGloss.Form;
                    dbAnalysis.ApprovalStatusIcon = 1;                             // Assume the analysis from the external application is user approved
                }
                else
                {
                    Guid senseGuid = new Guid(senseId);
                    ILexSense dbSense = m_cache.ServiceLocator.GetInstance <ILexSenseRepository>().GetObject(senseGuid);
                    dbGlosses = dbSense.Gloss;
                }

                // Add the new gloss to the list of glosses for the sense
                ILgWritingSystem writingSystem = m_cache.WritingSystemFactory.get_Engine(language);
                dbGlosses.set_String(writingSystem.Handle, TsStringUtils.MakeTss(text, writingSystem.Handle));

                return new LexGloss(language, text);
            }));
        }
        /// ------------------------------------------------------------------------------------
        /// <summary>
        /// Removes the gloss with the specified language form the sense with the specified information
        /// </summary>
        /// ------------------------------------------------------------------------------------
        public void RemoveGloss(LexemeType type, string lexicalForm, int homograph,
                                string senseId, string language)
        {
            LexicalProviderManager.ResetLexicalProviderTimer();
            Logger.WriteEvent("Removing gloss from lexeme '" + lexicalForm + "' from an external application");

            NonUndoableUnitOfWorkHelper.Do(m_cache.ActionHandlerAccessor, () =>
            {
                IMultiUnicode dbGlosses;
                Guid guid;
                if (senseId.StartsWith(kAnalysisPrefix))
                {
                    // The "sense" is actually an analysis for a wordform and the gloss
                    // we want to delete is a meaning for that analysis.
                    guid = new Guid(senseId.Substring(kAnalysisPrefix.Length));
                    IWfiAnalysis dbAnalysis = m_cache.ServiceLocator.GetInstance <IWfiAnalysisRepository>().GetObject(guid);
                    IWfiGloss dbGloss       = dbAnalysis.MeaningsOC.First();
                    dbGlosses = dbGloss.Form;
                }
                else
                {
                    guid = new Guid(senseId);
                    ILexSense dbSense = m_cache.ServiceLocator.GetInstance <ILexSenseRepository>().GetObject(guid);
                    dbGlosses         = dbSense.Gloss;
                }
                // Remove the gloss from the list of glosses for the sense
                int wsId = m_cache.WritingSystemFactory.GetWsFromStr(language);
                dbGlosses.set_String(wsId, (ITsString)null);

                // Delete the sense if there are no more glosses for it
                if (dbGlosses.StringCount == 0)
                {
                    RemoveSense(senseId, guid);
                }
            });
        }
        /// ------------------------------------------------------------------------------------
        /// <summary>
        /// Gets all lexemes in the Lexicon
        /// </summary>
        /// ------------------------------------------------------------------------------------
        public IEnumerable <LexicalEntry> Lexemes()
        {
            LexicalProviderManager.ResetLexicalProviderTimer();

            List <LexicalEntry> entries = new List <LexicalEntry>();

            // Get all of the lexical entries in the database
            foreach (ILexEntry dbEntry in m_cache.ServiceLocator.GetInstance <ILexEntryRepository>().AllInstances())
            {
                IMoMorphType morphType = dbEntry.PrimaryMorphType;
                if (morphType != null)
                {
                    entries.Add(CreateEntryFromDbEntry(GetLexemeTypeForMorphType(morphType), dbEntry));
                }
            }

            // Get all the wordforms in the database
            foreach (IWfiWordform wordform in m_cache.ServiceLocator.GetInstance <IWfiWordformRepository>().AllInstances())
            {
                entries.Add(CreateEntryFromDbWordform(wordform));
            }

            return(entries);
        }
 /// ------------------------------------------------------------------------------------
 /// <summary>
 /// This must be called after entries are changed to ensure that
 /// other features dependent on the lexicon are made aware of the
 /// change.
 /// </summary>
 /// ------------------------------------------------------------------------------------
 public void EndChange()
 {
     LexicalProviderManager.ResetLexicalProviderTimer();
 }
Beispiel #15
0
 /// ------------------------------------------------------------------------------------
 /// <summary>
 /// Unlike a normal ping method that gets a response, we just use this ping method
 /// to determine if the service provider is actually valid since no exception is
 /// thrown until a method is called.
 /// </summary>
 /// ------------------------------------------------------------------------------------
 public void Ping()
 {
     // Nothing to do for this method except reset our timer for the life of the LexicalProvider.
     // See comment for this method.
     LexicalProviderManager.ResetLexicalProviderTimer();
 }