package com.vst.webapp.action;
import com.vst.model.ConstructionExample;
import com.vst.model.ObjectConstruction;
import com.vst.service.ConstructionExampleManager;
import com.vst.service.DangerCategoryManager;
import com.vst.service.ObjectConstructionManager;
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.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.sql.Blob;
import java.util.Locale;
public class ConstructionExampleFormController extends BaseFormController {
private ConstructionExampleManager constructionExampleManager = null;
private ObjectConstructionManager objectConstructionManager = null;
private DangerCategoryManager dangerCategoryManager = null;
public void setDangerCategoryManager(DangerCategoryManager dangerCategoryManager) {
this.dangerCategoryManager = dangerCategoryManager;
}
public void setObjectConstructionManager(ObjectConstructionManager objectConstructionManager) {
this.objectConstructionManager = objectConstructionManager;
}
public void setConstructionExampleManager(ConstructionExampleManager constructionExampleManager) {
this.constructionExampleManager = constructionExampleManager;
}
public ConstructionExampleFormController() {
setCommandName("constructionExample");
setCommandClass(ConstructionExample.class);
}
protected Object formBackingObject(HttpServletRequest request)
throws Exception {
if (!isFormSubmission(request)) {
String exampleId = request.getParameter("exampleId");
ConstructionExample constructionExample = null;
String typeId = "-1";
if(request.getParameter("typeId") != null)
typeId = request.getParameter("typeId");
else if(request.getAttribute("typeId") != null)
typeId = request.getAttribute("typeId").toString();
request.setAttribute("dangerCategoryList", dangerCategoryManager.getDangerCategorys(null));
if (!StringUtils.isEmpty(exampleId)) {
constructionExample = constructionExampleManager.getConstructionExample(exampleId);
//constructionExampleManager.evict(constructionExample);
} else {
constructionExample = new ConstructionExample();
//generating the unique number of construction
Integer number =constructionExampleManager.getExampleNumberByConstructionObjectId(new Integer(typeId));
constructionExample.setExampleRelativeNumber(number);
}
if (typeId != null) {
// constructionExample.setConstructionType(((ObjectConstruction)objectConstructionManager.getObjectConstruction(typeId)).getConstructionType());
constructionExample.setObjectConstructionId(new Integer(typeId));
}
constructionExampleManager.makeNotNull(constructionExample);
if (request.getParameter("edited") != null) {
request.setAttribute("addition", "?edited=1");
constructionExample.setEdited(true);
}
//checking esciz
if (constructionExample.getWayToDefectEsckiz() == null || constructionExample.getWayToDefectEsckiz().equals("")) {
//retrieving esciz from object construction
ObjectConstruction objectConstruction = objectConstructionManager.getObjectConstruction(constructionExample.getObjectConstructionId().toString());
//esciz for defects
if (objectConstruction.getEsckizBlob() != null && objectConstruction.getWayToEsckiz() != null && !objectConstruction.getWayToEsckiz().equals("")) {
String wayToDefectEskiz = ImageUtil.getUniqueJPEGFile(request);
constructionExample.setWayToDefectEsckiz(wayToDefectEskiz);
objectConstruction.getEsckizBlob();
File file = new File(FileHelper.getCurrentPath(request) + wayToDefectEskiz);
file.createNewFile();
FileOutputStream fileOutputStream = new FileOutputStream(FileHelper.getCurrentPath(request) + wayToDefectEskiz);
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();
//esciz for power points
String wayToPowerEskiz = ImageUtil.getUniqueJPEGFile(request);
constructionExample.setWayToPowerEsckiz(wayToPowerEskiz);
objectConstruction.getEsckizBlob();
file = new File(FileHelper.getCurrentPath(request) + wayToPowerEskiz);
if (file.exists()) {
file.delete();
}
file.createNewFile();
fileOutputStream = new FileOutputStream(FileHelper.getCurrentPath(request) + wayToPowerEskiz);
blob = objectConstruction.getEsckizBlob();
byte[] powerBufer = new byte[62000];
inputStream = blob.getBinaryStream();
while (inputStream.read(powerBufer) != -1) {
fileOutputStream.write(powerBufer);
}
inputStream.close();
fileOutputStream.close();
}
}
constructionExample.setDocLocation(request.getParameter("docLocation"));
return constructionExample;
} else {
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...");
}
ConstructionExample constructionExample = (ConstructionExample) command;
boolean isNew = (constructionExample.getExampleId() == null);
Locale locale = request.getLocale();
if (request.getParameter("delete") != null) {
ObjectConstruction objectConstruction = objectConstructionManager.getObjectConstruction(constructionExample.getObjectConstructionId().toString());
objectConstruction.getConstructionExamples().remove(constructionExample);
objectConstructionManager.saveObjectConstruction(objectConstruction);
saveMessage(request, getText("constructionExample.deleted", locale));
} else {
Integer dangerId = constructionExample.getDangerCategory().getDangerCategoryId();
if (!dangerId.equals(new Integer(-1))) {
constructionExample.setDangerCategory(dangerCategoryManager.getDangerCategory(dangerId.toString()));
} else {
constructionExample.setDangerCategory(null);
}
ObjectConstruction objectConstruction = objectConstructionManager.getObjectConstruction(constructionExample.getObjectConstructionId().toString());
objectConstructionManager.evict(objectConstruction);
constructionExample.setConstructionType(objectConstruction.getConstructionType());
if (constructionExample.getWayToPowerEsckiz() != null && !constructionExample.getWayToPowerEsckiz().equals("") && (new File(FileHelper.getCurrentPath(request) + constructionExample.getWayToDefectEsckiz())).exists()) {
constructionExample.setDefectEsckizBlob(Hibernate.createBlob(new FileInputStream(FileHelper.getCurrentPath(request) + constructionExample.getWayToDefectEsckiz())));
}
if (constructionExample.getWayToPowerEsckiz() != null && !constructionExample.getWayToPowerEsckiz().equals("") && (new File(FileHelper.getCurrentPath(request) + constructionExample.getWayToPowerEsckiz())).exists()) {
constructionExample.setPowerEsckizBlob(Hibernate.createBlob(new FileInputStream(FileHelper.getCurrentPath(request) + constructionExample.getWayToPowerEsckiz())));
}
//restoring construction examples associations
if (!isNew) {
ConstructionExample oldConstructionExample = constructionExampleManager.getConstructionExample(constructionExample.getExampleId().toString());
constructionExample.setExampleDefects(oldConstructionExample.getExampleDefects());
constructionExample.setConstructionType(oldConstructionExample.getConstructionType());
constructionExample.setStrengthPoints(oldConstructionExample.getStrengthPoints());
constructionExample.setDefectEsckizBlob(oldConstructionExample.getDefectEsckizBlob());
constructionExample.setWayToDefectEsckiz(oldConstructionExample.getWayToDefectEsckiz());
constructionExample.setWayToPowerEsckiz(oldConstructionExample.getWayToPowerEsckiz());
constructionExample.setPowerEsckizBlob(oldConstructionExample.getPowerEsckizBlob());
constructionExampleManager.evict(oldConstructionExample);
}
if (isNew) {
constructionExample.setBuildObjectId(objectConstruction.getObjectId());
objectConstruction.addConstructionExample(constructionExample);
objectConstructionManager.saveObjectConstruction(objectConstruction);
} else {
System.out.println("editing example"+constructionExample.getExampleId());
constructionExampleManager.saveConstructionExample(constructionExample);
}
String key = (isNew) ? "constructionExample.added" : "constructionExample.updated";
saveMessage(request, getText(key, locale));
if (constructionExample.isEdited()) {
return new ModelAndView("redirect:updating.html?id=" + constructionExample.getExampleId() + "&fieldId=" + request.getParameter("fieldId"));
}
}
request.getSession().setAttribute("madeChanges", new Boolean(true));
return new ModelAndView("redirect:" + constructionExample.getDocLocation());
}
public ModelAndView processFormSubmission(HttpServletRequest request,
HttpServletResponse response,
Object command,
BindException errors)
throws Exception {
ConstructionExample constructionExample = (ConstructionExample) command;
if (request.getParameter("cancel") != null) {
String addition = "";
if (constructionExample.getExampleId() != null) {
addition = "&exampleId=" + constructionExample.getExampleId();
}
return new ModelAndView("redirect: constructionExamples.html?typeId=" + constructionExample.getObjectConstructionId() + addition);
}
return super.processFormSubmission(request, response, command, errors);
}
}