package com.vst.webapp.action;
import com.vst.model.BuildingObject;
import com.vst.model.ObjectConstruction;
import com.vst.service.BuildingObjectManager;
import com.vst.service.ConstructionTypeManager;
import com.vst.service.MaterialManager;
import com.vst.service.ObjectConstructionManager;
import com.vst.util.FileHelper;
import com.vst.util.ImageUtil;
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 javax.imageio.ImageIO;
import java.io.*;
import java.util.Locale;
import java.awt.image.BufferedImage;
import java.awt.*;
public class ObjectConstructionFormController extends BaseFormController {
private ObjectConstructionManager objectConstructionManager = null;
private MaterialManager materialManager = null;
private ConstructionTypeManager constructionTypeManager = null;
private BuildingObjectManager buildingObjectManager = null;
public void setBuildingObjectManager(BuildingObjectManager buildingObjectManager) {
this.buildingObjectManager = buildingObjectManager;
}
public void setMaterialManager(MaterialManager materialManager) {
this.materialManager = materialManager;
}
public void setConstructionTypeManager(ConstructionTypeManager constructionTypeManager) {
this.constructionTypeManager = constructionTypeManager;
}
public void setObjectConstructionManager(ObjectConstructionManager objectConstructionManager) {
this.objectConstructionManager = objectConstructionManager;
}
public ObjectConstructionFormController() {
setCommandName("objectConstruction");
setCommandClass(ObjectConstruction.class);
}
protected Object formBackingObject(HttpServletRequest request)
throws Exception {
request.setAttribute("materialList", materialManager.getMaterials(null));
request.setAttribute("constructionTypeList", constructionTypeManager.getConstructionTypes(null));
if (!isFormSubmission(request)) {
String typeId = request.getParameter("typeId");
ObjectConstruction objectConstruction = null;
if (!StringUtils.isEmpty(typeId)) {
objectConstruction = objectConstructionManager.getObjectConstruction(typeId);
objectConstructionManager.evict(objectConstruction);
} else {
objectConstruction = new ObjectConstruction();
}
String objectId = request.getParameter("objectId");
if (objectId != null) {
objectConstruction.setObjectId(new Integer(objectId));
}
if (request.getParameter("edited") != null) {
request.setAttribute("addition", "?edited=1");
objectConstruction.setEdited(true);
}
if (objectConstruction.getObjectId() == null) {
objectConstruction.setObjectId(new Integer(objectId));
}
//checking if the esciz already exists
System.out.println(" WAYYYYYYY ");
if (objectConstruction.getWayToEsckiz() == null || objectConstruction.getWayToEsckiz().equals("")) {
String saveWay = ImageUtil.getUniqueJPEGFile(request);
String loadPath = FileHelper.getCurrentPath(request) + "blank.jpg";
InputStream imageStream = new FileInputStream(loadPath);
Image image = ImageIO.read(imageStream);
imageStream.close();
int w = image.getWidth(null);
int h = image.getHeight(null);
BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = bi.createGraphics();
g2.drawImage(image, 0, 0, null);
try {
ImageIO.write(bi, "jpg", new FileOutputStream(FileHelper.getCurrentPath(request)+saveWay));
} catch (IOException e) {
}
System.out.println("saveWay = "+saveWay);
request.setAttribute("saveWay", saveWay);
objectConstruction.setWayToEsckiz(saveWay);
String saveWayImage="image"+saveWay;
objectConstruction.setWayToEsckizImage(saveWayImage);
request.setAttribute("saveWayImage", saveWayImage);
saveWay = "DefectMap"+saveWay;
saveWayImage="image"+saveWay;
objectConstruction.setWayToDefectMap(saveWay);
objectConstruction.setWayToDefectMapImage(saveWayImage);
} else {
request.setAttribute("saveWay", objectConstruction.getWayToEsckiz());
request.setAttribute("saveWayImage", objectConstruction.getWayToEsckizImage());
request.setAttribute("loadWay", objectConstruction.getWayToEsckiz());
}
objectConstruction.setDocLocation(request.getParameter("docLocation"));
return objectConstruction;
}
return super.formBackingObject(request);
}
public ModelAndView onSubmit(HttpServletRequest request,
HttpServletResponse response, Object command,
BindException errors)
throws Exception {
if (log.isDebugEnabled()) {
log.debug("entering 'onSubmit' method...");
}
ObjectConstruction objectConstruction = (ObjectConstruction) command;
boolean isNew = (objectConstruction.getTypeId() == null);
Locale locale = request.getLocale();
//getting the object
if (request.getParameter("delete") != null) {
BuildingObject buildingObject = buildingObjectManager.getBuildingObject(objectConstruction.getObjectId().toString());
buildingObject.getConstructionTypes().remove(objectConstruction);
buildingObjectManager.saveBuildingObject(buildingObject);
// objectConstructionManager.removeObjectConstruction(objectConstruction.getTypeId().toString());
saveMessage(request, getText("objectConstruction.deleted", locale));
} else {
Integer constructionTypeId = objectConstruction.getConstructionType().getConstructionTypeId();
Integer materialId = objectConstruction.getMaterial().getMaterialId();
if (!constructionTypeId.equals(new Integer(-1))) {
objectConstruction.setConstructionType(constructionTypeManager.getConstructionType(constructionTypeId.toString()));
}
objectConstruction.setMaterial(null);
File eskizFile = new File(FileHelper.getCurrentPath(request) + objectConstruction.getWayToEsckiz());
// //setting escizBlob if it's not empty
boolean hasEskiz = false;
if (objectConstruction.getWayToEsckiz() != null && !objectConstruction.getWayToEsckiz().equals("") && eskizFile.exists()) {
objectConstruction.setEsckizBlob(Hibernate.createBlob(new FileInputStream(FileHelper.getCurrentPath(request) + objectConstruction.getWayToEsckiz())));
hasEskiz = true;
}
if (!isNew) {
ObjectConstruction oldObjectConstruction = objectConstructionManager.getObjectConstruction(objectConstruction.getTypeId().toString());
objectConstruction.setDocumentationQuestions(oldObjectConstruction.getDocumentationQuestions());
objectConstruction.setConstructionExamples(oldObjectConstruction.getConstructionExamples());
objectConstruction.setConstructionType(oldObjectConstruction.getConstructionType());
objectConstructionManager.evict(oldObjectConstruction);
}
MultipartHttpServletRequest multipartRequest =
(MultipartHttpServletRequest) request;
CommonsMultipartFile file =
(CommonsMultipartFile) multipartRequest.getFile("file");
if (file.getSize() > 0) {
String fileName = ImageUtil.getUniqueJPEGFile(request);
FileOutputStream fileOutputStream = new FileOutputStream(FileHelper.getCurrentPath(request) + fileName);
fileOutputStream.write(file.getBytes());
fileOutputStream.close();
InputStream imageStream = ImageUtil.scaleImage(new FileInputStream(FileHelper.getCurrentPath(request) + fileName), 200, 200);
fileOutputStream = new FileOutputStream(fileName);
byte[] bufer = new byte[62000];
while (imageStream.read(bufer) != -1) {
fileOutputStream.write(bufer);
}
fileOutputStream.close();
objectConstruction.setWayToPhoto(fileName);
objectConstruction.setPhotoBlob(Hibernate.createBlob(new FileInputStream(FileHelper.getCurrentPath(request) + fileName)));
} else {
//restoring old blob if new picture wasn't added
if (!isNew) {
ObjectConstruction oldObjectConstruction = objectConstructionManager.getObjectConstruction(objectConstruction.getTypeId().toString());
objectConstructionManager.evict(oldObjectConstruction);
objectConstruction.setPhotoBlob(oldObjectConstruction.getPhotoBlob());
objectConstruction.setWayToPhoto(oldObjectConstruction.getWayToPhoto());
}
}
if (isNew) {
BuildingObject buildingObject = buildingObjectManager.getBuildingObject(objectConstruction.getObjectId().toString());
objectConstruction.setObjectId(buildingObject.getObjectId());
buildingObject.getConstructionTypes().add(objectConstruction);
buildingObjectManager.saveBuildingObject(buildingObject);
} else {
objectConstructionManager.saveObjectConstruction(objectConstruction);
//setting eskiz to all examples if it didn't exist before
if (hasEskiz) {
try {
objectConstructionManager.setNewEskizToAllExamples(objectConstruction, request);
} catch (Exception e) {
e.printStackTrace();
}
}
}
String key = (isNew) ? "objectConstruction.added" : "objectConstruction.updated";
saveMessage(request, getText(key, locale));
if (objectConstruction.isEdited()) {
return new ModelAndView("redirect:updating.html?id=" + objectConstruction.getTypeId() + "&fieldId=" + request.getParameter("fieldId"));
}
}
request.getSession().setAttribute("madeChanges", new Boolean(true));
return new ModelAndView("redirect:" + objectConstruction.getDocLocation());
}
public ModelAndView processFormSubmission(HttpServletRequest request,
HttpServletResponse response,
Object command,
BindException errors)
throws Exception {
ObjectConstruction objectConstruction = (ObjectConstruction) command;
if (request.getParameter("cancel") != null) {
String addition = "";
if (objectConstruction.getTypeId() != null) {
addition = "&typeId=" + objectConstruction.getTypeId();
}
return new ModelAndView("redirect: objectConstructions.html?objectId=" + objectConstruction.getObjectId() + addition);
}
return super.processFormSubmission(request, response, command, errors);
}
}