Example #1
0
        private void start(Period currentPeriod, Period sourcePeriod, IEventPublisher publisher)
        {
            basicDataCopyingProgress.State = new PeriodBasicDataCopying();
            basicDataCopyingProgress.Messages.Add("شروع کپی دوره");
            var preState = currentPeriod.State;
            publisher.OnHandlerError(exp => { basicDataCopyingProgress.Messages.Add(exp.Message); });
            copyCompletedSub = new DelegateHandler<CopyBasicDataCompleted>(
                p =>
                {
                    var srvManagerPeriod = periodServiceFactory.Create();
                    try
                    {
                        var ps = srvManagerPeriod.GetService();
                        ps.CompleteCopyingBasicData(currentPeriod.Id, preState);
                    }
                    finally
                    {
                        IsCopying = false;
                        basicDataCopyingProgress = new BasicDataCopyingProgress();
                        periodServiceFactory.Release(srvManagerPeriod);
                    }
                });
            publisher.RegisterHandler(copyCompletedSub);

            try
            {
                BasicDataCopyingProgress.SetProgress(100, 5);
                deleteAllPeriodData(currentPeriod);
                basicDataCopyingProgress.Messages.Add("اطلاعات پایه پیشین پاک شد.");

                basicDataCopyingProgress.SetProgress(100, 25);
                copyUnits(sourcePeriod, currentPeriod);
                basicDataCopyingProgress.Messages.Add("کپی واحد های سازمانی انجام شد.");


                basicDataCopyingProgress.SetProgress(100, 35);
                copyPeriodJobIndices(sourcePeriod, currentPeriod);
                basicDataCopyingProgress.Messages.Add("کپی شاخص ها انجام شد.");

                basicDataCopyingProgress.SetProgress(100, 45);
                copyJobs(sourcePeriod, currentPeriod);
                basicDataCopyingProgress.Messages.Add("کپی شغل ها انجام شد.");
                basicDataCopyingProgress.SetProgress(100, 65);

                copyJobPositions(sourcePeriod, currentPeriod);
                basicDataCopyingProgress.Messages.Add("کپی پست ها انجام شد.");
                basicDataCopyingProgress.SetProgress(100, 100);
                basicDataCopyingProgress.Messages.Add("اتمام عملیات کپی دوره.");
                lastPeriod=new KeyValuePair<PeriodId,List<string>>(currentPeriod.Id, basicDataCopyingProgress.Messages);
                publisher.Publish(new CopyBasicDataCompleted(currentPeriod,preState));
            }
            catch (Exception ex)
            {
                basicDataCopyingProgress.Messages.Add("خطا در کپی اطلاعات " + ex.Message);
                var logServiceMngt = LogServiceFactory.Create();
                try
                {
                    var logService = logServiceMngt.GetService();
                    logService.AddEventLog("Copy Exception",
                        LogLevel.Error,
                        null, this.GetType().Name, "start", ex.Message, ex.StackTrace);
                    logService.AddExceptionLog(ex);
                }
                finally
                {
                    LogServiceFactory.Release(logServiceMngt);
                }
                IsCopying = false;
                basicDataCopyingProgress = new BasicDataCopyingProgress();
            }
        }
Example #2
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();
                }
            }
        }