PageRenderTime 62ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/SobekCM_Core/ApplicationState/Aggregation_Code_Manager.cs

http://github.com/MarkVSullivan/SobekCM-Web-Application
C# | 313 lines | 213 code | 37 blank | 63 comment | 24 complexity | 518448a0034905eada447952f4e45a2f MD5 | raw file
Possible License(s): LGPL-2.1, MIT, GPL-3.0, LGPL-2.0
  1. #region Using directives
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Collections.ObjectModel;
  5. using System.Linq;
  6. using System.Runtime.Serialization;
  7. using System.Xml.Serialization;
  8. using ProtoBuf;
  9. using SobekCM.Core.Aggregations;
  10. #endregion
  11. namespace SobekCM.Core.ApplicationState
  12. {
  13. /// <summary>
  14. /// Code manager maintains a list of all the valid aggregation codes and
  15. /// also provides a lookup from aggregation code to greenstone full text collection code
  16. /// to allow for full text searching of single items from the item viewer.
  17. /// </summary>
  18. [Serializable, DataContract, ProtoContract]
  19. [XmlRoot("codeManager")]
  20. public class Aggregation_Code_Manager : iSerializationEvents
  21. {
  22. private readonly Dictionary<string, Item_Aggregation_Related_Aggregations> aggregationsByCode;
  23. private readonly Dictionary<string, List<Item_Aggregation_Related_Aggregations>> aggregationsByType;
  24. private readonly List<string> allTypes;
  25. /// <summary>
  26. /// Constructor for a new instance of the Aggregation_Code_Manager class
  27. /// </summary>
  28. public Aggregation_Code_Manager()
  29. {
  30. // Declare the collections
  31. Aggregations_By_Thematic_Heading = new Dictionary<int, List<Item_Aggregation_Related_Aggregations>>();
  32. aggregationsByType = new Dictionary<string, List<Item_Aggregation_Related_Aggregations>>();
  33. aggregationsByCode = new Dictionary<string, Item_Aggregation_Related_Aggregations>();
  34. allTypes = new List<string>();
  35. All_Aggregations = new List<Item_Aggregation_Related_Aggregations>();
  36. }
  37. /// <summary> Collection of basic information about every aggregation in this instance, sorted by code </summary>
  38. [DataMember(EmitDefaultValue = false, Name = "aggregations")]
  39. [XmlArray("aggregations")]
  40. [XmlArrayItem("aggregation", typeof(Item_Aggregation_Related_Aggregations))]
  41. [ProtoMember(1)]
  42. public List<Item_Aggregation_Related_Aggregations> All_Aggregations { get; private set; }
  43. /// <summary> Dictionary which maps aggregations to thematic headings </summary>
  44. [DataMember]
  45. [XmlIgnore]
  46. public Dictionary<int, List<Item_Aggregation_Related_Aggregations>> Aggregations_By_Thematic_Heading { get; private set; }
  47. /// <summary> Read-only collection of all the aggregation information sorted by full name </summary>
  48. [XmlIgnore]
  49. public ReadOnlyCollection<Item_Aggregation_Related_Aggregations> All_Aggregations_Name_Sorted
  50. {
  51. get
  52. {
  53. SortedDictionary<string, Item_Aggregation_Related_Aggregations> sorter = new SortedDictionary<string, Item_Aggregation_Related_Aggregations>();
  54. foreach (Item_Aggregation_Related_Aggregations thisAggr in All_Aggregations)
  55. {
  56. sorter[thisAggr.Name.ToUpper()] = thisAggr;
  57. }
  58. List<Item_Aggregation_Related_Aggregations> returnVal = sorter.Select(DictEntry => DictEntry.Value).ToList();
  59. return new ReadOnlyCollection<Item_Aggregation_Related_Aggregations>(returnVal);
  60. }
  61. }
  62. /// <summary> Read-only collection of all the aggregation information sorted by short name </summary>
  63. [XmlIgnore]
  64. public ReadOnlyCollection<Item_Aggregation_Related_Aggregations> All_Aggregations_ShortName_Sorted
  65. {
  66. get
  67. {
  68. SortedDictionary<string, Item_Aggregation_Related_Aggregations> sorter = new SortedDictionary<string, Item_Aggregation_Related_Aggregations>();
  69. foreach (Item_Aggregation_Related_Aggregations thisAggr in All_Aggregations)
  70. {
  71. sorter[thisAggr.ShortName.ToUpper()] = thisAggr;
  72. }
  73. List<Item_Aggregation_Related_Aggregations> returnVal = sorter.Select(DictEntry => DictEntry.Value).ToList();
  74. return new ReadOnlyCollection<Item_Aggregation_Related_Aggregations>(returnVal);
  75. }
  76. }
  77. /// <summary> Gets the aggregation by primary key </summary>
  78. /// <param name="AggregationID"> Aggregation ID for the aggregation to get </param>
  79. /// <returns> Either the match, or NULL </returns>
  80. /// <remarks> This is not particularly a quick way to do this, since it just loops through all the aggregations, without
  81. /// utilizing a dictionary for lookup. </remarks>
  82. public Item_Aggregation_Related_Aggregations Aggregation_By_ID(int AggregationID)
  83. {
  84. return All_Aggregations.FirstOrDefault(ThisAggr => ThisAggr.ID == AggregationID);
  85. }
  86. /// <summary> Read-only collection of all the aggregation types </summary>
  87. [XmlIgnore]
  88. public ReadOnlyCollection<string> All_Types
  89. {
  90. get { return new ReadOnlyCollection<string>(allTypes); }
  91. }
  92. /// <summary> Gets the number of different aggregation types present </summary>
  93. [XmlIgnore]
  94. public int Types_Count
  95. {
  96. get { return allTypes.Count; }
  97. }
  98. /// <summary>
  99. /// Gets the aggregation information by aggregation code
  100. /// </summary>
  101. /// <param name = "Aggregation_Code"> Code for the aggregation of interest</param>
  102. /// <returns> Aggregation information, or NULL if not present </returns>
  103. public Item_Aggregation_Related_Aggregations this[string Aggregation_Code]
  104. {
  105. get {
  106. return aggregationsByCode.ContainsKey(Aggregation_Code.ToUpper()) ? aggregationsByCode[Aggregation_Code.ToUpper()] : null;
  107. }
  108. }
  109. /// <summary> Clears the internal data for this code manager </summary>
  110. public void Clear()
  111. {
  112. Aggregations_By_Thematic_Heading.Clear();
  113. aggregationsByType.Clear();
  114. aggregationsByCode.Clear();
  115. allTypes.Clear();
  116. All_Aggregations.Clear();
  117. }
  118. /// <summary> Add the basic information about an aggregation to this aggreation manager </summary>
  119. /// <param name="New_Aggregation"> New aggregation to add information about </param>
  120. public void Add_Collection(Item_Aggregation_Related_Aggregations New_Aggregation)
  121. {
  122. // Insert this into the proper spot in the item aggregation list
  123. int index = 0;
  124. while ((index < All_Aggregations.Count) && ( string.CompareOrdinal(New_Aggregation.Code, All_Aggregations[index].Code) > 0 ))
  125. {
  126. index++;
  127. }
  128. All_Aggregations.Insert(index, New_Aggregation);
  129. // Add this to the various dictionaries
  130. aggregationsByCode[New_Aggregation.Code] = New_Aggregation;
  131. if (!allTypes.Contains(New_Aggregation.Type))
  132. {
  133. allTypes.Add(New_Aggregation.Type);
  134. }
  135. if (aggregationsByType.ContainsKey(New_Aggregation.Type))
  136. {
  137. aggregationsByType[New_Aggregation.Type].Add(New_Aggregation);
  138. }
  139. else
  140. {
  141. aggregationsByType[New_Aggregation.Type] = new List<Item_Aggregation_Related_Aggregations> {New_Aggregation};
  142. }
  143. if (( New_Aggregation.Thematic_Heading != null ) && ( New_Aggregation.Thematic_Heading.ID > 0 ))
  144. {
  145. if (Aggregations_By_Thematic_Heading.ContainsKey(New_Aggregation.Thematic_Heading.ID))
  146. {
  147. Aggregations_By_Thematic_Heading[New_Aggregation.Thematic_Heading.ID].Add(New_Aggregation);
  148. }
  149. else
  150. {
  151. Aggregations_By_Thematic_Heading[New_Aggregation.Thematic_Heading.ID] = new List<Item_Aggregation_Related_Aggregations> { New_Aggregation };
  152. }
  153. }
  154. }
  155. /// <summary>
  156. /// Read-only collection of item aggregationPermissions matching a particular aggregation type
  157. /// </summary>
  158. /// <param name = "AggregationType"> Type of aggregationPermissions to return </param>
  159. /// <returns> Read-only collection of item aggregation relational objects </returns>
  160. public ReadOnlyCollection<Item_Aggregation_Related_Aggregations> Aggregations_By_Type(string AggregationType)
  161. {
  162. if (aggregationsByType.ContainsKey(AggregationType))
  163. {
  164. return new ReadOnlyCollection<Item_Aggregation_Related_Aggregations>(aggregationsByType[AggregationType]);
  165. }
  166. return new ReadOnlyCollection<Item_Aggregation_Related_Aggregations>( new List<Item_Aggregation_Related_Aggregations>());
  167. }
  168. /// <summary>
  169. /// Read-only collection of item aggregationPermissions matching a particular thematic heading id
  170. /// </summary>
  171. /// <param name = "ThemeID"> Primary key for the thematic heading to pull </param>
  172. /// <returns> Read-only collection of item aggregation relational objects </returns>
  173. public ReadOnlyCollection<Item_Aggregation_Related_Aggregations> Aggregations_By_ThemeID(int ThemeID)
  174. {
  175. if (Aggregations_By_Thematic_Heading.ContainsKey(ThemeID))
  176. {
  177. return new ReadOnlyCollection<Item_Aggregation_Related_Aggregations>(Aggregations_By_Thematic_Heading[ThemeID]);
  178. }
  179. return new ReadOnlyCollection<Item_Aggregation_Related_Aggregations>(new List<Item_Aggregation_Related_Aggregations>());
  180. }
  181. /// <summary>
  182. /// Gets the short name associated with a provided aggregation code
  183. /// </summary>
  184. /// <param name = "Aggregation_Code"> Code for the aggregation of interest</param>
  185. /// <returns> Short name of valid aggregation, otehrwise the aggregation code is returned </returns>
  186. public string Get_Collection_Short_Name(string Aggregation_Code)
  187. {
  188. if (aggregationsByCode.ContainsKey(Aggregation_Code.ToUpper()))
  189. return aggregationsByCode[Aggregation_Code.ToUpper()].ShortName;
  190. return Aggregation_Code;
  191. }
  192. /// <summary> Checks to see if an aggregation code exists </summary>
  193. /// <param name = "Aggregation_Code"> Code for the aggregation of interest </param>
  194. /// <returns> TRUE if the aggregation exists, otherwise FALSE </returns>
  195. public bool isValidCode(string Aggregation_Code)
  196. {
  197. return aggregationsByCode.ContainsKey(Aggregation_Code.ToUpper());
  198. }
  199. /// <summary> Set an aggregation to be a part of an existing thematic heading id </summary>
  200. /// <param name="Code"></param>
  201. /// <param name="ThematicHeadingID"></param>
  202. public void Set_Aggregation_Thematic_Heading(string Code, int? ThematicHeadingID)
  203. {
  204. Item_Aggregation_Related_Aggregations thisAggr = aggregationsByCode[Code.ToUpper()];
  205. // If this is NULL, just return
  206. if ((!ThematicHeadingID.HasValue) || ( ThematicHeadingID.Value < 0 ))
  207. {
  208. foreach (KeyValuePair<int, List<Item_Aggregation_Related_Aggregations>> theme in Aggregations_By_Thematic_Heading)
  209. {
  210. if (theme.Value.Contains(thisAggr))
  211. theme.Value.Remove(thisAggr);
  212. }
  213. return;
  214. }
  215. // If the thematic heading ID does not exit, just return
  216. if (!Aggregations_By_Thematic_Heading.ContainsKey(ThematicHeadingID.Value))
  217. return;
  218. // If this aggregation does not exist, just return
  219. if (!aggregationsByCode.ContainsKey(Code.ToUpper()))
  220. return;
  221. // Get this aggregation and list for this thematic heading
  222. List<Item_Aggregation_Related_Aggregations> thematicHeadingList = Aggregations_By_Thematic_Heading[ThematicHeadingID.Value];
  223. // If this is already a part of the thematic heading, just return
  224. if (thematicHeadingList.Contains(thisAggr))
  225. return;
  226. // Ensure this aggregation is not a part of any other thematic headings
  227. foreach (KeyValuePair<int, List<Item_Aggregation_Related_Aggregations>> theme in Aggregations_By_Thematic_Heading)
  228. {
  229. if (theme.Value.Contains(thisAggr))
  230. theme.Value.Remove(thisAggr);
  231. }
  232. // Now, add this to the list for this thematic heading
  233. int index = 0;
  234. while ((index < thematicHeadingList.Count) && (string.CompareOrdinal(thisAggr.Code, thematicHeadingList[index].Code) > 0))
  235. {
  236. index++;
  237. }
  238. thematicHeadingList.Insert(index, thisAggr);
  239. }
  240. /// <summary> Adds a new blank thematic heading when a user adds one through
  241. /// the administrative tools </summary>
  242. /// <param name="NewThematicHeadingID">ID for the new thematic heading</param>
  243. public void Add_Blank_Thematic_Heading(int NewThematicHeadingID)
  244. {
  245. if (!Aggregations_By_Thematic_Heading.ContainsKey(NewThematicHeadingID))
  246. {
  247. Aggregations_By_Thematic_Heading[NewThematicHeadingID] = new List<Item_Aggregation_Related_Aggregations>();
  248. }
  249. }
  250. /// <summary> Method is called by the serializer after this item is unserialized </summary>
  251. public void PostUnSerialization()
  252. {
  253. Aggregations_By_Thematic_Heading.Clear();
  254. aggregationsByType.Clear();
  255. aggregationsByCode.Clear();
  256. allTypes.Clear();
  257. foreach (Item_Aggregation_Related_Aggregations thisAggr in All_Aggregations)
  258. {
  259. // Add this to the various dictionaries
  260. aggregationsByCode[thisAggr.Code] = thisAggr;
  261. if (!allTypes.Contains(thisAggr.Type))
  262. {
  263. allTypes.Add(thisAggr.Type);
  264. }
  265. if (aggregationsByType.ContainsKey(thisAggr.Type))
  266. {
  267. aggregationsByType[thisAggr.Type].Add(thisAggr);
  268. }
  269. else
  270. {
  271. aggregationsByType[thisAggr.Type] = new List<Item_Aggregation_Related_Aggregations> { thisAggr };
  272. }
  273. }
  274. }
  275. }
  276. }