Package Modele

Source Code of Modele.FeuilleNote

package Modele;

import Config.NoteJDOM;
import Crypto.Certificat;
import Crypto.RSA;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.security.PrivateKey;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.LinkedList;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import javax.mail.MessagingException;
import javax.mail.internet.AddressException;

/**
* Classe FeuilleNote.
*
*
* @author Tiphaine TEYSSIER
* @author Tan Phong HUYNH
*
*
*/
public class FeuilleNote {

        private LinkedList<EleveNote> noteEleves = new LinkedList<EleveNote>();

        public LinkedList<EleveNote> getNoteEleves() {
                return noteEleves;
        }
        private String message = "";
        private String path;

        public String getPath() {
                return path;
        }

        /**
         * Instanci une feuille de note a partir d'un fichier
         *
         * @param path
         */
        public FeuilleNote(String path) {
                this.path = path;


                // defini la ligne ou les donné commence
                final int START_AT = 1;
                int i = 0;
                try {
                        BufferedReader br = new BufferedReader(new FileReader(path));
                        String line;
                        String[] split;
                        while ((line = br.readLine()) != null) {
                                i++;
                                if (i < START_AT) {
                                        continue;
                                }
                                split = line.split(";");

                                noteEleves.add(new EleveNote(
                                        new Eleve(split[0].replaceAll("\"", ""),
                                        split[1].replaceAll("\"", ""),
                                        split[2].replaceAll("\"", ""),
                                        split[3].replaceAll("\"", ""),
                                        null),
                                        split[4].replaceAll("\"", ""),
                                        Boolean.valueOf(split[5].replaceAll("\"", ""))));
                        }
                        br.close();
                } catch (IOException ex) {
                        Logger.getLogger(Promotion.class.getName()).log(Level.SEVERE, null, ex);
                }
        }

        /**
         * Instanci une feuille a partir d'une promo
         *
         * @param promo
         */
        public FeuilleNote(Promotion promo) {
                for (Eleve eleve : promo.getEleves()) {
                        noteEleves.add(new EleveNote(eleve, null, false));
                }
        }

        /**
         * Ajoute une note a un eleve La note est verifier et si valide
         * sauvegardé sinon non modifier
         *
         * @param eleve
         * @param note
         */
        public void setNote(Eleve eleve, String note) {
                for (EleveNote eleveNote : noteEleves) {
                        if (eleveNote.getEleve().getNum().equals(eleve.getNum())) {
                                eleveNote.setNote(note);
                                break;
                        }
                }
        }

        /**
         *
         * @param message
         */
        public void setMessage(String message) {
                this.message = message;
        }

        /**
         * Envoie l'archive contenant les notes crypté par email à tous les
         * eleve coché
         *
         * s'ocupe de crypté et de faire l'archive
         */
        public void sendNotes() throws MessagingException {
                String pathZIP = null;
                try {
                        Email email;
                        String pathnote = null;
                        boolean forsend = false; // Permet juste d'envoyer si il y a au moin 1 destinataire

                        String subjectemail = "Nouvelle note";
                        String messageemail = "Bonjour, en pièce jointe votre note consultable avec le logiciel CNote 20/20 : https://sourceforge.net/projects/cnote2020/files/App%20etudiant/ \n\n Message de l'expéditeur : "+getMessage();
                        pathZIP = createZip("notes_" + Calendar.getInstance().getTimeInMillis() + ".zip");
                        email = new Email();
                        email.setSubject(subjectemail);
                        email.setMessage(messageemail);
                        email.addPJ(pathZIP);
                        for (EleveNote eleveNote : noteEleves) {
                                // Si on doit lui envoyer
                                if (eleveNote.getEnvoie()) {
                                        forsend = true;
                                        email.addDestinataireBCC(eleveNote.getEleve().getEmail());
                                }
                        }
                        if (forsend) {
                                email.send();
                        }

                } catch (IOException ex) {
                        Logger.getLogger(FeuilleNote.class.getName()).log(Level.SEVERE, null, ex);
                } catch (AddressException ex) {
                        Logger.getLogger(FeuilleNote.class.getName()).log(Level.SEVERE, null, ex);
                } finally{
                        new File(pathZIP).delete();
                }

        }

        /**
         * Creer un fichier note pour un eleve
         *
         * crypte la note la signe et creer le fichier
         *
         * @param e
         * @return le chemin du fichier
         */
        private String createNoteFile(EleveNote e, String path) {

               
                String certif = null; //
                String note_crypte = null;
                String signature = null; // On signe : note crypté + message + certif

                certif = Certificat.X509CertImplToHex(Certificat.readX509CertFromFile(Config.Config.getInstance().getPathCertificat()));
                try {
                        if(e.getNote() == null || e.getNote().isEmpty()){
                                e.setNote(""); // rajoute une note vide dans l'absolu metre stoper l'envoie metre un message a l'utilisateur
                        }
                        note_crypte = Crypto.AES.encrypt(e.getNote(), e.getEleve().getNum());
                } catch (Exception ex) {
                        Logger.getLogger(FeuilleNote.class.getName()).log(Level.SEVERE, null, ex);
                }
                signature = RSA.sign(note_crypte + message + certif, (PrivateKey) RSA.readKeyFromFile(Config.Config.getInstance().getPathPrivateKey()));

                NoteJDOM notefile = new NoteJDOM(path);
                notefile.addNote(certif, message, note_crypte.toString(), signature);
                notefile.save();


                return path;
        }

        /**
         * Creer le zip de toutes les notes crypté de la feuille
         *
         * @param zippathname
         * @return le chemin de l'archive
         */
        public String createZip(String zippathname) {
                ArrayList fs = new ArrayList<File>(noteEleves.size());
                for (EleveNote e : noteEleves) {
                        if (e.getEnvoie()) {
                                fs.add(new File(createNoteFile(e, e.getEleve().getNom() + "_" + e.getEleve().getPrenom()+ ".note")));
                        }
                }
                return createZip(fs, zippathname);
        }

        private String createZip(ArrayList<File> tf, String zippathname) {
                try {
                        // Create a buffer for reading the files
                        byte[] buf = new byte[1024];
                        // Create the ZIP file         
                        ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zippathname));

                        // Compress the files
                        for (int i = 0; i < tf.size(); i++) {
                                FileInputStream in = new FileInputStream(tf.get(i));

                                // Add ZIP entry to output stream.
                                out.putNextEntry(new ZipEntry(tf.get(i).getName()));

                                // Transfer bytes from the file to the ZIP file
                                int len;
                                while ((len = in.read(buf)) > 0) {
                                        out.write(buf, 0, len);
                                }

                                // Complete the entry
                                out.closeEntry();
                                in.close();
                        }

                        // Complete the ZIP file
                        out.close();
                        for (File file : tf) {
                                file.delete();
                        }
                        return zippathname;
                } catch (IOException ex) {
                        Logger.getLogger(FeuilleNote.class.getName()).log(Level.SEVERE, null, ex);
                }

                return null;


        }

        /**
         *
         * Sauvegarde la feuille de note sous format csv
         *
         * @param path
         *
         */
        public void save(String path) {
                this.path = path;
                PrintWriter out;
                try {
                        out = new PrintWriter(new BufferedWriter(new FileWriter(path)));
                        for (EleveNote eleveNote : noteEleves) {
                                out.println(
                                        "\"" + eleveNote.getEleve().getNom() + "\";"
                                        + "\"" + eleveNote.getEleve().getPrenom() + "\";"
                                        + "\"" + eleveNote.getEleve().getEmail() + "\";"
                                        + "\"" + eleveNote.getEleve().getNum() + "\";"
                                        + "\"" + eleveNote.getNote() + "\";"
                                        + "\"" + eleveNote.getEnvoie() + "\";");
                        }
                        out.flush();
                        out.close();
                } catch (IOException ex) {
                        Logger.getLogger(FeuilleNote.class.getName()).log(Level.SEVERE, null, ex);
                }
        }

        public String getMessage() {
                return this.message;
        }
}
TOP

Related Classes of Modele.FeuilleNote

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.