Ejemplo n.º 1
0
 private void addOrUpdateCalculationPoints(IEnumerable <CalculationPoint> points)
 {
     calculatorSession.AddCalculationPoints(points);
 }
Ejemplo n.º 2
0
        public void RuleTest()
        {
            using (var scope = new TransactionScope())
            {
                using (var con = new SqlConnection(ConfigurationManager.ConnectionStrings["PMSDBConnection"].ConnectionString))
                {
                    var uows = new MITD.Domain.Repository.UnitOfWorkScope(
                        new Data.NH.NHUnitOfWorkFactory(() => PMSAdmin.Persistence.NH.PMSAdminSession.GetSession(con)));

                    using (var uow = new NHUnitOfWork(PMSSession.GetSession(con)))
                        using (var uow2 = uows.CurrentUnitOfWork)
                        {
                            con.Open();
                            var pmsAdminService = new PMS.ACL.PMSAdmin.PMSAdminService(
                                new PMSAdmin.Application.UnitService(new PMSAdmin.Persistence.NH.UnitRepository(uows),
                                                                     new PMSAdmin.Persistence.NH.CustomFieldRepository(uows)),
                                new PMSAdmin.Application.JobService(new PMSAdmin.Persistence.NH.JobRepository(uows),
                                                                    new PMSAdmin.Persistence.NH.CustomFieldRepository(uows)),
                                new PMSAdmin.Application.CustomFieldService(new PMSAdmin.Persistence.NH.CustomFieldRepository(uows)),
                                new PMSAdmin.Application.JobPositionService(new PMSAdmin.Persistence.NH.JobPositionRepository(uows)),
                                new PMSAdmin.Application.JobIndexService(new PMSAdmin.Persistence.NH.JobIndexRepository(uows),
                                                                         new PMSAdmin.Persistence.NH.CustomFieldRepository(uows)),
                                new PMSAdmin.Application.UnitIndexService(new PMSAdmin.Persistence.NH.UnitIndexRepository(uows),
                                                                          new PMSAdmin.Persistence.NH.CustomFieldRepository(uows))
                                );
                            EventPublisher publisher = new EventPublisher();
                            var            rep       = new PMS.Persistence.NH.EmployeeRepository(uow);
                            var            periodRep = new PMS.Persistence.NH.PeriodRepository(uow);
                            var            calcRep   = new PMS.Persistence.NH.CalculationRepository(uow);
                            var            policyRep = new MITD.PMS.Persistence.NH.PolicyRepository(uow, new PolicyConfigurator(
                                                                                                        new RuleBasedPolicyEngineService(new LocatorProvider("PMSDbConnection"), publisher)));
                            var provider = new PMS.Application.CalculationDataProvider(rep, pmsAdminService,
                                                                                       new PMS.Persistence.NH.JobIndexPointRepository(uow));
                            var policy = policyRep.GetById(new PolicyId(1));
                            var period = periodRep.GetBy(c => c.Active);
                            var emp    = rep.GetBy(new EmployeeId("150554", period.Id));

                            // if period has no calculation
                            var calculation = new Calculation(calcRep.GetNextId(), period, policy, Guid.NewGuid().ToString(), DateTime.Now, "150554");
                            calcRep.Add(calculation);
                            uow.Commit();

                            // if period has calculation , get it by its identifier
                            //var calculation = calcRep.GetById(new CalculationId(1));

                            MITD.PMSReport.Domain.Model.CalculationData empData;
                            var pathNo = 1;
                            List <SummaryCalculationPoint> calcList = new List <SummaryCalculationPoint>();
                            var session = new CalculatorSession();
                            while (pathNo <= 2)
                            {
                                Utils.Res = new MITD.PMS.RuleContracts.RuleResult();
                                session.AddCalculationPoints(calcList);
                                session.PathNo = pathNo;
                                var data  = provider.Provide(emp, out empData, calculation, true, session);
                                var rule1 = new Rule10();
                                rule1.Execute(data);
                                var rule2 = new Rule11();
                                rule2.Execute(data);
                                var rule3 = new Rule12();
                                rule3.Execute(data);
                                //var rule4 = new Rule13();
                                //rule4.Execute(data);
                                var res = provider.Convert(Utils.Res, empData, emp, period, calculation);
                                calcList = res.CalculationPoints.OfType <SummaryCalculationPoint>().ToList();
                                var jipRep = new JobIndexPointRepository(uow);
                                if (res.EmployeePointsForAdd != null)
                                {
                                    foreach (var point in res.EmployeePointsForAdd)
                                    {
                                        jipRep.Add(point);
                                    }
                                }
                                if (res.EmployeePointsForUpdate != null)
                                {
                                    foreach (var point in res.EmployeePointsForUpdate)
                                    {
                                        var employeePoint = jipRep.GetById(point.Key);
                                        employeePoint.SetValue(point.Value);
                                    }
                                }
                                uow.Commit();
                                pathNo++;
                            }
                        }
                }
            }
        }
Ejemplo n.º 3
0
        private void start(Calculation calculation, bool doResume)
        {
            long totalCalcOperationCount   = 0;
            long preCalcOperationDoneCount = 0;

            calculatorSession = new CalculatorSession {
                PathNo = 1
            };
            if (doResume)
            {
                var calcResult = calculation.CalculationResult;
                foreach (string s in calcResult.Messages)
                {
                    messages.Add(s);
                }
                messages.Add("=====================================================");
                totalCalcOperationCount   = calcResult.TotalEmployeesCount;
                preCalcOperationDoneCount = calcResult.EmployeesCalculatedCount;
                progress.SetProgress(totalCalcOperationCount, preCalcOperationDoneCount);
                if (calcResult.LastCalculatedPath.HasValue)
                {
                    progress.SetLastCalculatedEmployee(calcResult.LastCalculatedEmployeeId, calcResult.LastCalculatedPath.Value);
                    calculatorSession.PathNo = calcResult.LastCalculatedPath.Value;
                }
                var engine = calculatorEngineFactory.Create();
                try
                {
                    calculatorSession.AddCalculationPoints(engine.GetCalculationPiontBy(calculation.Id));
                }
                finally
                {
                    calculatorEngineFactory.Release(engine);
                }
            }

            Policy policy = null;

            messages.Add(DateTime.Now + "شروع محاسبه  ");
            try
            {
                startTrigger.Set();
                CalculationCompletedSub = new DelegateHandler <CalculationCompleted>(e =>
                {
                    var engine = calculatorEngineFactory.Create();
                    try
                    {
                        engine.AddUpdateCalculationPoint(calculatorSession.CalculationPoints);
                        engine.UpdateCalculationResult(calculation, progress, this, messages);
                    }
                    finally
                    {
                        calculatorEngineFactory.Release(engine);
                    }
                });
                publisher.RegisterHandler(CalculationCompletedSub);

                rulsCompiledSub = new DelegateHandler <RulesCompiled>(e =>
                {
                    if (!doResume)
                    {
                        Task.Factory.StartNew(() =>
                        {
                            var engine = calculatorEngineFactory.Create();
                            try
                            {
                                engine.UpdateCompileResult(calculation.Id, e.CompileResult.LibraryText, e.Rules);
                            }
                            finally
                            {
                                calculatorEngineFactory.Release(engine);
                            }

                            messages.Add(DateTime.Now + "  قوانین کامپایل شدند");
                        });
                    }
                });
                publisher.RegisterHandler(rulsCompiledSub);

                publisher.RegisterHandler <JobIndexPointsReady>(jobIndexPointPersister);
                publisher.RegisterHandler <CalculationExceptionReady>(calculationExceptionPersister);
                publisher.RegisterHandler <CalculationCompleted>(jobIndexPointPersister);
                publisher.RegisterHandler <CalculationCompleted>(calculationExceptionPersister);
                publisher.RegisterHandler <CalculationForPathCompleted>(jobIndexPointPersister);
                publisher.RegisterHandler <PathPersisteCompleted>(this);

                Dictionary <int, IList <Employee> > employeesWithPath;
                Period period;
                fetchPolicyAndEmployees(calculation, doResume, calculatorSession.PathNo, out policy, out employeesWithPath, out period);
                deleteCalculationException(calculation);

                long currentCalcOperationCount = 0;
                foreach (var employees in employeesWithPath)
                {
                    currentCalcOperationCount = currentCalcOperationCount + employees.Value.Count();
                }

                if (!doResume)
                {
                    totalCalcOperationCount = currentCalcOperationCount;
                    progress.SetProgress(totalCalcOperationCount, 0);
                }

                messages.Add(DateTime.Now + "  تعداد " + currentCalcOperationCount + " عملیات محاسبه برای  " + calculation.EmployeeCount + " کارمند آماده می باشد");

                var currentCalcOperationDoneCount = preCalcOperationDoneCount;
                var pathCount = employeesWithPath.Count();

                foreach (var emlpoyees in employeesWithPath)
                {
                    calculatorSession.PathNo = emlpoyees.Key;

                    foreach (var employee in emlpoyees.Value)
                    {
                        try
                        {
                            if (doStop || doPause)
                            {
                                break;
                            }

                            var pointsHolder = calculateIndices(calculation, policy, period, employee, calculatorSession);
                            currentCalcOperationDoneCount++;
                            progress.SetProgress(totalCalcOperationCount, currentCalcOperationDoneCount);
                            addOrUpdateCalculationPoints(pointsHolder.CalculationPoints);
                            publisher.Publish(new JobIndexPointsReady(pointsHolder, calculation.Id, employee.Id, calculatorSession.PathNo));
                            progress.SetLastCalculatedEmployee(employee.Id, calculatorSession.PathNo);
                        }
                        catch (Exception ex)
                        {
                            publisher.Publish(new CalculationExceptionReady(calculation.Id, employee.Id, calculatorSession.PathNo, ex));
                            calculatorSession.HasEmployeeCalculationFailed = true;
                            messages.Add("*** خطا در محاسبه شاخص های کارمند " + employee.Id.EmployeeNo + " **** ");
                            var logServiceMngt = LogServiceFactory.Create();
                            try
                            {
                                var logService = logServiceMngt.GetService();
                                logService.AddEventLog("JobIndexPointCalculator_EmpId:" + employee.Id.EmployeeNo,
                                                       LogLevel.Error,
                                                       null, this.GetType().Name, "start", ex.Message, ex.StackTrace);
                                logService.AddExceptionLog(ex);
                            }
                            finally
                            {
                                LogServiceFactory.Release(logServiceMngt);
                            }
                        }
                    }

                    if (pathCount > calculatorSession.PathNo)
                    {
                        publisher.Publish(new CalculationForPathCompleted());
                        pathPersisterTrigger.WaitOne();
                    }

                    if (calculatorSession.HasEmployeeCalculationFailed)
                    {
                        break;
                    }
                }

                messages.Add(DateTime.Now + "  تعداد " + currentCalcOperationDoneCount + " عملیات محاسبه انجام شد");
                if (!(doStop || doPause))
                {
                    publisher.Publish(new CalculationCompleted());
                }
                else if (doStop)
                {
                    messages.Add(DateTime.Now + "  لغو محاسبه.");
                }
                else if (doPause)
                {
                    messages.Add(DateTime.Now + "  وقفه در محاسبه.");
                }
                if (doPause || doStop)
                {
                    var engine = calculatorEngineFactory.Create();
                    try
                    {
                        engine.AddUpdateCalculationPoint(calculatorSession.CalculationPoints);
                    }
                    finally
                    {
                        calculatorEngineFactory.Release(engine);
                    }
                }
                pauseTrigger.Set();
            }
            catch (Exception e)
            {
                startTrigger.Set();
                calculatorSession.HasEmployeeCalculationFailed = true;
                messages.Add(DateTime.Now + "در آماده سازی محاسبه با مشکل مواجه شده است");
                publisher.Publish(new CalculationCompleted());
            }
            finally
            {
                state = CalculatorState.Completed;
                if (policy != null)
                {
                    policy.Dispose();
                }
            }
        }