PageRenderTime 85ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 1ms

/Faceted Search 2.0/Facets.cs

#
C# | 297 lines | 242 code | 38 blank | 17 comment | 17 complexity | 09e0f2316e83dfa4938b5f04c1781b45 MD5 | raw file
  1. /// ===========================================================================
  2. /// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
  3. /// KIND, WHETHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  4. /// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR
  5. /// PURPOSE.
  6. /// ===========================================================================
  7. ///
  8. /// Project: MOSS Faceted Search
  9. /// Author: Leonid Lyublinski (leonidly@microsoft.com)
  10. /// Company: Microsoft Services
  11. /// Date: 09/17/2007 Version: 1.1
  12. ///
  13. /// ===========================================================================
  14. using System;
  15. using System.Collections;
  16. using System.Collections.Generic;
  17. using System.Text;
  18. using System.Xml;
  19. namespace Microsoft.SharePoint.Portal.ExtendedSearch
  20. {
  21. internal class FacetMenu : List<FacetCollection>, IComparer<FacetCollection>
  22. {
  23. #region Fields
  24. private FacetSortEnum _sortOrder = FacetSortEnum.Name;
  25. private int _hits;
  26. #endregion
  27. #region Properties
  28. public FacetSortEnum SortOrder
  29. {
  30. get { return _sortOrder; }
  31. set { _sortOrder = value; }
  32. }
  33. public int Hits
  34. {
  35. get
  36. {
  37. _hits = 0;
  38. for (int i = 0; i < Count; i++)
  39. {
  40. _hits += this[i].Hits;
  41. }
  42. return _hits;
  43. }
  44. }
  45. #endregion
  46. #region Methods
  47. public bool ContainsFacetCollection(string facetCollectionName)
  48. {
  49. for (int i = 0; i < Count; i++)
  50. {
  51. if (this[i].Name.ToLower() == facetCollectionName.ToLower()) return true;
  52. }
  53. return false;
  54. }
  55. public FacetCollection GetFacetCollection(string facetCollectionName)
  56. {
  57. for (int i = 0; i < Count; i++)
  58. {
  59. if (this[i].Name.ToLower() == facetCollectionName.ToLower()) return this[i];
  60. }
  61. return null;
  62. }
  63. #endregion
  64. #region Constructors
  65. public FacetMenu()
  66. : base()
  67. { }
  68. public FacetMenu(Dictionary<string, Dictionary<string, Int32>> facetMenu)
  69. : this()
  70. {
  71. foreach (string facetCollectionName in facetMenu.Keys)
  72. {
  73. FacetCollection facetCollection = new FacetCollection(facetCollectionName);
  74. this.Add(facetCollection);
  75. Dictionary<string, int> facets = facetMenu[facetCollectionName];
  76. foreach (string facetName in facets.Keys)
  77. {
  78. Facet facet = new Facet(facetName, facets[facetName]);
  79. facetCollection.Add(facet);
  80. }
  81. }
  82. }
  83. #endregion
  84. #region IComparer<FacetCollection> Members
  85. public int Compare(FacetCollection x, FacetCollection y)
  86. {
  87. switch (_sortOrder)
  88. {
  89. case FacetSortEnum.Name:
  90. return x.Name.CompareTo(y.Name);
  91. case FacetSortEnum.Hits:
  92. return -1 * x.Hits.CompareTo(y.Hits); // by default DESC
  93. case FacetSortEnum.Max:
  94. return -1 * x.Max.CompareTo(y.Max); // by default DESC
  95. default:
  96. return 0;
  97. }
  98. }
  99. #endregion
  100. }
  101. internal class FacetCollection : List<Facet>, IComparer<Facet>, ICloneable
  102. {
  103. #region Fields
  104. private FacetSortEnum _sortOrder = FacetSortEnum.None;
  105. private string _name;
  106. private int _hits;
  107. private int _max;
  108. #endregion
  109. #region Properties
  110. public int Hits
  111. {
  112. get
  113. {
  114. _hits = 0;
  115. for (int i = 0; i < Count; i++)
  116. {
  117. _hits += this[i].Hits;
  118. }
  119. return _hits;
  120. }
  121. }
  122. public int Max
  123. {
  124. get
  125. {
  126. for (int i = 0; i < Count; i++)
  127. {
  128. _max = Math.Max(_max, this[i].Hits);
  129. }
  130. return _max;
  131. }
  132. }
  133. public FacetSortEnum SortOrder
  134. {
  135. get { return _sortOrder; }
  136. set { _sortOrder = value; }
  137. }
  138. public string Name
  139. {
  140. get { return _name; }
  141. set { _name = value; }
  142. }
  143. #endregion
  144. #region Methods
  145. public bool ContainsFacet(string facetName)
  146. {
  147. for (int i = 0; i < this.Count; i++)
  148. {
  149. if (this[i].Name.ToLower() == facetName.ToLower()) return true;
  150. }
  151. return false;
  152. }
  153. public Facet GetFacet(string facetName)
  154. {
  155. for (int i = 0; i < this.Count; i++)
  156. {
  157. if (this[i].Name.ToLower() == facetName.ToLower()) return this[i];
  158. }
  159. return null;
  160. }
  161. #endregion
  162. #region Constructors
  163. public FacetCollection()
  164. : base()
  165. { }
  166. public FacetCollection(string name)
  167. : this()
  168. {
  169. _name = name;
  170. }
  171. #endregion
  172. #region IComparer<Facet> Members
  173. public int Compare(Facet x, Facet y)
  174. {
  175. switch (_sortOrder)
  176. {
  177. case FacetSortEnum.Name:
  178. return x.DisplayName.CompareTo(y.DisplayName);
  179. case FacetSortEnum.Hits:
  180. return -1 * x.Hits.CompareTo(y.Hits); // by default DESC
  181. default:
  182. return 0;
  183. }
  184. }
  185. #endregion
  186. #region ICloneable Members
  187. /// <summary>
  188. /// Returns deep copy of the object
  189. /// </summary>
  190. /// <returns></returns>
  191. public FacetCollection Clone()
  192. {
  193. FacetCollection clone = this;
  194. return clone;
  195. }
  196. #endregion
  197. #region ICloneable Members
  198. object ICloneable.Clone()
  199. {
  200. throw new Exception("The method or operation is not implemented.");
  201. }
  202. #endregion
  203. }
  204. internal class Facet
  205. {
  206. #region Fields
  207. private string _name;
  208. private string _displayName;
  209. private int _hits;
  210. private string _image;
  211. #endregion
  212. #region Properties
  213. public string Image
  214. {
  215. get { return _image; }
  216. set { _image = value; }
  217. }
  218. public string Name
  219. {
  220. get { return _name; }
  221. set { _name = value; }
  222. }
  223. public string DisplayName
  224. {
  225. get { return _displayName; }
  226. set { _displayName = value; }
  227. }
  228. public int Hits
  229. {
  230. get { return _hits; }
  231. set { _hits = value; }
  232. }
  233. #endregion
  234. #region Constructors
  235. public Facet(string name, int count)
  236. {
  237. _name = name;
  238. _displayName = name;
  239. _hits = count;
  240. }
  241. public Facet(string name, string displayName, int count):this( name, count)
  242. {
  243. _displayName = displayName;
  244. }
  245. public Facet(string name, string displayName, int count, string image)
  246. : this(name, displayName, count)
  247. {
  248. _image = image;
  249. }
  250. #endregion
  251. }
  252. public enum FacetSortEnum
  253. {
  254. None,
  255. Name,
  256. Hits,
  257. Max
  258. }
  259. }