package com.vst.webapp.action;
import com.vst.model.BuildingObject;
import com.vst.model.ObjectPhoto;
import com.vst.service.BuildingObjectManager;
import com.vst.service.ObjectPhotoManager;
import com.vst.webapp.util.ImageUtil;
import com.vst.util.FileHelper;
import org.apache.commons.lang.StringUtils;
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.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Locale;
public class PhotoFormController extends BaseFormController {
private BuildingObjectManager buildingObjectManager = null;
private ObjectPhotoManager objectPhotoManager = null;
public void setBuildingObjectManager(BuildingObjectManager buildingObjectManager) {
this.buildingObjectManager = buildingObjectManager;
}
public void setObjectPhotoManager(ObjectPhotoManager objectPhotoManager) {
this.objectPhotoManager = objectPhotoManager;
}
public PhotoFormController() {
setCommandName("objectPhoto");
setCommandClass(ObjectPhoto.class);
}
protected Object formBackingObject(HttpServletRequest request)
throws Exception {
String objectId = request.getParameter("objectId");
ObjectPhoto objectPhoto = null;
String objectPhotoId = request.getParameter("objectPhotoId");
if (!StringUtils.isEmpty(objectPhotoId)) {
objectPhoto = objectPhotoManager.getObjectPhoto(objectPhotoId);
objectPhotoManager.evict(objectPhoto);
} else {
objectPhoto = new ObjectPhoto();
}
if (objectId != null) {
objectPhoto.setObjectId(new Integer(objectId));
}
if (request.getParameter("edited") != null) {
objectPhoto.setEdited(true);
}
objectPhoto.setDocLocation(request.getParameter("docLocation"));
return objectPhoto;
}
public ModelAndView onSubmit(HttpServletRequest request,
HttpServletResponse response, Object command,
BindException errors)
throws Exception {
if (log.isDebugEnabled()) {
log.debug("entering 'onSubmit' method...");
}
ObjectPhoto objectPhoto = (ObjectPhoto) command;
boolean isNew = (objectPhoto.getPhotoId() == null);
Locale locale = request.getLocale();
BuildingObject buildingObject = buildingObjectManager.getBuildingObject(objectPhoto.getObjectId().toString());
if (request.getParameter("delete") != null) {
buildingObject.getPhotoes().remove(objectPhoto);
buildingObjectManager.saveBuildingObject(buildingObject);
saveMessage(request, getText("objectPhoto.deleted", locale));
} else {
MultipartHttpServletRequest multipartRequest =
(MultipartHttpServletRequest) request;
CommonsMultipartFile file =
(CommonsMultipartFile) multipartRequest.getFile("file");
if (file.getSize() > 0) {
String wayToPhoto = ImageUtil.getUniqueJPEGFile(request);
OutputStream fl = new FileOutputStream(FileHelper.getCurrentPath(request) + wayToPhoto);
fl.write(file.getBytes());
fl.close();
InputStream inputStream = new FileInputStream(FileHelper.getCurrentPath(request) + wayToPhoto);
//filling blob field
objectPhoto.setPhotoBlob(Hibernate.createBlob(ImageUtil.scaleImage(inputStream, 400, 400)));
objectPhotoManager.saveObjectPhoto(objectPhoto);
if (isNew) {
buildingObject.getPhotoes().add(objectPhoto);
buildingObjectManager.saveBuildingObject(buildingObject);
}
}
String key = (isNew) ? "objectPhoto.added" : "objectPhoto.updated";
saveMessage(request, getText(key, locale));
if (!isNew) {
return new ModelAndView("redirect:buildObjects.html");
}
}
return new ModelAndView("redirect:" + buildingObject.getDocLocation());
}
}