/// <summary> /// </summary> private void btnUpdateTermSet_Click(object sender, EventArgs e) { StartWait(); this.Invoke((MethodInvoker) delegate { while (true) { TreeNode curNode = tvMMD.SelectedNode; if (curNode == null) { break; } if (txtTermSetName.Text.Trim().Length == 0) { cout("Cannot update termset, name is required."); break; } if (curNode.Level == 2) { TermSet tSet = MMDHelper.GetObj(txtSiteUrl.Text, curNode.Level, curNode) as TermSet; if (!string.IsNullOrEmpty(MMDHelper.errMsg)) { cout("ERROR", MMDHelper.errMsg); break; } try { tSet.Name = GenUtil.MmdNormalize(txtTermSetName.Text.Trim()); tSet.IsAvailableForTagging = chkTermSetIsAvailableForTagging.Checked; tSet.Description = txtTermSetDescription.Text.Trim(); tSet.TermStore.CommitAll(); cout("TermSet Updated"); curNode.Text = string.Format("{1} [{0}]", tSet.Terms.Count, GenUtil.MmdNormalize(txtTermSetName.Text.Trim())); } catch (Exception exc) { cout("ERROR updating termset", exc.Message); } } break; } }); StopWait(); }
/// <summary> /// </summary> private void btnCreateTerm_Click(object sender, EventArgs e) { if (MessageBox.Show("Are you sure?", "Confirm", MessageBoxButtons.YesNo) != DialogResult.Yes) { return; } StartWait(); this.Invoke((MethodInvoker) delegate { while (true) { TreeNode curNode = tvMMD.SelectedNode; if (curNode == null) { break; } if (txtTermName.Text.Trim().Length == 0) { cout("Cannot create term, name is required."); break; } Term newTerm = null; try { if (curNode.Level == 2) { // add term to termset TermSet tSet = MMDHelper.GetObj(txtSiteUrl.Text, curNode.Level, curNode) as TermSet; if (!string.IsNullOrEmpty(MMDHelper.errMsg)) { cout("ERROR", MMDHelper.errMsg); break; } if (GenUtil.IsNull(txtNewTermGuid.Text) || txtNewTermGuid.Text.Trim().ToLower() == NewTermGuidLabel.Trim().ToLower() || !GenUtil.IsGuid(txtNewTermGuid.Text)) { newTerm = tSet.CreateTerm(GenUtil.MmdNormalize(txtTermName.Text), CultureInfo.CurrentCulture.LCID); } else { if (!GenUtil.IsGuid(txtNewTermGuid.Text)) { MessageBox.Show("Cannot create term, invalid new term Guid."); break; } else { newTerm = tSet.CreateTerm(GenUtil.MmdNormalize(txtTermName.Text), CultureInfo.CurrentCulture.LCID, new Guid(txtNewTermGuid.Text)); } } } else if (curNode.Level >= 3) { // add term to term Term term = MMDHelper.GetObj(txtSiteUrl.Text, curNode.Level, curNode) as Term; if (!string.IsNullOrEmpty(MMDHelper.errMsg)) { cout("ERROR", MMDHelper.errMsg); break; } if (GenUtil.IsNull(txtNewTermGuid.Text) || txtNewTermGuid.Text.Trim().ToLower() == NewTermGuidLabel.Trim().ToLower() || !GenUtil.IsGuid(txtNewTermGuid.Text)) { newTerm = term.CreateTerm(GenUtil.MmdNormalize(txtTermName.Text), CultureInfo.CurrentCulture.LCID); } else { if (!GenUtil.IsGuid(txtNewTermGuid.Text)) { MessageBox.Show("Cannot create term, invalid new term Guid."); break; } else { newTerm = term.CreateTerm(GenUtil.MmdNormalize(txtTermName.Text), CultureInfo.CurrentCulture.LCID, new Guid(txtNewTermGuid.Text)); } } } else { MessageBox.Show("Cannot create term in current location, select a termset or term."); break; } if (newTerm != null) { newTerm.IsAvailableForTagging = chkTermIsAvailableForTagging.Checked; // labels if (!GenUtil.IsNull(txtTermLabels.Text)) { foreach (string lbl in GenUtil.MmdNormalize(txtTermLabels.Text).Split(new char[] { ';' })) { if (!GenUtil.IsNull(lbl) && lbl.ToLower() != GenUtil.MmdNormalize(newTerm.Name).ToLower()) { newTerm.CreateLabel(lbl, CultureInfo.CurrentCulture.LCID, false); } } } newTerm.TermStore.CommitAll(); // add term to tree TreeNode newNode = new TreeNode(); newNode.Text = string.Format("{0} [0]", GenUtil.MmdDenormalize(newTerm.Name)); newNode.Name = newTerm.Id.ToString(); curNode.Nodes.Add(newNode); curNode.Expand(); cout("New Term Created"); txtNewTermGuid.Text = NewTermGuidLabel; } } catch (Exception ex) { cout("ERROR creating term", ex.Message); } break; } }); StopWait(); }
/// <summary> /// </summary> private void btnCreateTermSet_Click(object sender, EventArgs e) { if (MessageBox.Show("Are you sure?", "Confirm", MessageBoxButtons.YesNo) != DialogResult.Yes) { return; } StartWait(); this.Invoke((MethodInvoker) delegate { while (true) { TreeNode curNode = tvMMD.SelectedNode; if (curNode == null) { break; } if (txtTermSetName.Text.Trim().Length == 0) { cout("Cannot create termset, name is required."); break; } Group tGroup = null; if (curNode.Level == 1) { // create new termset in group tGroup = MMDHelper.GetObj(txtSiteUrl.Text, curNode.Level, curNode) as Group; if (!string.IsNullOrEmpty(MMDHelper.errMsg)) { cout("ERROR", MMDHelper.errMsg); break; } } else { MessageBox.Show("Cannot create termset in current location, select a group."); break; } if (tGroup != null) { if (tGroup.TermSets.Any(x => GenUtil.MmdNormalize(x.Name).ToLower() == GenUtil.MmdNormalize(txtTermSetName.Text).ToLower())) { cout("Cannot create termset, name not unique."); break; } try { // create new termset TermSet tSet = tGroup.CreateTermSet(GenUtil.MmdNormalize(txtTermSetName.Text.Trim())); tSet.IsAvailableForTagging = chkTermSetIsAvailableForTagging.Checked; tSet.Description = txtTermSetDescription.Text.Trim(); tSet.TermStore.CommitAll(); // add termset to tree TreeNode newNode = new TreeNode(); newNode.Text = string.Format("{0} [0]", GenUtil.MmdDenormalize(tSet.Name)); newNode.Name = tSet.Id.ToString(); curNode.Nodes.Add(newNode); curNode.Expand(); cout("New TermSet Created"); } catch (Exception exc) { cout("ERROR creating termset", exc.Message); } } break; } }); StopWait(); }
/// <summary> /// </summary> private void btnUpdateTerm_Click(object sender, EventArgs e) { StartWait(); this.Invoke((MethodInvoker) delegate { while (true) { TreeNode curNode = tvMMD.SelectedNode; if (curNode == null) { break; } if (txtTermName.Text.Trim().Length == 0) { cout("Cannot update term, name is required."); break; } if (curNode.Level >= 3) { Term term = MMDHelper.GetObj(txtSiteUrl.Text, curNode.Level, curNode) as Term; if (!string.IsNullOrEmpty(MMDHelper.errMsg)) { cout("ERROR", MMDHelper.errMsg); break; } try { // term labels string ignoreTermName = GenUtil.MmdNormalize(txtTermName.Text); List <string> lstNewLabels = GenUtil.MmdNormalize(txtTermLabels.Text).Split(new char[] { ';' }).ToList(); // delete existing labels (if label in collection not found in textbox) int i = 0; while (i < term.Labels.Select(x => x.Value).ToList().Count()) { var curLbl = term.Labels[i]; var curLblVal = GenUtil.MmdNormalize(curLbl.Value); if (curLblVal.ToLower() != ignoreTermName.ToLower() && !lstNewLabels.Any(x => x.Trim().ToLower() == curLblVal.ToLower()) && !curLbl.IsDefaultForLanguage) { curLbl.Delete(); } else { i++; } } // add new labels (if label in textbox not found in collection) foreach (var lblNew in lstNewLabels) { if (!GenUtil.IsNull(lblNew) && lblNew.Trim().ToLower() != ignoreTermName.ToLower() && !term.Labels.Any(x => GenUtil.MmdNormalize(x.Value).ToLower() == lblNew.ToLower())) { term.CreateLabel(lblNew.Trim(), CultureInfo.CurrentCulture.LCID, false); } } term.IsAvailableForTagging = chkTermIsAvailableForTagging.Checked; term.Name = GenUtil.MmdNormalize(txtTermName.Text); term.TermStore.CommitAll(); curNode.Text = string.Format("{1} [{0}]", term.TermsCount, GenUtil.MmdNormalize(txtTermName.Text)); cout("Term Updated"); } catch (Exception exc) { cout("ERROR updating term", exc.Message); } } break; } }); StopWait(); }
/// <summary> /// </summary> public static string ProcessXml(string xml, Group tGroup, TreeNode curNode) { var retMsg = ""; XElement xTree = null; try { // load xml xTree = XElement.Load(new StringReader(xml)); } catch (Exception ex) { return(ex.Message); } foreach (var termSetElement in xTree.XPathSelectElements("/termset")) { string newTermSetName = GenUtil.MmdNormalize(GenUtil.SafeXmlAttributeToString(termSetElement, "name")); Guid newTermSetId = GenUtil.SafeXmlAttributeToGuid(termSetElement, "id"); bool newTermSetIsAvailForTagging = GenUtil.SafeXmlAttributeToBool(termSetElement, "isavailfortagging"); string newTermDescr = GenUtil.SafeXmlAttributeToString(termSetElement, "description"); bool newTermSetIsOpenForTermCreation = GenUtil.SafeXmlAttributeToBool(termSetElement, "isopenfortermcreation"); if (GenUtil.IsNull(newTermSetName)) { return("TermSet name is missing."); } // create termset (or update if found) TermSet tSet = null; try { tSet = tGroup.TermStore.GetTermSet(newTermSetId); if (tSet != null) { // termset found using guid tSet.Name = newTermSetName; tSet.Description = newTermDescr; tSet.IsAvailableForTagging = newTermSetIsAvailForTagging; tSet.IsOpenForTermCreation = newTermSetIsOpenForTermCreation; tSet.TermStore.CommitAll(); } else { tSet = tGroup.TermStore.GetTermSets(newTermSetName, CultureInfo.CurrentCulture.LCID).FirstOrDefault(); if (tSet != null) { // termset found using name tSet.Description = newTermDescr; tSet.IsAvailableForTagging = newTermSetIsAvailForTagging; tSet.IsOpenForTermCreation = newTermSetIsOpenForTermCreation; tSet.TermStore.CommitAll(); } else { tSet = tGroup.CreateTermSet(newTermSetName, newTermSetId, CultureInfo.CurrentCulture.LCID); tSet.Description = newTermDescr; tSet.IsAvailableForTagging = newTermSetIsAvailForTagging; tSet.IsOpenForTermCreation = newTermSetIsOpenForTermCreation; tSet.TermStore.CommitAll(); } } } catch (Exception ex) { return(ex.Message); } // create terms within (recursive) try { foreach (var termElement in termSetElement.XPathSelectElements("term")) { ProcessTerm(termElement, tSet, null); } } catch (Exception ex) { return(ex.Message); } } return(retMsg); }
/// <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); } }