Insite WIS realtime user initiated
There are three options for an Insite WIS job. First, is to setup a job that would run a regular schedule interval. The second option is a triggered Insite Console Job Definition. Finally the third option is to trigger the code within the code to allow on demand pull of data based on user initiated actions.
To start off we are going to create a UserManager class. The manager will use the UnitOfWork and IntegrationJobSchedulingServices. These classes will be initiated with Unity.
using InSite.Model;
using InSite.Model;
using InSite.Model.Integration.Interfaces;
using InSite.Model.Integration.Plugins;
using InSite.Model.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
namespace InsiteCommerce.Business.Manager
{
public class UserManager
{
//Name of the Insite Job
private static readonly string USER_REAL_TIME_JOB_NAME = "Customer Update";
//Name of the Insite Job step that contains the primary key
private static readonly string USER_REAL_TIME_JOB_STEP_NAME = "Customer";
public IUnitOfWork UnitOfWork { get; set; }
public IIntegrationJobSchedulingService IntegrationJobSchedulingService { get; set; }
public UserManager(IUnitOfWork unitOfWork, IIntegrationJobSchedulingService integrationJobSchedulingService)
{
this.UnitOfWork = unitOfWork;
this.IntegrationJobSchedulingService = integrationJobSchedulingService;
}
}
}
Next, we are going to specify two string variables. The variable for the user real time job name will be used to specify the name of the job in Insite WIS. The variable for the user real time job step name is the name of the step that contains the Customer table.
//Name of the Insite Job
private static readonly string JOB_NAME = "Customer Update";
//Name of the Insite Job step that contains the primary key
private static readonly string JOB_STEP_NAME = "Customer";
In the below example we are going to create a function that that will trigger the Insite WIS real time. The first part of the function will setup the properties. The second part of function will take properties. Then it will call the Insite WIS job with the created properties.
Below is the basic
public void UpdateCustomerRealtime(Customer customer)
{
}
To setup the parameters we need to look at the Integration Job Definition. To look up the Job Definition we will use the UnitOfWork.GetRepository() function. This will allow the repository request to be cached. We are going to use the user real time job name to return the specific job.
IntegrationJobDefinition updateJob = UnitOfWork.GetRepository().GetByNaturalKey(new object { JOB_NAME });
Next we are going to loop through the Job Definition steps to find the step matching the job step name.
List definitionSteps = updateJob.IntegrationJobDefinitionSteps.Where(p => p.Name == JOB_STEP_NAME).ToList();
var listParams = new System.Collections.ObjectModel.Collection();
With the selected job step matching against the selected job and job step name we set the value to the CustomerNumber. These steps allow us to retrieve only the specific user customer record. This parameter will be saved to the paramsList and be passed to the job when launched.
//Lookup key parameter
IntegrationJobDefinitionStepParameter keyParam = definitionSteps.SelectMany(p => p.IntegrationJobDefinitionStepParameters).Where(p => p.Prompt == JOB_NAME && p.IsValid).FirstOrDefault();
//Save lookup value to paramter
keyParam.Value = customer.CustomerNumber;
With the selected job step matching against the selected job and job step name we set the value to the CustomerNumber. These steps allow us to retrieve only the specific user customer record. This parameter will be saved to the paramsLis.
keyParam.Value = customer.CustomerNumber;
Next we will want to save the job steps to the parameter list. This will be saved to the listParams, then pass the listParams to the job when executed.
foreach(var currentStepParameter in definitionSteps.SelectMany(p => p.IntegrationJobDefinitionStepParameters))
{
listParams.Add(currentStepParameter);
}
The last and final step is to execute the job. The below method will trigger the method and wait until the job is complete before continuing with the code execution. If the code is unable to execute the job from WIS it will take about 5 seconds to timeout. The execution will automatically be triggered when the WIS server is brought online again.
//Execute integration job
IntegrationJobSchedulingService.RunRealTimeIntegrationJob(USER_REAL_TIME_JOB_NAME, null, listParams, null);
After the job has completed successfully the UnitOfWork will need to be refreshed so we have the last customer data.
UnitOfWork.Refresh(customer);
Below is the full working example of the class.
using InSite.Model;
using InSite.Model.Integration.Interfaces;
using InSite.Model.Integration.Plugins;
using InSite.Model.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
namespace InsiteCommerce.Business.Manager
{
public class UserManager
{
//Name of the Insite Job
private static readonly string JOB_NAME = "Customer Update";
//Name of the Insite Job step that contains the primary key
private static readonly string JOB_STEP_NAME = "Customer";
public IUnitOfWork UnitOfWork { get; set; }
public IIntegrationJobSchedulingService IntegrationJobSchedulingService { get; set; }
public UserManager(IUnitOfWork unitOfWork, IIntegrationJobSchedulingService integrationJobSchedulingService)
{
this.UnitOfWork = unitOfWork;
this.IntegrationJobSchedulingService = integrationJobSchedulingService;
}
///
/// Execute the Customer Insite WIS job and refresh the UnitOfWork
///
///
public void UpdateCustomerRealtime(Customer customer)
{
IntegrationJobDefinition updateJob = UnitOfWork.GetRepository().GetByNaturalKey(new object { JOB_NAME });
if (updateJob == null || updateJob.IntegrationJobDefinitionSteps.Count < 1)="" throw="" new="" exception("unabled="" to="" find="" realtime="" job:="" "="" +="" job_name);="" list definitionSteps = updateJob.IntegrationJobDefinitionSteps.Where(p => p.Name == JOB_STEP_NAME).ToList();
var listParams = new System.Collections.ObjectModel.Collection();
foreach (var currentStepParameter in definitionSteps.SelectMany(p => p.IntegrationJobDefinitionStepParameters))
{
listParams.Add(currentStepParameter);
}
//Lookup key parameter
IntegrationJobDefinitionStepParameter keyParam = definitionSteps.SelectMany(p => p.IntegrationJobDefinitionStepParameters).Where(p => p.Prompt == JOB_NAME && p.IsValid).FirstOrDefault();
//Save lookup value to paramter
keyParam.Value = customer.CustomerNumber;
//Execute integration job
IntegrationJobSchedulingService.RunRealTimeIntegrationJob(JOB_NAME, null, listParams, null);
UnitOfWork.Refresh(customer);
}
}
}
After all the above steps have been executed the Insite Commerce WIS job can be called with a trigger action.