PageRenderTime 58ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/MITD.PMS.Test/CalculationsTest.cs

https://github.com/ehsmohammadi/HPMS
C# | 982 lines | 835 code | 117 blank | 30 comment | 29 complexity | bb3ee9a098dcf1edda7531de57847cff MD5 | raw file
  1. using System;
  2. using MITD.PMS.Application;
  3. using MITD.PMS.Domain.Model.JobIndices;
  4. using MITD.PMS.Domain.Model.Jobs;
  5. using Microsoft.VisualStudio.TestTools.UnitTesting;
  6. using MITD.PMS.Domain.Model.Calculations;
  7. using MITD.PMS.Domain.Model.Periods;
  8. using MITD.PMS.Domain.Model.Policies;
  9. using MITD.PMS.Domain.Service;
  10. using MITD.Data.NH;
  11. using MITD.PMS.Persistence.NH;
  12. using MITD.Core.RuleEngine.NH;
  13. using MITD.PMS.Domain.Model.Employees;
  14. using System.Collections.Generic;
  15. using MITD.Core.RuleEngine;
  16. using MITD.Core;
  17. using Castle.Windsor;
  18. using MITD.Domain.Repository;
  19. using MITD.DataAccess.Config;
  20. using MITD.Core.Config;
  21. using MITD.Core.RuleEngine.Model;
  22. using MITD.PMSAdmin.Persistence.NH;
  23. using System.Transactions;
  24. using System.Threading.Tasks;
  25. using System.Threading;
  26. using MITD.PMSAdmin.Domain.Model.CustomFieldTypes;
  27. using NHibernate.Linq;
  28. using NHibernate;
  29. using System.Linq;
  30. using MITD.PMS.Domain.Model.JobIndexPoints;
  31. using System.Data.SqlClient;
  32. using System.Configuration;
  33. namespace MITD.PMS.Test
  34. {
  35. [TestClass]
  36. public class CalculationsTest
  37. {
  38. EventPublisher publisher = new EventPublisher();
  39. [TestMethod]
  40. public void RuleTest()
  41. {
  42. using (var scope = new TransactionScope())
  43. {
  44. using (var con = new SqlConnection(ConfigurationManager.ConnectionStrings["PMSDBConnection"].ConnectionString))
  45. {
  46. var uows = new MITD.Domain.Repository.UnitOfWorkScope(
  47. new Data.NH.NHUnitOfWorkFactory(() => PMSAdmin.Persistence.NH.PMSAdminSession.GetSession(con)));
  48. using (var uow = new NHUnitOfWork(PMSSession.GetSession(con)))
  49. using (var uow2 = uows.CurrentUnitOfWork)
  50. {
  51. con.Open();
  52. var pmsAdminService = new PMS.ACL.PMSAdmin.PMSAdminService(
  53. new PMSAdmin.Application.UnitService(new PMSAdmin.Persistence.NH.UnitRepository(uows),
  54. new PMSAdmin.Persistence.NH.CustomFieldRepository(uows)),
  55. new PMSAdmin.Application.JobService(new PMSAdmin.Persistence.NH.JobRepository(uows),
  56. new PMSAdmin.Persistence.NH.CustomFieldRepository(uows)),
  57. new PMSAdmin.Application.CustomFieldService(new PMSAdmin.Persistence.NH.CustomFieldRepository(uows)),
  58. new PMSAdmin.Application.JobPositionService(new PMSAdmin.Persistence.NH.JobPositionRepository(uows)),
  59. new PMSAdmin.Application.JobIndexService(new PMSAdmin.Persistence.NH.JobIndexRepository(uows),
  60. new PMSAdmin.Persistence.NH.CustomFieldRepository(uows)),
  61. new PMSAdmin.Application.UnitIndexService(new PMSAdmin.Persistence.NH.UnitIndexRepository(uows),
  62. new PMSAdmin.Persistence.NH.CustomFieldRepository(uows))
  63. );
  64. EventPublisher publisher = new EventPublisher();
  65. var rep = new PMS.Persistence.NH.EmployeeRepository(uow);
  66. var periodRep = new PMS.Persistence.NH.PeriodRepository(uow);
  67. var calcRep = new PMS.Persistence.NH.CalculationRepository(uow);
  68. var policyRep = new MITD.PMS.Persistence.NH.PolicyRepository(uow, new PolicyConfigurator(
  69. new RuleBasedPolicyEngineService(new LocatorProvider("PMSDbConnection"), publisher)));
  70. var provider = new PMS.Application.CalculationDataProvider(rep, pmsAdminService,
  71. new PMS.Persistence.NH.JobIndexPointRepository(uow));
  72. var policy = policyRep.GetById(new PolicyId(1));
  73. var period = periodRep.GetBy(c => c.Active);
  74. var emp = rep.GetBy(new EmployeeId("150554", period.Id));
  75. // if period has no calculation
  76. var calculation = new Calculation(calcRep.GetNextId(), period, policy, Guid.NewGuid().ToString(), DateTime.Now, "150554");
  77. calcRep.Add(calculation);
  78. uow.Commit();
  79. // if period has calculation , get it by its identifier
  80. //var calculation = calcRep.GetById(new CalculationId(1));
  81. MITD.PMSReport.Domain.Model.CalculationData empData;
  82. var pathNo = 1;
  83. List<SummaryCalculationPoint> calcList = new List<SummaryCalculationPoint>();
  84. var session = new CalculatorSession();
  85. while (pathNo <= 2)
  86. {
  87. Utils.Res = new MITD.PMS.RuleContracts.RuleResult();
  88. session.AddCalculationPoints(calcList);
  89. session.PathNo = pathNo;
  90. var data = provider.Provide(emp, out empData, calculation, true, session);
  91. var rule1 = new Rule10();
  92. rule1.Execute(data);
  93. var rule2 = new Rule11();
  94. rule2.Execute(data);
  95. var rule3 = new Rule12();
  96. rule3.Execute(data);
  97. //var rule4 = new Rule13();
  98. //rule4.Execute(data);
  99. var res = provider.Convert(Utils.Res, empData, emp, period, calculation);
  100. calcList = res.CalculationPoints.OfType<SummaryCalculationPoint>().ToList();
  101. var jipRep = new JobIndexPointRepository(uow);
  102. if (res.EmployeePointsForAdd != null)
  103. {
  104. foreach (var point in res.EmployeePointsForAdd)
  105. {
  106. jipRep.Add(point);
  107. }
  108. }
  109. if (res.EmployeePointsForUpdate != null)
  110. {
  111. foreach (var point in res.EmployeePointsForUpdate)
  112. {
  113. var employeePoint = jipRep.GetById(point.Key);
  114. employeePoint.SetValue(point.Value);
  115. }
  116. }
  117. uow.Commit();
  118. pathNo++;
  119. }
  120. }
  121. }
  122. }
  123. }
  124. [TestMethod]
  125. public void EmployeeProvideDataTest()
  126. {
  127. var uows = new MITD.Domain.Repository.UnitOfWorkScope(
  128. new Data.NH.NHUnitOfWorkFactory(() => PMSAdmin.Persistence.NH.PMSAdminSession.GetSession()));
  129. using (var uow = new NHUnitOfWork(PMSSession.GetSession()))
  130. using (var uow2 = uows.CurrentUnitOfWork)
  131. {
  132. var pmsAdminService = new PMS.ACL.PMSAdmin.PMSAdminService(
  133. new PMSAdmin.Application.UnitService(new PMSAdmin.Persistence.NH.UnitRepository(uows), new PMSAdmin.Persistence.NH.CustomFieldRepository(uows)),
  134. new PMSAdmin.Application.JobService(new PMSAdmin.Persistence.NH.JobRepository(uows),
  135. new PMSAdmin.Persistence.NH.CustomFieldRepository(uows)),
  136. new PMSAdmin.Application.CustomFieldService(new PMSAdmin.Persistence.NH.CustomFieldRepository(uows)),
  137. new PMSAdmin.Application.JobPositionService(new PMSAdmin.Persistence.NH.JobPositionRepository(uows)),
  138. new PMSAdmin.Application.JobIndexService(new PMSAdmin.Persistence.NH.JobIndexRepository(uows),
  139. new PMSAdmin.Persistence.NH.CustomFieldRepository(uows))
  140. ,
  141. new PMSAdmin.Application.UnitIndexService(new PMSAdmin.Persistence.NH.UnitIndexRepository(uows),
  142. new PMSAdmin.Persistence.NH.CustomFieldRepository(uows)
  143. ));
  144. var rep = new PMS.Persistence.NH.EmployeeRepository(uow);
  145. var provider = new PMS.Application.CalculationDataProvider(rep, pmsAdminService,
  146. new PMS.Persistence.NH.JobIndexPointRepository(uow));
  147. var emp = rep.First();
  148. MITD.PMSReport.Domain.Model.CalculationData empData;
  149. //var data = provider.Provide(emp, out empData);
  150. }
  151. }
  152. [TestMethod]
  153. public void EmployeeDataTest()
  154. {
  155. List<PMSAdmin.Domain.Model.CustomFieldTypes.CustomFieldType> employeeCftList = new List<CustomFieldType>();
  156. List<PMSAdmin.Domain.Model.CustomFieldTypes.CustomFieldType> jobIndexCftList = new List<CustomFieldType>();
  157. List<PMSAdmin.Domain.Model.CustomFieldTypes.CustomFieldType> jobCftList = new List<CustomFieldType>();
  158. List<PMSAdmin.Domain.Model.Jobs.Job> jobList = new List<PMSAdmin.Domain.Model.Jobs.Job>();
  159. List<PMSAdmin.Domain.Model.JobIndices.JobIndex> jobIndexList = new List<PMSAdmin.Domain.Model.JobIndices.JobIndex>();
  160. List<PMSAdmin.Domain.Model.JobPositions.JobPosition> jobPositionList = new List<PMSAdmin.Domain.Model.JobPositions.JobPosition>();
  161. List<PMSAdmin.Domain.Model.Units.Unit> unitList = new List<PMSAdmin.Domain.Model.Units.Unit>();
  162. PMSAdmin.Domain.Model.Policies.RuleEngineBasedPolicy policy;
  163. Core.RuleEngine.Model.Rule rule;
  164. Core.RuleEngine.Model.RuleFunction rf;
  165. Period period;
  166. List<PMS.Domain.Model.Jobs.Job> jobInPeriodList = new List<PMS.Domain.Model.Jobs.Job>();
  167. List<PMS.Domain.Model.JobIndices.JobIndex> jobIndexInPeriodList = new List<PMS.Domain.Model.JobIndices.JobIndex>();
  168. List<PMS.Domain.Model.JobPositions.JobPosition> jobPositionInPeriodList = new List<PMS.Domain.Model.JobPositions.JobPosition>();
  169. List<PMS.Domain.Model.Units.Unit> unitInPeriodList = new List<PMS.Domain.Model.Units.Unit>();
  170. List<PMS.Domain.Model.Employees.Employee> empList = new List<PMS.Domain.Model.Employees.Employee>();
  171. #region rule Engine
  172. var uows = new MITD.Domain.Repository.UnitOfWorkScope(
  173. new Data.NH.NHUnitOfWorkFactory(() =>
  174. {
  175. RuleEngineSession.sessionName = "PMSDBConnection";
  176. return Core.RuleEngine.NH.RuleEngineSession.GetSession();
  177. }));
  178. using (var uow = uows.CurrentUnitOfWork as NHUnitOfWork)
  179. {
  180. var recRep = new Core.RuleEngine.NH.REConfigeRepository(uow);
  181. var rec = new Core.RuleEngine.Model.RuleEngineConfigurationItem(
  182. new Core.RuleEngine.Model.RuleEngineConfigurationItemId("RuleTextTemplate"),
  183. @"
  184. public class <#classname#> : IRule<CalculationData>
  185. {
  186. public void Execute(CalculationData data)
  187. {
  188. <#ruletext#>
  189. }
  190. }");
  191. recRep.Add(rec);
  192. rec = new Core.RuleEngine.Model.RuleEngineConfigurationItem(
  193. new Core.RuleEngine.Model.RuleEngineConfigurationItemId("ReferencedAssemblies"),
  194. @"System.Core.dll;MITD.Core.RuleEngine.dll;MITD.PMS.RuleContracts.dll");
  195. recRep.Add(rec);
  196. rec = new Core.RuleEngine.Model.RuleEngineConfigurationItem(
  197. new Core.RuleEngine.Model.RuleEngineConfigurationItemId("LibraryTextTemplate"),
  198. @"
  199. using System;
  200. using System.Collections.Generic;
  201. using MITD.Core;
  202. using MITD.Core.RuleEngine;
  203. using MITD.PMS.RuleContracts;
  204. using System.Linq;
  205. using System.Globalization;
  206. namespace MITD.Core.RuleEngine
  207. {
  208. public static class Utils
  209. {
  210. public static RuleResult Res = new RuleResult();
  211. <#functions#>
  212. }
  213. public class RuleResultHelper : IRuleResult<RuleResult>
  214. {
  215. public RuleResult GetResult()
  216. {
  217. return Utils.Res;
  218. }
  219. public void Clear()
  220. {
  221. Utils.Res = new RuleResult();
  222. }
  223. }
  224. <#rules#>
  225. }");
  226. recRep.Add(rec);
  227. var rfRep = new Core.RuleEngine.NH.RuleFunctionRepository(uow);
  228. rf = new RuleFunction(rfRep.GetNextId(), "توابع خطکش پورتر",
  229. @"
  230. public static int IndexCount(JobPosition job, string indexCFName, string indexCFValue, string group)
  231. {
  232. return job.Indices.Count(j => j.Key.CustomFields.Any(k => k.Key == indexCFName && k.Value == indexCFValue) && j.Key.Group.DictionaryName == group);
  233. }
  234. public static string IndexGroup(KeyValuePair<JobIndex, Dictionary<Employee, Inquiry>> index)
  235. {
  236. return index.Key.Group.DictionaryName;
  237. }
  238. public static string IndexField(KeyValuePair<JobIndex, Dictionary<Employee, Inquiry>> index, string fieldName)
  239. {
  240. return index.Key.CustomFields[fieldName];
  241. }
  242. public static RulePoint AddPoint(JobPosition job, KeyValuePair<JobIndex, Dictionary<Employee, Inquiry>> index,
  243. string name, decimal value, bool final = false)
  244. {
  245. var res = new RulePoint { Name = name, Value = value, Final = final };
  246. if (!Utils.Res.JobResults.Any(j => j.Key == job.DictionaryName))
  247. Utils.Res.JobResults.Add(job.DictionaryName, new JobPositionResult());
  248. if (!Utils.Res.JobResults[job.DictionaryName].IndexResults.Any(j => j.Key == index.Key.DictionaryName))
  249. Utils.Res.JobResults[job.DictionaryName].IndexResults.Add(index.Key.DictionaryName, new List<RulePoint>());
  250. Utils.Res.JobResults[job.DictionaryName].IndexResults[index.Key.DictionaryName].Add(res);
  251. return res;
  252. }
  253. public static RulePoint AddPoint(JobPosition job, string name, decimal value, bool final = false)
  254. {
  255. var res = new RulePoint { Name = name, Value = value, Final = final };
  256. if (!Utils.Res.JobResults.Any(j => j.Key == job.DictionaryName))
  257. Utils.Res.JobResults.Add(job.DictionaryName, new JobPositionResult());
  258. Utils.Res.JobResults[job.DictionaryName].Results.Add(res);
  259. return res;
  260. }
  261. public static RulePoint AddPoint(string name, decimal value, bool final = false)
  262. {
  263. var res = new RulePoint { Name = name, Value = value, Final = final };
  264. Utils.Res.Results.Add(res);
  265. return res;
  266. }
  267. public static RulePoint GetPoint(JobPosition job, KeyValuePair<JobIndex, Dictionary<Employee, Inquiry>> index, string name)
  268. {
  269. return Utils.Res.JobResults[job.DictionaryName].IndexResults[index.Key.DictionaryName].Single(j=>j.Name==name);
  270. }
  271. "
  272. );
  273. rfRep.Add(rf);
  274. var ruleRep = new RuleRepository(uow);
  275. rule = new Rule(new RuleId(ruleRep.GetNextId()), "محاسبه امتیاز شاخص ها",
  276. @"
  277. //محاسبه تعداد شاخص ها با اهمیت های مختلف
  278. decimal total = 0;
  279. int it = 0;
  280. foreach(var job in data.JobPositions)
  281. {
  282. decimal a1 = Utils.IndexCount(job, ""Importance"", ""1"", ""General"");
  283. decimal b1 = Utils.IndexCount(job, ""Importance"", ""3"", ""General"");
  284. decimal c1 = Utils.IndexCount(job, ""Importance"", ""5"", ""General"");
  285. decimal d1 = Utils.IndexCount(job, ""Importance"", ""7"", ""General"");
  286. decimal e1 = Utils.IndexCount(job, ""Importance"", ""9"", ""General"");
  287. //محاسبه عدد وزنی شاخص های عمومی
  288. decimal y1 = 0;
  289. decimal n = (9 * a1 + 7 * b1 + 5 * c1 + 3 * d1 + e1);
  290. if (n != 0)
  291. y1 = 20 / n;
  292. a1 = 9 * y1;
  293. b1 = 7 * y1;
  294. c1 = 5 * y1;
  295. d1 = 3 * y1;
  296. e1 = y1;
  297. decimal a2 = Utils.IndexCount(job, ""Importance"", ""1"", ""Technical"");
  298. decimal b2 = Utils.IndexCount(job, ""Importance"", ""3"", ""Technical"");
  299. decimal c2 = Utils.IndexCount(job, ""Importance"", ""5"", ""Technical"");
  300. decimal d2 = Utils.IndexCount(job, ""Importance"", ""7"", ""Technical"");
  301. decimal e2 = Utils.IndexCount(job, ""Importance"", ""9"", ""Technical"");
  302. //محاسبه عدد وزنی شاخص های تخصصی
  303. decimal y2 = 0;
  304. decimal m = (9 * a2 + 7 * b2 + 5 * c2 + 3 * d2 + e2);
  305. if (m != 0)
  306. y2 = 80 / m;
  307. a2 = 9 * y2;
  308. b2 = 7 * y2;
  309. c2 = 5 * y2;
  310. d2 = 3 * y2;
  311. e2 = y2;
  312. decimal a3 = Utils.IndexCount(job, ""Importance"", ""1"", ""Equalizer"");
  313. decimal b3 = Utils.IndexCount(job, ""Importance"", ""3"", ""Equalizer"");
  314. decimal c3 = Utils.IndexCount(job, ""Importance"", ""5"", ""Equalizer"");
  315. decimal d3 = Utils.IndexCount(job, ""Importance"", ""7"", ""Equalizer"");
  316. decimal e3 = Utils.IndexCount(job, ""Importance"", ""9"", ""Equalizer"");
  317. //محاسبه عدد وزنی شاخص های یکسان ساز
  318. decimal z = .1m;
  319. decimal y3 = 0;
  320. decimal o = (9 * a3 + 7 * b3 + 5 * c3 + 3 * d3 + e3);
  321. if (o != 0)
  322. y3 = z / o;
  323. a3 = 9 * y3;
  324. b3 = 7 * y3;
  325. c3 = 5 * y3;
  326. d3 = 3 * y3;
  327. e3 = y3;
  328. decimal sum = 0;
  329. decimal sum2 = 0;
  330. decimal sum3 = 0;
  331. Random rnd = new Random();
  332. foreach (var index in job.Indices)
  333. {
  334. if (Utils.IndexGroup(index) == ""General"")
  335. {
  336. Utils.AddPoint(job, index, ""gross"", Convert.ToDecimal(rnd.NextDouble()));
  337. if (Utils.IndexField(index, ""Importance"") == ""9"")
  338. {
  339. sum += Utils.AddPoint(job, index, ""net"", a1 * Utils.GetPoint(job, index, ""gross"").Value).Value;
  340. }
  341. if (Utils.IndexField(index, ""Importance"") == ""7"")
  342. {
  343. sum += Utils.AddPoint(job, index, ""net"", b1 * Utils.GetPoint(job, index, ""gross"").Value).Value;
  344. }
  345. if (Utils.IndexField(index, ""Importance"") == ""5"")
  346. {
  347. sum += Utils.AddPoint(job, index, ""net"", c1 * Utils.GetPoint(job, index, ""gross"").Value).Value;
  348. }
  349. if (Utils.IndexField(index, ""Importance"") == ""3"")
  350. {
  351. sum += Utils.AddPoint(job, index, ""net"", d1 * Utils.GetPoint(job, index, ""gross"").Value).Value;
  352. }
  353. if (Utils.IndexField(index, ""Importance"") == ""1"")
  354. {
  355. sum += Utils.AddPoint(job, index, ""net"", e1 * Utils.GetPoint(job, index, ""gross"").Value).Value;
  356. }
  357. }
  358. else if (Utils.IndexGroup(index) == ""Technical"")
  359. {
  360. Utils.AddPoint(job, index, ""gross"", Convert.ToDecimal(rnd.NextDouble()));
  361. if (Utils.IndexField(index, ""Importance"") == ""9"")
  362. {
  363. sum2 += Utils.AddPoint(job, index, ""net"", a2 * Utils.GetPoint(job, index, ""gross"").Value).Value;
  364. }
  365. if (Utils.IndexField(index, ""Importance"") == ""7"")
  366. {
  367. sum2 += Utils.AddPoint(job, index, ""net"", b2 * Utils.GetPoint(job, index, ""gross"").Value).Value;
  368. }
  369. if (Utils.IndexField(index, ""Importance"") == ""5"")
  370. {
  371. sum2 += Utils.AddPoint(job, index, ""net"", c2 * Utils.GetPoint(job, index, ""gross"").Value).Value;
  372. }
  373. if (Utils.IndexField(index, ""Importance"") == ""3"")
  374. {
  375. sum2 += Utils.AddPoint(job, index, ""net"", d2 * Utils.GetPoint(job, index, ""gross"").Value).Value;
  376. }
  377. if (Utils.IndexField(index, ""Importance"") == ""1"")
  378. {
  379. sum2 += Utils.AddPoint(job, index, ""net"", e2 * Utils.GetPoint(job, index, ""gross"").Value).Value;
  380. }
  381. }
  382. else if (Utils.IndexGroup(index) == ""Equalizer"")
  383. {
  384. Utils.AddPoint(job, index, ""gross"", Convert.ToDecimal(rnd.NextDouble()));
  385. if (Utils.IndexField(index, ""Importance"") == ""9"")
  386. {
  387. sum3 += Utils.AddPoint(job, index, ""net"", a3 * Utils.GetPoint(job, index, ""gross"").Value).Value;
  388. }
  389. if (Utils.IndexField(index, ""Importance"") == ""7"")
  390. {
  391. sum3 += Utils.AddPoint(job, index, ""net"", b3 * Utils.GetPoint(job, index, ""gross"").Value).Value;
  392. }
  393. if (Utils.IndexField(index, ""Importance"") == ""5"")
  394. {
  395. sum3 += Utils.AddPoint(job, index, ""net"", c3 * Utils.GetPoint(job, index, ""gross"").Value).Value;
  396. }
  397. if (Utils.IndexField(index, ""Importance"") == ""3"")
  398. {
  399. sum3 += Utils.AddPoint(job, index, ""net"", d3 * Utils.GetPoint(job, index, ""gross"").Value).Value;
  400. }
  401. if (Utils.IndexField(index, ""Importance"") == ""1"")
  402. {
  403. sum3 += Utils.AddPoint(job, index, ""net"", e3 * Utils.GetPoint(job, index, ""gross"").Value).Value;
  404. }
  405. }
  406. }
  407. Utils.AddPoint(job, ""final-general"", sum);
  408. Utils.AddPoint(job, ""initial-technical"", sum2);
  409. Utils.AddPoint(job, ""final-equalizer"", sum3);
  410. sum2 = sum2 * (1 - z / 2 + sum3);
  411. Utils.AddPoint(job, ""final-technical"", sum2);
  412. sum = Math.Min(sum + sum2 + sum3, 100);
  413. Utils.AddPoint(job, ""final-job"", sum);
  414. total += sum;
  415. it++;
  416. }
  417. if (it > 0)
  418. Utils.AddPoint(""final"", total / it, true);
  419. ", RuleType.PerCalculation, 1);
  420. ruleRep.Add(rule);
  421. uow.Commit();
  422. }
  423. #endregion
  424. #region PMS Admin
  425. uows = new MITD.Domain.Repository.UnitOfWorkScope(
  426. new Data.NH.NHUnitOfWorkFactory(() =>
  427. {
  428. return PMSAdmin.Persistence.NH.PMSAdminSession.GetSession();
  429. }));
  430. using (var uow = uows.CurrentUnitOfWork as NHUnitOfWork)
  431. {
  432. var cftRep = new PMSAdmin.Persistence.NH.CustomFieldRepository(uow);
  433. #region Employee CustomFields
  434. for (int i = 0; i < 10; i++)
  435. {
  436. var cft = new PMSAdmin.Domain.Model.CustomFieldTypes.CustomFieldType(cftRep.GetNextId(),
  437. "فبلد دلخواه کارمند" + i, "EmployeeCft" + i, 0, 100, EntityTypeEnum.Employee, "string");
  438. cftRep.Add(cft);
  439. employeeCftList.Add(cft);
  440. }
  441. #endregion
  442. #region JobIndex CustomFields Creation
  443. for (int i = 0; i < 9; i++)
  444. {
  445. var cft = new PMSAdmin.Domain.Model.CustomFieldTypes.CustomFieldType(cftRep.GetNextId(),
  446. "فبلد دلخواه شاخص شغل" + i, "JobIndexCft" + i, 0, 100, EntityTypeEnum.JobIndex, "string");
  447. cftRep.Add(cft);
  448. jobIndexCftList.Add(cft);
  449. }
  450. var imp = new PMSAdmin.Domain.Model.CustomFieldTypes.CustomFieldType(cftRep.GetNextId(),
  451. "اهمیت", "Importance", 0, 100, EntityTypeEnum.JobIndex, "string");
  452. cftRep.Add(imp);
  453. jobIndexCftList.Add(imp);
  454. #endregion
  455. #region Job CustomFields Creation
  456. for (int i = 0; i < 10; i++)
  457. {
  458. var cft = new PMSAdmin.Domain.Model.CustomFieldTypes.CustomFieldType(cftRep.GetNextId(),
  459. "فبلد دلخواه شغل" + i, "JobCft" + i, 0, 100, EntityTypeEnum.Job, "string");
  460. cftRep.Add(cft);
  461. jobCftList.Add(cft);
  462. }
  463. #endregion
  464. var jobRep = new PMSAdmin.Persistence.NH.JobRepository(uow);
  465. #region Jobs Creation
  466. for (int i = 0; i < 5; i++)
  467. {
  468. var job = new PMSAdmin.Domain.Model.Jobs.Job(jobRep.GetNextId(),
  469. " شغل" + i, "Job" + i);
  470. job.AssignCustomFields(jobCftList.Skip(i * 2).Take(2).ToList());
  471. jobRep.AddJob(job);
  472. jobList.Add(job);
  473. }
  474. #endregion
  475. var jobPositionRep = new PMSAdmin.Persistence.NH.JobPositionRepository(uow);
  476. #region JobPositions Creation
  477. for (int i = 0; i < 5; i++)
  478. {
  479. var jobPosition = new PMSAdmin.Domain.Model.JobPositions.JobPosition(jobPositionRep.GetNextId(),
  480. " پست" + i, "JobPosition" + i);
  481. jobPositionRep.Add(jobPosition);
  482. jobPositionList.Add(jobPosition);
  483. }
  484. #endregion
  485. var unitRep = new PMSAdmin.Persistence.NH.UnitRepository(uow);
  486. #region Unit Creation
  487. for (int i = 0; i < 5; i++)
  488. {
  489. var unit = new PMSAdmin.Domain.Model.Units.Unit(unitRep.GetNextId(),
  490. " واحد" + i, "Unit" + i);
  491. unitRep.Add(unit);
  492. unitList.Add(unit);
  493. }
  494. #endregion
  495. var jobIndexRep = new PMSAdmin.Persistence.NH.JobIndexRepository(uow);
  496. #region JobIndexes Creation
  497. var jobIndexCategory = new PMSAdmin.Domain.Model.JobIndices.JobIndexCategory(jobIndexRep.GetNextId(), null, "دسته شاخص", "JobIndexCategory");
  498. jobIndexRep.Add(jobIndexCategory);
  499. for (int i = 0; i < 5; i++)
  500. {
  501. var jobIndex = new PMSAdmin.Domain.Model.JobIndices.JobIndex(jobIndexRep.GetNextId(), jobIndexCategory,
  502. " شاخص شغل" + i, "JobIndex" + i);
  503. var jobIndexCustomFields = jobIndexCftList.Skip(i * 2).Take(2).ToList();
  504. jobIndexCustomFields.Add(jobIndexCftList.Single(j => j.DictionaryName == "Importance"));
  505. jobIndex.AssignCustomFields(jobIndexCustomFields);
  506. jobIndexRep.Add(jobIndex);
  507. jobIndexList.Add(jobIndex);
  508. }
  509. #endregion
  510. var policyRep = new PMSAdmin.Persistence.NH.PolicyRepository(uow);
  511. #region Policy Creation
  512. policy = new PMSAdmin.Domain.Model.Policies.RuleEngineBasedPolicy(policyRep.GetNextId(),
  513. " خظ کش پرتر", "PorterRulerPolicy");
  514. policyRep.Add(policy);
  515. policy.AssignRule(rule);
  516. policy.AssignRuleFunction(rf);
  517. #endregion
  518. uow.Commit();
  519. }
  520. #endregion
  521. uows = new MITD.Domain.Repository.UnitOfWorkScope(
  522. new Data.NH.NHUnitOfWorkFactory(() => PMS.Persistence.NH.PMSSession.GetSession()));
  523. using (var uow = uows.CurrentUnitOfWork as NHUnitOfWork)
  524. {
  525. var periodRep = new PeriodRepository(uow);
  526. var periodManagerService = new PeriodManagerService(periodRep, null, null, null, null, null, null, null, null, null, null, null);
  527. #region Period creation
  528. period = new Period(new PeriodId(periodRep.GetNextId()), Guid.NewGuid().ToString(), DateTime.Now, DateTime.Now,91);
  529. period.ChangeActiveStatus(periodManagerService, true);
  530. periodRep.Add(period);
  531. #endregion
  532. var jobIndexRep = new PMS.Persistence.NH.JobIndexRepository(uow);
  533. #region JobIndex Creation
  534. var jobIndexGroupGenaral = new PMS.Domain.Model.JobIndices.JobIndexGroup(jobIndexRep.GetNextId(), period, null,
  535. "گروه شاخص های عمومی", "General");
  536. jobIndexRep.Add(jobIndexGroupGenaral);
  537. var jobIndexGroupTechnical = new PMS.Domain.Model.JobIndices.JobIndexGroup(jobIndexRep.GetNextId(), period, null,
  538. "گروه شاخص های تخصصی", "Technical");
  539. jobIndexRep.Add(jobIndexGroupTechnical);
  540. var countjil = jobIndexList.Count();
  541. var index = 0;
  542. foreach (var itm in jobIndexList.Take(countjil / 2))
  543. {
  544. var sharedJobIndex =
  545. new PMS.Domain.Model.JobIndices.SharedJobIndex(
  546. new PMS.Domain.Model.JobIndices.SharedJobIndexId(itm.Id.Id), itm.Name,
  547. itm.DictionaryName);
  548. var jobIndex = new PMS.Domain.Model.JobIndices.JobIndex(jobIndexRep.GetNextId(), period,
  549. sharedJobIndex, jobIndexGroupGenaral, index % 2 == 0);
  550. var dicSharedCutomField = jobIndexCftList
  551. .Where(j => itm.CustomFieldTypeIdList.Select(i => i.Id).Contains(j.Id.Id)).Select(p =>
  552. new PMS.Domain.Model.JobIndices.SharedJobIndexCustomField(
  553. new PMS.Domain.Model.JobIndices.SharedJobIndexCustomFieldId(p.Id.Id), p.Name,
  554. p.DictionaryName,
  555. p.MinValue, p.MaxValue)).ToDictionary(s => s, s => s.DictionaryName == "Importance" ? (((index + 1) * 2) - 1).ToString() : string.Empty);
  556. jobIndex.UpdateCustomFields(dicSharedCutomField);
  557. jobIndexInPeriodList.Add(jobIndex);
  558. jobIndexRep.Add(jobIndex);
  559. index++;
  560. }
  561. index = 0;
  562. foreach (var itm in jobIndexList.Skip(countjil / 2))
  563. {
  564. var sharedJobIndex =
  565. new PMS.Domain.Model.JobIndices.SharedJobIndex(
  566. new PMS.Domain.Model.JobIndices.SharedJobIndexId(itm.Id.Id), itm.Name,
  567. itm.DictionaryName);
  568. var jobIndex = new PMS.Domain.Model.JobIndices.JobIndex(jobIndexRep.GetNextId(), period,
  569. sharedJobIndex, jobIndexGroupTechnical, index % 2 == 0);
  570. var dicSharedCutomField = jobIndexCftList
  571. .Where(j => itm.CustomFieldTypeIdList.Select(i => i.Id).Contains(j.Id.Id)).Select(p =>
  572. new PMS.Domain.Model.JobIndices.SharedJobIndexCustomField(
  573. new PMS.Domain.Model.JobIndices.SharedJobIndexCustomFieldId(p.Id.Id), p.Name,
  574. p.DictionaryName,
  575. p.MinValue, p.MaxValue)).ToDictionary(s => s, s => s.DictionaryName == "Importance" ? (((index + 1) * 2) - 1).ToString() : string.Empty);
  576. jobIndex.UpdateCustomFields(dicSharedCutomField);
  577. jobIndexInPeriodList.Add(jobIndex);
  578. jobIndexRep.Add(jobIndex);
  579. index++;
  580. }
  581. #endregion
  582. var jobRep = new PMS.Persistence.NH.JobRepository(uow);
  583. #region Job creation
  584. foreach (var pmsAdminJob in jobList)
  585. {
  586. var jobJobIndices = jobIndexInPeriodList.Select(jobIndex => new JobJobIndex(jobIndex.Id, true, true, true)).ToList();
  587. var job = new PMS.Domain.Model.Jobs.Job(period, new PMS.Domain.Model.Jobs.SharedJob(
  588. new PMS.Domain.Model.Jobs.SharedJobId(pmsAdminJob.Id.Id), pmsAdminJob.Name, pmsAdminJob.DictionaryName), jobCftList
  589. .Where(j => pmsAdminJob.CustomFieldTypeIdList.Select(i => i.Id)
  590. .Contains(j.Id.Id)).Select(p =>
  591. new PMS.Domain.Model.Jobs.JobCustomField(new PMS.Domain.Model.Jobs.JobCustomFieldId(period.Id, new SharedJobCustomFieldId(p.Id.Id), new SharedJobId(pmsAdminJob.Id.Id))
  592. , new SharedJobCustomField(new SharedJobCustomFieldId(p.Id.Id), p.Name, p.DictionaryName, p.MinValue, p.MaxValue, p.TypeId))).ToList(), jobJobIndices);
  593. jobInPeriodList.Add(job);
  594. jobRep.Add(job);
  595. }
  596. #endregion
  597. var unitRep = new PMS.Persistence.NH.UnitRepository(uow);
  598. #region Unit Creation
  599. foreach (var pmsAdminUnit in unitList)
  600. {
  601. var unit = new PMS.Domain.Model.Units.Unit(period, new PMS.Domain.Model.Units.SharedUnit(
  602. new PMS.Domain.Model.Units.SharedUnitId(pmsAdminUnit.Id.Id), pmsAdminUnit.Name, pmsAdminUnit.DictionaryName), null);
  603. unitInPeriodList.Add(unit);
  604. unitRep.Add(unit);
  605. }
  606. #endregion
  607. var jobPositionRep = new PMS.Persistence.NH.JobPositionRepository(uow);
  608. #region JobPosition Creation
  609. var jpIndex = 0;
  610. PMS.Domain.Model.JobPositions.JobPosition jobPositionParent = null;
  611. foreach (var pmsAdminJobPosition in jobPositionList)
  612. {
  613. var jobPosition = new PMS.Domain.Model.JobPositions.JobPosition(period,
  614. new Domain.Model.JobPositions.SharedJobPosition(new Domain.Model.JobPositions.SharedJobPositionId(pmsAdminJobPosition.Id.Id), pmsAdminJobPosition.Name, pmsAdminJobPosition.DictionaryName)
  615. , jobPositionParent,
  616. jobInPeriodList[jpIndex],
  617. unitInPeriodList[jpIndex]
  618. );
  619. if (jpIndex != 1 && jpIndex != 2)
  620. jobPositionParent = jobPosition;
  621. jobPositionInPeriodList.Add(jobPosition);
  622. jobPositionRep.Add(jobPosition);
  623. jpIndex++;
  624. }
  625. #endregion
  626. var employeeRep = new PMS.Persistence.NH.EmployeeRepository(uow);
  627. #region Employee Creation
  628. for (var i = 0; i < 10; i++)
  629. {
  630. var employeeCustomFields =
  631. employeeCftList.ToList()
  632. .ToDictionary(
  633. e =>
  634. new Domain.Model.Employees.SharedEmployeeCustomField(
  635. new Domain.Model.Employees.SharedEmployeeCustomFieldId(e.Id.Id), e.Name,
  636. e.DictionaryName, e.MinValue, e.MaxValue), e => e.Id.Id.ToString());
  637. var employee =
  638. new PMS.Domain.Model.Employees.Employee(
  639. ((i + 1) * 2000).ToString(), period, "کارمند" + i,
  640. "کارمندیان" + i, employeeCustomFields);
  641. var jobPositionInPeriod = jobPositionInPeriodList.Skip(i / 2).Take(1).Single();
  642. var jobcustomFields = jobInPeriodList.Single(j => j.Id.Equals(jobPositionInPeriod.JobId)).CustomFields;
  643. if (jobcustomFields != null && jobcustomFields.Count != 0)
  644. {
  645. var employeeJobPosition = new Domain.Model.Employees.EmployeeJobPosition(employee, jobPositionInPeriod, period.StartDate, period.EndDate, 100, 1,
  646. jobcustomFields.Select(j => new EmployeeJobCustomFieldValue(j.Id, "10")).ToList()
  647. );
  648. employee.AssignJobPositions(new List<Domain.Model.Employees.EmployeeJobPosition> { employeeJobPosition }, periodManagerService);
  649. }
  650. empList.Add(employee);
  651. employeeRep.Add(employee);
  652. }
  653. #endregion
  654. uow.Commit();
  655. }
  656. using (var uow = uows.CurrentUnitOfWork as NHUnitOfWork)
  657. {
  658. var jobPositionRep = new PMS.Persistence.NH.JobPositionRepository(uow);
  659. var jobRep = new PMS.Persistence.NH.JobRepository(uow);
  660. var jobIndexRep = new PMS.Persistence.NH.JobIndexRepository(uow);
  661. var inquiryRep = new InquiryJobIndexPointRepository(uow);
  662. var inquiryConfiguratorService = new JobPositionInquiryConfiguratorService(jobPositionRep);
  663. foreach (var jobPosition in jobPositionInPeriodList)
  664. {
  665. var jobp = jobPositionRep.GetBy(jobPosition.Id);
  666. jobp.ConfigeInquirer(inquiryConfiguratorService, false);
  667. }
  668. uow.Commit();
  669. }
  670. using (var uow = uows.CurrentUnitOfWork as NHUnitOfWork)
  671. {
  672. var jobPositionRep = new PMS.Persistence.NH.JobPositionRepository(uow);
  673. var jobRep = new PMS.Persistence.NH.JobRepository(uow);
  674. var jobIndexRep = new PMS.Persistence.NH.JobIndexRepository(uow);
  675. var inquiryRep = new InquiryJobIndexPointRepository(uow);
  676. var inquiryConfiguratorService = new JobPositionInquiryConfiguratorService(jobPositionRep);
  677. foreach (var jobPosition in jobPositionInPeriodList)
  678. {
  679. var jobp = jobPositionRep.GetBy(jobPosition.Id);
  680. foreach (var itm in jobp.ConfigurationItemList)
  681. {
  682. var job = jobRep.GetById(itm.JobPosition.JobId);
  683. foreach (var jobIndexId in job.JobIndexList)
  684. {
  685. var jobIndex = jobIndexRep.GetById(jobIndexId.JobIndexId);
  686. if ((jobIndex as JobIndex).IsInquireable)
  687. {
  688. var id = inquiryRep.GetNextId();
  689. var inquiryIndexPoint = new Domain.Model.InquiryJobIndexPoints.InquiryJobIndexPoint(
  690. new Domain.Model.InquiryJobIndexPoints.InquiryJobIndexPointId(id),
  691. itm, jobIndex as Domain.Model.JobIndices.JobIndex, "5");
  692. inquiryRep.Add(inquiryIndexPoint);
  693. }
  694. }
  695. }
  696. }
  697. uow.Commit();
  698. }
  699. using (var uow = uows.CurrentUnitOfWork as NHUnitOfWork)
  700. {
  701. EventPublisher publisher = new EventPublisher();
  702. var rebps = new RuleBasedPolicyEngineService(new LocatorProvider("PMSDb"), publisher);
  703. var policyRep = new MITD.PMS.Persistence.NH.PolicyRepository(uow,
  704. new PolicyConfigurator(rebps));
  705. var pmsPolicy = policyRep.GetById(new PolicyId(policy.Id.Id));
  706. var calcRep = new CalculationRepository(uow);
  707. var calc = new Calculation(calcRep.GetNextId(), period, pmsPolicy,
  708. "محاسبه آزمایشی", DateTime.Now, empList[0].Id.EmployeeNo + ";" + empList[1].Id.EmployeeNo);
  709. calcRep.Add(calc);
  710. uow.Commit();
  711. }
  712. }
  713. [TestMethod]
  714. public void CalculationTest()
  715. {
  716. MITD.PMSAdmin.Domain.Model.Policies.PolicyId policyId;
  717. var empLst = string.Empty;
  718. PMSAdmin.Domain.Model.JobIndices.JobIndex sharedji;
  719. using (var transaction = new TransactionScope())
  720. using (var puow = new NHUnitOfWork(PMSAdminSession.GetSession()))
  721. using (var reuow = new NHUnitOfWork(RuleEngineSession.GetSession()))
  722. {
  723. var ruleRep = new RuleRepository(reuow);
  724. var rfRep = new RuleFunctionRepository(reuow);
  725. var reConfigRep = new REConfigeRepository(reuow);
  726. var rebps = new RuleBasedPolicyEngineService(new LocatorProvider("PMSDb"), publisher);
  727. var policyRep = new MITD.PMSAdmin.Persistence.NH.PolicyRepository(puow);
  728. var rule = new Rule(new RuleId(ruleRep.GetNextId()), Guid.NewGuid().ToString(),
  729. @"
  730. RuleExecutionUtil.Res.Add( new RuleResult{Value = data.Data});
  731. ", RuleType.PerCalculation, 1);
  732. ruleRep.Add(rule);
  733. var policy = new MITD.PMSAdmin.Domain.Model.Policies.RuleEngineBasedPolicy(
  734. policyRep.GetNextId(), Guid.NewGuid().ToString(), Guid.NewGuid().ToString());
  735. policyId = policy.Id;
  736. policyRep.Add(policy);
  737. policy.AssignRule(rule);
  738. var jirep = new PMSAdmin.Persistence.NH.JobIndexRepository(puow);
  739. var ai = new PMSAdmin.Domain.Model.JobIndices.JobIndexCategory(
  740. jirep.GetNextId(), null,
  741. Guid.NewGuid().ToString(), Guid.NewGuid().ToString());
  742. jirep.Add(ai);
  743. sharedji = new PMSAdmin.Domain.Model.JobIndices.JobIndex(
  744. jirep.GetNextId(), ai,
  745. Guid.NewGuid().ToString(), Guid.NewGuid().ToString());
  746. jirep.Add(sharedji);
  747. reuow.Commit();
  748. puow.Commit();
  749. transaction.Complete();
  750. }
  751. Calculation calc;
  752. var puows = new UnitOfWorkScope(new NHUnitOfWorkFactory(() => PMSAdminSession.GetSession()));
  753. var uows = new UnitOfWorkScope(new NHUnitOfWorkFactory(() =>
  754. {
  755. var res = PMSSession.GetSession();
  756. res.SetBatchSize(100);
  757. return res;
  758. }));
  759. Period period;
  760. using (var transaction = new TransactionScope())
  761. using (var puow = puows.CurrentUnitOfWork as NHUnitOfWork)
  762. using (var uow = uows.CurrentUnitOfWork as NHUnitOfWork)
  763. {
  764. var periodRep = new PeriodRepository(uow);
  765. period = new Period(new PeriodId(periodRep.GetNextId()), Guid.NewGuid().ToString(), DateTime.Now, DateTime.Now,91);
  766. periodRep.Add(period);
  767. var sjiService = new PMS.ACL.PMSAdmin.PMSAdminService(null, null, null, null,
  768. new PMSAdmin.Application.JobIndexService(new PMSAdmin.Persistence.NH.JobIndexRepository(puow),
  769. new PMSAdmin.Persistence.NH.CustomFieldRepository(puow))
  770. ,
  771. new PMSAdmin.Application.UnitIndexService(new PMSAdmin.Persistence.NH.UnitIndexRepository(uows),
  772. new PMSAdmin.Persistence.NH.CustomFieldRepository(uows)
  773. )
  774. );
  775. var sji = sjiService.GetSharedJobIndex(new PMS.Domain.Model.JobIndices.SharedJobIndexId(sharedji.Id.Id));
  776. var jiRep = new PMS.Persistence.NH.JobIndexRepository(uow);
  777. var jic = new PMS.Domain.Model.JobIndices.JobIndexGroup(
  778. jiRep.GetNextId(), period, null, Guid.NewGuid().ToString(), Guid.NewGuid().ToString());
  779. jiRep.Add(jic);
  780. var ji = new PMS.Domain.Model.JobIndices.JobIndex(
  781. jiRep.GetNextId(), period, sji, jic, true);
  782. jiRep.Add(ji);
  783. uow.Commit();
  784. transaction.Complete();
  785. }
  786. for (int j = 0; j < 10; j++)
  787. {
  788. using (var transaction = new TransactionScope())
  789. using (var uow = uows.CurrentUnitOfWork as NHUnitOfWork)
  790. {
  791. var empRep = new EmployeeRepository(uow);
  792. for (int i = 0; i < 500; i++)
  793. {
  794. var emp = new Employee(Guid.NewGuid().ToString(), period, Guid.NewGuid().ToString(), Guid.NewGuid().ToString());
  795. empLst += emp.Id.EmployeeNo + ";";
  796. empRep.Add(emp);
  797. }
  798. empLst = empLst.Remove(empLst.Length - 1);
  799. uow.Commit();
  800. transaction.Complete();
  801. }
  802. }
  803. using (var transaction = new TransactionScope())
  804. using (var uow = uows.CurrentUnitOfWork as NHUnitOfWork)
  805. {
  806. var rebps = new RuleBasedPolicyEngineService(new LocatorProvider("PMSDb"), publisher);
  807. var calcRep = new CalculationRepository(uow);
  808. var policyRep = new MITD.PMS.Persistence.NH.PolicyRepository(uow,
  809. new PolicyConfigurator(rebps));
  810. var policy = policyRep.GetById(new PolicyId(policyId.Id));
  811. calc = new Calculation(calcRep.GetNextId(), period, policy,
  812. Guid.NewGuid().ToString(), DateTime.Now, empLst);
  813. calcRep.Add(calc);
  814. uow.Commit();
  815. transaction.Complete();
  816. }
  817. //var calculator = new JobIndexPointCalculator(publisher);
  818. //using (var transaction = new TransactionScope())
  819. //using (var uow = new NHUnitOfWork(PMSSession.GetSession()))
  820. //{
  821. // var calcRep = new CalculationRepository(uow);
  822. // calc = calcRep.GetById(calc.Id);
  823. // calc.Run(calculator);
  824. // uow.Commit();
  825. // transaction.Complete();
  826. //}
  827. //var t = Task.Factory.StartNew(() =>
  828. // {
  829. // Thread.Sleep(1000);
  830. // using (var transaction = new TransactionScope())
  831. // using (var uow = new NHUnitOfWork(PMSSession.GetSession()))
  832. // {
  833. // var calcRep = new CalculationRepository(uow);
  834. // calc = calcRep.GetById(calc.Id);
  835. // calc.Stop(calculator);
  836. // uow.Commit();
  837. // transaction.Complete();
  838. // }
  839. // });
  840. //t.Wait();
  841. }
  842. }
  843. }