package com.vst.webapp.action;
import com.vst.model.Expert;
import com.vst.service.ExpertManager;
import com.vst.util.FileHelper;
import com.vst.util.StringUtil;
import com.vst.webapp.util.ImageUtil;
import com.lowagie.text.Image;
import org.hibernate.Hibernate;
import org.springframework.validation.BindException;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.Date;
/**
* Created by IntelliJ IDEA.
* User: And Lilia
* Date: 16.04.2009
* Time: 14:41:06
* To change this template use File | Settings | File Templates.
*/
public class InsertExpertController extends BaseFormController {
ExpertManager expertManager;
public void setExpertManager(ExpertManager expertManager) {
this.expertManager = expertManager;
}
public InsertExpertController() {
setCommandName("expert");
setCommandClass(Expert.class);
}
protected Object formBackingObject(HttpServletRequest request) throws Exception {
if (!isFormSubmission(request)) {
Expert expert = new Expert();
if (request.getParameter("expertId") != null) {
int id = Integer.parseInt(request.getParameter("expertId"));
expert = expertManager.getExpertById((Integer) id);
}
if (request.getParameter("edited") != null) {
request.setAttribute("addition", "?edited=1");
expert.setEdited(true);
}
if (request.getParameter("static") != null) {
request.setAttribute("static", "1");
if (request.getParameter("static").equals("true")) {
expert.setExpert(true);
} else if (request.getParameter("static").equals("false")) {
expert.setExpert(false);
}
} else {
request.setAttribute("static", "0");
}
return expert;
}
return super.formBackingObject(request);
}
public ModelAndView onSubmit(HttpServletRequest request,
HttpServletResponse response,
Object command,
BindException errors) throws Exception {
ModelAndView mav = new ModelAndView(getSuccessView());
Expert expert = (Expert) command;
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
CommonsMultipartFile frontFile = (CommonsMultipartFile) multipartRequest.getFile("frontFile");
CommonsMultipartFile backFile = (CommonsMultipartFile) multipartRequest.getFile("backFile");
if (expert.getExpertId() != null) {
String dateNew;
Date d;
d = expert.getDataComplite();
// d.setMonth(d.getMonth()); ?? for what this code
expert.setDataComplite(d);
dateNew = StringUtil.formatDate(d);
expert.setDateForLook(dateNew);
d = expert.getDataStart();
// d.setMonth(d.getMonth());
expert.setDataStart(d);
dateNew = StringUtil.formatDate(d);
expert.setStartDateForLook(dateNew);
if (frontFile.getSize() > 0) {
Date date = new Date();
String wayToPhoto = String.valueOf(date.getTime()) + ".jpg";
OutputStream fl = null;
fl = new FileOutputStream(FileHelper.getCurrentPath(request) + wayToPhoto);
fl.write(frontFile.getBytes());
fl.close();
InputStream inputStream = new FileInputStream(FileHelper.getCurrentPath(request) + wayToPhoto);
expert.setFrontExpertBlob(Hibernate.createBlob(ImageUtil.scaleImage(inputStream)));
expert.setFrontWayToPhoto(wayToPhoto);
} else {
Expert ex = expertManager.getExpertById(expert.getExpertId());
expert.setFrontExpertBlob(ex.getFrontExpertBlob());
expert.setFrontWayToPhoto(ex.getFrontWayToPhoto());
}
if (backFile.getSize() > 0) {
Date date = new Date();
String wayToPhoto = String.valueOf(date.getTime()) + ".jpg";
OutputStream fl = null;
fl = new FileOutputStream(FileHelper.getCurrentPath(request) + wayToPhoto);
fl.write(backFile.getBytes());
fl.close();
InputStream inputStream = new FileInputStream(FileHelper.getCurrentPath(request) + wayToPhoto);
expert.setBackExpertBlob(Hibernate.createBlob(ImageUtil.scaleImage(inputStream)));
expert.setBackWayToPhoto(wayToPhoto);
} else {
Expert ex = expertManager.getExpertById(expert.getExpertId());
if (ex.getBackExpertBlob()!=null && ex.getBackWayToPhoto()!=null){
expert.setBackExpertBlob(ex.getBackExpertBlob());
expert.setBackWayToPhoto(ex.getBackWayToPhoto());
} else{
expert.setBackWayToPhoto("no photo");
}
}
expertManager.updateExpert(expert);
if (expert.isEdited()) {
return new ModelAndView("redirect:updating.html?id=" + expert.getExpertId() + "&fieldId=" + request.getParameter("fieldId"));
}
} else {
Date d = expert.getDataComplite();
// d.setMonth(d.getMonth());
expert.setDataComplite(d);
String dateNew = StringUtil.formatDate(d);
expert.setDateForLook(dateNew);
d = expert.getDataStart();
// d.setMonth(d.getMonth());
expert.setDataComplite(d);
dateNew = StringUtil.formatDate(d);
expert.setStartDateForLook(dateNew);
if (frontFile.getSize() > 0) {
Date date = new Date();
String wayToPhoto = String.valueOf(date.getTime()) + ".jpg";
OutputStream fl = new FileOutputStream(FileHelper.getCurrentPath(request) + wayToPhoto);
fl.write(frontFile.getBytes());
fl.close();
InputStream inputStream = new FileInputStream(FileHelper.getCurrentPath(request) + wayToPhoto);
expert.setFrontExpertBlob(Hibernate.createBlob(ImageUtil.scaleImage(inputStream)));
expert.setFrontWayToPhoto(wayToPhoto);
}
if (backFile.getSize() > 0) {
Date date = new Date();
String wayToPhoto = String.valueOf(date.getTime()) + ".jpg";
OutputStream fl = new FileOutputStream(FileHelper.getCurrentPath(request) + wayToPhoto);
fl.write(backFile.getBytes());
fl.close();
InputStream inputStream = new FileInputStream(FileHelper.getCurrentPath(request) + wayToPhoto);
expert.setBackExpertBlob(Hibernate.createBlob(ImageUtil.scaleImage(inputStream)));
expert.setBackWayToPhoto(wayToPhoto);
}else{
expert.setBackWayToPhoto("no photo");
}
expertManager.insertExpert(expert);
mav.addObject("expert", new Expert());
mav.addObject("result", new Integer(1));
if (expert.isEdited()) {
return new ModelAndView("redirect:updating.html?id=" + expert.getExpertId() + "&fieldId=" + request.getParameter("fieldId"));
}
return mav;
}
return new ModelAndView("redirect:/lookAllExpert.html");
}
}