package com.vst.webapp.action;
import com.vst.model.Hint;
import com.vst.model.Question;
import com.vst.model.ConstructionType;
import com.vst.model.Answer;
import com.vst.service.HintManager;
import com.vst.service.QuestionManager;
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.util.ArrayList;
import java.util.List;
import java.util.Locale;
public class HintFormController extends BaseFormController {
private HintManager hintManager = null;
private QuestionManager questionManager = null;
public void setQuestionManager(QuestionManager questionManager) {
this.questionManager = questionManager;
}
public void setHintManager(HintManager hintManager) {
this.hintManager = hintManager;
}
public HintFormController() {
setCommandName("hint");
setCommandClass(Hint.class);
}
protected Object formBackingObject(HttpServletRequest request)
throws Exception {
request.setAttribute("questionList", questionManager.getQuestions(null));
if (!isFormSubmission(request)) {
String hintId = request.getParameter("hintId");
Hint hint = null;
if (!StringUtils.isEmpty(hintId)) {
hint = hintManager.getHint(hintId);
hintManager.evict(hint);
} else {
hint = new Hint();
}
if (request.getParameter("edited") != null) {
request.setAttribute("addition", "?edited=1");
hint.setEdited(true);
}
hint.setDocLocation(request.getParameter("docLocation"));
return hint;
}
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...");
}
Hint hint = (Hint) command;
boolean isNew = (hint.getHintId() == null);
Locale locale = request.getLocale();
if (request.getParameter("delete") != null) {
hintManager.removeHint(hint.getHintId().toString());
saveMessage(request, getText("hint.deleted", locale));
} else {
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(FileHelper.getCurrentPath(request) + fileName);
byte[] bufer = new byte[62000];
while (imageStream.read(bufer) != -1) {
fileOutputStream.write(bufer);
}
fileOutputStream.close();
hint.setWayToPhoto(fileName);
hint.setPhotoBlob(Hibernate.createBlob(new FileInputStream(FileHelper.getCurrentPath(request) + fileName)));
InputStream imageSmallStream = ImageUtil.scaleImage(new FileInputStream(FileHelper.getCurrentPath(request) + fileName), 50, 50);
String fileSmallName = ImageUtil.getUniqueJPEGFile(request);
fileOutputStream = new FileOutputStream(FileHelper.getCurrentPath(request) + fileSmallName);
bufer = new byte[62000];
while (imageSmallStream.read(bufer) != -1) {
fileOutputStream.write(bufer);
}
fileOutputStream.close();
hint.setWayToSmallPhoto(fileSmallName);
hint.setPhotoSmallBlob(Hibernate.createBlob(new FileInputStream(FileHelper.getCurrentPath(request) + fileSmallName)));
} else {
if (!isNew) {
Hint curHint = hintManager.getHint(hint.getHintId().toString());
hint.setWayToSmallPhoto(curHint.getWayToSmallPhoto());
hint.setPhotoSmallBlob(curHint.getPhotoSmallBlob());
hint.setWayToPhoto(curHint.getWayToPhoto());
hint.setPhotoBlob(curHint.getPhotoBlob());
hintManager.evict(curHint);
}
}
List questionList = hint.getQuestions();
List baseQuestionList = new ArrayList();
for (int i = 0; i < questionList.size(); i++) {
Question question = (Question) questionList.get(i);
baseQuestionList.add(questionManager.getQuestion(question.getQuestionId().toString()));
}
hint.setQuestions(baseQuestionList);
hintManager.saveHint(hint);
String key = (isNew) ? "hint.added" : "hint.updated";
saveMessage(request, getText(key, locale));
if (hint.isEdited()) {
return new ModelAndView("redirect:updating.html?id=" + hint.getHintId() + "&fieldId=" + request.getParameter("fieldId"));
}
}
return new ModelAndView("redirect:" + hint.getDocLocation());
}
public ModelAndView processFormSubmission(HttpServletRequest request,
HttpServletResponse response,
Object command,
BindException errors)
throws Exception {
Hint hint = (Hint) command;
if (request.getParameter("addQuestion") != null) {
hint.getQuestions().add(new Question());
return new ModelAndView("hintForm", "hint", hint);
}
if (request.getParameter("deleteQuestion") != null) {
hint.getQuestions().remove(hint.getQuestionNumber());
return new ModelAndView("hintForm", "hint", hint);
}
return super.processFormSubmission(request,
response,
command,
errors);
}
}