WorkFlowResultMessage <WorkFlowTransition> IManagerWorkFlow.ManageTransition(Guid tenantId, TransitionWapper operationWapper, bool isSuperAdmin)
        {
            var resM = new WorkFlowResultMessage <WorkFlowTransition>();
            ITransitionFlowEngine transitionEngine = new TransitionFlowEngine();
            var subTypeCode = iMetadataManager.GetSubTypeId(operationWapper.EntityName, operationWapper.SubTypeName);

            if (!isSuperAdmin)
            {
                var isValid = CheckRoleWorkFlowSecurity(tenantId, operationWapper, subTypeCode);
                if (!isValid)
                {
                    throw new CustomWorkflowException <WorkFlowMessage>(WorkFlowMessage.InValidTransition);
                }
            }


            var properties = new WorkFlowProcessProperties {
                EntityName = operationWapper.EntityName, SubTypeCode = subTypeCode, UserId = operationWapper.UserId, IsSuperAdmin = isSuperAdmin
            };

            transitionEngine.PreProcess(tenantId, operationWapper);
            resM = Transition(tenantId, operationWapper);
            transitionEngine.Process(tenantId, operationWapper);
            transitionEngine.PostProcess(tenantId, operationWapper);
            return(resM);
        }
        private WorkFlowResultMessage <WorkFlowTransition> Transition(Guid tenantId, TransitionWapper operationWapper)
        {
            var resM = new WorkFlowResultMessage <WorkFlowTransition>();

            //-------Update  last active step endtime
            var transactionHistories = _managerTrasition.GetTransitionHistoryByRefId(tenantId, operationWapper.RefId);

            if (transactionHistories.Count > 0)
            {
                var currentTrasitionTypes = (from transactionHistorie in transactionHistories where transactionHistorie.TransitionType.Id == operationWapper.CurrentTransitionType
                                             select transactionHistorie).OrderByDescending(p => p.StartTime).ToList();
                if (currentTrasitionTypes.Count > 0)
                {
                    var transitionLast = new WorkFlowTransition {
                        TransitionHistoryId = currentTrasitionTypes[0].TransitionHistoryId, EndTime = DateTime.UtcNow
                    };
                    _managerTrasition.UpdateTransitionHistory(tenantId, transitionLast);
                }

                dynamic jsonObject = new JObject();
                jsonObject.CurrentWorkFlowStep = operationWapper.NextTransitionType;

                var myObj = new JObject();
                myObj.Add(operationWapper.EntityName, jsonObject);

                IEntityResourceManager _iEntityResourceManager = new VPC.Framework.Business.EntityResourceManager.Contracts.EntityResourceManager();
                var resultId = _iEntityResourceManager.UpdateResultWithoutWorkFlow(tenantId, operationWapper.UserId, operationWapper.RefId,
                                                                                   operationWapper.EntityName, myObj, operationWapper.SubTypeName);

                //----- Insert into history table
                var entityId    = iMetadataManager.GetEntityContextByEntityName(operationWapper.EntityName);
                var subTypeCode = iMetadataManager.GetSubTypeId(operationWapper.EntityName, operationWapper.SubTypeName);
                var workFlow    = _reviewWorkFlow.GetWorkFlow(tenantId, entityId, subTypeCode);
                if (workFlow == null)
                {
                    throw new CustomWorkflowException <WorkFlowMessage>(WorkFlowMessage.NoOperationActivity);
                }
                var itsCurrentStep = _managerWorkFlowStep.GetWorkFlowSteps(tenantId, workFlow.WorkFlowId).FirstOrDefault(x => x.TransitionType.Id == operationWapper.NextTransitionType);
                var transitionNew  = new WorkFlowTransition();
                transitionNew.TransitionHistoryId = Guid.NewGuid();
                transitionNew.StartTime           = DateTime.UtcNow;
                transitionNew.WorkFlowStepId      = itsCurrentStep.WorkFlowStepId;
                transitionNew.EntityId            = entityId;
                transitionNew.RefId = operationWapper.RefId;
                _managerTrasition.CreateTransitionHistory(tenantId, transitionNew);
                resM.Result = transitionNew;
            }


            return(resM);
        }
        WorkFlowResultMessage <WorkFlowProcessMessage> IOperationFlowEngine.PostProcess(Guid tenantId, OperationWapper operation, WorkFlowProcessProperties properties)
        {
            var arrayList = new ArrayList {
                properties, operation.Data, tenantId
            };
            var resM = new WorkFlowResultMessage <WorkFlowProcessMessage>();


            var itsOperationProcess = WorkFlowCommon(tenantId, properties.EntityName, properties.SubTypeCode, operation.OperationType);

            if (itsOperationProcess.Count > 0)
            {
                //get its all process task
                var allTasks            = _managerWorkFlowProcessTask.GetWorkFlowProcessTask(tenantId, itsOperationProcess[0].WorkFlowId);
                var itsPostProcessTasks = new List <WorkFlowProcessTaskInfo>();
                foreach (var itsOperationProc in itsOperationProcess)
                {
                    if (itsOperationProc.ProcessType == (int)WorkFlowProcessType.PostProcess)
                    {
                        itsPostProcessTasks = (from allProcessTask in allTasks
                                               where itsOperationProc.WorkFlowProcessId == allProcessTask.WorkFlowProcessId
                                               orderby allProcessTask.SequenceCode
                                               select allProcessTask).ToList();
                    }
                }

                foreach (var itsPostProcessTask in itsPostProcessTasks)
                {
                    WorkFlowProcessMessage processResult = _processWorkFlow.OperationProcess(itsPostProcessTask.ProcessCode, arrayList);
                    if (!processResult.Success)
                    {
                        throw new CustomWorkflowException <WorkFlowMessage>(processResult.ErrorMessage.Code);
                    }
                    resM.Result = processResult;
                }
            }


            var resultArrayList = new ArrayList {
                properties
            };

            if (resM.Result != null && resM.Result.Data != null)
            {
                resultArrayList.Add(resM.Result.Data);
            }

            return(resM);
        }
        WorkFlowResultMessage <WorkFlowTransition> IManagerWorkFlow.ManageTransitionFirstStep(Guid tenantId, TransitionWapper operationWapper)
        {
            var resM = new WorkFlowResultMessage <WorkFlowTransition>();
            IOperationFlowEngine operationEngine = new OperationFlowEngine();
            var subTypeCode = iMetadataManager.GetSubTypeId(operationWapper.EntityName, operationWapper.SubTypeName);
            var properties  = new WorkFlowProcessProperties {
                EntityName = operationWapper.EntityName, SubTypeCode = subTypeCode, UserId = operationWapper.UserId, IsSuperAdmin = false
            };

            operationEngine.PreProcess(tenantId, new OperationWapper {
                OperationType = WorkFlowOperationType.Create, Data = operationWapper.ObjectData
            }, properties);
            resM = Transition(tenantId, operationWapper);
            operationEngine.Process(tenantId, new OperationWapper {
                OperationType = WorkFlowOperationType.Create, Data = operationWapper.ObjectData
            }, properties);
            operationEngine.FirstOperation(tenantId, properties);
            operationEngine.PostProcess(tenantId, new OperationWapper {
                OperationType = WorkFlowOperationType.Create, Data = operationWapper.ObjectData
            }, properties);
            return(resM);
        }