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)); }
public RuleSegments(IFatpipeDocument fatpipeDocument, string fileName) { FileName = fileName; FatpipeDocument = fatpipeDocument; UsedSegments = new Dictionary <string, List <IDocumentFragment> >(); CurrentSegments = new Dictionary <string, IDocumentFragment>(); UnusedSegments = new Dictionary <string, List <IDocumentFragment> >(); }
/// <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); }
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); }
public static IFatpipeDocument Transform(IFatpipeDocument sourceDocument, ITransformPlug transformPlug) { if (sourceDocument == null) { throw new ArgumentNullException("sourceDocument"); } Stopwatch sw = new Stopwatch(); sw.Start(); FatpipeDocument targetDocument = new FatpipeDocument(); targetDocument.RootFragment = transformPlug.TargetDocument.RootPluglet.ConstructDocumentFragment(null, null);; //TODO: Handle loops foreach (ITransformGroup transformGroup in transformPlug.Facets) { Logger.Debug("DocumentTransformer.Transform", "Processing {0} tranformGroup", transformGroup.Name); foreach (ITransformLink transformLink in transformGroup.Links) { Logger.Debug("DocumentTransformer.Transform", "Processing Link#-{0}: {1} [{2}] => {3} [{4}]" , transformLink.Name , transformLink.Source.ReferenceType, transformLink.Source.Name , transformLink.Target.ReferenceType, transformLink.Target.Name); //TODO: Handle all kind of transformation if (transformLink.Source.ReferenceType == ReferenceType.Document && transformLink.Target.ReferenceType == ReferenceType.Document) { // Traverse source path IDocumentFragment sourceFragment = sourceDocument.RootFragment.MoveTo(transformLink.Source.Name); if (sourceFragment != null) { IPluglet targetPluglet = targetDocument.RootFragment.Pluglet.MoveTo(transformLink.Target.Name); if (targetPluglet != null) { Logger.Debug("DocumentTransformer.Transform", "Source = {0}, Target = {1}", sourceFragment.Name, targetPluglet.Tag); Dictionary <string, string> attributes = new Dictionary <string, string>(); foreach (IPluglet attr in targetPluglet.Attributes) { string attributeName = attr.Name; attributeName.Remove(0, 1); attributeName.Remove(attributeName.Length, 1); attributes.Add(attributeName, attributeName); } bool isAttribute = false; string name = transformLink.Target.Name.Substring( transformLink.Target.Name.LastIndexOf(targetPluglet.PathSeperator) + targetPluglet.PathSeperator.Length); // Check if this transformation point to attribute or leaf node if (targetPluglet.Attributes != null && attributes.ContainsKey(name)) { isAttribute = true; } if (isAttribute == false) { IDocumentFragment newFragment = targetPluglet.ConstructDocumentFragment(null, sourceFragment.Value); ((DocumentFragment)targetDocument.RootFragment).AddDocumentFragment(newFragment); } else { ((DocumentFragment)targetDocument.RootFragment).AddDocumentFragment(transformLink.Target.Name, sourceFragment.Value); } } else { string error = string.Format("Link#-{0}: {1} path not found in target tree", transformLink.Name, transformLink.Target.Name); if (targetDocument.Errors == null) { targetDocument.Errors = new List <string>(); } targetDocument.Errors.Add(error); Logger.Error("DocumentTransformer.Transform", EventId.DocTransformerNoMapping, error); } } } else { Logger.Debug("DocumentTransformer.Transform", "Ignoring Link#-{0}", transformLink.Name); } } //TODO: Handle transformGroup.Formulas } sw.Stop(); Logger.Debug("DocumentTransformer.Transform", "Stop. Elapsed time {0} ms", sw.ElapsedMilliseconds); return(targetDocument); }