/// <summary>
        /// Insere um novo operador ou atualiza caso ele já exista.
        /// </summary>
        /// <param name="oper">Operador a ser inserido ou atualizado</param>
        public void InsertUpdateOperator(Operator oper)
        {
            try
            {
                // Gera o hash da senha.
                oper.Password = PasswordTools.GenerateEncodedPassword(oper.Password);

                using (this.operatorDataAccess = new OperatorDataAccess())
                {
                    if (oper.Id > 0)
                    {
                        this.operatorDataAccess.UpdateOperator(oper.Id, oper.Code, oper.Name, oper.Password);
                    }
                    else
                    {
                        this.operatorDataAccess.InsertOperator(oper.Code, oper.Name, oper.Password, oper.Status);
                    }
                }
            }
            catch (Exception ex)
            {
                Logging.Error(BusinessConstants.ProjectName,
                              MethodBase.GetCurrentMethod().DeclaringType.Name,
                              MethodBase.GetCurrentMethod().Name,
                              ex.Message);

                throw ex;
            }
        }
        /// <summary>
        /// Valida o login do operador.
        /// </summary>
        /// <param name="oper">Operador que deseja fazer o login</param>
        /// <returns>Retorna o id do operador em caso de sucesso, ou 0 caso o login não seja válido</returns>
        public int Login(Operator oper)
        {
            try
            {
                // Gera o hash da senha.
                oper.Password = PasswordTools.GenerateEncodedPassword(oper.Password);

                using (this.operatorDataAccess = new OperatorDataAccess())
                {
                    return this.operatorDataAccess.Login(oper.Code, oper.Password);
                }
            }
            catch (Exception ex)
            {
                Logging.Error(BusinessConstants.ProjectName,
                              MethodBase.GetCurrentMethod().DeclaringType.Name,
                              MethodBase.GetCurrentMethod().Name,
                              ex.Message);

                throw ex;
            }
        }
        /// <summary>
        /// Remove um operador.
        /// </summary>
        /// <param name="oper">Operador a ser removido</param>
        public void DeleteOperator(Operator oper)
        {
            try
            {
                using (this.operatorDataAccess = new OperatorDataAccess())
                {
                    this.operatorDataAccess.UpdateOperatorStatus(oper.Code, oper.Status);
                }
            }
            catch (Exception ex)
            {
                Logging.Error(BusinessConstants.ProjectName,
                              MethodBase.GetCurrentMethod().DeclaringType.Name,
                              MethodBase.GetCurrentMethod().Name,
                              ex.Message);

                throw ex;
            }
        }
        /// <summary>
        /// Busca operador pelo id.
        /// </summary>
        /// <param name="id">Id do operador</param>
        /// <returns>Retorna o operador</returns>
        public Operator OperatorById(int id)
        {
            Operator oper = null;
            DataTable dtOperator;

            try
            {
                using (this.operatorDataAccess = new OperatorDataAccess())
                {
                    dtOperator = this.operatorDataAccess.GetOperatorById(id);
                }

                if (dtOperator != null && dtOperator.Rows.Count > 0)
                {
                    oper = new Operator();
                    oper.Id = (int)dtOperator.Rows[0][DatabaseConstants.OperatorIdColumn];
                    oper.Code = (string)dtOperator.Rows[0][DatabaseConstants.OperatorCodeColumn];
                    oper.Name = (string)dtOperator.Rows[0][DatabaseConstants.OperatorNameColumn];
                }
            }
            catch (Exception ex)
            {
                Logging.Error(BusinessConstants.ProjectName,
                              MethodBase.GetCurrentMethod().DeclaringType.Name,
                              MethodBase.GetCurrentMethod().Name,
                              ex.Message);

                throw ex;
            }

            return oper;
        }
 /// <summary>
 /// Limpa a SharedMemory.
 /// </summary>
 public void Clear()
 {
     this.operatorData = null;
 }
Пример #6
0
        /// <summary>
        /// Ação de Login.
        /// Caso o login seja realizado com sucesso, joga o usuário logado na 'SharedMemory'.
        /// </summary>
        private void Login()
        {
            try
            {
                Cursor.Current = Cursors.WaitCursor;

                if (string.IsNullOrWhiteSpace(this.textBoxOperator.Text) ||
                    string.IsNullOrWhiteSpace(this.textBoxPassword.Text))
                {
                    MessageBox.Show(this, Resources.LoginEmptyFields, Resources.Warning, 
                                    MessageBoxButtons.OK, MessageBoxIcon.Warning);

                    return;
                }

                Operator oper = new Operator();
                oper.Code = this.textBoxOperator.Text;
                oper.Password = this.textBoxPassword.Text;

                // Verifica se o login é válido.
                oper.Id = this.operatorBusiness.Login(oper);

                // Se id > 0, então login efetuado com sucesso.
                if (oper.Id > 0)
                {
                    SharedData.Instance.OperatorData = this.operatorBusiness.OperatorById(oper.Id);

                    ClearForm();

                    this.Hide();

                    FormMain frmMain = new FormMain();
                    frmMain.Show(this);
                }
                else
                {
                    MessageBox.Show(this, Resources.LoginFailed, Resources.Error, 
                                    MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            catch (Exception ex)
            {
                Logging.Error(ViewConstants.ProjectName,
                              MethodBase.GetCurrentMethod().DeclaringType.Name,
                              MethodBase.GetCurrentMethod().Name,
                              ex.Message);

                MessageBox.Show(this, Resources.LoginError, Resources.Error, 
                                MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                Cursor.Current = Cursors.Default;
            }
        }
        /// <summary>
        /// Deleta um operador.
        /// </summary>
        private void DeleteOperator()
        {
            try
            {
                Operator oper = new Operator();
                oper.Code = this.textBoxCode.Text;

                this.operatorBusiness.DeleteOperator(oper);

                MessageBox.Show(this, Resources.OperatorDeleteSuccess, Resources.Success, MessageBoxButtons.OK,
                                MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                Logging.Error(ViewConstants.ProjectName,
                              MethodBase.GetCurrentMethod().DeclaringType.Name,
                              MethodBase.GetCurrentMethod().Name,
                              ex.Message);

                MessageBox.Show(this, Resources.OperatorDeleteError, Resources.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        /// <summary>
        /// Salva os dados do operador.
        /// </summary>
        private void SaveOperator()
        {
            try
            {
                Operator oper = new Operator();

                if (this.dataGridViewOperators.SelectedRows.Count == 1)
                {
                    oper.Id = int.Parse(this.dataGridViewOperators.SelectedRows[0].Cells[0].Value.ToString());
                }

                oper.Code = this.textBoxCode.Text;
                oper.Name = this.textBoxName.Text;
                oper.Password = this.textBoxPassword.Text;
                oper.Status = 1;

                this.operatorBusiness.InsertUpdateOperator(oper);

                MessageBox.Show(this, Resources.OperatorSaveSuccess, Resources.Success, 
                                MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                Logging.Error(ViewConstants.ProjectName,
                              MethodBase.GetCurrentMethod().DeclaringType.Name,
                              MethodBase.GetCurrentMethod().Name,
                              ex.Message);

                MessageBox.Show(this, Resources.OperatorSaveError, Resources.Error, 
                                MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }