Example #1
0
 public NewContractUserControl()
 {
     InitializeComponent();
     _viewModel  = new NewContractViewModel();
     DataContext = _viewModel;
     _viewModel.NewContract.Date = DatePicker.SelectedDate;
 }
Example #2
0
        public async Task <IActionResult> NewContract(NewContractViewModel model)
        {
            List <ContractActionParameter> paramList = new List <ContractActionParameter>();

            paramList.Add(new ContractActionParameter {
                Name = "Device", Value = model.Device
            });
            paramList.Add(new ContractActionParameter {
                Name = "SupplyChainOwner", Value = model.Owner
            });
            paramList.Add(new ContractActionParameter {
                Name = "SupplyChainObserver", Value = model.Observer
            });
            paramList.Add(new ContractActionParameter {
                Name = "MinHumidity", Value = model.MinHumidity.ToString()
            });
            paramList.Add(new ContractActionParameter {
                Name = "MaxHumidity", Value = model.MaxHumidity.ToString()
            });
            paramList.Add(new ContractActionParameter {
                Name = "MinTemperature", Value = model.MinTemperature.ToString()
            });
            paramList.Add(new ContractActionParameter {
                Name = "MaxTemperature", Value = model.MaxTemperature.ToString()
            });

            WorkflowActionInput wfActionInput = new WorkflowActionInput()
            {
                WorkflowActionParameters = paramList,
                WorkflowFunctionID       = 1
            };

            var jsonObject = JsonConvert.SerializeObject(wfActionInput);

            AuthenticationResult result = null;

            try
            {
                // Because we signed-in already in the WebApp, the userObjectId is know
                string userObjectID = (User.FindFirst("http://schemas.microsoft.com/identity/claims/objectidentifier"))?.Value;

                // Using ADAL.Net, get a bearer token to access the WorkbenchListService
                AuthenticationContext authContext = new AuthenticationContext(AzureAdOptions.Settings.Authority,
                                                                              new NaiveSessionCache(userObjectID, HttpContext.Session));
                ClientCredential credential = new ClientCredential(AzureAdOptions.Settings.ClientId, AzureAdOptions.Settings.ClientSecret);
                result = await authContext.AcquireTokenSilentAsync(AzureAdOptions.Settings.WorkbenchResourceId, credential,
                                                                   new UserIdentifier(userObjectID, UserIdentifierType.UniqueId));

                HttpClient client = new HttpClient();
                client.BaseAddress = new Uri(AzureAdOptions.Settings.WorkbenchBaseAddress);
                HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post,
                                                                    "/api/v1/contracts?workflowId=1&contractCodeId=1&connectionId=1");
                request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", result.AccessToken);
                request.Content = new StringContent(jsonObject.ToString(), System.Text.Encoding.UTF8, "application/json");

                HttpResponseMessage response = await client.SendAsync(request);

                if (response.IsSuccessStatusCode)
                {
                    JsonSerializerSettings settings = new JsonSerializerSettings();
                    String json_string = await response.Content.ReadAsStringAsync();

                    int newContractID = JsonConvert.DeserializeObject <int>(json_string);

                    HttpRequestMessage newRequest = new HttpRequestMessage(HttpMethod.Get, AzureAdOptions.Settings.WorkbenchBaseAddress + "/api/v1/contracts/" + newContractID);
                    newRequest.Headers.Authorization = new AuthenticationHeaderValue("Bearer", result.AccessToken);
                    HttpResponseMessage newResponse = await client.SendAsync(newRequest);

                    if (newResponse.IsSuccessStatusCode)
                    {
                        String new_json_string = await newResponse.Content.ReadAsStringAsync();

                        Contract newContract = JsonConvert.DeserializeObject <Contract>(new_json_string);

                        return(RedirectToAction("ContractDetail", newContract));
                    }
                    else
                    {
                        var x = 0;
                    }
                    return(View());
                }
                else
                {
                    var x = 0;
                }
            }
            catch (Exception e)
            {
                throw;
            }
            return(View());
        }
 public NewContractPage()
 {
     InitializeComponent();
     BindingContext = new NewContractViewModel();
 }