package com.vst.webapp.action;
import com.vst.model.Hint;
import com.vst.service.HintManager;
import com.vst.util.FileHelper;
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;
import java.util.ArrayList;
import java.util.List;
/**
* Created by IntelliJ IDEA.
* User: ALEXEI
* Date: 18.03.2008
* Time: 21:29:37
* To change this template use File | Settings | File Templates.
*/
public class ShowHintController implements Controller {
private HintManager hintManager = null;
public void setHintManager(HintManager hintManager) {
this.hintManager = hintManager;
}
public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
String questionId = httpServletRequest.getParameter("questionId");
List hints = new ArrayList();
if (questionId != null) {
hints = hintManager.getHintByQuestionId(questionId);
}
for (int i = 0; i < hints.size(); i++) {
Hint hint = (Hint) hints.get(i);
if (hint.getWayToPhoto() != null && !hint.getWayToPhoto().trim().equals("")) {
File file = new File(FileHelper.getCurrentPath(httpServletRequest) + hint.getWayToPhoto());
if (file.exists()) {
file.delete();
}
file.createNewFile();
FileOutputStream fileOutputStream = new FileOutputStream(FileHelper.getCurrentPath(httpServletRequest) + hint.getWayToPhoto());
Blob blob = hint.getPhotoBlob();
byte[] bufer = new byte[62000];
InputStream inputStream = blob.getBinaryStream();
while (inputStream.read(bufer) != -1) {
fileOutputStream.write(bufer);
}
inputStream.close();
fileOutputStream.close();
}
}
return new ModelAndView("showHint", "hints", hints);
}
}