package controller;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import manager.FolderManager;
import org.springframework.web.bind.RequestUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;
import util.MailSenDer;
import domain.Advancement;
import domain.Etat;
import domain.Folder;
import domain.Formation;
import domain.Personnel;
import domain.Responsable;
public class ResponsableCTRL extends AbstractController {
private FolderManager foldermanager;
public FolderManager getFoldermanager() {
return foldermanager;
}
public void setFoldermanager(FolderManager foldermanager) {
this.foldermanager = foldermanager;
}
@Override
protected ModelAndView handleRequestInternal(HttpServletRequest request,
HttpServletResponse response) throws Exception {
String method = request.getMethod();
ModelAndView mav = null;
if (method.equals("POST"))
mav = handlePost(request, response);
else if (method.equals("GET"))
mav = handleGet(request, response);
return mav;
}
@SuppressWarnings("unchecked")
private ModelAndView handleGet(HttpServletRequest request,
HttpServletResponse response) {
HttpSession session = request.getSession(false);
if(session == null){
return new ModelAndView("logout-personnel");
}
Personnel p = (Responsable)session.getAttribute("responsable");
if(p==null)
return new ModelAndView("logout-personnel");
String link = null;
String message = null;
ModelAndView mav = null;
Map model = new HashMap();
String filename=null;
String action = request.getParameter("action");
String sId = request.getParameter("id");
int id ;
List page = new ArrayList();
if (action == null) {
page.add("Accueil "+p.getFormation().getLabel());
model.put("page", page);
Integer admis = foldermanager.getAdvancementdao().getCountAdvancementByStateAndFormation(Folder.FOLDER_ACCEPTED, p.getFormation().getId());
model.put("countadmis", admis);
Integer complementary = foldermanager.getAdvancementdao().getCountAdvancementByStateAndFormation(Folder.FOLDER_COMPLEMENTARY, p.getFormation().getId());
model.put("countcomplementary", complementary);
Integer interviews = foldermanager.getAdvancementdao().getCountAdvancementByStateAndFormation(Folder.FOLDER_INTERVIEW, p.getFormation().getId());
model.put("countinterview", interviews);
Integer refused = foldermanager.getAdvancementdao().getCountAdvancementByStateAndFormation(Folder.FOLDER_REFUSED, p.getFormation().getId());
model.put("countrefused", refused);
mav = new ModelAndView("responsable",model);
}else if(action.equals("logout")){
return new ModelAndView("logout-personnel");
} else {
if (action.equals("choix1")){
page.add(p.getFormation().getLabel()+" : Liste des premiers choix.");
model.put("page", page);
List choix1 = foldermanager.findPersonalInfoByChoice(p.getFormation().getId(), 1);
model.put("folders", choix1);
link = "linkNotTreated";
}else if(action.equals("choix2")){
page.add(p.getFormation().getLabel()+" : Liste des seconds choix.");
model.put("page", page);
List choix2 = foldermanager.findPersonalInfoByChoice(p.getFormation().getId(), 2);
model.put("folders", choix2);
link = "linkNotTreated";
}else if(action.equals("choix3")){
page.add(p.getFormation().getLabel()+" : Liste des troisiemes choix.");
model.put("page", page);
List choix3 = foldermanager.findPersonalInfoByChoice(p.getFormation().getId(), 3);
model.put("folders", choix3);
link = "linkNotTreated";
}else if(action.equals("admis")){
page.add(p.getFormation().getLabel()+" : Liste des �tudiants admis");
model.put("page", page);
List admis = foldermanager.findByStateAndFormation(Folder.FOLDER_ACCEPTED, p.getFormation());//findPersonalInfoByChoice(1, 1);
link = "linkAccepted";
filename="Admis";
model.put("admis", admis);
}else if(action.equals("interview")){
page.add(p.getFormation().getLabel()+" : Liste des �tudiants en entretient.");
model.put("page", page);
List interviews = foldermanager.findByStateAndFormation(Folder.FOLDER_INTERVIEW, p.getFormation());//findPersonalInfoByChoice(1, 1);
model.put("entretiens", interviews);
link = "linkInterview";
filename="Entretiens";
}else if(action.equals("attente")){
page.add(p.getFormation().getLabel()+" : Liste des �tudiants sur liste compl�mentaire.");
model.put("page", page);
List attente = foldermanager.findByStateAndFormation(Folder.FOLDER_COMPLEMENTARY, p.getFormation());//findPersonalInfoByChoice(1, 1);
model.put("complementary", attente);
link = "linkComplement";
filename="ListeComplementaire";
}else if(action.equals("refused")){
page.add(p.getFormation().getLabel()+" : Liste des �tudiants refus�s.");
model.put("page", page);
List refused = foldermanager.findByStateAndFormation(Folder.FOLDER_REFUSED, p.getFormation());//findPersonalInfoByChoice(1, 1);
model.put("refused", refused);
link = "linkRefused";
filename="Refuses";
}else if (action.equals("edit")){
id = new Integer(sId);
Folder folder = foldermanager.findFolderById(id);
page.add(p.getFormation().getLabel()+" : D�tails pour "+folder.getPi().getLastname()+" "+folder.getPi().getFirstname());
model.put("page",page);
model.put("folder", folder);
}else if (action.equals("admit")){
id = new Integer(sId);
Etat etat = new Etat();
etat.setState(Folder.FOLDER_ACCEPTED);
this.foldermanager.storeAdvancement(id,etat, p.getFormation());
// foldermanager.updatePersonalInfoState(id, Folder.FOLDER_TREATED);
message = "Le dossier numero "+id+" a ete admit en liste principale.";
page.add("Confirmation");
model.put("page",page);
}else if (action.equals("complement")){
id = new Integer(sId);
Etat etat = new Etat();
etat.setState(Folder.FOLDER_COMPLEMENTARY);
// A MODIFIER
//etat.setComplemantary_pos(1);
this.foldermanager.storeAdvancement(id, etat, p.getFormation());
// foldermanager.updatePersonalInfoState(id, Folder.FOLDER_TREATED);
page.add("Confirmation");
model.put("page",page);
message = "Le dossier numero "+id+" a ete admit en liste complementaire.";
}else if (action.equals("tointerview")){
id = new Integer(sId);
Etat etat = new Etat();
etat.setState(Folder.FOLDER_INTERVIEW);
// A MODIFIER
etat.setInterview("demain");
this.foldermanager.storeAdvancement(id, etat, p.getFormation());
// foldermanager.updatePersonalInfoState(id, Folder.FOLDER_TREATED);
page.add("Confirmation");
model.put("page",page);
message = "Le dossier num�rot� "+id+" a ete convoquee en entretien.";
}else if (action.equals("refuse")){
id = new Integer(sId);
Etat etat = new Etat();
etat.setState(Folder.FOLDER_REFUSED);
this.foldermanager.storeAdvancement(id, etat, p.getFormation());
// foldermanager.updatePersonalInfoState(id, Folder.FOLDER_TREATED);
page.add("Confirmation");
model.put("page",page);
message = "Le dossier numero "+id+" a ete refuse.";
/* The page of a secretary displaying the processus of change of the password */
} else if (action.equals("changepass")) {
page.add("Changement du mot de passe");
model.put("page", page);
model.put("personnel", p);
model.put("changepass", "changepass");
return new ModelAndView("responsable", model);
}else if(action.equals("retirer")){
id = new Integer(sId);
int res = foldermanager.removeAdvancement(id, p.getFormation().getId());
if(res == 1){
page.add("Confirmation");
model.put("page",page);
message = "Le dossier numero "+id+" a ete retire de la liste principale.";
}else{
page.add("Echec");
model.put("page",page);
message = "Le dossier numero "+id+" n'a pas pu etre retire de la liste principale.";
}
}
model.put("link", link);
model.put("message", message);
model.put("filename", filename);
}
mav = new ModelAndView("responsable",model);
return mav;
}
private ModelAndView handleGetThirdChoice(HttpServletRequest request,
HttpServletResponse response) {
// TODO Auto-generated method stub
return null;
}
private ModelAndView handleGetSecondChoice(HttpServletRequest request,
HttpServletResponse response) {
// TODO Auto-generated method stub
return null;
}
private ModelAndView handleGetFirstChoice(HttpServletRequest request,
HttpServletResponse response) {
ModelAndView mav = null;
String path = request.getSession().getServletContext().getRealPath("/");
MailSenDer ms = new MailSenDer(path + "mail.properties");
Map model = new HashMap<String, List>();
List error = new ArrayList();
String[] boxes = RequestUtils.getStringParameters(request, "box");
String action = request.getParameter("action");
String state = null;
String view = request.getParameter("page");
int piId;
List folders = foldermanager.findPersonalInfoByState("FINALIZED");
// System.out.println("LIST SIZE "+folders.size());
// request.getSession().setAttribute("received", folders);
model.put("received", folders);
return new ModelAndView("responsable-acceuil", model);
}
private ModelAndView handlePost(HttpServletRequest request,
HttpServletResponse response) {
HttpSession session = request.getSession(false);
if (session == null) {
return new ModelAndView("logout-personnel");
}
Personnel p = (Responsable) session.getAttribute("responsable");
if (p == null)
return new ModelAndView("logout-personnel");
String message = null;
List page = new ArrayList();
Map model = new HashMap();
String changepass = request.getParameter("changepass");
String complementaryButon = request.getParameter("complementaryButon");
String interviewButon = request.getParameter("interviewButon");
String convocationButon = request.getParameter("convocationButon");
if (changepass != null) {
System.out.println("On entre dans le changement du mot de passe");
String newpass1 = request.getParameter("newpass1");
String newpass2 = request.getParameter("newpass2");
Personnel personnel = (Responsable) session.getAttribute("responsable");
if (newpass1.equals(newpass2)) {
personnel.setPassword(newpass1);
boolean update = foldermanager
.updatePersonnelPassword(personnel);
if (update) {
message = "Le mot de passe a �t� chang�";
page.add("Confirmation");
model.put("page", page);
model.put("message", message);
} else {
message = "Erreur lors du changement du mot de passe";
page.add("Echec");
model.put("page", page);
model.put("message", message);
}
} else {
message = "Erreur lors de la confirmation : Champs \"Nouveau mot de passe\" et \"V�rification\" diff�rents.";
page.add("Echec");
model.put("page", page);
model.put("message", message);
}
}else if(complementaryButon != null){
String[] positions = RequestUtils.getStringParameters(request, "positionsPis");
String position = "";
String pis;
Integer pi_ref;
Etat e = new Etat();
e.setState(Folder.FOLDER_COMPLEMENTARY);
for (int i = 0; i < positions.length; i++) {
pis = positions[i];
pi_ref = Integer.parseInt(pis);
position = request.getParameter(pis);
if(!position.equals("")){
try{
e.setComplemantary_pos(Integer.parseInt(position));
foldermanager.storeAdvancement(pi_ref, e, p.getFormation());
}catch(NumberFormatException nfe){
message = "Vous avez entre autre chose qu'un entier!";
page.add("Echec");
model.put("page", page);
model.put("message", message);
return new ModelAndView("responsable", model);
}
}
}
message = "Les positions sur la liste complementaire ont ete mise a jour";
page.add("Confirmation");
model.put("page", page);
model.put("message", message);
}else if(interviewButon != null){
String[] positions = RequestUtils.getStringParameters(request, "positionsPisHoraire");
String interview = "";
String pis;
Integer pi_ref;
int updates = 0;
Etat e = new Etat();
e.setState(Folder.FOLDER_INTERVIEW);
for (int i = 0; i < positions.length; i++) {
pis = positions[i];
pi_ref = Integer.parseInt(pis);
interview = request.getParameter(pis);
if(!interview.equals("")){
e.setInterview(interview);
updates += foldermanager.storeAdvancement(pi_ref, e, p.getFormation());
}
}
if(updates == positions.length){
message = "Les horaires d'entretiens ont �t� mis a jour";
page.add("Confirmation");
model.put("page", page);
model.put("message", message);
}else{
message = "Les horaires d'entretiens n'ont pas �t� mis a jour correctement. V�rifier les horaires des entretiens.";
page.add("Echec");
model.put("page", page);
model.put("message", message);
}
}else if(convocationButon!=null){
message = "";
List advancementsInterviews = foldermanager.findByStateAndFormation(Folder.FOLDER_INTERVIEW, p.getFormation());
String path = request.getSession().getServletContext().getRealPath("/");
MailSenDer mdInterview = new MailSenDer(path+"mail.properties");
boolean send = true;
for (Iterator iterAdvancements = advancementsInterviews.iterator(); iterAdvancements
.hasNext();) {
Advancement advancement = (Advancement) iterAdvancements.next();
Formation f = (Formation)advancement.getAdvancement().keySet().iterator().next();
Etat interview = advancement.getAdvancement().get(f);
boolean convoq = mdInterview.sendInterviewMail(advancement, p, interview);
send = send && convoq;
if(!convoq){
message += advancement.getPi().getEmail()+" ";
}
}
if(send){
page.add("Confirmation");
message = "L'envoie de mail reussit!";
model.put("page", page);
model.put("message", message);
}else{
page.add("Echec");
message = "Le mail n'a pas pu etre envoye aux candidats suivants :"+message;
model.put("page", page);
model.put("message", message);
}
}
return new ModelAndView("responsable", model);
}
}