Ejemplo n.º 1
0
        /// <summary>
        /// Encode un message.
        /// </summary>
        /// <param name="message">Message à encoder.</param>
        /// <returns></returns>
        public static string Encode(IMessage message)
        {
            StringBuilder retMessage = new StringBuilder();

            for (int i = 0; i < message.CountStructure; i++)
            {
                int         pos  = i + 1;
                MessageItem item = message.GetStructure(pos);

                if (item.Repetitions.Count > 0)
                {
                    foreach (ISegment segment in item.Repetitions)
                    {
                        string segString = PipeParser.Encode(segment, message.EncodingCharacters);
                        if (!string.IsNullOrWhiteSpace(segString) && segString.Length >= 4)
                        {
                            retMessage.Append(segString);
                            retMessage.Append(PipeParser.SEG_DELIMITER);
                        }
                    }
                }
            }

            return(retMessage.ToString());
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Encode un segment.
        /// </summary>
        /// <param name="segment">Segment à encoder.</param>
        /// <param name="encChars">Caractères d'encodage utilisés.</param>
        /// <returns></returns>
        public static string Encode(ISegment segment, EncodingCharacters encChars)
        {
            StringBuilder retSegment = new StringBuilder();

            // Code du segment et premier séparateur de champ
            retSegment.Append(segment.SegmentName);
            retSegment.Append(encChars.FieldSeparator);

            // Si segment MSH : position de départ sur MSH-2 car MSH-1 correspond au séparateur de champ
            int startPos = InteropUtil.IsSegmentDefDelimiters(segment.SegmentName) ? 2 : 1;

            // Parcours des champs
            for (int i = startPos; i <= segment.Fields.Count; i++)
            {
                try
                {
                    // Parcours des répétitions
                    IType[] repetitions = segment.GetField(i);

                    for (int j = 0; j < repetitions.Length; j++)
                    {
                        string repValue = PipeParser.Encode(repetitions[j], encChars);

                        // Si MSH-2 : il faut annuler l'échappement des caractères réservés
                        if (InteropUtil.IsSegmentDefDelimiters(segment.SegmentName) && i == 2)
                        {
                            repValue = EscapeCharacterUtil.Unescape(repValue, encChars);
                        }

                        retSegment.Append(repValue);

                        if (j < repetitions.Length - 1)
                        {
                            retSegment.Append(encChars.RepetitionSeparator);
                        }
                    }
                }
                catch
                {
                    throw;
                }

                retSegment.Append(encChars.FieldSeparator);
            }

            return(InteropUtil.RemoveExtraDelimiters(retSegment.ToString(), encChars.FieldSeparator));
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Encode du type de données.
        /// </summary>
        /// <param name="type">Type de données à encoder.</param>
        /// <param name="encChars">Caractères d'encodage utilisés.</param>
        /// <param name="subComponent">Indique si l'on encode un sous-composant.</param>
        /// <returns></returns>
        public static string Encode(IType type, EncodingCharacters encChars, bool subComponent = false)
        {
            StringBuilder retType = new StringBuilder();

            if (type is ITypePrimitive)
            {
                ITypePrimitive primitive = type as ITypePrimitive;
                if (primitive == null)
                {
                    throw new EncodingException("Une erreur s'est produite à la conversion de 'IType' vers 'ITypePrimitive'.");
                }
                else
                {
                    retType.Append(PipeParser.Encode(primitive, encChars));
                }
            }
            else
            {
                ITypeComposite composite = type as ITypeComposite;
                if (composite == null)
                {
                    throw new EncodingException("Une erreur s'est produite à la conversion de 'IType' vers 'ITypeComposite'.");
                }
                else
                {
                    StringBuilder retComp       = new StringBuilder();
                    char          compDelimiter = subComponent ? encChars.SubComponentSeparator : encChars.ComponentSeparator;

                    for (int i = 0; i < composite.Components.Length; i++)
                    {
                        retComp.Append(PipeParser.Encode(composite.Components[i], encChars, true));
                        if (i < composite.Components.Length - 1)
                        {
                            retComp.Append(compDelimiter);
                        }
                    }

                    retType.Append(InteropUtil.RemoveExtraDelimiters(retComp.ToString(), compDelimiter));
                }
            }

            return(retType.ToString());
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Encodage d'un message.
        /// </summary>
        /// <param name="message">Message à encoder.</param>
        /// <param name="createFiles">Indique si les fichiers doivent être créés.</param>
        /// <returns></returns>
        public string Encode(IMessage message, bool createFiles = true)
        {
            if (createFiles && (this._options.ExportPaths == null || !this._options.ExportPaths.Any()))
            {
                throw new EncodingException("Aucun chemin d'export n'a été renseigné.");
            }

            string dtEvent = DateTimeUtil.DateSecondeToString();

            #region Champs variables - MSH

            // MSH-1
            message.MSH.FieldSeparator.Value = this._options.EncodingChars.FieldSeparator.ToString();

            // MSH-2
            message.MSH.EncodingCharacters.Value = this._options.EncodingChars.ToString();

            // MSH-7
            message.MSH.DateTimeOfMessage.Time.Value = dtEvent;

            // MSH-10
            if (this._options.MsgControlIdInMilliseconds)
            {
                message.MSH.MessageControlId.Value = DateTimeOffset.Now.ToUnixTimeMilliseconds().ToString();
            }
            else
            {
                message.MSH.MessageControlId.Value = DateTimeOffset.Now.ToUnixTimeSeconds().ToString();
            }

            // MSH-11
#if DEBUG
            message.MSH.ProcessingId.ProcessingId.Value = ProcessingID.DEBUGGING;
#else
            message.MSH.ProcessingId.ProcessingId.Value = ProcessingID.PRODUCTION;
#endif

            // MSH-12
            message.MSH.VersionId.VersionId.Value = this._options.Version;
            message.MSH.VersionId.InternationalizationCode.Identifier.Value = this._options.CountryCode;
            message.MSH.VersionId.InternationalizationCode.Text.Value       = CountryCode.Description[this._options.CountryCode];
            message.MSH.VersionId.InternationalVersionId.Identifier.Value   = this._options.Version;

            // MSH-17
            message.MSH.CountryCode.Value = this._options.CountryCode;

            #endregion

            #region Champs variables - EVN

            // EVN-2
            message.EVN.RecordedDateTime.Time.Value = dtEvent;

            // TODO: initialize EVN-5
            if (message.EventOperator != null)
            {
            }

            #endregion

            #region Encodage, création des fichiers et retour

            string encodingText = PipeParser.Encode(message);

            if (createFiles)
            {
                foreach (string path in this._options.ExportPaths)
                {
                    // TODO: pouvoir choisir de préfixer/suffixer avec le type d'événement (A28, A01, ...)
                    string fileName      = DateTimeOffset.Now.ToUnixTimeMilliseconds().ToString();
                    bool   errCreateFile = false;

                    try
                    {
                        FileUtil.CreateFile(fileName, this._options.FileExtension, encodingText, path);
                    }
                    catch
                    {
                        errCreateFile = true;
                        throw;
                    }
                    finally
                    {
                        if (!errCreateFile && this._options.CreateValidationFile)
                        {
                            FileUtil.CreateFile(fileName, this._options.ValidationFileExtension, encodingText, path);
                        }
                    }
                }
            }

            return(encodingText);

            #endregion
        }