package com.vst.webapp.action;
import com.vst.dto.ConstructionTypeDTO;
import com.vst.dto.DefectCategoryDTO;
import com.vst.dto.DefectVarityDTO;
import com.vst.dto.DefectZoneDTO;
import com.vst.model.*;
import com.vst.service.*;
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.util.ArrayList;
import java.util.List;
/**
* Created by IntelliJ IDEA.
* User: ALEXEI
* Date: 11.04.2008
* Time: 10:53:49
* To change this template use File | Settings | File Templates.
*/
public class DefectTreeController implements Controller {
private DefectTypeManager defectTypeManager;
private DefectCategoryManager defectCategoryManager;
private DefectRecomendationManager defectRecomendationManager;
private DefectZoneManager defectZoneManager;
private ConstructionTypeManager constructionTypeManager;
private ReasonManager reasonManager;
public void setReasonManager(ReasonManager reasonManager) {
this.reasonManager = reasonManager;
}
public void setConstructionTypeManager(ConstructionTypeManager constructionTypeManager) {
this.constructionTypeManager = constructionTypeManager;
}
public void setDefectZoneManager(DefectZoneManager defectZoneManager) {
this.defectZoneManager = defectZoneManager;
}
public void setDefectRecomendationManager(DefectRecomendationManager defectRecomendationManager) {
this.defectRecomendationManager = defectRecomendationManager;
}
public void setDefectCategoryManager(DefectCategoryManager defectCategoryManager) {
this.defectCategoryManager = defectCategoryManager;
}
public void setDefectTypeManager(DefectTypeManager defectTypeManager) {
this.defectTypeManager = defectTypeManager;
}
public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
List defectTypes = defectTypeManager.getDefectTypes(null);
for (int i = 0; i < defectTypes.size(); i++) {
DefectType defectType = (DefectType) defectTypes.get(i);
List varityList = defectType.getVarities();
List varityDTOList = new ArrayList();
for (int j = 0; j < varityList.size(); j++) {
DefectVarityDTO defectVarity = new DefectVarityDTO((DefectVarity) varityList.get(j));
varityDTOList.add(defectVarity);
List constructionTypeList = constructionTypeManager.getConstructionTypesByDefectVarityAndDefectType(defectVarity.getVarityId().toString(), defectType.getDefectTypeId().toString());
// adding reasons for defect zones
List constructionTypeDTOList = new ArrayList();
for (int k = 0; k < constructionTypeList.size(); k++) {
ConstructionTypeDTO constructionType = new ConstructionTypeDTO((ConstructionType) constructionTypeList.get(k));
constructionTypeDTOList.add(constructionType);
List defectZones = constructionType.getDefectZones();
List defectZonesDTOList = new ArrayList();
for (int l = 0; l < defectZones.size(); l++) {
DefectZoneDTO defectZone = new DefectZoneDTO((DefectZone) defectZones.get(l));
defectZonesDTOList.add(defectZone);
defectZone.setReasons(reasonManager.getReasonsByConstructionVarityZone(constructionType.getConstructionTypeId().toString(), defectVarity.getVarityId().toString(), defectZone.getDefectZoneId().toString()));
}
constructionType.setDefectZones(defectZonesDTOList);
// setting defect categories for construction type
List defectCategories = defectCategoryManager.getCategoriesByVarityIdConstructionTypeId(defectVarity.getVarityId().toString(), constructionType.getConstructionTypeId().toString());
List defectCategoryDTOList = new ArrayList();
for (int z = 0; z < defectCategories.size(); z++) {
DefectCategoryDTO defectCategory = new DefectCategoryDTO((DefectCategory) defectCategories.get(z));
defectCategoryDTOList.add(defectCategory);
defectCategory.setDefectRecomendations(defectRecomendationManager.getDefectRecomendationsByCategoryIdConstructionTypeId(defectCategory.getDefectCategoryId().toString(), constructionType.getConstructionTypeId().toString()));
}
constructionType.setDefectCategories(defectCategoryDTOList);
}
defectVarity.setConstructionTypes(constructionTypeDTOList);
}
defectType.setVarities(varityDTOList);
}
httpServletRequest.setAttribute("defectTypes", defectTypes);
return new ModelAndView("defectTree");
}
}