-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRuleProvider.cs
391 lines (319 loc) · 17.3 KB
/
RuleProvider.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Linq.Expressions;
using System.Net.Configuration;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Xml.Schema;
using Newtonsoft.Json.Bson;
using RuleEngine.Core.Domain;
using RuleEngine.Core.Helper;
using RuleEngine.DataObject;
using ServiceStack.Common;
using ServiceStack.Common.Extensions;
using ServiceStack.Logging;
using ServiceStack.Logging.Log4Net;
using ServiceStack.Text;
namespace RuleEngine.Core
{
public class RuleProvider
{
#region "Private Variables"
private static List<CommunicationRules> communicationRules;
private static List<CorporateRules> corporateRules;
private static RuleProvider _instance = null;
private static object _locker = new object();
private static long ThreadSleepTimeInMS = 5 * 60 * 1000;
private static ILogFactory _LogFactory = null;
private static DateTime cutOffDateTime;
#endregion "Private Variables"
private RuleProvider()
{
communicationRules = new List<CommunicationRules>();
corporateRules = new List<CorporateRules>();
}
public static RuleProvider Instance
{
get
{
if (_instance == null)
{
lock (_locker)
{
if (_instance == null)
{
_instance = new RuleProvider();
_LogFactory = new Log4NetFactory(true);
_instance.FetchRuleFromDB();
corporateRules.AddRange(_instance.GetRuleObjectfromJson(communicationRules));
Task.Factory.StartNew(() => _instance.GetUpdatedCorporateRule());
}
}
}
return _instance;
}
}
public void GetUpdatedCorporateRule()
{
while (true)
{
try
{
FetchRuleFromDB();
cutOffDateTime = System.DateTime.Now;
corporateRules = GetRuleObjectfromJson(communicationRules);
if (_LogFactory != null)
_LogFactory.GetLogger("Global").Info("RULE_ENGINE: Get the updated rules and assign to Rule Engine");
}
catch (Exception ex)
{
if (_LogFactory != null)
_LogFactory.GetLogger("Global").Error("RULE_ENGINE: Failed to get updated Rules due to exception", ex);
}
finally
{
try
{
Thread.Sleep(TimeSpan.FromMilliseconds(ThreadSleepTimeInMS));
}
catch { }
}
}
}
//fetch the rules form db
//get the ruleSet of CorpId or SchemeID
//Run the filter and return bool
//if filter return true get the action and update the final actionObject value if action value !=null
public RuleAction Execute(Request request)
{
RuleAction resultRuleAction = new RuleAction();
try
{
var corporateStateRules = GetRuleSetsByCorpSchemaIdAndToState(request);
//if no rule than get default rule Set for corporate
//Default CorporateID = 0
if (corporateStateRules == null)
{
request.CorpId = 0;
corporateStateRules = GetRuleSetsByCorpSchemaIdAndToState(request);
}
if (corporateStateRules != null)
foreach (var rules in corporateStateRules)
{
if (rules.filter != null && rules.filter.ruleExpression != null && rules.filter.ruleExpression.Count > 0)
{
var CompiledRule = PrecompiledRules.CompileRule(request, rules.filter.ruleExpression);
//it will evaluate the list of RuleExpression as And condition
if (!CompiledRule.Any(x=>!x.Invoke(request)))
{
if (_LogFactory != null)
_LogFactory.GetLogger("Global")
.Info("RULE_ENGINE: Validate the Rule as True for " + rules.SerializeToString());
ComposeFinalAction(rules.action, resultRuleAction);
}
}
}
ReplaceCustomerHRTagsByValue(request,resultRuleAction);
}
catch (Exception ex)
{
if (_LogFactory != null)
_LogFactory.GetLogger("Global").Error("RULE_ENGINE: Failed to Execute Rule Engine due to exception", ex);
}
return resultRuleAction;
}
public void ReplaceCustomerHRTagsByValue(Request request,RuleAction ruleAction)
{
//Replace customer tag by value
if (ruleAction.toEmailHashSet != null) ruleAction.toEmailHashSet.ReplaceValue(request.CustomerEmail, "customertag");
if (ruleAction.ccEmailHashSet != null) ruleAction.ccEmailHashSet.ReplaceValue(request.CustomerEmail, "customertag");
if (ruleAction.bccEmailHashSet != null) ruleAction.bccEmailHashSet.ReplaceValue(request.CustomerEmail, "customertag");
if (ruleAction.contactNoHashSet != null) ruleAction.contactNoHashSet.ReplaceValue(request.CustomerContact, "customertag");
//replace HR tag by value
if (ruleAction.toEmailHashSet != null) ruleAction.toEmailHashSet.ReplaceValue(request.HREmail, "hrtag");
if (ruleAction.ccEmailHashSet != null) ruleAction.ccEmailHashSet.ReplaceValue(request.HREmail, "hrtag");
if (ruleAction.bccEmailHashSet != null) ruleAction.bccEmailHashSet.ReplaceValue(request.HREmail, "hrtag");
if (ruleAction.contactNoHashSet != null) ruleAction.contactNoHashSet.ReplaceValue(request.HRContact, "hrtag");
//replace Hospital tag by value
if (ruleAction.toEmailHashSet != null) ruleAction.toEmailHashSet.ReplaceValue(request.HospitalEmail, "hospitaltag");
if (ruleAction.ccEmailHashSet != null) ruleAction.ccEmailHashSet.ReplaceValue(request.HospitalEmail, "hospitaltag");
if (ruleAction.bccEmailHashSet != null) ruleAction.bccEmailHashSet.ReplaceValue(request.HospitalEmail, "hospitaltag");
if (ruleAction.contactNoHashSet != null) ruleAction.contactNoHashSet.ReplaceValue(request.HRContact, "hospitaltag");
//replace PolicyHolder tag by value
if (ruleAction.toEmailHashSet != null) ruleAction.toEmailHashSet.ReplaceValue(request.PolicyHolderEmail, "policyholdertag");
if (ruleAction.ccEmailHashSet != null) ruleAction.ccEmailHashSet.ReplaceValue(request.PolicyHolderEmail, "policyholdertag");
if (ruleAction.bccEmailHashSet != null) ruleAction.bccEmailHashSet.ReplaceValue(request.PolicyHolderEmail, "policyholdertag");
if (ruleAction.contactNoHashSet != null) ruleAction.contactNoHashSet.ReplaceValue(request.PolicyHolderContact, "policyholdertag");
}
public void ComposeFinalAction(RuleAction ruleAction, RuleAction resultRuleAction)
{
if (ruleAction.sendEmail)
{
resultRuleAction.sendEmail = true;
resultRuleAction.toEmailHashSet = resultRuleAction.toEmailHashSet.AddHashSet(ruleAction.toEmailHashSet);
resultRuleAction.ccEmailHashSet = resultRuleAction.ccEmailHashSet.AddHashSet(ruleAction.ccEmailHashSet);
resultRuleAction.bccEmailHashSet = resultRuleAction.bccEmailHashSet.AddHashSet(ruleAction.bccEmailHashSet);
resultRuleAction.attachmentHashSet = resultRuleAction.attachmentHashSet.AddHashSet(ruleAction.attachmentHashSet);
resultRuleAction.emailTemplateId = ruleAction.emailTemplateId;
resultRuleAction.letterId = ruleAction.letterId;
}
else
{
//except email mention in sendEmail = false
resultRuleAction.toEmailHashSet.ExceptWithNullCheck(ruleAction.toEmailHashSet);
resultRuleAction.ccEmailHashSet.ExceptWithNullCheck(ruleAction.ccEmailHashSet);
resultRuleAction.bccEmailHashSet.ExceptWithNullCheck(ruleAction.bccEmailHashSet);
resultRuleAction.attachmentHashSet.ExceptWithNullCheck(ruleAction.attachmentHashSet);
}
if (ruleAction.sendSMS)
{
resultRuleAction.sendSMS = true;
resultRuleAction.contactNoHashSet = resultRuleAction.contactNoHashSet.AddHashSet(ruleAction.contactNoHashSet);
resultRuleAction.smsTemplateId = ruleAction.smsTemplateId;
}
else
{
if (resultRuleAction.contactNoHashSet != null && ruleAction.contactNoHashSet != null)
resultRuleAction.contactNoHashSet.ExceptWith(ruleAction.contactNoHashSet);
}
}
public List<RuleSet> GetRuleSetsByCorpSchemaIdAndToState(Request request)
{
List<RuleSet> corporatStateRuleSet = null;
CorporateRules corporateRule = null;
//getting the rule for Corporate or Scheme
if (corporateRules != null && corporateRules.Count>0)
{
corporateRule = new CorporateRules();
corporateRule = corporateRules.Where(x => x.corpId == request.CorpId && x.schemeId == request.SchemeId).FirstOrDefault();
}
//get the rules of corporate for the specific ToState of Claim
if (corporateRule != null && corporateRule.ruleSets != null && corporateRule.ruleSets.Count > 0)
{
corporatStateRuleSet = new List<RuleSet>();
corporatStateRuleSet = corporateRule.ruleSets.Where(corpRule => corpRule.filter.toState == request.ToState).ToList();
return corporatStateRuleSet;
}
return corporatStateRuleSet;
}
public void FetchRuleFromDB()
{
if (_LogFactory != null)
_LogFactory.GetLogger("Global").Info("RULE_ENGINE: Call DB to get the Rules ");
cutOffDateTime = System.DateTime.Now; //set cutOff date time after get the latest rules from DB
//if CommunicationRules list is null than fetch all else fetch only those which are updated recently
if (communicationRules.Count == 0)
communicationRules = DBUtility.GetRules();
else
{
var ListOfRuleModifiedAfterCutOff = DBUtility.GetRules(cutOffDateTime);
//update only those which are modified after Last CutOffTime
communicationRules.ForEach(
r =>
r.Rules =
ListOfRuleModifiedAfterCutOff.Any(
dbRule => dbRule.CommunicationRuleId == r.CommunicationRuleId)?ListOfRuleModifiedAfterCutOff.First(dbRule => dbRule.CommunicationRuleId == r.CommunicationRuleId).Rules : null);
}
if (_LogFactory != null)
_LogFactory.GetLogger("Global").Info("RULE_ENGINE: Successfully Get Rules from DB ");
}
#region Utility
public string RemoveDuplicate(string inputCSV)
{
string distinctResponseCSV;
if (!string.IsNullOrEmpty(inputCSV))
distinctResponseCSV = string.Join(",",
inputCSV.Split(new[] { "," }, StringSplitOptions.RemoveEmptyEntries).Distinct());
else
distinctResponseCSV = inputCSV;
return distinctResponseCSV;
}
public string GetCSVAppend(string inputCSV, string appendTo)
{
string responseCSV = appendTo;
if (!string.IsNullOrEmpty(inputCSV) && !string.IsNullOrEmpty(responseCSV))
responseCSV = responseCSV + "," + inputCSV;
else
responseCSV = inputCSV;
return responseCSV;
}
public string RemoveFromCSV(string rejectCSV, string removefromCSV)
{
string responseCSV = removefromCSV;
if (!string.IsNullOrEmpty(removefromCSV) && !string.IsNullOrEmpty(rejectCSV))
{
var rejectList = rejectCSV.Split(new[] { "," }, StringSplitOptions.RemoveEmptyEntries);
responseCSV = string.Join(",", removefromCSV.Split(new[] { "," }, StringSplitOptions.RemoveEmptyEntries).Except(rejectList));
}
return responseCSV;
}
public List<CorporateRules> GetRuleObjectfromJson(List<CommunicationRules> communicationRules)
{
List<CorporateRules> objList = new List<CorporateRules>();
if (communicationRules != null && communicationRules.Count > 0)
{
foreach (var commRule in communicationRules)
{
objList.Add(GetRuleObjectfromJson(commRule.Rules));
}
}
return objList;
}
public CorporateRules GetRuleObjectfromJson(string jsonRule)
{
CorporateRules obj = new CorporateRules();
try
{
obj = (CorporateRules)Newtonsoft.Json.JsonConvert.DeserializeObject(jsonRule, typeof(CorporateRules));
if (_LogFactory != null)
_LogFactory.GetLogger("Global").Info("RULE_ENGINE: Converted Json to Object");
}
catch (Exception ex)
{
if (_LogFactory != null)
_LogFactory.GetLogger("Global").Error("RULE_ENGINE: Failed to convert Json Rule to CorporateRules Object", ex);
}
return obj;
}
#endregion
//public string MockCorporateRules()
//{
// return "{ \"rules\": [{ \"corpId\": 1, \"schemeId\": null, \"ruleSets\": [{ \"filter\": { \"fromState\": 0, \"toState\": 1, \"ruleExpression\": { \"propertyName\": \"Amount\", \"operation\": \"GreaterThan\", \"value\": \"1000\" } }, \"action\": { \"sendEmail\": true, \"toEmailcsv\": \"abc@tcs.com\", \"ccEmailcsv\": \"cc@abc.com\", \"bccEmailcsv\": null, \"EmailTemplateId\": 1, \"sendSMS\": false, \"contactNocsv\": null, \"SmsTemplateId\": 0 } }, { \"filter\": { \"fromState\": 0, \"toState\": 1, \"ruleExpression\": { \"propertyName\": \"Amount \", \"operation\": \"GreaterThan\", \"value\": \"800 \" } }, \"action\": { \"sendEmail\": true, \"toEmailcsv\": \"800@abc.com,abc@tcs.com\", \"ccEmailcsv\": null, \"bccEmailHashSet\": null, \"EmailTemplateId\": 1, \"sendSMS\": true, \"contactNoHashSet\": \"9999999999\", \"SmsTemplateId\": 1 } }, { \"filter\": { \"fromState\": 0, \"toState\": 1, \"ruleExpression\": { \"propertyName\": \"Amount \", \"operation\": \"Equal\", \"value\": \"11000 \" } }, \"action\": { \"sendEmail\": false, \"toEmailHashSet\": \"abc@tcs.com\", \"ccEmailHashSet\": null, \"bccEmailHashSet\": null, \"EmailTemplateId\": 1, \"sendSMS\": true, \"contactNoHashSet\": \"9999999999\", \"SmsTemplateId\": 1 } }] }, { \"corpId\": 0, \"schemeId\": null, \"ruleSets\": [{ \"filter\": { \"fromState\": 0, \"toState\": 1, \"ruleExpression\": { \"propertyName\": \"Amount\", \"operation\": \"GreaterThan\", \"value\": \"1000\" } }, \"action\": { \"sendEmail\": true, \"toEmailHashSet\": \"abc@tcs.com\", \"ccEmailcsv\": null, \"bccEmailcsv\": null, \"EmailTemplateId\": 1, \"sendSMS\": false, \"contactNocsv\": null, \"SmsTemplateId\": 0 } }, { \"filter\": { \"fromState\": 0, \"toState\": 0, \"ruleExpression\": { \"propertyName\": \"Amount\", \"operation\": \"GreaterThan\", \"value\": \"1000\" } }, \"action\": { \"sendEmail\": false, \"toEmailcsv\": null, \"ccEmailcsv\": null, \"bccEmailcsv\": null, \"EmailTemplateId\": 0, \"sendSMS\": false, \"contactNocsv\": null, \"SmsTemplateId\": 0 } }] }] }";
//}
}
public static class CustomExtension
{
public static HashSet<T> AddHashSet<T>(this HashSet<T> addToSet, HashSet<T> valueset)
{
if (addToSet != null)
{
if(valueset != null)
addToSet.UnionWith(valueset);
}
else
addToSet = valueset;
return addToSet;
}
public static HashSet<T> ExceptWithNullCheck<T>(this HashSet<T> sourceHashSet, HashSet<T> excepSet)
{
if (sourceHashSet != null && excepSet != null)
sourceHashSet.ExceptWith(excepSet);
return sourceHashSet;
}
public static HashSet<T> ReplaceValue<T>(this HashSet<T> sourceHashSet, T newValue, T tag)
{
if (sourceHashSet != null)
{
if (sourceHashSet.Contains(tag))
{
sourceHashSet.Add(newValue);
sourceHashSet.Remove(tag);
}
}
return sourceHashSet;
}
}
}