/// <summary>
        /// Open connection k2.
        /// </summary>
        /// <param name="model">The value for connect k2.</param>
        /// <param name="retry">The round of try to connect k2.</param>
        /// <returns></returns>
        private SourceCode.Workflow.Client.Connection Connection(K2ConnectModel model, int retry = 0)
        {
            var connection = new SourceCode.Workflow.Client.Connection();
            int retryNum   = retry;

            try
            {
                string connectionString = this.GetConnectionString(model);
                connection.Open(model.Url, connectionString);
                if (model.K2Profile.Impersonate)
                {
                    connection.ImpersonateUser(model.SecurityLabelName + ":" + model.K2Profile.ImpersonateUser);
                }
            }
            catch (System.Exception ex)
            {
                connection = null;
                retryNum++;

                if (retryNum == 2)
                {
                    throw new System.Exception("Unable to create connection (retry:" + retryNum.ToString() + ") : " + ex.ToString());
                }
                else
                {
                    System.Threading.Thread.Sleep(50);
                    return(Connection(model, retryNum));
                }
            }
            return(connection);
        }
 /// <summary>
 /// Initial for k2 connection.
 /// </summary>
 /// <param name="model"></param>
 public void Initial(K2ProfileModel model)
 {
     _model = this.SetValue(model.UserName, model.Password, model.ImpersonateUser);
     if (model.Management)
     {
         _connectionManagement = this.ConnectionManagement(_model);
     }
     else
     {
         _connection = this.Connection(_model);
     }
 }
        /// <summary>
        /// Set value for open connection k2.
        /// </summary>
        /// <returns></returns>
        private K2ConnectModel SetValue(string userName, string password, string impersonateUser)
        {
            K2ConnectModel result = new K2ConnectModel
            {
                K2Profile = new K2ProfileModel
                {
                    UserName = userName,
                    Password = UtilityService.DecryptString(password, ConfigSetting.EncryptionKey)
                },
                Port = Convert.ToInt32(ConfigSetting.K2WorkflowPort),
                Url  = ConfigSetting.K2Url,
                SecurityLabelName = ConfigSetting.K2SecurityLabel
            };

            if (!string.IsNullOrEmpty(impersonateUser))
            {
                result.K2Profile.Impersonate     = true;
                result.K2Profile.ImpersonateUser = impersonateUser;
            }
            return(result);
        }
        /// <summary>
        /// Set value for open connection k2.
        /// </summary>
        /// <returns></returns>
        private K2ConnectModel SetValue(string userName, string password, bool impersonate, string impersonateUser)
        {
            K2ConnectModel result = new K2ConnectModel
            {
                K2Profile = new K2ProfileModel
                {
                    UserName = userName,
                    Password = password
                },
                Port = Convert.ToInt32(ConfigurationManager.AppSettings[ConstantValueService.K2_PORT]),
                Url  = ConfigurationManager.AppSettings[ConstantValueService.K2_URL],
                SecurityLabelName = ConfigurationManager.AppSettings[ConstantValueService.K2_SECURITYLABEL]
            };

            if (impersonate)
            {
                result.K2Profile.Impersonate     = true;
                result.K2Profile.ImpersonateUser = impersonateUser;
            }
            return(result);
        }
 /// <summary>
 /// Get k2 url connection string.
 /// </summary>
 /// <param name="model">The value for connect k2.</param>
 /// <returns></returns>
 private string GetConnectionString(K2ConnectModel model)
 {
     return(string.Format(ConfigSetting.K2ConnectionString,
                          model.Url, model.Port, model.SecurityLabelName, model.K2Profile.UserName, model.K2Profile.Password));
 }
        /// <summary>
        /// Open connection management k2.
        /// </summary>
        /// <param name="model">The value for connect k2.</param>
        /// <returns></returns>
        private SourceCode.Workflow.Management.WorkflowManagementServer ConnectionManagement(K2ConnectModel model)
        {
            var managementServer = new SourceCode.Workflow.Management.WorkflowManagementServer();

            try
            {
                model.K2Profile.UserName = ConfigSetting.K2AdminUserName;
                model.K2Profile.Password = ConfigSetting.K2AdminPassword;
                model.Port = Convert.ToInt32(ConfigSetting.K2ManagementPort);
                string connectionString = this.GetConnectionString(model);
                managementServer.Open(connectionString);
            }
            catch (Exception ex)
            {
                throw new Exception("Unable to create management connection : " + ex.ToString());
            }
            return(managementServer);
        }
 /// <summary>
 /// Get k2 url connection string.
 /// </summary>
 /// <param name="model">The value for connect k2.</param>
 /// <returns></returns>
 private string GetConnectionString(K2ConnectModel model)
 {
     return(string.Format(ConfigurationManager.ConnectionStrings[ConstantValueService.K2_WORKFLOWEMPLOYEE].ToString(),
                          model.Url, model.Port, model.SecurityLabelName, model.K2Profile.UserName, model.K2Profile.Password));
 }
        /// <summary>
        /// Open connection management k2.
        /// </summary>
        /// <param name="model">The value for connect k2.</param>
        /// <returns></returns>
        private SourceCode.Workflow.Management.WorkflowManagementServer ConnectionManagement(K2ConnectModel model)
        {
            var managementServer = new SourceCode.Workflow.Management.WorkflowManagementServer();

            try
            {
                model.K2Profile.UserName = ConfigurationManager.AppSettings[ConstantValueService.K2_ADMINUSERNAME];
                model.K2Profile.Password = ConfigurationManager.AppSettings[ConstantValueService.K2_ADMINPASSWORD];
                model.Port = Convert.ToInt32(ConfigurationManager.AppSettings[ConstantValueService.K2_MANAGEMENT_PORT]);
                string connectionString = this.GetConnectionString(model);
                managementServer.Open(connectionString);
            }
            catch (Exception ex)
            {
                throw new Exception("Unable to create management connection : " + ex.ToString());
            }
            return(managementServer);
        }