-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathContextToSourceFileName.cs
117 lines (94 loc) · 4.43 KB
/
ContextToSourceFileName.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
using Microsoft.BizTalk.Component.Interop;
using Microsoft.BizTalk.Message.Interop;
using System;
using System.ComponentModel;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
namespace INT.BizTalk.PipelineComponents
{
[ComponentCategory(CategoryTypes.CATID_PipelineComponent)]
[ComponentCategory(CategoryTypes.CATID_Any)]
[System.Runtime.InteropServices.Guid("4338FA62-5D37-4F7C-B2FC-89E99CB5B543")]
public class ContextToSourceFileName : IBaseComponent, IPersistPropertyBag, Microsoft.BizTalk.Component.Interop.IComponent
{
private const string BizTalkReceivedFileNameContext = "ReceivedFileName";
private const string BizTalkFilePropertySchema = "http://schemas.microsoft.com/BizTalk/2003/file-properties";
#region BaseComponent Configuration
public string Name => "ContextToSourceFileName";
public string Version => "1.0.0";
public string Description => "Adds a context property to the SourceFileName macro.";
[Description("The target SourceFileName using context macros [%ContextName%]. Example: [%ReceivePortName%]_[%CompanyId%]_othertext")]
public string TargetSourceFileName { get; set; }
[Description("Namespace of context property schemas to use. Can use multiple delimited by |")]
public string ContextPropertySchemas { get; set; }
[Description("Override and replace the current file extension. Leave blank to keep current extension.")]
public string OverrideFileExtension { get; set; }
public void GetClassID(out Guid classID)
{
classID = new Guid("4338FA62-5D37-4F7C-B2FC-89E99CB5B543");
}
public void InitNew()
{
}
public void Load(IPropertyBag propertyBag, int errorLog)
{
object val = ReadPropertyBag(propertyBag, "TargetSourceFileName");
if (val != null) TargetSourceFileName = val as string;
val = ReadPropertyBag(propertyBag, "ContextPropertySchema");
if (val != null) ContextPropertySchemas = val as string;
val = ReadPropertyBag(propertyBag, "OverrideFileExtension");
if (val != null) OverrideFileExtension = val as string;
}
public void Save(IPropertyBag propertyBag, bool clearDirty, bool saveAllProperties)
{
propertyBag.Write("TargetSourceFileName", TargetSourceFileName);
propertyBag.Write("ContextPropertySchema", ContextPropertySchemas);
propertyBag.Write("OverrideFileExtension", OverrideFileExtension);
}
private object ReadPropertyBag(IPropertyBag propertyBag, string propName)
{
object val = null;
try
{
propertyBag.Read(propName, out val, 0);
}
catch (ArgumentException)
{
return val;
}
catch (Exception e)
{
throw new ApplicationException(e.Message, e);
}
return val;
}
#endregion
public IBaseMessage Execute(IPipelineContext pContext, IBaseMessage pInMsg)
{
string receivedFileName = pInMsg.Context.Read(BizTalkReceivedFileNameContext, BizTalkFilePropertySchema) as string;
string pattern = @"\[%([^%]+)%\]";
var values = Regex.Matches(TargetSourceFileName, pattern);
var propertySchemas = ContextPropertySchemas.Split('|');
StringBuilder sourceFileNameBuilder = new StringBuilder(TargetSourceFileName);
foreach (Match match in values)
{
string contextName = match.Groups[1].Value;
string contextValue = "";
foreach (string schema in propertySchemas)
{
contextValue = pInMsg.Context.Read(contextName, schema) as string;
if (!string.IsNullOrWhiteSpace(contextValue))
break;
}
sourceFileNameBuilder.Replace(match.Value, contextValue);
}
if (!string.IsNullOrWhiteSpace(OverrideFileExtension))
sourceFileNameBuilder.Append(OverrideFileExtension);
else
sourceFileNameBuilder.Append(Path.GetExtension(receivedFileName));
pInMsg.Context.Write(BizTalkReceivedFileNameContext, BizTalkFilePropertySchema, sourceFileNameBuilder.ToString());
return pInMsg;
}
}
}