/// <inheritdoc/> public ServiceState InitializeService(Guid instanceId, string owner, string service, int partyId) { string developer = AuthenticationHelper.GetDeveloperUserName(_httpContextAccessor.HttpContext); string apiUrl = $"{_settings.GetRuntimeAPIPath(CreateInitialServiceStateMethod, owner, service, developer, partyId)}&instanceId={instanceId}"; ServiceState returnState = null; using (HttpClient client = AuthenticationHelper.GetDesignerHttpClient(_httpContextAccessor.HttpContext, _testdataRepositorySettings.GetDesignerHost())) { client.BaseAddress = new Uri(apiUrl); Task <HttpResponseMessage> response = client.GetAsync(apiUrl); if (!response.Result.IsSuccessStatusCode) { throw new Exception("Unable initialize service"); } else { try { returnState = response.Result.Content.ReadAsAsync <ServiceState>().Result; } catch { return(returnState); } } } return(returnState); }
/// <summary> /// Method that creates the form model object based on serialized data on disk. /// </summary> /// <param name="formID">The formId</param> /// <param name="type">The type that form data will be serialized to</param> /// <param name="org">The Organization code for the service owner</param> /// <param name="service">The service code for the current service</param> /// <param name="partyId">The partyId used to find the party on disc</param> /// <param name="developer">The name of the developer if any</param> /// <returns>The deserialized form model</returns> public object GetFormModel(int formID, Type type, string org, string service, int partyId, string developer = null) { string apiUrl = $"{_settings.GetRuntimeAPIPath(GetFormModelApiMethod, org, service, developer, partyId)}&formID={formID}"; using (HttpClient client = AuthenticationHelper.GetDesignerHttpClient(_httpContextAccessor.HttpContext, _testdataRepositorySettings.GetDesignerHost())) { client.BaseAddress = new Uri(apiUrl); Task <HttpResponseMessage> response = client.GetAsync(apiUrl); if (response.Result.IsSuccessStatusCode) { XmlSerializer serializer = new XmlSerializer(type); try { using (Stream stream = response.Result.Content.ReadAsStreamAsync().Result) { return(serializer.Deserialize(stream)); } } catch { return(Activator.CreateInstance(type)); } } else { return(Activator.CreateInstance(type)); } } }
/// <summary> /// Creates a list of form instances stored on disk for a given partyId and serviceId /// </summary> /// <param name="partyId">The partyId</param> /// <param name="org">The Organization code for the service owner</param> /// <param name="service">The service code for the current service</param> /// <param name="developer">The developer for the current service if any</param> /// <returns>The service instance list</returns> public List <ServiceInstance> GetFormInstances(int partyId, string org, string service, string developer = null) { string apiUrl = _settings.GetRuntimeAPIPath(GetFormInstancesApiMethod, org, service, developer, partyId); List <ServiceInstance> returnList = new List <ServiceInstance>(); using (HttpClient client = AuthenticationHelper.GetDesignerHttpClient(_httpContextAccessor.HttpContext, _testdataRepositorySettings.GetDesignerHost())) { client.BaseAddress = new Uri(apiUrl); Task <HttpResponseMessage> response = client.GetAsync(apiUrl); returnList = response.Result.Content.ReadAsAsync <List <ServiceInstance> >().Result; } return(returnList); }
/// <inheritdoc/> public void ArchiveServiceModel <T>(T dataToSerialize, Guid instanceId, Type type, string org, string service, int partyId) { string developer = AuthenticationHelper.GetDeveloperUserName(_httpContextAccessor.HttpContext); string apiUrl = $"{_settings.GetRuntimeAPIPath(ArchiveServiceModelApiMethod, org, service, developer, partyId)}&instanceId={instanceId}"; using (HttpClient client = AuthenticationHelper.GetDesignerHttpClient(_httpContextAccessor.HttpContext, _testdataRepositorySettings.GetDesignerHost())) { client.BaseAddress = new Uri(apiUrl); XmlSerializer serializer = new XmlSerializer(type); using (MemoryStream stream = new MemoryStream()) { serializer.Serialize(stream, dataToSerialize); stream.Position = 0; Task <HttpResponseMessage> response = client.PostAsync(apiUrl, new StreamContent(stream)); if (!response.Result.IsSuccessStatusCode) { throw new Exception("Unable to archive service model"); } } } }
public async Task <IActionResult> Index(string org, string service, int reporteeId) { var developer = AuthenticationHelper.GetDeveloperUserName(_httpContextAccessor.HttpContext); string apiUrl = _settings.GetRuntimeAPIPath("ZipAndSendRepo", org, service, developer); using (HttpClient client = AuthenticationHelper.GetDesignerHttpClient(_httpContextAccessor.HttpContext, _testdataRepositorySettings.GetDesignerHost())) { client.BaseAddress = new Uri(apiUrl); HttpResponseMessage response = await client.GetAsync(apiUrl); string zipPath = $"{_settings.GetServicePath(org, service, developer)}{service}.zip"; string extractPath = _settings.GetServicePath(org, service, developer); if (!Directory.Exists(extractPath)) { Directory.CreateDirectory(extractPath); } else { Directory.Delete(extractPath, true); Directory.CreateDirectory(extractPath); } using (Stream s = response.Content.ReadAsStreamAsync().Result) { using (var w = System.IO.File.OpenWrite(zipPath)) { s.CopyTo(w); } } ZipFile.ExtractToDirectory(zipPath, extractPath); } RequestContext requestContext = RequestHelper.GetRequestContext(Request.Query, 0); requestContext.UserContext = _userHelper.GetUserContext(HttpContext); requestContext.Reportee = requestContext.UserContext.Reportee; var startServiceModel = new StartServiceModel { ServiceID = org + "_" + service, ReporteeList = _authorization.GetReporteeList(requestContext.UserContext.UserId) .Select(x => new SelectListItem { Text = x.ReporteeNumber + " " + x.ReporteeName, Value = x.PartyID.ToString() }) .ToList(), PrefillList = _testdata.GetServicePrefill(requestContext.Reportee.PartyId, org, service) .Select(x => new SelectListItem { Text = x.PrefillKey + " " + x.LastChanged, Value = x.PrefillKey }) .ToList(), ReporteeID = requestContext.Reportee.PartyId, Org = org, Service = service, }; if (reporteeId != 0 && reporteeId != startServiceModel.ReporteeID && startServiceModel.ReporteeList.Any(r => r.Value.Equals(reporteeId.ToString()))) { startServiceModel.ReporteeID = reporteeId; requestContext.Reportee = _register.GetParty(startServiceModel.ReporteeID); requestContext.UserContext.ReporteeId = reporteeId; requestContext.UserContext.Reportee = requestContext.Reportee; HttpContext.Response.Cookies.Append("altinncorereportee", startServiceModel.ReporteeID.ToString()); } List <ServiceInstance> formInstances = _testdata.GetFormInstances(requestContext.Reportee.PartyId, org, service, AuthenticationHelper.GetDeveloperUserName(_httpContextAccessor.HttpContext)); ViewBag.InstanceList = formInstances.OrderBy(r => r.LastChanged).ToList(); return(View(startServiceModel)); }
/// <summary> /// Generates a new service instanceID for a service. /// </summary> /// <returns>A new instanceId.</returns> public async Task <Guid> InstantiateInstance(StartServiceModel startServiceModel, object serviceModel, IServiceImplementation serviceImplementation) { Guid instanceId = Guid.NewGuid(); string applicationId = startServiceModel.Service; string applicationOwnerId = startServiceModel.Org; int instanceOwnerId = startServiceModel.ReporteeID; Instance instance = new Instance { Id = instanceId.ToString(), InstanceOwnerId = instanceOwnerId.ToString(), ApplicationId = applicationId, CreatedBy = instanceOwnerId, CreatedDateTime = DateTime.UtcNow, }; string developer = AuthenticationHelper.GetDeveloperUserName(_httpContextAccessor.HttpContext); string apiUrl = $"{_settings.GetRuntimeAPIPath(SaveInstanceMethod, applicationOwnerId, applicationId, developer, instanceOwnerId)}&instanceId={instanceId}"; using (HttpClient client = AuthenticationHelper.GetDesignerHttpClient(_httpContextAccessor.HttpContext, _testdataRepositorySettings.GetDesignerHost())) { client.BaseAddress = new Uri(apiUrl); using (MemoryStream stream = new MemoryStream()) { var jsonData = JsonConvert.SerializeObject(instance); StreamWriter writer = new StreamWriter(stream); writer.Write(jsonData); writer.Flush(); stream.Position = 0; Task <HttpResponseMessage> response = client.PostAsync(apiUrl, new StreamContent(stream)); if (!response.Result.IsSuccessStatusCode) { throw new Exception("Unable to save instance"); } } } // Save instantiated form model Guid dataId = await _form.SaveFormModel( serviceModel, instanceId, serviceImplementation.GetServiceModelType(), applicationOwnerId, applicationId, instanceOwnerId, Guid.Empty); // Update instance with dataId instance = await GetInstance(applicationId, applicationOwnerId, instanceOwnerId, instanceId); Data data = new Data { Id = dataId.ToString(), ContentType = "application/Xml", StorageUrl = "data/boatdata/", CreatedBy = instanceOwnerId.ToString(), }; Dictionary <string, Data> formData = new Dictionary <string, Data>(); formData.Add(dataId.ToString(), data); instance.Data = new Dictionary <string, Dictionary <string, Data> >(); instance.Data.Add("boatData", formData); await UpdateInstance(instance, applicationId, applicationOwnerId, instanceOwnerId, instanceId); return(instanceId); }