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.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.durable.management.info.MakeInUseInfo;
import com.cim.idm.wmspackage.durable.management.info.SetEventInfo;
import com.cim.idm.wmspackage.durable.management.info.SetLocationInfo;
import com.cim.idm.wmspackage.name.NameServiceProxy;
import com.cim.idm.framework.IDMFrameServiceProxy;
import com.cim.idm.framework.data.EventInfo;
import com.cim.idm.framework.exception.ext.NotFoundSignal;
import com.cim.idm.framework.util.time.TimeStampUtil;

public class DurableServiceImpl implements ApplicationContextAware {
	private ApplicationContext applicationContext;
	private static Log log = LogFactory.getLog(DurableServiceImpl.class);

	/**
	 * Name : setApplicationContext<br/>
	 * Desc : This function is setApplicationContext<br/>
	 * Author : BOE CIM Dev.<br/>
	 * Date : 2014.12.23<br/>
	 */
	public void setApplicationContext(ApplicationContext arg0)
			throws BeansException {
		applicationContext = arg0;
	}

	public List<String> generateTrayName(String durableSpecName,long count ){
		List<String> tryNameList = null;
		List<String> args = new ArrayList<String>();
		args.add("B17");
		args.add(durableSpecName);
		tryNameList = NameServiceProxy.getNameRuleDefService().generateName("TRAYID", args, count);
		return tryNameList;
	}
	public String generateTrayName(String durableSpecName){
		String tryName = null;
		List<String> args = new ArrayList<String>();
		args.add("B17");
		args.add(durableSpecName);
			List<String> tryNameList = NameServiceProxy.getNameRuleDefService().generateName("TRAYID", args, 1);
			if(tryNameList!=null&&tryNameList.size()>0)
				tryName = tryNameList.get(0);
		return tryName;
	}
	
	public void create(EventInfo eventInfo, List<CreateInfo> createInfoList ){
		DurableServiceProxy.getDurableService().create(eventInfo, createInfoList);
	}
	
	public void setEvent(DurableKey key, EventInfo eventInfo, Map<String,Object> trayUserColumns ){
		SetEventInfo setEventInfo = new SetEventInfo();
		setEventInfo.setUserColumns(trayUserColumns);
		DurableServiceProxy.getDurableService().setEvent(key, eventInfo, setEventInfo);
	}
	
	public void setLocation(DurableKey key, EventInfo eventInfo,String locationName, Map<String, Object> durableUserColumns, Map<String, Object> storageUserColumns){
		
		SetLocationInfo setLocationInfo = new SetLocationInfo();
		setLocationInfo.setLocationName(locationName);
		setLocationInfo.setUserColumns(durableUserColumns);
		setLocationInfo.setLocationUserColumns(storageUserColumns);
		DurableServiceProxy.getDurableService().setLocation(key, eventInfo, setLocationInfo);
		
		
	}	
	
	
	public void makeInUse(DurableKey key, EventInfo eventInfo, Map<String, Object> durableUserColumns){
		
		MakeInUseInfo makeInUseInfo = new MakeInUseInfo();
		makeInUseInfo.setUserColumns(durableUserColumns);
		makeInUseInfo.setPackingGroupQuantity(1L);
		DurableServiceProxy.getDurableService().makeInUse(key, eventInfo, makeInUseInfo);
		
	}
	
	
	public void changeTrayAttribute(String durableName,String durableState ,String lastEventComment,  EventInfo eventInfo,String siteName){
		SetEventInfo setEventInfo = new SetEventInfo();
		Map<String, Object> bindMap = new HashMap<String, Object>();
		bindMap.put("durableState", durableState);
		 if(durableState.equals("InUse"))
			{
				bindMap.put("assignedPackingQuantity", 1);
			}
			 else
			 {
				 bindMap.put("assignedPackingQuantity", 0);
			 }
		setEventInfo.setObjectValueUserColumns(bindMap);
		if(lastEventComment!="")
		{
			eventInfo.setEventComment(lastEventComment);
		}
		
		DurableKey key=new DurableKey();
		key.setSiteName(siteName);
		key.setDurableName(durableName);
		
		DurableServiceProxy.getDurableService().setEvent(key, eventInfo, setEventInfo);
	}
	
	public void changeTrayLocation(String trayName,String locationName,String eventComment , EventInfo eventInfo,String siteName){
		
		DurableKey key=new DurableKey();
		key.setSiteName(siteName);
		key.setDurableName(trayName);
		
		Durable tray = null;
		
		try{
			tray = DurableServiceProxy.getDurableService().selectByKey(key);
			
			SetEventInfo setEventInfo = new SetEventInfo();
			Map<String, Object> bindMap = new HashMap<String, Object>();
			bindMap.put("locationName", locationName);
			setEventInfo.setObjectValueUserColumns(bindMap);
			if(!eventComment.equals(""))
			{
				eventInfo.setEventComment(eventComment);
			}
			
			DurableServiceProxy.getDurableService().setEvent(key, eventInfo, setEventInfo);
		}catch(NotFoundSignal e){
			log.error("Tray["+trayName+"] not exist!"  );
		}
		
		
		
	}
	/*
	* Name : create
	* Desc : This function is create durable
	* Author : ZXY IDM Team
	* Date : 2011.01.23
	*/
	/*public void create(EventInfo eventInfo, 
					   String durableName,
					   String durableSpecName, 
					   String siteName, 
					   Map<String, String> udfs) 
			throws InvalidStateTransitionSignal, FrameworkErrorSignal, NotFoundSignal, DuplicateNameSignal {
		log.info("Durable Create");

		DurableSpecKey durableSpecKey = new DurableSpecKey();
		
		durableSpecKey.setDurableSpecName( durableSpecName );
		durableSpecKey.setSiteName(siteName);
		
		DurableSpec durableSpecData = DurableServiceProxy.getDurableSpecService().selectByKey( durableSpecKey );
		
		DurableKey durableKey = new DurableKey();
		durableKey.setDurableName( durableName );
		
		CreateInfo createInfo = new CreateInfo();
		createInfo.setAreaName( "" );
		createInfo.setCapacity( durableSpecData.getDefaultCapacity());
		createInfo.setDurableName( durableName );
		createInfo.setDurableSpecName( durableSpecName );
		createInfo.setDurableType( durableSpecData.getDurableType());
		
		createInfo.setUdfs( udfs );
		
		DurableServiceProxy.getDurableService().create( durableKey, eventInfo, createInfo );
	}

	
	* Name : increamentTimeUsed
	* Desc : This function is increamentTimeUsed durable
	* Author : ZXY IDM Team
	* Date : 2011.01.23
	
	public void increamentTimeUsed(EventInfo eventInfo,
								   String durableName, 
								   String consumerLotName, 
								   String consumerPOName,
								   String consumerPOVersion, 
								   String consumerProductName,
								   String consumerTimeKey, long timeUsed,
								   Map<String, String> udfs) 
			throws InvalidStateTransitionSignal, FrameworkErrorSignal, NotFoundSignal, DuplicateNameSignal {
		log.info("IncrementTimeUsed");

		DurableKey durableKey = new DurableKey();
		durableKey.setDurableName( durableName );

		IncrementTimeUsedInfo incrementTimeUsedInfo = new IncrementTimeUsedInfo();
		incrementTimeUsedInfo.setConsumerLotName( consumerLotName );
		incrementTimeUsedInfo.setConsumerPOName( consumerPOName );
		incrementTimeUsedInfo.setConsumerPOVersion( consumerPOVersion );
		incrementTimeUsedInfo.setConsumerProductName( consumerProductName );
		incrementTimeUsedInfo.setConsumerTimeKey( consumerTimeKey );
		incrementTimeUsedInfo.setTimeUsed( timeUsed );
		incrementTimeUsedInfo.setUdfs( udfs );

		DurableServiceProxy.getDurableService().incrementTimeUsed( durableKey, eventInfo, incrementTimeUsedInfo);
	}

	
	* Name : decrementTimeUsed
	* Desc : This function is decrementTimeUsed durable
	* Author : ZXY IDM Team
	* Date : 2011.01.23
	
	public void decrementTimeUsed(EventInfo eventInfo,
								  String durableName, 
								  long timeUsed, 
								  Map<String, String> udfs) 
			throws InvalidStateTransitionSignal, FrameworkErrorSignal, NotFoundSignal, DuplicateNameSignal {
		log.info("DecrementTimeUsed");
		
		DurableKey durableKey = new DurableKey();
		durableKey.setDurableName( durableName );

		DecrementTimeUsedInfo decrementTimeUsedInfo = new DecrementTimeUsedInfo();
		decrementTimeUsedInfo.setTimeUsed( timeUsed );
		decrementTimeUsedInfo.setUdfs( udfs );

		DurableServiceProxy.getDurableService().decrementTimeUsed( durableKey, eventInfo, decrementTimeUsedInfo);
	}

	
	* Name : incrementDurationUsed
	* Desc : This function is incrementDurationUsed durable
	* Author : ZXY IDM Team
	* Date : 2011.01.23
	
	public void incrementDurationUsed(EventInfo eventInfo,
									  String durableName, 
									  long durationUsed,
									  Map<String, String> udfs) 
			throws InvalidStateTransitionSignal, FrameworkErrorSignal, NotFoundSignal, DuplicateNameSignal {
		log.info("IncrementDurableUsed");
		
		DurableKey durableKey = new DurableKey();
		durableKey.setDurableName( durableName );

		IncrementDurationUsedInfo incrementDurationUsedInfo = new IncrementDurationUsedInfo();
		incrementDurationUsedInfo.setDurationUsed( durationUsed );
		incrementDurationUsedInfo.setUdfs( udfs );

		DurableServiceProxy.getDurableService().incrementDurationUsed( durableKey, eventInfo, incrementDurationUsedInfo);
	}

	
	* Name : decrementDurationUsed
	* Desc : This function is decrementDurationUsed durable
	* Author : ZXY IDM Team
	* Date : 2011.01.23
	
	public void decrementDurationUsed(EventInfo eventIfno,
									  String durableName, 
									  long durationUsed,
									  Map<String, String> udfs) 
			throws InvalidStateTransitionSignal, FrameworkErrorSignal, NotFoundSignal, DuplicateNameSignal {
		log.info("DecrementDurationUsed");
		
		DurableKey durableKey = new DurableKey();
		durableKey.setDurableName( durableName );

		DecrementDurationUsedInfo decrementDurationUsedInfo = new DecrementDurationUsedInfo();
		decrementDurationUsedInfo.setDurationUsed( durationUsed );
		decrementDurationUsedInfo.setUdfs( udfs );

		DurableServiceProxy.getDurableService().decrementDurationUsed( durableKey, eventIfno, decrementDurationUsedInfo);
	}

	
	* Name : makeAvailable
	* Desc : This function is makeAvailable durable 
	* Author : ZXY IDM Team
	* Date : 2011.01.23
	
	public void makeAvailable(EventInfo eventInfo,
							  String durableName, 
							  Map<String, String> udfs) 
			throws InvalidStateTransitionSignal, FrameworkErrorSignal, NotFoundSignal, DuplicateNameSignal {
		log.info("MakeAvailable");
		
		DurableKey durableKey = new DurableKey();
		durableKey.setDurableName( durableName );

		MakeAvailableInfo makeAvailableInfo = new MakeAvailableInfo();
		makeAvailableInfo.setUdfs( udfs );

		DurableServiceProxy.getDurableService().makeAvailable( durableKey, eventInfo, makeAvailableInfo);
	}

	
	* Name : makeNotAvailable
	* Desc : This function is makeNotAvailable durable 
	* Author : ZXY IDM Team
	* Date : 2011.01.23
	
	public void makeNotAvailable(EventInfo eventInfo,
								 String durableName, 
								 Map<String, String> udfs) 
			throws InvalidStateTransitionSignal, FrameworkErrorSignal, NotFoundSignal, DuplicateNameSignal {
		log.info("MakeNotAvailable");
		
		DurableKey durableKey = new DurableKey();
		durableKey.setDurableName( durableName );

		MakeNotAvailableInfo makeNotAvailableInfo = new MakeNotAvailableInfo();
		makeNotAvailableInfo.setUdfs( udfs );

		DurableServiceProxy.getDurableService().makeNotAvailable( durableKey, eventInfo, makeNotAvailableInfo);
	}

	
	* Name : makeInUse
	* Desc : This function is makeInUse durable 
	* Author : ZXY IDM Team
	* Date : 2011.01.24
	
	public void makeInUse(EventInfo eventInfo, 
						  String durableName,
						  String consumerLotName, 
						  String consumerPOName,
						  String consumerPOVersion, 
						  String consumerTimeKey,
						  Map<String, String> udfs) 
			throws InvalidStateTransitionSignal, FrameworkErrorSignal, NotFoundSignal, DuplicateNameSignal {
		log.info("MakeInUse");
		
		DurableKey durableKey = new DurableKey();
		durableKey.setDurableName( durableName );

		MakeInUseInfo makeInUseInfo = new MakeInUseInfo();
		makeInUseInfo.setConsumerLotName( "" ); 
		makeInUseInfo.setConsumerPOName( "" );
		makeInUseInfo.setConsumerPOVersion( "" );
		makeInUseInfo.setConsumerTimeKey( "" ); 
		makeInUseInfo.setUdfs( udfs );

		DurableServiceProxy.getDurableService().makeInUse( durableKey, eventInfo, makeInUseInfo);
	}

	
	* Name : makeNotInUse
	* Desc : This function is makeNotInUse durable 
	* Author : ZXY IDM Team
	* Date : 2011.01.24
	
	public void makeNotInUse(EventInfo eventInfo,
							 String durableName, 
							 String consumerLotName, 
							 String consumerPOName,
							 String consumerPOVersion, 
							 String consumerTimeKey,
							 Map<String, String> udfs) 
			throws InvalidStateTransitionSignal, FrameworkErrorSignal, NotFoundSignal, DuplicateNameSignal {
		log.info("MakeNotInUse");
		
		DurableKey durableKey = new DurableKey();
		durableKey.setDurableName( durableName );

		MakeNotInUseInfo makeNotInUseInfo = new MakeNotInUseInfo();
		makeNotInUseInfo.setConsumerLotName( "" );
		makeNotInUseInfo.setConsumerPOName( "" );
		makeNotInUseInfo.setConsumerPOVersion( "" );
		makeNotInUseInfo.setConsumerTimeKey( "" );
		makeNotInUseInfo.setUdfs( udfs );

		DurableServiceProxy.getDurableService().makeNotInUse( durableKey, eventInfo, makeNotInUseInfo);
	}

	
	* Name : makeScrapped
	* Desc : This function is makeScrapped durable 
	* Author : ZXY IDM Team
	* Date : 2011.01.24
	
	public void makeScrapped(EventInfo eventInfo,
							 String durableName, 
							 Map<String, String> udfs)
			throws InvalidStateTransitionSignal, FrameworkErrorSignal, NotFoundSignal, DuplicateNameSignal {
		log.info("MakeScrapped");
		
		DurableKey durableKey = new DurableKey();
		durableKey.setDurableName( durableName );

		MakeScrappedInfo makeScrappedInfo = new MakeScrappedInfo();
		makeScrappedInfo.setUdfs( udfs );

		DurableServiceProxy.getDurableService().makeScrapped( durableKey, eventInfo, makeScrappedInfo);
	}

	
	* Name : clean
	* Desc : This function is clean durable 
	* Author : ZXY IDM Team
	* Date : 2011.01.24
	
	public void clean(EventInfo eventInfo, 
					  String durableName,
					  Map<String, String> udfs)
			throws InvalidStateTransitionSignal, FrameworkErrorSignal, NotFoundSignal, DuplicateNameSignal {
		log.info("Clean");
		
		DurableKey durableKey = new DurableKey();
		durableKey.setDurableName( durableName );

		CleanInfo cleanInfo = new CleanInfo();
		cleanInfo.setUdfs( udfs );

		DurableServiceProxy.getDurableService().clean( durableKey, eventInfo, cleanInfo);
	}

	
	* Name : dirty
	* Desc : This function is dirty durable 
	* Author : ZXY IDM Team
	* Date : 2011.01.24
	
	public void dirty(EventInfo eventInfo, 
					  String durableName,
					  Map<String, String> udfs) 
			throws InvalidStateTransitionSignal, FrameworkErrorSignal, NotFoundSignal, DuplicateNameSignal {
		log.info("Dirty");
		
		DurableKey durableKey = new DurableKey();
		durableKey.setDurableName( durableName );

		DirtyInfo dirtyInfo = new DirtyInfo();
		dirtyInfo.setUdfs( udfs );

		DurableServiceProxy.getDurableService().dirty( durableKey, eventInfo, dirtyInfo);
	}

	
	* Name : repair
	* Desc : This function is durable repair 
	* Author : ZXY IDM Team
	* Date : 2011.01.24
	
	public void repair(EventInfo eventInfo, 
					   String durableName,
					   Map<String, String> udfs) 
			throws InvalidStateTransitionSignal, FrameworkErrorSignal, NotFoundSignal, DuplicateNameSignal {
		log.info("Repair");
		
		DurableKey durableKey = new DurableKey();
		durableKey.setDurableName( durableName );

		RepairInfo repairInfo = new RepairInfo();
		repairInfo.setUdfs( udfs );

		DurableServiceProxy.getDurableService().repair( durableKey, eventInfo, repairInfo);
	}

	
	* Name : assignTransportGroup
	* Desc : This function is assignTransportGroup 
	* Author : ZXY IDM Team
	* Date : 2011.01.24
	
	public void assignTransportGroup(EventInfo eventInfo,
									 String durableName, 
									 String transportGroupName,
									 Map<String, String> udfs)
			throws InvalidStateTransitionSignal, FrameworkErrorSignal, NotFoundSignal, DuplicateNameSignal {
		log.info("AssignTransportGroup");
		
		DurableKey durableKey = new DurableKey();
		durableKey.setDurableName( durableName );

		AssignTransportGroupInfo assignTransportGroupInfo = new AssignTransportGroupInfo();
		assignTransportGroupInfo.setTransportGroupName( transportGroupName );
		assignTransportGroupInfo.setUdfs( udfs );

		DurableServiceProxy.getDurableService().assignTransportGroup( durableKey, eventInfo, assignTransportGroupInfo);
	}

	
	* Name : deassignTransportGroup
	* Desc : This function is deassignTransportGroup 
	* Author : ZXY IDM Team
	* Date : 2011.01.24
	
	public void deassignTransportGroup(EventInfo eventInfo,
									   String durableName, 
									   Map<String, String> udfs)
			throws InvalidStateTransitionSignal, FrameworkErrorSignal, NotFoundSignal, DuplicateNameSignal {
		log.info("DeassignTransportGroup");
		
		DurableKey durableKey = new DurableKey();
		durableKey.setDurableName( durableName );

		DeassignTransportGroupInfo deassignTransportGroupInfo = new DeassignTransportGroupInfo();
		deassignTransportGroupInfo.setUdfs( udfs );

		DurableServiceProxy.getDurableService().deassignTransportGroup( durableKey, eventInfo, deassignTransportGroupInfo);
	}

	
	* Name : changeSpec
	* Desc : This function is change durable Spec 
	* Author : ZXY IDM Team
	* Date : 2011.01.24
	
	public void changeSpec(EventInfo eventInfo,
						   String durableName, 
						   String durableSpecName,
						   String durableSpecVersion, 
						   long timeUsedLimit,
						   long durationUsedLimit, 
						   int capacity, 
						   String factoryName,
						   String areaName, 
						   Map<String, String> udfs)
			throws InvalidStateTransitionSignal, FrameworkErrorSignal, NotFoundSignal, DuplicateNameSignal {
		log.info("ChangeSpec");
		
		DurableKey durableKey = new DurableKey();
		durableKey.setDurableName( durableName );

		ChangeSpecInfo changeSpecInfo = new ChangeSpecInfo();
		changeSpecInfo.setAreaName( areaName );
		changeSpecInfo.setCapacity( capacity );
		changeSpecInfo.setDurableSpecName( durableSpecName );
		changeSpecInfo.setDurableSpecVersion( durableSpecVersion );
		changeSpecInfo.setFactoryName( factoryName );
		changeSpecInfo.setDurationUsedLimit( durationUsedLimit );
		changeSpecInfo.setTimeUsedLimit( timeUsedLimit );
		changeSpecInfo.setUdfs( udfs );

		DurableServiceProxy.getDurableService().changeSpec( durableKey, eventInfo, changeSpecInfo);
	}

	
	* Name : setArea
	* Desc : This function is setArea 
	* Author : ZXY IDM Team
	* Date : 2011.01.24
	
	public void setArea(EventInfo eventInfo, 
						String durableName,
						String areaName, Map<String, String> udfs)
			throws InvalidStateTransitionSignal, FrameworkErrorSignal, NotFoundSignal, DuplicateNameSignal {
		log.info("SetArea");
		
		DurableKey durableKey = new DurableKey();
		durableKey.setDurableName( durableName );

		SetAreaInfo setAreaInfo = new SetAreaInfo();
		setAreaInfo.setAreaName( areaName );
		setAreaInfo.setUdfs( udfs );

		DurableServiceProxy.getDurableService().setArea( durableKey, eventInfo, setAreaInfo);
	}

	
	* Name : setMaterialLocation
	* Desc : This function is setMaterialLocation 
	* Author : ZXY IDM Team
	* Date : 2011.01.24
	
	public void setMaterialLocation(EventInfo eventInfo,
									String durableName, 
									String materialLocationName,
									Map<String, String> udfs)
			throws InvalidStateTransitionSignal, FrameworkErrorSignal, NotFoundSignal, DuplicateNameSignal {
		log.info("SetMaterialLocation");
		
		DurableKey durableKey = new DurableKey();
		durableKey.setDurableName( durableName );

		SetMaterialLocationInfo setMaterialLocationInfo = new SetMaterialLocationInfo();
		setMaterialLocationInfo.setMaterialLocationName( materialLocationName );
		setMaterialLocationInfo.setUdfs( udfs );

		DurableServiceProxy.getDurableService().setMaterialLocation( durableKey, eventInfo, setMaterialLocationInfo);
	}

	
	* Name : setEvent
	* Desc : This function is setEvent 
	* Author : ZXY IDM Team
	* Date : 2011.01.24
	
	public void setEvent(EventInfo eventInfo, 
						 String durableName,
						 Map<String, String> udfs)
			throws InvalidStateTransitionSignal, FrameworkErrorSignal, NotFoundSignal, DuplicateNameSignal {
		log.info("SetEvent");
		
		DurableKey durableKey = new DurableKey();
		durableKey.setDurableName( durableName );

		SetEventInfo setEventInfo = new SetEventInfo();
		setEventInfo.setUdfs( udfs );

		DurableServiceProxy.getDurableService().setEvent( durableKey, eventInfo, setEventInfo);
	}

	
	* Name : undo
	* Desc : This function is undo durable 
	* Author : ZXY IDM Team
	* Date : 2011.01.24
	
	public void undo(String durableName, 
					 String eventName,
					 Timestamp eventTime, 
					 String eventTimeKey,
					 String eventUser, 
					 String lastEventTimeKey)
			throws InvalidStateTransitionSignal, FrameworkErrorSignal, NotFoundSignal, DuplicateNameSignal {
		log.info("Undo");
		
		DurableKey durableKey = new DurableKey();
		durableKey.setDurableName( durableName );

		UndoInfo undoInfo = new UndoInfo();
		undoInfo.setEventName( eventName);
		undoInfo.setEventTime( eventTime);
		undoInfo.setEventTimeKey( eventTimeKey);
		undoInfo.setEventUser( eventUser);
		undoInfo.setLastEventTimeKey( lastEventTimeKey);
		
		EventInfo eventInfo = new EventInfo();
		eventInfo.setEventName(eventName);
		eventInfo.setEventTime(eventTime);
		eventInfo.setEventTimeKey("");
		
		DurableServiceProxy.getDurableService().undo( durableKey, eventInfo, undoInfo );
	}

	
	* Name : createCancel
	* Desc : This function is createCancel durable 
	* Author : ZXY IDM Team
	* Date : 2011.01.25
	
	public void createCancel(String durableName,
							 EventInfo eventInfo) throws Exception {
		log.info("CreateCancel");
		
		DurableKey durableKey = new DurableKey();
		durableKey.setDurableName( durableName );

		UndoInfo undoInfo = new UndoInfo();
		undoInfo.setEventName( eventInfo.getEventName());
		undoInfo.setEventTime( eventInfo.getEventTime());
		undoInfo.setEventTimeKey( "");
		undoInfo.setEventUser( eventInfo.getEventUser());
		undoInfo.setLastEventTimeKey( eventInfo.getLastEventTimekey());
		
		DurableServiceProxy.getDurableService().undo( durableKey, eventInfo, undoInfo );

	}

	
	* Name : changeInfo
	* Desc : This function is changeInfo durable 
	* Author : ZXY IDM Team
	* Date : 2011.01.25
	
	public void changeInfo(EventInfo eventInfo,
						   String durableName, 
						   String timeUsedLimit, 
						   String durationUsedLimit,
						   String capacity, 
						   Map<String, String> udfs)
			throws InvalidStateTransitionSignal, FrameworkErrorSignal, NotFoundSignal, DuplicateNameSignal {
		log.info("ChangeInfo");
		
		DurableKey durableKey = new DurableKey();
		durableKey.setDurableName( durableName );
		
		Durable durableData = DurableServiceProxy.getDurableService().selectByKey( durableKey );

		ChangeSpecInfo changeSpecInfo = new ChangeSpecInfo();
		changeSpecInfo.setAreaName( durableData.getAreaName());
		changeSpecInfo.setDurableSpecName( durableData.getDurableSpecName());
		changeSpecInfo.setDurableSpecVersion( durableData.getDurableSpecVersion());
		changeSpecInfo.setFactoryName( durableData.getFactoryName());
		changeSpecInfo.setCapacity( Integer.parseInt(capacity));
		changeSpecInfo.setDurationUsedLimit( Long.parseLong(durationUsedLimit));
		changeSpecInfo.setTimeUsedLimit( Long.parseLong(timeUsedLimit));	
		changeSpecInfo.setUdfs( udfs );

		DurableServiceProxy.getDurableService().changeSpec( durableKey, eventInfo, changeSpecInfo);
	}
	
	
	* Name : changeCarrierAttributes
	* Desc : This function is changeCarrierAttributes 
	* Author : ZXY IDM Team
	* Date : 2011.01.25
	
	public void changeCarrierAttributes(EventInfo eventInfo,
			   String durableName, 
			   String timeused,
			   String durationused, 
			   Map<String, String> udfs)
	throws InvalidStateTransitionSignal, FrameworkErrorSignal, NotFoundSignal, DuplicateNameSignal {
		log.info("ChangeAttributes");
		
		DurableKey durableKey = new DurableKey();
		
		durableKey.setDurableName( durableName );
		
		Durable durableData = DurableServiceProxy.getDurableService().selectByKey( durableKey );
		
		durableData.setDurationUsed(Double.parseDouble(durationused));
		
		durableData.setTimeUsed(Double.parseDouble(timeused));
		
		DurableServiceProxy.getDurableService().update(durableData);
		
		SetEventInfo setEventInfo = new SetEventInfo();
		
		setEventInfo.setUdfs(udfs);
		
		DurableServiceProxy.getDurableService().setEvent(durableKey, eventInfo, setEventInfo);
	}

	
	* Name : cancelCreateMask
	* Desc : This function is cancelCreateMask 
	* Author : ZXY IDM Team
	* Date : 2011.01.25
	
	public void cancelCreateMask(String durableName, 
								 String eventUser)
			throws InvalidStateTransitionSignal, FrameworkErrorSignal, NotFoundSignal, DuplicateNameSignal {
		
		DurableKey durableKey = new DurableKey();
		durableKey.setDurableName( durableName );
		
		Durable durableData = DurableServiceProxy.getDurableService().selectByKey( durableKey );

		if (durableData.getLastEventName().equals("CreateMask")
				|| durableData.getLastEventName().equals("create")) 
		{
			UndoInfo undoInfo = new UndoInfo();
			undoInfo.setEventName( "CancelCreateMask");
			undoInfo.setEventTime( ConvertUtil.getCurrTimeStampSQL());
			undoInfo.setEventUser( eventUser);
			undoInfo.setLastEventTimeKey( durableData.getLastEventTimeKey());

			
			EventInfo eventInfo = new EventInfo();
			eventInfo.setEventName("");
			eventInfo.setEventTimeKey("");
			
			DurableServiceProxy.getDurableService().undo( durableKey, eventInfo, undoInfo );
		}
		else
			throw new NotFoundSignal(getClass(), "cancelCreateMask", "LastEventName=" + durableData.getLastEventName(), "" );
	}
	
	
	* Name : cancelCreateMask
	* Desc : This function is cancelCreateMask 
	* Author : ZXY IDM Team
	* Date : 2011.01.25
	
	public void cancelCreateMask(EventInfo eventInfo,String durableName)
	throws InvalidStateTransitionSignal, FrameworkErrorSignal, NotFoundSignal, DuplicateNameSignal {
	
		DurableKey durableKey = new DurableKey();
		durableKey.setDurableName( durableName );
		
		Durable durableData = DurableServiceProxy.getDurableService().selectByKey( durableKey );
		
		if (durableData.getLastEventName().equals("CreateMask")
		|| durableData.getLastEventName().equals("create")) 
		{
			UndoInfo undoInfo = new UndoInfo();
			undoInfo.setEventName( "CancelCreateMask");
			undoInfo.setEventTime( ConvertUtil.getCurrTimeStampSQL());
			undoInfo.setEventUser( eventInfo.getEventUser());
			undoInfo.setLastEventTimeKey( durableData.getLastEventTimeKey());
			
			DurableServiceProxy.getDurableService().undo( durableKey, eventInfo, undoInfo );
		}
		else{
			throw new NotFoundSignal(getClass(), "cancelCreateMask", "LastEventName=" + durableData.getLastEventName(), "" );
		}
	}
	

	
	* Name : LoadMask
	* Desc : This function is LoadMask 
	* Author : ZXY IDM Team
	* Date : 2011.01.25
	
	public void LoadMask(EventInfo eventInfo, 
						 String durableName,
						 Map<String, String> udfs)
			throws InvalidStateTransitionSignal, FrameworkErrorSignal, NotFoundSignal, DuplicateNameSignal {
		log.info("LoadMask");
		
		DurableKey durableKey = new DurableKey();
		durableKey.setDurableName( durableName );
		
		Durable durableData = DurableServiceProxy.getDurableService().selectByKey( durableKey );

		ChangeSpecInfo changeSpecInfo = new ChangeSpecInfo();

		changeSpecInfo.setAreaName( durableData.getAreaName());
		changeSpecInfo.setDurableSpecName( durableData.getDurableSpecName());
		changeSpecInfo.setDurableSpecVersion( durableData.getDurableSpecVersion());
		changeSpecInfo.setFactoryName( durableData.getFactoryName());
		changeSpecInfo.setCapacity( durableData.getCapacity());
		changeSpecInfo.setDurationUsedLimit( durableData.getDurationUsedLimit());
		changeSpecInfo.setTimeUsedLimit( durableData.getTimeUsedLimit());
		changeSpecInfo.setUdfs( udfs );

		DurableServiceProxy.getDurableService().changeSpec( durableKey, eventInfo, changeSpecInfo);
	}

	
	* Name : UnloadMask
	* Desc : This function is UnloadMask 
	* Author : ZXY IDM Team
	* Date : 2011.01.25
	
	public void UnloadMask(EventInfo eventInfo, 
						   String durableName,
						   Map<String, String> udfs)
			throws InvalidStateTransitionSignal, FrameworkErrorSignal, NotFoundSignal, DuplicateNameSignal {
		log.info("UnloadMask");
	
		DurableKey durableKey = new DurableKey();
		durableKey.setDurableName( durableName );
		
		Durable durableData = DurableServiceProxy.getDurableService().selectByKey( durableKey );

		ChangeSpecInfo changeSpecInfo = new ChangeSpecInfo();

		changeSpecInfo.setAreaName( durableData.getAreaName());
		changeSpecInfo.setDurableSpecName( durableData.getDurableSpecName());
		changeSpecInfo.setDurableSpecVersion( durableData.getDurableSpecVersion());
		changeSpecInfo.setFactoryName( durableData.getFactoryName());
		changeSpecInfo.setCapacity( durableData.getCapacity());
		changeSpecInfo.setDurationUsedLimit( durableData.getDurationUsedLimit());
		changeSpecInfo.setTimeUsedLimit( durableData.getTimeUsedLimit());
		
		udfs = ( HashMap<String, String> )durableData.getUdfs();
	
		String machineName = udfs.get( "MACHINENAME" );
		if ( machineName != null )
		{
			udfs.put( "MACHINENAME", "" );
			changeSpecInfo.setUdfs( udfs );		
		}
		
		DurableServiceProxy.getDurableService().changeSpec( durableKey, eventInfo, changeSpecInfo);
	}
	
	
	* Name : changeMaskInfo
	* Desc : This function is changeMaskInfo 
	* Author : ZXY IDM Team
	* Date : 2011.01.25
	
	public void changeMaskInfo(EventInfo eventInfo,
							   String durableName, 
							   long timeUsedLimit, 
							   long durationUsedLimit, 
							   String areaName,
							   Map<String, String> udfs)
			throws InvalidStateTransitionSignal, FrameworkErrorSignal, NotFoundSignal, DuplicateNameSignal {
		log.info("ChangeSpec");
		
		DurableKey durableKey = new DurableKey();
		durableKey.setDurableName( durableName );
		
		Durable durableData = DurableServiceProxy.getDurableService().selectByKey( durableKey );

		ChangeSpecInfo changeSpecInfo = new ChangeSpecInfo();
		changeSpecInfo.setAreaName( areaName );
		changeSpecInfo.setCapacity( durableData.getCapacity());
		changeSpecInfo.setDurableSpecName( durableData.getDurableSpecName());
		changeSpecInfo.setDurableSpecVersion( durableData.getDurableSpecVersion());
		changeSpecInfo.setFactoryName( durableData.getFactoryName());
		changeSpecInfo.setDurationUsedLimit( durationUsedLimit );
		changeSpecInfo.setTimeUsedLimit( timeUsedLimit );
		changeSpecInfo.setUdfs( udfs );

		DurableServiceProxy.getDurableService().changeSpec( durableKey, eventInfo, changeSpecInfo);
	}
	
	
	* Name : changeMaskInfo
	* Desc : This function is changeMaskInfo 
	* Author : ZXY IDM Team
	* Date : 2011.01.25
	
	public void changeMaskInfo(EventInfo eventInfo,
			   String durableName, 
			   String timeUsedLimit, 
			   String durationUsedLimit, 
			   Map<String, String> udfs)
		throws InvalidStateTransitionSignal, FrameworkErrorSignal, NotFoundSignal, DuplicateNameSignal {
		log.info("ChangeSpec");
		
		DurableKey durableKey = new DurableKey();
		durableKey.setDurableName( durableName );
		
		Durable durableData = DurableServiceProxy.getDurableService().selectByKey( durableKey );
		
		ChangeSpecInfo changeSpecInfo = new ChangeSpecInfo();
		changeSpecInfo.setAreaName( durableData.getAreaName() );
		changeSpecInfo.setCapacity( durableData.getCapacity());
		changeSpecInfo.setDurableSpecName( durableData.getDurableSpecName());
		changeSpecInfo.setDurableSpecVersion( durableData.getDurableSpecVersion());
		changeSpecInfo.setFactoryName( durableData.getFactoryName());
		changeSpecInfo.setDurationUsedLimit(Double.parseDouble(durationUsedLimit));
		changeSpecInfo.setTimeUsedLimit(Double.parseDouble(timeUsedLimit));
		changeSpecInfo.setUdfs( udfs );
		
		DurableServiceProxy.getDurableService().changeSpec( durableKey, eventInfo, changeSpecInfo);
	}
	
	
	* Name : getDurableData
	* Desc : This function is getDurableData 
	* Author : ZXY IDM Team
	* Date : 2011.01.25
	
	public Element getDurableData(String durableName) throws FrameworkErrorSignal, NotFoundSignal
	{		
	    Element element = null;
	    
	    return element;
	}
	
	
	* Name : createMask
	* Desc : This function is createMask 
	* Author : ZXY IDM Team
	* Date : 2011.01.25
	
	public void createMask(EventInfo eventInfo, 
						   String durableName,
						   String machineName,
						   String maskPosition,
						   String factoryName, 
						   Map<String, String> udfs) 
			throws InvalidStateTransitionSignal, FrameworkErrorSignal, NotFoundSignal, DuplicateNameSignal, Exception {
		log.info("Mask Create");
		
		String sql = " SELECT DURABLESPECNAME " +
		 			 "   FROM DURABLESPEC " +
		             "  WHERE FACTORYNAME = ? " +
		             "    AND DURABLETYPE = 'MASK' ";

		String[] bindSet = new String[]{ factoryName };
		
		String[][] resultList = null;
		resultList = IDMFrameServiceProxy.getSqlTemplate().queryForStringArray(sql, bindSet);
		
		if (resultList.length == 0)
		{
			log.info("Not found durableSpec");
			throw new CustomException("durableSpec-0001", resultList[0][0]);
		} 
		else 
		{
			DurableSpecKey durableSpecKey = new DurableSpecKey();
	
			durableSpecKey.setFactoryName( factoryName );
			durableSpecKey.setDurableSpecName( resultList[0][0] );
			durableSpecKey.setDurableSpecVersion( "00001" );
			
			DurableSpec durableSpecData = DurableServiceProxy.getDurableSpecService().selectByKey( durableSpecKey );
			
			DurableKey durableKey = new DurableKey();
			durableKey.setDurableName( durableName );
			
			CreateInfo createInfo = new CreateInfo();
			createInfo.setAreaName( "" );
			createInfo.setCapacity( durableSpecData.getDefaultCapacity());
			createInfo.setDurableName( durableName );
			createInfo.setDurableSpecName( resultList[0][0] );
			createInfo.setDurableSpecVersion( "00001" );
			createInfo.setDurableType( durableSpecData.getDurableType());
			createInfo.setDurationUsedLimit( durableSpecData.getDurationUsedLimit());
			createInfo.setTimeUsedLimit( durableSpecData.getTimeUsedLimit());
			createInfo.setFactoryName( factoryName );
			
			for(int i = 0; i < udfs.size(); i++)
			{
				if(udfs.get(i).equals("MACHINENAME"))
				{
					udfs.put("MACHINENAME", machineName);
				}
				if(udfs.get(i).equals("PORTNAME"))
				{
					udfs.put("PORTNAME", maskPosition);
				}
			}			
			createInfo.setUdfs( udfs );
			
			DurableServiceProxy.getDurableService().create( durableKey, eventInfo, createInfo );
		}
	}*/
	

	
	
	public void create(EventInfo eventInfo, CreateInfo DurableData) throws Exception
	{		 
		String createSql ="INSERT INTO DURABLE "
				+ " ( "
				+ " SITENAME"
				+ " ,DURABLENAME "
				+ " ,DURABLESPECNAME "
				+ " ,DURABLESTATE "
				+ " ,SHIPREQUESTNAME "
				+ " ,ASSIGNEDPACKINGQUANTITY "
				+ " ,LOCATIONNAME"
				+ " ,CREATETIME "
				+ " ,CREATEUSER "
				+ " ,LASTEVENTNAME "
				+ " ,LASTEVENTTIMEKEY "
				+ " ,LASTEVENTTIME "
				+ " ,LASTEVENTUSER "
				+ " ,LASTEVENTCOMMENT "
				+ " ,REASONCODETYPE "
				+ " ,REASONCODE "
				+ " ,STOCKSTATE "
				+ " ) "
				+ " VALUES ( "
				+ " :SITENAME"
				+ " ,:DURABLENAME "
				+ " ,:DURABLESPECNAME "
				+ " ,:DURABLESTATE "
				+ " ,:SHIPREQUESTNAME "
				+ " ,:ASSIGNEDPACKINGQUANTITY "
				+ " ,:LOCATIONNAME"
				+ " ,SYSDATE "
				+ " ,:CREATEUSER "
				+ " ,:LASTEVENTNAME "
				+ " ,:LASTEVENTTIMEKEY "
				+ " ,SYSDATE "
				+ " ,:LASTEVENTUSER "
				+ " ,:LASTEVENTCOMMENT "
				+ " ,:REASONCODETYPE "
				+ " ,:REASONCODE "
				+ " ,:STOCKSTATE "
				+ " ) ";
		Map<String, String> bindMap = new HashMap<String, String>();
		bindMap.put("SITENAME", System.getProperty("factory"));
		bindMap.put("DURABLENAME", DurableData.getDurableName());
		bindMap.put("DURABLESPECNAME", DurableData.getDurableSpecName());
		bindMap.put("DURABLESTATE", "IDLE");
		bindMap.put("SHIPREQUESTNAME", "");
		bindMap.put("ASSIGNEDPACKINGQUANTITY", "0");
		bindMap.put("LOCATIONNAME", "");
		bindMap.put("CREATEUSER", eventInfo.getEventUser());
		bindMap.put("LASTEVENTNAME", eventInfo.getEventName());
		bindMap.put("LASTEVENTTIMEKEY", TimeStampUtil.getCurrentEventTimeKey());
		bindMap.put("LASTEVENTUSER", eventInfo.getEventUser());
		bindMap.put("LASTEVENTCOMMENT", eventInfo.getEventComment());
		bindMap.put("REASONCODETYPE", "");
		bindMap.put("REASONCODE", "");
		bindMap.put("STOCKSTATE", DurableData.getLocationName());
			
		IDMFrameServiceProxy.getSqlTemplate().update(createSql, bindMap);
		
	}
	
	public void modify(EventInfo eventInfo, String durableName, String durableState ) throws Exception
	{		 
		String updateSql ="UPDATE DURABLE SET "
				+ " DURABLESTATE=:DURABLESTATE "
				+ " ,LASTEVENTNAME=:LASTEVENTNAME "
				+ " ,LASTEVENTTIMEKEY=:LASTEVENTTIMEKEY "
				+ " ,LASTEVENTTIME=SYSDATE "
				+ " ,LASTEVENTUSER=:LASTEVENTUSER "
				+ " ,LASTEVENTCOMMENT=:LASTEVENTCOMMENT "
				+ " WHERE "
				+ " SITENAME=:SITENAME "
				+ " AND DURABLENAME=:DURABLENAME ";
		Map<String, String> bindMap = new HashMap<String, String>();
		bindMap.put("SITENAME", System.getProperty("factory"));
		bindMap.put("DURABLENAME", durableName);
		bindMap.put("DURABLESTATE", durableState);
		bindMap.put("LASTEVENTNAME", eventInfo.getEventName());
		bindMap.put("LASTEVENTTIMEKEY", TimeStampUtil.getCurrentEventTimeKey());
		bindMap.put("LASTEVENTUSER", eventInfo.getEventUser());
		bindMap.put("LASTEVENTCOMMENT", eventInfo.getEventComment());

			
		IDMFrameServiceProxy.getSqlTemplate().update(updateSql, bindMap);
		
	}
	
	public void remove(EventInfo eventInfo, String durableName ) throws Exception
	{		 
		String updateSql ="DELETE FROM DURABLE  "
				+ " WHERE "
				+ " SITENAME=:SITENAME "
				+ " AND DURABLENAME=:DURABLENAME ";
		Map<String, String> bindMap = new HashMap<String, String>();
		bindMap.put("SITENAME", System.getProperty("factory"));
		bindMap.put("DURABLENAME", durableName);
		
		IDMFrameServiceProxy.getSqlTemplate().update(updateSql, bindMap);
		
	}
	
	public void recycle(EventInfo eventInfo,String durableName,String siteName) throws Exception{
		
		String[] durableNameList = durableName.split("\\|");
		
		String sql="UPDATE DURABLE SET "
				+ " DURABLESTATE='IDLE' "
				+ " ,LASTEVENTNAME=:LASTEVENTNAME "
				+ " ,LASTEVENTTIMEKEY=:LASTEVENTTIMEKEY "
				+ " ,LASTEVENTTIME=SYSDATE "
				+ " ,LASTEVENTUSER=:LASTEVENTUSER "
				+ " ,LASTEVENTCOMMENT=:LASTEVENTCOMMENT "
				+ " ,SHIPREQUESTNAME='' "
				+ " ,ASSIGNEDPACKINGQUANTITY='0' "
				+ " WHERE "
				+ " SITENAME=:SITENAME "
				+ " AND DURABLENAME=:DURABLENAME ";
		Map<String, Object> bindMap = new HashMap<String, Object>();
		bindMap.put("SITENAME", siteName);
		bindMap.put("LASTEVENTNAME", eventInfo.getEventName());
		bindMap.put("LASTEVENTTIMEKEY", TimeStampUtil.getCurrentEventTimeKey());
		bindMap.put("LASTEVENTUSER", eventInfo.getEventUser());
		bindMap.put("LASTEVENTCOMMENT", eventInfo.getEventComment());
		
		for (int i = 0; i < durableNameList.length; i++) {
			bindMap.put("DURABLENAME", durableNameList[i]);
			IDMFrameServiceProxy.getSqlTemplate().update(sql, bindMap);
		}
		
	}


}
