private async void Delete()
        {
            Log.Info(LogMessages.ManageDataSetDeleteCommand);
            if (SelectedDataSet == null)
            {
                return;
            }
            var context = new CommonDialogViewModel
            {
                Buttons = ButtonsEnum.YesNo,
                Header  = "Delete dataset",
                Content = new JContent("Are you sure to remove the following data set: " + SelectedDataSet.Name)
            };
            var view = new CommonDialog {
                DataContext = context
            };
            var canClose = false;
            var result   = await _dialogHandler.Show(view, "RootDialog",
                                                     async (object sender, DialogClosingEventArgs args) =>
            {
                if (!canClose && (CommonDialogResult)args.Parameter == CommonDialogResult.Yes)
                {
                    args.Cancel();
                    args.Session.UpdateContent(new ProgressDialog());
                    var isSuccessful = false;
                    var errorMessage = "";
                    try
                    {
                        var response = await _dataSetManager.DeleteDataSetAsync(SelectedDataSet.Name);
                        isSuccessful = response.IsSuccessful;
                        ResponseValidator.Validate(response, false);
                    }
                    catch (Exception exception)
                    {
                        isSuccessful = false;
                        errorMessage = exception.Message;
                    }
                    finally
                    {
                        if (!isSuccessful)
                        {
                            context.ErrorMessage = errorMessage;
                            context.ShowError    = true;
                            args.Session.UpdateContent(view);
                        }
                        else
                        {
                            canClose = true;
                            args.Session.Close((CommonDialogResult)args.Parameter);
                        }
                    }
                }
            });

            if ((CommonDialogResult)result == CommonDialogResult.Yes)
            {
                DispatcherHelper.CheckBeginInvokeOnUI(() => DataSets.Remove(SelectedDataSet));
            }
        }
 private async void Rename()
 {
     Log.Info(LogMessages.ManageDataSetRenameCommand);
     if (SelectedDataSet == null)
     {
         return;
     }
     var originalName = SelectedDataSet.Name;
     var context      = new CommonDialogViewModel
     {
         Header  = "Rename Dataset",
         Content = new JContent(originalName),
         Buttons = ButtonsEnum.OkCancel
     };
     var view = new CommonDialog {
         DataContext = context
     };
     var canClose = false;
     await _dialogHandler.Show(view, "RootDialog", async (object s, DialogClosingEventArgs args) =>
     {
         if (!canClose && (CommonDialogResult)args.Parameter == CommonDialogResult.Ok)
         {
             args.Cancel();
             args.Session.UpdateContent(new ProgressDialog());
             var isSuccessFul = true;
             var errorMessage = "";
             var newName      = "";
             try
             {
                 newName      = ((JContent)context.Content).GetJToken().ToString();
                 var response = await _dataSetManager.UpdateDataSetAsync(originalName, new DataSetUpdate {
                     Name = newName
                 });
                 ResponseValidator.Validate(response, false);
             }
             catch (Exception exception)
             {
                 isSuccessFul = false;
                 errorMessage = exception.Message;
             }
             finally
             {
                 if (!isSuccessFul)
                 {
                     context.ErrorMessage = errorMessage;
                     context.ShowError    = true;
                     args.Session.UpdateContent(view);
                 }
                 else
                 {
                     var selectedIndex            = DataSets.IndexOf(SelectedDataSet);
                     DataSets[selectedIndex].Name = newName;
                     DataSets = new ObservableCollection <DataSet>(DataSets);
                     Messenger.Default.Send(new UpdateMessage(UpdateType.DatasetRename, originalName));
                     canClose = true;
                     args.Session.Close((CommonDialogResult)args.Parameter);
                 }
             }
         }
     });
 }
        private async Task Add(DataSet selectedDataSet = null)
        {
            Log.Info(LogMessages.ManageDataSetAddCommand);
            var newDataSet = selectedDataSet == null ? new DataSet
            {
                NGramCount        = 3,
                IdField           = "id",
                TagField          = "tag",
                InterpretedFields = new List <string> {
                    "title", "desc"
                },
                SampleDocument = JsonConvert.SerializeObject(new
                {
                    id    = 10,
                    title = "thisisthetitle",
                    desc  = "thisisthedesc",
                    tag   = "tag1"
                }, Formatting.Indented),
                Schema = JsonConvert.SerializeObject(new
                {
                    type       = "object",
                    properties = new
                    {
                        id    = new { type = "integer" },
                        title = new { type = "string" },
                        desc  = new { type = "string" },
                        tag   = new { type = "string" }
                    }
                }, Formatting.Indented)
            } : new DataSet
            {
                NGramCount        = SelectedDataSet.NGramCount,
                IdField           = SelectedDataSet.IdField,
                TagField          = SelectedDataSet.TagField,
                InterpretedFields = SelectedDataSet.InterpretedFields,
                SampleDocument    = SelectedDataSet.SampleDocument ?? JsonConvert.SerializeObject(new
                {
                    id    = 10,
                    title = "thisisthetitle",
                    desc  = "thisisthedesc",
                    tag   = "tag1"
                }, Formatting.Indented),
                Schema = selectedDataSet.Schema ?? JsonConvert.SerializeObject(new
                {
                    type       = "object",
                    properties = new
                    {
                        id    = new { type = "integer" },
                        title = new { type = "string" },
                        desc  = new { type = "string" },
                        tag   = new { type = "string" }
                    }
                }, Formatting.Indented)
            };

            var context = new CommonDialogViewModel
            {
                Header  = "Add Dataset",
                Buttons = ButtonsEnum.OkCancel,
                Content = new NewDataSetWrapper {
                    DataSet = newDataSet, SampleDocumentChecked = true
                }
            };
            var view = new CommonDialog {
                DataContext = context
            };
            var canClose = false;
            var result   = await _dialogHandler.Show(view, "RootDialog",
                                                     async (object sender, DialogClosingEventArgs args) =>
            {
                if (!canClose && (CommonDialogResult)args.Parameter == CommonDialogResult.Ok)
                {
                    args.Cancel();
                    args.Session.UpdateContent(new ProgressDialog());
                    var isSuccessful = false;
                    var errorMessage = "";
                    try
                    {
                        var wrapper = (NewDataSetWrapper)(context.Content);
                        newDataSet  = wrapper.DataSet;
                        if (wrapper.SampleDocumentChecked)
                        {
                            newDataSet.Schema         = null;
                            newDataSet.SampleDocument = JsonConvert.DeserializeObject(newDataSet.SampleDocument.ToString());
                        }
                        else
                        {
                            newDataSet.SampleDocument = null;
                            newDataSet.Schema         = JsonConvert.DeserializeObject((newDataSet.Schema).ToString());
                        }
                        newDataSet.InterpretedFields = newDataSet.InterpretedFields.Where(f => !string.IsNullOrEmpty(f.Trim())).ToList();
                        var response = wrapper.SampleDocumentChecked ? await _dataSetManager.CreateDataSetAsync(newDataSet) : await _dataSetManager.CreateDataSetSchemaAsync(newDataSet);
                        isSuccessful = response.IsSuccessful;
                        ResponseValidator.Validate(response, false);
                    }
                    catch (Exception exception)
                    {
                        isSuccessful = false;
                        errorMessage = exception.Message;
                    }
                    finally
                    {
                        if (!isSuccessful)
                        {
                            context.ErrorMessage = errorMessage;
                            context.ShowError    = true;
                            args.Session.UpdateContent(view);
                        }
                        else
                        {
                            canClose = true;
                            args.Session.Close((CommonDialogResult)args.Parameter);
                        }
                    }
                }
            });

            if ((CommonDialogResult)result == CommonDialogResult.Ok)
            {
                DataSets.Add(newDataSet);
            }
        }