package com.vst.webapp.action;
import com.vst.Constants;
import com.vst.model.ConstructionExample;
import com.vst.model.ObjectConstruction;
import com.vst.model.Strength;
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.ObjectPicture;
import com.vst.webapp.util.Symbols;
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.*;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;
public class StrengthController implements Controller {
private final Log log = LogFactory.getLog(StrengthController.class);
private StrengthManager strengthManager = null;
private ConstructionExampleManager constructionExampleManager = null;
private ObjectConstructionManager objectConstructionManager=null;
public void setObjectConstructionManager(ObjectConstructionManager objectConstructionManager) {
this.objectConstructionManager = objectConstructionManager;
}
public void setConstructionExampleManager(ConstructionExampleManager constructionExampleManager) {
this.constructionExampleManager = constructionExampleManager;
}
public void setStrengthManager(StrengthManager strengthManager) {
this.strengthManager = strengthManager;
}
public ModelAndView handleRequest(HttpServletRequest request,
HttpServletResponse response)
throws Exception {
String pageNumber = request.getParameter("d-8260813-p");
if (pageNumber != null) {
request.setAttribute("pageNumber", "d-8260813-p=" + pageNumber);
}
if (log.isDebugEnabled()) {
log.debug("entering 'handleRequest' method...");
}
if (request.getParameter("edited") != null) {
request.setAttribute("addition", "?edited=1");
}
if (request.getParameter("delete") != null) {
String strengthId = request.getParameter("id");
ResourceBundle bundle = ResourceBundle.getBundle("ApplicationResources");
boolean hasFatalError = false;
try {
Strength strength = strengthManager.getStrength(strengthId);
ConstructionExample constructionExample = constructionExampleManager.getConstructionExample(strength.getExampleId().toString());
constructionExample.getStrengthPoints().remove(strength);
ObjectConstruction objectConstruction = objectConstructionManager.getObjectConstruction(constructionExample.getObjectConstructionId().toString());
try {
ObjectInputStream in = new ObjectInputStream(new FileInputStream(FileHelper.getCurrentPath(request)+objectConstruction.getWayToDefectMap()));
ObjectPicture objectPicture=new ObjectPicture();
try {
objectPicture = (ObjectPicture) in.readObject();
if (objectPicture != null) {
List list=objectPicture.getObjectList();
for (int l=0; l<list.size(); l++){
if (list.get(l) instanceof Symbols){
Symbols symbols=(Symbols)list.get(l);
if (symbols.getType().equals("point") && symbols.getStrength().getPointId()!=null && symbols.getStrength().getPointId().equals(strength.getPointId())){
objectPicture.deleteObject(symbols);
}
}
}
}
} catch (ClassNotFoundException e) {
}
in.close();
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(FileHelper.getCurrentPath(request)+objectConstruction.getWayToDefectMap()));
out.writeObject(objectPicture);
out.close();
} catch (IOException e) {
}
strengthManager.removeStrength(strengthId);
constructionExampleManager.saveConstructionExample(constructionExample);
request.setAttribute("message", bundle.getString("strength.deleted"));
return new ModelAndView("redirect:strengths.html?exampleId=" + request.getParameter("exampleId"));
} catch (Exception e) {
hasFatalError = true;
}
if (hasFatalError) {
request.setAttribute("errormessage", bundle.getString("commonMistake"));
}
}
String exampleId = request.getParameter("exampleId");
List strengths = new ArrayList();
if (exampleId != null) {
strengths = strengthManager.getStrengthsByExampleId(exampleId);
}
return new ModelAndView("strengthList", Constants.STRENGTH_LIST, strengths);
}
}