package com.vst.webapp.action;
import com.vst.Constants;
import com.vst.util.FileHelper;
import com.vst.model.ObjectConstruction;
import com.vst.service.ObjectConstructionManager;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.File;
import java.sql.Blob;
public class EskizController implements Controller {
private final Log log = LogFactory.getLog(ObjectPhotoController.class);
private ObjectConstructionManager objectConstructionManager = null;
public void setObjectConstructionManager(ObjectConstructionManager objectConstructionManager) {
this.objectConstructionManager = objectConstructionManager;
}
public ModelAndView handleRequest(HttpServletRequest request,
HttpServletResponse response)
throws Exception {
if (log.isDebugEnabled()) {
log.debug("entering 'handleRequest' method...");
}
String objectConstructionId = request.getParameter("objectConstructionId");
if (objectConstructionId != null) {
ObjectConstruction objectConstruction = objectConstructionManager.getObjectConstruction(objectConstructionId);
if (objectConstruction.getWayToEsckiz() != null && !"".equals(objectConstruction.getWayToEsckiz()) && objectConstruction.getEsckizBlob() != null && objectConstruction.getEsckizBlob().length() > 0) {
File file = new File(FileHelper.getCurrentPath(request) + objectConstruction.getWayToEsckiz());
if (file.exists()) {
file.delete();
}
file.createNewFile();
FileOutputStream fileOutputStream = new FileOutputStream(FileHelper.getCurrentPath(request) + objectConstruction.getWayToEsckiz());
Blob blob = objectConstruction.getEsckizBlob();
byte[] bufer = new byte[62000];
InputStream inputStream = blob.getBinaryStream();
while (inputStream.read(bufer) != -1) {
fileOutputStream.write(bufer);
}
inputStream.close();
fileOutputStream.close();
} else {
request.setAttribute("noPhoto", "true");
}
return new ModelAndView("objectConstructionPicture", Constants.OBJECTCONSTRUCTIONTYPE_WAY, objectConstruction.getWayToEsckiz());
} else {
request.setAttribute("noObjectConstruction", "true");
}
return new ModelAndView("objectConstructionPicture");
}
}