WorkFlowInnerStepInfo IManagerWorkFlowInnerStep.CreateWorkFlowInnerStep(Guid tenantId, WorkFlowInnerStepInfo workFlowInnerStep)
        {
            workFlowInnerStep.InnerStepId = Guid.NewGuid();
            _adminWorkFlowInnerStep.CreateWorkFlowInnerStep(tenantId, workFlowInnerStep);
            var workFlowProcess = new List <WorkFlowProcessInfo>();

            workFlowProcess.Add(new WorkFlowProcessInfo {
                WorkFlowProcessId          = Guid.NewGuid(), WorkFlowId = workFlowInnerStep.WorkFlowId,
                OperationOrTransactionId   = workFlowInnerStep.InnerStepId,
                OperationOrTransactionType = (int)OperationOrTransactionType.Transaction, ProcessType = (int)WorkFlowProcessType.PreProcess
            });
            workFlowProcess.Add(new WorkFlowProcessInfo {
                WorkFlowProcessId          = Guid.NewGuid(), WorkFlowId = workFlowInnerStep.WorkFlowId,
                OperationOrTransactionId   = workFlowInnerStep.InnerStepId,
                OperationOrTransactionType = (int)OperationOrTransactionType.Transaction, ProcessType = (int)WorkFlowProcessType.Process
            });
            workFlowProcess.Add(new WorkFlowProcessInfo {
                WorkFlowProcessId          = Guid.NewGuid(), WorkFlowId = workFlowInnerStep.WorkFlowId,
                OperationOrTransactionId   = workFlowInnerStep.InnerStepId,
                OperationOrTransactionType = (int)OperationOrTransactionType.Transaction, ProcessType = (int)WorkFlowProcessType.PostProcess
            });
            _managerWorkFlowProcess.CreateWorkFlowProcess(tenantId, workFlowProcess);
            workFlowInnerStep.WorkFlowProcess = workFlowProcess;
            return(workFlowInnerStep);
        }
        bool IManagerWorkFlowSecurity.InitializeTenantWorkFlow(Guid rootTenantId, Guid newTenantId, List <string> entityIds)
        {
            List <RoleInfo> newRoles = new List <RoleInfo>();
            var             allRoles = _managerRole.Roles(rootTenantId);

            foreach (var allRole in allRoles)
            {
                var itsRole = new RoleInfo();
                DataUtility.CopyPropertiesTo(allRole, itsRole);
                itsRole.RoleId = Guid.NewGuid();
                newRoles.Add(itsRole);
            }
            //Init roles
            _managerRole.CreateRoles(newTenantId, newRoles);

            var workFlows = _managerWorkFlow.GetWorkFlowsByEntityIds(rootTenantId, entityIds);

            if (workFlows.Count > 0)
            {
                //Get root tenant
                var workFlowIds          = workFlows.Select(p => p.WorkFlowId).ToList();
                var workFlowSteps        = _managerWorkFlowStep.GetWorkFlowStepsByWorkFlowIds(rootTenantId, workFlowIds);
                var workFlowInnerSteps   = _managerInnerStep.GetWorkFlowInnerStepByWorkFlowIds(rootTenantId, workFlowIds);
                var workFlowOperations   = _managerOperation.GetWorkFlowOperationsByWorkFlowIds(rootTenantId, workFlowIds);
                var workFlowProcess      = _managerWorkFlowProcess.GetWorkFlowProcessByWorkFlowIds(rootTenantId, workFlowIds);
                var workFlowProcessTasks = _managerWorkFlowProcessTask.GetWorkFlowProcessTaskByWorkFlowIds(rootTenantId, workFlowIds);
                var workFlowRoles        = _managerWorkFlowRole.GetWorkFlowRolesByWorkFlowIds(rootTenantId, workFlowIds);

                List <WorkFlowInfo>            newWorkFlows     = new List <WorkFlowInfo>();
                List <WorkFlowStepInfo>        newWorkFlowSteps = new List <WorkFlowStepInfo>();
                List <WorkFlowInnerStepInfo>   newInnerSteps    = new List <WorkFlowInnerStepInfo>();
                List <WorkFlowOperationInfo>   newOperations    = new List <WorkFlowOperationInfo>();
                List <WorkFlowProcessInfo>     newProcess       = new List <WorkFlowProcessInfo>();
                List <WorkFlowProcessTaskInfo> newProcessTasks  = new List <WorkFlowProcessTaskInfo>();
                List <WorkFlowRoleInfo>        newWorkFlowRoles = new List <WorkFlowRoleInfo>();

                foreach (var workFlow in workFlows)
                {
                    var itsWorkFlow = new WorkFlowInfo();
                    DataUtility.CopyPropertiesTo(workFlow, itsWorkFlow);
                    itsWorkFlow.WorkFlowId = Guid.NewGuid();

                    //Filter steps
                    var filterWorkFlowSteps = (from workFlowStep in workFlowSteps where workFlowStep.WorkFlowId == workFlow.WorkFlowId select workFlowStep).ToList();
                    foreach (var filterWorkFlowStep in filterWorkFlowSteps)
                    {
                        var itsStep = new WorkFlowStepInfo();
                        DataUtility.CopyPropertiesTo(filterWorkFlowStep, itsStep);
                        itsStep.WorkFlowId     = itsWorkFlow.WorkFlowId;
                        itsStep.WorkFlowStepId = Guid.NewGuid();

                        //Filter Inner steps
                        var filterInnerSteps = (from innerStep in workFlowInnerSteps where innerStep.WorkFlowStepId == filterWorkFlowStep.WorkFlowStepId &&
                                                innerStep.WorkFlowId == workFlow.WorkFlowId select innerStep).ToList();

                        foreach (var filterInnerStep in filterInnerSteps)
                        {
                            var itsInnerStep = new WorkFlowInnerStepInfo();;
                            DataUtility.CopyPropertiesTo(filterInnerStep, itsInnerStep);
                            itsInnerStep.InnerStepId    = Guid.NewGuid();
                            itsInnerStep.WorkFlowId     = itsWorkFlow.WorkFlowId;
                            itsInnerStep.WorkFlowStepId = itsStep.WorkFlowStepId;
                            //Filter Process
                            var filterProcess = (from workFlowProcs in workFlowProcess where workFlowProcs.OperationOrTransactionId == filterInnerStep.InnerStepId &&
                                                 workFlowProcs.WorkFlowId == workFlow.WorkFlowId select workFlowProcs).ToList();

                            foreach (var filterProces in filterProcess)
                            {
                                var itsProcess = new WorkFlowProcessInfo();
                                DataUtility.CopyPropertiesTo(filterProces, itsProcess);

                                itsProcess.WorkFlowProcessId        = Guid.NewGuid();
                                itsProcess.WorkFlowId               = itsWorkFlow.WorkFlowId;
                                itsProcess.OperationOrTransactionId = itsInnerStep.InnerStepId;

                                //Filter process tasks
                                var filterProcessTasks = (from processTask in workFlowProcessTasks where processTask.WorkFlowProcessId == filterProces.WorkFlowProcessId &&
                                                          processTask.WorkFlowId == workFlow.WorkFlowId select processTask).ToList();

                                foreach (var filterProcessTask in filterProcessTasks)
                                {
                                    var itsTasks = new WorkFlowProcessTaskInfo();
                                    DataUtility.CopyPropertiesTo(filterProcessTask, itsTasks);
                                    itsTasks.WorkFlowProcessTaskId = Guid.NewGuid();
                                    itsTasks.WorkFlowId            = itsWorkFlow.WorkFlowId;
                                    itsTasks.WorkFlowProcessId     = itsProcess.WorkFlowProcessId;
                                    newProcessTasks.Add(itsTasks);
                                }

                                newProcess.Add(itsProcess);
                            }

                            newInnerSteps.Add(itsInnerStep);
                        }

                        //Filter roles

                        var filterRoles = (from workFlowRole in workFlowRoles where workFlowRole.WorkFlowStepId == filterWorkFlowStep.WorkFlowStepId &&
                                           workFlowRole.WorkFlowId == workFlow.WorkFlowId select workFlowRole).ToList();

                        foreach (var filterRole in filterRoles)
                        {
                            var roleId = Guid.Empty;
                            var rootTenantRoleFilter = (from allRole in allRoles where allRole.RoleId == filterRole.RoleId select allRole).ToList();
                            if (rootTenantRoleFilter.Count > 0)
                            {
                                roleId = (from newRole in newRoles where newRole.Name == rootTenantRoleFilter[0].Name select newRole.RoleId).FirstOrDefault();
                            }
                            var itsRole = new WorkFlowRoleInfo();
                            DataUtility.CopyPropertiesTo(filterRole, itsRole);
                            itsRole.RoleAssignmetId = Guid.NewGuid();
                            itsRole.WorkFlowId      = itsWorkFlow.WorkFlowId;
                            itsRole.WorkFlowStepId  = itsStep.WorkFlowStepId;
                            itsRole.RoleId          = roleId;

                            newWorkFlowRoles.Add(itsRole);
                        }

                        newWorkFlowSteps.Add(itsStep);
                    }

                    //Filter Operation*****************************************
                    var filterOperations = (from workFlowOperation in workFlowOperations where workFlowOperation.WorkFlowId == workFlow.WorkFlowId select workFlowOperation).ToList();
                    foreach (var filterOperation in filterOperations)
                    {
                        var itsOperation = new WorkFlowOperationInfo();
                        DataUtility.CopyPropertiesTo(filterOperation, itsOperation);
                        itsOperation.WorkFlowOperationId = Guid.NewGuid();
                        itsOperation.WorkFlowId          = itsWorkFlow.WorkFlowId;

                        //filter process
                        var filterProcess = (from workFlowProcs in workFlowProcess where workFlowProcs.OperationOrTransactionId == filterOperation.WorkFlowOperationId &&
                                             workFlowProcs.WorkFlowId == workFlow.WorkFlowId select workFlowProcs).ToList();

                        foreach (var filterProces in filterProcess)
                        {
                            var itsProcess = new WorkFlowProcessInfo();
                            DataUtility.CopyPropertiesTo(filterProces, itsProcess);

                            itsProcess.WorkFlowProcessId        = Guid.NewGuid();
                            itsProcess.WorkFlowId               = itsWorkFlow.WorkFlowId;
                            itsProcess.OperationOrTransactionId = itsOperation.WorkFlowOperationId;

                            //Filter process tasks
                            var filterProcessTasks = (from processTask in workFlowProcessTasks where processTask.WorkFlowProcessId == filterProces.WorkFlowProcessId &&
                                                      processTask.WorkFlowId == workFlow.WorkFlowId select processTask).ToList();

                            foreach (var filterProcessTask in filterProcessTasks)
                            {
                                var itsTasks = new WorkFlowProcessTaskInfo();
                                DataUtility.CopyPropertiesTo(filterProcessTask, itsTasks);
                                itsTasks.WorkFlowProcessTaskId = Guid.NewGuid();
                                itsTasks.WorkFlowId            = itsWorkFlow.WorkFlowId;
                                itsTasks.WorkFlowProcessId     = itsProcess.WorkFlowProcessId;
                                newProcessTasks.Add(itsTasks);
                            }

                            newProcess.Add(itsProcess);
                        }


                        newOperations.Add(itsOperation);
                    }


                    newWorkFlows.Add(itsWorkFlow);
                }

                //Init workflow
                if (newWorkFlows.Count > 0)
                {
                    _managerWorkFlow.CreateWorkFlows(newTenantId, newWorkFlows);
                }
                if (newOperations.Count > 0)
                {
                    _managerOperation.CreateWorkFlowOperations(newTenantId, newOperations);
                }
                if (newWorkFlowSteps.Count > 0)
                {
                    _managerWorkFlowStep.CreateWorkFlowSteps(newTenantId, newWorkFlowSteps);
                }
                if (newInnerSteps.Count > 0)
                {
                    _managerInnerStep.CreateWorkFlowInnerSteps(newTenantId, newInnerSteps);
                }
                if (newProcess.Count > 0)
                {
                    _managerWorkFlowProcess.CreateWorkFlowProcess(newTenantId, newProcess);
                }
                if (newProcessTasks.Count > 0)
                {
                    _managerWorkFlowProcessTask.CreateWorkFlowProcessTasks(newTenantId, newProcessTasks);
                }
                if (newWorkFlowRoles.Count > 0)
                {
                    _managerWorkFlowRole.CreateWorkFlowRoles(newTenantId, newWorkFlowRoles);
                }
            }

            return(true);
        }