Please ensure Javascript is enabled for purposes of website accessibility
Powered by Zoomin Software. For more details please contactZoomin

AVEVA™ Work Tasks

Code Sample - Class Library for Custom Event Provider

Code Sample - Class Library for Custom Event Provider

  • Last UpdatedJun 10, 2024
  • 5 minute read

using System;

using System.Collections.Generic;

using System.Text;

using Skelta.Events.Interfaces;

using Skelta.Core;

using Workflow.NET;

using Skelta.Events;

using Workflow.NET.Engine;

using System.Collections;

namespace SkeltaSampleEventProvider

{

class SampleEventPort : IEventPort

{

string _EventName;

Guid _PortGuid;

string _PortName;

string _PortXmlString;

public SampleEventPort(string eventName)

{

_EventName = eventName;

 }

public SampleEventPort(string eventName, Guid portGuid, string portName, string portXml)

{

_EventName = eventName;

_PortGuid = portGuid;

_PortName = portName;

_PortXmlString = portXml;

}

#region IEventPort Members

/// <summary>

/// Get Event Provider name specific to provider.

/// </summary>

public string EventName

{

get

{

return _EventName;

}

}

/// <summary>

/// Internally used to identify the port

/// </summary>

public Guid PortGuid

{

 get

{

return _PortGuid;

}

}

/// <summary>

/// Internally used to display port names

 /// </summary>

public string PortName

{

get

{

return _PortName;

}

set

{

_PortName = value;

}

}

 

/// <summary>

/// Port level custom logic can be persisted inside this xmlstring.

/// </summary>

public string PortXmlString

{

get

{

return _PortXmlString;

}

set

{

_PortXmlString = value;

}

}

#endregion

}

public class EngineHostedProvider : ISkeltaAddInProvider, IEventClientServiceProvider, IEventServiceProvider

{

#region ISkeltaAddInProvider Members

string _Settings;

Guid _Id;

/// <summary>

/// Return Id

/// </summary>

public Guid Id

{

get

{

return _Id;

}

}

/// <summary>

/// Initialize add in provider properties

/// </summary>

/// <param name="settings"></param>

/// <param name="id"></param>

public void InitializeProvider(string settings, Guid id)

{

_Settings = settings;

_Id = id;

//write code for initializing tasks required for this provider

}

/// <summary>

/// Return addin provider settings

/// </summary>

public string Settings

{

get

{

return _Settings;

}

}

#endregion

#region IEventClientServiceProvider Members

string _EventName = "SkeltaSampleEventProvider";

/// <summary>

/// This method will get called while adding an eventbinding of this provider type.

/// </summary>

/// <param name="eventBinding"></param>

/// <returns></returns>

public bool EventBindingAdding(EventBinding eventBinding)

{

//write code required while adding an event binding

return true;

}

/// <summary>

/// This method will get called while deleting an eventbinding of this provider type.

/// </summary>

/// <param name="eventBinding"></param>

/// <returns></returns>

public bool EventBindingDeleting(EventBinding eventBinding)

{

//write code required while deleting an event binding

return true;

}

/// <summary>

/// This method will get called while updating an eventbinding of this provider type.

/// </summary>

/// <param name="eventBinding"></param>

/// <returns></returns>

public bool EventBindingUpdating(EventBinding eventBinding)

{

//write code required while updating an event binding

return true;

}

/// <summary>

/// Unique Event Provider Name. This name should be added in SKAddInProviders table.

/// </summary>

public string EventName

{

get

{

return _EventName;

}

}

/// <summary>

/// This method gives all correlation parameters used for filtering event bindings when an event occurs.

/// </summary>

/// <param name="port"></param>

/// <returns></returns>

public EventParameter[] GetAllEventParameters(IEventPort port)

{

ArrayList AL = new ArrayList();

EventParameter evntParam = new EventParameter("MandatoryParam1", EventParameterTypes.StringType, "str1");

evntParam.IsMandatory = true;

AL.Add(evntParam);

evntParam = new EventParameter("OptionalParam2", EventParameterTypes.GUIDType, "GUID1");

AL.Add(evntParam);

evntParam.IsMandatory = false;

return (EventParameter[])AL.ToArray(evntParam.GetType());

}

/// <summary>

/// This method will give name for displaying purpose

/// </summary>

/// <param name="eventBinding"></param>

/// <returns></returns>

public string GetEventBindingNameForDisplay(EventBinding eventBinding)

{

return _EventName + "_" + eventBinding.BindingType.ToString() + "_" + eventBinding.str1;

}

/// <summary>

/// Gives new port specific to the provider.

/// </summary>

/// <returns></returns>

public IEventPort GetNewEventPort()

{

return new SampleEventPort(_EventName);

}

/// <summary>

/// Used to render forms for association.

/// </summary>

/// <param name="provisionType"></param>

/// <returns></returns>

public List<EventProvisionFormDetails> GetProvisionForms(EventProvisionFormType provisionType)

{

//It should return list of EventProvisionFormDetails which can be used for rendering Event association page

//This is the page which should appear when we are trying to associate a WF with the Custom Created Event Provider. On the page we should specify the details of the event on which WF has to be triggered.

//Before adding, need to set the following EventProvisionFormDetails properties FormType(Web/Win/Skelta forms engine), FormURI(if web location of aspx file),

List<EventProvisionFormDetails> formDetails = new List<EventProvisionFormDetails>();

EventProvisionFormDetails allEventTypesNewEventForm = new EventProvisionFormDetails();

allEventTypesNewEventForm.FormType = "Web";

//Please click here for details of the aspx page being added

allEventTypesNewEventForm.FormURI = "Plugins/FileWatcher/EventProvider/FileAssociateEC.aspx";


allEventTypesNewEventForm.Name = "Sample Associations";

allEventTypesNewEventForm.ProvisionType = EventProvisionFormType.NewEvent;

formDetails.Add(allEventTypesNewEventForm);

return formDetails;

}

/// <summary>

/// This method will be called from Engine after filtering the eventbindings for specific events.

/// If it returns true then this eventbinding should be considered for executing/alerting workflow.

/// Users business logic can be done here.

/// For Activity type eventbindings, variables can be updated here.

/// </summary>

/// <param name="eventItem"></param>

/// <param name="eventBinding"></param>

/// <param name="varCollection"></param>

/// <returns></returns>

public bool IsEventValidForTheTarget(EventItem eventItem, EventBinding eventBinding, VariablesCollection varCollection)

{

varCollection.Add("MyVariable", "string", "Testing Skelta Event Provider");

return true;

}

/// <summary>

/// Returned icon should be shown in the event association page

/// </summary>

public string ListBigIcon

{

get

{

return "Plugins/FileWatcher/Images/icon-sampleprovider_Big.png";

}

}

/// <summary>

/// Returned icon should be shown in the event association page

/// </summary>

public string ListSmallIcon

{

get

{

return "Plugins/FileWatcher/Images/icon-sampleprovider_small.png";

}

}

/// <summary>

/// Internally used to get a specific event port.

/// </summary>

/// <param name="id"></param>

/// <param name="portName"></param>

/// <param name="portXml"></param>

/// <returns></returns>

public IEventPort LoadEventPort(Guid id, string portName, string portXml)

{

return new SampleEventPort(_EventName, id, portName, portXml);

}

#endregion

#region IEventServiceProvider Members

Dictionary<string, IEventPort> dicListeningPorts;

/// <summary>

/// Get all listening event ports

/// </summary>

/// <returns></returns>

public Dictionary<string, IEventPort> GetListeningEventPorts()

{

return dicListeningPorts;

}

/// <summary>

/// Initialize the provider before starting the provider.

/// </summary>

public void Initialize()

{

dicListeningPorts = new Dictionary<string, IEventPort>();

}

/// <summary>

/// Internally used for identifying global providers

/// </summary>

/// <returns></returns>

public bool IsGlobal()

{

return false;

}

/// <summary>

///

/// </summary>

/// <param name="methodName"></param>

/// <param name="message"></param>

/// <returns></returns>

public object OnClientMessage(string methodName, object message)

{

return null;

//currently not implemented

}

/// <summary>

///

/// </summary>

public void OnStop()

{

//currently not implemented

}

/// <summary>

/// Will be called from Engine for re-starting an eventport

/// </summary>

/// <param name="eventPort"></param>

public void RefreshListeningPort(IEventPort eventPort)

{

//...

}

/// <summary>

/// Will be called from Engine for starting an eventport

/// </summary>

/// <param name="eventPort"></param>

public void StartListeningPort(IEventPort eventPort)

{

//...

dicListeningPorts.Add(eventPort.PortGuid.ToString(), eventPort);

}

/// <summary>

/// Will be called from Engine for stoping an eventport

/// </summary>

/// <param name="eventPort"></param>

public void StopListeningPort(IEventPort eventPort)

{

//...

dicListeningPorts.Remove(eventPort.PortGuid.ToString());

}

#endregion

#region OnEventReceived

/****************************************************************

 * This region of code is completly sample.

 * This is for demonstrating how to react on events when they are received.

 *

*****************************************************************/

// Current port will be known to the provider when event occurs as this port is listening for the event

//so initiation as in the next line is not required.

SampleEventPort currentPort = new SampleEventPort("SkeltaSampleEventProvider");

/// <summary>

/// Sample method for raising skelta client on event receive.

/// Normally there will be some objects as part of the event

/// For example

/// Email received, email is the eventMessage

/// File updated - file  is the eventMessage

/// SMS received - smd is the eventMessage

/// </summary>

/// <param name="Message"></param>

public void EngineHostedEventHappend(string eventMessage)

{

EventParameter[] eventParams = GetAllEventParameters(currentPort);

eventParams[0].Value = GetMandatoryParam1(eventMessage);

eventParams[1].Value = GetOptionalParam2(eventMessage);

EventItem evntItm = new EventItem(this, currentPort, eventParams, eventMessage);

VariablesCollection wfVarCol = new VariablesCollection();

wfVarCol.Add("SampleEventVariable", "string", "TestValue");

Client.RaiseEvent(evntItm, "<Test/>", wfVarCol, "", Workflow.NET.Engine.Interfaces.ActionResult.Sleep);

}

private string GetMandatoryParam1(string EventMessage)

{

//...

return "";

}

private Guid GetOptionalParam2(string EventMessage)

{

//...

return new Guid();

}

#endregion

}


}

TitleResults for “How to create a CRG?”Also Available in