Advertisement
mr_robd_lon_1

SO_PreserveReferencesHandling_A

Sep 18th, 2014
285
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.64 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Newtonsoft.Json;
  6.  
  7. namespace Contracts.Models.SalesForce
  8. {
  9.     public class TemplateDataLinkedListBase<T> where T : TemplateDataLinkedListBase<T>
  10.     {
  11.         [JsonProperty(TypeNameHandling = TypeNameHandling.Objects)]
  12.         public T Parent { get; set; }
  13.  
  14.         [JsonProperty(TypeNameHandling=TypeNameHandling.Objects)]
  15.         public List<T> Children { get; set; }
  16.         public string EntityName { get; set; }
  17.         public HashSet<string> Fields { get; set; }
  18.  
  19.         public string Key { get { return getKey(); } }
  20.  
  21.         public TemplateDataLinkedListBase(string entityName)
  22.         {
  23.             EntityName = entityName;
  24.             Children = new List<T>();
  25.             Fields = new HashSet<string>();
  26.         }
  27.  
  28.         private string getKey()
  29.         {
  30.             List<string> keys = new List<string>();
  31.             keys.Add(this.EntityName);
  32.             getParentKeys(ref keys, this);
  33.             keys.Reverse();
  34.             return string.Join(".", keys);
  35.  
  36.         }
  37.  
  38.         private void getParentKeys(ref List<string> keys, TemplateDataLinkedListBase<T> element)
  39.         {
  40.             if (element.Parent != null)
  41.             {
  42.                 keys.Add(element.Parent.EntityName);
  43.                 getParentKeys(ref keys, element.Parent);
  44.             }
  45.         }
  46.  
  47.         public T AddChild(T child)
  48.         {
  49.             child.Parent = (T)this;
  50.             Children.Add(child);
  51.             return (T)this;
  52.         }
  53.  
  54.         public T AddChildren(List<T> children)
  55.         {
  56.             foreach (var child in children)
  57.             {
  58.                 child.Parent = (T)this;
  59.             }
  60.             Children.AddRange(children);
  61.             return (T)this;
  62.         }
  63.  
  64.         public void AddFields(IEnumerable<string> fields)
  65.         {
  66.             foreach (var field in fields)
  67.                 this.Fields.Add(field);
  68.         }
  69.  
  70.         public TemplateDataLinkedListBase<T> Find(string searchkey)
  71.         {
  72.             if (this.Key == searchkey)
  73.             {
  74.                 return this;
  75.             }
  76.             else
  77.             {
  78.                 foreach (var child in Children)
  79.                 {
  80.                     if (child.Key == searchkey)
  81.                     {
  82.                         return child;
  83.                     }
  84.                     else
  85.                     {
  86.                         var childResult = child.Find(searchkey);
  87.                         if (childResult != null) return childResult;
  88.                     }
  89.                 }
  90.             }
  91.             return null;
  92.         }
  93.     }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement