A single batch operation for a document EVAL (using a Javascript)
Inheritance: ICommandData
Example #1
0
		public override void Execute(object parameter)
		{
		    AskUser.ConfirmationAsync("Patch Documents", "Are you sure you want to apply this patch to all matching documents?")
                .ContinueWhenTrueInTheUIThread(() =>
                {
                    patchModel.ClearQueryError();

					var values = patchModel.GetValues();
					if (values == null)
						return;
                    var request = new ScriptedPatchRequest { Script = patchModel.Script.CurrentSnapshot.Text, Values = values};
					patchModel.InProcess.Value = true;

                    switch (patchModel.PatchOn)
                    {
                        case PatchOnOptions.Document:
                            var commands = new ICommandData[1];
                            commands[0] = new ScriptedPatchCommandData
                            {
                                Patch = request,
                                Key = patchModel.SelectedItem
                            };

		                    ApplicationModel.Database.Value.AsyncDatabaseCommands.BatchAsync(commands)
			                    .ContinueOnUIThread(t => { if (t.IsFaulted) patchModel.HandlePatchError(t.Exception); })
			                    .ContinueOnSuccessInTheUIThread(
				                    () => ApplicationModel.Database.Value.AsyncDatabaseCommands.GetAsync(patchModel.SelectedItem).
					                          ContinueOnSuccessInTheUIThread(doc =>
					                          {
						                          patchModel.OriginalDoc.SetText(doc.ToJson().ToString());
						                          patchModel.NewDoc.SetText("");
						                          patchModel.ShowAfterPrompt = true;
					                          }))
			                    .Finally(() => patchModel.InProcess.Value = false);
                            break;

                        case PatchOnOptions.Collection:
                            ApplicationModel.Database.Value.AsyncDatabaseCommands.UpdateByIndex(PatchModel.CollectionsIndex,
                                                                                                new IndexQuery { Query = "Tag:" + patchModel.SelectedItem }, request)
																								.ContinueOnSuccessInTheUIThread(() => patchModel.UpdateCollectionSource())
                                                                                                 .ContinueOnUIThread(t => { if (t.IsFaulted) patchModel.HandlePatchError(t.Exception); })
																								 .Finally(() => patchModel.InProcess.Value = false);
                            break;

                        case PatchOnOptions.Index:
                            ApplicationModel.Database.Value.AsyncDatabaseCommands.UpdateByIndex(patchModel.SelectedItem, new IndexQuery { Query = patchModel.QueryDoc.CurrentSnapshot.Text },
                                                                                                request)
																								.ContinueOnSuccessInTheUIThread(() => patchModel.UpdateCollectionSource())
                                                                                                 .ContinueOnUIThread(t => { if (t.IsFaulted) patchModel.HandlePatchError(t.Exception); })
																								 .Finally(() => patchModel.InProcess.Value = false);
                            break;
                    }

					
                });
		}
Example #2
0
		public override void Execute(object parameter)
		{
            patchModel.ClearQueryError();

			AskUser.ConfirmationAsync("Patch Documents", "Are you sure you want to apply this patch to all selected documents?")
				.ContinueWhenTrueInTheUIThread(() =>
				{
					var values = patchModel.GetValues();
					if (values == null)
						return;
					var request = new ScriptedPatchRequest {Script = patchModel.Script.CurrentSnapshot.Text, Values = values};
					var selectedItems = patchModel.QueryResults.ItemSelection.GetSelectedItems();
					var commands = new ICommandData[selectedItems.Count()];
					var counter = 0;

					foreach (var item in selectedItems)
					{
						commands[counter] = new ScriptedPatchCommandData
						{
							Patch = request,
							Key = item.Item.Id
						};

						counter++;
					}

					ApplicationModel.Database.Value.AsyncDatabaseCommands
						.BatchAsync(commands)
                         .ContinueOnUIThread(t => { if (t.IsFaulted) patchModel.HandlePatchError(t.Exception); })
						.ContinueOnSuccessInTheUIThread(() => ApplicationModel.Database.Value
							.AsyncDatabaseCommands
							.GetAsync(patchModel.SelectedItem)
							.ContinueOnSuccessInTheUIThread(doc =>
							{
								if (doc != null)
								{
									patchModel.OriginalDoc.SetText(doc.ToJson().ToString());
									patchModel.NewDoc.SetText("");
									patchModel.ShowAfterPrompt = true;
								}
								else
								{
									patchModel.OriginalDoc.SetText("");
									patchModel.NewDoc.SetText("");
									patchModel.ShowAfterPrompt = true;
									patchModel.ShowBeforeAndAfterPrompt = true;
								}
							}));
				});
		}
Example #3
0
		public override void Execute(object parameter)
		{
            patchModel.ClearQueryError();

			var values = patchModel.GetValues();
			if (values == null)
				return;
			var request = new ScriptedPatchRequest {Script = patchModel.Script.CurrentSnapshot.Text, Values = values};
			var commands = new ICommandData[1];

			switch (patchModel.PatchOn)
			{
				case PatchOnOptions.Document:
					ApplicationModel.Database.Value.AsyncDatabaseCommands.GetAsync(patchModel.SelectedItem).
						ContinueOnSuccessInTheUIThread(doc => patchModel.OriginalDoc.SetText(doc.ToJson().ToString()));

					commands[0] = new ScriptedPatchCommandData
					{
						Patch = request,
						Key = patchModel.SelectedItem,
						DebugMode = true
					};

					break;

				case PatchOnOptions.Collection:
				case PatchOnOptions.Index:
			        var selectedItem = patchModel.QueryResults.ItemSelection.GetSelectedItems().FirstOrDefault();
                    if (selectedItem == null || !selectedItem.IsRealized)
                    {
                        return;
                    }

                    patchModel.OriginalDoc.SetText(selectedItem.Item.Document.ToJson().ToString());
			        var docId = selectedItem.Item.Document.Key;

					commands[0] = new ScriptedPatchCommandData
					{
						Patch = request,
						Key = docId,
						DebugMode = true
					};

					break;
			}

			patchModel.InProcess.Value = true;

			ApplicationModel.Database.Value.AsyncDatabaseCommands.BatchAsync(commands)
				.ContinueOnSuccessInTheUIThread(batch => patchModel.NewDoc.SetText(batch[0].AdditionalData.ToString()))
				.ContinueOnUIThread(t => { if (t.IsFaulted) patchModel.HandlePatchError(t.Exception); })
				.Finally(() => patchModel.InProcess.Value = false);

		    patchModel.ShowBeforeAndAfterPrompt = false;
			patchModel.ShowAfterPrompt = false;
		}
        private void SaveEmployeeEvents(
            Employee employee            )
        {
            var patches = new List<PatchRequest>();
            foreach (var evt in employee.PendingEvents)
            {
                patches.Add(new PatchRequest
                { 
                    Type = PatchCommandType.Add,
                    Name = "Events",
                    Value = RavenJObject.FromObject(evt, _serializer)
                });
            }
            var localId = $"employees/{employee.Id}";

            var addEmployeeEvents = new PatchCommandData()
            {
                Key = localId,
                Patches = patches.ToArray()
            }; 

            var updateMetadata = new ScriptedPatchCommandData()
            {
                Key = localId,
                Patch = new ScriptedPatchRequest
                {
                    Script = $"this['@metadata']['{EmployeeEntityVersion}'] = {employee.Version}; "
                }
            };
            
            _store.DatabaseCommands.Batch(new ICommandData[]
            {
                addEmployeeEvents,
                updateMetadata
            });
        }