package com.vst.service.impl;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.vst.dao.hibernate.XmlGatewayDaoHibernate;
import com.vst.dto.BuildingFormDto;
import com.vst.dto.CustomerFormDto;
import com.vst.dto.CustomerListItemDto;
import com.vst.dto.CustomersNavigationListDto;
import com.vst.dto.GeoTreeItemDto;
import com.vst.model.ConstructionType;
import com.vst.model.DangerCategory;
import com.vst.model.DefectType;
import com.vst.model.DefectZone;
import com.vst.model.Hint;
import com.vst.model.Material;
import com.vst.model.ObjectType;
import com.vst.model.Region;
import com.vst.model.ext.BaseAnnotatedEntity;
import com.vst.model.ext.Building;
import com.vst.model.ext.GeoCity;
import com.vst.model.ext.GeoClassificator;
import com.vst.model.ext.GeoCountry;
import com.vst.model.ext.GeoMacroRegion;
import com.vst.model.ext.GeoProvince;
import com.vst.model.ext.Organization;
import com.vst.model.ext.Person;
/**
* Actions manager for xml gateway
*/
public class XmlGatewayManager {
private XmlGatewayDaoHibernate gatewayDao;
/**
* @return
*/
public XmlGatewayDaoHibernate getGatewayDao() {
return gatewayDao;
}
/**
* @param gatewayDao
*/
public void setGatewayDao(XmlGatewayDaoHibernate gatewayDao) {
this.gatewayDao = gatewayDao;
}
// списсок для таблицы справочника
public List<?> getDataForRegionsView() {
return gatewayDao.getSimpleList(Region.class);
}
// списсок для таблицы справочника
public List<?> getDataForHintsView() {
return gatewayDao.getSimpleList(Hint.class);
}
// списсок для таблицы справочника
public List<?> getDataForDefectTypesView() {
return gatewayDao.getSimpleList(DefectType.class);
}
// списсок для таблицы справочника
public List<?> getDataForMaterialsView() {
return gatewayDao.getSimpleList(Material.class);
}
// списсок для таблицы справочника
public List<?> getDataForDangerCategoriesView() {
return gatewayDao.getSimpleList(DangerCategory.class);
}
// списсок для таблицы справочника
public List<?> getDataForDefectZonesView() {
return gatewayDao.getSimpleList(DefectZone.class);
}
// список для таблицы справочника
public List<?> getDataForConstructionTypesView() {
return gatewayDao.getConstructionTypesList();
}
public List<?> getDataForPersonsList() {
return gatewayDao.getSimpleList(Person.class);
}
// дерево регионов
@SuppressWarnings("unchecked")
public GeoTreeItemDto getGeoTree() {
List<GeoClassificator> geolist = (List<GeoClassificator>) gatewayDao
.getSimpleList(GeoClassificator.class);
Map<Integer, GeoTreeItemDto> idxCountry = new HashMap<Integer, GeoTreeItemDto>();
Map<Integer, GeoTreeItemDto> idxMacroReg = new HashMap<Integer, GeoTreeItemDto>();
Map<Integer, GeoTreeItemDto> idxProvince = new HashMap<Integer, GeoTreeItemDto>();
Map<Integer, GeoTreeItemDto> idxCity = new HashMap<Integer, GeoTreeItemDto>();
GeoClassificator russia = null;
GeoTreeItemDto res = null;
for (GeoClassificator item : geolist) {
if (item instanceof GeoCountry) {
idxCountry.put(item.getId(), new GeoTreeItemDto(item));
if (item.getLatinName().equalsIgnoreCase("russia")) {
russia = item;
}
} else if (item instanceof GeoMacroRegion) {
idxMacroReg.put(item.getId(), new GeoTreeItemDto(item));
} else if (item instanceof GeoProvince) {
idxProvince.put(item.getId(), new GeoTreeItemDto(item));
} else if (item instanceof GeoCity) {
idxCity.put(item.getId(), new GeoTreeItemDto(item));
}
}
for (GeoTreeItemDto item : idxCity.values()){
GeoClassificator dbitem = item.getGeoItem();
GeoTreeItemDto parent = idxProvince.get(dbitem.getParent().getId());
if (parent != null){
parent.addChild(item);
}
}
for (GeoTreeItemDto item : idxProvince.values()){
GeoClassificator dbitem = item.getGeoItem();
GeoTreeItemDto parent = idxMacroReg.get(dbitem.getParent().getId());
if (parent != null){
parent.addChild(item);
}
}
for (GeoTreeItemDto item : idxMacroReg.values()){
GeoClassificator dbitem = item.getGeoItem();
GeoTreeItemDto parent = idxCountry.get(dbitem.getParent().getId());
if (parent != null){
parent.addChild(item);
}
}
if (russia != null) {
res = idxCountry.get(russia.getId());
}
return res;
}
private Integer[] extractKeys(List<? extends BaseAnnotatedEntity> list){
List<Integer> reslist = new ArrayList<Integer>();
for (BaseAnnotatedEntity item : list){
reslist.add(item.getId());
}
return reslist.toArray(new Integer[0]);
}
//список заказчиков для корня меню
@SuppressWarnings("unchecked")
public CustomersNavigationListDto getCustomersForNavigationList(){
CustomersNavigationListDto res = new CustomersNavigationListDto();
List<CustomerListItemDto> customers = new ArrayList<CustomerListItemDto>();
List<Person> contacts = new ArrayList<Person>();
List<GeoClassificator> countries = new ArrayList<GeoClassificator>();
List<GeoClassificator> provincies = new ArrayList<GeoClassificator>();
List<GeoClassificator> cities = new ArrayList<GeoClassificator>();
List<Organization> dblist =
(List<Organization>) gatewayDao.getCustomers();
for(Organization item : dblist){
CustomerListItemDto dto = new CustomerListItemDto(item);
countries.add(item.getAddress().getCountry());
provincies.add(item.getAddress().getProvince());
cities.add(item.getAddress().getCity());
contacts.addAll(item.getContacts());
customers.add(dto);
}
res.setCustomers(customers);
res.setCities((List<GeoClassificator>)getCityList(extractKeys(cities)));
res.setContacts((List<Person>)getContactsList((extractKeys(contacts))));
res.setProvincies((List<GeoClassificator>)getProvinceList(extractKeys(provincies)));
res.setCountries((List<GeoClassificator>)getCountryList((extractKeys(countries))));
return res;
}
@SuppressWarnings("unchecked")
public CustomerFormDto getCustomerForForm(Integer customerKey){
Organization org = gatewayDao.getCustomer(customerKey);
if (org==null){
throw new IllegalArgumentException("Customer with id=" + customerKey +" not found.");
}
List<?> countries = getCountryList();
List<?> provinces = getProvinceListForCountry();
GeoClassificator ancestor = new GeoClassificator();
ancestor.setId(org.getAddress().getProvince().getId());
List<?> cities = getCityListForAncestor(ancestor);
List<?> contacts = getContactsList(extractKeys(org.getContacts()));
CustomerFormDto dto = new CustomerFormDto();
dto.setCities((List<GeoClassificator>) cities);
dto.setCountries((List<GeoClassificator>) countries);
dto.setProvincies((List<GeoClassificator>) provinces);
dto.setContacts((List<Person>) contacts);
dto.setOrganization(org);
return dto;
}
@SuppressWarnings("unchecked")
public BuildingFormDto getBuildingForForm(Integer buildingKey) {
Building building = gatewayDao.getBuilding(buildingKey);
if (building==null){
throw new IllegalArgumentException("Building with id=" + buildingKey +" not found.");
}
List<?> countries = getCountryList();
List<?> provinces = getProvinceListForCountry();
GeoClassificator ancestor = new GeoClassificator();
ancestor.setId(building.getAddress().getProvince().getId());
List<?> cities = getCityListForAncestor(ancestor);
List<?> objectTypes = getObjectTypesList();
List<?> dangerCategories = getDangerCategoriesList();
Organization org = getOrganization(building.getBuildingowner().getId());
BuildingFormDto dto = new BuildingFormDto();
dto.setBuilding(building);
dto.setCustomer(org);
dto.setCities((List<GeoClassificator>) cities);
dto.setCountries((List<GeoClassificator>) countries);
dto.setProvincies((List<GeoClassificator>) provinces);
dto.setDangerCategories((List<DangerCategory>) dangerCategories);
dto.setBuildingTypes((List<ObjectType>) objectTypes);
return dto;
}
//========
public List<?> getProvinceListForCountry(){
GeoClassificator russia = new GeoCountry();
russia.setId(2);
return gatewayDao.getGeoListForAncestor(GeoProvince.class, russia);
}
public List<?> getProvinceList(Integer[] keys){
return gatewayDao.getSimpleList(GeoProvince.class, keys);
}
public List<?> getCountryList(Integer[] keys){
return gatewayDao.getSimpleList(GeoCountry.class, keys);
}
public List<?> getCountryList(){
return gatewayDao.getSimpleList(GeoCountry.class, new Integer[]{2});
}
public List<?> getCityList(Integer[] keys){
return gatewayDao.getSimpleList(GeoCity.class, keys);
}
public List<?> getCityListForAncestor(GeoClassificator ancestor){
return gatewayDao.getGeoListForAncestor(GeoCity.class, ancestor);
}
public List<?> getContactsList(Integer[] keys){
return gatewayDao.getSimpleList(Person.class, keys);
}
//=====
public List<?> getObjectTypesList(){
return gatewayDao.getSimpleList(ObjectType.class);
}
public List<?> getDangerCategoriesList(){
return gatewayDao.getSimpleList(DangerCategory.class);
}
//=======
public GeoProvince getProvince(Integer key){
return gatewayDao.getEntity(GeoProvince.class, key);
}
public GeoCountry getCountry(Integer key){
return gatewayDao.getEntity(GeoCountry.class, key);
}
public GeoCity getCity(Integer key){
return gatewayDao.getEntity(GeoCity.class, key);
}
public Organization getOrganization(Integer key){
return gatewayDao.getEntity(Organization.class, key);
}
//=====
public Person getPerson(Integer key){
return gatewayDao.getEntity(Person.class, key);
}
}