====== Use Case: Write to disk depending on partnervariables ======
In this HowTo, you will learn which conditions are necessary for implementing your own plug-in. Below you can follow the implementation on the basis of a use case, be written in the attachments of a message to the hard disk, if the partner has filed a corresponding variable (with a path).
==== Create the Jobstep class ====
To do this, first create a new project in Visual Studio with the settings:
* **Target framework**: minimum **.NET Framework 4.5** or higher
* **Project reference**: the **eBiss.Api** .dll, which can be found in your ebiss directory.
*
You can find the complete project in your eBiss standard templates subdirectory, at:
\StandardTemplates\PluginSample\eBiss.Plugin.Sample
Let's first create our Jobstep with the name AttachmentWriter. For this, our created class inherits from an abstract class ** ClientJobStepBase ** and implements the Run () method. We also need private methods that read a partnervariable and write the attachment to the hard disk using a stream.
public class AttachmentWriter : ClientJobStepBase
{
#region Internal and private Member
private void WriteToFile(IAttachment attachment, string folder)
{
using (Stream readStream = attachment.OpenReadStream())
{
string outFn = attachment.Filename;
if (String.IsNullOrEmpty(outFn))
{
outFn = attachment.Oid + ".dat";
}
if (folder[folder.Length - 1] != '/' && folder[folder.Length - 1] != '\\')
{
folder += "\\";
}
string fn = folder + outFn;
LogContext.Debug("Write attachment to file: " + fn);
using (Stream writeStream = File.Create(fn))
{
byte[] fileBuff = new byte[1024];
int bytesRead = 0;
while ((bytesRead = readStream.Read(fileBuff, 0, fileBuff.Length)) > 0)
{
writeStream.Write(fileBuff, 0, bytesRead);
}
writeStream.Flush();
writeStream.Close();
}
}
}
private bool CheckVariableForWriteAttachment(IPartner partner)
{
ILocation loc = partner.MainLocation;
IVariableInstance writeAttachment = loc.VariableInstances.Where(v => v.VariableDefinition.Name == "WriteAttachment").FirstOrDefault();
if (writeAttachment != null)
return writeAttachment.Value_Bool;
return false;
}
private string GetPath(IPartner partner)
{
ILocation loc = partner.MainLocation;
IVariableInstance writeAttachmentPath = loc.VariableInstances.Where(v => v.VariableDefinition.Name == "AttachmentPath").FirstOrDefault();
if (writeAttachmentPath != null && string.IsNullOrWhiteSpace(writeAttachmentPath.Value_Text) == false)
{
string outPath = writeAttachmentPath.Value_Text;
if (!Directory.Exists(outPath))
{
throw new ApplicationException($"Path '{outPath}' from variable AttachmentPath for partner '{partner.Name}' is not a valid or existing directory.");
}
return outPath;
}
return null;
}
[...]
The Run method is then implemented and, in our case, logged sufficiently using our LogContext. Effectively iterating through a list of messages and checks whether the current partner has a variable called "Write Attachment". Then the variable "AttachmentPath" validates the corresponding path and writes the attachment to the disk using a stream. In case of error, we throw an exception and assign a task.
public override JobStepResult Run()
{
JobStepResult rc = JobStepResult.Ok;
LogContext.Debug("Enter");
foreach (IMessage msg in Job.InputMessages)
{
try
{
if (CheckVariableForWriteAttachment(msg.Partner) == false)
{
LogContext.Debug($"Variable 'WriteAttachment' not set or false for Partner '{msg.Partner.Name}'.");
}
else
{
string folder = GetPath(msg.Partner);
if (folder != null)
{
foreach (IAttachment attachment in msg.Attachments)
{
WriteToFile(attachment, folder);
}
}
else
{
throw new ApplicationException($"Variable 'AttachmentPath' for Partner '{msg.Partner.Name}' is not set.");
}
}
}
catch (Exception ex)
{
LogContext.Exception(ex);
this.Job.AddTask(msg, ex.Message);
rc = JobStepResult.Error;
}
}
LogContext.Debug("Exit with: " + rc.ToString());
return rc;
}
Create your project and copy the .dll to the eBiss Plugin subdirectory. Once you have restarted the service, you will find your jobsteps in the Workflow Editor.
{{:howtos:plugins:jobsteps:jobexampletwo.png?nolink&500|}}
//Fig. 1: Jobstep in the Workflow-Editor//