package controller;
import java.util.Calendar;
import java.util.GregorianCalendar;
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 manager.ChoiceManager;
import manager.PersonalInfoManager;
import manager.PromoManager;
import manager.SkillsManager;
import org.springframework.validation.BindException;
import org.springframework.validation.Errors;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;
import util.WebgiProperties;
import web.FolderForm;
import domain.Folder;
import domain.PersonalInfo;
import domain.Skill;
import domain.WebgiUser;
public class LoginCTRL extends SimpleFormController {
private PersonalInfoManager pimng;
private ChoiceManager cmng;
private PromoManager pmng;
private SkillsManager skmng;
public ChoiceManager getCmng() {
return cmng;
}
public void setCmng(ChoiceManager cmng) {
this.cmng = cmng;
}
public PersonalInfoManager getPimng() {
return pimng;
}
public void setPimng(PersonalInfoManager pimng) {
this.pimng = pimng;
}
public PromoManager getPmng() {
return pmng;
}
public void setPmng(PromoManager pmng) {
this.pmng = pmng;
}
public SkillsManager getSkmng() {
return skmng;
}
public void setSkmng(SkillsManager skmng) {
this.skmng = skmng;
}
public LoginCTRL() {
setFormView("login");
setCommandClass(WebgiUser.class);
setCommandName("wuser");
}
@Override
protected ModelAndView onSubmit(HttpServletRequest request,
HttpServletResponse response, Object command, BindException be)
throws Exception {
Map model = new HashMap();
ModelAndView mav;
String path = request.getSession().getServletContext().getRealPath(
"/"+"candidature.properties");
WebgiUser wuser = (WebgiUser) command;
PersonalInfo pi = pimng.findEP(wuser.getEmail(), wuser.getPassword());
if (pi == null) {
model.put("error", "Verifiez vos identifiants.");
be.rejectValue("email", "email.invalid",
"Identifiant(s) invalide(s)");
return showForm(request, response, be);
} else {
if (pi.getState().equals(Folder.FOLDER_FINALIZED) || pi.getState().equals(Folder.FOLDER_INCOMPLETE) || pi.getState().equals(Folder.FOLDER_COMPLETE)) {
String etat = "";
if(pi.getState().equals(Folder.FOLDER_FINALIZED)){
etat = "Le secretariat est en attente de votre dossier version papier. Envoyez ce dernier avant la date limite des inscriptions, autrement votre inscription ne sera pas traitee.";
}else if(pi.getState().equals(Folder.FOLDER_INCOMPLETE)){
etat = "Le secretariat a recu votre dossier version papier mais il manque certainnes pieces. Un mail vous a ete envoye avec le detail des pieces. Votre dossier sera traite que apres la reception des pieces manquantes.";
}else{
etat = "Le secretariat a recu votre dossier version papier. Il est en cours de traitement. Un mail et un courrier vous seront adresses des la prise de une decision.";
}
String pdf = pi.getId()+"_"+pi.getLastname()+"_"+pi.getFirstname()+".pdf";
model.put("etat", etat);
model.put("pdf",pdf);
request.getSession(true);
return new ModelAndView("state",model);
}
if (isClosed(path)){
model.put("closed", true);
return new ModelAndView("logout",model);
}
List choices = cmng.findStudentChoices(pi.getId());
List promos = pmng.getPromos(pi.getId());
List skills = skmng.findStudentSkills(pi.getId());
Folder folder = new Folder(choices, skills);
folder.initChoices(pi.getId());
folder.initSkills(pi.getId());
folder.setPi(pi);
folder.setPromodetails(promos);
folder.majPromodetailPositions();
folder.setNbPromodetails(promos.size());
FolderForm ff = new FolderForm();
ff.setFolder(folder);
/* debut de la session */
request.getSession(true);
request.getSession().setAttribute("updatedFF", ff);
return new ModelAndView("finalize-or-update");
}
}
@Override
protected Map referenceData(HttpServletRequest request) throws Exception {
Map model = new HashMap();
String action = request.getParameter("action");
request.removeAttribute("action");
if(action!=null && action.equals("closed")){
model.put("closed",true);
}
String path = request.getSession().getServletContext().getRealPath(
"/")+"candidature.properties";
WebgiProperties wgip = new WebgiProperties(path);
model.put("datelimite", wgip.get("dateFermeture"));
return model;
}
private boolean isClosed(String path){
WebgiProperties wgip = new WebgiProperties(path);
String dateS = wgip.get("dateFermeture");
String[] date=dateS.split("-");
int day = Integer.parseInt(date[0]);
int month = Integer.parseInt(date[1])-1;
int year = Integer.parseInt(date[2]);
Calendar fermeture = new GregorianCalendar(year, month, day);
Calendar c = Calendar.getInstance();
int currentYear = c.get(Calendar.YEAR);
int currentMonth = c.get(Calendar.MONTH);
int currentDay = c.get(Calendar.DAY_OF_MONTH);
Calendar current = new GregorianCalendar(currentYear,
currentMonth, currentDay);
return current.after(fermeture);
}
}