PageRenderTime 37ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/YABE/Models/TagDL.cs

#
C# | 115 lines | 97 code | 15 blank | 3 comment | 25 complexity | 710e0954cfeca39c622016ccbece66e7 MD5 | raw file
Possible License(s): LGPL-2.1
  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Linq;
  5. using System.Web;
  6. using System.Web.Security;
  7. using System.Web.UI;
  8. using System.Web.UI.HtmlControls;
  9. using System.Web.UI.WebControls;
  10. using System.Web.UI.WebControls.WebParts;
  11. using System.Xml.Linq;
  12. using System.Collections.Generic;
  13. using System.Data.Linq;
  14. namespace YABE.Models
  15. {
  16. public class TagDL
  17. {
  18. static public List<Tag> AddTags(string[] tagArray)
  19. {
  20. List<Tag> tagList = new List<Tag>();
  21. using (YabeDataContext context = YabeDataManager.Instance.DataContext)
  22. {
  23. foreach (string tagName in tagArray)
  24. {
  25. Tag t = context.Tags.SingleOrDefault(c =>
  26. c.TagName.ToLower() == tagName.Trim().ToLower() );
  27. if (t == null)
  28. {
  29. t = new Tag()//tag does not exist in db
  30. {
  31. TagName = tagName.Trim(),
  32. };
  33. context.Tags.InsertOnSubmit(t);
  34. }
  35. tagList.Add(t);
  36. }
  37. try
  38. {
  39. context.SubmitChanges(ConflictMode.ContinueOnConflict);
  40. }
  41. catch (ChangeConflictException)
  42. {
  43. context.ChangeConflicts.ResolveAll(RefreshMode.KeepCurrentValues); //User values will be keeped on the resolution
  44. }
  45. }
  46. return tagList;
  47. }
  48. static public List<Tag> GetTags()
  49. {
  50. using (YabeDataContext context = YabeDataManager.Instance.DataContext)
  51. {
  52. return context.Tags.ToList();
  53. }
  54. }
  55. static public List<vwTagCount> GetTagCounts()
  56. {
  57. List<vwTagCount> tagCloud;
  58. using (YabeDataContext context = YabeDataManager.Instance.DataContext)
  59. {
  60. tagCloud = context.vwTagCounts.ToList();
  61. }
  62. if (tagCloud == null || tagCloud.Count == 0)
  63. return tagCloud;
  64. double mean = 0;
  65. double stdDev = 0;
  66. // calculate mean
  67. foreach (vwTagCount tag in tagCloud)
  68. {
  69. mean = mean + tag.TagCount ?? 0;
  70. }
  71. mean = mean / tagCloud.Count;
  72. // calculate std
  73. foreach (vwTagCount tag in tagCloud)
  74. {
  75. stdDev = stdDev + Math.Pow(tag.TagCount ?? 0 - mean,2);
  76. }
  77. stdDev = Math.Sqrt(stdDev / tagCloud.Count);
  78. // set the sizes based on standard deviation
  79. foreach (vwTagCount tag in tagCloud)
  80. {
  81. double factor = (tag.TagCount ?? 0 - mean) / stdDev;
  82. if( factor <= -2*stdDev )
  83. tag.TagSize = 60;
  84. else if ( -2*stdDev < factor && factor <= -1*stdDev )
  85. tag.TagSize = 80;
  86. else if (-1 * stdDev < factor && factor <= -0.5 * stdDev)
  87. tag.TagSize = 100;
  88. else if (-0.5 * stdDev < factor && factor < 0.5 * stdDev)
  89. tag.TagSize = 120;
  90. else if (0.5 * stdDev <= factor && factor < 1 * stdDev)
  91. tag.TagSize = 140;
  92. else if (1 * stdDev <= factor && factor < 2 * stdDev)
  93. tag.TagSize = 160;
  94. else if ( factor >= 2*stdDev )
  95. tag.TagSize = 180;
  96. }
  97. return tagCloud;
  98. }
  99. }
  100. }