1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
| package controller;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.ManagedBean;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.enterprise.context.SessionScoped;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.event.ValueChangeEvent;
import javax.inject.Named;
import javax.servlet.http.Part;
import org.apache.poi.EncryptedDocumentException;
import com.fasterxml.jackson.databind.exc.InvalidFormatException;
import entities.Etudiant;
import entities.NoteEtudiant;
import entities.Seminaire;
import sessionejb.GestionEtudiantEJB;
import sessionejb.GestionNoteEtudiantEJB;
import sessionejb.GestionSeminaireEJB;
import util.ExcelReader;
@Named
@SessionScoped
public class EtudiantController implements Serializable {
/**
*
*/
private int tmpId = 0;
private boolean loadFile = false;
private String comboSection = null;
private static final long serialVersionUID = 1L;
@EJB
private GestionEtudiantEJB gestionEtudiant;
@EJB
private GestionNoteEtudiantEJB gestionNote;
private List<Etudiant> etudiants = new ArrayList<Etudiant>();
private Etudiant etudiant;
private String section;
public EtudiantController() {}
@PostConstruct
public void init() {
etudiant = new Etudiant();
note = new NoteEtudiant();
}
public Etudiant getEtudiant() {
if(etudiant == null) {
init();
}
return etudiant;
}
public void setEtudiant(Etudiant etudiant) {
this.etudiant = etudiant;
}
public List<Etudiant> getEtudiantsSection(String section) {
etudiants = gestionEtudiant.selectAllEtudiantSection(section);
return etudiants;
}
private Part file;
public void save() throws InvalidFormatException, EncryptedDocumentException, org.apache.poi.openxml4j.exceptions.InvalidFormatException, IOException {
File cFile = new File(file.getSubmittedFileName());
try {
cFile.createNewFile();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try (InputStream input = file.getInputStream()) {
Files.copy(input, cFile.toPath(),StandardCopyOption.REPLACE_EXISTING);
}
catch (IOException e) {
e.printStackTrace();
}
/* for(Etudiant e: etudiants) {
gestionEtudiant.deleteEtudiant(e);
}*/
gestionEtudiant.deleteAll();
etudiants.clear();
try {
etudiants = ExcelReader.getEtudiants(cFile);
for(Etudiant e : etudiants) {
gestionEtudiant.addEtudiant(e);
}
loadFile = true;
}catch(Exception e) {
loadFile = false;
}
}
public void messageLoadFile() {
if(loadFile) {
FacesContext context = FacesContext.getCurrentInstance();
context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Liste ajoutée", null));
}else {
FacesContext context = FacesContext.getCurrentInstance();
context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Fichier non correct !", null));
}
}
public Part getFile() {
return file;
}
public void setFile(Part file) {
this.file = file;
}
public String getSection() {
return section;
}
public String setSection(String section) {
this.section = section;
return "ListeEtudiant.xhtml?faces-redirect=true";
}
} |
Partager