aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.Web/ExtensionMethods/CloudTableExtensions.cs
blob: ffffe69508e3f0ae2138ebaaa71df7ac792810df (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
using Microsoft.WindowsAzure.Storage.Table;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


public static class CloudTableExtensions
{
    /// <summary>
    /// Demonstrate the most efficient storage query - the point query - where both partition key and row key are specified.
    /// </summary>
    /// <param name="table">Sample table name</param>
    /// <param name="partitionKey">Partition key - i.e., last name</param>
    /// <param name="rowKey">Row key - i.e., first name</param>
    /// <returns>A Task object</returns>
    public static T GetEntity<T>(this CloudTable table, string partitionKey, string rowKey) where T : class, ITableEntity
    {
        TableOperation retrieveOperation = TableOperation.Retrieve<T>(partitionKey, rowKey);
        TableResult result = table.Execute(retrieveOperation);
        T customer = result.Result as T;
        return customer;
    }

    /// <summary>
    /// The Table Service supports two main types of insert operations.
    ///  1. Insert - insert a new entity. If an entity already exists with the same PK + RK an exception will be thrown.
    ///  2. Replace - replace an existing entity. Replace an existing entity with a new entity.
    ///  3. Insert or Replace - insert the entity if the entity does not exist, or if the entity exists, replace the existing one.
    ///  4. Insert or Merge - insert the entity if the entity does not exist or, if the entity exists, merges the provided entity properties with the already existing ones.
    /// </summary>
    /// <param name="table">The sample table name</param>
    /// <param name="entity">The entity to insert or merge</param>
    /// <returns>A Task object</returns>
    public static T InsertOrUpdateEntity<T>(this CloudTable table, T entity) where T : class, ITableEntity
    {
        if (entity == null)
        {
            throw new ArgumentNullException("Specified entity is null.");
        }

        // Create the InsertOrReplace table operation
        TableOperation insertOrMergeOperation = TableOperation.InsertOrReplace(entity);

        // Execute the operation.
        TableResult result = table.Execute(insertOrMergeOperation);
        T insertedCustomer = result.Result as T;

        return insertedCustomer;
    }

    /// <summary>
    /// Delete an entity
    /// </summary>
    /// <param name="table">Sample table name</param>
    /// <param name="deleteEntity">Entity to delete</param>
    /// <returns>A Task object</returns>
    public static void DeleteEntity<T>(this CloudTable table, T deleteEntity) where T : class, ITableEntity
    {
        if (deleteEntity == null)
        {
            throw new ArgumentNullException("Specified entity was null.");
        }

        TableOperation deleteOperation = TableOperation.Delete(deleteEntity);
        table.Execute(deleteOperation);
    }
}
e.JobHandler; e.JobHandler.StatusChanged += JobHandler_StatusChanged; e.JobHandler.Stopped += JobHandler_Stopped; _currentJobDTO = JobDTO.FromObservable(e.Job); _currentJobProcessParameters = ProcessParametersTableDTO.FromObservable(_handler.ProcessParameters); foreach (var client in _clients.ToList()) { try { RemoteJobProgress progress = new RemoteJobProgress(); progress.Stage = RemoteJobStage.Started; progress.JobStatus = _handler.JobStatus; await client.Receiver.SendGenericResponse(new RemoteJobUpdateResponse() { Job = _currentJobDTO, ProcessParameters = _currentJobProcessParameters, Progress = progress }, client.Token); client.JobSent = true; } catch { } } } private async void JobHandler_StatusChanged(object sender, RunningJobStatus e) { foreach (var client in _clients.ToList()) { if (client.JobSent) { try { await client.Receiver.SendGenericResponse(new RemoteJobUpdateResponse() { Progress = GetJobProgress(_handler) }, client.Token); } catch { } } else { try { RemoteJobProgress progress = new RemoteJobProgress(); progress.Stage = RemoteJobStage.Started; progress.JobStatus = _handler.JobStatus; await client.Receiver.SendGenericResponse(new RemoteJobUpdateResponse() { Job = _currentJobDTO, ProcessParameters = _currentJobProcessParameters, Progress = progress }, client.Token); client.JobSent = true; } catch { } } } } private async void JobHandler_Stopped(object sender, EventArgs e) { foreach (var client in _clients.ToList().Where(x => x.JobSent)) { try { await client.Receiver.SendGenericResponse(new RemoteJobUpdateResponse() { Progress = GetJobProgress(_handler), }, client.Token); client.JobSent = false; } catch { } } } private RemoteJobProgress GetJobProgress(JobHandler handler) { RemoteJobStage stage = RemoteJobStage.Running; if (_handler.Status.IsCanceled) { stage = RemoteJobStage.Aborted; } else if (_handler.Status.IsCompleted) { stage = RemoteJobStage.Completed; } else if (_handler.Status.IsFailed) { stage = RemoteJobStage.Failed; } return new RemoteJobProgress() { Stage = stage, JobStatus = handler.JobStatus, }; } [ExternalBridgeRequestHandlerMethod(typeof(RemoteJobUpdateRequest), RequestHandlerLoggingMode.LogRequestName)] public async Task OnRunningJobUpdateRequest(RemoteJobUpdateRequest request, String token, ExternalBridgeReceiver receiver) { this.ThrowIfDisabled(); if (!_clients.ToList().Exists(x => x.Receiver == receiver)) { _clients.Add(new RunningJobUpdateClient() { Receiver = receiver, Token = token }); } await receiver.SendGenericResponse(new RemoteJobUpdateResponse(), token); } public void OnReceiverDisconnected(ExternalBridgeReceiver receiver) { _clients.RemoveAll(x => x.Receiver == receiver); } } }