Exemplo n.º 1
0
        private Boolean formValid()
        {
            // Validate if function ship is inside Nao Docente main ship

            Turno novoTurno = new Turno();

            novoTurno.horaInicio = TimeSpan.Parse(panelFormFieldHoraInicio.Text);
            novoTurno.horaFim    = TimeSpan.Parse(panelFormFieldHoraFim.Text);

            /* Moved this validation for DB (Trigger)
             * if (!Turno.isInside(novoTurno, nd.turno))
             * {
             *  MessageBox.Show(
             *      "O horário da função deve estar dentro do turno do funcionário!",
             *      "Erro!",
             *      MessageBoxButtons.OK,
             *      MessageBoxIcon.Error
             *  );
             *  return false;
             * }
             */
            // Other time validations
            if (novoTurno.horaInicio.CompareTo(novoTurno.horaFim) == 0)
            {
                MessageBox.Show(
                    "As horas de início de fim não podem ser iguais!",
                    "Erro!",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Error
                    );
                return(false);
            }
            if (novoTurno.horaInicio.CompareTo(novoTurno.horaFim) > 0)
            {
                MessageBox.Show(
                    "O horário de início é maior ou igual ao de fim!",
                    "Erro!",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Error
                    );
                return(false);
            }
            return(true);
        }
Exemplo n.º 2
0
        //  Methods

        private void loadTurnos()
        {
            // Execute SQL query to get Docente rows
            SqlCommand    cmd    = new SqlCommand("SELECT * FROM GestaoEscola.Turno", cn);
            SqlDataReader reader = cmd.ExecuteReader();

            while (reader.Read())
            {
                Turno t = new Turno();
                t.codigo     = Int32.Parse(reader["codigo"].ToString());
                t.horaInicio = TimeSpan.Parse(reader["horaInicio"].ToString());
                t.horaFim    = TimeSpan.Parse(reader["horaFim"].ToString());
                panelFormFieldTurno.Items.Add(t.str);
                turnos.Add(t);
            }

            // Close reader
            reader.Close();
        }
Exemplo n.º 3
0
        private void editObject()
        {
            // Get Object
            if (listObjects.Items.Count == 0 | current < 0)
            {
                return;
            }
            Turno f = (Turno)listObjects.SelectedObjects[0];

            // Set textboxes value
            panelFormFieldHoraInicio.Text = f.horaInicio.ToString();
            panelFormFieldHoraFim.Text    = f.horaFim.ToString();
            // Set title and description
            panelFormTitulo.Text    = "Editar turno";
            panelFormDescricao.Text = "Altere os dados e submita o formulário";
            panelFormButton.Text    = "Submeter";
            // Make panel visible
            if (!panelForm.Visible)
            {
                panelForm.Visible = true;
            }
        }
Exemplo n.º 4
0
        private void showObject()
        {
            // Get Object
            if (listObjects.Items.Count == 0 | current < 0)
            {
                return;
            }
            Turno f = (Turno)listObjects.SelectedObjects[0];

            // Set labels values
            panelObjectTitulo.Text    = "Turno";
            panelObjectSubtitulo.Text = f.str;
            // Show panel
            if (!panelObject.Visible)
            {
                panelObject.Visible = true;
            }
            // Hide form
            if (panelForm.Visible)
            {
                panelForm.Visible = false;
            }
        }
Exemplo n.º 5
0
        private void submitForm(Turno turno)
        {
            /*
             * If submition for edit t!=null
             * If new submition t==null
             */
            bool edit = (turno != null);
            // Get form data
            TimeSpan horaInicio = TimeSpan.Parse(panelFormFieldHoraInicio.Text);
            TimeSpan horaFim    = TimeSpan.Parse(panelFormFieldHoraFim.Text);
            // Create command
            String commandText = "INSERT INTO GestaoEscola.Turno VALUES (@ID, @HORAINICIO, @HORAFIM)";

            if (edit)
            {
                commandText = "UPDATE GestaoEscola.Turno SET horaInicio = @HORAINICIO, horaFim = @HORAFIM WHERE codigo = @ID";
            }
            SqlCommand command = new SqlCommand(commandText, cn);

            // Add vars
            command.Parameters.Add("@ID", SqlDbType.Int);
            if (edit)
            {
                command.Parameters["@ID"].Value = turno.codigo;
            }
            else
            {
                command.Parameters["@ID"].Value = lastId + 1;
            }
            command.Parameters.Add("@HORAINICIO", SqlDbType.Time);
            command.Parameters["@HORAINICIO"].Value = horaInicio;
            command.Parameters.Add("@HORAFIM", SqlDbType.Time);
            command.Parameters["@HORAFIM"].Value = horaFim;
            // Execute query
            int rowsAffected = 0;

            try
            {
                rowsAffected = command.ExecuteNonQuery();
                Console.WriteLine(String.Format("rowsAffected {0}", rowsAffected));
            }
            catch (SqlException ex)
            {
                MessageBox.Show(
                    "Ocorreu um erro, verifique que preencheu todos os dados corretamente e tente novamente!\r\n" + ex.ToString(),
                    "Erro!",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Error
                    );
                return;
            }
            // If query is successful
            if (rowsAffected == 1)
            {
                // If add operation
                if (!edit)
                {
                    // Update lastId
                    lastId++;
                    // Add tuple to interface list
                    turno            = new Turno();
                    turno.codigo     = lastId;
                    turno.horaInicio = horaInicio;
                    turno.horaFim    = horaFim;
                    listObjects.AddObject(turno);
                }
                else
                {
                    // Get object on interface list and change attributes
                    turno.horaInicio = horaInicio;
                    turno.horaFim    = horaFim;
                }
                // SHow feedback to user
                String successMessage = "O turno foi adicionado com sucesso!";
                if (edit)
                {
                    successMessage = "O turno foi editado com sucesso";
                }
                MessageBox.Show(
                    successMessage,
                    "Sucesso!",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Information
                    );
                // Update objects displayed on interface
                listObjects.BuildList(true);
                // Update stats
                updateStats();
                // Hide panels
                panelForm.Visible   = false;
                panelObject.Visible = false;
            }
            else
            {
                MessageBox.Show(
                    "Ocorreu um erro, verifique que preencheu todos os dados corretamente e tente novamente!",
                    "Erro!",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Error
                    );
            }
        }
Exemplo n.º 6
0
        private void deleteObject()
        {
            // Get Object
            if (listObjects.Items.Count == 0 | current < 0)
            {
                return;
            }
            Turno f         = (Turno)listObjects.SelectedObjects[0];
            int   itemIndex = listObjects.SelectedIndex;
            // Confirm delete
            DialogResult msgb = MessageBox.Show("Tem a certeza que quer eliminar este turno (" + f.str + ")?", "Esta operação é irreversível!", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation);

            if (msgb == DialogResult.No)
            {
                return;
            }
            // Create command
            String     commandText = "pr_TurnosDELETE";
            SqlCommand command     = new SqlCommand(commandText, cn);

            command.CommandType = CommandType.StoredProcedure;
            // Add vars
            command.Parameters.Add("@Codigo", SqlDbType.Int);
            command.Parameters["@Codigo"].Value = f.codigo;
            command.Parameters.Add("@Feedback", SqlDbType.VarChar, 4000).Direction = ParameterDirection.Output;
            // Return value stuff
            command.Parameters.Add("@ReturnVal", SqlDbType.Int).Direction = ParameterDirection.ReturnValue;

            // Execute query
            int    rowsAffected = 0;
            int    returnValue;
            String returnMessage = "";

            try
            {
                rowsAffected  = command.ExecuteNonQuery();
                returnValue   = (int)command.Parameters["@ReturnVal"].Value;
                returnMessage = (String)command.Parameters["@Feedback"].Value;
                Console.WriteLine(String.Format("rowsAffected {0}", rowsAffected));
            }
            catch (SqlException ex)
            {
                MessageBox.Show(ex.GetType().ToString());
                MessageBox.Show(
                    "Ocorreu um erro, tente novamente!\r\n\r\n" + ex.ToString(),
                    "Erro!",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Error
                    );
                return;
            }
            // If successful query
            if (rowsAffected == 1 && returnValue == 1)
            {
                // Remove object from interface list
                listObjects.Items.RemoveAt(itemIndex);
                // Show user feedback
                MessageBox.Show(
                    "O tuplo foi eliminado com sucesso da base de dados!",
                    "Sucesso!",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Information
                    );
                // Update stats
                updateStats();
                // Hide panels
                panelForm.Visible   = false;
                panelObject.Visible = false;
            }
            else
            {
                String errorMessage = "Ocorreu um erro, tente novamente!";
                if (returnMessage.Contains("conflicted with the REFERENCE constraint \"FK"))
                {
                    errorMessage = "Este turno não pode ser eliminado enquanto estiver atribuído a um funcionário!";
                }
                MessageBox.Show(
                    errorMessage + "\r\n\r\n" + returnMessage,
                    "Erro!",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Error
                    );
            }
        }
Exemplo n.º 7
0
        private void submitForm(NaoDocente ndocente)
        {
            bool edit = (ndocente != null);

            // Get form data
            int    nmec         = Int32.Parse(panelFormFieldNMec.Text);
            String nome         = panelFormFieldNome.Text;
            Double salario      = Double.Parse(panelFormFieldSalario.Text);
            int    telemovel    = Int32.Parse(panelFormFieldContacto.Text);
            String emailPrefixo = panelFormFieldEmail.Text.Split('@')[0];
            String emailDominio = panelFormFieldEmail.Text.Split('@')[1];
            Turno  turno        = getTurno(panelFormFieldTurno.Text);

            // Create command
            String     commandText = "pr_NaoDocentes";
            SqlCommand command     = new SqlCommand(commandText, cn);

            command.CommandType = CommandType.StoredProcedure;
            // Add vars
            command.Parameters.Add("@NMec", SqlDbType.Int);
            command.Parameters["@NMec"].Value = nmec;
            command.Parameters.Add("@Nome", SqlDbType.VarChar);
            command.Parameters["@Nome"].Value = nome;
            command.Parameters.Add("@Telemovel", SqlDbType.Int);
            command.Parameters["@Telemovel"].Value = telemovel;
            command.Parameters.Add("@Email", SqlDbType.VarChar);
            command.Parameters["@Email"].Value = emailPrefixo;
            command.Parameters.Add("@EmailDominio", SqlDbType.VarChar);
            command.Parameters["@EmailDominio"].Value = emailDominio;
            command.Parameters.Add("@Salario", SqlDbType.Money);
            command.Parameters["@Salario"].Value = salario;
            command.Parameters.Add("@Turno", SqlDbType.Int);
            command.Parameters["@Turno"].Value = turno.codigo;
            command.Parameters.Add("@Edit", SqlDbType.Bit);
            command.Parameters["@Edit"].Value = 0;
            if (edit)
            {
                command.Parameters["@Edit"].Value = 1;
            }
            // Return value stuff
            var returnParameter = command.Parameters.Add("@ReturnVal", SqlDbType.Int);

            returnParameter.Direction = ParameterDirection.ReturnValue;
            // Execute query
            int rowsAffected = 0;
            int returnValue;

            try
            {
                rowsAffected = command.ExecuteNonQuery();
                returnValue  = (int)returnParameter.Value;
                Console.WriteLine(String.Format("rowsAffected {0}", rowsAffected));
            }
            catch (SqlException ex)
            {
                String errorMessage = "Ocorreu um erro, verifique que preencheu todos os dados corretamente e tente novamente!";
                for (int i = 0; i < ex.Errors.Count; i++)
                {
                    if (ex.Errors[i].Message.IndexOf("Já existe uma pessoa com o email", StringComparison.OrdinalIgnoreCase) >= 0)
                    {
                        errorMessage = ex.Errors[i].Message;
                        break;
                    }
                    if (ex.Errors[i].Message.IndexOf("Já existe uma pessoa com o número de telemóvel fornecido", StringComparison.OrdinalIgnoreCase) >= 0)
                    {
                        errorMessage = ex.Errors[i].Message;
                        break;
                    }
                }
                MessageBox.Show(
                    errorMessage + "\r\n\r\n" + ex.ToString(),
                    "Erro!",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Error
                    );
                return;
            }
            // If query is successful
            if (returnValue == 1)
            {
                // If add operation, construct object (was null)
                if (!edit)
                {
                    ndocente = new NaoDocente();
                }
                ndocente.nmec      = nmec;
                ndocente.nome      = nome;
                ndocente.email     = emailPrefixo + "@" + emailDominio;
                ndocente.telemovel = telemovel;
                ndocente.salario   = salario;
                ndocente.turno     = turno;
                if (!edit)
                {
                    listObjects.AddObject(ndocente);
                }
                // SHow feedback to user
                String successMessage = "O não docente foi adicionado com sucesso!";
                if (edit)
                {
                    successMessage = "O não docente foi editado com sucesso";
                }
                MessageBox.Show(
                    successMessage,
                    "Sucesso!",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Information
                    );
                // Update objects displayed on interface
                listObjects.BuildList(true);
                // Update stats
                updateStats();
                // Hide panels
                panelForm.Visible   = false;
                panelObject.Visible = false;
            }
            else
            {
                String messageError = "Ocorreu um erro, verifique que preencheu todos os dados corretamente e tente novamente!";
                if (returnValue == -2)
                {
                    messageError = "Já existe uma pessoa na base de dados com esse número mecanográfico!";
                }
                else if (returnValue == -3 || returnValue == -4)
                {
                    messageError = "Ocorreu um erro interno na base de dados! Tente novamente.";
                }
                MessageBox.Show(
                    messageError,
                    "Erro!",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Error
                    );
            }
        }