/**
 * 
 */
package com.cim.idm.mwmsextend.durable.service.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jdom.Document;
import org.jdom.Element;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

import com.cim.idm.wmspackage.durable.DurableServiceProxy;
import com.cim.idm.wmspackage.durable.management.data.Durable;
import com.cim.idm.wmspackage.durable.management.data.DurableKey;
import com.cim.idm.wmspackage.durable.management.info.CreateInfo;
import com.cim.idm.wmspackage.name.NameServiceProxy;
import com.cim.idm.wmsextend.generic.errorHandler.CustomException;
import com.cim.idm.framework.IDMFrameServiceProxy;
import com.cim.idm.framework.exception.ext.FrameworkErrorSignal;
import com.cim.idm.framework.exception.ext.NotFoundSignal;
import com.cim.idm.framework.orm.ObjectAttributeDef;
import com.cim.idm.framework.util.xml.JdomUtils;


/**
 * @author lvmingtao
 *
 */
public class DurableServiceUtil implements ApplicationContextAware
{
	
	private ApplicationContext		applicationContext;
	private static Log				log = LogFactory.getLog("DurableServiceUtil");
	
	/**
	 * Name : setApplicationContext<br/>
	 * Desc : This function is setApplicationContext<br/>
	 * Author : BOE CIM Dev.<br/>
	 * Date : 2014.12.20<br/>
	 */	
	public void setApplicationContext( ApplicationContext arg0 ) throws BeansException
	{
		applicationContext = arg0;
	}
	/**
	 * 
	 * @param durableName tryName, ...
	 * @param durableSpecName specName ...
	 * @param locationName 
	 * @return
	 */
	public CreateInfo makeCreateInfo(String SiteName,String durableName, String durableSpecName, String locationName ){
		
		CreateInfo createInfo = new CreateInfo();
		createInfo.setDurableName(durableName);
		createInfo.setDurableSpecName(durableSpecName);
		createInfo.setLocationName(locationName);
		createInfo.setSiteName(SiteName);
		return createInfo;
		
	}
	

	public CreateInfo makeDurableData(String siteName, String durableName,
			String durableSpecName, String locationName){
		
		CreateInfo DurableData = new CreateInfo();
	
		DurableData.setSiteName(siteName);	
		DurableData.setDurableName(durableName);
		DurableData.setDurableSpecName(durableSpecName);
		DurableData.setLocationName(locationName);
		
		return DurableData;
	}

	
	
	public Durable getDurableData(String SiteName,String durableName) throws CustomException{
		
		DurableKey key = new DurableKey();
		key.setSiteName(SiteName);
		key.setDurableName(durableName);
		
		Durable durable = null;
		try{
			durable = DurableServiceProxy.getDurableService().selectByKey(key);
		}catch(NotFoundSignal e){
			throw new CustomException("DURABLE-9000", durableName);
		}
		return durable;
		
	}
	
	public boolean checkTrayExist(String SiteName,String durableName) throws CustomException{
		
		DurableKey key = new DurableKey();
		key.setSiteName(SiteName);
		key.setDurableName(durableName);
		
		Durable durable = null;
		try{
			durable = DurableServiceProxy.getDurableService().selectByKey(key);
		}catch(NotFoundSignal e){
			log.error("Tray["+durableName+"] Not Exist in FGMS");
			return false;
		}
		return true;
		
	}
	
	/*
	* Name : setNamedValueSequence
	* Desc : This function is setNamedValueSequence
	* Author : ZXY IDM Team
	* Date : 2011.02.01
	*/
	/*public Map<String, String> setNamedValueSequence(String durableName, Element element) 
	throws FrameworkErrorSignal, NotFoundSignal
	{		
		Map<String, String> map = new HashMap<String, String>();
		
		DurableKey durableKey = new DurableKey();
		durableKey.setDurableName( durableName );
		
		Durable durableData = null;
		
		try
		{
			durableData = DurableServiceProxy.getDurableService().selectByKey(durableKey);
		}
		catch ( NotFoundSignal e )
		{
		}
		
		List<ObjectAttributeDef> objectAttributeDefs 
		  = IDMFrameServiceProxy.getObjectAttributeMap().getAttributeNames("Durable", "ExtendedC");

		if ( objectAttributeDefs != null ) 
		{
			for ( int i = 0; i < objectAttributeDefs.size(); i++ )
			{
				String name = "";
				String value = "";
				
				if ( durableData == null )
				{
					name = objectAttributeDefs.get(i).getAttributeName();
				}
				
				if ( element != null )
				{
					for ( int j = 0; j < element.getContentSize(); j++ )
					{
						if ( element.getChildText(objectAttributeDefs.get(i).getAttributeName()) != null )
						{							
							name = objectAttributeDefs.get(i).getAttributeName();
							if ( durableData != null )
								value = element.getChildText(objectAttributeDefs.get(i).getAttributeName());
						}
					}
				}
				if ( name.equals("") != true )
					map.put( name, value );
			}
		}
		return map;
	}	
	 
	
	* Name : generateCstName
	* Desc : This function is generateCstName 
	* Author : ZXY IDM Team
	* Date : 2011.02.01
	
	public List<String> generateCstName(String factoryName,
										String durableSpecName,
										String quantity)
	{
		log.info("Generate CstName");
		
		List<String> list = new ArrayList<String>();
		
		int count = Integer.parseInt(quantity);
		
		NameGeneratorRuleDefKey nameGenratorRuleDefKey = new NameGeneratorRuleDefKey();
		nameGenratorRuleDefKey.setRuleName( "DURABLENAME" );
		
		try 
		{
			NameServiceProxy.getNameGeneratorRuleDefService().selectByKey( nameGenratorRuleDefKey );
		}
		catch (FrameworkErrorSignal e) 
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		catch (NotFoundSignal e) 
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
				
		List<String> argSeq = new ArrayList<String>();
		argSeq.add(factoryName);
		argSeq.add(durableSpecName);

		List<String> cstList = null;
		
		try 
		{
			cstList = NameServiceProxy.getNameGeneratorRuleDefService().generateName("DURABLENAME", argSeq, count);
		} 
		catch (FrameworkErrorSignal e)
		{
		}
		catch (NotFoundSignal e)
		{
		}

		log.info("CstList Length : " + cstList.size());
		
		for ( int i = 0; i < count; i++)
		{
			list.add(cstList.get(i));
			log.info(list.get(i));
		}
		return list;
	}
	
	
	* Name : createResultElementNode
	* Desc : This function is createResultElementNode 
	* Author : ZXY IDM Team
	* Date : 2011.02.01
	
	public Element createResultElementNode(String ObjectName, List ObjectList)
	{
		Element element = null;
		String node = null;
		String snode = null;
		
		if( ObjectName.indexOf("DURABLE") > -1 )
		{
		   node = "DURABLELIST";
		   snode = "DURABLE";
		}
		else
		{
			node = "OBJECTLIST";
			snode = "OBJECT";
		}
		
		element = new Element(node);		
		Element selement = new Element(snode);
		
		for( int i=0; i < ObjectList.size(); i++)
		{
			Element velement = new Element(ObjectName);
			velement.setText(ObjectList.get(i).toString());
			selement.addContent(velement);
		}
		
		element.addContent(selement);
		
		return  element;
	}
	
	
	* Name : createDurableListElement
	* Desc : This function is createDurableListElement 
	* Author : ZXY IDM Team
	* Date : 2011.02.01
	
	public Element createDurableListElement(List<String> durableNames)
	{
		Element element = null;

		element = new Element("DurableList");		
		
		for( int i=0; i < durableNames.size(); i++)
		{
			Element selement = new Element("Durable");
			Element velement = new Element("DURABLENAME");
			velement.setText(durableNames.get(i).toString());
			selement.addContent(velement);
			element.addContent(selement);
		}
		return  element;
	}

	
	* Name : makeCassetteAssignMessageFormat
	* Desc : This function is makeCassetteAssignMessageFormat 
	* Author : ZXY IDM Team
	* Date : 2011.02.01
	
	public Document makeCassetteAssignMessageFormat(Document inputXmlData, String lotName, String cstName) throws Exception
	{
		Element root = inputXmlData.getDocument().getRootElement();
		
		Document returnMsg = (Document)inputXmlData.clone();
		Element bodyElement = JdomUtils.getNode(returnMsg, "//message/body");
		//bodyElement.detach();
		  
		bodyElement.removeContent();
		
		Element tempElement = new Element("LOTNAME");
		tempElement.setText(lotName);
		bodyElement.addContent(tempElement);
		
		tempElement = new Element("DURABLENAME");
		tempElement.setText(cstName);
		bodyElement.addContent(tempElement);
		
		tempElement = new Element("EVENTUSER");
		tempElement.setText(root.getChild("body").getChildText("EVENTUSER"));
		bodyElement.addContent(tempElement);
		
		tempElement = new Element("EVENTCOMMENT");
		tempElement.setText(root.getChild("body").getChildText("EVENTCOMMENT"));
		bodyElement.addContent(tempElement);
		
		tempElement = new Element("LANGUAGE");
		tempElement.setText(root.getChild("body").getChildText("LANGUAGE"));
		bodyElement.addContent(tempElement);
		
		return returnMsg;
	}
	
	
	* Name : makeCassetteDeAssignMessageFormat
	* Desc : This function is makeCassetteDeAssignMessageFormat 
	* Author : ZXY IDM Team
	* Date : 2011.02.01
	
	public Document makeCassetteDeAssignMessageFormat(Document inputXmlData, String lotName) throws Exception
	{
		Element root = inputXmlData.getDocument().getRootElement();
		
		Document returnMsg = (Document)inputXmlData.clone();
		Element bodyElement = JdomUtils.getNode(returnMsg, "//message/body");
		  
		bodyElement.removeContent();
		
		Element tempElement = new Element("LOTNAME");
		tempElement.setText(lotName);
		bodyElement.addContent(tempElement);
		
		tempElement = new Element("EVENTUSER");
		tempElement.setText(root.getChild("body").getChildText("EVENTUSER"));
		bodyElement.addContent(tempElement);
		
		tempElement = new Element("EVENTCOMMENT");
		tempElement.setText(root.getChild("body").getChildText("EVENTCOMMENT"));
		bodyElement.addContent(tempElement);
		
		tempElement = new Element("LANGUAGE");
		tempElement.setText(root.getChild("body").getChildText("LANGUAGE"));
		bodyElement.addContent(tempElement);
		
		return returnMsg;
	}
	
	
	* Name : makeCassetteDeAssignMessageFormat
	* Desc : This function is makeCassetteDeAssignMessageFormat 
	* Author : ZXY IDM Team
	* Date : 2011.02.01
	
	public Durable getMaskData (String durableName) throws FrameworkErrorSignal, NotFoundSignal
	{
		Boolean isExistMask = false;
		
		DurableKey durableKey = new DurableKey();
		durableKey.setDurableName(durableName);
		
		Durable durableData = null;
		
		try{
			durableData = DurableServiceProxy.getDurableService().selectByKey(durableKey);
		}
		catch (FrameworkErrorSignal e)
		{
			// pass
			return durableData;
		}

		return durableData;
		
	}
	
	
	* Name : getDurableData
	* Desc : This function is getDurableData
	* Author : ZXY IDM Team
	* Date : 2011.02.01
	
	public Durable getDurableData(String durableName) throws FrameworkErrorSignal, NotFoundSignal
	{		
		DurableKey durableKey = new DurableKey();
		
		durableKey.setDurableName(durableName);
		
		Durable durableData = null;
		
		durableData = DurableServiceProxy.getDurableService().selectByKey(durableKey);
		
		return durableData;
	}	*/
}
