package com.vst.webapp.action;
import com.vst.util.FileHelper;
import com.vst.model.ConstructionExample;
import com.vst.service.ConstructionExampleManager;
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.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.sql.Blob;
public class ExampleEscizController implements Controller {
private final Log log = LogFactory.getLog(ObjectPhotoController.class);
private ConstructionExampleManager constructionExampleManager = null;
public void setConstructionExampleManager(ConstructionExampleManager constructionExampleManager) {
this.constructionExampleManager = constructionExampleManager;
}
public ModelAndView handleRequest(HttpServletRequest request,
HttpServletResponse response)
throws Exception {
if (log.isDebugEnabled()) {
log.debug("entering 'handleRequest' method...");
}
String exampleId = request.getParameter("exampleId");
if (exampleId != null) {
ConstructionExample constructionExample = constructionExampleManager.getConstructionExample(exampleId);
if (constructionExample.getWayToDefectEsckiz() != null && !"".equals(constructionExample.getWayToDefectEsckiz())) {
File file = new File(FileHelper.getCurrentPath(request) + constructionExample.getWayToDefectEsckiz());
if (file.exists()) {
file.delete();
}
file.createNewFile();
FileOutputStream fileOutputStream = new FileOutputStream(FileHelper.getCurrentPath(request) + constructionExample.getWayToDefectEsckiz());
Blob blob = constructionExample.getDefectEsckizBlob();
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");
}
if (constructionExample.getWayToPowerEsckiz() != null && !"".equals(constructionExample.getWayToPowerEsckiz())) {
File file = new File(FileHelper.getCurrentPath(request) + constructionExample.getWayToPowerEsckiz());
if (file.exists()) {
file.delete();
}
file.createNewFile();
FileOutputStream fileOutputStream = new FileOutputStream(FileHelper.getCurrentPath(request) + constructionExample.getWayToPowerEsckiz());
Blob blob = constructionExample.getPowerEsckizBlob();
byte[] bufer = new byte[62000];
InputStream inputStream = blob.getBinaryStream();
while (inputStream.read(bufer) != -1) {
fileOutputStream.write(bufer);
}
inputStream.close();
fileOutputStream.close();
} else {
request.setAttribute("noPowerPhoto", "true");
}
request.setAttribute("defectPhoto", constructionExample.getWayToDefectEsckiz());
request.setAttribute("powerPhoto", constructionExample.getWayToPowerEsckiz());
return new ModelAndView("examplePicture");
} else {
request.setAttribute("noObjectConstruction", "true");
}
return new ModelAndView("examplePicture");
}
}