package com.vst.webapp.action;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
import com.vst.dto.BuildingObjectTypeDTO;
import com.vst.model.BuildingObject;
import com.vst.model.ObjectConstruction;
import com.vst.service.BuildingObjectManager;
import com.vst.service.ConstructionExampleManager;
import com.vst.service.UserManager;
/**
* Created by IntelliJ IDEA.
* User: ALEXEI
* Date: 15.04.2008
* Time: 19:31:23
* To change this template use File | Settings | File Templates.
*/
public class TreeController implements Controller {
private BuildingObjectManager buildingObjectManager = null;
private ConstructionExampleManager constructionExampleManager = null;
public void setConstructionExampleManager(ConstructionExampleManager constructionExampleManager) {
this.constructionExampleManager = constructionExampleManager;
}
private UserManager userManager = null;
public void setUserManager(UserManager userManager) {
this.userManager = userManager;
}
public void setBuildingObjectManager(BuildingObjectManager buildingObjectManager) {
this.buildingObjectManager = buildingObjectManager;
}
private String reduceDescription(String description){
if (description == null){
return null;
} else {
return description.length() > 10 ? description.substring(0, 9) + "..." : description;
}
}
private void prepareForView(List<BuildingObject> buildingObjects){
for (int i = 0; i < buildingObjects.size(); i++) {
BuildingObject buildingObject = (BuildingObject) buildingObjects.get(i);
List<?> objectConstructionList = buildingObject.getConstructionTypes();
// OLD buildingObjectManager.getConstructionTypesForTree(buildingObject);
for (int j = 0; j < objectConstructionList.size(); j++) {
ObjectConstruction objectConstruction = (ObjectConstruction) objectConstructionList.get(j);
objectConstruction.setBriefDescription(reduceDescription(objectConstruction.getDescription()));
// objectConstruction.setConstructionExamples(
// constructionExampleManager.getConstructionExamplesLite(
// objectConstruction.getTypeId().toString(), "tree"));
}
// buildingObject.setConstructionTypes(objectConstructionList);
}
}
private List<?> groupByRegionNames(List<BuildingObject> buildingObjects){
List buildingObjectTypes = new ArrayList();
String currentRegion = "";
BuildingObjectTypeDTO buildingObjectTypeDTO = new BuildingObjectTypeDTO();
/////
for (int i = 0; i < buildingObjects.size(); i++) {
BuildingObject buildingObject = (BuildingObject) buildingObjects.get(i);
if (i == 0 || (!currentRegion.equals(buildingObject.getRegion())
&& !(currentRegion == null) && !currentRegion.trim().equals(""))) {
if (i != 0) {
buildingObjectTypes.add(buildingObjectTypeDTO);
}
buildingObjectTypeDTO = new BuildingObjectTypeDTO();
currentRegion = buildingObject.getRegion();
buildingObjectTypeDTO.setObjectType(currentRegion);
}
buildingObjectTypeDTO.getBuildingObjects().add(buildingObject);
}
buildingObjectTypeDTO.setObjectType(currentRegion);
buildingObjectTypes.add(buildingObjectTypeDTO);
return buildingObjectTypes;
}
@SuppressWarnings("unchecked")
public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
List buildingObjects = buildingObjectManager.getBuildingObjectsForTree();
/* test successfull
for(int i=0; i<buildingObjects.size(); i++) {
//System.out.println(" BuildingObject ID ******************** " + ((BuildingObject)buildingObjects.get(i)).getObjectId());
//System.out.println(" BuildingObject NAME ******************** " + ((BuildingObject)buildingObjects.get(i)).getName());
//System.out.println(" BuildingObject REGION ******************** " + ((BuildingObject)buildingObjects.get(i)).getRegion());
} */
prepareForView(buildingObjects);
List buildingObjectTypes = groupByRegionNames(buildingObjects);
httpServletRequest.setAttribute("buildingObjectTypes", buildingObjectTypes);
httpServletRequest.getSession().setAttribute("buildingObjectTypes", buildingObjectTypes);
httpServletRequest.getSession().setAttribute("currentUserId", userManager.getUserByUsername(httpServletRequest.getRemoteUser()).getId());
httpServletRequest.getSession().setAttribute("isAdmin", Boolean.valueOf(httpServletRequest.isUserInRole("admin")));
ModelAndView mav = new ModelAndView("treeMenu");
// if(httpServletRequest.getParameter("treeParams") != null)
// mav.addObject("treeParams", httpServletRequest.getParameter("treeParams"));
return mav;
}
}