package com.vst.webapp.action;
import com.lowagie.text.Image;
import com.vst.model.CompanyLicense;
import com.vst.model.Photo;
import com.vst.service.CompanyLicenseManager;
import com.vst.service.PhotoManager;
import com.vst.util.FileHelper;
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;
/**
* Created by IntelliJ IDEA.
* User: Администратор
* Date: 06.07.2009
* Time: 17:30:08
* To change this template use File | Settings | File Templates.
*/
public class CompanyLicenseFormController extends BaseFormController {
CompanyLicenseManager companyLicenseManager;
PhotoManager photoManager;
public void setPhotoManager(PhotoManager photoManager) {
this.photoManager = photoManager;
}
public void setCompanyLicenseManager(CompanyLicenseManager companyLicenseManager) {
this.companyLicenseManager = companyLicenseManager;
}
CompanyLicenseFormController() {
setCommandClass(CompanyLicense.class);
setCommandName("companyLicense");
}
protected Object formBackingObject(HttpServletRequest request) throws Exception {
if (!isFormSubmission(request)) {
CompanyLicense companyLicense = new CompanyLicense();
// companyLicense.setPhoto(new Photo());
if (request.getParameter("companyId") != null) {
Integer id = Integer.valueOf(request.getParameter("companyId"));
Integer photoId = companyLicenseManager.getById(id).getPhoto().getPhotoId();
companyLicense.setLicenseId(id);
companyLicense.setPhoto(photoManager.getByIdPhoto(photoId));
}
return companyLicense;
}
return super.formBackingObject(request);
}
public ModelAndView onSubmit(HttpServletRequest request,
HttpServletResponse response,
Object command,
BindException errors) throws Exception {
ModelAndView mav = new ModelAndView(getSuccessView());
CompanyLicense companyLicense = (CompanyLicense) command;
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
CommonsMultipartFile file = (CommonsMultipartFile) multipartRequest.getFile("photo.file");
if (companyLicense.getLicenseId() != null) {
String comment = companyLicense.getPhoto().getComment();
companyLicense = companyLicenseManager.getById(companyLicense.getLicenseId());
Photo photo = photoManager.getByIdPhoto(companyLicense.getPhoto().getPhotoId());
photo.setComment(comment);
photoManager.update(photo, file, FileHelper.getCurrentPath(request),400,400);
companyLicenseManager.update(companyLicense);
return new ModelAndView("redirect:/companyLicenseList.html");
} else {
if (file.getSize() > 0) {
Photo photo = photoManager.insertPhoto(file, FileHelper.getCurrentPath(request),400,400);
photo.setComment(companyLicense.getPhoto().getComment());
companyLicense.setPhoto(photo);
}
companyLicenseManager.insert(companyLicense);
mav.addObject("result", new Integer(1));
}
CompanyLicense com = new CompanyLicense();
// com.setPhoto(new Photo());
mav.addObject("companyLicense", com);
return mav;
}
}