/src/AppLogic/Presenters/DicitionaryEditor/CarModelPresenter.cs

https://bitbucket.org/tkestowicz/solidworkshop · C# · 174 lines · 114 code · 37 blank · 23 comment · 16 complexity · a7fda39ac2c85445ac4c94123890193e MD5 · raw file

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using AppLogic.Interfaces;
  6. using AppLogic.Interfaces.Editors;
  7. namespace AppLogic.Presenters.DicitionaryEditor
  8. {
  9. /// <summary>
  10. /// Klasa logiki do obsługi kontrolki edytora
  11. /// </summary>
  12. public partial class CarModelPresenter : EditorPresenter, IRelationalEditorPresenter
  13. {
  14. #region Properties
  15. /// <summary>
  16. /// Dostęp do właściwego widoku dla logiki
  17. /// </summary>
  18. protected ICarModelEditorView _View
  19. {
  20. get
  21. {
  22. return _view as ICarModelEditorView;
  23. }
  24. }
  25. #endregion
  26. public override void RecordReceived(EventArgs.RecordEventArgs record)
  27. {
  28. try
  29. {
  30. var rec = record.Record<Model.Projection.CarModel>();
  31. ID = rec.Id;
  32. _View.ModelName = rec.Name;
  33. _View.BrandName = rec.BrandName;
  34. }
  35. catch (InvalidCastException)
  36. {
  37. Reset();
  38. }
  39. }
  40. public override void SaveChanges()
  41. {
  42. try
  43. {
  44. // Sprawdzamy poprawność wprowadzonych danych
  45. Validate();
  46. // Pobranie uchwytu do bd
  47. var db = Core.ServiceManager.Database;
  48. // Sprawdzamy, czy wpis jest unikalny
  49. var query = from o in db.CarModels where o.ModelName == _View.ModelName && o.CarBrandId== _View.BrandId && o.CarModelId != ID select o;
  50. // Jeżeli wpis nie jest unikalny
  51. if (query.Count() > 0)
  52. throw new ArgumentException(String.Format(Properties.ApplicationMessages.Default.msgDictionaryDataRedundant, _View.ModelName));
  53. // Dodajemy nowy rekord jeżeli nie mamy w pamięci ID
  54. if (ID == 0)
  55. {
  56. // Dodajemy nowy wpis do bazy
  57. db.CarModels.AddObject(new Model.CarModel() {
  58. ModelName = _View.ModelName,
  59. CarBrandId = (int)_View.BrandId
  60. });
  61. }
  62. else
  63. {
  64. // Pobieramy rekord z bazy żeby wprowadzić nowe dane
  65. var record = (from o in db.CarModels where o.CarModelId == ID select o).Single();
  66. record.ModelName = _View.ModelName;
  67. record.CarBrandId = (int)_View.BrandId;
  68. }
  69. // Zapisujemy zmiany w bazie
  70. db.SaveChanges();
  71. _view.ShowInfo(Properties.ApplicationMessages.Default.msgDefaultSaveSucceed);
  72. Reset();
  73. Core.ServiceManager.Events.OnSaveSucceed(this, new EventArgs.RecordEventArgs(new Model.Projection.CarModel() { }.GetType()));
  74. }// Błędne dane
  75. catch (ArgumentException e)
  76. {
  77. _view.ShowError(e.Message, Properties.ApplicationMessages.Default.msgSaveErrorCaption);
  78. }// Wpis nie istnieje w bazie
  79. catch (InvalidOperationException)
  80. {
  81. _view.ShowError(String.Format(Properties.ApplicationMessages.Default.msgInvalidRecordId, ID), Properties.ApplicationMessages.Default.msgSaveErrorCaption);
  82. }// Wszelkie inne błędy napotkane podczas zapisu
  83. catch (Exception)
  84. {
  85. _view.ShowError(Properties.ApplicationMessages.Default.msgDefaultSaveError, Properties.ApplicationMessages.Default.msgSaveErrorCaption);
  86. }
  87. }
  88. public override void Validate()
  89. {
  90. string msg = null;
  91. // Nazwa nie może być pusta
  92. if (String.IsNullOrEmpty(_View.ModelName))
  93. msg += Properties.ValidationMessages.Default.emptyCarModelName + "\n";
  94. // Należy wybrać markę
  95. if (_View.BrandId == null || _View.BrandId <= 0)
  96. msg += Properties.ValidationMessages.Default.emptyCarBrand + "\n";
  97. // Mamy błędy to rzucamy wyjątek
  98. if (msg != null)
  99. throw new ArgumentException(String.Format(Properties.ValidationMessages.Default.validationError, msg));
  100. }
  101. public override void Reset()
  102. {
  103. try
  104. {
  105. _View.ModelName = null;
  106. _View.BrandId = null;
  107. base.Reset();
  108. // Próbujemy załadować marki samochodów
  109. LoadRelatedData();
  110. }// Brak marek w bazie
  111. catch (Core.Exceptions.DataNotFoundException e)
  112. {
  113. _View.ShowError(e.Message, Properties.ApplicationMessages.Default.msgRequiredDataError);
  114. ForceOffView = true;
  115. }
  116. }
  117. /// <summary>
  118. /// Metoda ładuje marki do listy rozwijanej
  119. /// Jeżeli w bazie nie ma marek rzucany wyjątek DataNotFoundException
  120. /// </summary>
  121. public void LoadRelatedData()
  122. {
  123. // Pobranie uchwytu do bd
  124. var db = Core.ServiceManager.Database;
  125. var brands = from c in db.CarBrands
  126. orderby c.BrandName
  127. select new Model.Projection.ComboBox
  128. {
  129. Id = c.CarBrandId,
  130. Value = c.BrandName
  131. };
  132. if (brands.Count() == 0)
  133. throw new Core.Exceptions.DataNotFoundException(Properties.ApplicationMessages.Default.msgCarBrandsBeforeModels);
  134. _View.CarBrands = brands.ToList();
  135. }
  136. }
  137. }