package com.vst.webapp.action;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.servlet.ModelAndView;
import com.vst.Constants;
import com.vst.model.BuildingObject;
import com.vst.model.ConstructionExample;
import com.vst.model.ObjectConstruction;
import com.vst.service.BuildingObjectManager;
import com.vst.service.ConstructionExampleManager;
import com.vst.service.ElementCopyManager;
import com.vst.service.ObjectConstructionManager;
import com.vst.webapp.util.ImageUtil;
public class ConstructionExampleController extends AbstractVstController {
protected static final Log log = LogFactory.getLog(ConstructionExampleController.class);
private ConstructionExampleManager constructionExampleManager = null;
private ObjectConstructionManager objectConstructionManager = null;
private BuildingObjectManager buildingObjectManager = null;
private ElementCopyManager elementCopyManager = null;
private ObjectConstruction selectedObjectConstruction;
public void setElementCopyManager(ElementCopyManager elementCopyManager) {
this.elementCopyManager = elementCopyManager;
}
public void setBuildingObjectManager(BuildingObjectManager buildingObjectManager) {
this.buildingObjectManager = buildingObjectManager;
}
public void setObjectConstructionManager(ObjectConstructionManager objectConstructionManager) {
this.objectConstructionManager = objectConstructionManager;
}
public void setConstructionExampleManager(ConstructionExampleManager constructionExampleManager) {
this.constructionExampleManager = constructionExampleManager;
}
private void processParamPageNumber(HttpServletRequest request,
HttpServletResponse response) {
String pageNumber = request.getParameter("d-2061369-p");
if (pageNumber != null) {
request.setAttribute("pageNumber", "d-2061369-p=" + pageNumber);
}
}
private void processParamElementCopy(HttpServletRequest request,
HttpServletResponse response) throws IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException {
if (request.getParameter("elementCopy") != null) {
ObjectConstruction objectConstruction = objectConstructionManager.getObjectConstruction(request.getParameter("typeId"));
ConstructionExample constructionExample = constructionExampleManager.getConstructionExample(request.getParameter("id"));
ConstructionExample newConstructionExample = (ConstructionExample) elementCopyManager.getNewObject(constructionExample);
objectConstruction.getConstructionExamples().add(newConstructionExample);
constructionExampleManager.saveConstructionExample(newConstructionExample);
objectConstructionManager.saveObjectConstruction(objectConstruction);
elementCopyManager.setNumZerro();
}
}
private void processParamTypeId(HttpServletRequest request,
HttpServletResponse response) {
if (extractParamTypeId(request) != null) {
ObjectConstruction objectConstruction = objectConstructionManager.getObjectConstructionForBreadCrump(extractParamTypeId(request));
//objectConstructionManager.evict(objectConstruction);
if(objectConstruction.getConstructionType()!=null)
request.getSession().setAttribute("constructionTypeName", objectConstruction.getConstructionType().getName());
if(objectConstruction.getTypeId()!=null)
request.getSession().setAttribute("constructionTypeNameRef", "objectConstructions.html?typeId=" + objectConstruction.getTypeId() + "&objectId=" + objectConstruction.getObjectId());
Integer buildingObjectId = null;
if(objectConstruction.getObjectId()!=null)
buildingObjectId = objectConstruction.getObjectId();
//setting the object to the context
BuildingObject buildingObject = buildingObjectManager.getBuildingObjectForBreadCrump(buildingObjectId.toString());
//buildingObjectManager.evict(buildingObject);
request.getSession().setAttribute("buildingObjectName", buildingObject.getName());
request.getSession().setAttribute("buildingObjectNameRef", "buildingObjects.html?objectId=" + buildingObject.getObjectId());
}
}
private void processParamExampleId(HttpServletRequest request,
HttpServletResponse response) {
//creating refference for construction example
if (request.getParameter("exampleId") != null) {
ConstructionExample constructionExample = constructionExampleManager.getConstructionExampleForBreadCrump(request.getParameter("exampleId"));
request.getSession().setAttribute("constructionExampleName", (constructionExample.getExampleName() == null || constructionExample.getExampleName().trim().equals("")) ? String.valueOf(constructionExample.getExampleRelativeNumber()) : constructionExample.getExampleName());
System.out.println("CENRef2"+constructionExample.getObjectConstructionId()+constructionExample);
request.getSession().setAttribute("constructionExampleNameRef", "constructionExamples.html?exampleId=" + constructionExample.getExampleId() + "&typeId=" + constructionExample.getObjectConstructionId());
} else {
//destroying construction example refference in breadcrump
request.getSession().setAttribute("constructionExampleName", "");
request.getSession().setAttribute("constructionExampleNameRef", "");
}
}
private void doRemoveConstructionExample(HttpServletRequest request,
HttpServletResponse response){
String constructionExampleId = request.getParameter("id");
ResourceBundle bundle = ResourceBundle.getBundle("ApplicationResources");
boolean hasFatalError = false;
try {
ConstructionExample constructionExample = constructionExampleManager.getConstructionExample(constructionExampleId);
ObjectConstruction objectConstruction = objectConstructionManager.getObjectConstruction(constructionExample.getObjectConstructionId().toString());
objectConstruction.getConstructionExamples().remove(constructionExample);
constructionExampleManager.removeConstructionExample(constructionExampleId);
objectConstructionManager.saveObjectConstruction(objectConstruction);
request.getSession().setAttribute("madeChanges", new Boolean(true));
request.setAttribute("message", bundle.getString("constructionExample.deleted"));
} catch (Exception e) {
hasFatalError = true;
}
if (hasFatalError) {
request.setAttribute("errormessage", bundle.getString("commonMistake"));
}
}
private String extractParamTypeId(HttpServletRequest request) {
return request.getParameter("typeId");
}
private void setSelectedObjectConstruction(HttpServletRequest request) {
String typeId = extractParamTypeId(request);
selectedObjectConstruction=objectConstructionManager.getObjectConstruction(typeId);
if(selectedObjectConstruction.getWayToDefectMap()==null){
//setting the way to defect esckiz
//TODO Remove it as soon as object constructions data will be updated and will have all the ways to eskiz
selectedObjectConstruction.setWayToDefectMap(ImageUtil.getUniqueJPEGFile(request));
}
}
private List<?> prepareExamplesListForView(HttpServletRequest request,
HttpServletResponse response){
List constructionExamples = new ArrayList();
String typeId = extractParamTypeId(request);
String objectId = request.getParameter("objectId");
boolean forObject = request.getParameter("forObject") != null;
if (typeId != null) {
request.setAttribute("addition", "?typeId=" + typeId);
}
if (typeId != null && !forObject) {
constructionExamples = constructionExampleManager.getConstructionExamplesForTable(null, new Integer(typeId));
} else {
if (objectId != null) {
constructionExamples = constructionExampleManager.getConstructionExamplesForTable(new Integer(objectId), null);
}
}
for (int i = 0; i < constructionExamples.size(); i++) {
ConstructionExample constructionExample = (ConstructionExample) constructionExamples.get(i);
constructionExample.setObjectConstructionId(new Integer(typeId));
constructionExamples.set(i, constructionExample);
}
// constructionExamples = constructionExampleManager.fillRecomendedDangerCategories(constructionExamples));
// constructionExamples = constructionExampleManager.fillConstractionExampleData(constructionExamples, new Integer(request.getParameter("exampleId") != null ? request.getParameter("exampleId") : "-1"));
if (constructionExamples.size() > 0) {
ConstructionExample firstExample = (ConstructionExample) constructionExamples.get(0);
request.setAttribute("constructionType", selectedObjectConstruction.getConstructionType());
request.setAttribute("material", selectedObjectConstruction.getConstructionType().getMaterial());
}
/*<input type="image"
src="<c:url value="/images/photo.png"/>"
value="<fmt:message key="objectConstruction.draw"/>"
onclick="var wind=window.open('painter.html?savePath=<c:out value="${saveWay}"/>&savePathImage=<c:out value="${saveWayImage}"/>&loadPath=<c:out value="${loadWay}"/>',null,'height=600,width=800,toolbar=no');wind.moveTo(0,0);return false;"/>
*/
return constructionExamples;
}
public ModelAndView handleRequest(HttpServletRequest request,
HttpServletResponse response)
throws Exception {
if (log.isDebugEnabled()) {
log.debug("entering 'handleRequest' method...");
}
//objectConstructionId typeId
processParamPageNumber(request, response);
if (isDeleteParamPresent(request)) {
doRemoveConstructionExample(request,response);
if(request.getAttribute("errormessage") == null){
return new ModelAndView("redirect:constructionExamples.html?typeId=" + extractParamTypeId(request));
}
}
processParamElementCopy(request, response);
processParamTypeId(request, response);
processParamExampleId(request, response);
//destroying refference for defects
request.getSession().setAttribute("constructionDefectName", "");
processParamEdited(request, response);
setSelectedObjectConstruction(request);
List constructionExamples = prepareExamplesListForView(request, response);
List objectConstructions = new ArrayList();
objectConstructions = (List)request.getSession().getAttribute(Constants.OBJECTCONSTRUCTION_LIST);
request.getSession().setAttribute("pType", "constructionExample");
request.getSession().setAttribute("pId", request.getParameter("exampleId"));
ModelAndView modelAndView=new ModelAndView("constructionExampleList");
modelAndView.addObject(Constants.OBJECTCONSTRUCTION_LIST, objectConstructions);
modelAndView.addObject(Constants.CONSTRUCTIONEXAMPLE_LIST, constructionExamples) ;
modelAndView.addObject("loadWay", selectedObjectConstruction.getWayToDefectMap()) ;
modelAndView.addObject("savePath", selectedObjectConstruction.getWayToDefectMap()) ;
modelAndView.addObject("savePathImage", selectedObjectConstruction.getWayToDefectMapImage()) ;
modelAndView.addObject("constructionId", selectedObjectConstruction.getConstructionType().getConstructionTypeId()) ;
modelAndView.addObject("objectConstructionId", selectedObjectConstruction.getTypeId()) ;
return modelAndView;
}
}