public ResultOperation Add(sysBpmsVariable variable, List <sysBpmsVariableDependency> listVariables)
        {
            ResultOperation resultOperation = new ResultOperation();

            try
            {
                this.BeginTransaction();
                if (resultOperation.IsSuccess)
                {
                    if (variable.RelationTypeLU != (int)sysBpmsVariable.e_RelationTypeLU.Entity)
                    {
                        variable.FilterTypeLU = variable.ApplicationPageID.HasValue ? (int)sysBpmsVariable.e_FilterTypeLU.AllEntities : variable.FilterTypeLU;
                        variable.EntityDefID  = null;
                        variable.FieldName    = string.Empty;
                    }

                    if (variable.RelationTypeLU != (int)sysBpmsVariable.e_RelationTypeLU.SqlQuery)
                    {
                        variable.Query          = string.Empty;
                        variable.DBConnectionID = null;
                    }
                    sysBpmsVariable preVariable = this.GetInfo(variable.ProcessID, variable.ApplicationPageID, variable.Name);
                    if (preVariable != null)
                    {
                        resultOperation.AddError(LangUtility.Get("SameName.Error", nameof(sysBpmsVariable)));
                    }

                    if (resultOperation.IsSuccess)
                    {
                        this.UnitOfWork.Repository <IVariableRepository>().Add(variable);
                        this.UnitOfWork.Save();

                        if (variable.RelationTypeLU == (int)sysBpmsVariable.e_RelationTypeLU.Entity)
                        {
                            listVariables = listVariables ?? new List <sysBpmsVariableDependency>();
                            foreach (var item in listVariables)
                            {
                                item.DependentVariableID = variable.ID;
                                if (resultOperation.IsSuccess)
                                {
                                    resultOperation = new VariableDependencyService(base.UnitOfWork).Add(item);
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                return(base.ExceptionHandler(ex));
            }
            base.FinalizeService(resultOperation);

            return(resultOperation);
        }
        public ResultOperation Delete(Guid variableId)
        {
            ResultOperation resultOperation = new ResultOperation();

            try
            {
                this.BeginTransaction();
                if (resultOperation.IsSuccess)
                {
                    List <sysBpmsVariableDependency> list = new VariableDependencyService(base.UnitOfWork).GetList(variableId, null);
                    foreach (sysBpmsVariableDependency item in list)
                    {
                        if (resultOperation.IsSuccess)
                        {
                            resultOperation = new VariableDependencyService(base.UnitOfWork).Delete(item.ID);
                        }
                    }

                    list = new VariableDependencyService(base.UnitOfWork).GetList(null, variableId);
                    foreach (sysBpmsVariableDependency item in list)
                    {
                        if (resultOperation.IsSuccess)
                        {
                            resultOperation = new VariableDependencyService(base.UnitOfWork).Delete(item.ID);
                        }
                    }
                    if (resultOperation.IsSuccess)
                    {
                        this.UnitOfWork.Repository <IVariableRepository>().Delete(variableId);
                        this.UnitOfWork.Save();
                    }
                }
            }
            catch (Exception ex)
            {
                return(base.ExceptionHandler(ex));
            }
            base.FinalizeService(resultOperation);
            return(resultOperation);
        }
        public ResultOperation NewVersion(sysBpmsProcess current, string userName)
        {
            ResultOperation resultOperation;

            try
            {
                resultOperation = new ResultOperation();
                if (resultOperation.IsSuccess)
                {
                    if (this.GetList((int)sysBpmsProcess.Enum_StatusLU.Draft, current.ParentProcessID ?? current.ID).Any())
                    {
                        resultOperation.AddError(LangUtility.Get("NewVersionError.Text", nameof(sysBpmsProcess)));
                        return(resultOperation);
                    }
                    this.BeginTransaction();

                    //Create new process
                    sysBpmsProcess newProcess = this.GetInfo(current.ID);
                    newProcess.CreateDate      = DateTime.Now;
                    newProcess.UpdateDate      = DateTime.Now;
                    newProcess.StatusLU        = (int)sysBpmsProcess.Enum_StatusLU.Draft;
                    newProcess.ProcessVersion  = newProcess.ProcessVersion + 1;
                    newProcess.ParentProcessID = current.ParentProcessID.HasValue ? current.ParentProcessID.Value : current.ID;
                    newProcess.Number          = this.CalculateSerlialNumber(this.UnitOfWork.Repository <IProcessRepository>().MaxNumber() + 1);
                    newProcess.FormattedNumber = this.CalculateFormatNumber(newProcess.Number.Value, DateTime.Now.Date);

                    this.UnitOfWork.Repository <IProcessRepository>().Add(newProcess);
                    this.UnitOfWork.Save();
                    //Copy Variable
                    Dictionary <Guid, Guid> variableConvertID   = new Dictionary <Guid, Guid>();
                    VariableService         variableService     = new VariableService(base.UnitOfWork);
                    List <sysBpmsVariable>  sysBpmsVariableList = variableService.GetList(current.ID, null, null, "", null, null);
                    if (resultOperation.IsSuccess)
                    {
                        foreach (sysBpmsVariable item in sysBpmsVariableList)
                        {
                            Guid oldGuidID = item.ID;
                            item.ProcessID  = newProcess.ID;
                            resultOperation = variableService.Add(item, null);
                            variableConvertID.Add(oldGuidID, item.ID);
                            if (!resultOperation.IsSuccess)
                            {
                                break;
                            }
                        }
                    }
                    //Copy VariableDependency
                    VariableDependencyService        variableDependencyService     = new VariableDependencyService(base.UnitOfWork);
                    List <sysBpmsVariableDependency> sysBpmsVariableDependencyList = variableDependencyService.GetList(current.ID);
                    if (resultOperation.IsSuccess)
                    {
                        foreach (sysBpmsVariableDependency item in sysBpmsVariableDependencyList)
                        {
                            if (item.ToVariableID.HasValue)
                            {
                                item.ToVariableID = variableConvertID[item.ToVariableID.Value];
                            }
                            item.DependentVariableID = variableConvertID[item.DependentVariableID];
                            resultOperation          = variableDependencyService.Add(item);
                            if (!resultOperation.IsSuccess)
                            {
                                break;
                            }
                        }
                    }
                    //Copy Element
                    ElementService        elementService     = new ElementService(base.UnitOfWork);
                    List <sysBpmsElement> sysBpmsElementList = elementService.GetList(current.ID, null, "");
                    if (resultOperation.IsSuccess)
                    {
                        foreach (sysBpmsElement item in sysBpmsElementList)
                        {
                            item.ProcessID  = newProcess.ID;
                            item.Process    = null;
                            resultOperation = elementService.Add(item);
                            if (!resultOperation.IsSuccess)
                            {
                                break;
                            }
                        }
                    }
                    //Copy Lane
                    LaneService        laneService     = new LaneService(base.UnitOfWork);
                    List <sysBpmsLane> sysBpmsLaneList = laneService.GetList(current.ID);
                    if (resultOperation.IsSuccess)
                    {
                        foreach (sysBpmsLane item in sysBpmsLaneList)
                        {
                            item.ID        = Guid.NewGuid();
                            item.ProcessID = newProcess.ID;
                            item.Element   = null;
                            laneService.Add(item);
                        }
                    }
                    //Copy event
                    EventService        eventService     = new EventService(base.UnitOfWork);
                    List <sysBpmsEvent> sysBpmsEventList = eventService.GetList(null, current.ID, "", null, null);
                    if (resultOperation.IsSuccess)
                    {
                        foreach (sysBpmsEvent item in sysBpmsEventList)
                        {
                            item.ID         = Guid.NewGuid();
                            item.ProcessID  = newProcess.ID;
                            item.Element    = null;
                            resultOperation = eventService.Add(item);
                            if (!resultOperation.IsSuccess)
                            {
                                break;
                            }
                        }
                    }
                    //Copy SequenceFlow
                    Dictionary <Guid, Guid>    flowConvertID           = new Dictionary <Guid, Guid>();
                    SequenceFlowService        sequenceFlowService     = new SequenceFlowService(base.UnitOfWork);
                    List <sysBpmsSequenceFlow> sysBpmsSequenceFlowList = sequenceFlowService.GetList(current.ID);
                    if (resultOperation.IsSuccess)
                    {
                        foreach (sysBpmsSequenceFlow item in sysBpmsSequenceFlowList)
                        {
                            Guid flowNewId = Guid.NewGuid();
                            flowConvertID.Add(item.ID, flowNewId);
                            item.ID         = flowNewId;
                            item.ProcessID  = newProcess.ID;
                            item.Process    = null;
                            item.Element    = null;
                            resultOperation = sequenceFlowService.Add(item);
                            if (!resultOperation.IsSuccess)
                            {
                                break;
                            }
                        }
                    }
                    //Copy gateway
                    GatewayService        gatewayService     = new GatewayService(base.UnitOfWork);
                    ConditionService      conditionService   = new ConditionService(base.UnitOfWork);
                    List <sysBpmsGateway> sysBpmsGatewayList = gatewayService.GetList(current.ID);
                    if (resultOperation.IsSuccess)
                    {
                        foreach (sysBpmsGateway item in sysBpmsGatewayList)
                        {
                            List <sysBpmsCondition> sysBpmsConditionList = conditionService.GetList(item.ID, null, null);
                            item.ID        = Guid.NewGuid();
                            item.ProcessID = newProcess.ID;
                            item.Element   = null;
                            if (item.DefaultSequenceFlowID.HasValue)
                            {
                                item.DefaultSequenceFlowID = flowConvertID[item.DefaultSequenceFlowID.Value];
                            }
                            resultOperation = gatewayService.Add(item);
                            if (!resultOperation.IsSuccess)
                            {
                                break;
                            }
                            foreach (sysBpmsCondition condition in sysBpmsConditionList)
                            {
                                condition.GatewayID = item.ID;
                                if (condition.SequenceFlowID.HasValue)
                                {
                                    condition.SequenceFlowID = flowConvertID[condition.SequenceFlowID.Value];
                                }
                                condition.Gateway      = null;
                                condition.SequenceFlow = null;
                                resultOperation        = conditionService.Add(condition);
                                if (!resultOperation.IsSuccess)
                                {
                                    break;
                                }
                            }
                        }
                    }
                    //Copy dynamicForm
                    Dictionary <Guid, Guid>   formConvertID          = new Dictionary <Guid, Guid>();
                    DynamicFormService        dynamicFormService     = new DynamicFormService(base.UnitOfWork);
                    ApplicationPageService    applicationPageService = new ApplicationPageService(base.UnitOfWork);
                    List <sysBpmsDynamicForm> sysBpmsDynamicFormList = dynamicFormService.GetList(current.ID, null, null, "", null, null);
                    if (resultOperation.IsSuccess)
                    {
                        foreach (sysBpmsDynamicForm item in sysBpmsDynamicFormList)
                        {
                            Guid oldID = item.ID;
                            item.ProcessId = newProcess.ID;
                            //First change code id.
                            dynamicFormService.UpdateBackendCodeID(item);
                            //Then update sourceCode value.
                            dynamicFormService.GetSourceCode(item);
                            item.Process    = null;
                            resultOperation = dynamicFormService.Add(item,
                                                                     item.ApplicationPageID.HasValue ? applicationPageService.GetInfo(item.ApplicationPageID.Value) : null, userName);
                            formConvertID.Add(oldID, item.ID);
                            if (!resultOperation.IsSuccess)
                            {
                                break;
                            }
                        }
                    }
                    //Update formHtml control formid property
                    if (resultOperation.IsSuccess)
                    {
                        foreach (sysBpmsDynamicForm item in sysBpmsDynamicFormList)
                        {
                            if (item.DesignJson.Contains("formId"))
                            {
                                formConvertID.ToList().ForEach(c =>
                                {
                                    item.DesignJson = item.DesignJson.Replace(c.Key.ToString(), c.Value.ToString());
                                });
                                resultOperation = dynamicFormService.Update(item, userName);
                                if (!resultOperation.IsSuccess)
                                {
                                    break;
                                }
                            }
                        }
                    }
                    //Copy task
                    TaskService        taskService     = new TaskService(base.UnitOfWork);
                    StepService        stepService     = new StepService(base.UnitOfWork);
                    List <sysBpmsTask> sysBpmsTaskList = taskService.GetList(null, current.ID);
                    if (resultOperation.IsSuccess)
                    {
                        foreach (sysBpmsTask item in sysBpmsTaskList)
                        {
                            List <sysBpmsStep> sysBpmsStepList = stepService.GetList(item.ID, null);
                            item.ID         = Guid.NewGuid();
                            item.ProcessID  = newProcess.ID;
                            item.Process    = null;
                            item.Element    = null;
                            resultOperation = taskService.Add(item);
                            if (!resultOperation.IsSuccess)
                            {
                                break;
                            }
                            foreach (sysBpmsStep step in sysBpmsStepList)
                            {
                                step.TaskID = item.ID;
                                if (step.DynamicFormID.HasValue)
                                {
                                    step.DynamicFormID = formConvertID[step.DynamicFormID.Value];
                                }
                                step.DynamicForm = null;
                                step.Task        = null;
                                resultOperation  = stepService.Add(step);
                                if (!resultOperation.IsSuccess)
                                {
                                    break;
                                }
                            }
                        }
                    }

                    this.UnitOfWork.Save();
                }
            }
            catch (Exception ex)
            {
                return(base.ExceptionHandler(ex));
            }
            base.FinalizeService(resultOperation);

            return(resultOperation);
        }
        public ResultOperation Update(sysBpmsVariable variable, List <sysBpmsVariableDependency> listVariables)
        {
            ResultOperation resultOperation = new ResultOperation();

            try
            {
                this.BeginTransaction();
                if (resultOperation.IsSuccess)
                {
                    if (variable.RelationTypeLU != (int)sysBpmsVariable.e_RelationTypeLU.Entity)
                    {
                        variable.FilterTypeLU = variable.ApplicationPageID.HasValue ? (int)sysBpmsVariable.e_FilterTypeLU.AllEntities : variable.FilterTypeLU;
                        variable.EntityDefID  = null;
                        variable.FieldName    = string.Empty;
                    }

                    if (variable.RelationTypeLU != (int)sysBpmsVariable.e_RelationTypeLU.SqlQuery)
                    {
                        variable.Query          = string.Empty;
                        variable.DBConnectionID = null;
                    }
                    sysBpmsVariable preVariable = this.GetInfo(variable.ProcessID, variable.ApplicationPageID, variable.Name);
                    if (preVariable != null && preVariable.ID != variable.ID)
                    {
                        resultOperation.AddError(LangUtility.Get("SameName.Error", nameof(sysBpmsVariable)));
                    }

                    if (resultOperation.IsSuccess)
                    {
                        this.UnitOfWork.Repository <IVariableRepository>().Update(variable);
                        this.UnitOfWork.Save();

                        //if variable is entity type.
                        if (variable.RelationTypeLU == (int)sysBpmsVariable.e_RelationTypeLU.Entity)
                        {
                            listVariables = listVariables ?? new List <sysBpmsVariableDependency>();
                            //VariableDependency
                            List <sysBpmsVariableDependency> Dependencies = new VariableDependencyService(base.UnitOfWork).GetList(variable.ID, null);

                            //delete VariableDependency if is not exist in listVariables
                            Dependencies.ForEach(c =>
                            {
                                if (!listVariables.Any(d => d.ID == c.ID))
                                {
                                    new VariableDependencyService(base.UnitOfWork).Delete(c.ID);
                                }
                            });

                            //add or update VariableDependency
                            foreach (var item in listVariables)
                            {
                                item.DependentVariableID = variable.ID;
                                if (resultOperation.IsSuccess)
                                {
                                    if (item.ID == Guid.Empty)
                                    {
                                        resultOperation = new VariableDependencyService(base.UnitOfWork).Add(item);
                                    }
                                    else
                                    {
                                        resultOperation = new VariableDependencyService(base.UnitOfWork).Update(item);
                                    }
                                }
                            }
                        }
                        else
                        {
                            new VariableDependencyService(base.UnitOfWork).GetList(variable.ID, null).ForEach(c =>
                            {
                                if (resultOperation.IsSuccess)
                                {
                                    resultOperation = new VariableDependencyService(base.UnitOfWork).Delete(c.ID);
                                }
                            });
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                return(base.ExceptionHandler(ex));
            }
            base.FinalizeService(resultOperation);

            return(resultOperation);
        }