Example #1
0
 public EdiMapReader(MapLoop map, EdiTrans currentTrans)
 {
     _trans = currentTrans;
     _currentLoopInstance            = currentTrans;
     _currentLoopInstance.Definition = map;
     _currentLoopDef = map;
 }
Example #2
0
 private void FindParentLoopContext(MapLoop parentLoop)
 {
     while (((MapLoop)_currentLoopInstance.Definition) != parentLoop && _currentLoopInstance.Parent != null)
     {
         _currentLoopInstance = _currentLoopInstance.Parent;
     }
 }
Example #3
0
        //TODO:Refactor to make same code base with EdiMapReader.ProcessRawSegment
        protected void SetMapContext(string name)
        {
            List <AllowedEntitity> allowedEntities = GetNextAllowedEntities(CurrentLoopDef);

            if (allowedEntities.All(e => e.Entity.Name != name))
            {
                string          expected = string.Join(", ", allowedEntities.Select(e => e.Entity.Name).ToList());
                string          msgPart  = allowedEntities.Count > 1 ? "Expected one of" : "Expected";
                ValidationError err      = new ValidationError()
                {
                    Message = $"Unexpected Segment. {msgPart} {expected}. Found {name}."
                };
                Trans.ValidationErrors.Add(err);
                return;
            }

            AllowedEntitity ae = allowedEntities.FirstOrDefault(e => e.Entity.Name == name);

            if (ae?.Entity is MapSegment)
            {
                ae.Entity.OccuredTimes++;

                CurrentLoopDef            = ae.LoopContext;
                CurrentLoopDef.CurrentPos = CurrentLoopDef.Content.IndexOf(ae.Entity);

                while (((MapLoop)CurrentLoopInstance.Definition) != ae.LoopContext && CurrentLoopInstance.Parent != null)
                {
                    CurrentLoopInstance = CurrentLoopInstance.Parent;
                }

                CurrentEntityDef = ae.Entity;
            }
            else if (ae?.Entity is MapLoop)
            {
                //find loop definition in map and reset counters
                ae.LoopContext.CurrentPos = ae.LoopContext.Content.IndexOf(ae.Entity);
                CurrentLoopDef            = (MapLoop)ae.Entity;
                CurrentLoopDef.CurrentPos = 0;
                CurrentLoopDef.OccuredTimes++;
                CurrentLoopDef.Content.ForEach(c => c.OccuredTimes = 0);

                ////create new loop instance and add to transaction
                while (((MapLoop)CurrentLoopInstance.Definition) != ae.LoopContext && CurrentLoopInstance.Parent != null)
                {
                    CurrentLoopInstance = CurrentLoopInstance.Parent;
                }

                var newLoop = new EdiLoop(ae.Entity, CurrentLoopInstance);
                CurrentLoopInstance.Content.Add(newLoop);
                CurrentLoopInstance = newLoop;
            }
        }
Example #4
0
        private void FindParentHlLoopContext(int parentHlLevel)
        {
            while (!(_currentLoopInstance is EdiHlLoop))
            {
                _currentLoopInstance = _currentLoopInstance.Parent;
            }

            if (((EdiHlLoop)_currentLoopInstance).HL01_HierarchicalIdNumber != parentHlLevel)
            {
                _currentLoopInstance = _currentLoopInstance.Parent;
                FindParentHlLoopContext(parentHlLevel);
            }
        }
Example #5
0
        public virtual EdiTrans ReadToEnd(string rawData)
        {
            Trans = new EdiTrans(CurrentLoopDef);

            CurrentLoopInstance            = Trans;
            CurrentLoopInstance.Definition = CurrentLoopDef;

            EdiIntermediateEntity intermediateTree = ReadIntermediateTree(rawData);

            ParseTree(intermediateTree);

            return(Trans);
        }
Example #6
0
        public void AckBuilder_AcceptAll_WithAk2()
        {
            EdiBatch b;

            using (Stream s = GetType().Assembly.GetManifestResourceStream("EdiEngine.Tests.TestData.AckTest.edi"))
            {
                EdiDataReader r = new EdiDataReader();
                b = r.FromStream(s);
            }

            AckBuilderSettings ackSettings = new AckBuilderSettings(AckValidationErrorBehavour.AcceptAll, true, 100, 200);
            var ack = new AckBuilder(ackSettings);

            EdiBatch ackBatch = ack.GetnerateAcknowledgment(b);
            //string data = ack.WriteToString(b);

            EdiTrans ack1 = ackBatch.Interchanges[0].Groups[0].Transactions[0];
            EdiTrans ack2 = ackBatch.Interchanges[0].Groups[1].Transactions[0];

            EdiSegment ak1       = (EdiSegment)ack1.Content.FirstOrDefault(l => l.Definition.GetType() == typeof(AK1));
            EdiLoop    loopAk2_1 = (EdiLoop)ack1.Content.FirstOrDefault(l => l.Definition.GetType() == typeof(M_997.L_AK2));
            EdiLoop    loopAk2_2 = (EdiLoop)ack1.Content.LastOrDefault(l => l.Definition.GetType() == typeof(M_997.L_AK2));
            EdiSegment ak5_1     = (EdiSegment)loopAk2_1.Content.FirstOrDefault(l => l.Definition.GetType() == typeof(AK5));
            EdiSegment ak5_2     = (EdiSegment)loopAk2_2.Content.FirstOrDefault(l => l.Definition.GetType() == typeof(AK5));
            EdiSegment ak9       = (EdiSegment)ack1.Content.FirstOrDefault(l => l.Definition.GetType() == typeof(AK9));

            Assert.IsNotNull(ak1);
            Assert.IsNotNull(loopAk2_1);
            Assert.AreEqual("A", ak5_1.Content[0].Val);
            Assert.AreEqual("A", ak5_2.Content[0].Val);
            Assert.IsNotNull(loopAk2_2);
            Assert.IsNotNull(ak9);
            Assert.AreEqual("A", ak9.Content[0].Val);
            Assert.AreEqual("2", ak9.Content[1].Val);
            Assert.AreEqual("2", ak9.Content[2].Val);
            Assert.AreEqual("2", ak9.Content[3].Val);

            ak1       = (EdiSegment)ack2.Content.FirstOrDefault(l => l.Definition.GetType() == typeof(AK1));
            loopAk2_1 = (EdiLoop)ack1.Content.FirstOrDefault(l => l.Definition.GetType() == typeof(M_997.L_AK2));
            ak5_1     = (EdiSegment)loopAk2_1.Content.FirstOrDefault(l => l.Definition.GetType() == typeof(AK5));
            ak9       = (EdiSegment)ack2.Content.FirstOrDefault(l => l.Definition.GetType() == typeof(AK9));

            Assert.IsNotNull(ak1);
            Assert.IsNotNull(loopAk2_1);
            Assert.AreEqual("A", ak5_1.Content[0].Val);
            Assert.IsNotNull(ak9);
            Assert.AreEqual("A", ak9.Content[0].Val);
            Assert.AreEqual("1", ak9.Content[1].Val);
            Assert.AreEqual("1", ak9.Content[2].Val);
            Assert.AreEqual("1", ak9.Content[3].Val);
        }
Example #7
0
        public void ProcessRawSegment(string name, string[] content, int rowPos)
        {
            List <AllowedEntitity> allowedEntities = GetNextAllowedEntities(_currentLoopDef);

            if (allowedEntities.All(e => e.Entity.EdiName != name))
            {
                string          expected = string.Join(", ", allowedEntities.Select(e => e.Entity.Name).ToList());
                string          msgPart  = allowedEntities.Count > 1 ? "Expected one of" : "Expected";
                ValidationError err      = new ValidationError()
                {
                    SegmentPos  = rowPos,
                    SegmentName = name,
                    Message     = $"Unexpected Segment. {msgPart} {expected}. Found {name}."
                };
                _trans.ValidationErrors.Add(err);
                return;
            }

            AllowedEntitity ae = allowedEntities.FirstOrDefault(e => e.Entity.EdiName == name);

            if (ae?.Entity is MapSegment)
            {
                ae.Entity.OccuredTimes++;

                _currentLoopDef            = ae.LoopContext;
                _currentLoopDef.CurrentPos = _currentLoopDef.Content.IndexOf(ae.Entity);

                while (((MapLoop)_currentLoopInstance.Definition) != ae.LoopContext && _currentLoopInstance.Parent != null)
                {
                    _currentLoopInstance = _currentLoopInstance.Parent;
                }
                _currentLoopInstance.Content.Add(ProcessSegment(ae.Entity, content, rowPos, _trans));
            }
            else if (ae?.Entity is MapLoop)
            {
                //find loop definition in map and reset counters
                ae.LoopContext.CurrentPos  = ae.LoopContext.Content.IndexOf(ae.Entity);
                _currentLoopDef            = (MapLoop)ae.Entity;
                _currentLoopDef.CurrentPos = 0;
                _currentLoopDef.OccuredTimes++;
                _currentLoopDef.Content.ForEach(c => c.OccuredTimes = 0);


                EdiLoop newLoop;
                if (name == "HL")
                {
                    int hl01;
                    int hl02;
                    int.TryParse(content[1], out hl01);
                    bool res02 = int.TryParse(content[2], out hl02);

                    if (hl01 > 1 && res02)
                    {
                        FindParentHlLoopContext(hl02);
                    }
                    else
                    {
                        FindParentLoopContext(ae.LoopContext);
                    }


                    newLoop = new EdiHlLoop(ae.Entity, _currentLoopInstance, hl01, (res02 ? hl02 : (int?)null));
                }
                else
                {
                    FindParentLoopContext(ae.LoopContext);
                    newLoop = new EdiLoop(ae.Entity, _currentLoopInstance);
                }

                _currentLoopInstance.Content.Add(newLoop);
                _currentLoopInstance = newLoop;

                ProcessRawSegment(name, content, rowPos);
            }
        }
Example #8
0
        public void EdiReader_ParseGenericEdi850()
        {
            using (Stream s = GetType().Assembly.GetManifestResourceStream("EdiEngine.Tests.TestData.850.OK.edi"))
            {
                EdiDataReader r = new EdiDataReader();
                EdiBatch      b = r.FromStream(s);

                Assert.AreEqual(1, b.Interchanges.Count);
                Assert.AreEqual(1, b.Interchanges[0].Groups.Count);
                Assert.AreEqual(1, b.Interchanges[0].Groups[0].Transactions.Count);

                Assert.AreEqual(1, b.Interchanges.Count);
                Assert.AreEqual(1, b.Interchanges[0].Groups.Count);
                Assert.AreEqual(1, b.Interchanges[0].Groups[0].Transactions.Count);

                EdiTrans t = b.Interchanges[0].Groups[0].Transactions[0];

                int rootSegCount   = t.Content.Count(seg => seg is EdiSegment);
                int rootLoopCount  = t.Content.Count(l => l is EdiLoop);
                int n1LoopCount    = t.Content.Count(l => l.Definition.GetType() == typeof(M_850.L_N1));
                int n1ContentCount = ((EdiLoop)t.Content.First(l => l.Definition.GetType() == typeof(M_850.L_N1))).Content.Count();

                int po1LoopCount = t.Content.Count(l => l.Definition.GetType() == typeof(M_850.L_PO1));
                int pidLoopCount = t.Content.Where(l => l.Definition.GetType() == typeof(M_850.L_PO1))
                                   .Select(l2 => l2.Definition.GetType() == typeof(M_850.L_PID)).Count();

                int po1Count = 0;
                int pidCount = 0;
                int po4Count = 0;
                var lst      = t.Content.Where(l => l.Definition.GetType() == typeof(M_850.L_PO1)).Select(l => l).ToList();
                lst.ForEach(l =>
                {
                    var po1Loop = (EdiLoop)l;
                    if (po1Loop.Content[0].Definition.GetType() == typeof(PO1))
                    {
                        po1Count++;
                    }

                    if (po1Loop.Content[2].Definition.GetType() == typeof(PO4))
                    {
                        po4Count++;
                    }

                    EdiLoop pidLoop = (EdiLoop)po1Loop.Content.First(l2 => l2.Definition.GetType() == typeof(M_850.L_PID));
                    if (pidLoop.Content[0].Definition.GetType() == typeof(PID))
                    {
                        pidCount++;
                    }
                });

                int cttLoopCount = t.Content.Count(l => l.Definition.GetType() == typeof(M_850.L_CTT));
                int cttCount     = ((EdiLoop)t.Content.First(l => l.Definition.GetType() == typeof(M_850.L_CTT)))
                                   .Content.Count(s2 => s2.Definition.GetType() == typeof(CTT));


                Assert.AreEqual(0, t.ValidationErrors.Count);

                Assert.AreEqual(8, rootSegCount);
                Assert.AreEqual(8, rootLoopCount);
                Assert.AreEqual(1, n1LoopCount);
                Assert.AreEqual(3, n1ContentCount);
                Assert.AreEqual(6, po1LoopCount);
                Assert.AreEqual(6, po1Count);
                Assert.AreEqual(6, pidLoopCount);
                Assert.AreEqual(6, pidCount);
                Assert.AreEqual(6, po4Count);
                Assert.AreEqual(1, cttLoopCount);
                Assert.AreEqual(1, cttCount);
            }
        }
Example #9
0
        public EdiBatch GetnerateAcknowledgment(EdiBatch input)
        {
            string mapVersion = null;
            var    firstGroup = input?.Interchanges?.FirstOrDefault()?.Groups?.FirstOrDefault();

            if (firstGroup != null)
            {
                mapVersion = firstGroup.GS.Content[7].Val;
            }

            if (string.IsNullOrWhiteSpace(mapVersion))
            {
                throw new InvalidDataException(
                          "Can not determine EDI X12 version from batch. Check there is at least one interchange with at least one group in it");
            }

            EdiBatch b997 = new EdiBatch();

            foreach (EdiInterchange ich in input.Interchanges)
            {
                var ich997 = new EdiInterchange();
                b997.Interchanges.Add(ich997);

                string asmName  = $"EdiEngine.Standards.X12_{mapVersion}";
                string typeName = $"{asmName}.Maps.M_997";

                var map     = (MapLoop)Activator.CreateInstance(asmName, typeName).Unwrap();
                var lAk2Def = (MapLoop)map.Content.First(s => s.Name == "L_AK2");

                foreach (EdiGroup g in ich.Groups)
                {
                    var g997 = new EdiGroup("FA");
                    ich997.Groups.Add(g997);

                    var t997 = new EdiTrans(map);
                    g997.Transactions.Add(t997);

                    int includedTranCount = int.Parse(g.GE.Content[0].Val);
                    int receivedTranCount = g.Transactions.Count;
                    int acceptedTranCount = g.Transactions.Count(t => !t.ValidationErrors.Any());

                    t997.Content.Add(CreateAk1Segment(map, g));

                    if (((_settings.AckValidationErrorBehavour == AckValidationErrorBehavour.AcceptButNoteErrors ||
                          _settings.AckValidationErrorBehavour == AckValidationErrorBehavour.RejectValidationErrors) &&
                         acceptedTranCount < receivedTranCount) ||
                        (_settings.AlwaysGenerateAk2Loop))
                    {
                        foreach (EdiTrans t in g.Transactions)
                        {
                            EdiLoop lAk2 = new EdiLoop(lAk2Def, null);
                            t997.Content.Add(lAk2);

                            lAk2.Content.Add(CreateAk2Segment(lAk2Def, t));
                            lAk2.Content.Add(CreateAk5Segment(lAk2Def, t));
                        }
                    }


                    t997.Content.Add(CreateAk9Segment(map, g, includedTranCount, receivedTranCount));
                }
            }

            return(b997);
        }
Example #10
0
        public void EdiWriter_CreateEdi940()
        {
            M_940    map = new M_940();
            EdiTrans t   = new EdiTrans(map);

            // W05
            var sDef = (MapSegment)map.Content.First(s => s.Name == "W05");

            var seg = new EdiSegment(sDef);

            seg.Content.AddRange(new[]
            {
                new EdiDataElement(sDef.Content[0], "N"),
                new EdiDataElement(sDef.Content[1], "538686"),
                new EdiDataElement(sDef.Content[2], null),
                new EdiDataElement(sDef.Content[3], "001001"),
                new EdiDataElement(sDef.Content[4], "538686")
            });
            t.Content.Add(seg);

            //LX*1
            var lDef = (MapLoop)map.Content.First(s => s.Name == "L_LX");

            sDef = (MapSegment)lDef.Content.First(s => s.Name == "LX");

            EdiLoop lx = new EdiLoop(lDef, null);

            t.Content.Add(lx);

            seg = new EdiSegment(sDef);
            seg.Content.Add(new EdiDataElement(sDef.Content[0], "1"));
            lx.Content.Add(seg);

            //LX > W01 loop
            lDef = (MapLoop)lDef.Content.First(s => s.Name == "L_W01");
            sDef = (MapSegment)lDef.Content.First(s => s.Name == "W01");

            EdiLoop w01 = new EdiLoop(lDef, null);

            lx.Content.Add(w01);

            seg = new EdiSegment(sDef);
            seg.Content.AddRange(new[]
            {
                new EdiDataElement(sDef.Content[0], "12"),
                new EdiDataElement(sDef.Content[1], "CA"),
                new EdiDataElement(sDef.Content[1], "000100033330")
            });
            w01.Content.Add(seg);

            //LX*2
            lDef = (MapLoop)map.Content.First(s => s.Name == "L_LX");
            sDef = (MapSegment)lDef.Content.First(s => s.Name == "LX");

            lx = new EdiLoop(lDef, null);
            t.Content.Add(lx);

            seg = new EdiSegment(sDef);
            seg.Content.Add(new EdiDataElement(sDef.Content[0], "2"));
            lx.Content.Add(seg);

            //LX > W01 loop
            lDef = (MapLoop)lDef.Content.First(s => s.Name == "L_W01");
            sDef = (MapSegment)lDef.Content.First(s => s.Name == "W01");

            w01 = new EdiLoop(lDef, null);
            lx.Content.Add(w01);

            seg = new EdiSegment(sDef);
            seg.Content.AddRange(new[]
            {
                new EdiDataElement(sDef.Content[0], "10"),
                new EdiDataElement(sDef.Content[1], "CA"),
                new EdiDataElement(sDef.Content[1], "000100033332")
            });
            w01.Content.Add(seg);

            //write test envelope
            string data = TestUtils.WriteEdiEnvelope(t, "OW");

            //read produced results and check for errors.
            EdiDataReader r     = new EdiDataReader();
            EdiBatch      batch = r.FromString(data);

            Assert.AreEqual(1, batch.Interchanges.Count);
            Assert.AreEqual(0, batch.Interchanges[0].ValidationErrors.Count);

            Assert.AreEqual(1, batch.Interchanges[0].Groups.Count);
            Assert.AreEqual(0, batch.Interchanges[0].Groups[0].ValidationErrors.Count);

            EdiTrans trans = batch.Interchanges[0].Groups[0].Transactions[0];

            Assert.AreEqual(0, trans.ValidationErrors.Count);
        }
        public string X12_855(Edi_SalesOrder_855 sapSalesOrder, string mode, string receiver)
        {
            M_855    map = new M_855();
            EdiTrans t   = new EdiTrans(map);

            var sDef = (MapSegment)map.Content.First(s => s.Name == "BAK");

            var seg = new EdiSegment(sDef);

            seg.Content.AddRange(new[]
            {
                new EdiSimpleDataElement((MapSimpleDataElement)sDef.Content[0], "00"),
                new EdiSimpleDataElement((MapSimpleDataElement)sDef.Content[1], sapSalesOrder.Header.Status),     //AD:No change AC:with change RD:rejected
                new EdiSimpleDataElement((MapSimpleDataElement)sDef.Content[2], sapSalesOrder.Header.PURCH_NO_C), //set 850 order number
                new EdiSimpleDataElement((MapSimpleDataElement)sDef.Content[3], sapSalesOrder.Header.PURCH_DATE),
                new EdiSimpleDataElement((MapSimpleDataElement)sDef.Content[7], sapSalesOrder.Header.OrderNumber),
                new EdiSimpleDataElement((MapSimpleDataElement)sDef.Content[8], sapSalesOrder.Header.DateTime)
            });
            t.Content.Add(seg);
            //create segment
            var lDef = (MapLoop)map.Content.First(s => s.Name == "L_PO1");

            sDef = (MapSegment)lDef.Content.First(s => s.Name == "PO1");

            EdiLoop p01 = new EdiLoop(lDef, null);

            t.Content.Add(p01);
            //get sap order price?
            foreach (Edi_SalesItem_855 i in sapSalesOrder.ItemList)
            {
                seg = new EdiSegment(sDef);
                seg.Content.AddRange(new[]
                {
                    new EdiSimpleDataElement((MapSimpleDataElement)sDef.Content[0], i.CustomerItemNumber),
                    new EdiSimpleDataElement((MapSimpleDataElement)sDef.Content[1], i.TARGET_QTY),
                    new EdiSimpleDataElement((MapSimpleDataElement)sDef.Content[2], i.CustomerUnit),        //ok
                    new EdiSimpleDataElement((MapSimpleDataElement)sDef.Content[3], i.Price),
                    new EdiSimpleDataElement((MapSimpleDataElement)sDef.Content[4], i.CustomerUnitOfPrice), //ok
                    new EdiSimpleDataElement((MapSimpleDataElement)sDef.Content[5], "BP"),
                    new EdiSimpleDataElement((MapSimpleDataElement)sDef.Content[6], i.CUST_MAT35),
                    new EdiSimpleDataElement((MapSimpleDataElement)sDef.Content[7], "VP"),
                    new EdiSimpleDataElement((MapSimpleDataElement)sDef.Content[8], i.MATERIAL)
                });
                p01.Content.Add(seg);

                var lDef_L_ACK = (MapLoop)lDef.Content.First(s => s.Name == "L_ACK");
                var sDef_ACK   = (MapSegment)lDef_L_ACK.Content.First(s => s.Name == "ACK");

                EdiLoop w = new EdiLoop(lDef_L_ACK, p01);
                p01.Content.Add(w);
                foreach (Edi_SalesSchedule_855 j in sapSalesOrder.ScheduleList)
                {
                    if (i.ITM_NUMBER == j.ITM_NUMBER)
                    {
                        seg = new EdiSegment(sDef_ACK);
                        seg.Content.AddRange(new[]
                        {
                            new EdiSimpleDataElement((MapSimpleDataElement)sDef.Content[0], j.Status),       //IA:accept IR:reject
                            new EdiSimpleDataElement((MapSimpleDataElement)sDef.Content[1], j.REQ_QTY),
                            new EdiSimpleDataElement((MapSimpleDataElement)sDef.Content[2], i.CustomerUnit), //ok
                            new EdiSimpleDataElement((MapSimpleDataElement)sDef.Content[2], j.DateTimeCode),
                            new EdiSimpleDataElement((MapSimpleDataElement)sDef.Content[2], j.DateTime)
                        });
                        w.Content.Add(seg);
                    }
                }
            }
            string data = writeEdiEnvelope(t, "PR", mode, receiver);
            //read produced results and check for errors.
            EdiDataReader r     = new EdiDataReader();
            EdiBatch      batch = r.FromString(data);
            EdiTrans      trans = batch.Interchanges[0].Groups[0].Transactions[0];

            return(data);
        }