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.Choice;
import domain.Etat;
import domain.Folder;
import domain.Formation;
import domain.PersonalInfo;
import domain.Personnel;
import domain.Secretary;
/**
* Controller that handls all interaction between a secretary an her interface.
* Every action of the secretary is describe here in ordre to define the
* correcte reaction.
*
* @author Marie Hausenblasova
*/
public class SecretaryCTRL extends AbstractController {
/**
* This attribute represents a manager which handels the interaction between
* the interface and the respectives DAO to access the database.
*/
private 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;
}
/**
*
* This method allows us to handel all request which comes from a GET form
*
* @param request
* a HttpServletRequest containing all data and information
* passed while sending the request
* @param response
* a HttpServletResponse which may contain data and information
* wanted at the end.
* @return A ModelAndView is returned. This view will be displayed as a
* result of the request. And it will contain a model with all data
* and information.
*/
private ModelAndView handleGet(HttpServletRequest request,
HttpServletResponse response) {
HttpSession session = request.getSession(false);
/* If session expired -> login */
if (session == null) {
return new ModelAndView("logout-personnel");
}
Personnel p = (Secretary) session.getAttribute("secretary");
if (p == null)
return new ModelAndView("logout-personnel");
/* le nom de methode dans le decorator qui va afficher les actions possibles sur une ligne d'un display tag*/
String link = null;
/* message affiche dans l'interface */
String message = null;
/* le ModelAndView retourne*/
ModelAndView mav = null;
/* va contenir toutes les informations qu'on veut passer a la jsp */
Map model = new HashMap();
String filename = null;
String action = request.getParameter("action");
String sId = request.getParameter("id");
int id;
/* L'entete de la page */
List page = new ArrayList();
/* The home page of a secretary */
if (action == null) {
page.add("Accueil Secr�tariat");
model.put("page", page);
Integer completes = foldermanager.getPidao()
.getCountPersonalInfoByState(Folder.FOLDER_COMPLETE);
model.put("complets", completes);
Integer incomplets = foldermanager.getPidao()
.getCountPersonalInfoByState(Folder.FOLDER_INCOMPLETE);
model.put("incomplets", incomplets);
Integer waiting = foldermanager.getPidao()
.getCountPersonalInfoByState(Folder.FOLDER_FINALIZED);
model.put("waiting", waiting);
mav = new ModelAndView("secretary", model);
/* The page of logout */
} else if (action.equals("logout")) {
return new ModelAndView("logout-personnel");
} else {
/* The page of a secretary displaying folders without a paper version received */
if (action.equals("wait")) {
page
.add("Secr�tariat : Liste des dossiers en attente de dossier papier.");
model.put("page", page);
/*
* Recuperation de la liste des dossiers qui ont ete valides par des
* candidats et qui sont en atteinte d'une version papier
*/
List<Folder> finalizedByCandidate = foldermanager
.findPersonalInfoByState(Folder.FOLDER_FINALIZED);
for (Folder f : finalizedByCandidate) {
List choices = f.getChoices();
if (choices.size() == 1) {
Choice c2 = new Choice(-1, new Formation(-1, "AUCUN"));
choices.add(1, c2);
Choice c3 = new Choice(-1, new Formation(-1, "AUCUN"));
choices.add(2, c3);
} else if (choices.size() == 2) {
Choice c3 = new Choice(-1, new Formation(-1, "AUCUN"));
choices.add(2, c3);
}
}
model.put("folders", finalizedByCandidate);
link = "wait";
/* The page of a secretary displaying folders with a paper version received but the version is incomplete, there are some attached documents missing */
} else if (action.equals("incomplete")) {
page.add("Secr�tariat : Liste des dossiers papiers incomplets.");
model.put("page", page);
/*
* Liste des dossier ayant recu une version papier mais
* incomplete
*/
List<Folder> incomplete = foldermanager.findPersonalInfoByState(Folder.FOLDER_INCOMPLETE);
for (Folder f : incomplete) {
List choices = f.getChoices();
if (choices.size() == 1) {
Choice c2 = new Choice(-1, new Formation(-1, "AUCUN"));
choices.add(1, c2);
Choice c3 = new Choice(-1, new Formation(-1, "AUCUN"));
choices.add(2, c3);
} else if (choices.size() == 2) {
Choice c3 = new Choice(-1, new Formation(-1, "AUCUN"));
choices.add(2, c3);
}
}
model.put("folders", incomplete);
link = "incomplete";
/* The page of a secretary displaying folders with a paper version received */
} else if (action.equals("complete")) {
page.add("Secr�tariat : Liste des dossiers papiers complets.");
model.put("page", page);
/* Liste des dossier ayant recu une version papier complete */
List<Folder> complete = foldermanager.findPersonalInfoByState(Folder.FOLDER_COMPLETE);
for (Folder f : complete) {
List choices = f.getChoices();
if (choices.size() == 1) {
Choice c2 = new Choice(-1, new Formation(-1, "AUCUN"));
choices.add(1, c2);
Choice c3 = new Choice(-1, new Formation(-1, "AUCUN"));
choices.add(2, c3);
} else if (choices.size() == 2) {
Choice c3 = new Choice(-1, new Formation(-1, "AUCUN"));
choices.add(2, c3);
}
}
model.put("folders", complete);
link = "complete";
/* The action of a secretary to marque a folder complete */
} else if (action.endsWith("completed")) {
id = Integer.parseInt(sId);
Folder folder = foldermanager.findFolderById(id);
int update = this.foldermanager.updatePersonalInfoState(id, Folder.FOLDER_COMPLETE);
/*Si update non reussit*/
if(update==0){
page.add("Echec");
message = "Le dossier de " + folder.getPi().getLastname() +" "+folder.getPi().getFirstname()+ " n\'a pas �t� transf�r� aux responsables.";
}else{
String path = request.getSession().getServletContext().getRealPath("/");
MailSenDer msd = new MailSenDer(path + "mail.properties");
boolean send = msd.sendAccuseDossierPapier(folder.getPi().getEmail(), folder.getPi().getLastname()
+ " " + folder.getPi().getFirstname());
if(send){
page.add("Confirmation");
message = "Le dossier de " + folder.getPi().getLastname() +" "+folder.getPi().getFirstname()+ " a �t� transf�r� aux responsables. Le candidat a �t� notifi�. Vous le trouverez dans la liste des dossiers complets.";
}else{
page.add("Confirmation et Echec");
message = "Le dossier de " + folder.getPi().getLastname() +" "+folder.getPi().getFirstname()+ " a �t� transf�r� aux responsables. Mais le candidat n'a pas �t� notifi�. Vous le trouverez dans la liste des dossiers complets.";
}
link = "complete";
}
model.put("page", page);
model.put("message", message);
/* The action of a secretary to notify a person of the state incomplete of his folder */
} else if (action.endsWith("notify")) {
String idtonotif = request.getParameter("id");
page.add("Choisissez les pieces manquantes");
model.put("idFolderToNotify", idtonotif);
model.put("page", page);
return new ModelAndView("secretary-notify", model);
/* The page of a secretary displaying folders which are accepted for a formation */
} else if (action.equals("admis")) {
page.add("Listes principales des candidats");
model.put("page", page);
List formations = foldermanager.getAllFormations();
model.put("formations", formations);
for (Iterator iterFormations = formations.iterator(); iterFormations
.hasNext();) {
Formation formation = (Formation) iterFormations.next();
List admis = foldermanager.findByStateAndFormation(Folder.FOLDER_ACCEPTED, formation);
model.put(formation.getLabel(), admis);
filename="ListePrincipale"+formation.getLabel();
model.put(filename, filename);
}
model.put("title", "Action");
link = "linkAccepted";
//filename = "Admis";
/* The page of a secretary displaying folders which are accepted on a complementary liste for a formation */
}else if (action.equals("complement")) {
page.add("Listes complementaire de candidats");
model.put("page", page);
List formations = foldermanager.getAllFormations();
model.put("formations", formations);
for (Iterator iterFormations = formations.iterator(); iterFormations
.hasNext();) {
Formation formation = (Formation) iterFormations.next();
List admis = foldermanager.findByStateAndFormation(Folder.FOLDER_COMPLEMENTARY, formation);
model.put(formation.getLabel(), admis);
filename="ListeComplementaire"+formation.getLabel();
model.put(filename, filename);
}
model.put("title", "Position");
link = "linkComplementary";
//filename = "Admis";
/* The page of notification of all candidates */
}else if(action.equals("finalmail")){
page.add("Notification finale des candidats par mail");
model.put("page", page);
model.put("confirmfinalmail", "confirmfinalmail");
return new ModelAndView("secretary-notify", model);
/* 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);
return new ModelAndView("secretary-notify", model);
/* The page of a secretary displaying a detail of a folder */
} else if (action.equals("edit")) {
id = new Integer(sId);
Folder folder = foldermanager.findFolderById(id);
page.add("Secr�tariat : D�tails pour "
+ folder.getPi().getLastname() + " "
+ folder.getPi().getFirstname());
model.put("page", page);
model.put("folder", folder);
/* The action of a secretary which refuses a candidat from a principal list
* and accepts the first on a complementary list */
} else if(action.equals("desiste")){
id = new Integer(sId);
Advancement desiste = foldermanager.findAdvancementByPersonalInfoIdAndState(id,Folder.FOLDER_ACCEPTED);
if(desiste!=null){
page.add("Veuillez choisir la formation pour le d�sistement");
message = "Vous pouvez d�sister le candidat de plusieurs formations. Attention, le candidat sera refus� pour les formations choisies.";
model.put("page", page);
model.put("message", message);
List formations = new ArrayList();
for (Iterator iterFormations = desiste.getAdvancement().keySet().iterator(); iterFormations.hasNext();) {
Formation formation = (Formation) iterFormations.next();
formations.add(formation);
}
model.put("formationsdesistement", formations);
model.put("desistement", desiste);
return new ModelAndView("secretary-notify",model);
}else{
page.add("Echec");
message = "Erreur lors de la r�cup�ration du candidat.";
}
}
model.put("link", link);
model.put("message", message);
model.put("filename", filename);
}
/* Renvoie du ModelAndView de la secretaire avec le model rempli par les ifs */
mav = new ModelAndView("secretary", model);
return mav;
}
/**
*
* This method allows us to handel all request which comes from a POST form
*
* @param request
* a HttpServletRequest containing all data and information
* passed while sending the request
* @param response
* a HttpServletResponse which may contain data and information
* wanted at the end.
* @return A ModelAndView is returned. This view will be displayed as a
* result of the request. And it will contain a model with all data
* and information.
*/
private ModelAndView handlePost(HttpServletRequest request,
HttpServletResponse response) {
HttpSession session = request.getSession(false);
if (session == null) {
return new ModelAndView("logout-personnel");
}
Personnel p = (Secretary) session.getAttribute("secretary");
if (p == null)
return new ModelAndView("logout-personnel");
String link = null;
String message = null;
ModelAndView mav = null;
List page = new ArrayList();
Map model = new HashMap();
/* Recuperation de toutes les checkbox portant le nom piece */
String[] pieces = RequestUtils.getStringParameters(request, "piece");
/* L'id recupere du formulaire */
String sId = request.getParameter("id");
/* Recuperation de differents boutons */
String changepass = request.getParameter("changepass");
String finalmail = request.getParameter("sendFinalMailButon");
String desistement = request.getParameter("desisterformationsbuton");
/* Recuperation de toutes les checkbox portant le nom formation */
String[] formations = RequestUtils.getStringParameters(request, "formation");
/* Si c'est le bouton changepass qui a ete enclanche */
if (changepass != null) {
String newpass1 = request.getParameter("newpass1");
String newpass2 = request.getParameter("newpass2");
/* Les deux nouveaux mot de passes sont egaux? */
if (newpass1.equals(newpass2)) {
p.setPassword(newpass1);
/* On update le mot de passe dans la base de donnes */
boolean update = foldermanager.updatePersonnelPassword(p);
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);
}
/* Page of the final email */
}else if(finalmail!=null){
List<Advancement> advancements = this.foldermanager.findFinalAdvancement();
Map<String,String> responsables = foldermanager.findAllResponsables();
String path = request.getSession().getServletContext().getRealPath("/");
MailSenDer mailSender = new MailSenDer(path+"mail.properties");
List nonReussit = mailSender.sendFinalMail(advancements, responsables);
if(nonReussit.isEmpty()){
page.add("Confirmation");
message = "L'envoie de mail reussit!";
model.put("page", page);
model.put("message", message);
}else{
page.add("Echec");
message = "Les mails n'ont pas pu etre envoy�s aux candidats suivants :";
model.put("page", page);
model.put("message", message);
model.put("nonReussit", nonReussit);
}
/* The action of removing a candidate from a principal list */
}else if(desistement!=null && formations.length > 0 ){
int id = Integer.parseInt(sId);
message ="";
boolean send = true;
int refuse = 0;
int beforeRefuse=0;
int remonte = 0;
List envoieNonReussit = new ArrayList();
/* For every formation that we want to remove */
for (int i = 0; i < formations.length; i++) {
String labelidf = formations[i];
String[] lesdeux = labelidf.split("_");
Formation f = new Formation();
f.setId(Integer.parseInt(lesdeux[1]));
f.setLabel(lesdeux[0]);
Etat e = new Etat();
e.setState(Folder.FOLDER_REFUSED);
beforeRefuse = refuse;
/* Refusing the candidate*/
refuse += foldermanager.storeAdvancement(id, e, f);
/*If correctly refused*/
if(beforeRefuse!=refuse){
/*Moving the first on the complementary list to the principal list */
int pi_first = foldermanager.findPersonalInfoByFormationAndComplementaryPosition(f.getId());
Etat newAdmit = new Etat();
newAdmit.setState(Folder.FOLDER_ACCEPTED);
remonte = foldermanager.storeAdvancement(pi_first, newAdmit, f);
/* If correctly transfered from complementary to principal list */
if(remonte != 0){
/* Sending email to the new accepted */
PersonalInfo personalInfo = foldermanager.findPersonalInfoById(pi_first);
String responsableEmail = (String)foldermanager.findResponsableEmailByFormation(f.getId()).get(0);
String path = request.getSession().getServletContext().getRealPath("/");
MailSenDer md = new MailSenDer(path + "mail.properties");
String mail = md.sendEvolutionComplementaryToPrincipalListMail(personalInfo.getEmail(),personalInfo.getLastname()+" "+personalInfo.getFirstname(), responsableEmail, f.getLabel());
if(!mail.equals("")){
envoieNonReussit.add(mail);
}
}
remonte = 0;
}
}
if(refuse==formations.length && envoieNonReussit.isEmpty()){
page.add("Confirmation");
}else{
page.add("Confirmations et Echecs");
}
if(refuse==formations.length){
message += "Le d�sistement du candidat a r�ussit. ";
}else{
message += "Le d�sistement du candidat n'a pas r�ussit correctement. ";
}
if(envoieNonReussit.isEmpty()){
message += "La notification des premiers sur les listes compl�mentaires a r�ussit. ";
}else{
message += "La notification des premiers sur les listes compl�mentaires n'a pas r�ussit pour les adresses suivantes : ";
for (Iterator iterMails = envoieNonReussit.iterator(); iterMails
.hasNext();) {
String email = (String) iterMails.next();
message += email+" ";
}
}
model.put("page",page);
model.put("message",message);
}else if (pieces != null && pieces.length > 0) {
int id = Integer.parseInt(sId);
boolean envoye = false;
Folder folder = foldermanager.findFolderById(id);
String path = request.getSession().getServletContext().getRealPath(
"/");
MailSenDer msd = new MailSenDer(path + "mail.properties");
//for (int i = 0; i < pieces.length; i++) {
envoye = msd.sentPiecesManquantes(pieces, folder.getPi().getEmail(), folder.getPi().getLastname() + " "+ folder.getPi().getFirstname());
if(envoye){
int update = this.foldermanager.updatePersonalInfoState(id,Folder.FOLDER_INCOMPLETE);
if(update == 0){
page.add("Confirmation et Echec");
message = "Le mail avec les pieces manquantes a �t� envoy� au candidat mais le dossier n\'a pas �t� marqu� incomplet.";
}else{
page.add("Confirmation");
message = "Le mail avec les pieces manquantes a �t� envoy� au candidat.";
}
}else{
page.add("Echec");
message = "Le mail avec les pieces manquantes n\'a pas pu etre envoy� au candidat. Le dossier n\'a pas �t� marqu� incomplet.";
}
model.put("page", page);
model.put("message", message);
/* Page of error because no attachement was specified */
} else if (pieces != null && pieces.length == 0) {
page.add("Echec");
message = "Le mail n\'a pas �t� envoy� au candidat car aucune piece n\'�tait s�lectionn�e.";
model.put("page", page);
model.put("message", message);
/* Change of the password */
}
return new ModelAndView("secretary", model);
}
/**
* Returns the value of the attribute foldermanager
*
* @return the foldermanager of type FolderManager
*/
public FolderManager getFoldermanager() {
return foldermanager;
}
/**
* Initiates the attribute foldermanager with the value of foldermanager
*
* @param foldermanager
* the value for foldermanager to set
*/
public void setFoldermanager(FolderManager foldermanager) {
this.foldermanager = foldermanager;
}
}