Serialize (map) TermsSet / Term
i should edit this sometime, but currently here is the code
public class ComplexMappedTerm
{
public static int LCID { get; set; }
public Guid ID { get; set; }
public string Label { get; set; }
public int ChildrenCount { get; set; }
public string Path { get; set; }
public List<ComplexMappedTerm> Children { get; set; }
private ComplexMappedTerm() { }
public static ComplexMappedTerm BuildTree(TermSetItem tsi)
{
if (LCID == 0)
{
throw new ArgumentException("TermMap - Must set static property LCID before using mapping functions");
}
if (tsi is Term)
{
return Map(tsi as Term);
}
if (tsi is TermSet)
{
return Map(tsi as TermSet);
}
return null;
}
static ComplexMappedTerm Map(Term t)
{
List<ComplexMappedTerm> children = null;
if (t.Terms.Count > 0)
{
children = new List<ComplexMappedTerm>();
List<Term> childTerms = t.Terms.Cast<Term>().ToList();
for (int i = 0; i < childTerms.Count; i++)
{
children.Add(ComplexMappedTerm.Map(childTerms[i]));
}
}
return new ComplexMappedTerm()
{
ID = t.Id,
Label = t.GetDefaultLabel(LCID),
ChildrenCount = t.Terms.Count,
Path = t.GetPath(LCID),
Children = children
};
}
static ComplexMappedTerm Map(TermSet ts)
{
List<ComplexMappedTerm> children = null;
if (ts.Terms.Count > 0)
{
children = new List<ComplexMappedTerm>();
List<Term> childTerms = ts.Terms.Cast<Term>().ToList();
for (int i = 0; i < childTerms.Count; i++)
{
children.Add(ComplexMappedTerm.Map(childTerms[i]));
}
}
Dictionary<int, string> names =
ts.GetType().GetProperty("Names",
System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance).
GetValue(ts) as Dictionary<int, string>;
string label = names.Where(n => n.Key == LCID).First().Value;
return new ComplexMappedTerm()
{
ID = ts.Id,
Label = label,
ChildrenCount = ts.Terms.Count,
Children = children
};
}
}
}
public class ComplexMappedTerm
{
public static int LCID { get; set; }
public Guid ID { get; set; }
public string Label { get; set; }
public int ChildrenCount { get; set; }
public string Path { get; set; }
public List<ComplexMappedTerm> Children { get; set; }
private ComplexMappedTerm() { }
public static ComplexMappedTerm BuildTree(TermSetItem tsi)
{
if (LCID == 0)
{
throw new ArgumentException("TermMap - Must set static property LCID before using mapping functions");
}
if (tsi is Term)
{
return Map(tsi as Term);
}
if (tsi is TermSet)
{
return Map(tsi as TermSet);
}
return null;
}
static ComplexMappedTerm Map(Term t)
{
List<ComplexMappedTerm> children = null;
if (t.Terms.Count > 0)
{
children = new List<ComplexMappedTerm>();
List<Term> childTerms = t.Terms.Cast<Term>().ToList();
for (int i = 0; i < childTerms.Count; i++)
{
children.Add(ComplexMappedTerm.Map(childTerms[i]));
}
}
return new ComplexMappedTerm()
{
ID = t.Id,
Label = t.GetDefaultLabel(LCID),
ChildrenCount = t.Terms.Count,
Path = t.GetPath(LCID),
Children = children
};
}
static ComplexMappedTerm Map(TermSet ts)
{
List<ComplexMappedTerm> children = null;
if (ts.Terms.Count > 0)
{
children = new List<ComplexMappedTerm>();
List<Term> childTerms = ts.Terms.Cast<Term>().ToList();
for (int i = 0; i < childTerms.Count; i++)
{
children.Add(ComplexMappedTerm.Map(childTerms[i]));
}
}
Dictionary<int, string> names =
ts.GetType().GetProperty("Names",
System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance).
GetValue(ts) as Dictionary<int, string>;
string label = names.Where(n => n.Key == LCID).First().Value;
return new ComplexMappedTerm()
{
ID = ts.Id,
Label = label,
ChildrenCount = ts.Terms.Count,
Children = children
};
}
}
}
Comments
Post a Comment