Advertisement
Guest User

Untitled

a guest
Feb 28th, 2015
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.55 KB | None | 0 0
  1. [ComVisible(true)]
  2. [ClassInterface(ClassInterfaceType.None)]
  3. [ComDefaultInterface(typeof(IFunctionality))]
  4. public class ImportFunctionality : IFunctionality
  5. {
  6. private ImportWindow _window; // a WPF view
  7.  
  8. public ImportFunctionality()
  9. { }
  10.  
  11. public bool CanExecute()
  12. {
  13. return CurrentUser.IsAuthorized(AuthId);
  14. }
  15.  
  16. public string AuthId
  17. {
  18. get { return GetType().ToString(); }
  19. }
  20.  
  21. /// <summary>
  22. /// Prompts for an Excel workbook filename and creates pricing tables from workbook data.
  23. /// </summary>
  24. public void Execute()
  25. {
  26. try
  27. {
  28. if (!CanExecute()) throw new NotAuthorizedException(resx.Functionality_Execute_NotAuthorised);
  29.  
  30. var xlData = GetExcelSourceData();
  31. if (xlData == null) return;
  32.  
  33. var viewModel = GetImportSettings(xlData);
  34. if (viewModel == null) return;
  35.  
  36. if (!GetUserConfirmation(viewModel)) return;
  37. ImportGridContent(viewModel);
  38. }
  39. catch (NotAuthorizedException exception)
  40. {
  41. MsgBox.Failure(resx.NotAuthorized, exception.Message);
  42. }
  43. catch (Exception exception)
  44. {
  45. MsgBox.Error(exception);
  46. }
  47. }
  48.  
  49. private DataTable GetExcelSourceData()
  50. {
  51. var file = FileDialogHelper.GetOpenFileName(resx.FileDialogFilter_Excel97_2003);
  52. if (file == string.Empty) return null;
  53.  
  54. return Excel8OleDbHelper.ImportExcelFile(file);
  55. }
  56.  
  57. private ImportViewModel GetImportSettings(DataTable xlData)
  58. {
  59. var viewModel = new ImportViewModel(xlData);
  60.  
  61. // todo: add metadata to viewModel constructor...
  62. using (var data = new ImportModel(Settings.Default.DefaultDb))
  63. {
  64. // Wrap Linq2SQL objects into UI-visible instances:
  65. var meta = data.LoadImportMetadata()
  66. .Select(e => new ImportMetaData
  67. {
  68. // named property setters
  69. }).ToList();
  70.  
  71. // load metadata into ViewModel:
  72. viewModel.SetMetadata(new ObservableCollection<ImportMetaData>(meta));
  73. }
  74.  
  75. _window = new ImportWindow(viewModel);
  76.  
  77. viewModel.WindowClosing += viewModel_WindowClosing;
  78. _window.ShowDialog();
  79.  
  80. var result = viewModel.DialogResult;
  81. return (result == DialogResult.Ok)
  82. ? viewModel
  83. : null;
  84. }
  85.  
  86. private bool GetUserConfirmation(ImportViewModel viewModel)
  87. {
  88. var result = MsgBox.Prompt(resx.ConfirmationRequired, resx.ImportFunctionality_ConfirmProceed);
  89. return (result == DialogResult.Yes);
  90. }
  91.  
  92. void viewModel_WindowClosing(object sender, EventArgs e)
  93. {
  94. _window.Close();
  95. }
  96.  
  97. /// <summary>
  98. /// Returns a <see cref="IImportData"/> implementation that corresponds to database name specified by supplied <see cref="viewModel"/>.
  99. /// </summary>
  100. /// <param name="viewModel"></param>
  101. /// <returns></returns>
  102. public IImportData GetTargetConnection(ImportViewModel viewModel)
  103. {
  104. var connectionString = Settings.Default[viewModel.SelectedDatabase].ToString();
  105. return viewModel.SelectedCompany == CompaniesEnum.Company1.ToString()
  106. ? new ImportDataCompany1(connectionString)
  107. : viewModel.SelectedCompany == CompaniesEnum.Company2.ToString()
  108. ? new ImportDataCompany2(connectionString)
  109. : new ImportData(connectionString)
  110. // this is begging to be refactored
  111. ;
  112. }
  113.  
  114. private void ImportGridContent(ImportViewModel viewModel)
  115. {
  116. using (var data = GetTargetConnection(viewModel))
  117. {
  118. var args = new AsyncImportEventArgs(viewModel, data);
  119. var iterations = viewModel.GridSource.Rows.Count * viewModel.SelectedMetadata.Count();
  120.  
  121. data.BeginTransaction();
  122. MsgBox.ProgressStatus<AsyncImportEventArgs>(resx.ImportFunctionality_Title, resx.ImportFunctionality_PleaseWait, DoAsyncImport, args, CancelAsyncImport, iterations);
  123.  
  124. if (args.Success)
  125. data.CommitTransaction();
  126. else
  127. data.RollbackTransaction();
  128. }
  129. }
  130.  
  131. protected void DoAsyncImport(AsyncProgressArgs<AsyncImportEventArgs> e)
  132. {
  133. // Lengthy operation involving DAL calls and periodically updating the view
  134. // ...
  135.  
  136. // Update the view:
  137. e.UpdateProgress(resx.ProgressCompleted, maxProgressValue);
  138. e.UpdateMessage(resx.ImportFunctionality_TitleCompleted);
  139.  
  140. }
  141.  
  142. private void CancelAsyncImport(AsyncImportEventArgs e)
  143. {
  144. e.Success = false;
  145. e.Data.RollbackTransaction();
  146. MsgBox.Info(resx.ImportFunctionality_Cancelled_Title, resx.ImportFunctionality_Cancelled_Msg);
  147. }
  148.  
  149. #region nested types
  150.  
  151. /// <summary>
  152. /// Encapsulates parameters passed to async method for importing pricing tables.
  153. /// </summary>
  154. public class AsyncImportEventArgs : EventArgs
  155. {
  156. public ImportViewModel ViewModel { get; private set; }
  157. public IImportData Data { get; private set; }
  158. public bool Success { get; set; }
  159.  
  160. public AsyncImportEventArgs(ImportViewModel dialog, IImportData data)
  161. {
  162. ViewModel = dialog;
  163. Data = data;
  164. }
  165. }
  166.  
  167. #endregion
  168. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement