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;
public class IndexController implements Controller {
private BuildingObjectManager buildingObjectManager = null;
private UserManager userManager = null;
public void setUserManager(UserManager userManager) {
this.userManager = userManager;
}
public void setBuildingObjectManager(BuildingObjectManager buildingObjectManager) {
this.buildingObjectManager = buildingObjectManager;
}
private ConstructionExampleManager constructionExampleManager = null;
public void setConstructionExampleManager(ConstructionExampleManager constructionExampleManager) {
this.constructionExampleManager = constructionExampleManager;
}
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;
}
public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
httpServletRequest.getSession().setAttribute("currentUserId", userManager.getUserByUsername(httpServletRequest.getRemoteUser()).getId());
httpServletRequest.getSession().setAttribute("isAdmin", new Boolean(httpServletRequest.isUserInRole("admin")));
List buildingObjects = buildingObjectManager.getBuildingObjectsForTree();
prepareForView(buildingObjects);
List buildingObjectTypes = groupByRegionNames(buildingObjects);
httpServletRequest.getSession().setAttribute("pType", "list" );
httpServletRequest.getSession().setAttribute("pId", "-1" );
httpServletRequest.getSession().setAttribute("buildingObjectTypes", buildingObjectTypes);
httpServletRequest.getSession().setAttribute("currentUserId", userManager.getUserByUsername(httpServletRequest.getRemoteUser()).getId());
httpServletRequest.getSession().setAttribute("isAdmin", Boolean.valueOf(httpServletRequest.isUserInRole("admin")));
return new ModelAndView("mainFrame");
}
}