Beispiel #1
0
        public void ProcessFiles(bool IsTryAgain)
        {
            try
            {
                var    releaseToShipper = _serviceSettings.GetById((int)Settings.releaseToShipper).Value;
                var    crashFilesPath   = _serviceSettings.GetById((int)Settings.WebCrashFilesPath).Value;
                var    AckFiles         = _serviceSettings.GetById((int)Settings.AckFiles).Value;
                var    UnknownFiles     = _serviceSettings.GetById((int)Settings.UnknownFiles).Value;
                string EngineFilePath   = string.Empty;
                if (IsTryAgain)
                {
                    EngineFilePath = crashFilesPath + @"\TryAgain";// _serviceSettings.GetById((int)Settings.TryAgainFilePath).Value;
                }
                else
                {
                    EngineFilePath = _serviceSettings.GetById((int)Settings.WebRecSchedFilePath).Value;
                }
                var inFileList = Directory.GetFiles(EngineFilePath);
                if (inFileList != null && inFileList.Count() > 0)
                {
                    List <NMQRPerTransaction>   NmqrList        = new List <NMQRPerTransaction>();
                    List <SQTSPerTransaction>   SqtsList        = new List <SQTSPerTransaction>();
                    List <FileSysIncomingData>  incominDataList = new List <FileSysIncomingData>();
                    List <SQTSOPPerTransaction> SqtsopList      = new List <SQTSOPPerTransaction>();
                    bool IsProcessed = false;
                    Parallel.ForEach(inFileList, (file) =>
                    {
                        string fileText = string.Empty;
                        try
                        {
                            FileInfo fileInfo = new FileInfo(file);
                            if (File.Exists(file) && !IsFileLocked(fileInfo))
                            {
                                var currentfile       = new FileSysIncomingData();
                                currentfile.MessageId = Path.GetFileNameWithoutExtension(file);
                                fileText = File.ReadAllText(file);
                                File.SetAttributes(file, FileAttributes.Normal);
                                File.Delete(file);
                                currentfile.DecryptedData  = fileText;
                                StringBuilder decryptedEDI = null;
                                decryptedEDI        = new StringBuilder(currentfile.DecryptedData);
                                int pos             = decryptedEDI.ToString().IndexOf("ISA");
                                var data            = decryptedEDI.Remove(0, pos);
                                string pipelineDUNS = "";
                                pipelineDUNS        = data.ToString().Substring(35, 9);

                                currentfile.DecryptedData = decryptedEDI.ToString();
                                currentfile.PipeDuns      = pipelineDUNS;

                                //Start Processing
                                char dataSeperator, segmentSeperator;
                                dataSeperator    = Convert.ToChar(decryptedEDI.ToString()[(decryptedEDI.ToString().IndexOf("ISA") + "ISA".Length)]);
                                segmentSeperator = Convert.ToChar(decryptedEDI.ToString()[(decryptedEDI.ToString().IndexOf("GS") - 1)]);

                                if (dataSeperator != '\0' || segmentSeperator != '\0')
                                {
                                    EDIWrapperBase ediWrapper = new EDIWrapperBase(decryptedEDI.ToString()
                                                                                   , new char[] { segmentSeperator }
                                                                                   , new char[] { dataSeperator });
                                    List <string[]> separateSTFiles = ediWrapper.EnvelopeBlocks(EDIEnvelopeNodes.ST, EDIEnvelopeNodes.SE);

                                    foreach (string[] separateFile in separateSTFiles)
                                    {
                                        string subEDIfile = "";
                                        foreach (string element in separateFile)
                                        {
                                            subEDIfile = subEDIfile + element + Convert.ToChar(segmentSeperator);
                                        }
                                        if (!string.IsNullOrEmpty(subEDIfile))
                                        {
                                            EDIWrapperBase subEdiWrapper = new EDIWrapperBase(subEDIfile
                                                                                              , new char[] { segmentSeperator }//segement seperators
                                                                                              , new char[] { dataSeperator });//Dataseperators

                                            EDIFileType fileType = subEdiWrapper.FileType;
                                            switch (fileType)
                                            {
                                            case EDIFileType.NMQR:
                                                currentfile.DatasetType = "Nmqr";
                                                NmqrList.AddRange(FillUpNMQRData(subEDIfile, currentfile.MessageId, dataSeperator, segmentSeperator));
                                                IsProcessed = true;
                                                break;

                                            case EDIFileType.SQTS:
                                                currentfile.DatasetType = "SQTS";
                                                SqtsList.AddRange(FillUpSQTSData(subEDIfile, currentfile.MessageId, dataSeperator, segmentSeperator, redReasonList));
                                                IsProcessed = true;
                                                break;

                                            case EDIFileType.SQTSOP:
                                                currentfile.DatasetType = "SQTSOP";
                                                SqtsopList.AddRange(FillUpSQTSOPData(subEDIfile, currentfile.MessageId, dataSeperator, segmentSeperator));
                                                IsProcessed = true;
                                                break;

                                            case EDIFileType.Ack:
                                                IsProcessed             = false;
                                                currentfile.DatasetType = "ACK";
                                                if (!Directory.Exists(AckFiles))
                                                {
                                                    Directory.CreateDirectory(AckFiles);
                                                }
                                                var AckFilePath = AckFiles + "/" + currentfile.MessageId + ".txt";
                                                File.WriteAllText(AckFilePath, decryptedEDI.ToString());
                                                break;

                                            case EDIFileType.Unknown:
                                                IsProcessed             = false;
                                                currentfile.DatasetType = "UNK";
                                                if (!Directory.Exists(UnknownFiles))
                                                {
                                                    Directory.CreateDirectory(UnknownFiles);
                                                }
                                                var UnknownFilePath = UnknownFiles + "/" + currentfile.MessageId + ".txt";
                                                File.WriteAllText(UnknownFilePath, decryptedEDI.ToString());
                                                break;
                                            }
                                        }
                                    }
                                }
                                currentfile.ReceivedAt = DateTime.Now;
                                incominDataList.Add(currentfile);
                            }
                        }
                        catch (Exception ex)
                        {
                            string storeFilePath = string.Empty;
                            var name             = Path.GetFileNameWithoutExtension(file);
                            string data          = fileText;
                            if (File.Exists(file))
                            {
                                data = File.ReadAllText(file);
                            }
                            IsProcessed = false;
                            Console.WriteLine("crash");

                            if (IsTryAgain)
                            {
                                storeFilePath          = crashFilesPath + @"\CrashAgain\";
                                var EmailTo            = new string[] { "*****@*****.**", "*****@*****.**" };
                                string subject         = "Crashed File " + "(" + ConfigurationManager.AppSettings["Environment"] + ")";
                                string fromDisplay     = "NatGasHub";
                                string Body            = "Please find the attached crashed file.";
                                MemoryStream stream    = new MemoryStream(Encoding.UTF8.GetBytes(data));
                                Attachment attachments = new Attachment(stream, name + ".txt", System.Net.Mime.MediaTypeNames.Text.Plain);
                                EmailService.SendEmailAsync(EmailTo, Body, subject, fromDisplay, attachments);
                                stream.Dispose();
                            }
                            else
                            {
                                storeFilePath = crashFilesPath + @"\TryAgain\";
                            }

                            if (!Directory.Exists(storeFilePath))
                            {
                                Directory.CreateDirectory(storeFilePath);
                            }
                            storeFilePath = storeFilePath + name + ".txt";
                            File.WriteAllText(storeFilePath, data + Environment.NewLine + ex.Message);
                        }
                    });

                    bool IsDataEntered = false;

                    if (IsProcessed)
                    {
                        if (NmqrList.Count > 0)
                        {
                            IsDataEntered = ProcessNmqrData(NmqrList);
                        }
                        if (SqtsList.Count > 0)
                        {
                            IsDataEntered = ProcessSqtsData(SqtsList);
                        }
                        if (SqtsopList.Count > 0)
                        {
                            IsDataEntered = ProcessSqtqOpData(SqtsopList);
                        }
                        if (incominDataList.Count > 0)
                        {
                            _serviceFileSysIncomingData.AddListOfFileSysIncomingData(incominDataList);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                AppLogManager("ReceivePartFileInsertion", "Error", ex.ToString());
            }
        }
Beispiel #2
0
        public ActionResult UploadFile(HttpPostedFileBase postedFile)
        {
            try
            {
                string filePath = string.Empty;
                if (postedFile != null)
                {
                    string path = Server.MapPath("~/Uploads/");
                    if (!Directory.Exists(path))
                    {
                        Directory.CreateDirectory(path);
                    }

                    filePath = path + Path.GetFileName(postedFile.FileName);
                    string extension = Path.GetExtension(postedFile.FileName);
                    postedFile.SaveAs(filePath);

                    //Read the contents of CSV file.

                    string ediFile          = System.IO.File.ReadAllText(filePath);
                    var    dataSeperator    = Convert.ToChar(ediFile[(ediFile.IndexOf("ISA") + "ISA".Length)]);
                    var    segmentSeperator = Convert.ToChar(ediFile[(ediFile.IndexOf("GS") - 1)]);



                    OACY_DS ediOacy = new OACY_DS(ediFile
                                                  , new char[] { segmentSeperator }//segement seperators
                                                  , new char[] { dataSeperator }
                                                  , "test");
                    var fg = ediOacy.ReadOacyFile();



                    EDIWrapperBase ediWrapper = new EDIWrapperBase(ediFile
                                                                   , new char[] { segmentSeperator }//segement seperators
                                                                   , new char[] { dataSeperator });//Dataseperators
                    List <string[]> separateSTFiles = ediWrapper.EnvelopeBlocks(EDIEnvelopeNodes.ST, EDIEnvelopeNodes.SE);

                    foreach (string[] separateFile in separateSTFiles)
                    {
                        string subEDIfile = "";
                        foreach (string element in separateFile)
                        {
                            subEDIfile = subEDIfile + element + Convert.ToChar(segmentSeperator);
                        }

                        EDIWrapperBase subEdiWrapper = new EDIWrapperBase(subEDIfile
                                                                          , new char[] { segmentSeperator }//segement seperators
                                                                          , new char[] { dataSeperator });//Dataseperators

                        EDIFileType fileType = subEdiWrapper.FileType;
                        switch (fileType)
                        {
                        case EDIFileType.NMQR:
                            //inbox.DatasetID = (int)DTO.DataSet.Nomination_Quick_Response;
                            //pendingJob.DatasetId = inbox.DatasetID;
                            //_serviceInbox.Update(inbox);
                            //_serviceInbox.Save();
                            //_serviceTaskMgrJob.Update(pendingJob);
                            //_serviceTaskMgrJob.Save();
                            //isProcessed = ProcessNmqrData(subEDIfile, pendingJob.TransactionId, inbox.PipelineID, dataSeperator, segmentSeperator);
                            break;

                        case EDIFileType.OACY:
                            //inbox.DatasetID = (int)DTO.DataSet.Operational_Capacity;
                            //pendingJob.DatasetId = inbox.DatasetID;
                            //_serviceInbox.Update(inbox);
                            //_serviceInbox.Save();
                            //_serviceTaskMgrJob.Update(pendingJob);
                            //_serviceTaskMgrJob.Save();
                            //isProcessed = ProcessOacyData(subEDIfile, pendingJob.TransactionId, inbox.PipelineID, dataSeperator, segmentSeperator);
                            break;

                        case EDIFileType.UNSC:
                            //inbox.DatasetID = (int)DTO.DataSet.Unsubscribed_Capacity;
                            //pendingJob.DatasetId = inbox.DatasetID;
                            //_serviceInbox.Update(inbox);
                            //_serviceInbox.Save();
                            //_serviceTaskMgrJob.Update(pendingJob);
                            //_serviceTaskMgrJob.Save();
                            //isProcessed = ProcessUnscData(subEDIfile, pendingJob.TransactionId, inbox.PipelineID, dataSeperator, segmentSeperator);
                            break;

                        case EDIFileType.RURD:
                            //inbox.DatasetID = (int)DTO.DataSet.Response_to_Upload_of_Request_for_Download;
                            //pendingJob.DatasetId = inbox.DatasetID;
                            //_serviceInbox.Update(inbox);
                            //_serviceInbox.Save();
                            //_serviceTaskMgrJob.Update(pendingJob);
                            //_serviceTaskMgrJob.Save();
                            //isProcessed = ProcessRurdData(subEDIfile, pendingJob.TransactionId, inbox.PipelineID, dataSeperator, segmentSeperator);
                            break;

                        case EDIFileType.SWNT:
                            //inbox.DatasetID = (int)DTO.DataSet.System_Wide_Notices;
                            //pendingJob.DatasetId = inbox.DatasetID;
                            //_serviceInbox.Update(inbox);
                            //_serviceInbox.Save();
                            //_serviceTaskMgrJob.Update(pendingJob);
                            //_serviceTaskMgrJob.Save();
                            //isProcessed = ProcessSwntData(subEDIfile, pendingJob.TransactionId, inbox.PipelineID, dataSeperator, segmentSeperator);
                            break;

                        case EDIFileType.SQTS:
                            //inbox.DatasetID = (int)DTO.DataSet.Scheduled_Quantity;
                            //pendingJob.DatasetId = inbox.DatasetID;
                            //_serviceInbox.Update(inbox);
                            //_serviceInbox.Save();
                            //_serviceTaskMgrJob.Update(pendingJob);
                            //_serviceTaskMgrJob.Save();
                            //isProcessed = ProcessSqtsData(subEDIfile, pendingJob.TransactionId, inbox.PipelineID, dataSeperator, segmentSeperator);
                            break;

                        case EDIFileType.Ack:
                            //inbox.DatasetID = (int)DTO.DataSet.Ack_997;
                            //pendingJob.DatasetId = inbox.DatasetID;
                            //_serviceInbox.Update(inbox);
                            //_serviceInbox.Save();
                            //_serviceTaskMgrJob.Update(pendingJob);
                            //_serviceTaskMgrJob.Save();
                            break;

                        case EDIFileType.Unknown:
                            //inbox.DatasetID = 99;
                            //pendingJob.DatasetId = inbox.DatasetID;
                            //_serviceInbox.Update(inbox);
                            //_serviceInbox.Save();
                            //_serviceTaskMgrJob.Update(pendingJob);
                            //_serviceTaskMgrJob.Save();
                            break;
                        }
                    }



                    //EdiOptions options = new EdiOptions();
                    //options.ComponentSeparator = '>';
                    //options.ElementSeparator = dataSeperator;
                    //options.SegmentTerminator = segmentSeperator;

                    //EdiDocument ediDocument = new EdiDocument(ediFile,options);


                    //var startNodeIndexes = Enumerable.Range(0, ediDocument.Segments.Count())
                    // .Where(i => ediDocument.Segments[i].Id.StartsWith("ST"))
                    // .ToList();

                    //var endingNodeIdexes = Enumerable.Range(0, ediDocument.Segments.Count())
                    //    .Where(i => ediDocument.Segments[i].Id.StartsWith("SE"))
                    //    .ToList();

                    //if (startNodeIndexes.Count != endingNodeIdexes.Count)
                    //{
                    //    //ST and SE don't match
                    //}
                    //else
                    //{
                    //    List<OACYPerTransaction> list = new List<OACYPerTransaction>();
                    //    for (int i = 0; i < startNodeIndexes.Count; i++)
                    //    {
                    //        int countMatch = (endingNodeIdexes[i] - startNodeIndexes[i]) + 1;
                    //        var singleExtractedFile = ediDocument.Segments.ToList().GetRange(startNodeIndexes[i], countMatch);
                    //        var fileType = CheckEDIFileType(singleExtractedFile);
                    //        switch (fileType)
                    //        {
                    //            case EDIFileType.OACY:
                    //                ProcessOacyFiles(singleExtractedFile);
                    //                break;
                    //            case EDIFileType.UNSC:
                    //                ProcessUNSCFiles(singleExtractedFile);
                    //                break;
                    //            case EDIFileType.RURD:
                    //                ProcessRURDFiles(singleExtractedFile);
                    //                break;
                    //            case EDIFileType.SWNT:
                    //                ProcessSWNTFiles(singleExtractedFile);
                    //                break;
                    //            case EDIFileType.NMQR:
                    //                ProcessNMQRFiles(singleExtractedFile);
                    //                break;
                    //            case EDIFileType.SQTS:
                    //                ProcessSQTSFiles(singleExtractedFile);
                    //                break;
                    //            case EDIFileType.Ack:
                    //                break;
                    //            case EDIFileType.Unknown:
                    //                break;
                    //            default:
                    //                break;
                    //        }
                    //    }
                    //}
                }
            }
            catch (Exception ex)
            {
                ViewBag.Message = "File upload failed!!";
            }
            return(View());
        }