package controller;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import manager.PersonalInfoManager;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;
import util.FolderJavaToXML;
import util.MailSenDer;
import util.XMLToPDF;
import web.FolderForm;
import domain.Folder;
public class DownloadCTRL extends SimpleFormController {
private PersonalInfoManager pimng;
public PersonalInfoManager getPimng() {
return pimng;
}
public void setPimng(PersonalInfoManager pimng) {
this.pimng = pimng;
}
public DownloadCTRL() {
setFormView("download");
setCommandName("FoldF");
// setValidator(new SubmitionValidator());
}
@Override
protected Object formBackingObject(HttpServletRequest request)
throws Exception {
HttpSession session = request.getSession(false);
FolderForm ff = (FolderForm)session.getAttribute("updatedFF");
if (ff == null) {
return new FolderForm();
}
return ff;
}
@Override
protected ModelAndView onSubmit(HttpServletRequest request,
HttpServletResponse response, Object object, BindException be)
throws Exception {
Map model = new HashMap();
Folder f = (Folder) ((FolderForm) object).getFolder();
if (be.hasErrors()) {
System.out.println("HAS ERRORS");
return showForm(request, response, be);
} else {
System.out.println("HAS NO ERRORS");
System.out.println(f.getPi().getBirthdate()+" DATE NAISSANCE FINALISATION");
/**Generation of pdf folder document*/
String path = request.getSession().getServletContext().getRealPath("/");
String xmlFile = FolderJavaToXML.createXML(f,path);
String pdfFile = XMLToPDF.createPdfBill(xmlFile,path);
/** Reversing the birthdate because of an update on personalinfo
* This update is made in order to change the state of the folder
* in the database
* */
f.getPi().setState("FINALIZED");
String[] birthOld = f.getPi().getBirthdate().split("-");
String birth = birthOld[2] + "-" + birthOld[1] + "-" + birthOld[0];
f.getPi().setBirthdate(birth);
this.pimng.savePersonalInfo(f.getPi());
/**Sends an email to the student*/
MailSenDer ms = new MailSenDer(path+"mail.properties");
ms.sendAccuse(f.getPi().getEmail(), f.getPi().getLastname()+" "+f.getPi().getFirstname());
model.put("urlPdfFolder", "./pdf/"+pdfFile);
return new ModelAndView("completed", model);
}
}
}