package com.vst.webapp.action;
import com.vst.model.ConstructionExample;
import com.vst.model.ObjectConstruction;
import com.vst.model.Strength;
import com.vst.model.DetailSymbol;
import com.vst.service.BuildingObjectManager;
import com.vst.service.ConstructionExampleManager;
import com.vst.service.ObjectConstructionManager;
import com.vst.service.StrengthManager;
import com.vst.util.FileHelper;
import com.vst.webapp.util.*;
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.*;
import java.util.List;
import java.util.Locale;
public class StrengthFormController extends BaseFormController {
private StrengthManager strengthManager = null;
private ConstructionExampleManager constructionExampleManager = null;
private BuildingObjectManager buildingObjectManager = null;
private ObjectConstructionManager objectConstructionManager = null;
public void setObjectConstructionManager(ObjectConstructionManager objectConstructionManager) {
this.objectConstructionManager = objectConstructionManager;
}
public void setBuildingObjectManager(BuildingObjectManager buildingObjectManager) {
this.buildingObjectManager = buildingObjectManager;
}
public void setConstructionExampleManager(ConstructionExampleManager constructionExampleManager) {
this.constructionExampleManager = constructionExampleManager;
}
public void setStrengthManager(StrengthManager strengthManager) {
this.strengthManager = strengthManager;
}
public StrengthFormController() {
setCommandName("strength");
setCommandClass(Strength.class);
}
protected Object formBackingObject(HttpServletRequest request)
throws Exception {
String pointId = request.getParameter("pointId");
String exampleId = request.getParameter("exampleId");
Strength strength = null;
if (!StringUtils.isEmpty(pointId)) {
strength = strengthManager.getStrength(pointId);
strengthManager.evict(strength);
} else {
strength = new Strength();
//generating the number of point
strength.setPointRelativeNumber(new Integer(strengthManager.getPointNumber(exampleId)));
}
if (!StringUtils.isEmpty(exampleId)) {
strength.setExampleId(new Integer(exampleId));
}
if (request.getParameter("edited") != null) {
request.setAttribute("addition", "?edited=1");
strength.setEdited(true);
}
//checking if the esciz already exists
if (strength.getWayToEskiz() == null || strength.getWayToEskiz().equals("")) {
//loading eskiz from objectconstruction
ConstructionExample constructionExample = constructionExampleManager.getConstructionExample(strength.getExampleId().toString());
constructionExampleManager.evict(constructionExample);
strength.setWayToEskiz(constructionExample.getWayToPowerEsckiz());
}
request.setAttribute("saveWay", strength.getWayToEskiz());
request.setAttribute("loadWay", strength.getWayToEskiz());
strength.setDocLocation(request.getParameter("docLocation"));
return strength;
}
public ModelAndView onSubmit(HttpServletRequest request,
HttpServletResponse response, Object command,
BindException errors)
throws Exception {
if (log.isDebugEnabled()) {
log.debug("entering 'onSubmit' method...");
}
Strength strength = (Strength) command;
boolean isNew = (strength.getPointId() == null);
Locale locale = request.getLocale();
if (request.getParameter("delete") != null) {
Integer exampleId = strength.getExampleId();
// strengthManager.removeStrength(strength.getPointId().toString());
ConstructionExample constructionExample = constructionExampleManager.getConstructionExample(exampleId.toString());
constructionExample.getStrengthPoints().remove(strength);
// constructionExampleManager.saveConstructionExample(constructionExample);
saveMessage(request, getText("strength.deleted", locale));
} else {
//adding this point to the list of the construction example
ConstructionExample constructionExample1 = constructionExampleManager.getConstructionExample(strength.getExampleId().toString());
if (constructionExample1.getWayToPowerEsckiz() != null) {
File eskizFile = new File(FileHelper.getCurrentPath(request) + constructionExample1.getWayToPowerEsckiz());
if (eskizFile.exists()) {
constructionExample1.setPowerEsckizBlob(Hibernate.createBlob(new FileInputStream(FileHelper.getCurrentPath(request) + constructionExample1.getWayToPowerEsckiz())));
}
}
constructionExampleManager.saveConstructionExample(constructionExample1);
constructionExampleManager.evict(constructionExample1);
if (isNew) {
Integer exampleId = strength.getExampleId();
ConstructionExample constructionExample = constructionExampleManager.getConstructionExample(exampleId.toString());
// BuildingObject buildingObject=buildingObjectManager.getBuildingObject(constructionExample.getObjectId().toString());
constructionExample.getStrengthPoints().add(strength);
constructionExampleManager.saveConstructionExample(constructionExample);
// buildingObjectManager.saveBuildingObject(buildingObject);
// constructionExampleManager.saveConstructionExample(constructionExample);
ObjectConstruction objectConstruction = objectConstructionManager.getObjectConstruction(constructionExample.getObjectConstructionId().toString());
String fileName = FileHelper.getCurrentPath(request) + objectConstruction.getWayToDefectMap();
paintStrength(constructionExample, strength, fileName,"save");
} else {
strengthManager.saveStrength(strength);
Integer exampleId = strength.getExampleId();
ConstructionExample constructionExample = constructionExampleManager.getConstructionExample(exampleId.toString());
ObjectConstruction objectConstruction = objectConstructionManager.getObjectConstruction(constructionExample.getObjectConstructionId().toString());
String fileName = FileHelper.getCurrentPath(request) + objectConstruction.getWayToDefectMap();
paintStrength(constructionExample, strength, fileName,"update");
}
String key = (isNew) ? "strength.added" : "strength.updated";
saveMessage(request, getText(key, locale));
if (strength.isEdited()) {
return new ModelAndView("redirect:updating.html?id=" + strength.getPointId() + "&fieldId=" + request.getParameter("fieldId"));
}
}
return new ModelAndView("redirect:" + strength.getDocLocation());
}
private void paintStrength(ConstructionExample constructionExample, Strength strength, String fileName, String action) {
try {
ObjectInputStream in = new ObjectInputStream(new FileInputStream(fileName));
ObjectPicture objectPicture = new ObjectPicture();
try {
objectPicture = (ObjectPicture) in.readObject();
if (objectPicture != null) {
if (action.equals("update")) {
List list = objectPicture.getObjectList();
for (int i = 0; i < list.size(); i++) {
if (list.get(i) instanceof Symbols) {
Symbols symbols = (Symbols) list.get(i);
if (symbols.getType().equals("point") && symbols.getStrength().getPointId().equals(strength.getPointId())) {
List l=symbols.getList();
for (int j=0; j<l.size(); j++){
if (l.get(j) instanceof TextLine){
TextLine textLine =(TextLine) l.get(j);
symbols.getList().remove(textLine);
j--;
}
}
MyPoint point=symbols.getMinPoint();
symbols.getList().add(new TextLine(point.x, point.y-5, String.valueOf(strength.getStrengthValue())));
symbols.getList().add(new TextLine(point.x, point.y-15,String.valueOf("№ " + strength.getPointRelativeNumber())));
symbols.setExample(constructionExample);
symbols.setStrength(strength);
symbols.setActivity("old");
symbols.setType("point");
}
}
}
} else if (action.equals("save")) {
Symbols symbols = new Symbols(createStrength(strength));
symbols.setExample(constructionExample);
symbols.setStrength(strength);
symbols.setActivity("old");
symbols.setType("point");
objectPicture.addObject(symbols);
}
}
} catch (ClassNotFoundException e) {
}
in.close();
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(fileName));
out.writeObject(objectPicture);
out.close();
} catch (IOException e) {
}
}
private ObjectPicture createStrength(Strength strength) {
ObjectPicture objectPicture1 = new ObjectPicture();
Line line = new Line();
line.setFirstPoint(new MyPoint(100, 100));
line.setSecondPoint(new MyPoint(110, 110));
objectPicture1.addObject(line);
line = new Line();
line.setFirstPoint(new MyPoint(110, 100));
line.setSecondPoint(new MyPoint(100, 110));
objectPicture1.addObject(line);
line = new Line();
line.setFirstPoint(new MyPoint(105, 100));
line.setSecondPoint(new MyPoint(105, 110));
objectPicture1.addObject(line);
line = new Line();
line.setFirstPoint(new MyPoint(110, 105));
line.setSecondPoint(new MyPoint(100, 105));
objectPicture1.addObject(line);
objectPicture1.addObject(new TextLine(100, 95, String.valueOf(strength.getStrengthValue())));
objectPicture1.addObject(new TextLine(100, 85, String.valueOf("№ " + strength.getPointRelativeNumber())));
return objectPicture1;
}
}