static VeisConfig RetrieveVeisConfig(string servicePathField, string subscriptionKeyField) { var config = new VeisConfig() { }; //Entity settings = this.RetrieveActiveSettings("patsr_transactiontiming", "patsr_veisservicebaseurl", "patsr_vastlistrefreshapiurl", // "patsr_veisorganizationname", "patsr_veisclientsecret", "patsr_veistenantid", "patsr_veisclientapplicationid", // "patsr_veisresourceid", servicePathField, subscriptionKeyField, "patsr_granulartiming", // "patsr_searchlogtiming", "patsr_searchlogsoap", "patsr_unexpectedmessage", "patsr_masksensitiveinfo", // "patsr_retrievesenitivitylevel", "patsr_enableauditing", "patsr_employeeendpoint", "patsr_sensitiveendpoint"); //if (settings != null) //{ // VeisConfig config = new VeisConfig() // { // OrgOverride = (settings.Contains("patsr_veisorganizationname")) ? settings["patsr_veisorganizationname"].ToString() : string.Empty, // GetSensitiveInfo = (settings.Contains("patsr_retrievesenitivitylevel")) ? (Boolean)settings["patsr_retrievesenitivitylevel"] : false, // EnableAuditing = (settings.Contains("patsr_enableauditing")) ? (Boolean)settings["patsr_enableauditing"] : false, // LogTimer = (settings.Contains("patsr_searchlogtiming")) ? (Boolean)settings["patsr_searchlogtiming"] : false, // LogSoap = (settings.Contains("patsr_searchlogsoap")) ? (Boolean)settings["patsr_searchlogsoap"] : false, // EmployeeEndpoint = (settings.Contains("patsr_employeeendpoint")) ? settings.GetAttributeValue<string>("patsr_employeeendpoint") : string.Empty, // SensitiveEndpoint = (settings.Contains("patsr_sensitiveendpoint")) ? settings.GetAttributeValue<string>("patsr_sensitiveendpoint") : string.Empty // }; // config.OrgName = (config.OrgOverride != "" && config.OrgOverride != string.Empty) ? config.OrgOverride : this.OrganizationName; config.VeisConfiguration = new VeisConfiguration() { CRMAuthInfo = new CRMAuthTokenConfiguration() { ClientApplicationId = "58d50ca3-b921-4d9b-ac76-8965be2eb80b", ClientSecret = "4RdVDdhq8cv/h2263Ojdy8fD3JI9OwHcr5hOkntAwTc=", TenantId = "f7c49e36-971b-42c7-b244-a88eed6c0bf6", ResourceId = "51472684-3639-41eb-9d5b-9d8c68f173a5", }, SvcConfigInfo = new VEISSvcLOBConfiguration() { ApimSubscriptionKey = "Ocp-Apim-Subscription-Key|4a61ad0eaea2445fbbad9d81dbbe1d98", } }; config.VeisConfiguration.SvcConfigInfo.SvcBaseUrl = "https://nonprod.integration.d365.va.gov/veis"; config.VeisConfiguration.SvcConfigInfo.SvcLOBServiceUrl = "https://nonprod.integration.d365.va.gov/veis/EC/MVIService/api/"; // //if (settings.Contains("patsr_vastlistrefreshapiurl") && settings.Contains(servicePathField)) // //{ // // if (settings["patsr_vastlistrefreshapiurl"].ToString().EndsWith("/")) // // { // // config.VeisConfiguration.VASTRefreshFacilityAPI = settings[servicePathField].ToString() + settings["patsr_vastlistrefreshapiurl"].ToString(); // // } // // else // // { // // config.VeisConfiguration.VASTRefreshFacilityAPI = settings[servicePathField].ToString() + "/" + settings["patsr_vastlistrefreshapiurl"].ToString(); // // } // //} return(config); //} //return null; }
//public static T ReceiveVeisRequest<T>(ActionContext localContext, VeisConfig config, string messageId) //{ // localContext.TracingService.Trace("Sending Request"); // return ReceiveVeisRequest<T>(config, messageId); //} public static T ReceiveVeisRequest <T>(VeisConfig config, string messageId) { try { if ((System.Net.ServicePointManager.SecurityProtocol & SecurityProtocolType.Tls12) == 0) { System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12; } using (WebClient client = new WebClient()) { string uri = string.Empty; if ((config.VeisConfiguration.SvcConfigInfo.SvcLOBServiceUrl.EndsWith("/") && (!messageId.StartsWith("/"))) || ((!config.VeisConfiguration.SvcConfigInfo.SvcLOBServiceUrl.EndsWith("/")) && (messageId.StartsWith("/")))) { uri = config.VeisConfiguration.SvcConfigInfo.SvcLOBServiceUrl + messageId; } else if (config.VeisConfiguration.SvcConfigInfo.SvcLOBServiceUrl.EndsWith("/") && (messageId.StartsWith("/"))) { uri = config.VeisConfiguration.SvcConfigInfo.SvcLOBServiceUrl + messageId.TrimStart('/'); } else if ((!config.VeisConfiguration.SvcConfigInfo.SvcLOBServiceUrl.EndsWith("/")) && (!messageId.StartsWith("/"))) { uri = config.VeisConfiguration.SvcConfigInfo.SvcLOBServiceUrl + "/" + messageId; } client.AddAuthHeader(config.VeisConfiguration.CRMAuthInfo); Console.WriteLine("Auth Header: " + client.Headers[HttpRequestHeader.Authorization]); client.Headers[HttpRequestHeader.ContentType] = "application/json"; string subKeys = config.VeisConfiguration.SvcConfigInfo.ApimSubscriptionKey; if (subKeys.Length > 0) { string[] headers = subKeys.Split('|'); for (int i = 0; i < headers.Length; i = i + 2) { client.Headers.Add(headers[i], headers[i + 1]); } } string response = client.DownloadString(uri); return(JsonHelper.Deserialize <T>(response)); } } catch (WebException exception) { string callResponse = string.Empty; if (exception.Response != null) { using (StreamReader reader = new StreamReader(exception.Response.GetResponseStream())) { callResponse = reader.ReadToEnd(); } exception.Response.Close(); } if (exception.Status == WebExceptionStatus.Timeout) { throw new Exception("The timeout elapsed while attempting to issue the request.", exception); } throw new Exception($"A Web exception occurred while attempting to issue the request. {exception.Message}: {callResponse}", exception); } }
protected override void Execute(CodeActivityContext context) { using (var localContext = new ActionContext(context)) { // retriev veis config VeisConfig config = localContext.RetrieveVeisConfig("ftp_mviserviceurl", "ftp_mvisubscriptionkey"); var token = AuthExtensions.GetAccessToken(config.VeisConfiguration.CRMAuthInfo); // get token this.Response.Set(context, $"{token.token_type} {token.access_token}"); } }
public static T SendReceiveVeisRequest <T>(IPluginExecutionContext localContext, VeisConfig config, string messageId, VeisRequest request) { //localContext.TracingService.Trace("Sending Request"); return(SendReceiveVeisRequest <T>(config, messageId, request)); }
protected override void Execute(CodeActivityContext context) { //WebClient webClient; //string str; Exception exception; int i; object[] correlationId; ITracingService extension = null; Response.Set(context, ""); // Use a 'using' statement to dispose of the service context properly // To use a specific early bound entity replace the 'Entity' below with the appropriate class type using (var localContext = new ActionContext(context)) { try { extension = context.GetExtension <ITracingService>(); IWorkflowContext workflowContext = context.GetExtension <IWorkflowContext>(); IOrganizationService organizationService = context.GetExtension <IOrganizationServiceFactory>().CreateOrganizationService(new Guid?(localContext.WorkflowExecutionContext.UserId)); OptionSetValue optionSetValue = this.FacilityEndpointToUse.Get(context); FacilityEndpoint facilityEndpoint = (optionSetValue != null ? (FacilityEndpoint)Enum.ToObject(typeof(FacilityEndpoint), optionSetValue.Value) : FacilityEndpoint.VASTProxy); OptionSetValue optionSetValue1 = this.VASTMappingMethodInput.Get(context); ParentFacilityMappingMethod parentFacilityMappingMethod = (optionSetValue1 != null ? (ParentFacilityMappingMethod)Enum.ToObject(typeof(ParentFacilityMappingMethod), optionSetValue1.Value) : ParentFacilityMappingMethod.UseParentStationIDFieldFromVAST); DateTime dateTime = this.FacilityDataDateInput.Get(context); int month = dateTime.Month; string str1 = month.ToString(); month = dateTime.Day; string str2 = month.ToString(); month = dateTime.Year; string str3 = string.Format("{0}-{1}-{2}", str1, str2, month.ToString()); VASTRefreshResponse vASTRefreshResponse = new VASTRefreshResponse(); if (facilityEndpoint != FacilityEndpoint.VASTProxy) { throw new NotImplementedException("Facility Endpoint Not Supported: " + facilityEndpoint.ToString()); } else { VeisConfig config = localContext.RetrieveVeisConfig("ftp_vastlistrefreshapiurl", "ftp_mvisubscriptionkey"); vASTRefreshResponse = WebApiUtility.ReceiveVeisRequest <VASTRefreshResponse>(config, config.VeisConfiguration.VASTRefreshFacilityAPI + "/" + str3); if (vASTRefreshResponse.ErrorOccurred) { throw new InvalidPluginExecutionException((string.IsNullOrEmpty((string)vASTRefreshResponse.ErrorMessage) ? "Unspecified error." : (string)vASTRefreshResponse.ErrorMessage)); } } if ((int)vASTRefreshResponse.Data.Length != 1) { throw new InvalidPluginExecutionException("Unhandled response content: multiple Data[] members."); } DataMember data = vASTRefreshResponse.Data[0]; if ((int)data.vistaList.Length > 0) { VASTStation[] vASTStationArray = data.vistaList; for (i = 0; i < (int)vASTStationArray.Length; i++) { vASTStationArray[i].MassageVASTStation(data.vistaList, parentFacilityMappingMethod); } extension.Trace("staring VISNs...", new object[0]); QueryExpression queryExpression = new QueryExpression(); queryExpression.EntityName = "ftp_visn"; queryExpression.ColumnSet = new ColumnSet(new string[] { "ftp_name" }); queryExpression.Criteria.Conditions.Add(new ConditionExpression("statecode", 0, "Active")); queryExpression.Orders.Add(new OrderExpression("modifiedon", OrderType.Ascending)); EntityCollection entityCollection = organizationService.RetrieveMultiple(queryExpression); extension.Trace("got active ftp_visn records", new object[0]); Dictionary <string, Entity> strs = new Dictionary <string, Entity>(); vASTStationArray = data.vistaList; for (i = 0; i < (int)vASTStationArray.Length; i++) { VASTStation vASTStation = vASTStationArray[i]; if (strs.FirstOrDefault <KeyValuePair <string, Entity> >((KeyValuePair <string, Entity> entry) => entry.Key == vASTStation.VisnID).Key == null) { extension.Trace(string.Concat("VISN from VAST: ", vASTStation.VisnID), new object[0]); Entity entity = entityCollection.Entities.FirstOrDefault <Entity>((Entity v) => (!v.Contains("ftp_name") ? false : (string)v.GetAttributeValue <string>("ftp_name") == vASTStation.VisnID)); if (entity != null) { strs.Add(vASTStation.VisnID, entity); } else { extension.Trace(string.Concat("creating new ftp_visn record: ", vASTStation.VisnID), new object[0]); Entity entity1 = new Entity("ftp_visn"); entity1.Attributes.Add("ftp_name", vASTStation.VisnID); try { entity1.Id = organizationService.Create(entity1); } catch (Exception exception3) { exception = exception3; extension.Trace(string.Concat("error creating new ftp_visn record: ", vASTStation.VisnID), new object[0]); throw new InvalidPluginExecutionException(string.Concat("error creating new ftp_visn record: ", exception.Message)); } strs.Add(vASTStation.VisnID, entity1); } } } extension.Trace("filled VISNDictionary.", new object[0]); extension.Trace("starting top-level facilities (ftp_facility entity)", new object[0]); QueryExpression queryExpression1 = new QueryExpression(); queryExpression1.EntityName = VEISVASTRefreshFacility.CrmEntityName; queryExpression1.ColumnSet = VEISVASTRefreshFacility.CrmColumns; queryExpression1.Criteria.Conditions.Add(new ConditionExpression("statecode", 0, "Active")); queryExpression1.Orders.Add(new OrderExpression(VEISVASTRefreshFacility.StationNumberFieldName, 0)); queryExpression1.Orders.Add(new OrderExpression("modifiedon", OrderType.Ascending)); EntityCollection entityCollection1 = organizationService.RetrieveMultiple(queryExpression1); extension.Trace("got active ftp_facility records.", new object[0]); Dictionary <VASTStation, Entity> vASTStations1 = new Dictionary <VASTStation, Entity>(); vASTStationArray = data.vistaList; for (i = 0; i < (int)vASTStationArray.Length; i++) { VASTStation vASTStation1 = vASTStationArray[i]; if (vASTStation1.IsATopLevelStation) { VEISVASTRefreshFacility facility = new VEISVASTRefreshFacility(vASTStation1); extension.Trace(string.Concat("top-level ftp_facility from VAST: ", facility.CRMDisplayname), new object[0]); Entity entity2 = entityCollection1.Entities.FirstOrDefault <Entity>((Entity f) => (!f.Contains(VEISVASTRefreshFacility.StationIDFieldName) ? false : (string)f.GetAttributeValue <string>(VEISVASTRefreshFacility.StationIDFieldName) == vASTStation1.StationID)); if (entity2 == null) { entity2 = entityCollection1.Entities.FirstOrDefault <Entity>((Entity f) => (!f.Contains(VEISVASTRefreshFacility.StationNumberFieldName) ? false : (string)f.GetAttributeValue <string>(VEISVASTRefreshFacility.StationNumberFieldName) == vASTStation1.StationNumber)); } if (entity2 == null) { extension.Trace(string.Concat("creating new ftp_facility record: ", facility.CRMDisplayname), new object[0]); Entity cRMRecord = facility.MapToCRMRecord(Guid.Empty, strs); try { cRMRecord.Id = organizationService.Create(cRMRecord); vASTStations1.Add(vASTStation1, cRMRecord); } catch (Exception exception4) { exception = exception4; extension.Trace("error creating new ftp_facility record.", new object[0]); throw new InvalidPluginExecutionException(string.Concat("error creating new ftp_facility record: ", exception.Message)); } } else if (facility.MatchesCRMRecord(entity2)) { vASTStations1.Add(vASTStation1, entity2); } else { extension.Trace(string.Concat("updating ftp_facility record: ", facility.CRMDisplayname), new object[0]); Entity cRMRecord1 = facility.MapToCRMRecord(entity2.Id, strs); try { organizationService.Update(cRMRecord1); vASTStations1.Add(vASTStation1, cRMRecord1); } catch (Exception exception5) { exception = exception5; extension.Trace("error updating facility record.", new object[0]); throw new InvalidPluginExecutionException(string.Concat("error updating ftp_facility record: ", exception.Message)); } } } } extension.Trace("finished creating/updating ftp_facility records from vastResponse.Data[0].vistaList.", new object[0]); extension.Trace("filled ParentFacilityDictionary.", new object[0]); extension.Trace("starting full facilities list (ftp_subfacility entity)...", new object[0]); QueryExpression queryExpression2 = new QueryExpression(); queryExpression2.EntityName = VEISVASTRefreshSubFacility.CrmEntityName; queryExpression2.ColumnSet = VEISVASTRefreshSubFacility.CrmColumns; queryExpression2.Criteria.Conditions.Add(new ConditionExpression("statecode", 0, "Active")); queryExpression2.Orders.Add(new OrderExpression(VEISVASTRefreshSubFacility.StationNumberFieldName, 0)); queryExpression2.Orders.Add(new OrderExpression("modifiedon", OrderType.Ascending)); EntityCollection entityCollection2 = organizationService.RetrieveMultiple(queryExpression2); extension.Trace("got active ftp_subfacility records.", new object[0]); vASTStationArray = data.vistaList; for (i = 0; i < (int)vASTStationArray.Length; i++) { VASTStation vASTStation2 = vASTStationArray[i]; VEISVASTRefreshSubFacility subFacility = new VEISVASTRefreshSubFacility(vASTStation2, vASTStations1); extension.Trace(string.Concat("ftp_subfacility from VAST: ", subFacility.CRMDisplayname), new object[0]); Entity entity3 = entityCollection2.Entities.FirstOrDefault <Entity>((Entity sf) => (!sf.Contains(VEISVASTRefreshSubFacility.StationIDFieldName) ? false : (string)sf.GetAttributeValue <string>(VEISVASTRefreshSubFacility.StationIDFieldName) == vASTStation2.StationID)); if (entity3 == null) { entity3 = entityCollection2.Entities.FirstOrDefault <Entity>((Entity sf) => (!sf.Contains(VEISVASTRefreshSubFacility.StationNumberFieldName) ? false : (string)sf.GetAttributeValue <string>(VEISVASTRefreshSubFacility.StationNumberFieldName) == vASTStation2.StationNumber)); } if (entity3 == null) { extension.Trace(string.Concat("creating new ftp_subfacility record: ", subFacility.CRMDisplayname), new object[0]); Entity cRMRecord2 = subFacility.MapToCRMRecord(Guid.Empty, vASTStations1); try { cRMRecord2.Id = organizationService.Create(cRMRecord2); } catch (Exception exception6) { exception = exception6; extension.Trace("error creating new ftp_subfacility record.", new object[0]); throw new InvalidPluginExecutionException(string.Concat("error creating new ftp_subfacility record: ", exception.Message)); } } else if (!subFacility.MatchesCRMRecord(entity3)) { extension.Trace(string.Concat("updating ftp_subfacility record: ", subFacility.CRMDisplayname), new object[0]); try { organizationService.Update(subFacility.MapToCRMRecord(entity3.Id, vASTStations1)); } catch (Exception exception7) { exception = exception7; extension.Trace("error updating ftp_subfacility record.", new object[0]); throw new InvalidPluginExecutionException(string.Concat("error updating ftp_subfacility record: ", exception.Message)); } } } this.RefreshCompletedSuccessfully.Set(context, true); this.ResultMessage.Set(context, "finished creating/updating ftp_subfacility records."); extension.Trace("finished creating/updating ftp_subfacility records.", new object[0]); } } catch (Exception ex) { this.RefreshCompletedSuccessfully.Set(context, false); correlationId = new object[] { ex.ToString() }; extension.Trace("Exception: {0}", correlationId); this.ResultMessage.Set(context, ex.ToString()); throw; } correlationId = new object[] { context.GetExtension <IWorkflowContext>().CorrelationId }; extension.Trace("Exiting RefreshFacilityListFromVAST.Execute(), Correlation Id: {0}", correlationId); } }