Exemple #1
0
        public TermInstance ReuseTerm(TermInstance term, bool reuseBranch)
        {
            var reusedTerm = m_termSet.ReuseTerm(term.Term, reuseBranch);

            return(new TermInstance(Engine.Object.InstancePrototype, reusedTerm));
        }
        /// <summary>
        /// </summary>
        private static void ProcessTerm(XElement termElement, TermSet tSet, Term term)
        {
            string newTermName = GenUtil.MmdNormalize(GenUtil.SafeXmlAttributeToString(termElement, "name"));
            Guid?  newTermId   = GenUtil.SafeXmlAttributeToGuidOrNull(termElement, "id");
            bool   newTermIsAvailForTagging = GenUtil.SafeXmlAttributeToBool(termElement, "isavailfortagging");
            string newTermDescr             = GenUtil.SafeXmlAttributeToString(termElement, "description");
            bool   newTermReuse             = GenUtil.SafeXmlAttributeToBool(termElement, "reuse");
            bool   newTermReuseBranch       = GenUtil.SafeXmlAttributeToBool(termElement, "reusebranch");

            if (GenUtil.IsNull(newTermName))
            {
                throw new Exception("Term name is empty.");
            }

            // create term (or get existing term to update, or reuse term)
            Term newTerm      = null;
            bool termExists   = true;
            bool termIsReused = false;

            if (tSet != null)
            {
                // termset passed to function, the term being worked on is a level 0 term in a termset
                if (newTermReuse && newTermId != null)
                {
                    // try to reuse term using termguid
                    newTerm = tSet.TermStore.GetTerm((Guid)newTermId);

                    if (newTerm != null)
                    {
                        // resuse term
                        newTerm      = tSet.ReuseTerm(newTerm, newTermReuseBranch);
                        termIsReused = true;
                        newTerm.TermStore.CommitAll();
                    }
                }

                if (!termIsReused)
                {
                    if (newTermId != null)
                    {
                        // try to get term based on guid
                        newTerm = tSet.TermStore.GetTerm((Guid)newTermId);
                    }

                    if (newTermId == null)
                    {
                        // try to get term based on name
                        try
                        {
                            newTerm = tSet.Terms[newTermName];
                        }
                        catch
                        {
                            newTerm = null;
                        }
                    }

                    if (newTerm == null)
                    {
                        // create new term
                        newTerm    = tSet.CreateTerm(newTermName, CultureInfo.CurrentCulture.LCID, (newTermId == null ? Guid.NewGuid() : (Guid)newTermId));
                        termExists = false;
                    }
                }
            }
            else
            {
                // termset not passed to function, term being worked on is a level n term in a termset (term within term)
                if (newTermReuse && newTermId != null)
                {
                    // try to reuse term using termguid
                    newTerm = term.TermStore.GetTerm((Guid)newTermId);

                    if (newTerm != null)
                    {
                        // resuse term
                        newTerm      = term.ReuseTerm(newTerm, newTermReuseBranch);
                        termIsReused = true;
                        newTerm.TermStore.CommitAll();
                    }
                }

                if (!termIsReused)
                {
                    if (newTermId != null)
                    {
                        // try to get term based on guid
                        newTerm = term.TermSet.GetTerm((Guid)newTermId);
                    }

                    if (newTermId == null)
                    {
                        try
                        {
                            newTerm = term.Terms[newTermName];
                        }
                        catch
                        {
                            newTerm = null;
                        }

                        // try to get term based on name
                        //foreach (var termFound in term.TermSet.GetTerms(newTermName, false))
                        //{
                        //    if (termFound.Parent.Name.Trim().ToLower() == termElement.Parent.Attribute("name").Value.Trim().ToLower())
                        //    {
                        //        newTerm = termFound;
                        //    }
                        //}

                        //newTerm = term.TermSet.GetTerms(newTermName, false).FirstOrDefault();

                        //newTerm = term.GetTerms(newTermName, CultureInfo.CurrentCulture.LCID, true, StringMatchOption.ExactMatch, 1, false).FirstOrDefault();
                    }

                    if (newTerm == null)
                    {
                        // create new term
                        newTerm    = term.CreateTerm(newTermName, CultureInfo.CurrentCulture.LCID, (newTermId == null ? Guid.NewGuid() : (Guid)newTermId));
                        termExists = false;
                    }
                }
            }


            // update term properties (not if being reused)
            if (!termIsReused)
            {
                // term not reused
                if (newTerm == null)
                {
                    throw new Exception("Term not found.");
                }

                newTerm.IsAvailableForTagging = newTermIsAvailForTagging;

                if (!GenUtil.IsNull(newTermDescr))
                {
                    newTerm.SetDescription(newTermDescr, CultureInfo.CurrentCulture.LCID);
                }

                // reset labels/synonyms
                if (termExists)
                {
                    int i = 0;
                    while (i < newTerm.Labels.Count)
                    {
                        if (!newTerm.Labels[i].IsDefaultForLanguage)
                        {
                            newTerm.Labels[i].Delete();
                        }
                        else
                        {
                            i++;
                        }
                    }
                }

                // recreate term labels
                foreach (var termLabel in termElement.XPathSelectElements("label"))
                {
                    var lbl = GenUtil.MmdNormalize(GenUtil.SafeXmlAttributeToString(termLabel, "name"));

                    if (!GenUtil.IsNull(lbl) && lbl != newTermName)
                    {
                        newTerm.CreateLabel(lbl, CultureInfo.CurrentCulture.LCID, false);
                    }
                }

                newTerm.TermStore.CommitAll();
            }

            if (termIsReused && newTermReuseBranch)
            {
                // quit if term is reused and using existing term branch
                return;
            }

            // continue processing subterms
            foreach (var subTermElement in termElement.XPathSelectElements("term"))
            {
                ProcessTerm(subTermElement, null, newTerm);
            }
        }