gestionsessions.FenSession.java Source code

Java tutorial

Introduction

Here is the source code for gestionsessions.FenSession.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package gestionsessions;

import com.itextpdf.text.*;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import java.sql.*;
import sql.GestionBdd;
import java.io.*;
import java.net.InetAddress;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author Valoo22
 */
public class FenSession extends javax.swing.JDialog {
    Connection conn;
    Statement stmt1, stmt2;

    /**
     * Creates new form FenSession
     */
    public FenSession() {
        initComponents();
        renseigne();
    }

    private void renseigne() {
        try {
            // On vide le JTable
            int j, k;
            String req;
            for (int i = 0; i < jTableGestionSession.getRowCount(); i++) {
                for (j = 0; j < jTableGestionSession.getColumnCount(); j++) {
                    jTableGestionSession.setValueAt(null, i, j);
                }
            }
            // Toutes les sessions
            req = "select * from viewdetailsession v";
            // et date suprieure  la date du jour
            req += " where v.date_debut > CURRENT_DATE ORDER BY v.date_debut";
            stmt1 = GestionBdd.connexionBdd(GestionBdd.TYPE_MYSQL, "formarmor", "localhost", "root", "");
            ResultSet rs2 = GestionBdd.envoiRequeteLMD(stmt1, req);
            ResultSet rs3 = null;
            k = 0;
            try {
                while (rs2.next()) {
                    // On calcule la marge
                    req = "Select sum(taux_horaire) as revenu_session from statut st, session_formation s, client c, inscription i where s.id = i.session_formation_id and c.id = i.client_id and c.statut_id = st.id and s.id = "
                            + rs2.getInt(1);
                    stmt2 = GestionBdd.connexionBdd(GestionBdd.TYPE_MYSQL, "formarmor", "localhost", "root", "");
                    rs3 = GestionBdd.envoiRequeteLMD(stmt2, req);
                    rs3.first();
                    jTableGestionSession.setValueAt((rs3.getFloat(1) - rs2.getFloat(13)), k, 9);
                    // On renseigne le jTable
                    jTableGestionSession.setValueAt(rs2.getInt(1), k, 0);
                    jTableGestionSession.setValueAt(rs2.getString(7), k, 1);
                    jTableGestionSession.setValueAt(rs2.getString(8), k, 2);
                    jTableGestionSession.setValueAt(rs2.getString(9), k, 3);
                    jTableGestionSession.setValueAt(rs2.getString(10), k, 4);
                    String diplomante;
                    if (rs2.getInt("diplomante") == 1) {
                        diplomante = "Oui";
                    } else {
                        diplomante = "Non";
                    }
                    jTableGestionSession.setValueAt(diplomante, k, 5);
                    jTableGestionSession.setValueAt(rs2.getObject(3), k, 6);
                    jTableGestionSession.setValueAt(rs2.getInt(12), k, 7);
                    jTableGestionSession.setValueAt(rs2.getInt(4), k, 8);
                    k++;
                }
                rs2.close();
            } catch (SQLException sqle) {
                System.out.print("Erreur SQL : " + sqle.getMessage());
            }
        } catch (NullPointerException npe) {
            System.out.println("Erreur : " + npe.getMessage());
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTableGestionSession = new javax.swing.JTable();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        cmbItems = new javax.swing.JComboBox();
        lblTypeFormation = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Gestions des Sessions");

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 30)); // NOI18N
        jLabel1.setText("Gestion des Sessions");
        jLabel1.setFocusable(false);
        jLabel1.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);

        jTableGestionSession.setModel(new ModeleJTableListeSession());
        jTableGestionSession.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTableGestionSessionMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jTableGestionSession);

        jButton1.setText("Retour au Menu");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Quitter");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        cmbItems.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Tous", "Bureautique", "Compta" }));
        cmbItems.setName("cmbItems"); // NOI18N
        cmbItems.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmbItemsActionPerformed(evt);
            }
        });

        lblTypeFormation.setText("Slectionner le type de formation: ");
        lblTypeFormation.setToolTipText("");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup().addContainerGap()
                                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 825,
                                        Short.MAX_VALUE)
                                .addContainerGap())
                        .addGroup(layout.createSequentialGroup().addGap(129, 129, 129)
                                .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 181,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,
                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 181,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(136, 136, 136))
                        .addGroup(layout.createSequentialGroup()
                                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(layout.createSequentialGroup().addComponent(lblTypeFormation)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(cmbItems, javax.swing.GroupLayout.PREFERRED_SIZE, 125,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                                        .addComponent(jLabel1))
                                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));
        layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup().addGap(20, 20, 20).addComponent(jLabel1).addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(cmbItems, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(lblTypeFormation))
                        .addGap(24, 24, 24)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 269,
                                javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(29, 29, 29)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jButton2).addComponent(jButton1))
                        .addGap(25, 25, 25)));

        cmbItems.getAccessibleContext().setAccessibleName("cmbItems");
        cmbItems.getAccessibleContext().setAccessibleDescription("");
        lblTypeFormation.getAccessibleContext().setAccessibleName("lblTypeFormation");

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jTableGestionSessionMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTableGestionSessionMouseClicked
        Document document = new Document();
        String req;
        int nbLignes = 0;
        // Creation d'une PdfPTable avec 5 colonnes
        final PdfPTable table = new PdfPTable(5);
        try {
            PdfWriter.getInstance(document,
                    new FileOutputStream(System.getProperty("user.dir") + "//listeEmargementSession.pdf"));
            document.open();
            //en tte du pdf
            Image logo = Image.getInstance("logo.png");
            logo.scaleAbsolute(120f, 67f);
            document.add(logo);
            //on indique la marge
            document.add(new Phrase("\n\n\nMarge dgage par la session: "
                    + jTableGestionSession.getValueAt(jTableGestionSession.getSelectedRow(), 9).toString()
                    + ""));
            document.add(new Phrase("\n\nListe d'margement de la session "));
            document.add(new Phrase(
                    jTableGestionSession.getValueAt(jTableGestionSession.getSelectedRow(), 0).toString()));
            document.add(new Phrase(
                    " / " + jTableGestionSession.getValueAt(jTableGestionSession.getSelectedRow(), 1).toString()));
            document.add(new Phrase(
                    " / " + jTableGestionSession.getValueAt(jTableGestionSession.getSelectedRow(), 2).toString()));
            document.add(new Phrase(" / "
                    + jTableGestionSession.getValueAt(jTableGestionSession.getSelectedRow(), 3).toString() + ":"));
            // Ajout d'un espace entre la PdfPTable et l'lment prcdent.
            table.setSpacingBefore(15f);
            //requte pour les personnes inscrites
            stmt1 = GestionBdd.connexionBdd(GestionBdd.TYPE_MYSQL, "formarmor", "localhost", "root", "");
            req = "Select c.* from client c, inscription i where c.id = i.client_id and i.session_formation_id = "
                    + jTableGestionSession.getValueAt(jTableGestionSession.getSelectedRow(), 0).toString();
            ResultSet rs1 = GestionBdd.envoiRequeteLMD(stmt1, req);
            // Ajout d'une entte pour PdfPTable
            table.addCell("Matricule");
            table.addCell("Nom");
            table.addCell("Rue");
            table.addCell("Code Postal");
            table.addCell("Ville");
            // Ajout d'un espace entre la PdfPTable et l'lment prcdent.
            table.setSpacingBefore(15f);
            table.setWidthPercentage(100);
            table.setSpacingBefore(0f);
            table.setSpacingAfter(0f);
            //change taille colonnes
            table.setWidths(new int[] { 5, 10, 14, 6, 8 });
            while (rs1.next()) {
                // Ajout d'objets String  la PdfPTable
                table.addCell(rs1.getString(1));
                table.addCell(rs1.getString(3));
                table.addCell(rs1.getString(5));
                table.addCell(rs1.getString(6));
                table.addCell(rs1.getString(7));
                // Ajout d'un espace entre la PdfPTable et l'lment prcdent.
                table.setSpacingBefore(15f);
                nbLignes++;
            }
            //cas ou il n'y a personne d'inscrit
            if (nbLignes == 0) {
                JOptionPane.showMessageDialog(null, "Il n'y a personne d'inscrits");
                document.add(new Phrase("\n\nIl n'y a personne d'inscrits"));
            } else {
                document.add(table);
                //ouvre le fichier listeEmargementSession.pdf avec le logiciel de lecture pdf par dfaut 
                Runtime r = Runtime.getRuntime();
                r.exec(new String[] { "cmd.exe", "/C",
                        System.getProperty("user.dir") + "//listeEmargementSession.pdf" });
            }
            document.close();
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException ex) {
            Logger.getLogger(FenSession.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(FenSession.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_jTableGestionSessionMouseClicked

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        this.dispose();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        System.exit(0);
    }//GEN-LAST:event_jButton2ActionPerformed

    private void cmbItemsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmbItemsActionPerformed
        try {
            // On vide le JTable
            int j, k;
            String req;
            for (int i = 0; i < jTableGestionSession.getRowCount(); i++) {
                for (j = 0; j < jTableGestionSession.getColumnCount(); j++) {
                    jTableGestionSession.setValueAt(null, i, j);
                }
            }
            // Toutes les sessions
            req = "Select * from viewdetailsession v ";
            // et date suprieure  la date du jour
            req += "where v.date_debut > CURDATE() ";
            if (cmbItems.getSelectedItem().toString() != "Tous") {
                req += "and v.type_form = '" + cmbItems.getSelectedItem().toString() + "'";
            }
            req += " order by v.date_debut";
            stmt1 = GestionBdd.connexionBdd(GestionBdd.TYPE_MYSQL, "formarmor", "localhost", "root", "");
            ResultSet rs2 = GestionBdd.envoiRequeteLMD(stmt1, req);
            ResultSet rs3 = null;
            k = 0;
            try {
                while (rs2.next()) {
                    // On calcule la marge
                    req = "Select sum(taux_horaire) as revenu_session from statut st, session_formation s, client c, inscription i where s.id = i.session_formation_id and c.id = i.client_id and c.statut_id = st.id and s.id = "
                            + rs2.getInt(1);
                    stmt2 = GestionBdd.connexionBdd(GestionBdd.TYPE_MYSQL, "formarmor", "localhost", "root", "");
                    rs3 = GestionBdd.envoiRequeteLMD(stmt2, req);
                    rs3.first();
                    jTableGestionSession.setValueAt((rs3.getFloat(1) - rs2.getFloat(13)), k, 9);
                    // On renseigne le jTable
                    jTableGestionSession.setValueAt(rs2.getInt(1), k, 0);
                    jTableGestionSession.setValueAt(rs2.getString(7), k, 1);
                    jTableGestionSession.setValueAt(rs2.getString(8), k, 2);
                    jTableGestionSession.setValueAt(rs2.getString(9), k, 3);
                    jTableGestionSession.setValueAt(rs2.getString(10), k, 4);
                    String diplomante;
                    if (rs2.getInt("diplomante") == 1) {
                        diplomante = "Oui";
                    } else {
                        diplomante = "Non";
                    }
                    jTableGestionSession.setValueAt(diplomante, k, 5);
                    jTableGestionSession.setValueAt(rs2.getObject(3), k, 6);
                    jTableGestionSession.setValueAt(rs2.getInt(12), k, 7);
                    jTableGestionSession.setValueAt(rs2.getInt(4), k, 8);
                    k++;
                }
                rs2.close();
            } catch (SQLException sqle) {
                System.out.print("Erreur SQL : " + sqle.getMessage());
            }
        } catch (NullPointerException npe) {
            System.out.println("Erreur : " + npe.getMessage());
        }
    }//GEN-LAST:event_cmbItemsActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox cmbItems;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTableGestionSession;
    private javax.swing.JLabel lblTypeFormation;
    // End of variables declaration//GEN-END:variables
}