package Actions;

import DAO.EtudiantDAO;
import Entities.Etudiant;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import org.apache.struts2.ServletActionContext;
import javax.servlet.http.HttpServletRequest;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Hajar on 24/01/2017.
 */
public class ActionEtudiant extends ActionSupport implements ModelDriven<Etudiant> {
    private Etudiant etudiant = new Etudiant();
    private EtudiantDAO dao = new EtudiantDAO();
    private List<Etudiant> listeEtudiants = new ArrayList<>();
    private InputStream inputStream;
    HttpServletRequest request = ServletActionContext.getRequest();

    public Etudiant getEtudiant() {
        return etudiant;
    }

    public void setEtudiant(Etudiant etudiant) {
        this.etudiant = etudiant;
    }

    public EtudiantDAO getDao() {
        return dao;
    }

    public void setDao(EtudiantDAO dao) {
        this.dao = dao;
    }

    public List<Etudiant> getListeEtudiants() {
        return listeEtudiants;
    }

    public void setListeEtudiants(List<Etudiant> listeEtudiants) {
        this.listeEtudiants = listeEtudiants;
    }

    public InputStream getInputStream() {
        return inputStream;
    }

    public void setInputStream(InputStream inputStream) {
        this.inputStream = inputStream;
    }

    public String add() {
        Gson gson = new Gson();
        etudiant = gson.fromJson(request.getParameter("newEtudiant"), Etudiant.class);
        dao.save(etudiant);
        return SUCCESS;
    }

    public String edit() {
        etudiant = dao.getEtudiant(request.getParameter("id"));

        Gson gson = new GsonBuilder()
                .excludeFieldsWithModifiers(Modifier.FINAL, Modifier.TRANSIENT, Modifier.STATIC)
                .serializeNulls()
                .create();

        String json = gson.toJson(etudiant);

        inputStream = new ByteArrayInputStream(json.getBytes());
        return SUCCESS;
    }

    public String list() {
        listeEtudiants = dao.listAll();
        String json = new Gson().toJson(listeEtudiants);
        inputStream = new ByteArrayInputStream(json.getBytes());
        return SUCCESS;
    }

    public String delete() {
        dao.delete(request.getParameter("id"));
        return SUCCESS;
    }

    public String affect() {

        dao.processAffected();
        listeEtudiants = dao.listByNote();
        String json = new Gson().toJson(listeEtudiants);
        inputStream = new ByteArrayInputStream(json.getBytes());
        return SUCCESS;
    }
    public String sortedList() {

        listeEtudiants = dao.listByNote();
        String json = new Gson().toJson(listeEtudiants);
        inputStream = new ByteArrayInputStream(json.getBytes());
        return SUCCESS;
    }

    @Override
    public Etudiant getModel() {
        return etudiant;
    }
}

@@ -35,16 +35,36 @@
 antDAO;%0A
+import DAO.UserDAO;%0A
 import E
@@ -81,16 +81,16 @@
 udiant;%0A
-
 import c
@@ -150,54 +150,8 @@
 er;%0A
-import com.opensymphony.xwork2.ActionContext;%0A
 impo
@@ -284,16 +284,17 @@
 ontext;%0A
+%0A
 import j
@@ -655,24 +655,67 @@
 Etudiant();%0A
+    private UserDAO daoUser=new UserDAO();%0A
     private 
@@ -1749,24 +1749,101 @@
 s);%0A        
+if(etudiant.getNote()==0)%0A        dao.importSansNote(etudiant);%0A        else 
 dao.save(etu
@@ -2632,32 +2632,32 @@
 SUCCESS;%0A    %7D%0A%0A
+
     public Strin
@@ -2665,25 +2665,24 @@
  affect() %7B%0A
-%0A
         dao.

update actionEtudiant