public static void ValidateInstanceFile(string btmFileName, string instanceFileName)
        {
            string outputDirName = Path.GetFileNameWithoutExtension(btmFileName);

            if (Directory.Exists(outputDirName) == false)
            {
                Directory.CreateDirectory(outputDirName);
            }

            string btmFileDirName = Path.GetDirectoryName(btmFileName);

            ConsoleExtensions.WriteInfo("Reading {0}", Path.GetFileName(btmFileName));

            BTMFileInfo btmFileInfo = BTMFileReader.ReadBTMFile(btmFileName);

            ConsoleExtensions.WriteInfo("\t{0}", btmFileInfo.SourceTree);
            SpecCertGenerator srcSpecCert = new SpecCertGenerator(btmFileInfo.SourceTree, btmFileDirName, outputDirName);

            srcSpecCert.Generate();
            IDocumentPlug srcDocumentPlug = srcSpecCert.DocumentPlug;

            ConsoleExtensions.WriteInfo("\t{0}", btmFileInfo.TargetTree);
            SpecCertGenerator trgSpecCert = new SpecCertGenerator(btmFileInfo.TargetTree, btmFileDirName, outputDirName);

            trgSpecCert.Generate();
            IDocumentPlug trgDocumentPlug = srcSpecCert.DocumentPlug;
        }
Ejemplo n.º 2
0
        public static IDocumentPlug CreateDocumentPlugFromXmlSchema(string schemaPath)
        {
            Stream        schemaStream = new FileStream(schemaPath, FileMode.Open, FileAccess.Read);
            IDocumentPlug plug         = XmlSchemaHelper.CreateDocumentPlug(schemaStream);

            return(plug);
        }
Ejemplo n.º 3
0
        public static EDIValidationResult ValidateEdi(string ediData, string ediFileName, string certFileFullName,
                                                      IDocumentPlug documentPlug, out IFatpipeDocument fatpipeDocument)
        {
            if (string.IsNullOrWhiteSpace(ediData))
            {
                throw new ArgumentNullException("ediData", "Edi file content cannot be empty");
            }

            if (string.IsNullOrWhiteSpace(certFileFullName))
            {
                throw new ArgumentNullException("certFileFullName", "certFileFullName cannot be empty");
            }

            Stopwatch sw = new Stopwatch();

            sw.Start();

            TradingPartnerSpecCertMetadata tradingPartnerSpecCertMetadata = new TradingPartnerSpecCertMetadata();

            // Purposely ignoring Initialize function return type (errors) since I don't expect errors here.
            // Spec cert is uploaded only after validation.
            tradingPartnerSpecCertMetadata.Initialize(certFileFullName, null, DateTime.MinValue);

            return(ValidateEdi(ediData, ediFileName, tradingPartnerSpecCertMetadata.SchemaFileName, tradingPartnerSpecCertMetadata.GetCertFileDisplayName(),
                               tradingPartnerSpecCertMetadata.Type, (SpecCertFileType)tradingPartnerSpecCertMetadata.FileType, documentPlug, out fatpipeDocument));
        }
Ejemplo n.º 4
0
        public static bool SerializeToStream(IDocumentPlug plug, Stream outputStream)
        {
            bool      result = true;
            XmlSchema schema = SerializeToXSD(plug);

            schema.Write(outputStream);
            return(result);
        }
Ejemplo n.º 5
0
        public XmlDocument initialize(string xmlContent, IDocumentPlug plug)
        {
            XmlDocument doc = new XmlDocument();

            doc.LoadXml(xmlContent);
            this.plug = plug;
            return(doc);
        }
        private static int AddPluglet(ExcelWorksheet ws, IDocumentPlug plug, ref int row, int level)
        {
            foreach (IPluglet child in plug.RootPluglet.Children)
            {
                AddPluglet(ws, child, ref row, level);
            }

            return(row);
        }
Ejemplo n.º 7
0
        /// <summary> Wrapper over EDIReader for GC validation feature (Based on spec cert). </summary>
        /// <param name="ediData">EDI text data (edi file content)</param>
        /// <param name="ediFileName">This is for record keeping only, not used by EDIReader</param>
        /// <param name="certFileFullName">Spec cert file (relative path)</param>
        /// <param name="dalManager">To retrieve schema</param>
        /// <returns></returns>
        public static EDIValidationResult ValidateEdi(string ediData, string ediFileName, string certFileFullName, IDalManager dalManager)
        {
            if (string.IsNullOrWhiteSpace(ediData))
            {
                throw new ArgumentNullException("ediData", "Edi file content cannot be empty");
            }

            if (string.IsNullOrWhiteSpace(certFileFullName))
            {
                throw new ArgumentNullException("certFileFullName", "certFileFullName cannot be empty");
            }

            if (dalManager == null)
            {
                throw new ArgumentNullException("dalManager", "dalManager cannot be null");
            }

            TradingPartnerSpecCertMetadata tradingPartnerSpecCertMetadata = new TradingPartnerSpecCertMetadata();

            // Purposely ignoring Initialize function return type (errors) since I don't expect errors here.
            // Spec cert is uploaded only after validation.
            tradingPartnerSpecCertMetadata.Initialize(certFileFullName, null, DateTime.MinValue);

            EDIValidationResult ediValidationResult = new EDIValidationResult()
            {
                FileName   = ediFileName,
                SchemaName = tradingPartnerSpecCertMetadata.SchemaFileName,
                SegmentValidationResults = new List <SegmentValidationResult>(),
                TransactionNumbers       = new List <string>(),
                DisplayName = tradingPartnerSpecCertMetadata.GetCertFileDisplayName(),
                Type        = tradingPartnerSpecCertMetadata.Type,
            };

            try
            {
                IDocumentPlug documentPlug = SchemaCache.GetDocumentPlug(tradingPartnerSpecCertMetadata, dalManager);

                IFatpipeDocument fatpipeDocument;
                ediValidationResult = ValidateEdi(ediData, ediFileName, certFileFullName, documentPlug, out fatpipeDocument);
            }
            catch (Exception ex)
            {
                ediValidationResult.SegmentValidationResults.Add(
                    new SegmentValidationResult()
                {
                    Type           = ResultType.Error,
                    SequenceNumber = -1,
                    Name           = "N/A",
                    Description    = "Internal error occurred",     //ex.Message,
                    StartIndex     = -1,
                    EndIndex       = -1,
                }
                    );
            }

            return(ediValidationResult);
        }
Ejemplo n.º 8
0
        public static IDocumentPlug CreateDocumentPlug(XmlSchemaCollection collection, string targetNamespace, string name)
        {
            IDocumentPlug plug = ParseSchema(new XmlQualifiedName(name, targetNamespace), collection[targetNamespace]);

            if (plug.Error.Count > 0)
            {
                throw new Exception("Error encountered in loading schema");
            }
            return(plug);
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Construct ITranformPlug based on source and target plug
        /// </summary>
        /// <param name="sourcePlug"></param>
        /// <param name="targetPlug"></param>
        /// <returns></returns>
        public static ITransformPlug CreateTransformPlug(IDocumentPlug sourcePlug, IDocumentPlug targetPlug)
        {
            //TODO: Implement this - currently hard coded path
            string path = Path.Combine(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"..\..\..\..\..\"),
                                       @"sources\test\GCommerceSuperSpec\GCommerceSuperSpecInbound850FromBuyer\InboundPO.btm");
            FileInfo btmFileInfo = new FileInfo(path);

            XElement btmRoot = XElement.Load(btmFileInfo.FullName);

            return(MapperHelper.CreateTransformPlugFromBTM(btmRoot, sourcePlug, targetPlug));
        }
Ejemplo n.º 10
0
        private IDocumentPlug CreateEDIDocumentPlug(int currentTransactionSetType, string[] ISARecordFields)
        {
            string        path, targetNamespace, name;
            IDocumentPlug documentPlug = null;

            switch (currentTransactionSetType)
            {
            case 850:
                name            = "X12_00401_850";
                targetNamespace = @"http://schemas.microsoft.com/BizTalk/EDI/X12/2006";

                if (SchemaCache.ContainsKey(name))
                {
                    documentPlug = SchemaCache[name];
                }
                break;

            case 277:
                name            = "X12_005010X214_277B3";
                targetNamespace = @"urn:x12:schemas:005:010:277B3:HealthCareInformationStatusNotification";

                if (SchemaCache.ContainsKey(name))
                {
                    documentPlug = SchemaCache[name];
                }
                break;

            default:
                throw new PlugDataModelException(string.Format("{0} schema not found", currentTransactionSetType));
            }

            if (documentPlug == null)
            {
                string    docType      = string.Format("{0}#{1}", targetNamespace, name);
                Stream    schemaStream = FPManager.RetrieveSchema(docType);
                XmlReader reader       = new XmlTextReader(schemaStream);

                //Stream schemaStream = FatPipeManager.RetrieveSchema(rootNodeName);
                //XmlReader reader = new XmlTextReader(schemaStream);

                XmlSchemaCollection schemaCollection = new XmlSchemaCollection();
                schemaCollection.Add(targetNamespace, reader);

                documentPlug = DocumentPlugFactory.CreateDocumentPlugFromXmlSchema(schemaCollection, targetNamespace, name);

                SchemaCache[name] = documentPlug;
            }

            return(documentPlug);
        }
Ejemplo n.º 11
0
        public static void PrintDocumentPlug(IDocumentPlug plug, string path)
        {
            StringBuilder builder = new StringBuilder(10 * 1000);

            PrintDocumentPlug(plug.RootPluglet, builder, 0);
            using (System.IO.StreamWriter sw = new System.IO.StreamWriter(path))
            {
                sw.Write("NodeType Mandatory Description TopLevelName Level2Name, Level3Name, Level4Name, Level5Name");
                sw.WriteLine();
                sw.WriteLine();
                sw.Write(builder.ToString());
            }

            Console.WriteLine("Written to " + path);
        }
Ejemplo n.º 12
0
        public static XmlSchema SerializeToXSD(IDocumentPlug plug)
        {
            IPluglet pluglet = plug.RootPluglet;

            if (pluglet == null)
            {
                return(null);
            }

            XmlSchema        schema      = new XmlSchema();
            XmlSchemaElement rootElement = SerializeToXmlSchemaElement(pluglet);

            schema.Items.Add(rootElement);
            return(schema);
        }
Ejemplo n.º 13
0
        private static void ValidateAndUploadSpecCert(MapDetail mapDetail, SpecCertGenerationResult result, StreamWriter logFile, bool uploadSpecCert, string specCertType)
        {
            LogInformation(logFile, string.Format("Generating document plug from spec cert {0}", Path.GetFileName(result.SpecCertPath)));

            using (StreamReader stream = new StreamReader(result.SpecCertPath))
            {
                try
                {
                    GCExcelToDocumentPlug excelToDocumentPlug;
                    switch (specCertType)
                    {
                    case "edi":
                        excelToDocumentPlug = new X12GCExcelToDocumentPlug();
                        break;

                    case "xml":
                        excelToDocumentPlug = new XmlGCExcelToDocumentPlug();
                        break;

                    case "flatfile":
                        excelToDocumentPlug = new FlatFileGCExcelToDocumentPlug();
                        break;

                    default:
                        throw new NotSupportedException(string.Format("Spec cert type {0} is not supported", specCertType));
                        break;
                    }

                    IDocumentPlug plug = excelToDocumentPlug.GenerateDocumentPlug(stream.BaseStream, mapDetail.OrgName, mapDetail.DocumentType, mapDetail.Direction, Maarg.Contracts.GCValidate.SpecCertFileType.X12);

                    // Serialize document plug for investigation
                    plug.SerializeToXml().Save(Path.ChangeExtension(result.SpecCertPath, "xml"));

                    if (uploadSpecCert)
                    {
                        // TODO: Add logic to upload to Azure blob
                        LogWarning(logFile, "Upload functionality will be added soon.");
                    }

                    LogInformation(logFile, string.Format("Document plug generated successfully"));
                }
                catch (Exception ex)
                {
                    result.Errors.Add(string.Format("Spec cert validation failed. Error: {0}", ex.ToString()));
                    LogError(logFile, string.Format("Spec cert validation failed. Error: {0}", ex.ToString()));
                }
            }
        }
Ejemplo n.º 14
0
        public static ITransformPlug CreateTransformPlugFromBTM(XElement btmRoot, IDocumentPlug sourcePlug, IDocumentPlug targetPlug)
        {
            string srcName, trgName;

            srcName = trgName = null;

            XElement e = GetFirstElement(btmRoot, "SrcTree");

            srcName = GetAttributeValue(e, "RootNode_Name");
            XElement reference      = GetFirstElement(e, "Reference");
            string   sourceLocation = null;

            if (reference != null)
            {
                sourceLocation = GetAttributeValue(reference, "Location");
            }

            e         = GetFirstElement(btmRoot, "TrgTree");
            trgName   = GetAttributeValue(e, "RootNode_Name");
            reference = GetFirstElement(e, "Reference");
            string targetLocation = null;

            if (reference != null)
            {
                targetLocation = GetAttributeValue(reference, "Location");
            }

            ITransformPlug plug = new TransformPlug(sourcePlug, targetPlug, null, sourceLocation, targetLocation);

            IEnumerable <XElement> pageList = btmRoot.Elements(XName.Get("Pages"));
            IEnumerable <XElement> pages    = null;

            foreach (XElement p in pageList)
            {
                pages = p.Elements(XName.Get("Page"));
                break;
            }

            foreach (XElement page in pages)
            {
                ITransformGroup group = ParsePage(page);
                MarkForIgnore(group);
                plug.Facets.Add(group);
            }

            //PrintTransformPlug(plug);
            return(plug);
        }
Ejemplo n.º 15
0
        public void SavePartnerSchema(IDocumentPlug documentPlug, string partnerIdentifier)
        {
            if (documentPlug == null)
            {
                throw new ArgumentNullException("documentPlug");
            }

            if (string.IsNullOrEmpty(partnerIdentifier))
            {
                throw new ArgumentException("To save schema, it must have a valid PartnerIdentifier.", "partnerIdentifier");
            }

            DALSchema dal = new DALSchema(this.storageAccount, this.container, partnerIdentifier);

            dal.Save(documentPlug as DocumentPlug);
        }
        private void GenerateDocumentPlug(string schema)
        {
            if (string.IsNullOrWhiteSpace(schema))
            {
                throw new ArgumentNullException("schema");
            }

            using (Stream schemaStream = GenerateStreamFromString(schema))
            {
                IDocumentPlug documentPlug = DocumentPlugFactory.CreateDocumentPlugFromXmlSchema(schemaStream);

                XElement schemaXml = documentPlug.SerializeToXml();
                schemaXml.Save(Path.Combine(OutputDirName, Path.ChangeExtension(TreeReference, "xml")));

                DocumentPlug = documentPlug;
            }
        }
Ejemplo n.º 17
0
        /// <summary>Wrapper over EDIReader for GC validation feature (based on Bts assembly file). </summary>
        /// <param name="ediData">EDI text data (edi file content)</param>
        /// <param name="ediFileName">This is for record keeping only, not used by EDIReader</param>
        /// <param name="certFileFullName">Spec cert file (relative path)</param>
        /// <param name="documentPlug">Document plug</param>
        /// <returns></returns>
        public static EDIValidationResult ValidateEdi(string ediData, string ediFileName, string schemaFileName, string certFileDisplayName, string type, SpecCertFileType fileType,
                                                      IDocumentPlug documentPlug)
        {
            if (string.IsNullOrWhiteSpace(ediData))
            {
                throw new ArgumentNullException("ediData", "Edi file content cannot be empty");
            }

            if (documentPlug == null)
            {
                throw new ArgumentNullException("documentPlug", "documentPlug cannot be null");
            }

            EDIValidationResult ediValidationResult = new EDIValidationResult()
            {
                FileName   = ediFileName,
                SchemaName = schemaFileName,
                SegmentValidationResults = new List <SegmentValidationResult>(),
                TransactionNumbers       = new List <string>(),
                DisplayName = certFileDisplayName,
                Type        = type,
            };

            try
            {
                IFatpipeDocument fatpipeDocument;
                ediValidationResult = ValidateEdi(ediData, ediFileName, schemaFileName, certFileDisplayName, type, fileType, documentPlug, out fatpipeDocument);
            }
            catch (Exception ex)
            {
                ediValidationResult.SegmentValidationResults.Add(
                    new SegmentValidationResult()
                {
                    Type           = ResultType.Error,
                    SequenceNumber = -1,
                    Name           = "N/A",
                    Description    = "Internal error occurred", //ex.Message,
                    StartIndex     = -1,
                    EndIndex       = -1,
                }
                    );
            }

            return(ediValidationResult);
        }
Ejemplo n.º 18
0
        private static Delimiters InitializeDelimiters(IDocumentPlug documentPlug)
        {
            int componentSeparator      = 0; // Flat file does not have have components
            int segmentDelimiterSuffix1 = 0;
            int segmentDelimiterSuffix2 = 0;

            if (documentPlug.SegmentDelimiters.Count > 1)
            {
                segmentDelimiterSuffix1 = documentPlug.SegmentDelimiters[1];
            }

            if (documentPlug.SegmentDelimiters.Count > 2)
            {
                segmentDelimiterSuffix2 = documentPlug.SegmentDelimiters[2];
            }

            return(new Delimiters(documentPlug.ElementDelimiters[0], componentSeparator,
                                  documentPlug.SegmentDelimiters[0], segmentDelimiterSuffix1, segmentDelimiterSuffix2));
        }
Ejemplo n.º 19
0
        /// <summary>
        /// This function should be used only for unit testing where we don't want to connect to Azure storage
        /// </summary>
        /// <param name="currentTransactionSetType"></param>
        /// <returns></returns>
        public static IDocumentPlug CreateEDIDocumentPlug(int currentTransactionSetType)
        {
            string path, targetNamespace, name;

            switch (currentTransactionSetType)
            {
            case 820:
                path            = "820-R1.xsd";
                targetNamespace = @"urn:x12:schemas:005010X306:820R1:HealthInsuranceExchangeRelatedPayments";
                name            = "X12_005010X306_820R1";
                break;

            case 850:
                path            = "GCommerce.EDI._00401._850.Schemas.Enriched_X12_00401_850";
                targetNamespace = @"http://schemas.microsoft.com/BizTalk/EDI/EDIFACT/2006/EnrichedMessageXML";
                name            = "X12EnrichedMessage";
                break;

            case 277:
                path            = "X12_005010X214_277B3.xsd";
                targetNamespace = @"urn:x12:schemas:005:010:277B3:HealthCareInformationStatusNotification";
                name            = "X12_005010X214_277B3";
                break;

            case 810:
                path            = "X12_00501_810.xsd";
                targetNamespace = @"http://schemas.microsoft.com/BizTalk/EDI/X12/2006";
                name            = "X12_00501_810";
                break;

            default:
                throw new Exception(string.Format("{0} schema not found", currentTransactionSetType));
            }

            IDocumentPlug schemaPlug = DocumentPlugFactory.CreateDocumentPlugFromXmlSchema(path, targetNamespace, name);

            XElement schemaXml = schemaPlug.SerializeToXml();

            schemaXml.Save(string.Format("Schema_{0}.xml", currentTransactionSetType));

            return(schemaPlug);
        }
        public static void GenerateExcelFromDocumentPlug(string outputFilePath, IDocumentPlug plug)
        {
            Console.WriteLine("Generating plug in " + outputFilePath);
            FileInfo newFile = new FileInfo(outputFilePath);

            if (newFile.Exists)
            {
                newFile.Delete();  // ensures we create a new workbook
                newFile = new FileInfo(outputFilePath);
            }

            //Create the workbook
            ExcelPackage pck = new ExcelPackage(newFile);

            GenerateWorksheetFromDocumentPlug(pck.Workbook, plug, 1, false);

            //Done! save the sheet
            pck.Save();

            Console.WriteLine("Plug written to " + outputFilePath);
        }
        public static XElement SerializeToXml(this IDocumentPlug documentPlug)
        {
            if (documentPlug == null)
            {
                return(null);
            }

            string name             = string.IsNullOrEmpty(documentPlug.Name) ? "undef" : documentPlug.Name;
            string elementDelimiter = GetDelimiterString(documentPlug.ElementDelimiters);
            string segmentDelimiter = GetDelimiterString(documentPlug.SegmentDelimiters);

            XElement plugletXml = new XElement("DocumentPlug"
                                               , new XAttribute("Name", name)
                                               , new XAttribute("ElementDelimiter", elementDelimiter)
                                               , new XAttribute("SegmentDelimiter", segmentDelimiter)
                                               );

            plugletXml.Add(documentPlug.RootPluglet.SerializeToXml());

            return(plugletXml);
        }
        /// <summary>
        /// Merge documentPlug with newDocumentPlug and return mergedDocumentPlug.
        /// canonicalDocumentPlug is used to decide children order during merge process
        /// </summary>
        /// <param name="documentPlug"></param>
        /// <param name="newDocumentPlug"></param>
        /// <param name="canonicalDocumentPlug"></param>
        /// <returns></returns>
        public static IDocumentPlug Merge(IDocumentPlug documentPlug, IDocumentPlug newDocumentPlug, IDocumentPlug canonicalDocumentPlug)
        {
            if (documentPlug == null)
            {
                throw new ArgumentNullException("documentPlug");
            }
            if (newDocumentPlug == null)
            {
                throw new ArgumentNullException("newDocumentPlug");
            }
            if (canonicalDocumentPlug == null)
            {
                throw new ArgumentNullException("canonicalDocumentPlug");
            }

            DocumentPlug mergedDocumentPlug = new DocumentPlug(null, documentPlug.BusinessDomain);

            mergedDocumentPlug.RootPluglet = documentPlug.RootPluglet.Merge(newDocumentPlug.RootPluglet, canonicalDocumentPlug.RootPluglet);

            return(mergedDocumentPlug);
        }
Ejemplo n.º 23
0
        public static IDocumentPlug GetDocumentPlug(TradingPartnerSpecCertMetadata tradingPartnerSpecCertMetadata, IDalManager dalManager)
        {
            IDocumentPlug documentPlug = gcSpecCertCache.GetObject(tradingPartnerSpecCertMetadata.SchemaFileName) as IDocumentPlug;

            if (documentPlug == null)
            {
                Stream schemaStream = dalManager.GetTradingPartnerSpecCert(tradingPartnerSpecCertMetadata);

                if (schemaStream == null)
                {
                    throw new GCEdiValidatorException(string.Format("{0} Schema not found.", tradingPartnerSpecCertMetadata.SchemaFileName));
                }

                GCExcelToDocumentPlug gcExcelToDocumentPlug = GCExcelToDocumentPlug.CreateInstance((SpecCertFileType)tradingPartnerSpecCertMetadata.FileType);

                documentPlug = gcExcelToDocumentPlug.GenerateDocumentPlug(schemaStream, tradingPartnerSpecCertMetadata.TradingPartnerName,
                                                                          tradingPartnerSpecCertMetadata.DocumentType, tradingPartnerSpecCertMetadata.Direction, (SpecCertFileType)tradingPartnerSpecCertMetadata.FileType);

                gcSpecCertCache.AddObject(tradingPartnerSpecCertMetadata.SchemaFileName, documentPlug);
            }

            return(documentPlug);
        }
        public static void GenerateWorksheetFromDocumentPlug(ExcelWorkbook workBook, IDocumentPlug plug, int startRow, bool usingTemplate)
        {
            ExcelWorksheet ws;

            if (usingTemplate)
            {
                ws = workBook.Worksheets[1];
            }
            else
            {
                //Add the Content sheet
                string name = plug.Name;
                ws = workBook.Worksheets.Add(name);
            }

            ws.View.ShowGridLines  = true;
            ws.OutLineSummaryRight = true;

            int row = startRow;

            int lastRow = AddPluglet(ws, plug, ref row, 0);

            ws.OutLineSummaryBelow = false;
        }
Ejemplo n.º 25
0
        public static IPluglet extractPluglet(IPluglet root, XmlNode node, IDocumentPlug plug)
        {
            string result = retreiveFullName(node);

            string[] nodeNames = result.Split('/');
            IPluglet current   = root;

            if (current.Name.Equals(nodeNames[0]))
            {
                for (int i = 1; i < nodeNames.Length; i++)
                {
                    current = findPlugletByName(current.Children, nodeNames[i]);
                    if (current == null)
                    {
                        plug.Error.Add("Could not find Element " + nodeNames[i]);
                    }
                }
            }
            else
            {
                plug.Error.Add("Root Element does not match");
            }
            return(current);
        }
Ejemplo n.º 26
0
        private static int ValidateInstanceFile(Parameters parameters)
        {
            string specCertFullPath = parameters.SpecCertName;
            string specCertFileName = Path.GetFileName(specCertFullPath);
            string instanceFilePath = parameters.InstanceFileName;
            string instanceFileName = Path.GetFileName(instanceFilePath);

            if (File.Exists(specCertFullPath) == false)
            {
                ConsoleExtensions.WriteError("{0} does not exist.", specCertFullPath);
                return(1);
            }

            if (Path.GetExtension(specCertFullPath) != ".xlsx")
            {
                ConsoleExtensions.WriteError("{0} is not a zip file.", specCertFullPath);
                return(1);
            }

            if (File.Exists(instanceFilePath) == false)
            {
                ConsoleExtensions.WriteError("{0} does not exist.", instanceFilePath);
                return(1);
            }

            string logFileName = Path.ChangeExtension(instanceFileName, "log");

            ConsoleExtensions.WriteInfo("Log file for this processing: {0}", logFileName);

            string instanceFileData = File.ReadAllText(instanceFilePath);

            using (StreamWriter logFile = new StreamWriter(logFileName))
            {
                try
                {
                    TradingPartnerSpecCertMetadata metadata = new TradingPartnerSpecCertMetadata();
                    metadata.Initialize(specCertFileName);

                    GCExcelToDocumentPlug excelToDocumentPlug;
                    switch ((SpecCertFileType)metadata.FileType)
                    {
                    case SpecCertFileType.X12:
                        excelToDocumentPlug = new X12GCExcelToDocumentPlug();
                        break;

                    case SpecCertFileType.Xml:
                        excelToDocumentPlug = new XmlGCExcelToDocumentPlug();
                        break;

                    case SpecCertFileType.FlatFile:
                        excelToDocumentPlug = new FlatFileGCExcelToDocumentPlug();
                        break;

                    default:
                        throw new NotSupportedException(string.Format("Spec cert type {0} is not supported", (SpecCertFileType)metadata.FileType));
                        break;
                    }

                    LogInformation(logFile, string.Format("Generating document plug from spec cert {0}", specCertFileName));

                    TradingPartnerSpecCertMetadata specCertMetadata = new TradingPartnerSpecCertMetadata();
                    specCertMetadata.Initialize(specCertFileName);

                    IDocumentPlug documentPlug = null;
                    using (StreamReader stream = new StreamReader(specCertFullPath))
                        documentPlug = excelToDocumentPlug.GenerateDocumentPlug(
                            stream.BaseStream, specCertMetadata.TradingPartnerName, specCertMetadata.DocumentType,
                            specCertMetadata.Direction, Maarg.Contracts.GCValidate.SpecCertFileType.X12);

                    if (documentPlug == null)
                    {
                        LogError(logFile, "Document plug generation failed");
                        return(-1);
                    }
                    else
                    {
                        LogInformation(logFile, "Document plug generated successfully");
                    }
                    LogInformation(logFile, "Validating instance file");

                    IFatpipeDocument    fatpipeDocument;
                    EDIValidationResult result = EdiValidator.ValidateEdi(instanceFileData, instanceFileName, specCertFileName, documentPlug, out fatpipeDocument);

                    LogValidationResult(result, logFile);
                }
                catch (Exception ex)
                {
                    LogError(logFile, string.Format("Error encountered during validating {0} file. Error: {1}", instanceFileName, ex.ToString()));
                }
            }

            return(0);
        }
Ejemplo n.º 27
0
        /// <summary>
        /// Create plug based on the fatpipeDocument (instance tree).
        /// Generated plug will only have pluglets referred in instance tree.
        /// Generated plug will have repeated pluglets in case of loops.
        /// </summary>
        /// <param name="fatpipeDocument"></param>
        /// <param name="existingDocumentPluglet"></param>
        /// <returns></returns>
        public static IDocumentPlug GenerateDocumentPlug(this IFatpipeDocument fatpipeDocument, IDocumentPlug existingDocumentPluglet)
        {
            if (fatpipeDocument == null)
            {
                throw new ArgumentNullException("fatpipeDocument", "Cannot generate document plug for null IFatpipeDocument");
            }

            DocumentPlug documentPlug = new DocumentPlug(null, fatpipeDocument.DocumentPlug.BusinessDomain);

            documentPlug.RootPluglet = fatpipeDocument.RootFragment.GeneratePluglet();

            documentPlug.RootPluglet = documentPlug.RootPluglet.RemoveLoops(fatpipeDocument.RootFragment.Pluglet);

            if (existingDocumentPluglet != null)
            {
                documentPlug.RootPluglet = documentPlug.RootPluglet.Merge(existingDocumentPluglet.RootPluglet, fatpipeDocument.RootFragment.Pluglet);
            }

            documentPlug.RootPluglet.SetParent(null);

            return(documentPlug);
        }
Ejemplo n.º 28
0
        /// <summary>
        /// Initialize EDIReader with a stream. Verify that stream contains EDI document (ISA segment).
        /// </summary>
        /// <param name="stream"></param>
        /// <param name="fatpipeManager"></param>
        /// <param name="documentPlug"></param>
        /// <returns>true if EDI document otherwise false</returns>
        public bool Initialize(Stream stream, IFatpipeManager fatpipeManager, IDocumentPlug documentPlug)
        {
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }

            Stopwatch sw = new Stopwatch();

            sw.Start();

            Errors = new InterchangeErrors();

            FPManager    = fatpipeManager;
            DocumentPlug = documentPlug;

            long orgStartPos = stream.Position;

            Delimiters           delimiters;
            InterchangeTokenizer tokenizer = new InterchangeTokenizer(stream);
            bool isValidEDIDocument        = tokenizer.IsX12Interchange(out delimiters);

            FunctionalGroupNumber    = TransactionSetNumber = SegmentNumberFromStart = SegmentNumber = 0;
            ValidTransactionSetCount = InvalidTransactionSetCount = 0;
            PrevTransactionSetType   = 0;

            DocumentReader = null;
            if (isValidEDIDocument == true)
            {
                EDIDelimiters = delimiters;
                //TODO: Review following logic
                //Read ISA field till component separator - Do not include component separator
                ISASegment = tokenizer.ISARecord.Substring(0, tokenizer.ISARecord.IndexOf((char)EDIDelimiters.ComponentSeperator) + 1);

                //TODO: Suraj: confirm this special case - last value as data element separator
                ISARecordFields = ISASegment.Split((char)EDIDelimiters.FieldSeperator);

                SegmentNumberFromStart = 1;

                stream.Position     = orgStartPos;
                this.DocumentReader = new StreamReader(stream, Encoding.UTF8);
                //TODO: Why is seek required here?
                this.CurrentSegmentStartPos = 0;
                this.CurrentSegmentEndPos   = 0;
                //this.CurrentSegmentEndPos = this.CurrentSegmentStartPos + tokenizer.ISARecordLen - 1;
                //this.DocumentReader.BaseStream.Seek(this.CurrentSegmentEndPos + basePosition, SeekOrigin.Begin);
                FatpipeDocumentInst = new FatpipeDocument();
                FatpipeDocumentInst.BeautifiedOriginalPayloadStartHeader = string.Empty;
                FatpipeDocumentInst.BeautifiedOriginalPayloadBody        = string.Empty;
                FatpipeDocumentInst.BeautifiedOriginalPayloadEndHeader   = string.Empty;

                SegmentDelimiter          = ((char)EDIDelimiters.SegmentDelimiter).ToString();
                FormattedSegmentDelimiter = SegmentDelimiter;

                bool crLFPresent = delimiters.SegmentDelimiter == Delimiters.CarriageReturn || delimiters.SegmentDelimiterSuffix1 == Delimiters.CarriageReturn;
                if (!crLFPresent)
                {
                    FormattedSegmentDelimiter = string.Format("{0}{1}", FormattedSegmentDelimiter, Environment.NewLine);
                }

                Logger.Info("EDIReader.Initialize", "EDIDelimiters: SegmentDelimiter={0}, FieldSeperator={1}, ComponentSeperator={2}", (char)EDIDelimiters.SegmentDelimiter, (char)EDIDelimiters.FieldSeperator, (char)EDIDelimiters.ComponentSeperator);
            }
            else //invalid document code path
            {
                this.CurrentSegmentStartPos = orgStartPos;
                this.CurrentSegmentEndPos   = orgStartPos + 3;
                Logger.Error("EDIReader.Initialize", EventId.EDIReaderInvalidDocument, "EDI document is not valid. Error: {0}", tokenizer.Error);

                Errors.AddGenericError("ISA", SchemaErrorCode.SchemaCode100EInvalidDocType, tokenizer.Error, SegmentNumber, this.CurrentSegmentStartPos, this.CurrentSegmentEndPos);

                StringBuilder sb         = new StringBuilder();
                int           errorIndex = 1;
                Errors.IsaIeaErrorList.WriteError(sb, ref errorIndex);

                ISASegment = string.Empty;
            }

            LastState = EDIState.ISA;

            sw.Stop();
            Logger.Debug("EDIReader.Initialize", "Elapsed time {0} ms", sw.ElapsedMilliseconds);

            return(isValidEDIDocument);
        }
Ejemplo n.º 29
0
        public static EDIValidationResult ValidateEdi(string ediData, string ediFileName, string schemaFileName, string certFileDisplayName, string type, SpecCertFileType fileType,
                                                      IDocumentPlug documentPlug, out IFatpipeDocument fatpipeDocument)
        {
            Stopwatch sw = new Stopwatch();

            sw.Start();

            EDIValidationResult ediValidationResult = new EDIValidationResult()
            {
                FileName   = ediFileName,
                SchemaName = schemaFileName,
                SegmentValidationResults = new List <SegmentValidationResult>(),
                TransactionNumbers       = new List <string>(),
                DisplayName = certFileDisplayName,
                Type        = type,
            };

            fatpipeDocument = null;

            try
            {
                ediValidationResult.SegmentValidationResults.Clear();
                string endHeader = string.Empty;

                switch (fileType)
                {
                case SpecCertFileType.X12:
                    EDIReader ediReader = new EDIReader();
                    if (ediReader.Initialize(new MemoryStream(Encoding.UTF8.GetBytes(ediData)), null, documentPlug) == true)
                    {
                        try
                        {
                            IFatpipeDocument currentFatpipeDocument = null;
                            while ((currentFatpipeDocument = ediReader.GetNextTransactionSet()) != null)
                            {
                                if (string.IsNullOrWhiteSpace(ediValidationResult.BeautifiedOriginalPayload))
                                {
                                    ediValidationResult.BeautifiedOriginalPayload = currentFatpipeDocument.BeautifiedOriginalPayloadStartHeader;
                                    endHeader = currentFatpipeDocument.BeautifiedOriginalPayloadEndHeader;
                                }

                                ediValidationResult.BeautifiedOriginalPayload += currentFatpipeDocument.BeautifiedOriginalPayloadBody;
                                ediValidationResult.TransactionNumbers.Add(currentFatpipeDocument.TransactionNumber);

                                ediValidationResult.SegmentValidationResults.AddRange(ediReader.Errors.GetSegmentValidationResults());

                                fatpipeDocument = currentFatpipeDocument;
                            }

                            ediValidationResult.BeautifiedOriginalPayload += endHeader;
                        }
                        catch (EDIReaderException ediReaderException)
                        {
                            // Add whatever errors we accumulated
                            ediValidationResult.SegmentValidationResults.AddRange(ediReader.Errors.GetSegmentValidationResults());

                            ediValidationResult.SegmentValidationResults.Add(
                                new SegmentValidationResult()
                            {
                                Type           = ResultType.Error,
                                SequenceNumber = -1,
                                Name           = "N/A",
                                Description    = ediReaderException.Message,
                                StartIndex     = -1,
                                EndIndex       = -1,
                            });
                        }
                    }

                    //ediValidationResult.SegmentValidationResults.AddRange(ediReader.Errors.GetSegmentValidationResults());
                    break;

                case SpecCertFileType.FlatFile:
                    FlatFileReader flatFileReader = new FlatFileReader();
                    fatpipeDocument = flatFileReader.ReadFile(new MemoryStream(Encoding.UTF8.GetBytes(ediData)), documentPlug);

                    ediValidationResult.BeautifiedOriginalPayload = fatpipeDocument.BeautifiedOriginalPayloadBody;
                    ediValidationResult.SegmentValidationResults.AddRange(flatFileReader.Errors.GetSegmentValidationResults());
                    break;

                case SpecCertFileType.Xml:
                    XmlFileReader xmlFileReader = new XmlFileReader();
                    fatpipeDocument = xmlFileReader.ReadFile(new MemoryStream(Encoding.UTF8.GetBytes(ediData)), documentPlug);

                    ediValidationResult.BeautifiedOriginalPayload = fatpipeDocument.BeautifiedOriginalPayloadBody;
                    ediValidationResult.SegmentValidationResults.AddRange(xmlFileReader.Errors.GetSegmentValidationResults());
                    break;

                default:
                    ediValidationResult.SegmentValidationResults.Add(
                        new SegmentValidationResult()
                    {
                        Type           = ResultType.Error,
                        SequenceNumber = -1,
                        Name           = "N/A",
                        Description    = "Invalid cert file type (only EDI and FaltFile is supported)",         //ex.Message,
                        StartIndex     = -1,
                        EndIndex       = -1,
                    });
                    break;
                }
            }
            catch (Exception ex)
            {
                ediValidationResult.SegmentValidationResults.Add(
                    new SegmentValidationResult()
                {
                    Type           = ResultType.Error,
                    SequenceNumber = -1,
                    Name           = "N/A",
                    //Description = "Internal error occurred",//ex.Message,
                    Description = "Internal error occurred. " + ex.ToString(),
                    StartIndex  = -1,
                    EndIndex    = -1,
                });
            }

            sw.Stop();

            ediValidationResult.ExecutionTime = sw.Elapsed;

            return(ediValidationResult);
        }
Ejemplo n.º 30
0
        /// <summary>
        /// Read xml file and construct IFatpipeDocument.
        /// Xml file reader will traverse Xml files and for each element
        /// match it with current pluglet. If match fails then it tries to
        /// find matching pluglet (similar to X12).
        /// </summary>
        /// <returns></returns>
        public IFatpipeDocument ReadFile(Stream xmlFileStream, IDocumentPlug documentPlug)
        {
            if (xmlFileStream == null)
            {
                throw new ArgumentNullException("xmlFileStream", "Xml file stream cannot be null");
            }

            if (documentPlug == null)
            {
                throw new ArgumentNullException("documentPlug", "Document plug cannot be null");
            }

            string location = "XmlFileReader.ReadFile";

            Logger.Debug(location, "Start");

            BeautifiedOriginalPayload = string.Empty;
            CurrentElementNumber      = 0;
            TotalPayloadLength        = 0;
            CurrentLinePayloadStart   = 0;
            CurrentLinePayloadEnd     = 0;
            CurrentLevel = 0;
            Stopwatch sw = new Stopwatch();

            sw.Start();

            errors = new InterchangeErrors();

            // Since xml file doesn't have concept of ST/SE, ans we want to use InterchangeErrors for reporting purpose
            // create dummy transaction set details
            Errors.AddTransactionSetDetails(1, "", "", true);

            IPluglet currentPluglet = documentPlug.RootPluglet;

            currentPluglet.ResetCurrentOccurances();
            currentPluglet.InitializeStartSegmentList();

            FatpipeDocumentInst = new FatpipeDocument();
            FatpipeDocumentInst.DocumentPlug = documentPlug;
            FatpipeDocumentInst.RootFragment = currentPluglet.ConstructDocumentFragment(null, null);

            IDocumentFragment currentDocumentFragment = FatpipeDocumentInst.RootFragment;
            IDocumentFragment newDocumentFragment     = null;

            bool isLeafNode = false;

            try
            {
                XmlTextReader xmlReader = new XmlTextReader(xmlFileStream);

                // If some element doesn't match document plutlet then stop
                // TODO: Should we try to match other elements? If yes, which pluglet to start with?
                // Also we need to ignore this entire element
                bool stopProcessing = false;

                while (xmlReader.Read())
                {
                    switch (xmlReader.NodeType)
                    {
                    case XmlNodeType.Element:
                        isLeafNode = false;
                        AddStartElementToPayload(xmlReader.Name, xmlReader.IsEmptyElement);

                        if (xmlReader.IsEmptyElement)
                        {
                            break;
                        }

                        if (stopProcessing == false)
                        {
                            currentDocumentFragment = ConstructNewDocumentFragment(xmlReader.Name, currentPluglet, currentDocumentFragment);
                        }

                        // If some element doesn't match document plutlet then stop
                        // TODO: Should we try to match other elements? If yes, which pluglet to start with?
                        // Also we need to ignore this entire element
                        if (currentDocumentFragment == null)
                        {
                            stopProcessing = true;
                        }
                        else
                        {
                            currentPluglet = currentDocumentFragment.Pluglet;
                        }

                        CurrentLevel++;
                        CurrentElementNumber++;
                        break;

                    case XmlNodeType.Text:
                        isLeafNode = true;
                        AddValueToPayload(xmlReader.Value);
                        if (stopProcessing == false)
                        {
                            currentDocumentFragment.Value = xmlReader.Value;
                        }
                        // Assumption: Leaf nodes are on same line and non-leaf nodes are 1-per line (separate line for start and end element)
                        // If this assumption is wrong then we can construct the xml in string format to match fulfill above assumption
                        // and then Ux can use this string version of xml to highlight the errors.
                        CurrentElementNumber--;     // Decrement since leaf level elements are one line, so we need to increment element on endElement only.
                        break;

                    case XmlNodeType.EndElement:
                        CurrentLevel--;
                        AddEndElementToPayload(xmlReader.Name, isLeafNode);
                        if (stopProcessing == false)
                        {
                            // Check if all mandatory segments were present
                            CheckMissingMandatoryElements(currentPluglet, currentDocumentFragment);
                            currentDocumentFragment = currentDocumentFragment.Parent;
                            currentPluglet          = currentPluglet.Parent;
                        }

                        CurrentElementNumber++;
                        isLeafNode = false;
                        break;

                    default:
                        break;
                    }
                }
            }
            catch (XmlException xmlException)
            {
                // TODO: Pass start and end postition
                Errors.AddGenericError(currentPluglet == null ? "N/A" : currentPluglet.Name, X12ErrorCode.UnexpectedSegmentCode,
                                       string.Format("Error parsing XML document: {0}", xmlException.Message),
                                       CurrentElementNumber / 2, CurrentLinePayloadStart + TotalPayloadLength, CurrentLinePayloadEnd + TotalPayloadLength);
            }
            catch (Exception exception)
            {
                // TODO: Pass start and end postition (for all Errors.Add* calls) in this file.
                Errors.AddGenericError(currentPluglet == null ? "N/A" : currentPluglet.Name, X12ErrorCode.UnexpectedSegmentCode,
                                       "Internal error occurred, please contact Maarg",
                                       CurrentElementNumber / 2, CurrentLinePayloadStart + TotalPayloadLength, CurrentLinePayloadEnd + TotalPayloadLength);
                Logger.Error(location, EventId.XmlReaderUnhandledException, "Error occured during xml file processing: {0}", exception.ToString());
            }

            FatpipeDocumentInst.BeautifiedOriginalPayloadBody = BeautifiedOriginalPayload;

            sw.Stop();
            Logger.Debug(location, "Stop - Elapsed time {0} ms", sw.ElapsedMilliseconds);

            return(FatpipeDocumentInst);
        }