Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using Newtonsoft.Json;
- namespace Contracts.Models.SalesForce
- {
- public class TemplateDataLinkedListBase<T> where T : TemplateDataLinkedListBase<T>
- {
- [JsonProperty(TypeNameHandling = TypeNameHandling.Objects)]
- public T Parent { get; set; }
- [JsonProperty(TypeNameHandling=TypeNameHandling.Objects)]
- public List<T> Children { get; set; }
- public string EntityName { get; set; }
- public HashSet<string> Fields { get; set; }
- public string Key { get { return getKey(); } }
- public TemplateDataLinkedListBase(string entityName)
- {
- EntityName = entityName;
- Children = new List<T>();
- Fields = new HashSet<string>();
- }
- private string getKey()
- {
- List<string> keys = new List<string>();
- keys.Add(this.EntityName);
- getParentKeys(ref keys, this);
- keys.Reverse();
- return string.Join(".", keys);
- }
- private void getParentKeys(ref List<string> keys, TemplateDataLinkedListBase<T> element)
- {
- if (element.Parent != null)
- {
- keys.Add(element.Parent.EntityName);
- getParentKeys(ref keys, element.Parent);
- }
- }
- public T AddChild(T child)
- {
- child.Parent = (T)this;
- Children.Add(child);
- return (T)this;
- }
- public T AddChildren(List<T> children)
- {
- foreach (var child in children)
- {
- child.Parent = (T)this;
- }
- Children.AddRange(children);
- return (T)this;
- }
- public void AddFields(IEnumerable<string> fields)
- {
- foreach (var field in fields)
- this.Fields.Add(field);
- }
- public TemplateDataLinkedListBase<T> Find(string searchkey)
- {
- if (this.Key == searchkey)
- {
- return this;
- }
- else
- {
- foreach (var child in Children)
- {
- if (child.Key == searchkey)
- {
- return child;
- }
- else
- {
- var childResult = child.Find(searchkey);
- if (childResult != null) return childResult;
- }
- }
- }
- return null;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement