private void editObject()
        {
            // Get Object
            if (listObjects.Items.Count == 0 | current < 0)
            {
                return;
            }
            NDTrabalhaBloco f = (NDTrabalhaBloco)listObjects.SelectedObjects[0];

            // Set textboxes value
            panelFormFieldBloco.Text      = f.bloco.nome;
            panelFormFieldFuncao.Text     = f.funcao.nome;
            panelFormFieldHoraInicio.Text = f.turno.horaInicio.ToString();
            panelFormFieldHoraFim.Text    = f.turno.horaFim.ToString();
            // Set title and description
            panelFormTitulo.Text    = "Editar função";
            panelFormDescricao.Text = "Altere os dados e submita o formulário";
            panelFormButton.Text    = "Submeter";
            // Block time fields (primary key, not changable)
            panelFormFieldHoraInicio.Enabled = false;
            panelFormFieldHoraFim.Enabled    = false;
            // Make panel visible
            if (!panelForm.Visible)
            {
                panelForm.Visible = true;
            }
        }
        private void FormLoad_Handler(object sender, EventArgs e)
        {
            // Get turnos
            loadBlocos();
            loadBibliotecas();
            loadFuncoes();

            // Execute SQL query to get trabalha_Bloco rows
            SqlCommand    cmd    = new SqlCommand("SELECT * FROM GestaoEscola.ND_trabalha_Bloco WHERE NMec=" + nd.nmec.ToString(), cn);
            SqlDataReader reader = cmd.ExecuteReader();
            // Create list of Objects given the query results
            List <NDTrabalhaBloco> tuplos = new List <NDTrabalhaBloco>();

            while (reader.Read())
            {
                NDTrabalhaBloco d = new NDTrabalhaBloco();
                d.nd     = nd;
                d.funcao = getFuncao(Int32.Parse(reader["codFuncao"].ToString()));
                d.bloco  = getBloco(reader["Bcoordenadas"].ToString());
                Turno t = new Turno();
                t.horaInicio = TimeSpan.Parse(reader["horaInicio"].ToString());
                t.horaFim    = TimeSpan.Parse(reader["horaFim"].ToString());
                d.turno      = t;
                tuplos.Add(d);
            }
            // Close reader
            reader.Close();

            // Execute SQL query to get trabalha_Biblioteca rows
            cmd    = new SqlCommand("SELECT * FROM GestaoEscola.ND_trabalha_Biblioteca WHERE NMec=" + nd.nmec.ToString(), cn);
            reader = cmd.ExecuteReader();
            // Create list of Objects given the query results
            while (reader.Read())
            {
                NDTrabalhaBloco d = new NDTrabalhaBloco();
                d.nd     = nd;
                d.funcao = getFuncao(Int32.Parse(reader["codFuncao"].ToString()));
                d.bloco  = getBlocoByNome("Biblioteca " + reader["biblioteca"].ToString());
                Turno t = new Turno();
                t.horaInicio = TimeSpan.Parse(reader["horaInicio"].ToString());
                t.horaFim    = TimeSpan.Parse(reader["horaFim"].ToString());
                d.turno      = t;
                tuplos.Add(d);
            }
            // Close reader
            reader.Close();

            // ObjectListView
            // Add Objects to list view
            listObjects.SetObjects(tuplos);

            // Update stats
            updateStats();
        }
Example #3
0
 void addToInterface(NDTrabalhaBloco d)
 {
     if (turnoInsideTimeSpan(d.turno, new TimeSpan(00, 00, 00), new TimeSpan(02, 00, 00)))
     {
         horario0002.Text = horario0002.Text + d.turno.str + "\r\n" + d.bloco.nome + ", como " + d.funcao.nome + "\r\n\r\n";
     }
     if (turnoInsideTimeSpan(d.turno, new TimeSpan(02, 00, 00), new TimeSpan(04, 00, 00)))
     {
         horario0204.Text = horario0204.Text + d.turno.str + "\r\n" + d.bloco.nome + ", como " + d.funcao.nome + "\r\n\r\n";
     }
     if (turnoInsideTimeSpan(d.turno, new TimeSpan(04, 00, 00), new TimeSpan(06, 00, 00)))
     {
         horario0406.Text = horario0406.Text + d.turno.str + "\r\n" + d.bloco.nome + ", como " + d.funcao.nome + "\r\n\r\n";
     }
     if (turnoInsideTimeSpan(d.turno, new TimeSpan(06, 00, 00), new TimeSpan(08, 00, 00)))
     {
         horario0608.Text = horario0608.Text + d.turno.str + "\r\n" + d.bloco.nome + ", como " + d.funcao.nome + "\r\n\r\n";
     }
     if (turnoInsideTimeSpan(d.turno, new TimeSpan(08, 00, 00), new TimeSpan(10, 00, 00)))
     {
         horario0810.Text = horario0810.Text + d.turno.str + "\r\n" + d.bloco.nome + ", como " + d.funcao.nome + "\r\n\r\n";
     }
     if (turnoInsideTimeSpan(d.turno, new TimeSpan(10, 00, 00), new TimeSpan(12, 00, 00)))
     {
         horario1012.Text = horario1012.Text + d.turno.str + "\r\n" + d.bloco.nome + ", como " + d.funcao.nome + "\r\n\r\n";
     }
     if (turnoInsideTimeSpan(d.turno, new TimeSpan(12, 00, 00), new TimeSpan(14, 00, 00)))
     {
         horario1214.Text = horario1214.Text + d.turno.str + "\r\n" + d.bloco.nome + ", como " + d.funcao.nome + "\r\n\r\n";
     }
     if (turnoInsideTimeSpan(d.turno, new TimeSpan(14, 00, 00), new TimeSpan(16, 00, 00)))
     {
         horario1416.Text = horario1416.Text + d.turno.str + "\r\n" + d.bloco.nome + ", como " + d.funcao.nome + "\r\n\r\n";
     }
     if (turnoInsideTimeSpan(d.turno, new TimeSpan(16, 00, 00), new TimeSpan(18, 00, 00)))
     {
         horario1618.Text = horario1618.Text + d.turno.str + "\r\n" + d.bloco.nome + ", como " + d.funcao.nome + "\r\n\r\n";
     }
     if (turnoInsideTimeSpan(d.turno, new TimeSpan(18, 00, 00), new TimeSpan(20, 00, 00)))
     {
         horario1820.Text = horario1820.Text + d.turno.str + "\r\n" + d.bloco.nome + ", como " + d.funcao.nome + "\r\n\r\n";
     }
     if (turnoInsideTimeSpan(d.turno, new TimeSpan(20, 00, 00), new TimeSpan(22, 00, 00)))
     {
         horario2022.Text = horario2022.Text + d.turno.str + "\r\n" + d.bloco.nome + ", como " + d.funcao.nome + "\r\n\r\n";
     }
     if (turnoInsideTimeSpan(d.turno, new TimeSpan(22, 00, 00), new TimeSpan(24, 00, 00)))
     {
         horario2224.Text = horario2224.Text + d.turno.str + "\r\n" + d.bloco.nome + ", como " + d.funcao.nome + "\r\n\r\n";
     }
 }
        private void showObject()
        {
            // Get Object
            if (listObjects.Items.Count == 0 | current < 0)
            {
                return;
            }
            NDTrabalhaBloco f = (NDTrabalhaBloco)listObjects.SelectedObjects[0];

            // Set labels values
            panelObjectTitulo.Text    = f.turno.str;
            panelObjectSubtitulo.Text = f.bloco.nome + ", como " + f.funcao.nome;
            // Show panel
            if (!panelObject.Visible)
            {
                panelObject.Visible = true;
            }
            panelForm.Visible = false;
        }
        private void submitForm(NDTrabalhaBloco ndtb)
        {
            /*
             * If submition for edit t!=null
             * If new submition t==null
             */
            bool edit = (ndtb != null);
            // Get form data
            TimeSpan horaInicio = TimeSpan.Parse(panelFormFieldHoraInicio.Text);
            TimeSpan horaFim    = TimeSpan.Parse(panelFormFieldHoraFim.Text);
            Bloco    bloco      = getBlocoByNome(panelFormFieldBloco.Text);
            NDFuncao funcao     = getFuncao(panelFormFieldFuncao.Text);
            // Create command
            bool   biblioteca  = bloco.nome.Substring(0, 5).Contains("Bibli");
            String commandText = "INSERT INTO GestaoEscola.ND_trabalha_Bloco VALUES (@IDBloco, @NMec, @CodFuncao, @HoraInicio, @HoraFim)";

            if (biblioteca)
            {
                commandText = "INSERT INTO GestaoEscola.ND_trabalha_Biblioteca VALUES (@IDBloco, @NMec, @CodFuncao, @HoraInicio, @HoraFim)";
            }
            if (edit)
            {
                if (ndtb.bloco.nome.Substring(0, 5).Equals("Bibli") && !biblioteca || ndtb.bloco.nome.Substring(0, 5).Equals("Bloco") && biblioteca)
                {
                    MessageBox.Show(
                        "Ainda não é possível alterar o local de uma função de um bloco para uma biblioteca e vice-versa. Para fazer esta alteração deve eliminar a função atual e criar uma nova.",
                        "Operação não suportada!",
                        MessageBoxButtons.OK,
                        MessageBoxIcon.Exclamation
                        );
                    return;
                }
                if (!biblioteca)
                {
                    commandText = "UPDATE GestaoEscola.ND_trabalha_Bloco SET Bcoordenadas = @IDBloco, codFuncao = @CodFuncao WHERE NMec = @NMec AND horaInicio = @HoraInicio";
                }
                else
                {
                    commandText = "UPDATE GestaoEscola.ND_trabalha_Biblioteca SET biblioteca = @IDBloco, codFuncao = @CodFuncao WHERE NMec = @NMec AND horaInicio = @HoraInicio";
                }
            }
            SqlCommand command = new SqlCommand(commandText, cn);

            // Add vars
            command.Parameters.Add("@IDBloco", SqlDbType.VarChar);
            if (!biblioteca) // Bloco
            {
                command.Parameters["@IDBloco"].Value = bloco.coordenadas.Trim();
            }
            else // Biblioteca
            {
                command.Parameters["@IDBloco"].Value = bloco.nome.Substring(11, bloco.nome.Length - 11);
            }
            command.Parameters.Add("@NMec", SqlDbType.Int);
            command.Parameters["@NMec"].Value = nd.nmec;
            command.Parameters.Add("@CodFuncao", SqlDbType.Int);
            command.Parameters["@CodFuncao"].Value = funcao.codigo;
            command.Parameters.Add("@HoraInicio", SqlDbType.Time);
            command.Parameters["@HoraInicio"].Value = horaInicio;
            if (!edit)
            {
                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)
            {
                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("A hora de início deve ser menor que a de fim", StringComparison.OrdinalIgnoreCase) >= 0)
                    {
                        errorMessage = ex.Errors[i].Message;
                        break;
                    }
                    else if (ex.Errors[i].Message.IndexOf("O horário da nova função deve estar dentro do turno principal do funcionário", StringComparison.OrdinalIgnoreCase) >= 0)
                    {
                        errorMessage = ex.Errors[i].Message;
                        break;
                    }
                    else if (ex.Errors[i].Message.IndexOf("O horário da nova função colide com o horário de outra função já atribuída", StringComparison.OrdinalIgnoreCase) >= 0)
                    {
                        errorMessage = ex.Errors[i].Message;
                        break;
                    }
                    else if (ex.Errors[i].Message.IndexOf("Ocorreu um erro interno à base de dados", 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 (!edit && rowsAffected == 2 || edit && rowsAffected == 1)
            {
                // If add operation
                if (!edit)
                {
                    // Add tuple to interface list
                    ndtb                  = new NDTrabalhaBloco();
                    ndtb.bloco            = bloco;
                    ndtb.nd               = nd;
                    ndtb.funcao           = funcao;
                    ndtb.turno            = new Turno();
                    ndtb.turno.horaInicio = horaInicio;
                    ndtb.turno.horaFim    = horaFim;
                    listObjects.AddObject(ndtb);
                }
                else
                {
                    // Get object on interface list and change attributes
                    ndtb.funcao = funcao;
                    ndtb.bloco  = bloco;
                }
                // SHow feedback to user
                String successMessage = "A função foi adicionada com sucesso ao funcionário!";
                if (edit)
                {
                    successMessage = "A função foi editada com sucesso no funcionário";
                }
                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
                    );
            }
        }
        private void deleteObject()
        {
            // Get Object
            if (listObjects.Items.Count == 0 | current < 0)
            {
                return;
            }
            NDTrabalhaBloco f         = (NDTrabalhaBloco)listObjects.SelectedObjects[0];
            int             itemIndex = listObjects.SelectedIndex;
            // Confirm delete
            DialogResult msgb = MessageBox.Show("Tem a certeza que quer eliminar esta função?", "Esta operação é irreversível!", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation);

            if (msgb == DialogResult.No)
            {
                return;
            }
            // Create command
            String commandText = "DELETE FROM GestaoEscola.ND_trabalha_Bloco WHERE NMec = @NMec AND horaInicio = @HoraInicio";

            if (f.bloco.nome.Substring(0, 5).Equals("Bibli"))
            {
                commandText = "DELETE FROM GestaoEscola.ND_trabalha_Biblioteca WHERE NMec = @NMec AND horaInicio = @HoraInicio";
            }
            SqlCommand command = new SqlCommand(commandText, cn);

            // Add vars
            command.Parameters.Add("@NMec", SqlDbType.Int);
            command.Parameters["@NMec"].Value = nd.nmec;
            command.Parameters.Add("@HoraInicio", SqlDbType.Time);
            command.Parameters["@HoraInicio"].Value = f.turno.horaInicio;
            // Execute query
            int rowsAffected = 0;

            try
            {
                rowsAffected = command.ExecuteNonQuery();
                Console.WriteLine(String.Format("rowsAffected {0}", rowsAffected));
            }
            catch (SqlException ex)
            {
                MessageBox.Show(
                    "Ocorreu um erro, tente novamente!\r\n" + ex.ToString(),
                    "Erro!",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Error
                    );
                return;
            }
            // If successful query
            if (rowsAffected == 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
            {
                MessageBox.Show(
                    "Ocorreu um erro, tente novamente!",
                    "Erro!",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Error
                    );
            }
        }