Ejemplo n.º 1
0
 /// <summary>
 /// Initializes a new instance of the <see cref="AckEventArgs{T, U}"/> class.
 /// </summary>
 /// <param name="interchangeHeader">The interchange header.</param>
 /// <param name="groupHeader">The group header.</param>
 /// <param name="message">The EDI message.</param>
 /// <param name="ackType">The acknowledgment type.</param>
 protected AckEventArgs(T interchangeHeader, U groupHeader, EdiMessage message, AckType ackType)
 {
     InterchangeHeader = interchangeHeader;
     GroupHeader       = groupHeader;
     Message           = message;
     AckType           = ackType;
 }
Ejemplo n.º 2
0
        public static async Task <string> CreateTransaction(EdiMessage message, string controlNumber,
                                                            string senderId      = "SENDER1",
                                                            string receiverId    = "RECEIVER1",
                                                            bool ackRequested    = true,
                                                            string testIndicator = "T")
        {
            //var sb = new StringBuilder();
            using (var stream = new MemoryStream())
            {
                using (var writer = new X12Writer(stream, new X12WriterSettings(Separators.X12)))
                {
                    var isa = BuildIsa(controlNumber, senderId, "14", receiverId, "16", ackRequested ? "1" : "0",
                                       testIndicator);
                    // construct the interchange header ...
                    await writer.WriteAsync(isa);

                    var gs = BuildGs(controlNumber, senderId, receiverId);
                    await writer.WriteAsync(gs);


                    await writer.WriteAsync(message);
                }

                stream.Position = 0;
                StreamReader reader = new StreamReader(stream);
                string       text   = reader.ReadToEnd();

                return(text);
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Writes a message to the destination.
        /// </summary>
        /// <param name="message">The message to write.</param>
        public override void Write(EdiMessage message)
        {
            if (_separators == null)
            {
                throw new Exception("No interchange was started.");
            }

            _messageCounter++;

            var segmentCounter = 0;
            var transactionSet = new TransactionSet(message.GetStandardType(), message);

            transactionSet.RemoveTrailer(MessageTrailer);

            foreach (var segment in transactionSet.Descendants <Segment>())
            {
                Write(segment.Generate(_separators, PreserveWhitespace));
                segmentCounter++;
            }

            if (message.Name == "TA1")
            {
                return;
            }

            segmentCounter++;
            var trailer = BuildTrailer(MessageTrailer, message.ControlNumber, segmentCounter);

            Write(trailer);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Reads an EDI message from the stream.
        /// </summary>
        /// <returns>If a new message was read.</returns>
        public bool ReadMessage()
        {
            var currentMessage = new List <SegmentContext>();
            var result         = false;

            while (_streamReader.Peek() >= 0 && !result)
            {
                var currentSegment = ReadSegment();
                if (string.IsNullOrEmpty(currentSegment))
                {
                    break;
                }

                var segmentContext = new SegmentContext(currentSegment, _separators);
                switch (segmentContext.Tag)
                {
                case SegmentTags.UNA:
                    break;

                case SegmentTags.UNZ:
                case SegmentTags.IEA:
                    _interchangeHeader = null;
                    _separators        = null;
                    break;

                case SegmentTags.UNE:
                case SegmentTags.GE:
                    _groupHeader = null;
                    break;

                case SegmentTags.UNB:
                case SegmentTags.ISA:
                    _interchangeHeader = segmentContext;
                    break;

                case SegmentTags.UNG:
                case SegmentTags.GS:
                    _groupHeader = segmentContext;
                    break;

                case SegmentTags.UNT:
                case SegmentTags.SE:
                    currentMessage.Add(segmentContext);
                    if (segmentContext.Tag == SegmentTags.SE)
                    {
                        currentMessage.Add(_groupHeader);
                    }
                    Message = ReadMessage(segmentContext, currentMessage);
                    result  = true;
                    currentMessage.Clear();
                    break;

                default:
                    currentMessage.Add(segmentContext);
                    break;
                }
            }

            return(result);
        }
Ejemplo n.º 5
0
        public override void Write(EdiMessage message)
        {
            var transactionSet = new TransactionSet(message.GetStandardType(), message);

            foreach (var segment in transactionSet.Descendants <Segment>())
            {
                Write(segment.Generate(Pad));
            }
        }
Ejemplo n.º 6
0
 /// <summary>
 /// Initializes a new instance of the <see cref="MessageEventArgs{T, U}"/> class.
 /// </summary>
 /// <param name="message">The EDI message.</param>
 /// <param name="errorContext">The message error context.</param>
 /// <param name="groupHeader">The group header.</param>
 /// <param name="interchangeHeader">The interchange header.</param>
 /// <param name="inDuplicateGroup">Detects if the message is part of a duplicate group.</param>
 /// <param name="inDuplicateInterchange">Detects if the message is part of a duplicate interchange.</param>
 protected MessageEventArgs(T interchangeHeader, U groupHeader, EdiMessage message, MessageErrorContext errorContext,
                            bool inDuplicateGroup, bool inDuplicateInterchange)
 {
     InterchangeHeader      = interchangeHeader;
     GroupHeader            = groupHeader;
     Message                = message;
     ErrorContext           = errorContext;
     InDuplicateGroup       = inDuplicateGroup;
     InDuplicateInterchange = inDuplicateInterchange;
 }
Ejemplo n.º 7
0
        public static string BuildAck(ISA originalIsa, GS originalGs, EdiMessage ack, AckVersion ackVersion, int isaControlNumber = 1, int gsControlNumber = 1)
        {
            var memoryStream = new MemoryStream();
            var writer       = new X12Writer(memoryStream, Encoding.Default, Environment.NewLine);

            writer.Write(originalIsa.ToAckIsa(isaControlNumber.ToString().PadLeft(9, '0'), ackVersion));
            writer.Write(originalGs.ToAckGs(gsControlNumber.ToString(), ackVersion));
            writer.Write(ack);
            writer.Flush();
            memoryStream.Position = 0;
            using (var reader = new StreamReader(memoryStream))
                return(reader.ReadToEnd());
        }
Ejemplo n.º 8
0
        public static XDocument SerializeDataContract(this EdiMessage instance)
        {
            if (instance == null)
            {
                throw new ArgumentNullException("instance");
            }

            var serializer = new DataContractSerializer(instance.GetType());

            using (var ms = new MemoryStream())
            {
                serializer.WriteObject(ms, instance);
                ms.Position = 0;
                return(XDocument.Load(ms, LoadOptions.PreserveWhitespace));
            }
        }
Ejemplo n.º 9
0
        private static string BuildAck(UNB originalUnb, UNG originalUng, EdiMessage ack, int unbControlNumber = 1, int ungControlNumber = 1)
        {
            var memoryStream = new MemoryStream();
            var writer       = new EdifactWriter(memoryStream, Encoding.Default, Environment.NewLine);

            writer.Write(originalUnb.ToAckUnb(unbControlNumber.ToString()));
            if (originalUng != null)
            {
                writer.Write(originalUng.ToAckUng(ungControlNumber.ToString()));
            }
            writer.Write(ack);
            writer.Flush();
            memoryStream.Position = 0;
            using (var reader = new StreamReader(memoryStream))
                return(reader.ReadToEnd());
        }
Ejemplo n.º 10
0
        public static XDocument Serialize(EdiMessage instance, Encoding encoding = null)
        {
            if (instance == null)
            {
                throw new ArgumentNullException("instance");
            }

            var serializer = new XmlSerializer(instance.GetType());

            using (var ms = new MemoryStream())
            {
                serializer.Serialize(ms, instance);
                ms.Position = 0;
                var str = LoadString(ms, encoding);
                return(XDocument.Parse(str));
            }
        }
Ejemplo n.º 11
0
        public async Task Send(string senderCompanyId, TradingChannel.TradingChannel channel, TradingPartner.TradingPartner partner,
                               string subject, EdiMessage message, string referenceId)
        {
            var sender = await _companyRepo.Find(senderCompanyId);

            partner.CurrentControlNumber++;
            await _partnerRepo.Save(partner);

            await _messageRepo.Save(new TradingPartnerMessage.TradingPartnerMessage()
            {
                Type    = TradingPartnerMessageType.Outgoing,
                Status  = TradingPartnerMessageStatus.Queued,
                Content = await EdiUtils.CreateTransaction(message,
                                                           partner.CurrentControlNumber.ToString().PadLeft(9, '0'),
                                                           sender.IsaId, partner.IsaId, partner.RequestAck, partner.TestMode ? "T":"P"),
                ReferenceId      = referenceId,
                ContentType      = "EDI",
                Subject          = subject,
                TradingChannelId = channel.Id,
                TradingPartnerId = partner.Id
            });
        }
Ejemplo n.º 12
0
 /// <summary>
 /// Writes a message to the destination.
 /// </summary>
 /// <param name="message">The message to write.</param>
 public abstract void Write(EdiMessage message);
Ejemplo n.º 13
0
        public EdiMessage ReadNextMessage()
        {
            EdiMessage result = null;

            if (sp_MessageRead == null)
            {
                CreateSP_MessageRead();
            }

            sp_MessageRead.Transaction = connectionProvider.CurrentTransaction;

            lock (syncLock)
            {
                currentCommand = sp_MessageRead;
            }

            // Set In Parameters

            // Execute stored procedure

            sp_MessageRead.Prepare();
            sp_MessageRead.ExecuteNonQuery();

            // Set Out Parameters

            if ((sp_MessageRead.Parameters["EDIOUTID_O"] as IDbDataParameter).Value != DBNull.Value)
            {
                result = new EdiMessage();

                result.EdiOutId = Convert <double>((sp_MessageRead.Parameters["EDIOUTID_O"] as IDbDataParameter).Value);

                if ((sp_MessageRead.Parameters["EDIMSGID_O"] as IDbDataParameter).Value == DBNull.Value)
                {
                    result.EdiMessageId = null;
                }
                else
                {
                    result.EdiMessageId = Convert <string>((sp_MessageRead.Parameters["EDIMSGID_O"] as IDbDataParameter).Value);
                }

                if ((sp_MessageRead.Parameters["PRESTRING_O"] as IDbDataParameter).Value == DBNull.Value)
                {
                    result.PreString = null;
                }
                else
                {
                    result.PreString = Convert <string>((sp_MessageRead.Parameters["PRESTRING_O"] as IDbDataParameter).Value);
                }

                if ((sp_MessageRead.Parameters["POSTSTRING_O"] as IDbDataParameter).Value == DBNull.Value)
                {
                    result.PostString = null;
                }
                else
                {
                    result.PostString = Convert <string>((sp_MessageRead.Parameters["POSTSTRING_O"] as IDbDataParameter).Value);
                }

                if ((sp_MessageRead.Parameters["SNDDIR_O"] as IDbDataParameter).Value == DBNull.Value)
                {
                    result.SendDirectory = null;
                }
                else
                {
                    result.SendDirectory = Convert <string>((sp_MessageRead.Parameters["SNDDIR_O"] as IDbDataParameter).Value);
                }
            }

            return(result);
        }
Ejemplo n.º 14
0
 public async Task Send(string senderCompanyId, string channelId, string partnerId, string subject, EdiMessage message, string referenceId)
 {
     await Send(senderCompanyId, await _channelRepo.Find(channelId), await _partnerRepo.Find(partnerId), subject, message, referenceId);
 }
Ejemplo n.º 15
0
        public async Task Send(string senderCompanyId, string partnerId, string subject, EdiMessage message, string referenceId)
        {
            var partner = await _partnerRepo.Find(partnerId);

            var channel = await _channelRepo.Find(partner.ChannelId);

            await Send(senderCompanyId, channel, partner, subject, message, referenceId);
        }