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 com.vst.dto.ConstructionTypeDTO;
import com.vst.dto.DefectCategoryDTO;
import com.vst.dto.DefectVarityDTO;
import com.vst.dto.DefectZoneDTO;
import com.vst.model.ConstructionType;
import com.vst.model.DefectCategory;
import com.vst.model.DefectType;
import com.vst.model.DefectVarity;
import com.vst.model.DefectZone;
import com.vst.service.ConstructionTypeManager;
import com.vst.service.DefectCategoryManager;
import com.vst.service.DefectRecomendationManager;
import com.vst.service.DefectTypeManager;
import com.vst.service.DefectZoneManager;
import com.vst.service.ReasonManager;
/**
* Created by IntelliJ IDEA.
* User: ALEXEI
* Date: 30.04.2008
* Time: 23:47:10
* To change this template use File | Settings | File Templates.
*/
public class CommonInformationController extends AbstractVstController {
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;
}
private void prepareDefectTypesForView(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse){
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));
//System.out.println("defectVarity"+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++) {
//System.out.println("constructionType"+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++) {
//System.out.println("defectZones"+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++) {
//System.out.println("defectCategory"+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);
}
private void prepareConstructionTypesForView(HttpServletRequest request, HttpServletResponse response){
List constructionTypes = constructionTypeManager.getConstructionTypes(null);
List constructionTypeDTOList = new ArrayList();
for (int i = 0; i < constructionTypes.size(); i++) {
ConstructionType constructionType = (ConstructionType) constructionTypes.get(i);
ConstructionTypeDTO constructionTypeDTO = new ConstructionTypeDTO(constructionType);
constructionTypeDTOList.add(constructionTypeDTO);
List defectZoneList = constructionType.getDefectZones();
List defectZoneDTOList = new ArrayList();
for (int j = 0; j < defectZoneList.size(); j++) {
DefectZoneDTO defectZoneDTO = new DefectZoneDTO((DefectZone) defectZoneList.get(j));
defectZoneDTOList.add(defectZoneDTO);
List defectCategoryDTOList = new ArrayList();
List defectCategoryList = defectCategoryManager.getDefectCategorysByConstructionTypeIdDefectZoneId(constructionTypeDTO.getConstructionTypeId().toString(), defectZoneDTO.getDefectZoneId().toString());
for (int k = 0; k < defectCategoryList.size(); k++) {
DefectCategory defectCategory = (DefectCategory) defectCategoryList.get(k);
DefectCategoryDTO defectCategoryDTO = new DefectCategoryDTO(defectCategory);
DefectVarity defectVarity = defectCategory.getDefectVarity();
DefectType defectType = defectCategory.getDefectType();
List reasonList = reasonManager.getReasonsByDefectTypeVarityConstructionZone(defectType.getDefectTypeId().toString(), defectVarity.getVarityId().toString(), constructionTypeDTO.getConstructionTypeId().toString(), defectZoneDTO.getDefectZoneId().toString());
// List reasonListDTO=new ArrayList();
// for (int l = 0; l < reasonList.size(); l++) {
// Reason reason = (Reason) reasonListDTO.get(l);
//
//
// }
defectCategoryDTO.setReasons(reasonList);
defectCategoryDTOList.add(defectCategoryDTO);
}
defectZoneDTO.setDefectCategories(defectCategoryDTOList);
}
constructionTypeDTO.setDefectZones(defectZoneDTOList);
}
request.setAttribute("constructionTypes", constructionTypeDTOList);
}
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
String commonParam = request.getParameter("informationParameter");
if ("1".equals(commonParam)) {
prepareDefectTypesForView(request,response);
}
if ("0".equals(commonParam)) {
prepareConstructionTypesForView(request,response);
}
return new ModelAndView("commonInformation");
}
}