package com.dev.swms.server.web.servlet;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;

import org.springframework.context.ApplicationContext;
import org.springframework.util.Assert;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.dev.swms.client.ui.page.service.CustomService;
import com.dev.swms.server.model.base.Warehouse;
import com.dev.swms.server.service.base.WarehouseManager;
import com.dev.energy.server.action.ConfigAction;
import com.dev.energy.server.security.acegi.holder.SecurityContextHolder;
import com.dev.energy.server.servlet.LoginServlet;

public class ChangeWarehouseServlet extends RemoteServiceServlet implements CustomService{

	 protected ApplicationContext ac;
	 
	 public void init(ServletConfig sc) throws ServletException {
		super.init(sc);
		ac = WebApplicationContextUtils.getRequiredWebApplicationContext(sc
				.getServletContext());
	}

	public Map<String, Object> swichWarehouse(Long warehouseId) throws Exception {
		Long wId = Long.valueOf(warehouseId);
		if(isSameWarehouse(wId,this.getThreadLocalRequest())){
			throw new Exception("未切换仓库！");
		}
		Object sessionWH = this.getThreadLocalRequest().getSession(false).getAttribute("SESSION_WAREHOUSE");
		if(sessionWH == null){
			throw new Exception("session错误！");
		}
		
		WarehouseManager wm = (WarehouseManager)ac.getBean("warehouseManager");
		Warehouse warehouse = wm.load(Warehouse.class,wId);
		if(warehouse == null){
			throw new Exception("所选仓库错误！");
		}
		
		this.getThreadLocalRequest().getSession(false).removeAttribute(SWMSLoginServlet.SESSION_WAREHOUSE);
		this.getThreadLocalRequest().getSession(false).setAttribute(SWMSLoginServlet.SESSION_WAREHOUSE,warehouse);
		this.getThreadLocalRequest().getSession(false).removeAttribute(SWMSLoginServlet.SESSION_WAREHOUSE_ID);
		this.getThreadLocalRequest().getSession(false).setAttribute(SWMSLoginServlet.SESSION_WAREHOUSE_ID,warehouse.getId());
		
		SecurityContextHolder.clearAllSession();
		SecurityContextHolder.addSecurityContext(this.getThreadLocalRequest().getSession(false));
		
		//重新初始化全局参数
		ConfigAction configAction = (ConfigAction) ac.getBean("configAction");
		configAction.setReferenceModel(this.getThreadLocalRequest().getSession().getAttribute(LoginServlet.MODELTYPE).toString());
		Map<String,Object> results = new HashMap<String,Object>();
		Map globalMap = configAction.reInitGlobalParams(new HashMap());
		results.put("globals",globalMap);
		results.put("warehouseName",warehouse.getShortName());
		return results;
	}
	 
	private boolean isSameWarehouse(Long id,HttpServletRequest req){
		Warehouse w = (Warehouse)req.getSession(false).getAttribute(SWMSLoginServlet.SESSION_WAREHOUSE);
		Assert.notNull(w,"Transfer OrgWarehouse to  DesWarehouse.id="+id);
		if(w.getId() == id){
			return true;
		}else{
			return false;
		}
	}
}

