package flow;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.log4j.Logger;

/**
 * A basic servlet which allows a user to define their code, generate
 * any output, and to select where to transition to next.
 * Last generated by Orchestration Designer at: 2017年4月12日 下午01时21分51秒
 */
public class GetDataFromUUI extends com.avaya.sce.runtime.BasicServlet {
	private static final boolean List = false;
	private static Logger log = Logger.getLogger(GetDataFromUUI.class);
	//{{START:CLASS:FIELDS
	//}}END:CLASS:FIELDS

	/**
	 * Default constructor
	 * Last generated by Orchestration Designer at: 2017年4月12日 下午01时21分51秒
	 */
	public GetDataFromUUI() {
		//{{START:CLASS:CONSTRUCTOR
		super();
		//}}END:CLASS:CONSTRUCTOR
	}

	/**
	 * This method allows for custom integration with other Java components.
	 * You may use Java for sophisticated logic or to integrate with custom
	 * connectors (i.e. JMS, custom web services, sockets, XML, JAXB, etc.)
	 *
	 * Any custom code added here should work as efficiently as possible to prevent delays.
	 * It's important to design your callflow so that the voice browser (Voice Portal/IR)
	 * is not waiting too long for a response as this can lead to a poor caller experience.
	 * Additionally, if the response to the client voice browser exceeds the configured
	 * timeout, the platform may throw an "error.badfetch". 
	 *
	 * Using this method, you have access to all session variables through the 
	 * SCESession object.
	 *
	 * The code generator will *** NOT *** overwrite this method in the future.
	 * Last generated by Orchestration Designer at: 2017年4月12日 下午01时21分51秒
	 */
	public void servletImplementation(com.avaya.sce.runtimecommon.SCESession mySession) {
		try {
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
			String sj = df.format(new Date());
			log.info("GetDataFromUUI开始时间=======" + df.format(new Date()));
			// TODO: Add your code here!
			String UUI = mySession.getVariableField(IProjectVariables.CTICALLINFO, IProjectVariables.CTICALLINFO_FIELD_UUI).getStringValue();
			String ucid = mySession.getVariableField(IProjectVariables.CTICALLINFO, IProjectVariables.CTICALLINFO_FIELD_UCID).getStringValue();
			String callid = mySession.getVariableField(IProjectVariables.CTICALLINFO, IProjectVariables.CTICALLINFO_FIELD_CALLID).getStringValue();
			String ani = mySession.getVariableField(IProjectVariables.CTICALLINFO, IProjectVariables.CTICALLINFO_FIELD_ANI).getStringValue();
			String dnis = mySession.getVariableField(IProjectVariables.CTICALLINFO, IProjectVariables.CTICALLINFO_FIELD_DNIS).getStringValue();
			String state = mySession.getVariableField(IProjectVariables.CTICALLINFO, IProjectVariables.CTICALLINFO_FIELD_STATE).getStringValue();
			String stationextension = mySession.getVariableField(IProjectVariables.CTICALLINFO, IProjectVariables.CTICALLINFO_FIELD_STATIONEXTENSION).getStringValue();
			log.info("UUI=======" + UUI);
			log.info("uui="   +UUI+ "  ucid=   "+ucid+ "  callid=   "+callid+ "  ani=   "+ani+ "  state=   "+state+ "  stationextension=   "+stationextension);
			String AAI = mySession.getVariableField(IProjectVariables.SESSION, IProjectVariables.SESSION_FIELD_AAI).getStringValue();
			log.info("AAI="   +AAI );
			System.out.println(UUI);
			if(UUI == "" || UUI == null)
			{
				UUI = "181BE32B-312E-8908-457D-A7B30BC8D034";
			}
			String[] UUIArray = null;
			System.out.println("ConvertStrToArray:");
			//UUIArray = ConvertStrToArray(UUI);
			//PrintArray(UUIArray);
			log.info("Set UUIGroup:          "+UUI);
			mySession.getVariableField(IProjectVariables.UUIGROUP, IProjectVariables.UUIGROUP_FIELD_UCID).setValue(UUI);
			System.out.println(mySession.getVariableField(IProjectVariables.UUIGROUP, IProjectVariables.UUIGROUP_FIELD_UCID).getStringValue());
			/*mySession.getVariableField(IProjectVariables.UUIGROUP, IProjectVariables.UUIGROUP_FIELD_CALLERNO).setValue(UUIArray[1]);
			System.out.println(mySession.getVariableField(IProjectVariables.UUIGROUP, IProjectVariables.UUIGROUP_FIELD_CALLERNO).getStringValue());
			mySession.getVariableField(IProjectVariables.UUIGROUP, IProjectVariables.UUIGROUP_FIELD_AGENTID).setValue(UUIArray[2]);
			System.out.println(mySession.getVariableField(IProjectVariables.UUIGROUP, IProjectVariables.UUIGROUP_FIELD_AGENTID).getStringValue());
			mySession.getVariableField(IProjectVariables.UUIGROUP, IProjectVariables.UUIGROUP_FIELD_EXTNO).setValue(UUIArray[3]);
			System.out.println(mySession.getVariableField(IProjectVariables.UUIGROUP, IProjectVariables.UUIGROUP_FIELD_EXTNO).getStringValue());
			mySession.getVariableField(IProjectVariables.UUIGROUP, IProjectVariables.UUIGROUP_FIELD_SVCTIME).setValue(UUIArray[4]);
			System.out.println(mySession.getVariableField(IProjectVariables.UUIGROUP, IProjectVariables.UUIGROUP_FIELD_SVCTIME).getStringValue());*/
			
		} catch (Exception e) {
			log.info("GetDataFromUUI=======" + e.getMessage());
		}
	}
	
	public static String[] ConvertStrToArray(String str){   
        String[] strArray = null;   
        strArray = str.split(","); //拆分字符为"," ,然后把结果交给数组strArray 
        return strArray;
    }
	
	public static void PrintArray(String[] array){   
	     for(int i=0;i<array.length;i++){
	   System.out.print(array[i]);
	   if(i==array.length-1){
	    System.out.print("\n");
	   }else{
	    System.out.print("|");
	   }
	  }
	}
	/**
	 * Builds the list of branches that are defined for this servlet object.
	 * This list is built automatically by defining Goto nodes in the call flow editor.
	 * It is the programmer's responsibilty to provide at least one enabled Goto.<BR>
	 *
	 * The user should override updateBranches() to determine which Goto that the
	 * framework will activate.  If there is not at least one enabled Goto item, 
	 * the framework will throw a runtime exception.<BR>
	 *
	 * This method is generated automatically and changes to it may
	 * be overwritten next time code is generated.  To modify the list
	 * of branches for the flow item, override:
	 *     <code>updateBranches(Collection branches, SCESession mySession)</code>
	 *
	 * @return a Collection of <code>com.avaya.sce.runtime.Goto</code>
	 * objects that will be evaluated at runtime.  If there are no gotos
	 * defined in the Servlet node, then this returns null.
	 * Last generated by Orchestration Designer at: 2021年12月23日 下午05时43分08秒
	 */
	public java.util.Collection getBranches(com.avaya.sce.runtimecommon.SCESession mySession) {
		java.util.List list = null;
		com.avaya.sce.runtime.Goto aGoto = null;
		list = new java.util.ArrayList(1);

		aGoto = new com.avaya.sce.runtime.Goto("SolveMenu", 0, true, "Default");
		list.add(aGoto);

		return list;
	}
}
