Ejemplo n.º 1
0
        private string ProcessFileHeader(string filename)
        {
            var header = File.ReadAllLines(filename)[0];

            var isemptyFile = !(string.IsNullOrEmpty(header) || header.Length < 18) ? string.Empty : "The Ascii file header record is missing or invalid. The file has not been processed. Please correct and resubmit.";

            if (!string.IsNullOrEmpty(isemptyFile))
            {
                headerErrors.AppendLine().AppendFormat(isemptyFile);
            }

            _fileInfo.FileName = filename;
            _fileInfo.SenderId = header.Length < 18 ? _fileProcessService.GetSenderIdByFileName(filename) : header.Substring(12, 6);

            dateValue = DateTime.Now.Date;
            var dateString = header.Length < 26 ? string.Empty : header.Substring(18, 2) + "/" + header.Substring(20, 2) + "/" + header.Substring(22, 4);

            DateTime.TryParseExact(dateString, "dd/MM/yyyy", new CultureInfo("en-GB"),
                                   DateTimeStyles.None, out dateValue);
            _fileInfo.CreationDate = dateValue;

            Int16 totalRecord    = 0;
            var   strTotalRecord = header.Length < 32 ? string.Empty : header.Substring(26, 6);

            Int16.TryParse(strTotalRecord, out totalRecord);
            _fileInfo.TotalRecords = totalRecord;


            _fileInfo.TableRef = header.Length < 44 ? string.Empty : header.Substring(32, 12);

            var validateMsdRecord = string.Empty;

            if (_fileInfo.TableRef.ToUpper().Equals("TABLE-1-UO-Q") ||
                _fileInfo.TableRef.ToUpper().Equals("TABLE-1-UQ-Q") ||
                _fileInfo.TableRef.ToUpper().Equals("TABLE-2-FT-A") ||
                _fileInfo.TableRef.ToUpper().Equals("TABLE-3-LA-A") ||
                _fileInfo.TableRef.ToUpper().Equals("TABLE-4-VS-Q") ||
                _fileInfo.TableRef.ToUpper().Equals("TABLE-5-FT-A"))
            {
                if (!(_fileInfo.TableRef.ToUpper().Equals("TABLE-1-UO-Q") ||
                      _fileInfo.TableRef.ToUpper().Equals("TABLE-1-UQ-Q") ||
                      _fileInfo.TableRef.ToUpper().Equals("TABLE-2-FT-A") ||
                      _fileInfo.TableRef.ToUpper().Equals("TABLE-3-LA-A")))
                {
                    validateMsdRecord = "The Ascii file header record is not in the correct format for a submission. The file has not been processed.";
                    ArchiveProcessedFilesFromBucket(filename, true);
                    var tokens = new Dictionary <string, dynamic>();
                    tokens.Add("filename", filename);
                    _emailSender.SendEmail(_settings.MaritimeHelpdeskEmailAddress, "Error in file submitted", validateMsdRecord, tokens);
                    return(string.Empty);
                }
            }
            else
            {
                validateMsdRecord = "The Ascii file header record is not in the correct format for a submission. The file has not been processed. Please correct and resubmit.";
                headerErrors.AppendLine().AppendFormat(validateMsdRecord);
            }


            _fileInfo.IsAmendment = header.Length < 45 || header.Substring(44, 1).ToUpper().Equals("Y") ? Convert.ToSByte(1) : Convert.ToSByte(0);
            _fileInfo.IsTest      = header.Length < 46 || header.Substring(45, 1).ToUpper().Equals("Y") ? Convert.ToSByte(1) : Convert.ToSByte(0);


            int regNumber    = 0;
            var strRegNumber = header.Length < 50 ? string.Empty : header.Substring(46, 4);

            Int32.TryParse(strRegNumber, out regNumber);
            _fileInfo.RegistrationNumber = regNumber;

            _fileInfo.SendersRef = header.Length < 62 ? string.Empty : header.Substring(50, 12);

            if (_fileInfo != null)
            {
                previousFileRefId = _fileProcessService.GetLastInvalidFileRefId(_fileInfo);

                isDuplicatedFile = _fileProcessService.CheckFileExist(_fileInfo);

                _fileProcessService.Add(_fileInfo);
            }

            if (!string.IsNullOrEmpty(headerErrors.ToString().Trim()))
            {
                AddErrorLogToDbAndNotifyUser(filename, headerErrors.ToString());
            }
            return(_fileInfo.SenderId);
        }
Ejemplo n.º 2
0
        private FlatFile ProcessHeader(string filename, GesmesHelpers gesmesHelpers)
        {
            var lines = File.ReadAllLines(filename);

            var senderID = gesmesHelpers.GetDataItem("UNOC:3");

            _fileInfo.SenderId = (senderID.Contains(":") ? senderID.Split(':')[0] : senderID);
            var isValidSenederId = !string.IsNullOrEmpty(_fileInfo.SenderId) ?
                                   string.Empty : "The Gemses file header record is missing or invalid. The file has not been processed. " + " Please correct and resubmit.";

            if (!string.IsNullOrEmpty(isValidSenederId))
            {
                headerErrors.AppendLine().AppendFormat(isValidSenederId);
                NotifyErrorMsgToUsers(_fileInfo, headerErrors.ToString());
                return(null);
            }

            _fileInfo.FileName     = filename;
            _fileInfo.TotalRecords = gesmesHelpers.GetNumberOfRecords();
            _fileInfo.TableRef     = gesmesHelpers.GetDataItem("DSI");

            var validateMsdRecord = string.Empty;

            if (_fileInfo.TableRef.ToUpper().Equals("TABLE-1-UO-Q") ||
                _fileInfo.TableRef.ToUpper().Equals("TABLE-1-UQ-Q") ||
                _fileInfo.TableRef.ToUpper().Equals("TABLE-2-FT-A") ||
                _fileInfo.TableRef.ToUpper().Equals("TABLE-3-LA-A") ||
                _fileInfo.TableRef.ToUpper().Equals("TABLE-4-VS-Q") ||
                _fileInfo.TableRef.ToUpper().Equals("TABLE-5-FT-A"))
            {
                if (!(_fileInfo.TableRef.ToUpper().Equals("TABLE-1-UO-Q") ||
                      _fileInfo.TableRef.ToUpper().Equals("TABLE-1-UQ-Q") ||
                      _fileInfo.TableRef.ToUpper().Equals("TABLE-2-FT-A") ||
                      _fileInfo.TableRef.ToUpper().Equals("TABLE-3-LA-A")))
                {
                    validateMsdRecord = "The Gesmes file header record is not in the correct format for an MSD1 submission. The file has not been processed. Please correct and resubmit.";
                    ArchiveProcessedFilesFromBucket(filename, true);
                    var tokens = new Dictionary <string, dynamic>();
                    tokens.Add("filename", filename);
                    _emailSender.SendEmail(_settings.MaritimeHelpdeskEmailAddress, "Error in file submitted", validateMsdRecord, tokens);
                    return(null);
                }
            }
            else
            {
                validateMsdRecord = "The Gesmes file header record is not in the correct format for a submission. The file has not been processed. Please correct and resubmit.";
                headerErrors.AppendLine().AppendFormat(validateMsdRecord);
            }

            _fileInfo.IsAmendment  = gesmesHelpers.GetAmmendment("STS");
            _fileInfo.CreationDate = DateTime.Now.Date;
            _fileInfo.IsTest       = null;
            var regNo = gesmesHelpers.GetDataItem("UNB");

            _fileInfo.RegistrationNumber = regNo.Contains(":") ? int.Parse(regNo.Split(':')[1]) : int.Parse(regNo);
            _fileInfo.SendersRef         = gesmesHelpers.GetDataItem("UNH");

            if (_fileInfo != null && string.IsNullOrEmpty(validateMsdRecord))
            {
                previousFileRefId = _fileProcessService.GetLastInvalidFileRefId(_fileInfo);
                isDuplicatedFile  = _fileProcessService.CheckFileExist(_fileInfo);
                _fileProcessService.Add(_fileInfo);
            }

            if (string.IsNullOrEmpty(headerErrors.ToString().Trim()))
            {
                return(_fileInfo);
            }
            AddErrorLogToDbAndNotifyUser(filename, headerErrors.ToString());
            return(null);
        }