private AutomaticSourcingData deserializeAutomaticSourcingData(String text)
 {
     try
     {
         System.Web.Script.Serialization.JavaScriptSerializer Serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
         Serializer.MaxJsonLength = int.MaxValue;
         AutomaticSourcingData automaticSourcingdata = Serializer.Deserialize <AutomaticSourcingData>(text);
         if (automaticSourcingdata == null || automaticSourcingdata.automaticSourcingOid == 0)
         {
             return(null);
         }
         return(automaticSourcingdata);
     }
     catch (Exception e)
     {
         logger.Debug("Fail to deserialize issue!", e);
     }
     return(null);
 }
        public void Run(AutomaticSourcingData automaticData)
        {
            System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
            serializer.MaxJsonLength = int.MaxValue;

            Socket socket = buildSocket(SocketResourcePath + "/Run/");

            socket.OnMessage += (sender, e) =>
            {
                SaveInfo info = deserializeSaveInfo(e.Data);
                if (info != null)
                {
                    if (SaveTableHandler != null)
                    {
                        System.Windows.Application.Current.Dispatcher.Invoke(() => SaveTableHandler(info, null));
                    }
                    if (info.isEnd)
                    {
                        socket.Close(CloseStatusCode.Normal);
                    }
                    return;
                }

                AutomaticSourcingData issue = deserializeAutomaticSourcingData(e.Data);
                if (issue != null && !issue.isLast)
                {
                    if (SaveTableHandler != null && issue.tableName != null)
                    {
                        System.Windows.Application.Current.Dispatcher.Invoke(() =>
                        {
                            if (buildTableNameEventHandler != null)
                            {
                                buildTableNameEventHandler(issue);
                                string json = serializer.Serialize(issue);
                                socket.Connect();
                                socket.Send(json);
                            }
                        }
                                                                             );
                    }
                }


                TableSaveIssue TableIssue = deserializeTableIssue(e.Data);
                if (TableIssue != null)
                {
                    if (SaveTableHandler != null)
                    {
                        System.Windows.Application.Current.Dispatcher.Invoke(() =>
                        {
                            if (OnUpdateUniverse != null)
                            {
                                OnUpdateUniverse(TableIssue, true);
                                if (TableIssue != null && TableIssue.targetItem != null)
                                {
                                    TableIssue.targetItem.attribute = null;
                                }
                                string json = serializer.Serialize(TableIssue);
                                socket.Connect();
                                socket.Send(json);
                            }
                        });
                    }
                }

                if (issue is Kernel.Domain.AutomaticSourcingData && issue.isLast)
                {
                    if (SaveTableHandler != null)
                    {
                        System.Windows.Application.Current.Dispatcher.Invoke(() => SaveTableHandler(null, issue));
                    }
                    return;
                }

                logger.Debug("Recieve text : " + e.Data);
            };

            socket.OnOpen  += (sender, e) => { logger.Debug("Socket opened!"); };
            socket.OnError += (sender, e) =>
            {
                logger.Debug("Socket error : " + e.Message);
            };
            socket.OnClose += (sender, e) =>
            {
                logger.Debug("Socket closed : " + e.Reason);
            };

            socket.Connect();
            string text = serializer.Serialize(automaticData);

            socket.Send(text);

            //item = base.Save(item);
            //return table.tableOid;
        }