PageRenderTime 57ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Lync.Widget.Library/Presentation/Presenters/LanguagePacksPresenter.cs

#
C# | 235 lines | 194 code | 41 blank | 0 comment | 27 complexity | 02763248f9d3fee9c9f9c069575336b4 MD5 | raw file
  1. using System;
  2. using System.Globalization;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Web;
  7. using Lync.Widget.Library.Presentation.Entities;
  8. using Lync.Widget.Library.Presentation.Models;
  9. using Lync.Widget.Library.Presentation.Views;
  10. namespace Lync.Widget.Library.Presentation.Presenters
  11. {
  12. public class LanguagePacksPresenter : ILanguagePacksPresenter
  13. {
  14. public ILanguagePacksView View { get; set; }
  15. public ILanguagePacksModel Model { get; set; }
  16. public LanguagePacksPresenter(ILanguagePacksView view, ILanguagePacksModel model)
  17. {
  18. View = view;
  19. Model = model;
  20. }
  21. public void OnLoad()
  22. {
  23. View.ShowDownloadButton = true;
  24. var allPacks = Model.GetAll();
  25. var allLanguages = allPacks.GroupBy(p => p.Language).Select(p => p.Key).ToList();
  26. View.SupportedLanguages = allLanguages.Select(l => new Language
  27. {
  28. Name = l,
  29. Value = l
  30. }).Cast<ILanguage>().ToList();
  31. var defaultPack = allPacks.SingleOrDefault(p => p.IsDefault);
  32. if (defaultPack != null)
  33. {
  34. View.Language = View.CurrentLanguage = defaultPack.Language;
  35. View.IsDefaultLanguage = defaultPack.IsDefault;
  36. var resources = defaultPack.Resources;
  37. if (resources != null && resources.Count > 0)
  38. {
  39. View.Resources = resources;
  40. View.ShowResults = true;
  41. View.ShowSaveButton = true;
  42. }
  43. else
  44. {
  45. View.ErrorMessage = "No resources found";
  46. }
  47. }
  48. }
  49. public bool AddLanguageResource()
  50. {
  51. View.Saved = false;
  52. var newLanguageResource = View.NewLanguageResource;
  53. var allResources = View.Resources;
  54. if (allResources.Any(f => f.Name.ToLower() == newLanguageResource.Name.ToLower() && f.Key.ToLower() == newLanguageResource.Key.ToLower()))
  55. {
  56. View.ErrorMessage = String.Format("Resource with name '{0}' and key '{1}' already exists", newLanguageResource.Name, newLanguageResource.Key);
  57. return false;
  58. }
  59. View.AddToList(newLanguageResource);
  60. return true;
  61. }
  62. public void UpdateView()
  63. {
  64. }
  65. public bool RemoveLanguageResource(string key, string name)
  66. {
  67. View.Saved = false;
  68. var allResources = View.Resources;
  69. var resource = allResources.SingleOrDefault(r => r.Key == key && r.Name == name);
  70. if (resource != null)
  71. {
  72. allResources.Remove(resource);
  73. View.Resources = allResources;
  74. return true;
  75. }
  76. View.ErrorMessage = String.Format("Resource with key '{0}' and name '{1}' was not found", key, name);
  77. return false;
  78. }
  79. public void ChangeLanguage()
  80. {
  81. try
  82. {
  83. var language = View.Language;
  84. var currentLanguage = View.CurrentLanguage;
  85. if (language == currentLanguage)
  86. return;
  87. var pack = Model.Get(language);
  88. if (pack == null || pack.Resources == null || pack.Resources.Count == 0)
  89. {
  90. pack = Model.GetDefault();
  91. View.IsDefaultLanguage = false;
  92. View.Resources = pack.Resources.Select(p => new LanguageResource
  93. {
  94. Name = p.Name,
  95. Key = p.Key
  96. }).Cast<ILanguageResource>().ToList();
  97. }
  98. else
  99. {
  100. View.IsDefaultLanguage = pack.IsDefault;
  101. View.Resources = pack.Resources;
  102. }
  103. View.CurrentLanguage = language;
  104. View.Saved = false;
  105. }
  106. catch (Exception exception)
  107. {
  108. View.ErrorMessage = String.Format("{0}", exception.Message);
  109. }
  110. }
  111. public bool Save()
  112. {
  113. var resources = View.Resources;
  114. var language = View.Language;
  115. var isDefault = View.IsDefaultLanguage;
  116. try
  117. {
  118. if (resources.Count > 0)
  119. {
  120. Model.Save(language, resources, isDefault);
  121. }
  122. View.Saved = true;
  123. return true;
  124. }
  125. catch (Exception exception)
  126. {
  127. View.ErrorMessage = exception.Message;
  128. }
  129. return false;
  130. }
  131. public void DownloadResourceFile(HttpContext context)
  132. {
  133. try
  134. {
  135. var pack = Model.Get(View.CurrentLanguage);
  136. var sb = new StringBuilder();
  137. var resourcesGrouped = pack.Resources.GroupBy(r => r.Name).ToList().Select(g => new { g.Key, Resources = g }).ToList();
  138. foreach (var resourceGroup in resourcesGrouped)
  139. {
  140. sb.AppendLine(String.Format("{0} = {{", resourceGroup.Key));
  141. var counter = 0;
  142. var resourceCount = resourceGroup.Resources.Count();
  143. foreach (var resource in resourceGroup.Resources)
  144. {
  145. var comma = (resourceCount > 1 && counter < resourceCount - 1) ? "," : "";
  146. sb.AppendLine(String.Format(" {0}: \"{1}\"{2}", resource.Key, resource.Value, comma));
  147. counter++;
  148. }
  149. sb.AppendLine(String.Format("}};"));
  150. sb.AppendLine();
  151. }
  152. var bytes = Encoding.UTF8.GetBytes(sb.ToString());
  153. context.Response.Clear();
  154. context.Response.ContentType = "text/xml";
  155. context.Response.AddHeader("content-disposition", String.Format("attachment;filename=widget-{0}.js", pack.Language.ToLower()));
  156. context.Response.Buffer = true;
  157. context.Response.OutputStream.Write(bytes, 0, bytes.Length);
  158. context.Response.End();
  159. }
  160. catch (Exception exception)
  161. {
  162. View.ErrorMessage = String.Format("Error downloading resource : {0}", exception.Message);
  163. }
  164. }
  165. public void AddLanguage()
  166. {
  167. try
  168. {
  169. var newLanguageCode = View.NewLanguage;
  170. if (!String.IsNullOrEmpty(newLanguageCode))
  171. {
  172. var languages = View.SupportedLanguages;
  173. if (languages.Any(l => l.Value == newLanguageCode))
  174. {
  175. throw new Exception("Language already exists");
  176. }
  177. var newLanguage = new Language
  178. {
  179. Name = newLanguageCode,
  180. Value = newLanguageCode,
  181. };
  182. languages.Add(newLanguage);
  183. View.SupportedLanguages = languages;
  184. View.Language = newLanguage.Value;
  185. ChangeLanguage();
  186. }
  187. }
  188. catch (Exception exception)
  189. {
  190. View.ErrorMessage = String.Format("Error adding language : {0}", exception.Message);
  191. }
  192. }
  193. }
  194. }