Beispiel #1
0
        public bool ImportFile(string sFile, DTDataDestination dest, bool bSilent)
        {
            bool bOK = false;

            System.Data.DataTable tblSrc    = null;
            System.IO.FileStream  stream    = null;
            IFormatter            formatter = new BinaryFormatter();

            try
            {
                stream = new System.IO.FileStream(sFile, System.IO.FileMode.Open, System.IO.FileAccess.Read);
                tblSrc = (System.Data.DataTable)formatter.Deserialize(stream);
                bOK    = true;
            }
            catch (Exception er)
            {
                _error = ExceptionLimnorDatabase.FormExceptionText(er, "Error importing file [{0}].", sFile);
                TraceLogClass.TraceLog.ShowMessageBox = !bSilent;
                TraceLogClass.TraceLog.Log(er);
            }
            finally
            {
                if (stream != null)
                {
                    stream.Close();
                }
            }
            if (bOK)
            {
                _error = dest.ReceiveData(tblSrc, bSilent);
            }
            return(bOK);
        }
        public object Clone()
        {
            DTDataDestination obj = new DTDataDestination();

            obj.SetOwner(_owner);
            obj.DestinationType = DestinationType;
            obj._db             = (DTDest)DatabaseDestination.Clone();
            obj._text           = (DTDestTextFile)TextDestination.Clone();
            return(obj);
        }
        void startDT()
        {
            _error = null;
            bool       bSilent = Silent;
            enumDTType tp      = this.EndPointType;

            if (tp == enumDTType.Both || tp == enumDTType.Sender)
            {
                IEPDataSource dtsSrc = DataSource;
                ClientDate    cp     = new ClientDate(ID);
                cp.LoadSettings();
                dtsSrc.Timestamp = cp.dt;
                if (tp == enumDTType.Both)
                {
                    DTDataDestination dest = this.DataDestination;
                    if (dest.IsReady)
                    {
                        if (StartedDataTransfer != null)
                        {
                            StartedDataTransfer(this, EventArgs.Empty);
                        }
                        DataTable tblSrc = dtsSrc.DataSource;
                        if (tblSrc != null)
                        {
                            _error = dest.ReceiveData(tblSrc, bSilent);
                        }
                        else
                        {
                            _error = dtsSrc.LastError;
                        }
                        if (string.IsNullOrEmpty(_error))
                        {
                            if (FinishedDataTransfer != null)
                            {
                                FinishedDataTransfer();
                            }
                        }
                        else
                        {
                            if (ErrorDataTransfer != null)
                            {
                                ErrorDataTransfer(this, EventArgs.Empty);
                            }
                        }
                    }
                }
                else
                {
                    if (StartedDataTransfer != null)
                    {
                        StartedDataTransfer(this, EventArgs.Empty);
                    }
                    System.Data.DataTable tblSrc = dtsSrc.DataSource;
                    if (tblSrc != null)
                    {
                        TransMethod method = DataTransferMethod;
                        if (method.SendFile(tblSrc, Name, bSilent))
                        {
                            if (FinishedDataTransfer != null)
                            {
                                FinishedDataTransfer();
                            }
                        }
                        else
                        {
                            _error = method.ErrorMessage;
                            if (ErrorDataTransfer != null)
                            {
                                ErrorDataTransfer(this, EventArgs.Empty);
                            }
                        }
                    }
                }
                if (string.IsNullOrEmpty(_error))
                {
                    cp.dt = dtsSrc.Timestamp;
                    cp.SaveSettings();
                }
                dtsSrc.ClearData();
            }
            else
            {
                DTDataDestination dest = DataDestination;
                if (dest.IsReady)
                {
                    if (StartedDataTransfer != null)
                    {
                        StartedDataTransfer(this, EventArgs.Empty);
                    }
                    TransMethod method = DataTransferMethod;
                    if (method.ReceiveFile(Name, dest, bSilent))
                    {
                        if (FinishedDataTransfer != null)
                        {
                            FinishedDataTransfer();
                        }
                    }
                    else
                    {
                        _error = method.ErrorMessage;
                        if (ErrorDataTransfer != null)
                        {
                            ErrorDataTransfer(this, EventArgs.Empty);
                        }
                    }
                }
            }
        }
Beispiel #4
0
        /// <summary>
        /// 1. download files to workFolder.
        /// 2. call ImportFiles to process all files in workFolder.
        /// 3. move processed files to workFolder\backup.
        /// </summary>
        /// <param name="name"></param>
        /// <param name="dest"></param>
        /// <param name="bSilent"></param>
        /// <returns></returns>
        public bool ReceiveFile(string name, DTDataDestination dest, bool bSilent)
        {
            bool bOK = false;

            if (ParametersOK(true))
            {
                int i;
                _error = null;
                if (TransferMethod == enumDTMethod.LAN)
                {
                    string[] sFiles = System.IO.Directory.GetFiles(LanFolder, string.Format(CultureInfo.InvariantCulture, "{0}_*.dt", name));
                    if (sFiles != null)
                    {
                        bOK = true;
                        for (i = 0; i < sFiles.Length; i++)
                        {
                            string tgt = Path.Combine(WorkFolder, System.IO.Path.GetFileName(sFiles[i]));
                            try
                            {
                                System.IO.File.Move(sFiles[i], tgt);
                            }
                            catch (Exception er)
                            {
                                _error = ExceptionLimnorDatabase.FormExceptionText(er, "ReceiveFile. Error moving file [{0}] to [{1}].", sFiles[i], tgt);
                                TraceLogClass.TraceLog.ShowMessageBox = !bSilent;
                                TraceLogClass.TraceLog.Log(er);
                                bOK = false;
                                break;
                            }
                        }
                    }
                }
                else if (TransferMethod == enumDTMethod.FTP)
                {
                    FtpClient objFTP = Ftp.CreateFtpClient();
                    if (objFTP.GetContents(Ftp.Folder))
                    {
                        FtpFileInfo[] sFiles = objFTP.FileList;
                        if (sFiles != null)
                        {
                            for (i = 0; i < sFiles.Length; i++)
                            {
                                if (IsDataFile(sFiles[i].Filename, name))
                                {
                                    if (objFTP.Download(WorkFolder, sFiles[i].Filename))
                                    {
                                        objFTP.DeleteFile(sFiles[i].Filename);
                                    }
                                    else
                                    {
                                        _error = string.Format(CultureInfo.InvariantCulture, "Cannot download file [{0}] from the FTP server [{1}]. {2}", sFiles[i].Filename, Ftp.Host, objFTP.ErrorMessage);
                                        break;
                                    }
                                }
                            }
                        }
                    }
                    else
                    {
                        _error = string.Format(CultureInfo.InvariantCulture, "Cannot get file list from the FTP server [{0}]. {1} ", Ftp.Host, objFTP.ErrorMessage);
                    }
                    bOK = string.IsNullOrEmpty(_error);
                    if (!bOK)
                    {
                        TraceLogClass.TraceLog.ShowMessageBox = !bSilent;
                        TraceLogClass.TraceLog.Log(new Exception(_error));
                    }
                }
            }
            bool bImport = ImportFiles(name, dest, bSilent);

            if (bOK)
            {
                bOK = bImport;
            }
            return(bOK);
        }
Beispiel #5
0
        public bool ImportFiles(string name, DTDataDestination dest, bool bSilent)
        {
            bool   bOK        = true;
            string sBackupDir = Path.Combine(WorkFolder, "Backup");

            if (!System.IO.Directory.Exists(sBackupDir))
            {
                try
                {
                    System.IO.Directory.CreateDirectory(sBackupDir);
                }
                catch (Exception er)
                {
                    _error = ExceptionLimnorDatabase.FormExceptionText(er, "Error creating backup folder [{0}].", sBackupDir);
                    TraceLogClass.TraceLog.ShowMessageBox = !bSilent;
                    TraceLogClass.TraceLog.Log(er);
                    return(false);
                }
            }
            string[] sFiles = System.IO.Directory.GetFiles(WorkFolder, string.Format(CultureInfo.InvariantCulture, "{0}_*.dt", name));
            if (sFiles != null)
            {
                //sort file names
                string s;
                int    i;
                for (i = 1; i < sFiles.Length; i++)
                {
                    if (sFiles[i].CompareTo(sFiles[i - 1]) > 0)
                    {
                        for (int k = i; k > 0; k--)
                        {
                            if (sFiles[k].CompareTo(sFiles[k - 1]) > 0)
                            {
                                s             = sFiles[k];
                                sFiles[k]     = sFiles[k - 1];
                                sFiles[k - 1] = s;
                            }
                            else
                            {
                                break;
                            }
                        }
                    }
                }
                for (i = 0; i < sFiles.Length; i++)
                {
                    bOK = ImportFile(sFiles[i], dest, bSilent);
                    if (bOK)
                    {
                        string tgt = Path.Combine(sBackupDir, System.IO.Path.GetFileName(sFiles[i]));
                        try
                        {
                            System.IO.File.Move(sFiles[i], tgt);
                        }
                        catch (Exception er)
                        {
                            _error = ExceptionLimnorDatabase.FormExceptionText(er, "Error moving file [{0}] to [{1}].", sFiles[i], tgt);
                            TraceLogClass.TraceLog.ShowMessageBox = !bSilent;
                            TraceLogClass.TraceLog.Log(er);
                            bOK = false;
                            break;
                        }
                    }
                    else
                    {
                        break;
                    }
                }
            }
            return(bOK);
        }